-
-
Notifications
You must be signed in to change notification settings - Fork 364
/
clipper.cpp
3302 lines (3033 loc) · 99.4 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.6.3 *
* Date : 11 November 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 *
* *
* See also: *
* "Polygon Offsetting by Computing Winding Numbers" *
* Paper no. DETC2005-85513 pp. 565-575 *
* ASME 2005 International Design Engineering Technical Conferences *
* and Computers and Information in Engineering Conference (IDETC/CIE2005) *
* September 24-28, 2005 , Long Beach, California, USA *
* http://www.me.berkeley.edu/~mcmains/pubs/DAC05OffsetPolygon.pdf *
* *
*******************************************************************************/
/*******************************************************************************
* *
* This is a translation of the Delphi Clipper library and the naming style *
* used has retained a Delphi flavour. *
* *
*******************************************************************************/
#include "../include/clipper.hpp"
#include <cmath>
#include <vector>
#include <algorithm>
#include <stdexcept>
#include <cstring>
#include <cstdlib>
#include <ostream>
namespace ClipperLib {
static long64 const loRange = 1518500249; //sqrt(2^63 -1)/2
static long64 const hiRange = 6521908912666391106LL; //sqrt(2^127 -1)/2
static double const pi = 3.141592653589793238;
enum Direction { dRightToLeft, dLeftToRight };
enum RangeTest { rtLo, rtHi, rtError };
#define HORIZONTAL (-1.0E+40)
#define TOLERANCE (1.0e-20)
#define NEAR_ZERO(val) (((val) > -TOLERANCE) && ((val) < TOLERANCE))
#define NEAR_EQUAL(a, b) NEAR_ZERO((a) - (b))
inline long64 Abs(long64 val)
{
if (val < 0) return -val; else return val;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Int128 class (enables safe math on signed 64bit integers)
// eg Int128 val1((long64)9223372036854775807); //ie 2^63 -1
// Int128 val2((long64)9223372036854775807);
// Int128 val3 = val1 * val2;
// val3.AsString => "85070591730234615847396907784232501249" (8.5e+37)
//------------------------------------------------------------------------------
class Int128
{
public:
Int128(long64 _lo = 0)
{
hi = 0;
if (_lo < 0) {
lo = -_lo;
Negate(*this);
} else
lo = _lo;
}
Int128(const Int128 &val): hi(val.hi), lo(val.lo){}
long64 operator = (const long64 &val)
{
hi = 0;
lo = Abs(val);
if (val < 0) Negate(*this);
return val;
}
bool operator == (const Int128 &val) const
{return (hi == val.hi && lo == val.lo);}
bool operator != (const Int128 &val) const { return !(*this == val);}
bool operator > (const Int128 &val) const
{
if (hi > val.hi) return true;
else if (hi < val.hi) return false;
else return ulong64(lo) > ulong64(val.lo);
}
bool operator < (const Int128 &val) const
{
if (hi < val.hi) return true;
else if (hi > val.hi) return false;
else return ulong64(lo) < ulong64(val.lo);
}
Int128& operator += (const Int128 &rhs)
{
hi += rhs.hi;
lo += rhs.lo;
if (ulong64(lo) < ulong64(rhs.lo)) hi++;
return *this;
}
Int128 operator + (const Int128 &rhs) const
{
Int128 result(*this);
result+= rhs;
return result;
}
Int128& operator -= (const Int128 &rhs)
{
Int128 tmp(rhs);
Negate(tmp);
*this += tmp;
return *this;
}
Int128 operator - (const Int128 &rhs) const
{
Int128 result(*this);
result-= rhs;
return result;
}
Int128 operator * (const Int128 &rhs) const {
if ( !(hi == 0 || hi == -1) || !(rhs.hi == 0 || rhs.hi == -1))
throw "Int128 operator*: overflow error";
bool negate = (hi < 0) != (rhs.hi < 0);
Int128 tmp(*this);
if (tmp.hi < 0) Negate(tmp);
ulong64 int1Hi = ulong64(tmp.lo) >> 32;
ulong64 int1Lo = ulong64(tmp.lo & 0xFFFFFFFF);
tmp = rhs;
if (tmp.hi < 0) Negate(tmp);
ulong64 int2Hi = ulong64(tmp.lo) >> 32;
ulong64 int2Lo = ulong64(tmp.lo & 0xFFFFFFFF);
//nb: see comments in clipper.pas
ulong64 a = int1Hi * int2Hi;
ulong64 b = int1Lo * int2Lo;
ulong64 c = int1Hi * int2Lo + int1Lo * int2Hi;
tmp.hi = long64(a + (c >> 32));
tmp.lo = long64(c << 32);
tmp.lo += long64(b);
if (ulong64(tmp.lo) < b) tmp.hi++;
if (negate) Negate(tmp);
return tmp;
}
Int128 operator/ (const Int128 &rhs) const
{
if (rhs.lo == 0 && rhs.hi == 0)
throw "Int128 operator/: divide by zero";
bool negate = (rhs.hi < 0) != (hi < 0);
Int128 result(*this), denom(rhs);
if (result.hi < 0) Negate(result);
if (denom.hi < 0) Negate(denom);
if (denom > result) return Int128(0); //result is only a fraction of 1
Negate(denom);
Int128 p(0);
for (int i = 0; i < 128; ++i)
{
p.hi = p.hi << 1;
if (p.lo < 0) p.hi++;
p.lo = long64(p.lo) << 1;
if (result.hi < 0) p.lo++;
result.hi = result.hi << 1;
if (result.lo < 0) result.hi++;
result.lo = long64(result.lo) << 1;
Int128 p2(p);
p += denom;
if (p.hi < 0) p = p2;
else result.lo++;
}
if (negate) Negate(result);
return result;
}
double AsDouble() const
{
const double shift64 = 18446744073709551616.0; //2^64
const double bit64 = 9223372036854775808.0;
if (hi < 0)
{
Int128 tmp(*this);
Negate(tmp);
if (tmp.lo < 0)
return (double)tmp.lo - bit64 - tmp.hi * shift64;
else
return -(double)tmp.lo - tmp.hi * shift64;
}
else if (lo < 0)
return -(double)lo + bit64 + hi * shift64;
else
return (double)lo + (double)hi * shift64;
}
//for bug testing ...
std::string AsString() const
{
std::string result;
unsigned char r = 0;
Int128 tmp(0), val(*this);
if (hi < 0) Negate(val);
result.resize(50);
std::string::size_type i = result.size() -1;
while (val.hi != 0 || val.lo != 0)
{
Div10(val, tmp, r);
result[i--] = char('0' + r);
val = tmp;
}
if (hi < 0) result[i--] = '-';
result.erase(0,i+1);
if (result.size() == 0) result = "0";
return result;
}
private:
long64 hi;
long64 lo;
static void Negate(Int128 &val)
{
if (val.lo == 0)
{
if( val.hi == 0) return;
val.lo = ~val.lo;
val.hi = ~val.hi +1;
}
else
{
val.lo = ~val.lo +1;
val.hi = ~val.hi;
}
}
//debugging only ...
void Div10(const Int128 val, Int128& result, unsigned char & remainder) const
{
remainder = 0;
result = 0;
for (int i = 63; i >= 0; --i)
{
if ((val.hi & ((long64)1 << i)) != 0)
remainder = char((remainder * 2) + 1); else
remainder *= char(2);
if (remainder >= 10)
{
result.hi += ((long64)1 << i);
remainder -= char(10);
}
}
for (int i = 63; i >= 0; --i)
{
if ((val.lo & ((long64)1 << i)) != 0)
remainder = char((remainder * 2) + 1); else
remainder *= char(2);
if (remainder >= 10)
{
result.lo += ((long64)1 << i);
remainder -= char(10);
}
}
}
};
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
RangeTest TestRange(const Polygon &pts)
{
RangeTest result = rtLo;
for (Polygon::size_type i = 0; i < pts.size(); ++i)
{
if (Abs(pts[i].X) > hiRange || Abs(pts[i].Y) > hiRange)
return rtError;
else if (Abs(pts[i].X) > loRange || Abs(pts[i].Y) > loRange)
result = rtHi;
}
return result;
}
//------------------------------------------------------------------------------
bool Orientation(const Polygon &poly)
{
int highI = (int)poly.size() -1;
if (highI < 2) return false;
bool UseFullInt64Range = false;
int j = 0, jplus, jminus;
for (int i = 0; i <= highI; ++i)
{
if (Abs(poly[i].X) > hiRange || Abs(poly[i].Y) > hiRange)
throw "Coordinate exceeds range bounds.";
if (Abs(poly[i].X) > loRange || Abs(poly[i].Y) > loRange)
UseFullInt64Range = true;
if (poly[i].Y < poly[j].Y) continue;
if ((poly[i].Y > poly[j].Y || poly[i].X < poly[j].X)) j = i;
};
if (j == highI) jplus = 0;
else jplus = j +1;
if (j == 0) jminus = highI;
else jminus = j -1;
IntPoint vec1, vec2;
//get cross product of vectors of the edges adjacent to highest point ...
vec1.X = poly[j].X - poly[jminus].X;
vec1.Y = poly[j].Y - poly[jminus].Y;
vec2.X = poly[jplus].X - poly[j].X;
vec2.Y = poly[jplus].Y - poly[j].Y;
if (UseFullInt64Range)
{
Int128 cross = Int128(vec1.X) * Int128(vec2.Y) -
Int128(vec2.X) * Int128(vec1.Y);
return cross > 0;
}
else
{
return (vec1.X * vec2.Y - vec2.X * vec1.Y) > 0;
}
}
//------------------------------------------------------------------------------
bool Orientation(OutRec *outRec, bool UseFullInt64Range)
{
OutPt *opBottom = outRec->pts, *op = outRec->pts->next;
while (op != outRec->pts)
{
if (op->pt.Y >= opBottom->pt.Y)
{
if (op->pt.Y > opBottom->pt.Y || op->pt.X < opBottom->pt.X)
opBottom = op;
}
op = op->next;
}
IntPoint vec1, vec2;
vec1.X = op->pt.X - op->prev->pt.X;
vec1.Y = op->pt.Y - op->prev->pt.Y;
vec2.X = op->next->pt.X - op->pt.X;
vec2.Y = op->next->pt.Y - op->pt.Y;
if (UseFullInt64Range)
{
Int128 cross = Int128(vec1.X) * Int128(vec2.Y) - Int128(vec2.X) * Int128(vec1.Y);
return cross > 0;
}
else
{
return (vec1.X * vec2.Y - vec2.X * vec1.Y) > 0;
}
}
//------------------------------------------------------------------------------
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 = (int)poly.size() -1;
if (highI < 2) return 0;
bool UseFullInt64Range;
RangeTest rt = TestRange(poly);
switch (rt) {
case rtLo:
UseFullInt64Range = false;
break;
case rtHi:
UseFullInt64Range = true;
break;
default:
throw "Coordinate exceeds range bounds.";
}
if (UseFullInt64Range) {
Int128 a(0);
a = (Int128(poly[highI].X) * Int128(poly[0].Y)) -
Int128(poly[0].X) * Int128(poly[highI].Y);
for (int i = 0; i < highI; ++i)
a += Int128(poly[i].X) * Int128(poly[i+1].Y) -
Int128(poly[i+1].X) * Int128(poly[i].Y);
return a.AsDouble() / 2;
}
else
{
double a;
a = (double)poly[highI].X * poly[0].Y - (double)poly[0].X * poly[highI].Y;
for (int i = 0; i < highI; ++i)
a += (double)poly[i].X * poly[i+1].Y - (double)poly[i+1].X * poly[i].Y;
return a/2;
}
}
//------------------------------------------------------------------------------
bool PointIsVertex(const IntPoint &pt, OutPt *pp)
{
OutPt *pp2 = pp;
do
{
if (PointsEqual(pp2->pt, pt)) return true;
pp2 = pp2->next;
}
while (pp2 != pp);
return false;
}
//------------------------------------------------------------------------------
bool PointInPolygon(const IntPoint &pt, OutPt *pp, bool UseFullInt64Range)
{
OutPt *pp2 = pp;
bool result = false;
if (UseFullInt64Range) {
do
{
if ((((pp2->pt.Y <= pt.Y) && (pt.Y < pp2->prev->pt.Y)) ||
((pp2->prev->pt.Y <= pt.Y) && (pt.Y < pp2->pt.Y))) &&
Int128(pt.X - pp2->pt.X) < (Int128(pp2->prev->pt.X - pp2->pt.X) *
Int128(pt.Y - pp2->pt.Y)) / Int128(pp2->prev->pt.Y - pp2->pt.Y))
result = !result;
pp2 = pp2->next;
}
while (pp2 != pp);
}
else
{
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->prev->pt.X - pp2->pt.X) * (pt.Y - pp2->pt.Y) /
(pp2->prev->pt.Y - pp2->pt.Y) + pp2->pt.X )) result = !result;
pp2 = pp2->next;
}
while (pp2 != pp);
}
return result;
}
//------------------------------------------------------------------------------
bool SlopesEqual(TEdge &e1, TEdge &e2, bool UseFullInt64Range)
{
if (e1.ybot == e1.ytop) return (e2.ybot == e2.ytop);
else if (e1.xbot == e1.xtop) return (e2.xbot == e2.xtop);
else if (UseFullInt64Range)
return Int128(e1.ytop - e1.ybot) * Int128(e2.xtop - e2.xbot) ==
Int128(e1.xtop - e1.xbot) * Int128(e2.ytop - e2.ybot);
else return (e1.ytop - e1.ybot)*(e2.xtop - e2.xbot) ==
(e1.xtop - e1.xbot)*(e2.ytop - e2.ybot);
}
//------------------------------------------------------------------------------
bool SlopesEqual(const IntPoint pt1, const IntPoint pt2,
const IntPoint pt3, bool UseFullInt64Range)
{
if (pt1.Y == pt2.Y) return (pt2.Y == pt3.Y);
else if (pt1.X == pt2.X) return (pt2.X == pt3.X);
else if (UseFullInt64Range)
return Int128(pt1.Y-pt2.Y) * Int128(pt2.X-pt3.X) ==
Int128(pt1.X-pt2.X) * Int128(pt2.Y-pt3.Y);
else return (pt1.Y-pt2.Y)*(pt2.X-pt3.X) == (pt1.X-pt2.X)*(pt2.Y-pt3.Y);
}
//------------------------------------------------------------------------------
bool SlopesEqual(const IntPoint pt1, const IntPoint pt2,
const IntPoint pt3, const IntPoint pt4, bool UseFullInt64Range)
{
if (pt1.Y == pt2.Y) return (pt3.Y == pt4.Y);
else if (pt1.X == pt2.X) return (pt3.X == pt4.X);
else if (UseFullInt64Range)
return Int128(pt1.Y-pt2.Y) * Int128(pt3.X-pt4.X) ==
Int128(pt1.X-pt2.X) * Int128(pt3.Y-pt4.Y);
else return (pt1.Y-pt2.Y)*(pt3.X-pt4.X) == (pt1.X-pt2.X)*(pt3.Y-pt4.Y);
}
//------------------------------------------------------------------------------
double GetDx(const IntPoint pt1, const IntPoint pt2)
{
if (pt1.Y == pt2.Y) return HORIZONTAL;
else return
(double)(pt2.X - pt1.X) / (double)(pt2.Y - pt1.Y);
}
//---------------------------------------------------------------------------
void SetDx(TEdge &e)
{
if (e.ybot == e.ytop) e.dx = HORIZONTAL;
else e.dx =
(double)(e.xtop - e.xbot) / (double)(e.ytop - e.ybot);
}
//---------------------------------------------------------------------------
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);
}
//------------------------------------------------------------------------------
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 = (double)(pt1.X-pt2.X)/(double)(pt1.Y-pt2.Y);
return Round(pt1.X + (currentY - pt1.Y) *q);
}
}
//------------------------------------------------------------------------------
bool IntersectPoint(TEdge &edge1, TEdge &edge2,
IntPoint &ip, bool UseFullInt64Range)
{
double b1, b2;
if (SlopesEqual(edge1, edge2, UseFullInt64Range)) return false;
else if (NEAR_ZERO(edge1.dx))
{
ip.X = edge1.xbot;
if (NEAR_EQUAL(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 (NEAR_ZERO(edge2.dx))
{
ip.X = edge2.xbot;
if (NEAR_EQUAL(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(OutPt &pp)
{
OutPt *pp1, *pp2;
pp1 = &pp;
do {
pp2 = pp1->next;
pp1->next = pp1->prev;
pp1->prev = pp2;
pp1 = pp2;
} while( pp1 != &pp );
}
//------------------------------------------------------------------------------
void DisposeOutPts(OutPt*& pp)
{
if (pp == 0) return;
pp->prev->next = 0;
while( pp )
{
OutPt *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;
}
}
//------------------------------------------------------------------------------
OutPt* PolygonBottom(OutPt* pp)
{
OutPt* p = pp->next;
OutPt* 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(OutPt* &pp, IntPoint &pt1, IntPoint &pt2)
{
//outPt1 & outPt2 => the overlap segment (if the function returns true)
if (!pp) return false;
OutPt* pp2 = pp;
IntPoint pt1a = pt1, pt2a = pt2;
do
{
if (SlopesEqual(pt1a, pt2a, pp->pt, pp->prev->pt, true) &&
SlopesEqual(pt1a, pt2a, pp->pt, true) &&
GetOverlapSegment(pt1a, pt2a, pp->pt, pp->prev->pt, pt1, pt2))
return true;
pp = pp->next;
}
while (pp != pp2);
return false;
}
//------------------------------------------------------------------------------
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);
}
//------------------------------------------------------------------------------
OutPt* InsertPolyPtBetween(OutPt* p1, OutPt* p2, const IntPoint pt)
{
if (p1 == p2) throw "JoinError";
OutPt* result = new OutPt;
result->pt = pt;
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;
m_UseFullRange = true;
}
//------------------------------------------------------------------------------
ClipperBase::~ClipperBase() //destructor
{
Clear();
}
//------------------------------------------------------------------------------
bool ClipperBase::AddPolygon( const Polygon &pg, PolyType polyType)
{
int len = (int)pg.size();
if (len < 3) return false;
Polygon p(len);
p[0] = pg[0];
int j = 0;
long64 maxVal;
if (m_UseFullRange) maxVal = hiRange; else maxVal = loRange;
for (int i = 0; i < len; ++i)
{
if (Abs(pg[i].X) > maxVal || Abs(pg[i].Y) > maxVal)
{
if (m_UseFullRange)
throw "Coordinate exceeds range bounds";
maxVal = hiRange;
if (Abs(pg[i].X) > maxVal || Abs(pg[i].Y) > maxVal)
throw "Coordinate exceeds range bounds";
m_UseFullRange = true;
}
if (i == 0 || PointsEqual(p[j], pg[i])) continue;
else if (j > 0 && SlopesEqual(p[j-1], p[j], pg[i], m_UseFullRange))
{
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], m_UseFullRange))
p[0] = p[j--];
else if (SlopesEqual(p[j-1], p[j], p[0], m_UseFullRange)) j--;
else if (SlopesEqual(p[0], p[1], p[2], m_UseFullRange))
{
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 (NEAR_EQUAL(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 (NEAR_EQUAL(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 ( NEAR_EQUAL(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 && !NEAR_EQUAL(e->next->dx, HORIZONTAL) ) break;
e->nextInLML = e->next;
e = e->next;
if ( NEAR_EQUAL(e->dx, HORIZONTAL) && e->xbot != e->prev->xtop) SwapX(*e);
}
return e->next;
}
//------------------------------------------------------------------------------
bool ClipperBase::AddPolygons(const Polygons &ppg, PolyType polyType)
{
bool result = true;
for (Polygons::size_type i = 0; i < ppg.size(); ++i)
if (AddPolygon(ppg[i], polyType)) result = false;
return result;
}
//------------------------------------------------------------------------------
void ClipperBase::Clear()
{
DisposeLocalMinimaList();
for (EdgeList::size_type i = 0; i < m_edges.size(); ++i) delete [] m_edges[i];
m_edges.clear();
m_UseFullRange = false;
}
//------------------------------------------------------------------------------
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 )
{