summaryrefslogtreecommitdiff
path: root/support/dktools/dk4bb.c
blob: 96c437c05cbddb5671278b8b6bc5e88d5e3a9814 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
/*
Copyright (C) 2017-2020, Dirk Krause
SPDX-License-Identifier: BSD-3-Clause
*/

/*
	WARNING: This file was generated by the dkct program (see
	http://dktools.sourceforge.net/ for details).
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk4bb.ctr
*/

/**	@file dk4bb.c The dk4bb module.
*/


#line 241 "dk4bb.ctr"

#include "dk4conf.h"
#include "dk4bb.h"
#if	DK4_HAVE_STDLIB_H
#ifndef	STDLIB_H_INCLUDED
#include <stdlib.h>
#define	STDLIB_H_INCLUDED 1
#endif
#endif

#if	DK4_HAVE_MATH_H
#ifndef	MATH_H_INCLUDED
#if	DK4_ON_WINDOWS
#ifndef	_USE_MATH_DEFINES
#define	_USE_MATH_DEFINES 1
#endif
#endif
#include <math.h>
#define	MATH_H_INCLUDED 1
#endif
#endif

#ifndef	DK4MEM_H_INCLUDED
#include "dk4mem.h"
#endif

#ifndef	DK4MATH_H_INCLUDED
#include "dk4math.h"
#endif

#if DK4_HAVE_ASSERT_H
#ifndef	ASSERT_H_INCLUDED
#include <assert.h>
#define	ASSERT_H_INCLUDED 1
#endif
#endif




#line 280 "dk4bb.ctr"


/**	Flag value to indicate information not present.
*/
static const unsigned char	have_not	= ((unsigned char)0U);

/**	Flag value to indicate X information presence.
*/
static const unsigned char	have_x		= ((unsigned char)1U);

/**	Flag value to indicate Y information presence.
*/
static const unsigned char	have_y		= ((unsigned char)2U);

/**	Flag value to check for information about both directions.
*/
static const unsigned char	have_both	= ((unsigned char)3U);


/**	Internally add an x value to bounding box.
	@param	bbptr	Bounding box to modify.
	@param	x		X value to add.
*/

static
void
dk4bb_i_add_x(
	dk4_bb_t		*bbptr,
	double			 x
)
{
	

#line 312 "dk4bb.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != bbptr);
#endif
	if (have_not != (have_x & (bbptr->fl))) {
		

#line 317 "dk4bb.ctr"
		if (x < bbptr->xmin) {
			bbptr->xmin = x;			

#line 319 "dk4bb.ctr"
		}
		if (x > bbptr->xmax) {
			bbptr->xmax = x;			

#line 322 "dk4bb.ctr"
		}
	}
	else {								

#line 325 "dk4bb.ctr"
		bbptr->xmin = bbptr->xmax = x;
		bbptr->fl |= have_x;
	}
	

#line 329 "dk4bb.ctr"
}



/**	Internally add an y value to bounding box.
	@param	bbptr	Bounding box to modify.
	@param	y		Y value to add.
*/

static
void
dk4bb_i_add_y(
	dk4_bb_t		*bbptr,
	double			 y
)
{
	

#line 346 "dk4bb.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != bbptr);
#endif
	if (have_not != (have_y & (bbptr->fl))) {
		

#line 351 "dk4bb.ctr"
		if (y < bbptr->ymin) {			

#line 352 "dk4bb.ctr"
			bbptr->ymin = y;
		}
		if (y > bbptr->ymax) {			

#line 355 "dk4bb.ctr"
			bbptr->ymax = y;
		}
	}
	else {								

#line 359 "dk4bb.ctr"
		bbptr->ymin = bbptr->ymax = y;
		bbptr->fl |= have_y;
	}
	

#line 363 "dk4bb.ctr"
}



/*
	The formula to calculate x for a given t in range 0 to 1 is
	x(t) = (1-t)^3*x0 + 3*(1-t)^2*t*x0c + 3*(1-t)*t^2*x1c + t^3*x1
	with
	x0	Left point coordinate
	x0c	Left control point coordinate
	x1c	Right control point coordinate
	x1	Right point coordinate.
	The ^ operator is used as power here (t^3 is t*t*t).

	The formula for y is similar.
*/

