This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimregions.c
2236 lines (2147 loc) · 63.5 KB
/
imregions.c
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
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef REGIONS_PTYPE
#include "imregions.h"
#endif
/*
* http://stackoverflow.com/questions/3599160/unused-parameter-warnings-in-c-code
*/
#ifdef __GNUC__
# define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
#else
# define UNUSED(x) UNUSED_ ## x
#endif
/* panda and pie incorrectly used astronomical angles. fixed 4/2004 */
#define USE_ASTRO_ANGLE 0
/* increment when adding more points */
#define PTSINC 1000
/* add to OpenRegions() to cause this module to be loaded for dynamic linking */
static int imregno=0;
void initimregions(void){
imregno++;
return;
}
static void markx(GReg g, int UNUSED(sno), int flag, int type, int x, int y){
/* don't mark exclude regions */
if( type == TOK_EREG )
return;
/* for include, we might extend the x limits */
if( flag ){
if( x <= g->x0s[y] )
g->x0s[y] = max(x,g->x0);
if( x >= g->x1s[y] )
g->x1s[y] = min(x,g->x1);
} else {
/* for exclude, we have to look at the full line */
g->x0s[y] = g->x0;
g->x1s[y] = g->x1;
}
}
static void marky(GReg g, int sno, int flag, int type){
int i;
/* don't mark exclude regions */
if( type == TOK_EREG )
return;
/* mark include shape */
if( flag ){
for(i=max(g->y0,g->shapes[sno].ystart);
i<=min(g->y1,g->shapes[sno].ystop);
i++)
g->ybuf[i] = 1;
} else {
/* mark exclude shape */
for(i=g->y0; i<=g->shapes[sno].ystart-1; i++)
g->ybuf[i] = 1;
for(i=g->shapes[sno].ystop+1; i<=g->y1; i++)
g->ybuf[i] = 1;
}
}
/* ***************************** shape support ***************************** */
static void quadeq(double a, double b, double c,
double *x1, double *x2, int *nr, int *nc){
double dis, q;
if( feq(a,0.0) ){
*nc = 0;
if( feq(b,0.0) ){
*nr = 0; *x1 = 0.0;
} else {
*nr = 1; *x1 = -c / b;
}
*x2 = *x1;
} else {
dis = b*b - 4.0 * a * c;
if( dis > 0.0 ){
*nr = 2; *nc = 0;
dis = sqrt(dis);
if( b < 0.0 ) dis = -dis;
q = -0.5 * (b + dis);
*x1 = q/a; *x2 = c/q;
if(*x1 > *x2){
q = *x1; *x1 = *x2; *x2 = q;
}
} else if( feq(dis,0.0) ){
*nr = 1; *nc = 0; *x1 = - 0.5 * b / a; *x2 = *x1;
} else {
*nr = 0; *nc = 2; *x1 = - 0.5 * b / a; *x2 = 0.5 * sqrt(-dis) / a;
}
}
}
static void rgs_mark(GReg g, Scan *scanlist,
int sno, int flag, int type, int xval, int yval )
{
Scan scanmark, mark;
/* since yval is used as an index, make sure its within limits */
if(yval < g->y0) yval = g->y0;
if(yval > g->y1) yval = g->y1;
mark = (Scan)calloc(1, sizeof(ScanRec));
mark->x = xval;
/* sanity check */
if( !scanlist ) return;
/* starts are installed at back of list for given x */
if( !scanlist[yval] || ((scanlist[yval])->x > xval) ){
mark->next = scanlist[yval];
scanlist[yval] = mark;
} else {
scanmark = scanlist[yval];
while( scanmark->next && ((scanmark->next)->x < xval) )
scanmark = scanmark->next;
mark->next = scanmark->next;
scanmark->next = mark;
}
markx(g, sno, flag, type, xval, yval);
}
static void rgs_segment(GReg g, Scan *scanlist, int width, int height,
int sno, int flag, int type,
double x1, double y1, double x2, double y2)
{
int ystart, ystop, yval, xval;
double invslope, xoffset;
ystart = PIXINCL(y1);
if( ystart < 1 ) ystart = 1;
/* note: PIXINCL(stop) is 1st pixel not counted */
ystop = PIXINCL(y2) - 1;
if( ystop > height ) ystop = height;
/* ignore segment if there is no positive slope in integer coords */
if( (ystart > ystop) || (ystop < 1) )
return;
/* use inverse slope (run/rise) to get x given y with a multiply */
invslope = (x1 - x2) / (y1 - y2);
xoffset = x1 + ((ystart - y1) * invslope);
for(yval=ystart; yval<=ystop; yval++){
xval = PIXINCL(xoffset);
/* clip line to edges of image area (actually bend line) */
if(xval < 1) xval = 1;
if(xval > width) xval = width + 1;
rgs_mark(g, scanlist, sno, flag, type, xval, yval);
xoffset = xoffset + invslope;
}
}
static void _polygoni(GReg g, int qt, int UNUSED(rno), int sno, int flag,
int type, double *vx, double *vy, int count)
{
int i, j;
double xlo, xhi;
double ylo, yhi;
#ifdef ALREADY_DONE
/* divide by block factor to get real endpoints */
for(i=0; i<count; i++){
vx[i] = (vx[i] - g->xmin)/g->block + 1.0;
vy[i] = (vy[i] - g->ymin)/g->block + 1.0;
}
#endif
/* find the limits */
xlo = vx[0];
xhi = xlo;
ylo = vy[0];
yhi = ylo;
for(i=0; i<count; i++){
if(vx[i] > xhi) xhi = vx[i];
if(vx[i] < xlo) xlo = vx[i];
if(vy[i] > yhi) yhi = vy[i];
if(vy[i] < ylo) ylo = vy[i];
}
if( qt && (sno > 1) ){
g->shapes[sno].ystart = g->shapes[sno-1].ystart;
g->shapes[sno].ystop = g->shapes[sno-1].ystop;
} else {
g->shapes[sno].ystart = max(g->y0,PIXINCL(ylo));
g->shapes[sno].ystop = min(g->y1,PIXINCL(yhi) - 1);
}
g->shapes[sno].scanlist = (Scan *)calloc(g->y1+1, sizeof(Scan));
marky(g, sno, flag, type);
/* mark all horizontal segment crossings */
/* start with segment between last and first point */
j = count-1;
for(i=0; i<count; i++){
/* make segments always run from lower y to higher y */
if(vy[i] > vy[j]){
rgs_segment(g, g->shapes[sno].scanlist, g->x1, g->y1,
sno, flag, type, vx[j], vy[j], vx[i], vy[i]);
} else {
rgs_segment(g, g->shapes[sno].scanlist, g->x1, g->y1,
sno, flag, type, vx[i], vy[i], vx[j], vy[j]);
}
j = i;
}
}
static int corner_vertex(int index, int width, int height,
double *x, double *y)
{
switch (index) {
case 1:
*x = 0.0;
*y = height + 1;
break;
case 2:
*x = 0.0;
*y = 0.0;
break;
case 3:
*x = width + 1;
*y = 0.0;
break;
case 4:
*x = width + 1;
*y = height + 1;
default:
break;
}
index = index + 1;
if(index > 4) index = 1;
return index;
}
static int pie_intercept(int width, int height, double xcen, double ycen,
double angle, double *xcept, double *ycept)
{
double angl, slope; /* l: angle and slope of ray */
angl = angle;
/* put angles in normal range */
while (angl < 0.0)
angl = angl + 360.0;
while (angl >= 360.0)
angl = angl - 360.0;
/* check for a horizontal angle */
#if USE_ASTRO_ANGLE
if(fabs(angl - 90.0) < SMALL_NUMBER) {
#else
if(fabs(angl - 180.0) < SMALL_NUMBER) {
#endif
*xcept = 0.0;
*ycept = ycen;
return 2;
}
#if USE_ASTRO_ANGLE
if(fabs(angl - 270.0) < SMALL_NUMBER) {
#else
if(fabs(angl - 0.0) < SMALL_NUMBER) {
#endif
*xcept = width + 1;
*ycept = ycen;
return 4;
}
#if USE_ASTRO_ANGLE
/* convert to a Cartesian angle */
angl = angl + 90.0;
#else
// angl = angl;
#endif
if(angl >= 360.0)
angl = angl - 360.0;
if(angl < 180.0) {
*ycept = height + 1;
/* rule out vertical line */
if(fabs(angl - 90.0) < SMALL_NUMBER) {
*xcept = xcen;
return 1;
}
} else {
*ycept = 0.0;
/* rule out vertical line */
if(fabs(angl - 270.0) < SMALL_NUMBER) {
*xcept = xcen;
return 3;
}
}
/* convert to radians */
angl = (angl / 180.0) * M_PI;
/* calculate slope */
slope = tan(angl);
/* calculate intercept with designated y edge */
*xcept = xcen + ((*ycept - ycen) / slope);
if(*xcept < 0) {
*ycept = (ycen - (xcen * slope));
*xcept = 0.0;
return 2;
} else if(*xcept > (width + 1)) {
*ycept = (ycen + ((width + 1 - xcen) * slope));
*xcept = width + 1;
return 4;
} else {
if(*ycept < height){
return 3;
} else {
return 1;
}
}
}
void _impiei(GReg g, int qt, int rno, int sno, int flag, int type,
double UNUSED(x), double UNUSED(y),
double xcen, double ycen, double angle1, double angle2)
{
int width, height; /* l: image mask width and height */
double sweep; /* l: sweep between cut angles */
double vx[7], vy[7]; /* l: arrays of vertices for polygon */
int count; /* l: number of polygon vertices */
int intrcpt1, intrcpt2; /* l: side intercepted by each cut */
double x2, y2; /* l: coordinates of second intercept */
/* NB: do not use x and y variables, they have bogus values */
/* divide by block factor to get "real" parameters */
xcen = (xcen - g->xmin)/g->block + 1.0;
ycen = (ycen - g->ymin)/g->block + 1.0;
/* temps */
width = g->x1;
height = g->y1;
/* start listing vertices of polygon */
vx[0] = xcen;
vy[0] = ycen;
sweep = angle2 - angle1;
/* if sweep is too small to be noticed, don't bother */
if(fabs(sweep) < SMALL_NUMBER)
return;
if (sweep < 0.0) sweep = sweep + 360.0;
intrcpt1 = pie_intercept(width, height, xcen, ycen, angle1,
&(vx[1]), &(vy[1]));
intrcpt2 = pie_intercept(width, height, xcen, ycen, angle2,
&x2, &y2);
count = 2;
/* if angles intercept same side and slice is between them, no corners */
/* else, mark corners until reaching side with second angle intercept */
if((intrcpt1 != intrcpt2) || (sweep > 180.0)){
do{
intrcpt1 = corner_vertex(intrcpt1, width, height,
&(vx[count]), &(vy[count]));
count = count + 1;
}while(intrcpt1 != intrcpt2);
}
vx[count] = x2;
vy[count] = y2;
count++;
_polygoni(g, qt, rno, sno, flag, type, vx, vy, count);
}
/* ***************************** shapes ********************************** */
void imannulusi(GReg g, int rno, int sno, int flag, int type,
double x, double y,
double xcen, double ycen, double iradius, double oradius)
{
int yy;
double dval;
double xoff, yoff;
Scan *scanlist;
/* NB: do not use x and y variables, they have bogus values */
if( iradius == 0 ){
imcirclei(g, rno, sno, flag, type, x, y, xcen, ycen, oradius);
return;
}
/* divide by block factor to get "real" parameters */
xcen = (xcen - g->xmin)/g->block + 1.0;
ycen = (ycen - g->ymin)/g->block + 1.0;
iradius /= (double)g->block;
oradius /= (double)g->block;
/* set y limits */
if( PIXSTART(ycen - oradius) < PIXSTOP(ycen + oradius) ){
g->shapes[sno].ystart = max(g->y0,PIXSTART(ycen - oradius));
g->shapes[sno].ystop = min(g->y1,PIXSTOP(ycen + oradius));
} else {
g->shapes[sno].ystart = min(g->y1,PIXSTOP(ycen + oradius));
g->shapes[sno].ystop = max(g->y0,PIXSTART(ycen - oradius));
}
g->shapes[sno].scanlist = (Scan *)calloc(g->y1+1, sizeof(Scan));
scanlist = g->shapes[sno].scanlist;
marky(g, sno, flag, type);
/* calculate start/stop values for each y line */
for(yy=g->shapes[sno].ystart; yy<=g->shapes[sno].ystop; yy++){
yoff = PIXCEN(yy) - ycen;
if( (dval=(oradius * oradius) - (yoff * yoff)) > 0.0 ){
xoff = sqrt(dval);
if( PIXSTART(xcen - xoff) <= PIXSTOP(xcen + xoff) ){
rgs_mark(g, scanlist, sno, flag, type, PIXSTART(xcen - xoff), yy);
rgs_mark(g, scanlist, sno, flag, type, PIXSTOP(xcen + xoff), yy);
}
if( (dval=(iradius * iradius) - (yoff * yoff)) > 0.0 ){
xoff = sqrt(dval);
if( PIXSTART(xcen - xoff) <= PIXSTOP(xcen + xoff) ){
rgs_mark(g, scanlist, sno, flag, type, PIXSTART(xcen - xoff), yy);
rgs_mark(g, scanlist, sno, flag, type, PIXSTOP(xcen + xoff), yy);
}
}
}
}
}
int imannulus(GReg g, int rno, int sno, int flag, int type,
double x, double y,
double xcen, double ycen, double iradius, double oradius){
Scan scan;
if( iradius == 0 ){
return imcircle(g, rno, sno, flag, type, x, y, xcen, ycen, oradius);
}
if( flag ){
if(y < g->shapes[sno].ystart ) return 0;
if(y > g->shapes[sno].ystop ) return 0;
}
scan = g->shapes[sno].scanlist[(int)y];
if( (scan &&
((y >= g->shapes[sno].ystart) && (y <= g->shapes[sno].ystop)) &&
(scan->next->next ?
(((x >= scan->x) && (x <= scan->next->next->next->x)) &&
!((x >= scan->next->x) && (x <= scan->next->next->x))) :
((x >= scan->x) && (x <= scan->next->x))))
==flag ){
if( rno && flag ) g->rid = rno;
return 1;
} else {
return 0;
}
}
void imboxi(GReg g, int rno, int sno, int flag, int type,
double UNUSED(x), double UNUSED(y),
double xcen, double ycen, double xwidth, double yheight,
double angle){
double angl; /* l: Cartesian angle in radians */
double half_width, half_height;/* l: radii (1/2 width and height) */
double cosangl, sinangl; /* l: sine, cosine of the Cartesian angle */
double hw_cos, hw_sin; /* l: products of half_width with sin, cos */
double hh_cos, hh_sin; /* l: products of half_height with sin, cos */
double cornerx[4], cornery[4]; /* l: arrays of x and y coords of 4 corners */
/* NB: do not use x and y variables, they have bogus values */
if( (xwidth == 0) && (yheight==0) ){
return;
}
/* divide by block factor to get "real" parameters */
xcen = (xcen - g->xmin)/g->block + 1.0;
ycen = (ycen - g->ymin)/g->block + 1.0;
xwidth /= (double)g->block;
yheight /= (double)g->block;
#if USE_ASTRO_ANGLE
/* convert to a Cartesian angle; save angle for use in multi or slices */
angl = angle + 90.0;
#else
angl = angle;
#endif
while (angl >= 360.0) angl = angl - 360.0;
/* convert to radians */
angl = (angl / 180.0) * M_PI;
sinangl = sin (angl);
cosangl = cos (angl);
#if USE_ASTRO_ANGLE
/* since we rotate by 90.0 degrees to get from astro angle to cartesian, */
/* we also need to switch the width and height. we do this secretly so */
/* that the display will turn out right, by doing it in the half terms */
half_width = yheight / 2.0;
half_height = xwidth / 2.0;
#else
half_width = xwidth / 2.0;
half_height = yheight / 2.0;
#endif
hw_cos = half_width * cosangl;
hw_sin = half_width * sinangl;
hh_cos = half_height * cosangl;
hh_sin = half_height * sinangl;
#if USE_ASTRO_ANGLE
cornerx[0] = xcen - hw_cos - hh_sin;
cornery[0] = ycen - hw_sin + hh_cos;
cornerx[1] = xcen + hw_cos - hh_sin;
cornery[1] = ycen + hw_sin + hh_cos;
cornerx[2] = xcen + hw_cos + hh_sin;
cornery[2] = ycen + hw_sin - hh_cos;
cornerx[3] = xcen - hw_cos + hh_sin;
cornery[3] = ycen - hw_sin - hh_cos;
#else
cornerx[0] = xcen - hw_cos + hh_sin;
cornery[0] = ycen - hh_cos - hw_sin;
cornerx[1] = xcen - hw_cos - hh_sin;
cornery[1] = ycen + hh_cos - hw_sin;
cornerx[2] = xcen + hw_cos - hh_sin;
cornery[2] = ycen + hh_cos + hw_sin;
cornerx[3] = xcen + hw_cos + hh_sin;
cornery[3] = ycen - hh_cos + hw_sin;
#endif
_polygoni(g, 0, rno, sno, flag, type, cornerx, cornery, 4);
}
int imbox(GReg g, int rno, int sno, int flag, int type,
double x, double y,
double UNUSED(xcen), double UNUSED(ycen),
double xwidth, double yheight,
double UNUSED(angle)){
if( (xwidth == 0) && (yheight==0) ){
return !flag;
}
return impolygon(g, rno, sno, flag, type, x, y);
}
void imcirclei(GReg g, int UNUSED(rno), int sno, int flag, int type,
double UNUSED(x), double UNUSED(y),
double xcen, double ycen, double radius){
int yy;
double dval;
double xoff, yoff;
Scan *scanlist;
/* NB: do not use x and y variables, they have bogus values */
if( radius == 0 ){
return;
}
/* divide by block factor to get "real" parameters */
xcen = (xcen - g->xmin)/g->block + 1.0;
ycen = (ycen - g->ymin)/g->block + 1.0;
radius /= (double)g->block;
/* set y limits */
if( PIXSTART(ycen - radius) < PIXSTOP(ycen + radius) ){
g->shapes[sno].ystart = max(g->y0,PIXSTART(ycen - radius));
g->shapes[sno].ystop = min(g->y1,PIXSTOP(ycen + radius));
} else {
g->shapes[sno].ystart = min(g->y1,PIXSTOP(ycen + radius));
g->shapes[sno].ystop = max(g->y0,PIXSTART(ycen - radius));
}
g->shapes[sno].scanlist = (Scan *)calloc(g->y1+1, sizeof(Scan));
scanlist = g->shapes[sno].scanlist;
marky(g, sno, flag, type);
/* calculate start/stop values for each y line */
for(yy=g->shapes[sno].ystart; yy<=g->shapes[sno].ystop; yy++){
yoff = PIXCEN(yy) - ycen;
if( (dval=(radius * radius) - (yoff * yoff))>=0.0 ){
xoff = sqrt(dval);
if( PIXSTART(xcen - xoff) <= PIXSTOP(xcen + xoff) ){
rgs_mark(g, scanlist, sno, flag, type, PIXSTART(xcen - xoff), yy);
rgs_mark(g, scanlist, sno, flag, type, PIXSTOP(xcen + xoff), yy);
}
}
}
}
int imcircle(GReg g, int rno, int sno, int flag, int UNUSED(type),
double x, double y,
double UNUSED(xcen), double UNUSED(ycen),
double radius){
Scan scan;
if( radius == 0 ){
return !flag;
}
if( flag ){
if(y < g->shapes[sno].ystart ) return 0;
if(y > g->shapes[sno].ystop ) return 0;
}
scan = g->shapes[sno].scanlist[(int)y];
if( (scan &&
((y>=g->shapes[sno].ystart) && (y<=g->shapes[sno].ystop)) &&
((x >= scan->x) && (x <= (scan->next)->x))) == flag ){
if( rno && flag ) g->rid = rno;
return 1;
} else {
return 0;
}
}
void imellipsei(GReg g, int rno, int sno, int flag, int type,
double x, double y,
double xcen, double ycen, double xrad, double yrad,
double angle){
int yy;
int nr, nc;
double yhi;
double yoff;
double xboff, xfoff;
double angl;
double sinangl, cosangl;
double cossq, sinsq, xradsq, yradsq;
double a, b_partial, c_partial;
double b, c;
Scan *scanlist;
/* NB: do not use x and y variables, they have bogus values */
if( xrad == yrad ){
imcirclei(g, rno, sno, flag, type, x, y, xcen, ycen, xrad);
return;
}
/* divide by block factor to get "real" parameters */
xcen = (xcen - g->xmin)/g->block + 1.0;
ycen = (ycen - g->ymin)/g->block + 1.0;
xrad /= (double)g->block;
yrad /= (double)g->block;
/* set worst case limits (xrad axis parallel to vertical axis) */
#if USE_ASTRO_ANGLE
/* convert to a Cartesian angle; save "angle" for use by other routines */
angl = angle + 90.0;
#else
angl = angle;
#endif
while( angl >= 360.0 )
angl = angl - 360.0;
/* convert to radians */
angl = (angl / 180.0) * M_PI;
sinangl = sin(angl);
cosangl = cos(angl);
/* calculate approximate y limits */
/* choose lesser of containing rotbox and circle */
#if USE_ASTRO_ANGLE
yhi = fabs(sinangl * yrad) + fabs(cosangl * xrad);
#else
yhi = fabs(sinangl * xrad) + fabs(cosangl * yrad);
#endif
yhi = min(yhi, max(yrad, xrad));
/* set y limits */
if( PIXSTART(ycen - yhi) < PIXSTOP(ycen + yhi) ){
g->shapes[sno].ystart = max(g->y0,PIXSTART(ycen - yhi));
g->shapes[sno].ystop = min(g->y1,PIXSTOP(ycen + yhi));
} else {
g->shapes[sno].ystart = min(g->y1,PIXSTOP(ycen + yhi));
g->shapes[sno].ystop = max(g->y0,PIXSTART(ycen - yhi));
}
g->shapes[sno].scanlist = (Scan *)calloc(g->y1+1, sizeof(Scan));
scanlist = g->shapes[sno].scanlist;
marky(g, sno, flag, type);
/* prepare partials for quadratic equation solutions to coordinates */
cossq = cosangl * cosangl;
sinsq = sinangl * sinangl;
#if USE_ASTRO_ANGLE
/* because we rotate by 90.0 degrees to get from astro angle to */
/* cartesian, we also need to switch the x and y axes. we do this */
/* secretly so that the display will turn out right, by doing it in */
/* the sq terms */
xradsq = yrad * yrad;
yradsq = xrad * xrad;
#else
xradsq = xrad * xrad;
yradsq = yrad * yrad;
#endif
/* fill in as much of a,b,c as we can */
a = (cossq / xradsq) + (sinsq / yradsq);
b_partial = (2.0 * sinangl) * ((cosangl / xradsq) - (cosangl / yradsq));
c_partial = (sinsq / xradsq) + (cossq / yradsq);
/* calculate start/stop values for each y line */
for(yy=g->shapes[sno].ystart; yy<=g->shapes[sno].ystop; yy++){
yoff = yy - ycen;
b = b_partial * yoff;
c = (c_partial * yoff * yoff) - 1.0;
/* solve quadratic */
quadeq (a, b, c, &xboff, &xfoff, &nr, &nc);
/* if real roots */
if( nr != 0 ) {
/* translate x coordinates */
rgs_mark(g, scanlist, sno, flag, type, PIXSTART(xcen + xboff), yy);
rgs_mark(g, scanlist, sno, flag, type, PIXSTOP(xcen + xfoff), yy);
}
}
}
int imellipse(GReg g, int rno, int sno, int flag, int type,
double x, double y,
double xcen, double ycen, double xrad, double yrad,
double UNUSED(angle)){
Scan scan;
if( xrad == yrad ){
return imcircle(g, rno, sno, flag, type, x, y, xcen, ycen, xrad);
}
if( flag ){
if(y < g->shapes[sno].ystart ) return 0;
if(y > g->shapes[sno].ystop ) return 0;
}
scan = g->shapes[sno].scanlist[(int)y];
if( (scan &&
((y>=g->shapes[sno].ystart) && (y<=g->shapes[sno].ystop)) &&
((x >= scan->x) && (x <= (scan->next)->x))) == flag ){
if( rno && flag ) g->rid = rno;
return 1;
} else {
return 0;
}
}
void imfieldi(GReg g, int UNUSED(rno), int sno, int flag, int type,
double UNUSED(x), double UNUSED(y)){
int yy;
Scan *scanlist;
/* NB: do not use x and y variables, they have bogus values */
g->shapes[sno].ystart = g->y0;
g->shapes[sno].ystop = g->y1;
g->shapes[sno].scanlist = (Scan *)calloc(g->y1+1, sizeof(Scan));
scanlist = g->shapes[sno].scanlist;
marky(g, sno, flag, type);
/* calculate start/stop values for each y line */
for(yy=g->shapes[sno].ystart; yy<=g->shapes[sno].ystop; yy++){
rgs_mark(g, scanlist, sno, flag, type, g->x0, yy);
rgs_mark(g, scanlist, sno, flag, type, g->x1, yy);
}
}
int imfield(GReg g, int rno, int UNUSED(sno), int flag, int UNUSED(type),
double UNUSED(x), double UNUSED(y)){
if( flag ){
if( rno && flag ) g->rid = rno;
return 1;
} else {
return 0;
}
}
void imlinei(GReg g, int UNUSED(rno), int sno, int flag, int type,
double UNUSED(x), double UNUSED(y),
double x1, double y1, double x2, double y2){
double vx[2];
double vy[2];
int xval, yval;
double invslope, xoffset;
/* NB: do not use x and y variables, they have bogus values */
/* divide by block factor to get "real" parameters */
x1 = (x1 - g->xmin)/g->block + 1.0;
y1 = (y1 - g->ymin)/g->block + 1.0;
x2 = (x2 - g->xmin)/g->block + 1.0;
y2 = (y2 - g->ymin)/g->block + 1.0;
/* order by increasing y */
if( y1 < y2 ){
vx[0] = x1;
vy[0] = y1;
vx[1] = x2;
vy[1] = y2;
} else {
vx[0] = x2;
vy[0] = y2;
vx[1] = x1;
vy[1] = y1;
}
/* set y limits */
g->shapes[sno].ystart = PIXNUM(vy[0]);
g->shapes[sno].ystop = PIXNUM(vy[1]);
g->shapes[sno].scanlist = (Scan *)calloc(g->y1+1, sizeof(Scan));
marky(g, sno, flag, type);
if( feq(vy[0],vy[1]) ){
rgs_mark(g, g->shapes[sno].scanlist, sno, flag, type,
(int)min(vx[0],vx[1]), (int)vy[0]);
rgs_mark(g, g->shapes[sno].scanlist, sno, flag, type,
(int)max(vx[0],vx[1]), (int)vy[0]);
} else {
/* mark all horizontal segment crossings */
invslope = (vx[0] - vx[1]) / (vy[0] - vy[1]);
xoffset = vx[0];
for(yval=vy[0]; yval<=vy[1]; yval++){
xval = xoffset;
rgs_mark(g, g->shapes[sno].scanlist, sno, flag, type, xval, yval);
xoffset = xoffset + invslope;
}
}
}
int imline(GReg g, int rno, int sno, int flag, int UNUSED(type),
double x, double y,
double UNUSED(x1), double UNUSED(y1),
double UNUSED(x2), double UNUSED(y2)){
Scan scan;
if( flag ){
if(y < g->shapes[sno].ystart ) return 0;
if(y > g->shapes[sno].ystop ) return 0;
}
scan = g->shapes[sno].scanlist[(int)y];
if( (scan &&
((x==(int)scan->x) ||
(scan->next&&((x>=(int)scan->x)&&(x<=(int)scan->next->x)))))==flag ) {
if( rno && flag ) g->rid = rno;
return 1;
} else {
return 0;
}
}
void impiei(GReg g, int rno, int sno, int flag, int type,
double x, double y,
double xcen, double ycen, double angle1, double angle2){
_impiei(g, 0, rno, sno, flag, type, x, y, xcen, ycen, angle1, angle2);
}
int impie(GReg g, int rno, int sno, int flag, int type,
double x, double y,
double UNUSED(xcen), double UNUSED(ycen),
double UNUSED(angle1), double UNUSED(angle2)){
return impolygon(g, rno, sno, flag, type, x, y);
}
void imqtpiei(GReg g, int rno, int sno, int flag, int type,
double x, double y,
double xcen, double ycen, double angle1, double angle2){
_impiei(g, 1, rno, sno, flag, type, x, y, xcen, ycen, angle1, angle2);
}
int imqtpie(GReg g, int rno, int sno, int flag, int type,
double x, double y,
double UNUSED(xcen), double UNUSED(ycen),
double UNUSED(angle1), double UNUSED(angle2)){
return impolygon(g, rno, sno, flag, type, x, y);
}
void impointi(GReg g, int UNUSED(rno), int sno, int flag, int type,
double UNUSED(x), double UNUSED(y),
double xcen, double ycen){
/* NB: do not use x and y variables, they have bogus values */
/* divide by block factor to get "real" parameters */
xcen = (xcen - g->xmin)/g->block + 1.0;
ycen = (ycen - g->ymin)/g->block + 1.0;
/* set y limits */
g->shapes[sno].ystart = PIXNUM(ycen);
g->shapes[sno].ystop = PIXNUM(ycen);
g->shapes[sno].scanlist = (Scan *)calloc(g->y1+1, sizeof(Scan));
marky(g, sno, flag, type);
rgs_mark(g, g->shapes[sno].scanlist, sno, flag, type,
PIXNUM(xcen), PIXNUM(ycen));
}
int impoint(GReg g, int rno, int sno, int flag, int UNUSED(type),
double x, double y,
double UNUSED(xcen), double UNUSED(ycen)){
Scan scan;
if( flag ){
if(y < g->shapes[sno].ystart ) return 0;
if(y > g->shapes[sno].ystop ) return 0;
}
scan = g->shapes[sno].scanlist[(int)y];
if( (scan &&
(y == (int)g->shapes[sno].ystart) &&
(x == (int)scan->x))==flag ) {
if( rno && flag ) g->rid = rno;
return 1;
} else {
return 0;
}
}
#ifdef __STDC__
void impolygoni(GReg g, int rno, int sno, int flag, int type,
double UNUSED(x), double y, ...){
double *vx=NULL, *vy=NULL;
int count, maxcount;
va_list args;
va_start(args, y);
#else
void impolygoni(va_alist) va_dcl{
GReg g;
int rno, sno, flag, type;
double x, y;
double *vx=NULL, *vy=NULL;
int count, maxcount;
va_list args;
va_start(args);
g = va_arg(args, GReg);
rno = va_arg(args, int);
sno = va_arg(args, int);
flag = va_arg(args, int);
type = va_arg(args, int);
x = va_arg(args, double);
y = va_arg(args, double);
#endif
/* NB: do not use x and y variables, they have bogus values */
/* allocate space for x,y arguments */
maxcount = PTSINC;
vx = (double *)calloc(maxcount, sizeof(double));
vy = (double *)calloc(maxcount, sizeof(double));
/* look for x,y arguments */
count = 0;
while( 1 ){
if( (count + 1) >= maxcount ){
maxcount += PTSINC;
vx = (double *)realloc(vx, maxcount*sizeof(double));
vy = (double *)realloc(vy, maxcount*sizeof(double));
}
vx[count] = va_arg(args, double);
vy[count] = va_arg(args, double);
if( feq(vx[count],PSTOP) && feq(vy[count],PSTOP) )
break;
vx[count] = (vx[count] - g->xmin)/g->block + 1.0;
vy[count] = (vy[count] - g->ymin)/g->block + 1.0;
count ++;
}
va_end(args);
/* realloc to actual size */
vx = (double *)realloc(vx, count*sizeof(double));
vy = (double *)realloc(vy, count*sizeof(double));
/* call the common routine */
_polygoni(g, 0, rno, sno, flag, type, vx, vy, count);
if( vx ) free(vx);
if( vy ) free(vy);
}
#ifdef __STDC__
int
impolygon(GReg g, int rno, int sno, int flag, int UNUSED(type),
double x, double y, ...){
int crossings;
Scan scan;
va_list args;
va_start(args, y);
#else
int impolygon(va_alist) va_dcl{
GReg g;
int rno, sno, flag, type;
double x, y;
int crossings;
Scan scan;
va_list args;
va_start(args);
g = va_arg(args, GReg);
rno = va_arg(args, int);
sno = va_arg(args, int);
flag = va_arg(args, int);
type = va_arg(args, int);
x = va_arg(args, double);
y = va_arg(args, double);
#endif
va_end(args);
if( flag ){
if(y < g->shapes[sno].ystart ) return 0;
if(y > g->shapes[sno].ystop ) return 0;
}
/* no initialization of x for this row, just jump right in */
if( (y>=g->shapes[sno].ystart)&&(y<=g->shapes[sno].ystop) ){
crossings = 0;
for(scan=g->shapes[sno].scanlist[(int)y]; scan; scan=scan->next){
if( x >= scan->x ){
crossings++;
} else {
break;
}
}
if( (crossings%2) == flag ){
if( rno && flag ) g->rid = rno;
return 1;
} else {
return 0;
}
} else {
return !flag;
}
}
void imnannulusi(GReg g, int rno, int sno, int flag, int type,
double x, double y,
double xcen, double ycen,
double lo, double hi, int n){
int i;
int xsno;
double dinc;
/* NB: do not use x and y variables, they have bogus values */
/* get limits */
dinc = (hi - lo)/n;
xsno = (g->nshape+1)+((sno-1)*XSNO);
/* init all shapes */
imannulusi(g, 0, xsno, flag, type, x, y, xcen, ycen, lo, hi);
for(i=0; i<n; i++){
imannulusi(g, rno+i, sno+i, flag, type, x, y,
xcen, ycen, lo+(i*dinc), lo+((i+1)*dinc));
}
}
void imnboxi(GReg g, int rno, int sno, int flag, int type,
double x, double y,
double xcen, double ycen,
double lox, double loy, double hix, double hiy, int n,
double ang){
int i;
int xsno;
double dincx;
double dincy;
/* NB: do not use x and y variables, they have bogus values */
/* get limits */
dincx = (hix - lox)/n;
dincy = (hiy - loy)/n;
xsno = (g->nshape+1)+((sno-1)*XSNO);
/* init all shapes */
imboxi(g, 0, xsno, flag, type, x, y, xcen, ycen, hix, hiy, ang);
imboxi(g, 0, xsno+1, flag, type, x, y, xcen, ycen, lox, loy, ang);
for(i=0; i<n; i++){
imboxi(g, rno+i, sno+i, flag, type, x, y,
xcen, ycen, lox+((i+1)*dincx), loy+((i+1)*dincy), ang);
}
}
void imnellipsei(GReg g, int rno, int sno, int flag, int type,
double x, double y,
double xcen, double ycen,
double lox, double loy, double hix, double hiy, int n,
double ang){
int i;
int xsno;
double dincx;
double dincy;