/
Clipper.hx
4010 lines (3578 loc) · 117 KB
/
Clipper.hx
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 : 5.1.6 *
* Date : 23 May 2013 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2013 *
* *
* 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: *
* "IPolygon 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 *
* *
*******************************************************************************/
// Ported from C# by @ncannasse
// original version uses 64 bits integer and 128 bit "bigint", we use 32 bit integer instead
// so valid range should be limited to [-32768,32767] in order to prevent overflow while multiplying
package hxd.clipper;
import h2d.col.Point;
import h2d.col.IPoint;
import h2d.col.IPolygon;
import h2d.col.IPolygons;
private enum EdgeSide {
Left;
Right;
}
private enum Direction {
RightToLeft;
LeftToRight;
}
@:allow(hxd.clipper)
private class PolyNode {
public var parent(default,null) : PolyNode;
public var childs(default, null) : Array<PolyNode>;
var polygon : IPolygon;
var index : Int;
var jointype : JoinType;
var endtype : EndType;
public function new() {
polygon = new IPolygon([]);
childs = [];
}
private function isHoleNode() {
var result = true;
var node = parent;
while( node != null ) {
result = !result;
node = node.parent;
}
return result;
}
public var childCount(get, never) : Int;
inline function get_childCount() {
return childs.length;
}
public var contour(get, never) : IPolygon;
inline function get_contour() {
return polygon;
}
function addChild(child:PolyNode) {
var cnt = childs.length;
childs.push(child);
child.parent = this;
child.index = cnt;
}
public function getNext()
{
if (childs.length > 0)
return childs[0];
else
return getNextSiblingUp();
}
function getNextSiblingUp()
{
if (parent == null)
return null;
else if (index == parent.childs.length - 1)
return parent.getNextSiblingUp();
else
return parent.childs[index + 1];
}
public inline function isHole() {
return isHoleNode();
}
}
private class PolyTree extends PolyNode {
public var allPolys : Array<PolyNode>;
public function new() {
super();
allPolys = [];
}
public function toPolygons(polygons:IPolygons) {
polygons = [];
addRec(this, polygons);
}
function addRec(polynode:PolyNode,polygons:IPolygons) {
if (polynode.contour.length > 0)
polygons.push(polynode.contour);
for(pn in polynode.childs)
addRec(pn, polygons);
}
public function clear() {
allPolys = [];
childs = [];
}
public function getFirst() {
if( childs.length > 0)
return childs[0];
return null;
}
public var total(get, never) : Int;
inline function get_total() {
var result = allPolys.length;
if( result > 0 && childs[0] != allPolys[0] ) result--;
return result;
}
}
@:generic
private class Ref<T> {
public var val : T;
public inline function new(?v:T) {
val = v;
}
}
private class TEdge {
public var botX : Int;
public var botY : Int;
public var currX : Int;
public var currY : Int;
public var topX : Int;
public var topY : Int;
public var deltaX : Int;
public var deltaY : Int;
public var dx : Float;
public var polyType : PolyType;
public var side : EdgeSide;
public var windDelta : Int; //1 or -1 depending on winding direction
public var windCnt : Int;
public var windCnt2 : Int; //winding count of the opposite polytype
public var outIdx : Int;
public var next : TEdge;
public var prev : TEdge;
public var nextInLML : TEdge;
public var nextInAEL : TEdge;
public var prevInAEL : TEdge;
public var nextInSEL : TEdge;
public var prevInSEL : TEdge;
public var top(get, set) : IPoint;
public var bot(get, set) : IPoint;
public var curr(get, set) : IPoint;
inline function get_top() return new IPoint(topX, topY);
inline function get_bot() return new IPoint(botX, botY);
inline function get_curr() return new IPoint(currX, currY);
inline function set_top(p:IPoint) {
topX = p.x;
topY = p.y;
return p;
}
inline function set_bot(p:IPoint) {
botX = p.x;
botY = p.y;
return p;
}
inline function set_curr(p:IPoint) {
currX = p.x;
currY = p.y;
return p;
}
public function new() {
}
}
private class IntersectNode {
public var edge1 : TEdge;
public var edge2 : TEdge;
public var pt : IPoint;
public function new() {
}
}
private class LocalMinima {
public var y : Int;
public var leftBound : TEdge;
public var rightBound : TEdge;
public var next : LocalMinima;
public function new() {
}
}
private class Scanbeam {
public var y : Int;
public var next : Scanbeam;
public function new() {
}
}
private class OutRec {
public var idx : Int;
public var isHole : Bool;
public var firstLeft : OutRec;
public var pts : OutPt;
public var bottomPt : OutPt;
public var polyNode : PolyNode;
public function new() {
}
}
private class OutPt {
public var idx : Int;
public var pt : IPoint;
public var next : OutPt;
public var prev : OutPt;
public function new() {
}
}
private class Join {
public var outPt1 : OutPt;
public var outPt2 : OutPt;
public var offPt : IPoint;
public function new() {
}
}
//------------------------------------------------------------------------------
@:noDebug
@:allow(hxd.clipper)
private class ClipperBase
{
static inline var HORIZONTAL = -9007199254740992.; // -2^53, big enough for JS
static inline var TOLERANCE = 1E-20;
static inline var SKIP = -2;
static inline var UNASSIGNED = -1;
public var preserveCollinear : Bool;
var m_MinimaList : LocalMinima;
var m_CurrentLM : LocalMinima;
var m_edges : Array<Array<TEdge>>;
//------------------------------------------------------------------------------
public inline function isHorizontal(e : TEdge) {
return e.deltaY == 0;
}
inline function abs(i:Int):Int {
return i < 0 ? -i : i;
}
public static inline function nearZero (v : Float) : Bool {
return v > -TOLERANCE && v < TOLERANCE;
}
//------------------------------------------------------------------------------
function PointIsVertex(pt:IPoint, pp:OutPt) {
var pp2 = pp;
do {
if( equals(pp2.pt, pt) ) return true;
pp2 = pp2.next;
} while (pp2 != pp);
return false;
}
//------------------------------------------------------------------------------
function PointOnLineSegment(pt:IPoint, linePt1:IPoint, linePt2:IPoint) {
return ((pt.x == linePt1.x) && (pt.y == linePt1.y)) ||
((pt.x == linePt2.x) && (pt.y == linePt2.y)) ||
(((pt.x > linePt1.x) == (pt.x < linePt2.x)) &&
((pt.y > linePt1.y) == (pt.y < linePt2.y)) &&
((pt.x - linePt1.x) * (linePt2.y - linePt1.y) == (linePt2.x - linePt1.x) * (pt.y - linePt1.y)));
}
//------------------------------------------------------------------------------
function PointOnPolygon(pt:IPoint, pp:OutPt)
{
var pp2 = pp;
while (true) {
if (PointOnLineSegment(pt, pp2.pt, pp2.next.pt))
return true;
pp2 = pp2.next;
if (pp2 == pp) break;
}
return false;
}
//------------------------------------------------------------------------------
inline function SlopesEqual(e1:TEdge, e2:TEdge) {
return e1.deltaY * e2.deltaX == e1.deltaX * e2.deltaY;
}
//------------------------------------------------------------------------------
inline function SlopesEqual3(pt1:IPoint, pt2:IPoint, pt3:IPoint) {
return (pt1.y - pt2.y) * (pt2.x - pt3.x) - (pt1.x - pt2.x) * (pt2.y - pt3.y) == 0;
}
//------------------------------------------------------------------------------
function new()
{
m_edges = [];
m_MinimaList = null;
m_CurrentLM = null;
}
//------------------------------------------------------------------------------
public function clear()
{
disposeLocalMinimaList();
m_edges = [];
}
//------------------------------------------------------------------------------
function disposeLocalMinimaList()
{
while( m_MinimaList != null )
{
var tmpLm = m_MinimaList.next;
m_MinimaList = null;
m_MinimaList = tmpLm;
}
m_CurrentLM = null;
}
//------------------------------------------------------------------------------
public function addPolygons(ppg:IPolygons, polyType:PolyType)
{
var result = false;
for ( p in ppg)
if (addPolygon(p, polyType)) result = true;
return result;
}
//------------------------------------------------------------------------------
public function addPolygon(pg:IPolygon, polyType:PolyType)
{
var highI = pg.length - 1;
while(highI > 0 && pg[highI] == pg[0]) highI--;
while(highI > 0 && pg[highI] == pg[highI - 1]) highI--;
if(highI < 2) return false;
//create a new edge array
var edges = [];
for( i in 0...highI + 1)
edges.push(new TEdge());
//basic edge initialization
var isFlat = true;
edges[1].curr = pg[1];
InitEdge(edges[0], edges[1], edges[highI], pg[0]);
InitEdge(edges[highI], edges[0], edges[highI - 1], pg[highI]);
var i = highI - 1;
while(i > 0) {
InitEdge(edges[i], edges[i + 1], edges[i - 1], pg[i]);
i--;
}
//remove duplicate vertices and collinear edges
var eStart = edges[0];
var eStop = eStart;
var e = eStart;
while(true) {
if(e.currX == e.next.currX && e.currY == e.next.currY) {
if(e == e.next) break;
if(e == eStart) eStart = e.next;
e = RemoveEdge(e);
eStop = e;
continue;
}
if(e.prev == e.next) break; //only two vertices
if(SlopesEqual3(e.prev.curr, e.curr, e.next.curr) && (!preserveCollinear || !Pt2IsBetweenPt1AndPt3(e.prev.curr, e.curr, e.next.curr))) {
//the default is to merge adjacent collinear edges into a single edge.
//However, if the PreserveCollinear property is enabled, only overlapping
//collinear edges (ie spikes) will be removed from polygons.
if(e == eStart) eStart = e.next;
e = RemoveEdge(e);
e = e.prev;
eStop = e;
continue;
}
e = e.next;
if(e == eStop) break;
}
if(e.prev == e.next)
return false;
//do second part of edge initialization
e = eStart;
do {
InitEdge2(e, polyType);
e = e.next;
if(e.currY != eStart.currY)
isFlat = false;
}
while(e != eStart);
if(isFlat)
return false;
//add edge bounds to LocalMinima list ...
m_edges.push(edges);
var leftBoundIsForward : Bool;
var eMin = null;
//workaround to avoid an endless loop in the while loop below when
//open paths have matching start and end points ...
if (e.prev.botX == e.prev.topX && e.prev.botY == e.prev.topY) e = e.next;
var old = null;
while(true) {
e = FindNextLocMin(e);
if(e == eMin) break;
else if (eMin == null) eMin = e;
if(e == old) throw("!");
old = e;
//e and e.prev now share a local minima (left aligned if horizontal).
//Compare their slopes to find which starts which bound ...
var locMin = new LocalMinima();
locMin.next = null;
locMin.y = e.botY;
if(e.dx < e.prev.dx) {
locMin.leftBound = e.prev;
locMin.rightBound = e;
leftBoundIsForward = false;
}
else {
locMin.leftBound = e;
locMin.rightBound = e.prev;
leftBoundIsForward = true;
}
locMin.leftBound.side = EdgeSide.Left;
locMin.rightBound.side = EdgeSide.Right;
if (locMin.leftBound.next == locMin.rightBound)
locMin.leftBound.windDelta = -1;
else locMin.leftBound.windDelta = 1;
locMin.rightBound.windDelta = -locMin.leftBound.windDelta;
e = ProcessBound(locMin.leftBound, leftBoundIsForward);
if(e.outIdx == SKIP) e = ProcessBound(e, leftBoundIsForward);
var e2 = ProcessBound(locMin.rightBound, !leftBoundIsForward);
if(e2.outIdx == SKIP) e2 = ProcessBound(e2, !leftBoundIsForward);
if(locMin.leftBound.outIdx == SKIP)
locMin.leftBound = null;
else if(locMin.rightBound.outIdx == SKIP)
locMin.rightBound = null;
InsertLocalMinima(locMin);
if(!leftBoundIsForward)
e = e2;
}
return true;
}
//------------------------------------------------------------------------------
inline function InitEdge(e:TEdge, eNext:TEdge, ePrev:TEdge, pt:IPoint) {
e.next = eNext;
e.prev = ePrev;
e.currX = pt.x;
e.currY = pt.y;
e.outIdx = UNASSIGNED;
}
inline function InitEdge2(e:TEdge, polyType : PolyType ) {
if (e.currY >= e.next.currY) {
e.botX = e.currX;
e.botY = e.currY;
e.topX = e.next.currX;
e.topY = e.next.currY;
}
else {
e.topX = e.currX;
e.topY = e.currY;
e.botX = e.next.currX;
e.botY = e.next.currY;
}
SetDx(e);
e.polyType = polyType;
}
//------------------------------------------------------------------------------
function RemoveEdge(e : TEdge) {
//removes e from double_linked_list (but without removing from memory)
e.prev.next = e.next;
e.next.prev = e.prev;
var result = e.next;
e.prev = null; //flag as removed (see ClipperBase.Clear)
return result;
}
//------------------------------------------------------------------------------
function FindNextLocMin(e : TEdge) {
var e2 : TEdge;
while(true) {
while (e.botX != e.prev.botX || e.botY != e.prev.botY || (e.currX == e.topX && e.currY == e.topY)) e = e.next;
if (e.dx != HORIZONTAL && e.prev.dx != HORIZONTAL) break;
while (e.prev.dx == HORIZONTAL) e = e.prev;
e2 = e;
while (e.dx == HORIZONTAL) e = e.next;
if (e.topY == e.prev.botY) continue; //ie just an intermediate horz.
if (e2.prev.botX < e.botX) e = e2;
break;
}
return e;
}
//------------------------------------------------------------------------------
function ProcessBound(E : TEdge, LeftBoundIsForward : Bool) {
var EStart : TEdge;
var Horz : TEdge;
var Result = E;
if (Result.outIdx == SKIP)
{
//check if there are edges beyond the skip edge in the bound and if so
//create another LocMin and calling ProcessBound once more ...
E = Result;
if (LeftBoundIsForward) {
while (E.topY == E.next.botY) E = E.next;
while (E != Result && E.dx == HORIZONTAL) E = E.prev;
}
else {
while (E.topY == E.prev.botY) E = E.prev;
while (E != Result && E.dx == HORIZONTAL) E = E.next;
}
if (E == Result)
{
if (LeftBoundIsForward) Result = E.next;
else Result = E.prev;
}
else
{
//there are more edges in the bound beyond result starting with E
if (LeftBoundIsForward)
E = Result.next;
else E = Result.prev;
var locMin = new LocalMinima();
locMin.next = null;
locMin.y = E.botY;
locMin.leftBound = null;
locMin.rightBound = E;
E.windDelta = 0;
Result = ProcessBound(E, LeftBoundIsForward);
InsertLocalMinima(locMin);
}
return Result;
}
if (E.dx == HORIZONTAL)
{
//We need to be careful with open paths because this may not be a
//true local minima (ie E may be following a skip edge).
//Also, consecutive horz. edges may start heading left before going right.
if (LeftBoundIsForward) EStart = E.prev;
else EStart = E.next;
if (EStart.outIdx != SKIP)
{
if (EStart.dx == HORIZONTAL) //ie an adjoining horizontal skip edge
{
if (EStart.botX != E.botX && EStart.topX != E.botX)
ReverseHorizontal(E);
}
else if (EStart.botX != E.botX)
ReverseHorizontal(E);
}
}
EStart = E;
if (LeftBoundIsForward)
{
while (Result.topY == Result.next.botY && Result.next.outIdx != SKIP)
Result = Result.next;
if (Result.dx == HORIZONTAL && Result.next.outIdx != SKIP)
{
//nb: at the top of a bound, horizontals are added to the bound
//only when the preceding edge attaches to the horizontal's left vertex
//unless a Skip edge is encountered when that becomes the top divide
Horz = Result;
while (Horz.prev.dx == HORIZONTAL) Horz = Horz.prev;
if (Horz.prev.topX == Result.next.topX)
{
if (!LeftBoundIsForward) Result = Horz.prev;
}
else if (Horz.prev.topX > Result.next.topX) Result = Horz.prev;
}
while (E != Result)
{
E.nextInLML = E.next;
if (E.dx == HORIZONTAL && E != EStart && E.botX != E.prev.topX)
ReverseHorizontal(E);
E = E.next;
}
if (E.dx == HORIZONTAL && E != EStart && E.botX != E.prev.topX)
ReverseHorizontal(E);
Result = Result.next; //move to the edge just beyond current bound
}
else
{
while (Result.topY == Result.prev.botY && Result.prev.outIdx != SKIP)
Result = Result.prev;
if (Result.dx == HORIZONTAL && Result.prev.outIdx != SKIP)
{
Horz = Result;
while (Horz.next.dx == HORIZONTAL) Horz = Horz.next;
if (Horz.next.topX == Result.prev.topX)
{
if (!LeftBoundIsForward) Result = Horz.next;
}
else if (Horz.next.topX > Result.prev.topX) Result = Horz.next;
}
while (E != Result)
{
E.nextInLML = E.prev;
if (E.dx == HORIZONTAL && E != EStart && E.botX != E.next.topX)
ReverseHorizontal(E);
E = E.prev;
}
if (E.dx == HORIZONTAL && E != EStart && E.botX != E.next.topX)
ReverseHorizontal(E);
Result = Result.prev; //move to the edge just beyond current bound
}
return Result;
}
//------------------------------------------------------------------------------
function ReverseHorizontal(e : TEdge)
{
//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.]
var tmp = e.topX;
e.topX = e.botX;
e.botX = tmp;
}
//------------------------------------------------------------------------------
function Pt2IsBetweenPt1AndPt3(pt1 : IPoint, pt2 : IPoint, pt3 : IPoint) {
if (equals(pt1, pt3) || equals(pt1, pt2) || equals(pt3, pt2)) return false;
else if (pt1.x != pt3.x) return (pt2.x > pt1.x) == (pt2.x < pt3.x);
else return (pt2.y > pt1.y) == (pt2.y < pt3.y);
}
//------------------------------------------------------------------------------
private function SetDx(e:TEdge)
{
e.deltaX = (e.topX - e.botX);
e.deltaY = (e.topY - e.botY);
if (e.deltaY == 0) e.dx = HORIZONTAL;
else e.dx = e.deltaX / e.deltaY;
}
//---------------------------------------------------------------------------
private function InsertLocalMinima(newLm:LocalMinima)
{
if( m_MinimaList == null )
{
m_MinimaList = newLm;
}
else if( newLm.y >= m_MinimaList.y )
{
newLm.next = m_MinimaList;
m_MinimaList = newLm;
} else
{
var tmpLm = m_MinimaList;
while( tmpLm.next != null && ( newLm.y < tmpLm.next.y ) )
tmpLm = tmpLm.next;
newLm.next = tmpLm.next;
tmpLm.next = newLm;
}
}
//------------------------------------------------------------------------------
function PopLocalMinima() {
if (m_CurrentLM == null) return;
m_CurrentLM = m_CurrentLM.next;
}
//------------------------------------------------------------------------------
private inline function SwapX(e:TEdge)
{
//swap ClipperBase.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.currX = e.topX;
e.topX = e.botX;
e.botX = e.currX;
}
//------------------------------------------------------------------------------
inline function equals(pt1 : IPoint, pt2 : IPoint) {
return pt1.x == pt2.x && pt1.y == pt2.y;
}
function Reset()
{
m_CurrentLM = m_MinimaList;
//reset all edges
var lm = m_MinimaList;
while (lm != null)
{
var e = lm.leftBound;
if (e != null)
{
e.currX = e.botX;
e.currY = e.botY;
e.side = EdgeSide.Left;
e.outIdx = UNASSIGNED;
e = e.nextInLML;
}
e = lm.rightBound;
if (e != null)
{
e.currX = e.botX;
e.currY = e.botY;
e.side = EdgeSide.Right;
e.outIdx = UNASSIGNED;
e = e.nextInLML;
}
lm = lm.next;
}
return;
}
//------------------------------------------------------------------------------
public static function getBounds(pols : IPolygons) {
var result = new Rect();
var i = 0;
var count = pols.length;
while(i < count && pols[i].length == 0) i++;
if(i == count) result;
result.left = result.right = pols[i][0].x;
result.top = result.bottom = pols[i][0].y;
for( i in 0...count)
for(p in pols[i]) {
if(p.x < result.left) result.left = p.x;
else if(p.x > result.right) result.right = p.x;
if(p.y < result.top) result.top = p.y;
else if(p.y > result.bottom) result.bottom = p.y;
}
return result;
}
} //ClipperBase
enum NodeType {
Any;
Open;
Closed;
}
enum ResultKind {
All;
NoHoles;
HolesOnly;
}
@:noDebug
@:allow(hxd.clipper)
class Clipper extends ClipperBase {
public var strictlySimple : Bool;
public var reverseSolution : Bool;
public var resultKind : ResultKind;
var m_PolyOuts : Array<OutRec>;
var m_ClipType : ClipType;
var m_Scanbeam : Scanbeam;
var m_ActiveEdges : TEdge;
var m_SortedEdges : TEdge;
var m_IntersectList : Array<IntersectNode>;
var m_ExecuteLocked : Bool;
var m_ClipFillType : PolyFillType;
var m_SubjFillType : PolyFillType;
var m_Joins : Array<Join>;
var m_GhostJoins : Array<Join>;
var m_UsingPolyTree : Bool;
public function new()
{
super();
m_Scanbeam = null;
m_ActiveEdges = null;
m_SortedEdges = null;
m_IntersectList = [];
m_ExecuteLocked = false;
m_UsingPolyTree = false;
m_PolyOuts = [];
m_Joins = [];
m_GhostJoins = [];
reverseSolution = false;
strictlySimple = false;
preserveCollinear = false;
resultKind = All;
}
inline function xor(a, b) {
return if( a ) !b else b;
}
//------------------------------------------------------------------------------
function DisposeScanbeamList()
{
while ( m_Scanbeam != null ) {
var sb2 = m_Scanbeam.next;
m_Scanbeam = null;
m_Scanbeam = sb2;
}
}
//------------------------------------------------------------------------------
override function Reset()
{
super.Reset();
m_Scanbeam = null;
m_ActiveEdges = null;
m_SortedEdges = null;
var lm = m_MinimaList;
while (lm != null)
{
InsertScanbeam(lm.y);
lm = lm.next;
}
}
//------------------------------------------------------------------------------
private function InsertScanbeam(y:Int)
{
if( m_Scanbeam == null ) {
m_Scanbeam = new Scanbeam();
m_Scanbeam.next = null;
m_Scanbeam.y = y;
}
else if( y > m_Scanbeam.y ) {
var newSb = new Scanbeam();
newSb.y = y;
newSb.next = m_Scanbeam;
m_Scanbeam = newSb;
} else {
var sb2 = m_Scanbeam;
while( sb2.next != null && ( y <= sb2.next.y ) ) sb2 = sb2.next;
if( y == sb2.y ) return; //ie ignores duplicates
var newSb = new Scanbeam();
newSb.y = y;
newSb.next = sb2.next;
sb2.next = newSb;
}
}
//------------------------------------------------------------------------------
public function execute(clipType:ClipType, ?subjFillType, ?clipFillType)
{
if( subjFillType == null ) subjFillType = PolyFillType.EvenOdd;
if( clipFillType == null ) clipFillType = PolyFillType.EvenOdd;
if (m_ExecuteLocked) return [];
m_ExecuteLocked = true;
var solution = [];
m_SubjFillType = subjFillType;
m_ClipFillType = clipFillType;
m_ClipType = clipType;
m_UsingPolyTree = false;
var succeeded = ExecuteInternal();
if (succeeded) solution = BuildResult();
DisposeAllPolyPts();
m_ExecuteLocked = false;
return solution;
}
//------------------------------------------------------------------------------
public function ExecuteTree(clipType:ClipType, polytree:PolyTree, ?subjFillType, ?clipFillType ) {
if( subjFillType == null ) subjFillType = PolyFillType.EvenOdd;
if( clipFillType == null ) clipFillType = PolyFillType.EvenOdd;
if (m_ExecuteLocked) return false;
m_ExecuteLocked = true;
m_SubjFillType = subjFillType;
m_ClipFillType = clipFillType;
m_ClipType = clipType;
m_UsingPolyTree = true;
var succeeded = ExecuteInternal();
//build the return polygons
if (succeeded) BuildResult2(polytree);
m_ExecuteLocked = false;
return succeeded;
}
//------------------------------------------------------------------------------
function FixHoleLinkage(outRec:OutRec) {
//skip if an outermost polygon or
//already already points to the correct firstLeft
if (outRec.firstLeft == null ||
(outRec.isHole != outRec.firstLeft.isHole &&
outRec.firstLeft.pts != null)) return;
var orfl:OutRec = outRec.firstLeft;
while (orfl != null && ((orfl.isHole == outRec.isHole) || orfl.pts == null))
orfl = orfl.firstLeft;
outRec.firstLeft = orfl;
}
//------------------------------------------------------------------------------
private function ExecuteInternal() {
Reset();
if (m_CurrentLM == null) return false;
var botY = PopScanbeam();
do
{
InsertLocalMinimaIntoAEL(botY);
if( m_GhostJoins.length > 0 ) m_GhostJoins = [];
ProcessHorizontals(false);
if(m_Scanbeam == null) break;
var topY = PopScanbeam();