/**	Calculate coordinate value for a t value, save to minimum and/or
	maximum if necessary.
	@param	pmin	Address of bounding box minimum component.
	@param	pmax	Address of bounding box maximum component.
	@param	pflags	Address of bounding box flags component.
	@param	flval	Flag bit for test (1=x, 2=y).
	@param	x0		Left point value.
	@param	x0c		Left control point value.
	@param	x1c		Right control point value.
	@param	x1		Right point value.
	@param	t		Variable t value.
*/

static
void
dk4bb_i_apply_t_bezier(
	double			*pmin,
	double			*pmax,
	unsigned char	*pflags,
	unsigned char	 flval,
	double			 x0,
	double			 x0c,
	double			 x1c,
	double			 x1,
	double			 t
)
{
	double	omt;	/* 1 - t */
	double	x;		/* x value */
	

#line 410 "dk4bb.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != pmin);
	assert(NULL != pmax);
	assert(NULL != pflags);
#endif
#if TRACE_DEBUG
	if (isfinite(t)) {	

#line 417 "dk4bb.ctr"
	}
	else {				

#line 419 "dk4bb.ctr"
	}
#endif
	if ( isgreaterequal(t, 0.0) ) {
		if ( islessequal(t, 1.0) ) {		

#line 423 "dk4bb.ctr"
			omt = 1.0 - t;
			x =	omt * omt * omt * x0
				+ 3.0 * omt * omt * t * x0c
				+ 3.0 * omt * t * t * x1c
				+ t * t * t * x1;			

#line 428 "dk4bb.ctr"
			if (0 != ((*pflags) & flval)) {	

#line 429 "dk4bb.ctr"
				if (isless(x, *pmin))	{	

#line 430 "dk4bb.ctr"
					*pmin = x;
				}
				if (isgreater(x, *pmax)) {	

#line 433 "dk4bb.ctr"
					*pmax = x;
				}
			}
			else {							

#line 437 "dk4bb.ctr"
				*pmin = x;
				*pmax = x;
				*pflags |= flval;
			}
		}
#if TRACE_DEBUG
		else {	

#line 444 "dk4bb.ctr"
		}
#endif
	}
#if TRACE_DEBUG
	else {		

#line 449 "dk4bb.ctr"
	}
#endif
	

#line 452 "dk4bb.ctr"
}



/*
	The formula used here was found using wxMaxima, input was

	x: (1-t)^3*x0 + 3*(1-t)^2*t*x0c + 3*(1-t)*t^2*x1c + t^3*x1;
	solve([diff(x, t)=0],[t]);

	To find minima and maxima values we derive the Bezier curve
	formula and search for zeroes of the derived function.
*/

/**	Check minimum and maximum coordinate value of a Bezier curve,
	save values in bounding box if necessary.
	@param	pmin	Address of bounding box minimum component.
	@param	pmax	Address of bounding box maximum component.
	@param	pflags	Address of bounding box flags component.
	@param	flval	Flag bit for test (1=x, 2=y).
	@param	x0		Left point value.
	@param	x0c		Left control point value.
	@param	x1c		Right control point value.
	@param	x1		Right point value.
*/

