-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathClipperBase.java
More file actions
2976 lines (2645 loc) · 75.7 KB
/
ClipperBase.java
File metadata and controls
2976 lines (2645 loc) · 75.7 KB
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
package clipper2.engine;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.NavigableSet;
import java.util.TreeSet;
import clipper2.Clipper;
import clipper2.Nullable;
import clipper2.core.ClipType;
import clipper2.core.FillRule;
import clipper2.core.InternalClipper;
import clipper2.core.Path64;
import clipper2.core.PathType;
import clipper2.core.Paths64;
import clipper2.core.Point64;
import clipper2.core.Rect64;
/**
* Subject and Clip paths are passed to a Clipper object via AddSubject,
* AddOpenSubject and AddClip methods. Clipping operations are then initiated by
* calling Execute. And Execute can be called multiple times (ie with different
* ClipTypes & FillRules) without having to reload these paths.
*/
abstract class ClipperBase {
private ClipType cliptype = ClipType.NoClip;
private FillRule fillrule = FillRule.EvenOdd;
private Active actives = null;
private Active sel = null;
private List<LocalMinima> minimaList;
private List<IntersectNode> intersectList;
private List<Vertex> vertexList;
private List<OutRec> outrecList;
private NavigableSet<Long> scanlineSet;
private List<HorzSegment> horzSegList;
private List<HorzJoin> horzJoinList;
private int currentLocMin;
private long currentBotY;
private boolean isSortedMinimaList;
private boolean hasOpenPaths;
boolean usingPolytree;
boolean succeeded;
private boolean preserveCollinear;
private boolean reverseSolution;
/**
* Path data structure for clipping solutions.
*/
static class OutRec {
int idx;
@Nullable
OutRec owner;
@Nullable
Active frontEdge;
@Nullable
Active backEdge;
@Nullable
OutPt pts;
@Nullable
PolyPathBase polypath;
Rect64 bounds = new Rect64();
Path64 path = new Path64();
boolean isOpen;
@Nullable
public List<Integer> splits = null;
@Nullable
OutRec recursiveSplit;
}
private static class HorzSegment {
public @Nullable OutPt leftOp;
public @Nullable OutPt rightOp;
public boolean leftToRight;
public HorzSegment(OutPt op) {
leftOp = op;
rightOp = null;
leftToRight = true;
}
}
private static class HorzJoin {
public @Nullable OutPt op1;
public @Nullable OutPt op2;
public HorzJoin(OutPt ltor, OutPt rtol) {
op1 = ltor;
op2 = rtol;
}
}
static class Active {
Point64 bot;
Point64 top;
long curX; // current (updated at every new scanline)
double dx;
int windDx; // 1 or -1 depending on winding direction
int windCount;
int windCount2; // winding count of the opposite polytype
@Nullable
OutRec outrec;
// AEL: 'active edge list' (Vatti's AET - active edge table)
// a linked list of all edges (from left to right) that are present
// (or 'active') within the current scanbeam (a horizontal 'beam' that
// sweeps from bottom to top over the paths in the clipping operation).
@Nullable
Active prevInAEL;
@Nullable
Active nextInAEL;
// SEL: 'sorted edge list' (Vatti's ST - sorted table)
// linked list used when sorting edges into their new positions at the
// top of scanbeams, but also (re)used to process horizontals.
@Nullable
Active prevInSEL;
@Nullable
Active nextInSEL;
@Nullable
Active jump;
@Nullable
Vertex vertexTop;
LocalMinima localMin = new LocalMinima(); // the bottom of an edge 'bound' (also Vatti)
boolean isLeftBound;
JoinWith joinWith = JoinWith.None;
}
private static class ClipperEngine {
private static <T> void EnsureCapacity(List<T> list, int minCapacity) {
if (list instanceof ArrayList<?>) {
((ArrayList<T>) list).ensureCapacity(minCapacity);
}
}
private static void AddLocMin(Vertex vert, PathType polytype, boolean isOpen, List<LocalMinima> minimaList) {
// make sure the vertex is added only once ...
if ((vert.flags & VertexFlags.LocalMin) != VertexFlags.None) {
return;
}
vert.flags |= VertexFlags.LocalMin;
LocalMinima lm = new LocalMinima(vert, polytype, isOpen);
minimaList.add(lm);
}
private static void AddPathsToVertexList(Paths64 paths, PathType polytype, boolean isOpen, List<LocalMinima> minimaList, List<Vertex> vertexList) {
int totalVertCnt = 0;
for (Path64 path : paths) {
totalVertCnt += path.size();
}
EnsureCapacity(vertexList, vertexList.size() + totalVertCnt);
for (Path64 path : paths) {
Vertex v0 = null, prevV = null, currV;
for (Point64 pt : path) {
if (v0 == null) {
v0 = new Vertex(pt, VertexFlags.None, null);
vertexList.add(v0);
prevV = v0;
} else if (prevV.pt.opNotEquals(pt)) { // ie skips duplicates
currV = new Vertex(pt, VertexFlags.None, prevV);
vertexList.add(currV);
prevV.next = currV;
prevV = currV;
}
}
if (prevV == null || prevV.prev == null) {
continue;
}
if (!isOpen && v0.pt.opEquals(prevV.pt)) {
prevV = prevV.prev;
}
prevV.next = v0;
v0.prev = prevV;
if (!isOpen && prevV == prevV.next) {
continue;
}
// OK, we have a valid path
boolean goingup, goingup0;
if (isOpen) {
currV = v0.next;
while (v0 != currV && currV.pt.y == v0.pt.y) {
currV = currV.next;
}
goingup = currV.pt.y <= v0.pt.y;
if (goingup) {
v0.flags = VertexFlags.OpenStart;
AddLocMin(v0, polytype, true, minimaList);
} else {
v0.flags = VertexFlags.OpenStart | VertexFlags.LocalMax;
}
} else { // closed path
prevV = v0.prev;
while (!v0.equals(prevV) && prevV.pt.y == v0.pt.y) {
prevV = prevV.prev;
}
if (v0.equals(prevV)) {
continue; // only open paths can be completely flat
}
goingup = prevV.pt.y > v0.pt.y;
}
goingup0 = goingup;
prevV = v0;
currV = v0.next;
while (!v0.equals(currV)) {
if (currV.pt.y > prevV.pt.y && goingup) {
prevV.flags |= VertexFlags.LocalMax;
goingup = false;
} else if (currV.pt.y < prevV.pt.y && !goingup) {
goingup = true;
AddLocMin(prevV, polytype, isOpen, minimaList);
}
prevV = currV;
currV = currV.next;
}
if (isOpen) {
prevV.flags |= VertexFlags.OpenEnd;
if (goingup) {
prevV.flags |= VertexFlags.LocalMax;
} else {
AddLocMin(prevV, polytype, isOpen, minimaList);
}
} else if (goingup != goingup0) {
if (goingup0) {
AddLocMin(prevV, polytype, false, minimaList);
} else {
prevV.flags = prevV.flags | VertexFlags.LocalMax;
}
}
}
}
}
public class ReuseableDataContainer64 {
private final List<LocalMinima> minimaList;
private final List<Vertex> vertexList;
public ReuseableDataContainer64() {
minimaList = new ArrayList<>();
vertexList = new ArrayList<>();
}
public void Clear() {
minimaList.clear();
vertexList.clear();
}
public void AddPaths(Paths64 paths, PathType pt, boolean isOpen) {
ClipperEngine.AddPathsToVertexList(paths, pt, isOpen, minimaList, vertexList);
}
}
/**
* Vertex data structure for clipping solutions
*/
static class OutPt {
Point64 pt;
@Nullable
OutPt next;
OutPt prev;
OutRec outrec;
@Nullable
HorzSegment horz;
OutPt(Point64 pt, OutRec outrec) {
this.pt = pt;
this.outrec = outrec;
next = this;
prev = this;
horz = null;
}
}
enum JoinWith {
None, Left, Right
}
enum HorzPosition {
Bottom, Middle, Top
}
/**
* A structure representing 2 intersecting edges. Intersections must be sorted
* so they are processed from the largest y coordinates to the smallest while
* keeping edges adjacent.
*/
static final class IntersectNode {
final Point64 pt;
final Active edge1;
final Active edge2;
IntersectNode(Point64 pt, Active edge1, Active edge2) {
this.pt = pt;
this.edge1 = edge1;
this.edge2 = edge2;
}
}
/**
* Vertex: a pre-clipping data structure. It is used to separate polygons into
* ascending and descending 'bounds' (or sides) that start at local minima and
* ascend to a local maxima, before descending again.
*/
static class Vertex {
Point64 pt = new Point64();
@Nullable
Vertex next;
@Nullable
Vertex prev;
int flags;
Vertex(Point64 pt, int flags, Vertex prev) {
this.pt = pt;
this.flags = flags;
next = null;
this.prev = prev;
}
}
static class VertexFlags {
static final int None = 0;
static final int OpenStart = 1;
static final int OpenEnd = 2;
static final int LocalMax = 4;
static final int LocalMin = 8;
}
protected ClipperBase() {
minimaList = new ArrayList<>();
intersectList = new ArrayList<>();
vertexList = new ArrayList<>();
outrecList = new ArrayList<>();
scanlineSet = new TreeSet<>();
horzSegList = new ArrayList<>();
horzJoinList = new ArrayList<>();
setPreserveCollinear(true);
}
public final boolean getPreserveCollinear() {
return preserveCollinear;
}
/**
* When adjacent edges are collinear in closed path solutions, the common vertex
* can safely be removed to simplify the solution without altering path shape.
* However, because some users prefer to retain these common vertices, this
* feature is optional. Nevertheless, when adjacent edges in solutions are
* collinear and also create a 'spike' by overlapping, the vertex creating the
* spike will be removed irrespective of the PreserveCollinear setting. This
* property is enabled by default.
*/
public final void setPreserveCollinear(boolean value) {
preserveCollinear = value;
}
public final boolean getReverseSolution() {
return reverseSolution;
}
public final void setReverseSolution(boolean value) {
reverseSolution = value;
}
private static boolean IsOdd(int val) {
return ((val & 1) != 0);
}
private static boolean IsHotEdge(Active ae) {
return ae.outrec != null;
}
private static boolean IsOpen(Active ae) {
return ae.localMin.isOpen;
}
private static boolean IsOpenEnd(Active ae) {
return ae.localMin.isOpen && IsOpenEnd(ae.vertexTop);
}
private static boolean IsOpenEnd(Vertex v) {
return (v.flags & (VertexFlags.OpenStart | VertexFlags.OpenEnd)) != VertexFlags.None;
}
private static Active GetPrevHotEdge(Active ae) {
Active prev = ae.prevInAEL;
while (prev != null && (IsOpen(prev) || !IsHotEdge(prev))) {
prev = prev.prevInAEL;
}
return prev;
}
private static boolean IsFront(Active ae) {
return (ae == ae.outrec.frontEdge);
}
private static double GetDx(Point64 pt1, Point64 pt2) {
/*-
* Dx: 0(90deg) *
* | *
* +inf (180deg) <--- o --. -inf (0deg) *
*******************************************************************************/
double dy = pt2.y - pt1.y;
if (dy != 0) {
return (pt2.x - pt1.x) / dy;
}
if (pt2.x > pt1.x) {
return Double.NEGATIVE_INFINITY;
}
return Double.POSITIVE_INFINITY;
}
private static long TopX(Active ae, long currentY) {
if ((currentY == ae.top.y) || (ae.top.x == ae.bot.x)) {
return ae.top.x;
}
if (currentY == ae.bot.y) {
return ae.bot.x;
}
return ae.bot.x + (long) Math.rint(ae.dx * (currentY - ae.bot.y));
}
private static boolean IsHorizontal(Active ae) {
return (ae.top.y == ae.bot.y);
}
private static boolean IsHeadingRightHorz(Active ae) {
return Double.NEGATIVE_INFINITY == ae.dx;
}
private static boolean IsHeadingLeftHorz(Active ae) {
return Double.POSITIVE_INFINITY == ae.dx;
}
private static PathType GetPolyType(Active ae) {
return ae.localMin.polytype;
}
private static boolean IsSamePolyType(Active ae1, Active ae2) {
return ae1.localMin.polytype == ae2.localMin.polytype;
}
private static void SetDx(Active ae) {
ae.dx = GetDx(ae.bot, ae.top);
}
private static Vertex NextVertex(Active ae) {
if (ae.windDx > 0) {
return ae.vertexTop.next;
}
return ae.vertexTop.prev;
}
private static Vertex PrevPrevVertex(Active ae) {
if (ae.windDx > 0) {
return ae.vertexTop.prev.prev;
}
return ae.vertexTop.next.next;
}
private static boolean IsMaxima(Vertex vertex) {
return ((vertex.flags & VertexFlags.LocalMax) != VertexFlags.None);
}
private static boolean IsMaxima(Active ae) {
return IsMaxima(ae.vertexTop);
}
private static Active GetMaximaPair(Active ae) {
Active ae2 = null;
ae2 = ae.nextInAEL;
while (ae2 != null) {
if (ae2.vertexTop == ae.vertexTop) {
return ae2; // Found!
}
ae2 = ae2.nextInAEL;
}
return null;
}
private static @Nullable Vertex GetCurrYMaximaVertex_Open(Active ae) {
@Nullable
Vertex result = ae.vertexTop;
if (ae.windDx > 0) {
while (result.next.pt.y == result.pt.y && ((result.flags & (VertexFlags.OpenEnd | VertexFlags.LocalMax)) == VertexFlags.None)) {
result = result.next;
}
} else {
while (result.prev.pt.y == result.pt.y && ((result.flags & (VertexFlags.OpenEnd | VertexFlags.LocalMax)) == VertexFlags.None)) {
result = result.prev;
}
}
if (!IsMaxima(result)) {
result = null; // not a maxima
}
return result;
}
private static Vertex GetCurrYMaximaVertex(Active ae) {
Vertex result = ae.vertexTop;
if (ae.windDx > 0) {
while (result.next.pt.y == result.pt.y) {
result = result.next;
}
} else {
while (result.prev.pt.y == result.pt.y) {
result = result.prev;
}
}
if (!IsMaxima(result)) {
result = null; // not a maxima
}
return result;
}
private static void SetSides(OutRec outrec, Active startEdge, Active endEdge) {
outrec.frontEdge = startEdge;
outrec.backEdge = endEdge;
}
private static void SwapOutrecs(Active ae1, Active ae2) {
OutRec or1 = ae1.outrec; // at least one edge has
OutRec or2 = ae2.outrec; // an assigned outrec
if (or1 == or2) {
Active ae = or1.frontEdge;
or1.frontEdge = or1.backEdge;
or1.backEdge = ae;
return;
}
if (or1 != null) {
if (ae1 == or1.frontEdge) {
or1.frontEdge = ae2;
} else {
or1.backEdge = ae2;
}
}
if (or2 != null) {
if (ae2 == or2.frontEdge) {
or2.frontEdge = ae1;
} else {
or2.backEdge = ae1;
}
}
ae1.outrec = or2;
ae2.outrec = or1;
}
private static void SetOwner(OutRec outrec, OutRec newOwner) {
// precondition1: newOwner is never null
while (newOwner.owner != null && newOwner.owner.pts == null) {
newOwner.owner = newOwner.owner.owner;
}
// make sure that outrec isn't an owner of newOwner
@Nullable
OutRec tmp = newOwner;
while (tmp != null && tmp != outrec) {
tmp = tmp.owner;
}
if (tmp != null) {
newOwner.owner = outrec.owner;
}
outrec.owner = newOwner;
}
private static double Area(OutPt op) {
// https://en.wikipedia.org/wiki/Shoelaceformula
double area = 0.0;
OutPt op2 = op;
do {
area += (op2.prev.pt.y + op2.pt.y) * (op2.prev.pt.x - op2.pt.x);
op2 = op2.next;
} while (op2 != op);
return area * 0.5;
}
private static double AreaTriangle(Point64 pt1, Point64 pt2, Point64 pt3) {
return (pt3.y + pt1.y) * (pt3.x - pt1.x) + (pt1.y + pt2.y) * (pt1.x - pt2.x) + (pt2.y + pt3.y) * (pt2.x - pt3.x);
}
private static OutRec GetRealOutRec(OutRec outRec) {
while ((outRec != null) && (outRec.pts == null)) {
outRec = outRec.owner;
}
return outRec;
}
private static boolean IsValidOwner(OutRec outRec, OutRec testOwner) {
while ((testOwner != null) && (testOwner != outRec)) {
testOwner = testOwner.owner;
}
return testOwner == null;
}
private static void UncoupleOutRec(Active ae) {
OutRec outrec = ae.outrec;
if (outrec == null) {
return;
}
outrec.frontEdge.outrec = null;
outrec.backEdge.outrec = null;
outrec.frontEdge = null;
outrec.backEdge = null;
}
private static boolean OutrecIsAscending(Active hotEdge) {
return (hotEdge == hotEdge.outrec.frontEdge);
}
private static void SwapFrontBackSides(OutRec outrec) {
// while this proc. is needed for open paths
// it's almost never needed for closed paths
Active ae2 = outrec.frontEdge;
outrec.frontEdge = outrec.backEdge;
outrec.backEdge = ae2;
outrec.pts = outrec.pts.next;
}
private static boolean EdgesAdjacentInAEL(IntersectNode inode) {
return (inode.edge1.nextInAEL == inode.edge2) || (inode.edge1.prevInAEL == inode.edge2);
}
protected final void ClearSolutionOnly() {
while (actives != null) {
DeleteFromAEL(actives);
}
scanlineSet.clear();
DisposeIntersectNodes();
outrecList.clear();
horzSegList.clear();
horzJoinList.clear();
}
public final void Clear() {
ClearSolutionOnly();
minimaList.clear();
vertexList.clear();
currentLocMin = 0;
isSortedMinimaList = false;
hasOpenPaths = false;
}
protected final void Reset() {
if (!isSortedMinimaList) {
minimaList.sort((locMin1, locMin2) -> Long.compare(locMin2.vertex.pt.y, locMin1.vertex.pt.y));
isSortedMinimaList = true;
}
for (int i = minimaList.size() - 1; i >= 0; i--) {
scanlineSet.add(minimaList.get(i).vertex.pt.y);
}
currentBotY = 0;
currentLocMin = 0;
actives = null;
sel = null;
succeeded = true;
}
/**
* @deprecated Has been inlined in Java version since function is much simpler
*/
@SuppressWarnings("unused")
@Deprecated
private void InsertScanline(long y) {
scanlineSet.add(y);
}
/**
* @deprecated Has been inlined in Java version since function is much simpler
*/
@SuppressWarnings("unused")
@Deprecated
private long PopScanline() {
if (scanlineSet.isEmpty()) {
return Long.MAX_VALUE;
}
return scanlineSet.pollLast();
}
private boolean HasLocMinAtY(long y) {
return (currentLocMin < minimaList.size() && minimaList.get(currentLocMin).vertex.pt.y == y);
}
private LocalMinima PopLocalMinima() {
return minimaList.get(currentLocMin++);
}
public final void AddSubject(Path64 path) {
AddPath(path, PathType.Subject);
}
/**
* Adds one or more closed subject paths (polygons) to the Clipper object.
*/
public final void AddSubject(Paths64 paths) {
paths.forEach(path -> AddPath(path, PathType.Subject));
}
/**
* Adds one or more open subject paths (polylines) to the Clipper object.
*/
public final void AddOpenSubject(Path64 path) {
AddPath(path, PathType.Subject, true);
}
public final void AddOpenSubject(Paths64 paths) {
paths.forEach(path -> AddPath(path, PathType.Subject, true));
}
/**
* Adds one or more clip polygons to the Clipper object.
*/
public final void AddClip(Path64 path) {
AddPath(path, PathType.Clip);
}
public final void AddClip(Paths64 paths) {
paths.forEach(path -> AddPath(path, PathType.Clip));
}
public final void AddPath(Path64 path, PathType polytype) {
AddPath(path, polytype, false);
}
public final void AddPath(Path64 path, PathType polytype, boolean isOpen) {
Paths64 tmp = new Paths64();
tmp.add(path);
AddPaths(tmp, polytype, isOpen);
}
public final void AddPaths(Paths64 paths, PathType polytype) {
AddPaths(paths, polytype, false);
}
public final void AddPaths(Paths64 paths, PathType polytype, boolean isOpen) {
if (isOpen) {
hasOpenPaths = true;
}
isSortedMinimaList = false;
ClipperEngine.AddPathsToVertexList(paths, polytype, isOpen, minimaList, vertexList);
}
protected void AddReuseableData(ReuseableDataContainer64 reuseableData) {
if (reuseableData.minimaList.isEmpty()) {
return;
}
// nb: reuseableData will continue to own the vertices, so it's important
// that the reuseableData object isn't destroyed before the Clipper object
// that's using the data.
isSortedMinimaList = false;
for (LocalMinima lm : reuseableData.minimaList) {
minimaList.add(new LocalMinima(lm.vertex, lm.polytype, lm.isOpen));
if (lm.isOpen) {
hasOpenPaths = true;
}
}
}
private boolean IsContributingClosed(Active ae) {
switch (fillrule) {
case Positive :
if (ae.windCount != 1) {
return false;
}
break;
case Negative :
if (ae.windCount != -1) {
return false;
}
break;
case NonZero :
if (Math.abs(ae.windCount) != 1) {
return false;
}
break;
case EvenOdd :
break;
}
switch (cliptype) {
case Intersection :
switch (fillrule) {
case Positive :
return ae.windCount2 > 0;
case Negative :
return ae.windCount2 < 0;
default :
return ae.windCount2 != 0;
}
case Union :
switch (fillrule) {
case Positive :
return ae.windCount2 <= 0;
case Negative :
return ae.windCount2 >= 0;
default :
return ae.windCount2 == 0;
}
case Difference :
boolean result;
switch (fillrule) {
case Positive :
result = ae.windCount2 <= 0;
break;
case Negative :
result = ae.windCount2 >= 0;
break;
default :
result = ae.windCount2 == 0;
break;
}
return (GetPolyType(ae) == PathType.Subject) == result;
case Xor :
return true; // XOr is always contributing unless open
default :
return false;
}
}
private boolean IsContributingOpen(Active ae) {
boolean isInClip, isInSubj;
switch (fillrule) {
case Positive :
isInSubj = ae.windCount > 0;
isInClip = ae.windCount2 > 0;
break;
case Negative :
isInSubj = ae.windCount < 0;
isInClip = ae.windCount2 < 0;
break;
default :
isInSubj = ae.windCount != 0;
isInClip = ae.windCount2 != 0;
break;
}
switch (cliptype) {
case Intersection :
return isInClip;
case Union :
return !isInSubj && !isInClip;
default :
return !isInClip;
}
}
private void SetWindCountForClosedPathEdge(Active ae) {
/*
* Wind counts refer to polygon regions not edges, so here an edge's WindCnt
* indicates the higher of the wind counts for the two regions touching the
* edge. (nb: Adjacent regions can only ever have their wind counts differ by
* one. Also, open paths have no meaningful wind directions or counts.)
*/
Active ae2 = ae.prevInAEL;
// find the nearest closed path edge of the same PolyType in AEL (heading left)
PathType pt = GetPolyType(ae);
while (ae2 != null && (GetPolyType(ae2) != pt || IsOpen(ae2))) {
ae2 = ae2.prevInAEL;
}
if (ae2 == null) {
ae.windCount = ae.windDx;
ae2 = actives;
} else if (fillrule == FillRule.EvenOdd) {
ae.windCount = ae.windDx;
ae.windCount2 = ae2.windCount2;
ae2 = ae2.nextInAEL;
} else {
// NonZero, positive, or negative filling here ...
// when e2's WindCnt is in the SAME direction as its WindDx,
// then polygon will fill on the right of 'e2' (and 'e' will be inside)
// nb: neither e2.WindCnt nor e2.WindDx should ever be 0.
if (ae2.windCount * ae2.windDx < 0) {
// opposite directions so 'ae' is outside 'ae2' ...
if (Math.abs(ae2.windCount) > 1) {
// outside prev poly but still inside another.
if (ae2.windDx * ae.windDx < 0) {
// reversing direction so use the same WC
ae.windCount = ae2.windCount;
} else {
// otherwise keep 'reducing' the WC by 1 (i.e. towards 0) ...
ae.windCount = ae2.windCount + ae.windDx;
}
} else {
// now outside all polys of same polytype so set own WC ...
ae.windCount = (IsOpen(ae) ? 1 : ae.windDx);
}
} else // 'ae' must be inside 'ae2'
if (ae2.windDx * ae.windDx < 0) {
// reversing direction so use the same WC
ae.windCount = ae2.windCount;
} else {
// otherwise keep 'increasing' the WC by 1 (i.e. away from 0) ...
ae.windCount = ae2.windCount + ae.windDx;
}
ae.windCount2 = ae2.windCount2;
ae2 = ae2.nextInAEL; // i.e. get ready to calc WindCnt2
}
// update windCount2 ...
if (fillrule == FillRule.EvenOdd) {
while (!ae2.equals(ae)) {
if (GetPolyType(ae2) != pt && !IsOpen(ae2)) {
ae.windCount2 = (ae.windCount2 == 0 ? 1 : 0);
}
ae2 = ae2.nextInAEL;
}
} else {
while (!ae2.equals(ae)) {
if (GetPolyType(ae2) != pt && !IsOpen(ae2)) {
ae.windCount2 += ae2.windDx;
}
ae2 = ae2.nextInAEL;
}
}
}
private void SetWindCountForOpenPathEdge(Active ae) {
Active ae2 = actives;
if (fillrule == FillRule.EvenOdd) {
int cnt1 = 0, cnt2 = 0;
while (!ae2.equals(ae)) {
if (GetPolyType(ae2) == PathType.Clip) {
cnt2++;
} else if (!IsOpen(ae2)) {
cnt1++;
}
ae2 = ae2.nextInAEL;
}
ae.windCount = (IsOdd(cnt1) ? 1 : 0);
ae.windCount2 = (IsOdd(cnt2) ? 1 : 0);
} else {
while (!ae2.equals(ae)) {
if (GetPolyType(ae2) == PathType.Clip) {
ae.windCount2 += ae2.windDx;
} else if (!IsOpen(ae2)) {
ae.windCount += ae2.windDx;
}
ae2 = ae2.nextInAEL;
}
}
}
private static boolean IsValidAelOrder(Active resident, Active newcomer) {
if (newcomer.curX != resident.curX) {
return newcomer.curX > resident.curX;
}
// get the turning direction a1.top, a2.bot, a2.top
double d = InternalClipper.CrossProduct(resident.top, newcomer.bot, newcomer.top);
if (d != 0) {
return (d < 0);
}
// edges must be collinear to get here
// for starting open paths, place them according to
// the direction they're about to turn
if (!IsMaxima(resident) && (resident.top.y > newcomer.top.y)) {
return InternalClipper.CrossProduct(newcomer.bot, resident.top, NextVertex(resident).pt) <= 0;
}
if (!IsMaxima(newcomer) && (newcomer.top.y > resident.top.y)) {
return InternalClipper.CrossProduct(newcomer.bot, newcomer.top, NextVertex(newcomer).pt) >= 0;
}
long y = newcomer.bot.y;
boolean newcomerIsLeft = newcomer.isLeftBound;
if (resident.bot.y != y || resident.localMin.vertex.pt.y != y) {
return newcomer.isLeftBound;
}
// resident must also have just been inserted
if (resident.isLeftBound != newcomerIsLeft) {
return newcomerIsLeft;
}