-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolygon1.c
3544 lines (3150 loc) · 81.9 KB
/
polygon1.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
/*
polygon clipping functions. harry eaton implemented the algorithm
described in "A Closed Set of Algorithms for Performing Set
Operations on Polygonal Regions in the Plane" which the original
code did not do. I also modified it for integer coordinates
and faster computation. The license for this modified copy was
switched to the GPL per term (3) of the original LGPL license.
Copyright (C) 2006 harry eaton
based on:
poly_Boolean: a polygon clip library
Copyright (C) 1997 Alexey Nikitin, Michael Leonov
(also the authors of the paper describing the actual algorithm)
leonov@propro.iis.nsk.su
in turn based on:
nclip: a polygon clip library
Copyright (C) 1993 Klamer Schutte
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public
License along with this program; if not, write to the Free
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
polygon1.c
(C) 1997 Alexey Nikitin, Michael Leonov
(C) 1993 Klamer Schutte
all cases where original (Klamer Schutte) code is present
are marked
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <setjmp.h>
#include <math.h>
#include <string.h>
#include "global.h"
#include "rtree.h"
#include "heap.h"
#define ROUND(a) (long)((a) > 0 ? ((a) + 0.5) : ((a) - 0.5))
#define EPSILON (1E-8)
#define IsZero(a, b) (fabs((a) - (b)) < EPSILON)
#ifndef ABS
#define ABS(x) ((x) < 0 ? -(x) : (x))
#endif
/*********************************************************************/
/* L o n g V e c t o r S t u f f */
/*********************************************************************/
#define Vcopy(a,b) {(a)[0]=(b)[0];(a)[1]=(b)[1];}
int vect_equal (Vector v1, Vector v2);
void vect_init (Vector v, double x, double y);
void vect_sub (Vector res, Vector v2, Vector v3);
void vect_min (Vector res, Vector v2, Vector v3);
void vect_max (Vector res, Vector v2, Vector v3);
double vect_dist2 (Vector v1, Vector v2);
double vect_det2 (Vector v1, Vector v2);
double vect_len2 (Vector v1);
int vect_inters2 (Vector A, Vector B, Vector C, Vector D, Vector S1,
Vector S2);
/* note that a vertex v's Flags.status represents the edge defined by
* v to v->next (i.e. the edge is forward of v)
*/
#define ISECTED 3
#define UNKNWN 0
#define INSIDE 1
#define OUTSIDE 2
#define SHARED 3
#define SHARED2 4
#define TOUCHES 99
#define NODE_LABEL(n) ((n)->Flags.status)
#define LABEL_NODE(n,l) ((n)->Flags.status = (l))
#define error(code) longjmp(*(e), code)
#define MemGet(ptr, type) \
if (UNLIKELY (((ptr) = (type *)malloc(sizeof(type))) == NULL)) \
error(err_no_memory);
#undef DEBUG_LABEL
#undef DEBUG_ALL_LABELS
#undef DEBUG_JUMP
#undef DEBUG_GATHER
#undef DEBUG_ANGLE
#undef DEBUG
#ifdef DEBUG
#define DEBUGP(...) fprintf(stderr, ## __VA_ARGS__)
#else
#define DEBUGP(...)
#endif
/* ///////////////////////////////////////////////////////////////////////////// * /
/ * 2-Dimentional stuff
/ * ///////////////////////////////////////////////////////////////////////////// */
#define Vsub2(r,a,b) {(r)[0] = (a)[0] - (b)[0]; (r)[1] = (a)[1] - (b)[1];}
#define Vadd2(r,a,b) {(r)[0] = (a)[0] + (b)[0]; (r)[1] = (a)[1] + (b)[1];}
#define Vsca2(r,a,s) {(r)[0] = (a)[0] * (s); (r)[1] = (a)[1] * (s);}
#define Vcpy2(r,a) {(r)[0] = (a)[0]; (r)[1] = (a)[1];}
#define Vequ2(a,b) ((a)[0] == (b)[0] && (a)[1] == (b)[1])
#define Vadds(r,a,b,s) {(r)[0] = ((a)[0] + (b)[0]) * (s); (r)[1] = ((a)[1] + (b)[1]) * (s);}
#define Vswp2(a,b) { long t; \
t = (a)[0], (a)[0] = (b)[0], (b)[0] = t; \
t = (a)[1], (a)[1] = (b)[1], (b)[1] = t; \
}
#ifdef DEBUG
static char *theState (VNODE * v);
static void
pline_dump (VNODE * v)
{
VNODE *s, *n;
s = v;
do
{
n = v->next;
fprintf (stderr, "Line [%d %d %d %d 10 10 \"%s\"]\n",
v->point[0], v->point[1],
n->point[0], n->point[1], theState (v));
}
while ((v = v->next) != s);
}
static void
poly_dump (POLYAREA * p)
{
POLYAREA *f = p;
PLINE *pl;
do
{
pl = p->contours;
do
{
pline_dump (&pl->head);
fprintf (stderr, "NEXT PLINE\n");
}
while ((pl = pl->next) != NULL);
fprintf (stderr, "NEXT POLY\n");
}
while ((p = p->f) != f);
}
#endif
/***************************************************************/
/* routines for processing intersections */
/*
node_add
(C) 1993 Klamer Schutte
(C) 1997 Alexey Nikitin, Michael Leonov
(C) 2006 harry eaton
returns a bit field in new_point that indicates where the
point was.
1 means a new node was created and inserted
4 means the intersection was not on the dest point
*/
static VNODE *
node_add_single (VNODE * dest, Vector po)
{
VNODE *p;
if (vect_equal (po, dest->point))
return dest;
if (vect_equal (po, dest->next->point))
return dest->next;
p = poly_CreateNode (po);
if (p == NULL)
return NULL;
p->cvc_prev = p->cvc_next = NULL;
p->Flags.status = UNKNWN;
return p;
} /* node_add */
#define ISECT_BAD_PARAM (-1)
#define ISECT_NO_MEMORY (-2)
/*
new_descriptor
(C) 2006 harry eaton
*/
static CVCList *
new_descriptor (VNODE * a, char poly, char side)
{
CVCList *l = (CVCList *) malloc (sizeof (CVCList));
Vector v;
register double ang, dx, dy;
if (!l)
return NULL;
l->head = NULL;
l->parent = a;
l->poly = poly;
l->side = side;
l->next = l->prev = l;
if (side == 'P') /* previous */
vect_sub (v, a->prev->point, a->point);
else /* next */
vect_sub (v, a->next->point, a->point);
/* Uses slope/(slope+1) in quadrant 1 as a proxy for the angle.
* It still has the same monotonic sort result
* and is far less expensive to compute than the real angle.
*/
if (vect_equal (v, vect_zero))
{
if (side == 'P')
{
if (a->prev->cvc_prev == (CVCList *) - 1)
a->prev->cvc_prev = a->prev->cvc_next = NULL;
poly_ExclVertex (a->prev);
vect_sub (v, a->prev->point, a->point);
}
else
{
if (a->next->cvc_prev == (CVCList *) - 1)
a->next->cvc_prev = a->next->cvc_next = NULL;
poly_ExclVertex (a->next);
vect_sub (v, a->next->point, a->point);
}
}
assert (!vect_equal (v, vect_zero));
dx = fabs ((double) v[0]);
dy = fabs ((double) v[1]);
ang = dy / (dy + dx);
/* now move to the actual quadrant */
if (v[0] < 0 && v[1] >= 0)
ang = 2.0 - ang; /* 2nd quadrant */
else if (v[0] < 0 && v[1] < 0)
ang += 2.0; /* 3rd quadrant */
else if (v[0] >= 0 && v[1] < 0)
ang = 4.0 - ang; /* 4th quadrant */
l->angle = ang;
assert (ang >= 0.0 && ang <= 4.0);
#ifdef DEBUG_ANGLE
DEBUGP ("node on %c at (%d,%d) assigned angle %g on side %c\n", poly,
a->point[0], a->point[1], ang, side);
#endif
return l;
}
/*
insert_descriptor
(C) 2006 harry eaton
argument a is a cross-vertex node.
argument poly is the polygon it comes from ('A' or 'B')
argument side is the side this descriptor goes on ('P' for previous
'N' for next.
argument start is the head of the list of cvclists
*/
static CVCList *
insert_descriptor (VNODE * a, char poly, char side, CVCList * start)
{
CVCList *l, *newone, *big, *small;
if (!(newone = new_descriptor (a, poly, side)))
return NULL;
/* search for the CVCList for this point */
if (!start)
{
start = newone; /* return is also new, so we know where start is */
start->head = newone; /* circular list */
return newone;
}
else
{
l = start;
do
{
assert (l->head);
if (l->parent->point[0] == a->point[0]
&& l->parent->point[1] == a->point[1])
{ /* this CVCList is at our point */
start = l;
newone->head = l->head;
break;
}
if (l->head->parent->point[0] == start->parent->point[0]
&& l->head->parent->point[1] == start->parent->point[1])
{
/* this seems to be a new point */
/* link this cvclist to the list of all cvclists */
for (; l->head != newone; l = l->next)
l->head = newone;
newone->head = start;
return newone;
}
l = l->head;
}
while (1);
}
assert (start);
l = big = small = start;
do
{
if (l->next->angle < l->angle) /* find start/end of list */
{
small = l->next;
big = l;
}
else if (newone->angle >= l->angle && newone->angle <= l->next->angle)
{
/* insert new cvc if it lies between existing points */
newone->prev = l;
newone->next = l->next;
l->next = l->next->prev = newone;
return newone;
}
}
while ((l = l->next) != start);
/* didn't find it between points, it must go on an end */
if (big->angle <= newone->angle)
{
newone->prev = big;
newone->next = big->next;
big->next = big->next->prev = newone;
return newone;
}
assert (small->angle >= newone->angle);
newone->next = small;
newone->prev = small->prev;
small->prev = small->prev->next = newone;
return newone;
}
/*
node_add_point
(C) 1993 Klamer Schutte
(C) 1997 Alexey Nikitin, Michael Leonov
return 1 if new node in b, 2 if new node in a and 3 if new node in both
*/
static VNODE *
node_add_single_point (VNODE * a, Vector p)
{
VNODE *next_a, *new_node;
next_a = a->next;
new_node = node_add_single (a, p);
assert (new_node != NULL);
new_node->cvc_prev = new_node->cvc_next = (CVCList *) - 1;
if (new_node == a || new_node == next_a)
return NULL;
return new_node;
} /* node_add_point */
/*
node_label
(C) 2006 harry eaton
*/
static unsigned int
node_label (VNODE * pn)
{
CVCList *first_l, *l;
char this_poly;
int region = UNKNWN;
assert (pn);
assert (pn->cvc_next);
this_poly = pn->cvc_next->poly;
/* search counter-clockwise in the cross vertex connectivity (CVC) list
*
* check for shared edges (that could be prev or next in the list since the angles are equal)
* and check if this edge (pn -> pn->next) is found between the other poly's entry and exit
*/
if (pn->cvc_next->angle == pn->cvc_next->prev->angle)
l = pn->cvc_next->prev;
else
l = pn->cvc_next->next;
first_l = l;
while ((l->poly == this_poly) && (l != first_l->prev))
l = l->next;
assert (l->poly != this_poly);
assert (l && l->angle >= 0 && l->angle <= 4.0);
if (l->poly != this_poly)
{
if (l->side == 'P')
{
if (l->parent->prev->point[0] == pn->next->point[0] &&
l->parent->prev->point[1] == pn->next->point[1])
{
region = SHARED2;
pn->shared = l->parent->prev;
}
else
region = INSIDE;
}
else
{
if (l->angle == pn->cvc_next->angle)
{
assert (l->parent->next->point[0] == pn->next->point[0] &&
l->parent->next->point[1] == pn->next->point[1]);
region = SHARED;
pn->shared = l->parent;
}
else
region = OUTSIDE;
}
}
if (region == UNKNWN)
{
for (l = l->next; l != pn->cvc_next; l = l->next)
{
if (l->poly != this_poly)
{
if (l->side == 'P')
region = INSIDE;
else
region = OUTSIDE;
break;
}
}
}
assert (region != UNKNWN);
assert (NODE_LABEL (pn) == UNKNWN || NODE_LABEL (pn) == region);
LABEL_NODE (pn, region);
if (region == SHARED || region == SHARED2)
return UNKNWN;
return region;
} /* node_label */
/*
add_descriptors
(C) 2006 harry eaton
*/
static CVCList *
add_descriptors (PLINE * pl, char poly, CVCList * list)
{
VNODE *node = &pl->head;
do
{
if (node->cvc_prev)
{
assert (node->cvc_prev == (CVCList *) - 1
&& node->cvc_next == (CVCList *) - 1);
list = node->cvc_prev = insert_descriptor (node, poly, 'P', list);
if (!node->cvc_prev)
return NULL;
list = node->cvc_next = insert_descriptor (node, poly, 'N', list);
if (!node->cvc_next)
return NULL;
}
}
while ((node = node->next) != &pl->head);
return list;
}
static inline void
cntrbox_adjust (PLINE * c, Vector p)
{
c->xmin = min (c->xmin, p[0]);
c->xmax = max (c->xmax, p[0] + 1);
c->ymin = min (c->ymin, p[1]);
c->ymax = max (c->ymax, p[1] + 1);
}
/* some structures for handling segment intersections using the rtrees */
typedef struct seg
{
BoxType box;
VNODE *v;
PLINE *p;
int intersected;
} seg;
typedef struct _insert_node_task insert_node_task;
struct _insert_node_task
{
insert_node_task *next;
seg * node_seg;
VNODE *new_node;
};
typedef struct info
{
double m, b;
rtree_t *tree;
VNODE *v;
struct seg *s;
jmp_buf *env, sego, *touch;
int need_restart;
insert_node_task *node_insert_list;
} info;
typedef struct contour_info
{
PLINE *pa;
jmp_buf restart;
jmp_buf *getout;
int need_restart;
insert_node_task *node_insert_list;
} contour_info;
/*
* adjust_tree()
* (C) 2006 harry eaton
* This replaces the segment in the tree with the two new segments after
* a vertex has been added
*/
static int
adjust_tree (rtree_t * tree, struct seg *s)
{
struct seg *q;
q = (seg *)malloc (sizeof (struct seg));
if (!q)
return 1;
q->intersected = 0;
q->v = s->v;
q->p = s->p;
q->box.X1 = min (q->v->point[0], q->v->next->point[0]);
q->box.X2 = max (q->v->point[0], q->v->next->point[0]) + 1;
q->box.Y1 = min (q->v->point[1], q->v->next->point[1]);
q->box.Y2 = max (q->v->point[1], q->v->next->point[1]) + 1;
r_insert_entry (tree, (const BoxType *) q, 1);
q = (seg *)malloc (sizeof (struct seg));
if (!q)
return 1;
q->intersected = 0;
q->v = s->v->next;
q->p = s->p;
q->box.X1 = min (q->v->point[0], q->v->next->point[0]);
q->box.X2 = max (q->v->point[0], q->v->next->point[0]) + 1;
q->box.Y1 = min (q->v->point[1], q->v->next->point[1]);
q->box.Y2 = max (q->v->point[1], q->v->next->point[1]) + 1;
r_insert_entry (tree, (const BoxType *) q, 1);
r_delete_entry (tree, (const BoxType *) s);
return 0;
}
/*
* seg_in_region()
* (C) 2006, harry eaton
* This prunes the search for boxes that don't intersect the segment.
*/
static int
seg_in_region (const BoxType * b, void *cl)
{
struct info *i = (struct info *) cl;
double y1, y2;
/* for zero slope the search is aligned on the axis so it is already pruned */
if (i->m == 0.)
return 1;
y1 = i->m * b->X1 + i->b;
y2 = i->m * b->X2 + i->b;
if (min (y1, y2) >= b->Y2)
return 0;
if (max (y1, y2) < b->Y1)
return 0;
return 1; /* might intersect */
}
/* Prepend a deferred node-insersion task to a list */
static insert_node_task *
prepend_insert_node_task (insert_node_task *list, seg *seg, VNODE *new_node)
{
insert_node_task *task = (insert_node_task *)malloc (sizeof (*task));
task->node_seg = seg;
task->new_node = new_node;
task->next = list;
return task;
}
/*
* seg_in_seg()
* (C) 2006 harry eaton
* This routine checks if the segment in the tree intersect the search segment.
* If it does, the plines are marked as intersected and the point is marked for
* the cvclist. If the point is not already a vertex, a new vertex is inserted
* and the search for intersections starts over at the beginning.
* That is potentially a significant time penalty, but it does solve the snap rounding
* problem. There are efficient algorithms for finding intersections with snap
* rounding, but I don't have time to implement them right now.
*/
static int
seg_in_seg (const BoxType * b, void *cl)
{
struct info *i = (struct info *) cl;
struct seg *s = (struct seg *) b;
Vector s1, s2;
int cnt;
VNODE *new_node;
/* When new nodes are added at the end of a pass due to an intersection
* the segments may be altered. If either segment we're looking at has
* already been intersected this pass, skip it until the next pass.
*/
if (s->intersected || i->s->intersected)
return 0;
cnt = vect_inters2 (s->v->point, s->v->next->point,
i->v->point, i->v->next->point, s1, s2);
if (!cnt)
return 0;
if (i->touch) /* if checking touches one find and we're done */
longjmp (*i->touch, TOUCHES);
i->s->p->Flags.status = ISECTED;
s->p->Flags.status = ISECTED;
for (; cnt; cnt--)
{
bool done_insert_on_i = false;
new_node = node_add_single_point (i->v, cnt > 1 ? s2 : s1);
if (new_node != NULL)
{
#ifdef DEBUG_INTERSECT
DEBUGP ("new intersection on segment \"i\" at (%d, %d)\n",
cnt > 1 ? s2[0] : s1[0], cnt > 1 ? s2[1] : s1[1]);
#endif
i->node_insert_list =
prepend_insert_node_task (i->node_insert_list, i->s, new_node);
i->s->intersected = 1;
done_insert_on_i = true;
}
new_node = node_add_single_point (s->v, cnt > 1 ? s2 : s1);
if (new_node != NULL)
{
#ifdef DEBUG_INTERSECT
DEBUGP ("new intersection on segment \"s\" at (%d, %d)\n",
cnt > 1 ? s2[0] : s1[0], cnt > 1 ? s2[1] : s1[1]);
#endif
i->node_insert_list =
prepend_insert_node_task (i->node_insert_list, s, new_node);
s->intersected = 1;
return 0; /* Keep looking for intersections with segment "i" */
}
/* Skip any remaining r_search hits against segment i, as any futher
* intersections will be rejected until the next pass anyway.
*/
if (done_insert_on_i)
longjmp (*i->env, 1);
}
return 0;
}
static void *
make_edge_tree (PLINE * pb)
{
struct seg *s;
VNODE *bv;
rtree_t *ans = r_create_tree (NULL, 0, 0);
bv = &pb->head;
do
{
s = (seg *)malloc (sizeof (struct seg));
s->intersected = 0;
if (bv->point[0] < bv->next->point[0])
{
s->box.X1 = bv->point[0];
s->box.X2 = bv->next->point[0] + 1;
}
else
{
s->box.X2 = bv->point[0] + 1;
s->box.X1 = bv->next->point[0];
}
if (bv->point[1] < bv->next->point[1])
{
s->box.Y1 = bv->point[1];
s->box.Y2 = bv->next->point[1] + 1;
}
else
{
s->box.Y2 = bv->point[1] + 1;
s->box.Y1 = bv->next->point[1];
}
s->v = bv;
s->p = pb;
r_insert_entry (ans, (const BoxType *) s, 1);
}
while ((bv = bv->next) != &pb->head);
return (void *) ans;
}
static int
get_seg (const BoxType * b, void *cl)
{
struct info *i = (struct info *) cl;
struct seg *s = (struct seg *) b;
if (i->v == s->v)
{
i->s = s;
longjmp (i->sego, 1);
}
return 0;
}
/*
* intersect() (and helpers)
* (C) 2006, harry eaton
* This uses an rtree to find A-B intersections. Whenever a new vertex is
* added, the search for intersections is re-started because the rounding
* could alter the topology otherwise.
* This should use a faster algorithm for snap rounding intersection finding.
* The best algorthim is probably found in:
*
* "Improved output-sensitive snap rounding," John Hershberger, Proceedings
* of the 22nd annual symposium on Computational geomerty, 2006, pp 357-366.
* http://doi.acm.org/10.1145/1137856.1137909
*
* Algorithms described by de Berg, or Goodrich or Halperin, or Hobby would
* probably work as well.
*
*/
static int
contour_bounds_touch (const BoxType * b, void *cl)
{
contour_info *c_info = (contour_info *) cl;
PLINE *pa = c_info->pa;
PLINE *pb = (PLINE *) b;
PLINE *rtree_over;
PLINE *looping_over;
VNODE *av; /* node iterators */
struct info info;
BoxType box;
jmp_buf restart;
/* Have seg_in_seg return to our desired location if it touches */
info.env = &restart;
info.touch = c_info->getout;
info.need_restart = 0;
info.node_insert_list = c_info->node_insert_list;
/* Pick which contour has the fewer points, and do the loop
* over that. The r_tree makes hit-testing against a contour
* faster, so we want to do that on the bigger contour.
*/
if (pa->Count < pb->Count)
{
rtree_over = pb;
looping_over = pa;
}
else
{
rtree_over = pa;
looping_over = pb;
}
av = &looping_over->head;
do /* Loop over the nodes in the smaller contour */
{
/* check this edge for any insertions */
double dx;
info.v = av;
/* compute the slant for region trimming */
dx = av->next->point[0] - av->point[0];
if (dx == 0)
info.m = 0;
else
{
info.m = (av->next->point[1] - av->point[1]) / dx;
info.b = av->point[1] - info.m * av->point[0];
}
box.X2 = (box.X1 = av->point[0]) + 1;
box.Y2 = (box.Y1 = av->point[1]) + 1;
/* fill in the segment in info corresponding to this node */
if (setjmp (info.sego) == 0)
{
r_search (looping_over->tree, &box, NULL, get_seg, &info);
assert (0);
}
/* If we're going to have another pass anyway, skip this */
if (info.s->intersected && info.node_insert_list != NULL)
continue;
if (setjmp (restart))
continue;
/* NB: If this actually hits anything, we are teleported back to the beginning */
info.tree = rtree_over->tree;
if (info.tree)
if (UNLIKELY (r_search (info.tree, &info.s->box,
seg_in_region, seg_in_seg, &info)))
assert (0); /* XXX: Memory allocation failure */
}
while ((av = av->next) != &looping_over->head);
c_info->node_insert_list = info.node_insert_list;
if (info.need_restart)
c_info->need_restart = 1;
return 0;
}
static int
intersect_impl (jmp_buf * jb, POLYAREA * b, POLYAREA * a, int add)
{
POLYAREA *t;
PLINE *pa;
contour_info c_info;
int need_restart = 0;
insert_node_task *task;
c_info.need_restart = 0;
c_info.node_insert_list = NULL;
/* Search the r-tree of the object with most contours
* We loop over the contours of "a". Swap if necessary.
*/
if (a->contour_tree->size > b->contour_tree->size)
{
t = b;
b = a;
a = t;
}
for (pa = a->contours; pa; pa = pa->next) /* Loop over the contours of POLYAREA "a" */
{
BoxType sb;
jmp_buf out;
int retval;
c_info.getout = NULL;
c_info.pa = pa;
if (!add)
{
retval = setjmp (out);
if (retval)
{
/* The intersection test short-circuited back here,
* we need to clean up, then longjmp to jb */
longjmp (*jb, retval);
}
c_info.getout = &out;
}
sb.X1 = pa->xmin;
sb.Y1 = pa->ymin;
sb.X2 = pa->xmax + 1;
sb.Y2 = pa->ymax + 1;
r_search (b->contour_tree, &sb, NULL, contour_bounds_touch, &c_info);
if (c_info.need_restart)
need_restart = 1;
}
/* Process any deferred node insersions */
task = c_info.node_insert_list;
while (task != NULL)
{
insert_node_task *next = task->next;
/* Do insersion */
task->new_node->prev = task->node_seg->v;
task->new_node->next = task->node_seg->v->next;
task->node_seg->v->next->prev = task->new_node;
task->node_seg->v->next = task->new_node;
task->node_seg->p->Count++;
cntrbox_adjust (task->node_seg->p, task->new_node->point);
if (adjust_tree (task->node_seg->p->tree, task->node_seg))
assert (0); /* XXX: Memory allocation failure */
need_restart = 1; /* Any new nodes could intersect */
free (task);
task = next;
}
return need_restart;
}
static int
intersect (jmp_buf * jb, POLYAREA * b, POLYAREA * a, int add)
{
int call_count = 1;
while (intersect_impl (jb, b, a, add))
call_count++;
return 0;
}
static void
M_POLYAREA_intersect (jmp_buf * e, POLYAREA * afst, POLYAREA * bfst, int add)
{
POLYAREA *a = afst, *b = bfst;
PLINE *curcA, *curcB;
CVCList *the_list = NULL;
if (a == NULL || b == NULL)
error (err_bad_parm);
do
{
do
{
if (a->contours->xmax >= b->contours->xmin &&
a->contours->ymax >= b->contours->ymin &&
a->contours->xmin <= b->contours->xmax &&
a->contours->ymin <= b->contours->ymax)
{
if (UNLIKELY (intersect (e, a, b, add)))
error (err_no_memory);
}
}
while (add && (a = a->f) != afst);
for (curcB = b->contours; curcB != NULL; curcB = curcB->next)
if (curcB->Flags.status == ISECTED)
{
the_list = add_descriptors (curcB, 'B', the_list);
if (UNLIKELY (the_list == NULL))
error (err_no_memory);
}
}
while (add && (b = b->f) != bfst);
do
{
for (curcA = a->contours; curcA != NULL; curcA = curcA->next)
if (curcA->Flags.status == ISECTED)
{
the_list = add_descriptors (curcA, 'A', the_list);
if (UNLIKELY (the_list == NULL))
error (err_no_memory);
}
}
while (add && (a = a->f) != afst);
} /* M_POLYAREA_intersect */
static inline int
cntrbox_inside (PLINE * c1, PLINE * c2)
{
assert (c1 != NULL && c2 != NULL);
return ((c1->xmin >= c2->xmin) &&
(c1->ymin >= c2->ymin) &&
(c1->xmax <= c2->xmax) && (c1->ymax <= c2->ymax));
}
/*****************************************************************/
/* Routines for making labels */
static int
count_contours_i_am_inside (const BoxType * b, void *cl)
{
PLINE *me = (PLINE *) cl;
PLINE *check = (PLINE *) b;
if (poly_ContourInContour (check, me))
return 1;
return 0;
}
/* cntr_in_M_POLYAREA
returns poly is inside outfst ? TRUE : FALSE */
static int
cntr_in_M_POLYAREA (PLINE * poly, POLYAREA * outfst, BOOLp test)
{
POLYAREA *outer = outfst;
heap_t *heap;
assert (poly != NULL);
assert (outer != NULL);
heap = heap_create ();
do
{
if (cntrbox_inside (poly, outer->contours))
heap_insert (heap, outer->contours->area, (void *) outer);
}
/* if checking touching, use only the first polygon */
while (!test && (outer = outer->f) != outfst);
/* we need only check the smallest poly container
* but we must loop in case the box containter is not
* the poly container */
do