static
void
dk4bb_i_apply_bezier(
	double			*pmin,
	double			*pmax,
	unsigned char	*pflags,
	unsigned char	 flval,
	double			 x0,
	double			 x0c,
	double			 x1c,
	double			 x1
)
{
	double	a;			/* Coefficient to third power of t */
	double	b;			/* Coefficient to second power of t */
	double	c;			/* Coefficient to t */
	double	denom;		/* Denominator */
	double	sqop;		/* Operand to square root */
	double	sqv;		/* Square root result */
	double	t1;			/* First extremum */
	double	t2;			/* Second extremum */

#if	DK4_USE_ASSERT
	assert(NULL != pmin);
	assert(NULL != pmax);
	assert(NULL != pflags);
#endif
	a		= x1 - 3.0 * x1c + 3.0 * x0c - x0;
	b		= 3.0 * x0 - 6.0 * x0c + 3.0 * x1c;
	c		= 3.0 * (x0c - x0);
	denom	= 3.0 * a;
	sqop	= b * b - 3.0 * a * c;
	sqv		= sqrt(sqop);
	t1		= (0.0 - b + sqv) / denom;
	t2		= (0.0 - b - sqv) / denom;
#if	DK4_HAVE_ISFINITE
	if ((isfinite(t1)) && (isfinite(t2)))
#else
#if	DK4_HAVE__FINITE
	if ((_finite(t1)) && (_finite(t2)))
#else
#error	"Neither isfinite() nor _finite() available!"
	if(1)
#endif
#endif
	{			

#line 524 "dk4bb.ctr"
		dk4bb_i_apply_t_bezier(pmin, pmax, pflags, flval, x0, x0c, x1c, x1, t1);
		dk4bb_i_apply_t_bezier(pmin, pmax, pflags, flval, x0, x0c, x1c, x1, t2);
	}
	else {		

#line 528 "dk4bb.ctr"
		t1 = (0.0 - c) / (2.0 * b);
#if	DK4_HAVE_ISFINITE
		if (isfinite(t1))
#else
#if	DK4_HAVE__FINITE
		if (_finite(t1))
#else
#error	"Neither isfinite() nor _finite() available!"
		if(1)
#endif
#endif
		{		

#line 540 "dk4bb.ctr"
			dk4bb_i_apply_t_bezier(pmin,pmax,pflags,flval,x0,x0c,x1c,x1,t1);
		}
#if TRACE_DEBUG
		else {	

#line 544 "dk4bb.ctr"
		}
#endif
	}
	

#line 548 "dk4bb.ctr"
}



/**	Internal function to add Bezier spline segment to bounding box.
	The bbptr pointer is already checked to be not NULL.
	@param	bbptr	Bounding box to modify.
	@param	x0		Start point, x coordinate.
	@param	y0		Start point, y coordinate.
	@param	c0x		Control point next to start point, x coordinate.
	@param	c0y		Control point next to start point, y coordinate.
	@param	c1x		Control point next to end point, x coordinate.
	@param	c1y		Control point next to end point, y coordinate.
	@param	x1		End point, x coordinate.
	@param	y1		End point, y coordinate.
*/

static
void
dk4bb_i_bezier(
	dk4_bb_t		*bbptr,
	double			 x0,
	double			 y0,
	double			 c0x,
	double			 c0y,
	double			 c1x,
	double			 c1y,
	double			 x1,
	double			 y1
)
{
	

#line 580 "dk4bb.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != bbptr);
#endif
	dk4bb_i_add_x(bbptr, x0);
	dk4bb_i_add_y(bbptr, y0);
	dk4bb_i_add_x(bbptr, x1);
	dk4bb_i_add_y(bbptr, y1);
	dk4bb_i_apply_bezier(
		&(bbptr->xmin), &(bbptr->xmax), &(bbptr->fl), have_x,
		x0, c0x, c1x, x1
	);
	dk4bb_i_apply_bezier(
		&(bbptr->ymin), &(bbptr->ymax), &(bbptr->fl), have_y,
		y0, c0y, c1y, y1
	);
	

#line 596 "dk4bb.ctr"
}



static
double
dk4bb_sq(double x)
{
	return (x * x);
}


