-
Notifications
You must be signed in to change notification settings - Fork 18
/
clipper.cpp
2422 lines (2229 loc) · 73.3 KB
/
clipper.cpp
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
/*******************************************************************************
* *
* Author : Angus Johnson *
* Version : 4.2.0 *
* Date : 11 April 2011 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2011 *
* *
* License: *
* Use, modification & distribution is subject to Boost Software License Ver 1. *
* http://www.boost.org/LICENSE_1_0.txt *
* *
* Attributions: *
* The code in this library is an extension of Bala Vatti's clipping algorithm: *
* "A generic solution to polygon clipping" *
* Communications of the ACM, Vol 35, Issue 7 (July 1992) pp 56-63. *
* http://portal.acm.org/citation.cfm?id=129906 *
* *
* Computer graphics and geometric modeling: implementation and algorithms *
* By Max K. Agoston *
* Springer; 1 edition (January 4, 2005) *
* http://books.google.com/books?q=vatti+clipping+agoston *
* *
*******************************************************************************/
/*******************************************************************************
* *
* This is a translation of the Delphi Clipper library and the naming style *
* used has retained a Delphi flavour. *
* *
*******************************************************************************/
#include "clipper.hpp"
#include <cmath>
#include <ctime>
#include <vector>
#include <algorithm>
#include <stdexcept>
#include <cstring>
#include <cstdlib>
namespace clipper {
static double const horizontal = -3.4E+38;
static double const pi = 3.14159265359;
enum Direction { dRightToLeft, dLeftToRight };
enum Position { pFirst, pMiddle, pSecond };
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
bool IsClockwise(const Polygon &poly)
{
int highI = poly.size() -1;
if (highI < 2) return false;
double a;
a = static_cast<double>(poly[highI].X) * static_cast<double>(poly[0].Y) -
static_cast<double>(poly[0].X) * static_cast<double>(poly[highI].Y);
for (int i = 0; i < highI; ++i)
a += static_cast<double>(poly[i].X) * static_cast<double>(poly[i+1].Y) -
static_cast<double>(poly[i+1].X) * static_cast<double>(poly[i].Y);
//area := area/2;
return a > 0; //ie reverse of normal formula because assume Y axis inverted
}
//------------------------------------------------------------------------------
bool IsClockwise(PolyPt *pt)
{
double a = 0;
PolyPt* startPt = pt;
do
{
a += static_cast<double>(pt->pt.X) * static_cast<double>(pt->next->pt.Y) -
static_cast<double>(pt->next->pt.X) * static_cast<double>(pt->pt.Y);
pt = pt->next;
}
while (pt != startPt);
//area = area /2;
return a > 0; //ie reverse of normal formula because Y axis inverted
}
//------------------------------------------------------------------------------
inline bool PointsEqual( const IntPoint &pt1, const IntPoint &pt2)
{
return ( pt1.X == pt2.X && pt1.Y == pt2.Y );
}
//------------------------------------------------------------------------------
double Area(const Polygon &poly)
{
int highI = poly.size() -1;
if (highI < 2) return 0;
double a;
a = static_cast<double>(poly[highI].X) * static_cast<double>(poly[0].Y) -
static_cast<double>(poly[0].X) * static_cast<double>(poly[highI].Y);
for (int i = 0; i < highI; ++i)
a += static_cast<double>(poly[i].X) * static_cast<double>(poly[i+1].Y) -
static_cast<double>(poly[i+1].X) * static_cast<double>(poly[i].Y);
return a/2;
}
//------------------------------------------------------------------------------
bool PointIsVertex(const IntPoint &pt, PolyPt *pp)
{
PolyPt *pp2 = pp;
do
{
if (PointsEqual(pp2->pt, pt)) return true;
pp2 = pp2->next;
}
while (pp2 != pp);
return false;
}
//------------------------------------------------------------------------------
bool PointInPolygon(const IntPoint &pt, PolyPt *pp)
{
PolyPt *pp2 = pp;
bool result = false;
do
{
if ((((pp2->pt.Y <= pt.Y) && (pt.Y < pp2->prev->pt.Y)) ||
((pp2->prev->pt.Y <= pt.Y) && (pt.Y < pp2->pt.Y))) &&
(pt.X - pp2->pt.X < (pp2->prev->pt.X - pp2->pt.X) * (pt.Y - pp2->pt.Y) /
(pp2->prev->pt.Y - pp2->pt.Y))) result = !result;
pp2 = pp2->next;
}
while (pp2 != pp);
return result;
}
//------------------------------------------------------------------------------
bool SlopesEqual(TEdge &e1, TEdge &e2)
{
if (e1.ybot == e1.ytop) return (e2.ybot == e2.ytop);
else if (e2.ybot == e2.ytop) return false;
else return (e1.ytop - e1.ybot)*(e2.xtop - e2.xbot) -
(e1.xtop - e1.xbot)*(e2.ytop - e2.ybot) == 0;
}
//------------------------------------------------------------------------------
bool SlopesEqual(const IntPoint pt1, const IntPoint pt2, const IntPoint pt3)
{
if (pt1.Y == pt2.Y) return (pt2.Y == pt3.Y);
else if (pt2.Y == pt3.Y) return false;
else return
(pt1.Y-pt2.Y)*(pt2.X-pt3.X) - (pt1.X-pt2.X)*(pt2.Y-pt3.Y) == 0;
}
//------------------------------------------------------------------------------
void SetDx(TEdge &e)
{
if (e.ybot == e.ytop) e.dx = horizontal;
else e.dx =
static_cast<double>(e.xtop - e.xbot) / static_cast<double>(e.ytop - e.ybot);
}
//---------------------------------------------------------------------------
double GetDx(const IntPoint pt1, const IntPoint pt2)
{
if (pt1.Y == pt2.Y) return horizontal;
else return
static_cast<double>(pt2.X - pt1.X) / static_cast<double>(pt2.Y - pt1.Y);
}
//---------------------------------------------------------------------------
void SwapSides(TEdge &edge1, TEdge &edge2)
{
EdgeSide side = edge1.side;
edge1.side = edge2.side;
edge2.side = side;
}
//------------------------------------------------------------------------------
void SwapPolyIndexes(TEdge &edge1, TEdge &edge2)
{
int outIdx = edge1.outIdx;
edge1.outIdx = edge2.outIdx;
edge2.outIdx = outIdx;
}
//------------------------------------------------------------------------------
inline long64 Round(double val)
{
if ((val < 0)) return static_cast<long64>(val - 0.5);
else return static_cast<long64>(val + 0.5);
}
//------------------------------------------------------------------------------
inline long64 Abs(long64 val)
{
if ((val < 0)) return -val; else return val;
}
//------------------------------------------------------------------------------
long64 TopX(TEdge &edge, const long64 currentY)
{
if( currentY == edge.ytop ) return edge.xtop;
return edge.xbot + Round(edge.dx *(currentY - edge.ybot));
}
//------------------------------------------------------------------------------
long64 TopX(const IntPoint pt1, const IntPoint pt2, const long64 currentY)
{
//preconditions: pt1.Y <> pt2.Y and pt1.Y > pt2.Y
if (currentY >= pt1.Y) return pt1.X;
else if (currentY == pt2.Y) return pt2.X;
else if (pt1.X == pt2.X) return pt1.X;
else
{
double q = static_cast<double>(pt1.X-pt2.X)/static_cast<double>(pt1.Y-pt2.Y);
return static_cast<long64>(pt1.X + (currentY - pt1.Y) *q);
}
}
//------------------------------------------------------------------------------
bool IntersectPoint(TEdge &edge1, TEdge &edge2, IntPoint &ip)
{
double b1, b2;
if (SlopesEqual(edge1, edge2)) return false;
else if (edge1.dx == 0)
{
ip.X = edge1.xbot;
if (edge2.dx == horizontal)
{
ip.Y = edge2.ybot;
} else
{
b2 = edge2.ybot - (edge2.xbot/edge2.dx);
ip.Y = Round(ip.X/edge2.dx + b2);
}
}
else if (edge2.dx == 0)
{
ip.X = edge2.xbot;
if (edge1.dx == horizontal)
{
ip.Y = edge1.ybot;
} else
{
b1 = edge1.ybot - (edge1.xbot/edge1.dx);
ip.Y = Round(ip.X/edge1.dx + b1);
}
} else
{
b1 = edge1.xbot - edge1.ybot * edge1.dx;
b2 = edge2.xbot - edge2.ybot * edge2.dx;
b2 = (b2-b1)/(edge1.dx - edge2.dx);
ip.Y = Round(b2);
ip.X = Round(edge1.dx * b2 + b1);
}
return
//can be *so close* to the top of one edge that the rounded Y equals one ytop ...
(ip.Y == edge1.ytop && ip.Y >= edge2.ytop && edge1.tmpX > edge2.tmpX) ||
(ip.Y == edge2.ytop && ip.Y >= edge1.ytop && edge1.tmpX > edge2.tmpX) ||
(ip.Y > edge1.ytop && ip.Y > edge2.ytop);
}
//------------------------------------------------------------------------------
void ReversePolyPtLinks(PolyPt &pp)
{
PolyPt *pp1, *pp2;
pp1 = &pp;
do {
pp2 = pp1->next;
pp1->next = pp1->prev;
pp1->prev = pp2;
pp1 = pp2;
} while( pp1 != &pp );
}
//------------------------------------------------------------------------------
void DisposePolyPts(PolyPt*& pp)
{
if (pp == 0) return;
PolyPt *tmpPp;
pp->prev->next = 0;
while( pp )
{
tmpPp = pp;
pp = pp->next;
delete tmpPp ;
}
}
//------------------------------------------------------------------------------
void InitEdge(TEdge *e, TEdge *eNext,
TEdge *ePrev, const IntPoint &pt, PolyType polyType)
{
std::memset( e, 0, sizeof( TEdge ));
e->next = eNext;
e->prev = ePrev;
e->xcurr = pt.X;
e->ycurr = pt.Y;
if (e->ycurr >= e->next->ycurr)
{
e->xbot = e->xcurr;
e->ybot = e->ycurr;
e->xtop = e->next->xcurr;
e->ytop = e->next->ycurr;
e->windDelta = 1;
} else
{
e->xtop = e->xcurr;
e->ytop = e->ycurr;
e->xbot = e->next->xcurr;
e->ybot = e->next->ycurr;
e->windDelta = -1;
}
SetDx(*e);
e->polyType = polyType;
e->outIdx = -1;
}
//------------------------------------------------------------------------------
inline void SwapX(TEdge &e)
{
//swap horizontal edges' top and bottom x's so they follow the natural
//progression of the bounds - ie so their xbots will align with the
//adjoining lower edge. [Helpful in the ProcessHorizontal() method.]
e.xcurr = e.xtop;
e.xtop = e.xbot;
e.xbot = e.xcurr;
}
//------------------------------------------------------------------------------
void SwapPoints(IntPoint &pt1, IntPoint &pt2)
{
IntPoint tmp = pt1;
pt1 = pt2;
pt2 = tmp;
}
//------------------------------------------------------------------------------
bool GetOverlapSegment(IntPoint pt1a, IntPoint pt1b, IntPoint pt2a,
IntPoint pt2b, IntPoint &pt1, IntPoint &pt2)
{
//precondition: segments are colinear.
if ( pt1a.Y == pt1b.Y || Abs((pt1a.X - pt1b.X)/(pt1a.Y - pt1b.Y)) > 1 )
{
if (pt1a.X > pt1b.X) SwapPoints(pt1a, pt1b);
if (pt2a.X > pt2b.X) SwapPoints(pt2a, pt2b);
if (pt1a.X > pt2a.X) pt1 = pt1a; else pt1 = pt2a;
if (pt1b.X < pt2b.X) pt2 = pt1b; else pt2 = pt2b;
return pt1.X < pt2.X;
} else
{
if (pt1a.Y < pt1b.Y) SwapPoints(pt1a, pt1b);
if (pt2a.Y < pt2b.Y) SwapPoints(pt2a, pt2b);
if (pt1a.Y < pt2a.Y) pt1 = pt1a; else pt1 = pt2a;
if (pt1b.Y > pt2b.Y) pt2 = pt1b; else pt2 = pt2b;
return pt1.Y > pt2.Y;
}
}
//------------------------------------------------------------------------------
PolyPt* PolygonBottom(PolyPt* pp)
{
PolyPt* p = pp->next;
PolyPt* result = pp;
while (p != pp)
{
if (p->pt.Y > result->pt.Y) result = p;
else if (p->pt.Y == result->pt.Y && p->pt.X < result->pt.X) result = p;
p = p->next;
}
return result;
}
//------------------------------------------------------------------------------
bool FindSegment(PolyPt* &pp, const IntPoint pt1, const IntPoint pt2)
{
if (!pp) return false;
PolyPt* pp2 = pp;
do
{
if (PointsEqual(pp->pt, pt1) &&
(PointsEqual(pp->next->pt, pt2) || PointsEqual(pp->prev->pt, pt2)))
return true;
pp = pp->next;
}
while (pp != pp2);
return false;
}
//------------------------------------------------------------------------------
Position GetPosition(const IntPoint pt1, const IntPoint pt2, const IntPoint pt)
{
if (PointsEqual(pt1, pt)) return pFirst;
else if (PointsEqual(pt2, pt)) return pSecond;
else return pMiddle;
}
//------------------------------------------------------------------------------
bool Pt3IsBetweenPt1AndPt2(const IntPoint pt1,
const IntPoint pt2, const IntPoint pt3)
{
if (PointsEqual(pt1, pt3) || PointsEqual(pt2, pt3)) return true;
else if (pt1.X != pt2.X) return (pt1.X < pt3.X) == (pt3.X < pt2.X);
else return (pt1.Y < pt3.Y) == (pt3.Y < pt2.Y);
}
//------------------------------------------------------------------------------
PolyPt* InsertPolyPtBetween(PolyPt* p1, PolyPt* p2, const IntPoint pt)
{
PolyPt* result = new PolyPt;
result->pt = pt;
result->isHole = p1->isHole;
if (p2 == p1->next)
{
p1->next = result;
p2->prev = result;
result->next = p2;
result->prev = p1;
} else
{
p2->next = result;
p1->prev = result;
result->next = p1;
result->prev = p2;
}
return result;
}
//------------------------------------------------------------------------------
// ClipperBase class methods ...
//------------------------------------------------------------------------------
ClipperBase::ClipperBase() //constructor
{
m_MinimaList = 0;
m_CurrentLM = 0;
}
//------------------------------------------------------------------------------
ClipperBase::~ClipperBase() //destructor
{
Clear();
}
//------------------------------------------------------------------------------
bool ClipperBase::AddPolygon( const Polygon &pg, PolyType polyType)
{
int len = pg.size();
if (len < 3) return false;
Polygon p(len);
p[0] = pg[0];
int j = 0;
const long64 MaxVal = 1500000000; //~ Sqrt(2^63)/2
for (int i = 1; i < len; ++i)
{
if (Abs(pg[i].X) > MaxVal|| Abs(pg[i].Y) > MaxVal)
throw clipperException("Integer exceeds range bounds");
else if (PointsEqual(p[j], pg[i])) continue;
else if (j > 0 && SlopesEqual(p[j-1], p[j], pg[i]))
{
if (PointsEqual(p[j-1], pg[i])) j--;
} else j++;
p[j] = pg[i];
}
if (j < 2) return false;
len = j+1;
for (;;)
{
//nb: test for point equality before testing slopes ...
if (PointsEqual(p[j], p[0])) j--;
else if (PointsEqual(p[0], p[1]) || SlopesEqual(p[j], p[0], p[1]))
p[0] = p[j--];
else if (SlopesEqual(p[j-1], p[j], p[0])) j--;
else if (SlopesEqual(p[0], p[1], p[2]))
{
for (int i = 2; i <= j; ++i) p[i-1] = p[i];
j--;
}
//exit loop if nothing is changed or there are too few vertices ...
if (j == len-1 || j < 2) break;
len = j +1;
}
if (len < 3) return false;
//create a new edge array ...
TEdge *edges = new TEdge [len];
m_edges.push_back(edges);
//convert vertices to a double-linked-list of edges and initialize ...
edges[0].xcurr = p[0].X;
edges[0].ycurr = p[0].Y;
InitEdge(&edges[len-1], &edges[0], &edges[len-2], p[len-1], polyType);
for (int i = len-2; i > 0; --i)
InitEdge(&edges[i], &edges[i+1], &edges[i-1], p[i], polyType);
InitEdge(&edges[0], &edges[1], &edges[len-1], p[0], polyType);
//reset xcurr & ycurr and find 'eHighest' (given the Y axis coordinates
//increase downward so the 'highest' edge will have the smallest ytop) ...
TEdge *e = &edges[0];
TEdge *eHighest = e;
do
{
e->xcurr = e->xbot;
e->ycurr = e->ybot;
if (e->ytop < eHighest->ytop) eHighest = e;
e = e->next;
}
while ( e != &edges[0]);
//make sure eHighest is positioned so the following loop works safely ...
if (eHighest->windDelta > 0) eHighest = eHighest->next;
if (eHighest->dx == horizontal) eHighest = eHighest->next;
//finally insert each local minima ...
e = eHighest;
do {
e = AddBoundsToLML(e);
}
while( e != eHighest );
return true;
}
//------------------------------------------------------------------------------
void ClipperBase::InsertLocalMinima(LocalMinima *newLm)
{
if( ! m_MinimaList )
{
m_MinimaList = newLm;
}
else if( newLm->Y >= m_MinimaList->Y )
{
newLm->next = m_MinimaList;
m_MinimaList = newLm;
} else
{
LocalMinima* tmpLm = m_MinimaList;
while( tmpLm->next && ( newLm->Y < tmpLm->next->Y ) )
tmpLm = tmpLm->next;
newLm->next = tmpLm->next;
tmpLm->next = newLm;
}
}
//------------------------------------------------------------------------------
TEdge* ClipperBase::AddBoundsToLML(TEdge *e)
{
//Starting at the top of one bound we progress to the bottom where there's
//a local minima. We then go to the top of the next bound. These two bounds
//form the left and right (or right and left) bounds of the local minima.
e->nextInLML = 0;
e = e->next;
for (;;)
{
if ( e->dx == horizontal )
{
//nb: proceed through horizontals when approaching from their right,
// but break on horizontal minima if approaching from their left.
// This ensures 'local minima' are always on the left of horizontals.
if (e->next->ytop < e->ytop && e->next->xbot > e->prev->xbot) break;
if (e->xtop != e->prev->xbot) SwapX(*e);
e->nextInLML = e->prev;
}
else if (e->ycurr == e->prev->ycurr) break;
else e->nextInLML = e->prev;
e = e->next;
}
//e and e.prev are now at a local minima ...
LocalMinima* newLm = new LocalMinima;
newLm->next = 0;
newLm->Y = e->prev->ybot;
if ( e->dx == horizontal ) //horizontal edges never start a left bound
{
if (e->xbot != e->prev->xbot) SwapX(*e);
newLm->leftBound = e->prev;
newLm->rightBound = e;
} else if (e->dx < e->prev->dx)
{
newLm->leftBound = e->prev;
newLm->rightBound = e;
} else
{
newLm->leftBound = e;
newLm->rightBound = e->prev;
}
newLm->leftBound->side = esLeft;
newLm->rightBound->side = esRight;
InsertLocalMinima( newLm );
for (;;)
{
if ( e->next->ytop == e->ytop && e->next->dx != horizontal ) break;
e->nextInLML = e->next;
e = e->next;
if ( e->dx == horizontal && e->xbot != e->prev->xtop) SwapX(*e);
}
return e->next;
}
//------------------------------------------------------------------------------
bool ClipperBase::AddPolygons(const Polygons &ppg, PolyType polyType)
{
bool result = false;
for (Polygons::size_type i = 0; i < ppg.size(); ++i)
if (AddPolygon(ppg[i], polyType)) result = true;
return result;
}
//------------------------------------------------------------------------------
void ClipperBase::Clear()
{
DisposeLocalMinimaList();
for (EdgeList::size_type i = 0; i < m_edges.size(); ++i) delete [] m_edges[i];
m_edges.clear();
}
//------------------------------------------------------------------------------
void ClipperBase::Reset()
{
m_CurrentLM = m_MinimaList;
if( !m_CurrentLM ) return; //ie nothing to process
//reset all edges ...
LocalMinima* lm = m_MinimaList;
while( lm )
{
TEdge* e = lm->leftBound;
while( e )
{
e->xcurr = e->xbot;
e->ycurr = e->ybot;
e->side = esLeft;
e->outIdx = -1;
e = e->nextInLML;
}
e = lm->rightBound;
while( e )
{
e->xcurr = e->xbot;
e->ycurr = e->ybot;
e->side = esRight;
e->outIdx = -1;
e = e->nextInLML;
}
lm = lm->next;
}
}
//------------------------------------------------------------------------------
void ClipperBase::DisposeLocalMinimaList()
{
while( m_MinimaList )
{
LocalMinima* tmpLm = m_MinimaList->next;
delete m_MinimaList;
m_MinimaList = tmpLm;
}
m_CurrentLM = 0;
}
//------------------------------------------------------------------------------
void ClipperBase::PopLocalMinima()
{
if( ! m_CurrentLM ) return;
m_CurrentLM = m_CurrentLM->next;
}
//------------------------------------------------------------------------------
IntRect ClipperBase::GetBounds()
{
IntRect result;
LocalMinima* lm = m_MinimaList;
if (!lm)
{
result.left = result.top = result.right = result.bottom = 0;
return result;
}
result.left = lm->leftBound->xbot;
result.top = lm->leftBound->ybot;
result.right = lm->leftBound->xbot;
result.bottom = lm->leftBound->ybot;
while (lm)
{
if (lm->leftBound->ybot > result.bottom)
result.bottom = lm->leftBound->ybot;
TEdge* e = lm->leftBound;
for (;;) {
while (e->nextInLML)
{
if (e->xbot < result.left) result.left = e->xbot;
if (e->xbot > result.right) result.right = e->xbot;
e = e->nextInLML;
}
if (e->xbot < result.left) result.left = e->xbot;
if (e->xbot > result.right) result.right = e->xbot;
if (e->xtop < result.left) result.left = e->xtop;
if (e->xtop > result.right) result.right = e->xtop;
if (e->ytop < result.top) result.top = e->ytop;
if (e == lm->leftBound) e = lm->rightBound;
else break;
}
lm = lm->next;
}
return result;
}
//------------------------------------------------------------------------------
// TClipper methods ...
//------------------------------------------------------------------------------
Clipper::Clipper() : ClipperBase() //constructor
{
m_Scanbeam = 0;
m_ActiveEdges = 0;
m_SortedEdges = 0;
m_IntersectNodes = 0;
m_ExecuteLocked = false;
};
//------------------------------------------------------------------------------
Clipper::~Clipper() //destructor
{
DisposeScanbeamList();
};
//------------------------------------------------------------------------------
void Clipper::DisposeScanbeamList()
{
while ( m_Scanbeam ) {
Scanbeam* sb2 = m_Scanbeam->next;
delete m_Scanbeam;
m_Scanbeam = sb2;
}
}
//------------------------------------------------------------------------------
void Clipper::Reset()
{
ClipperBase::Reset();
m_Scanbeam = 0;
m_ActiveEdges = 0;
m_SortedEdges = 0;
LocalMinima* lm = m_MinimaList;
while (lm)
{
InsertScanbeam(lm->Y);
InsertScanbeam(lm->leftBound->ytop);
lm = lm->next;
}
}
//------------------------------------------------------------------------------
bool Clipper::Execute(ClipType clipType, Polygons &solution,
PolyFillType subjFillType, PolyFillType clipFillType)
{
if( m_ExecuteLocked ) return false;
bool succeeded;
try {
m_ExecuteLocked = true;
solution.resize(0);
Reset();
if (!m_CurrentLM )
{
m_ExecuteLocked = false;
return true;
}
m_SubjFillType = subjFillType;
m_ClipFillType = clipFillType;
m_ClipType = clipType;
long64 botY = PopScanbeam();
do {
InsertLocalMinimaIntoAEL(botY);
ClearHorzJoins();
ProcessHorizontals();
long64 topY = PopScanbeam();
succeeded = ProcessIntersections(topY);
if (succeeded) ProcessEdgesAtTopOfScanbeam(topY);
botY = topY;
} while( succeeded && m_Scanbeam );
//build the return polygons ...
if (succeeded) BuildResult(solution);
}
catch(...) {
ClearJoins();
ClearHorzJoins();
solution.resize(0);
succeeded = false;
}
ClearJoins();
ClearHorzJoins();
DisposeAllPolyPts();
m_ExecuteLocked = false;
return succeeded;
}
//------------------------------------------------------------------------------
void Clipper::InsertScanbeam(const long64 Y)
{
if( !m_Scanbeam )
{
m_Scanbeam = new Scanbeam;
m_Scanbeam->next = 0;
m_Scanbeam->Y = Y;
}
else if( Y > m_Scanbeam->Y )
{
Scanbeam* newSb = new Scanbeam;
newSb->Y = Y;
newSb->next = m_Scanbeam;
m_Scanbeam = newSb;
} else
{
Scanbeam* sb2 = m_Scanbeam;
while( sb2->next && ( Y <= sb2->next->Y ) ) sb2 = sb2->next;
if( Y == sb2->Y ) return; //ie ignores duplicates
Scanbeam* newSb = new Scanbeam;
newSb->Y = Y;
newSb->next = sb2->next;
sb2->next = newSb;
}
}
//------------------------------------------------------------------------------
long64 Clipper::PopScanbeam()
{
long64 Y = m_Scanbeam->Y;
Scanbeam* sb2 = m_Scanbeam;
m_Scanbeam = m_Scanbeam->next;
delete sb2;
return Y;
}
//------------------------------------------------------------------------------
void Clipper::DisposeAllPolyPts(){
for (PolyPtList::size_type i = 0; i < m_PolyPts.size(); ++i)
DisposePolyPts(m_PolyPts[i]);
m_PolyPts.clear();
}
//------------------------------------------------------------------------------
void Clipper::SetWindingCount(TEdge &edge)
{
TEdge *e = edge.prevInAEL;
//find the edge of the same polytype that immediately preceeds 'edge' in AEL
while ( e && e->polyType != edge.polyType ) e = e->prevInAEL;
if ( !e )
{
edge.windCnt = edge.windDelta;
edge.windCnt2 = 0;
e = m_ActiveEdges; //ie get ready to calc windCnt2
} else if ( IsNonZeroFillType(edge) )
{
//nonZero filling ...
if ( e->windCnt * e->windDelta < 0 )
{
if (Abs(e->windCnt) > 1)
{
if (e->windDelta * edge.windDelta < 0) edge.windCnt = e->windCnt;
else edge.windCnt = e->windCnt + edge.windDelta;
} else
edge.windCnt = e->windCnt + e->windDelta + edge.windDelta;
} else
{
if ( Abs(e->windCnt) > 1 && e->windDelta * edge.windDelta < 0)
edge.windCnt = e->windCnt;
else if ( e->windCnt + edge.windDelta == 0 )
edge.windCnt = e->windCnt;
else edge.windCnt = e->windCnt + edge.windDelta;
}
edge.windCnt2 = e->windCnt2;
e = e->nextInAEL; //ie get ready to calc windCnt2
} else
{
//even-odd filling ...
edge.windCnt = 1;
edge.windCnt2 = e->windCnt2;
e = e->nextInAEL; //ie get ready to calc windCnt2
}
//update windCnt2 ...
if ( IsNonZeroAltFillType(edge) )
{
//nonZero filling ...
while ( e != &edge )
{
edge.windCnt2 += e->windDelta;
e = e->nextInAEL;
}
} else
{
//even-odd filling ...
while ( e != &edge )
{
edge.windCnt2 = (edge.windCnt2 == 0) ? 1 : 0;
e = e->nextInAEL;
}
}
}
//------------------------------------------------------------------------------
bool Clipper::IsNonZeroFillType(const TEdge& edge) const
{
if (edge.polyType == ptSubject)
return m_SubjFillType == pftNonZero; else
return m_ClipFillType == pftNonZero;
}
//------------------------------------------------------------------------------
bool Clipper::IsNonZeroAltFillType(const TEdge& edge) const
{
if (edge.polyType == ptSubject)
return m_ClipFillType == pftNonZero; else
return m_SubjFillType == pftNonZero;
}
//------------------------------------------------------------------------------
bool Clipper::IsContributing(const TEdge& edge) const
{
switch( m_ClipType ){
case ctIntersection:
if ( edge.polyType == ptSubject )
return Abs(edge.windCnt) == 1 && edge.windCnt2 != 0; else
return Abs(edge.windCnt2) > 0 && Abs(edge.windCnt) == 1;
case ctUnion:
return Abs(edge.windCnt) == 1 && edge.windCnt2 == 0;
case ctDifference:
if ( edge.polyType == ptSubject )
return std::abs(edge.windCnt) == 1 && edge.windCnt2 == 0; else
return std::abs(edge.windCnt) == 1 && edge.windCnt2 != 0;
default: //case ctXor:
return std::abs(edge.windCnt) == 1;
}
}
//------------------------------------------------------------------------------
void Clipper::AddLocalMinPoly(TEdge *e1, TEdge *e2, const IntPoint &pt)
{
if( e2->dx == horizontal || ( e1->dx > e2->dx ) )
{
AddPolyPt( e1, pt );
e2->outIdx = e1->outIdx;
e1->side = esLeft;
e2->side = esRight;
} else
{
AddPolyPt( e2, pt );
e1->outIdx = e2->outIdx;
e1->side = esRight;
e2->side = esLeft;
}
}
//------------------------------------------------------------------------------
void Clipper::AddLocalMaxPoly(TEdge *e1, TEdge *e2, const IntPoint &pt)
{
AddPolyPt( e1, pt );
if( e1->outIdx == e2->outIdx )
{
e1->outIdx = -1;
e2->outIdx = -1;
}
else
AppendPolygon( e1, e2 );
}
//------------------------------------------------------------------------------
void Clipper::AddEdgeToSEL(TEdge *edge)
{
//SEL pointers in PEdge are reused to build a list of horizontal edges.
//However, we don't need to worry about order with horizontal edge processing.
if( !m_SortedEdges )
{
m_SortedEdges = edge;
edge->prevInSEL = 0;
edge->nextInSEL = 0;
}
else
{
edge->nextInSEL = m_SortedEdges;
edge->prevInSEL = 0;
m_SortedEdges->prevInSEL = edge;
m_SortedEdges = edge;
}
}
//------------------------------------------------------------------------------
void Clipper::CopyAELToSEL()
{
TEdge* e = m_ActiveEdges;
m_SortedEdges = e;
if (!m_ActiveEdges) return;
m_SortedEdges->prevInSEL = 0;
e = e->nextInAEL;
while ( e )
{