static
void
dk4bb_i_add_rotated_ellipse(
	dk4_bb_t	*bbptr,
	double		 xc,
	double		 yc,
	double		 rx,
	double		 ry,
	double		 rot
)
{
	double	th;
	double	tw;
	double	xw;
	double	yw;
	double	xh;
	double	yh;
	double	h;
	double	w;
	double	beta;
	

#line 629 "dk4bb.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != bbptr);
#endif
	th = atan2((-1.0 * ry), (rx * tan(M_PI - rot)));
	tw = atan2((-1.0 * ry), (rx * tan(M_PI_2 - rot)));
	

#line 635 "dk4bb.ctr"
	xh = rx * cos(th);
	yh = ry * sin(th);
	

#line 638 "dk4bb.ctr"
	xw = rx * cos(tw);
	yw = ry * sin(tw);
	

#line 641 "dk4bb.ctr"
	beta = 0.0 - rot;
	h = sqrt(
		dk4bb_sq(yh - (yh*sin(beta)+xh*cos(beta))*sin(beta))
		+ dk4bb_sq(xh - (yh*sin(beta)+xh*cos(beta))*cos(beta))
	);	

#line 646 "dk4bb.ctr"
	beta += M_PI_2;
	w = sqrt(
		dk4bb_sq(yw - (yw*sin(beta)+xw*cos(beta))*sin(beta))
		+ dk4bb_sq(xw - (yw*sin(beta)+xw*cos(beta))*cos(beta))
	);	

#line 651 "dk4bb.ctr"
	dk4bb_i_add_x(bbptr, xc + w);
	dk4bb_i_add_x(bbptr, xc - w);
	dk4bb_i_add_y(bbptr, yc + h);
	dk4bb_i_add_y(bbptr, yc - h);
	

#line 656 "dk4bb.ctr"
}



void
dk4bb_init(
	dk4_bb_t		*bbptr
)
{
	

#line 666 "dk4bb.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != bbptr);
#endif
	if (NULL != bbptr) {
		DK4_MEMRES(bbptr, sizeof(dk4_bb_t));
		bbptr->xmin = bbptr->xmax = bbptr->ymin = bbptr->ymax = 0.0;
		bbptr->fl = have_not;
	}
#if TRACE_DEBUG
	else {					

#line 676 "dk4bb.ctr"
	}
#endif
	

#line 679 "dk4bb.ctr"
}



void
dk4bb_add_x(
	dk4_bb_t		*bbptr,
	double			 x
)
{
	

#line 690 "dk4bb.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != bbptr);
#endif
	if (NULL != bbptr) {
		dk4bb_i_add_x(bbptr, x);
	}
#if TRACE_DEBUG
	else {			

#line 698 "dk4bb.ctr"
	}
#endif
	

#line 701 "dk4bb.ctr"
}



void
dk4bb_add_y(
	dk4_bb_t		*bbptr,
	double			 y
)
{
	

#line 712 "dk4bb.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != bbptr);
#endif
	if (NULL != bbptr) {
		dk4bb_i_add_y(bbptr, y);
	}
#if TRACE_DEBUG
	else {		

#line 720 "dk4bb.ctr"
	}
#endif
	

#line 723 "dk4bb.ctr"
}



void
dk4bb_add_point(
	dk4_bb_t		*bbptr,
	double			 x,
	double			 y
)
{
	

#line 735 "dk4bb.ctr"
#if	DK4_USE_ASSERT
	assert(NULL != bbptr);
#endif
	if (NULL != bbptr) {
		dk4bb_i_add_x(bbptr, x);
		dk4bb_i_add_y(bbptr, y);
	}
#if TRACE_DEBUG
	else {		

#line 744 "dk4bb.ctr"
	}
#endif
	

#line 747 "dk4bb.ctr"
}



void
dk4bb_add_bezier(
	dk4_bb_t		*bbptr,
	double			 x0,
	double			 y0,
	double			 c0x,
	double			 c0y,
	double			 c1x,
	double			 c1y,
	double			 x1,
	double			 y1
)
{
#if	DK4_USE_ASSERT
	assert(NULL != bbptr);
#endif
	if (NULL != bbptr) {
		dk4bb_i_bezier(bbptr, x0, y0, c0x, c0y, c1x, c1y, x1, y1);
	}
}


void
dk4bb_add_rotated_ellipse(
	dk4_bb_t	*bbptr,
	double		 xc,
	double		 yc,
	double		 rx,
	double		 ry,
	double		 rot
)
{
#if	DK4_USE_ASSERT
	assert(NULL != bbptr);
#endif
	if (NULL != bbptr) {
		dk4bb_i_add_rotated_ellipse(bbptr, xc, yc, rx, ry, rot);
	}
}



void
dk4bb_add_bb(
	dk4_bb_t		*pdst,
	dk4_bb_t const	*psrc
)
{
	if ((NULL != pdst) && (NULL != psrc)) {
		if (have_not != (have_x & (psrc->fl))) {
			dk4bb_i_add_x(pdst, psrc->xmin);
			dk4bb_i_add_x(pdst, psrc->xmax);
		}
		if (have_not != (have_y & (psrc->fl))) {
			dk4bb_i_add_y(pdst, psrc->ymin);
			dk4bb_i_add_y(pdst, psrc->ymax);
		}
	}
}



int
dk4bb_have_x(
	const dk4_bb_t	*bbptr
)
{
#if	DK4_USE_ASSERT
	assert(NULL != bbptr);
#endif
	if (NULL != bbptr) {
		return ((have_not != (have_x & (bbptr->fl))) ? (1) : (0));
	}
	return 0;
}



int
dk4bb_have_y(
	const dk4_bb_t	*bbptr
)
{
#if	DK4_USE_ASSERT
	assert(NULL != bbptr);
#endif
	if (NULL != bbptr) {
		return ((have_not != (have_y & (bbptr->fl))) ? (1) : (0));
	}
	return 0;
}



double
dk4bb_get_xmin(
	const dk4_bb_t	*bbptr
)
{
#if	DK4_USE_ASSERT
	assert(NULL != bbptr);
#endif
	if (NULL != bbptr) {
		return bbptr->xmin;
	}
	return 0.0;
}



double
dk4bb_get_xmax(
	const dk4_bb_t	*bbptr
)
{
#if	DK4_USE_ASSERT
	assert(NULL != bbptr);
#endif
	if (NULL != bbptr) {
		return bbptr->xmax;
	}
	return 0.0;
}



double
dk4bb_get_ymin(
	const dk4_bb_t	*bbptr
)
{
#if	DK4_USE_ASSERT
	assert(NULL != bbptr);
#endif
	if (NULL != bbptr) {
		return bbptr->ymin;
	}
	return 0.0;
}



double
dk4bb_get_ymax(
	const dk4_bb_t	*bbptr
)
{
#if	DK4_USE_ASSERT
	assert(NULL != bbptr);
#endif
	if (NULL != bbptr) {
		return bbptr->ymax;
	}
	return 0.0;
}



#if	TRACE_DEBUG
static
void
dk4bb_report_bb(const dk4_bb_t *p)
{
	

#line 915 "dk4bb.ctr"
	

#line 916 "dk4bb.ctr"
}
#endif


int
dk4bb_contains_bb(
	const dk4_bb_t	*pouter,
	const dk4_bb_t	*pinner,
	int				 aborder
)
{
	int		back	= 0;
	

#line 929 "dk4bb.ctr"
	

#line 930 "dk4bb.ctr"
	

#line 931 "dk4bb.ctr"
	

#line 932 "dk4bb.ctr"
	

#line 933 "dk4bb.ctr"
	if ((NULL != pouter) && (NULL != pinner)) {		

#line 934 "dk4bb.ctr"
		if (
			(have_both == (have_both & (pouter->fl)))
			&& (have_both == (have_both & (pinner->fl)))
		) {											

#line 938 "dk4bb.ctr"
			if (0 != aborder) {						

#line 939 "dk4bb.ctr"
				if (
					(pinner->xmin >= pouter->xmin)
					&& (pinner->xmax <= pouter->xmax)
					&& (pinner->ymin >= pouter->ymin)
					&& (pinner->ymax <= pouter->ymax)
				) {
					back = 1;
				}
			}
			else {									

#line 949 "dk4bb.ctr"
				if (
					(pinner->xmin > pouter->xmin)
					&& (pinner->xmax < pouter->xmax)
					&& (pinner->ymin > pouter->ymin)
					&& (pinner->ymax < pouter->ymax)
				) {
					back = 1;
				}
			}
		}
		else {										

#line 960 "dk4bb.ctr"
		}
	}
	else {											

#line 963 "dk4bb.ctr"
	}
	

#line 965 "dk4bb.ctr"
	return back;
}



/* vim: set ai sw=4 ts=4 : */