This repository was archived by the owner on Dec 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathDefaultClipper.java
More file actions
2519 lines (2283 loc) · 92.8 KB
/
DefaultClipper.java
File metadata and controls
2519 lines (2283 loc) · 92.8 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 de.lighti.clipper;
import de.lighti.clipper.Path.Join;
import de.lighti.clipper.Path.OutRec;
import de.lighti.clipper.Point.LongPoint;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class DefaultClipper extends ClipperBase {
private class IntersectNode {
Edge edge1;
Edge Edge2;
private LongPoint pt;
LongPoint getPt() {
return pt;
}
void setPt( LongPoint pt ) {
this.pt = pt;
}
}
private static void getHorzDirection(Edge HorzEdge, Direction[] Dir, long[] Left, long[] Right ) {
if (HorzEdge.getBot().getX() < HorzEdge.getTop().getX()) {
Left[0] = HorzEdge.getBot().getX();
Right[0] = HorzEdge.getTop().getX();
Dir[0] = Direction.LEFT_TO_RIGHT;
}
else {
Left[0] = HorzEdge.getTop().getX();
Right[0] = HorzEdge.getBot().getX();
Dir[0] = Direction.RIGHT_TO_LEFT;
}
}
private static boolean getOverlap( long a1, long a2, long b1, long b2, long[] Left, long[] Right ) {
if (a1 < a2) {
if (b1 < b2) {
Left[0] = Math.max( a1, b1 );
Right[0] = Math.min( a2, b2 );
}
else {
Left[0] = Math.max( a1, b2 );
Right[0] = Math.min( a2, b1 );
}
}
else {
if (b1 < b2) {
Left[0] = Math.max( a2, b1 );
Right[0] = Math.min( a1, b2 );
}
else {
Left[0] = Math.max( a2, b2 );
Right[0] = Math.min( a1, b1 );
}
}
return Left[0] < Right[0];
}
private static boolean isOutRec1RightOfOutRec2(OutRec outRec1, OutRec outRec2 ) {
do {
outRec1 = outRec1.firstLeft;
if (outRec1 == outRec2) {
return true;
}
}
while (outRec1 != null);
return false;
}
//See "The Point in Polygon Problem for Arbitrary Polygons" by Hormann & Agathos
//http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.88.5498&rep=rep1&type=pdf
private static int isPointInPolygon(LongPoint pt, Path.OutPt op ) {
//returns 0 if false, +1 if true, -1 if pt ON polygon boundary
int result = 0;
final Path.OutPt startOp = op;
final long ptx = pt.getX(), pty = pt.getY();
long poly0x = op.getPt().getX(), poly0y = op.getPt().getY();
do {
op = op.next;
final long poly1x = op.getPt().getX(), poly1y = op.getPt().getY();
if (poly1y == pty) {
if (poly1x == ptx || poly0y == pty && poly1x > ptx == poly0x < ptx) {
return -1;
}
}
if (poly0y < pty != poly1y < pty) {
if (poly0x >= ptx) {
if (poly1x > ptx) {
result = 1 - result;
}
else {
final double d = (double) (poly0x - ptx) * (poly1y - pty) - (double) (poly1x - ptx) * (poly0y - pty);
if (d == 0) {
return -1;
}
if (d > 0 == poly1y > poly0y) {
result = 1 - result;
}
}
}
else {
if (poly1x > ptx) {
final double d = (double) (poly0x - ptx) * (poly1y - pty) - (double) (poly1x - ptx) * (poly0y - pty);
if (d == 0) {
return -1;
}
if (d > 0 == poly1y > poly0y) {
result = 1 - result;
}
}
}
}
poly0x = poly1x;
poly0y = poly1y;
}
while (startOp != op);
return result;
}
//------------------------------------------------------------------------------
private static boolean joinHorz(Path.OutPt op1, Path.OutPt op1b, Path.OutPt op2, Path.OutPt op2b, LongPoint Pt, boolean DiscardLeft ) {
final Direction Dir1 = op1.getPt().getX() > op1b.getPt().getX() ? Direction.RIGHT_TO_LEFT : Direction.LEFT_TO_RIGHT;
final Direction Dir2 = op2.getPt().getX() > op2b.getPt().getX() ? Direction.RIGHT_TO_LEFT : Direction.LEFT_TO_RIGHT;
if (Dir1 == Dir2) {
return false;
}
//When DiscardLeft, we want Op1b to be on the Left of Op1, otherwise we
//want Op1b to be on the Right. (And likewise with Op2 and Op2b.)
//So, to facilitate this while inserting Op1b and Op2b ...
//when DiscardLeft, make sure we're AT or RIGHT of Pt before adding Op1b,
//otherwise make sure we're AT or LEFT of Pt. (Likewise with Op2b.)
if (Dir1 == Direction.LEFT_TO_RIGHT) {
while (op1.next.getPt().getX() <= Pt.getX() && op1.next.getPt().getX() >= op1.getPt().getX() && op1.next.getPt().getY() == Pt.getY()) {
op1 = op1.next;
}
if (DiscardLeft && op1.getPt().getX() != Pt.getX()) {
op1 = op1.next;
}
op1b = op1.duplicate( !DiscardLeft );
if (!op1b.getPt().equals( Pt )) {
op1 = op1b;
op1.setPt( new LongPoint( Pt ) );
op1b = op1.duplicate( !DiscardLeft );
}
}
else {
while (op1.next.getPt().getX() >= Pt.getX() && op1.next.getPt().getX() <= op1.getPt().getX() && op1.next.getPt().getY() == Pt.getY()) {
op1 = op1.next;
}
if (!DiscardLeft && op1.getPt().getX() != Pt.getX()) {
op1 = op1.next;
}
op1b = op1.duplicate( DiscardLeft );
if (!op1b.getPt().equals( Pt )) {
op1 = op1b;
op1.setPt( new LongPoint( Pt ) );
op1b = op1.duplicate( DiscardLeft );
}
}
if (Dir2 == Direction.LEFT_TO_RIGHT) {
while (op2.next.getPt().getX() <= Pt.getX() && op2.next.getPt().getX() >= op2.getPt().getX() && op2.next.getPt().getY() == Pt.getY()) {
op2 = op2.next;
}
if (DiscardLeft && op2.getPt().getX() != Pt.getX()) {
op2 = op2.next;
}
op2b = op2.duplicate( !DiscardLeft );
if (!op2b.getPt().equals( Pt )) {
op2 = op2b;
op2.setPt( new LongPoint( Pt ) );
op2b = op2.duplicate( !DiscardLeft );
}
}
else {
while (op2.next.getPt().getX() >= Pt.getX() && op2.next.getPt().getX() <= op2.getPt().getX() && op2.next.getPt().getY() == Pt.getY()) {
op2 = op2.next;
}
if (!DiscardLeft && op2.getPt().getX() != Pt.getX()) {
op2 = op2.next;
}
op2b = op2.duplicate( DiscardLeft );
if (!op2b.getPt().equals( Pt )) {
op2 = op2b;
op2.setPt( new LongPoint( Pt ) );
op2b = op2.duplicate( DiscardLeft );
}
}
if (Dir1 == Direction.LEFT_TO_RIGHT == DiscardLeft) {
op1.prev = op2;
op2.next = op1;
op1b.next = op2b;
op2b.prev = op1b;
}
else {
op1.next = op2;
op2.prev = op1;
op1b.prev = op2b;
op2b.next = op1b;
}
return true;
}
private static boolean joinPoints(Join j, OutRec outRec1, OutRec outRec2 ) {
Path.OutPt op1 = j.outPt1, op1b;
Path.OutPt op2 = j.outPt2, op2b;
//There are 3 kinds of joins for output polygons ...
//1. Horizontal joins where Join.OutPt1 & Join.OutPt2 are vertices anywhere
//along (horizontal) collinear edges (& Join.OffPt is on the same horizontal).
//2. Non-horizontal joins where Join.OutPt1 & Join.OutPt2 are at the same
//location at the Bottom of the overlapping segment (& Join.OffPt is above).
//3. StrictlySimple joins where edges touch but are not collinear and where
//Join.OutPt1, Join.OutPt2 & Join.OffPt all share the same point.
final boolean isHorizontal = j.outPt1.getPt().getY() == j.getOffPt().getY();
if (isHorizontal && j.getOffPt().equals( j.outPt1.getPt() ) && j.getOffPt().equals( j.outPt2.getPt() )) {
//Strictly Simple join ...
if (outRec1 != outRec2) {
return false;
}
op1b = j.outPt1.next;
while (op1b != op1 && op1b.getPt().equals( j.getOffPt() )) {
op1b = op1b.next;
}
final boolean reverse1 = op1b.getPt().getY() > j.getOffPt().getY();
op2b = j.outPt2.next;
while (op2b != op2 && op2b.getPt().equals( j.getOffPt() )) {
op2b = op2b.next;
}
final boolean reverse2 = op2b.getPt().getY() > j.getOffPt().getY();
if (reverse1 == reverse2) {
return false;
}
if (reverse1) {
op1b = op1.duplicate( false );
op2b = op2.duplicate( true );
op1.prev = op2;
op2.next = op1;
op1b.next = op2b;
op2b.prev = op1b;
j.outPt1 = op1;
j.outPt2 = op1b;
return true;
}
else {
op1b = op1.duplicate( true );
op2b = op2.duplicate( false );
op1.next = op2;
op2.prev = op1;
op1b.prev = op2b;
op2b.next = op1b;
j.outPt1 = op1;
j.outPt2 = op1b;
return true;
}
}
else if (isHorizontal) {
//treat horizontal joins differently to non-horizontal joins since with
//them we're not yet sure where the overlapping is. OutPt1.Pt & OutPt2.Pt
//may be anywhere along the horizontal edge.
op1b = op1;
while (op1.prev.getPt().getY() == op1.getPt().getY() && op1.prev != op1b && op1.prev != op2) {
op1 = op1.prev;
}
while (op1b.next.getPt().getY() == op1b.getPt().getY() && op1b.next != op1 && op1b.next != op2) {
op1b = op1b.next;
}
if (op1b.next == op1 || op1b.next == op2) {
return false;
} //a flat 'polygon'
op2b = op2;
while (op2.prev.getPt().getY() == op2.getPt().getY() && op2.prev != op2b && op2.prev != op1b) {
op2 = op2.prev;
}
while (op2b.next.getPt().getY() == op2b.getPt().getY() && op2b.next != op2 && op2b.next != op1) {
op2b = op2b.next;
}
if (op2b.next == op2 || op2b.next == op1) {
return false;
} //a flat 'polygon'
final long[] LeftV = new long[1], RightV = new long[1];
//Op1 -. Op1b & Op2 -. Op2b are the extremites of the horizontal edges
if (!getOverlap( op1.getPt().getX(), op1b.getPt().getX(), op2.getPt().getX(), op2b.getPt().getX(), LeftV, RightV )) {
return false;
}
final long Left = LeftV[0];
final long Right = RightV[0];
//DiscardLeftSide: when overlapping edges are joined, a spike will created
//which needs to be cleaned up. However, we don't want Op1 or Op2 caught up
//on the discard Side as either may still be needed for other joins ...
LongPoint Pt;
boolean DiscardLeftSide;
if (op1.getPt().getX() >= Left && op1.getPt().getX() <= Right) {
Pt = new LongPoint( op1.getPt() );
DiscardLeftSide = op1.getPt().getX() > op1b.getPt().getX();
}
else if (op2.getPt().getX() >= Left && op2.getPt().getX() <= Right) {
Pt = new LongPoint( op2.getPt() );
DiscardLeftSide = op2.getPt().getX() > op2b.getPt().getX();
}
else if (op1b.getPt().getX() >= Left && op1b.getPt().getX() <= Right) {
Pt = new LongPoint( op1b.getPt() );
DiscardLeftSide = op1b.getPt().getX() > op1.getPt().getX();
}
else {
Pt = new LongPoint( op2b.getPt() );
DiscardLeftSide = op2b.getPt().getX() > op2.getPt().getX();
}
j.outPt1 = op1;
j.outPt2 = op2;
return joinHorz( op1, op1b, op2, op2b, Pt, DiscardLeftSide );
}
else {
//nb: For non-horizontal joins ...
// 1. Jr.OutPt1.getPt().getY() == Jr.OutPt2.getPt().getY()
// 2. Jr.OutPt1.Pt > Jr.OffPt.getY()
//make sure the polygons are correctly oriented ...
op1b = op1.next;
while (op1b.getPt().equals( op1.getPt() ) && op1b != op1) {
op1b = op1b.next;
}
final boolean Reverse1 = op1b.getPt().getY() > op1.getPt().getY() || !Point.slopesEqual( op1.getPt(), op1b.getPt(), j.getOffPt() );
if (Reverse1) {
op1b = op1.prev;
while (op1b.getPt().equals( op1.getPt() ) && op1b != op1) {
op1b = op1b.prev;
}
if (op1b.getPt().getY() > op1.getPt().getY() || !Point.slopesEqual( op1.getPt(), op1b.getPt(), j.getOffPt() )) {
return false;
}
}
op2b = op2.next;
while (op2b.getPt().equals( op2.getPt() ) && op2b != op2) {
op2b = op2b.next;
}
final boolean Reverse2 = op2b.getPt().getY() > op2.getPt().getY() || !Point.slopesEqual( op2.getPt(), op2b.getPt(), j.getOffPt() );
if (Reverse2) {
op2b = op2.prev;
while (op2b.getPt().equals( op2.getPt() ) && op2b != op2) {
op2b = op2b.prev;
}
if (op2b.getPt().getY() > op2.getPt().getY() || !Point.slopesEqual( op2.getPt(), op2b.getPt(), j.getOffPt() )) {
return false;
}
}
if (op1b == op1 || op2b == op2 || op1b == op2b || outRec1 == outRec2 && Reverse1 == Reverse2) {
return false;
}
if (Reverse1) {
op1b = op1.duplicate( false );
op2b = op2.duplicate( true );
op1.prev = op2;
op2.next = op1;
op1b.next = op2b;
op2b.prev = op1b;
j.outPt1 = op1;
j.outPt2 = op1b;
return true;
}
else {
op1b = op1.duplicate( true );
op2b = op2.duplicate( false );
op1.next = op2;
op2.prev = op1;
op1b.prev = op2b;
op2b.next = op1b;
j.outPt1 = op1;
j.outPt2 = op1b;
return true;
}
}
}
private static Paths minkowski(Path pattern, Path path, boolean IsSum, boolean IsClosed ) {
final int delta = IsClosed ? 1 : 0;
final int polyCnt = pattern.size();
final int pathCnt = path.size();
final Paths result = new Paths( pathCnt );
if (IsSum) {
for (int i = 0; i < pathCnt; i++) {
final Path p = new Path( polyCnt );
for (final LongPoint ip : pattern) {
p.add( new LongPoint( path.get( i ).getX() + ip.getX(), path.get( i ).getY() + ip.getY(), 0 ) );
}
result.add( p );
}
}
else {
for (int i = 0; i < pathCnt; i++) {
final Path p = new Path( polyCnt );
for (final LongPoint ip : pattern) {
p.add( new LongPoint( path.get( i ).getX() - ip.getX(), path.get( i ).getY() - ip.getY(), 0 ) );
}
result.add( p );
}
}
final Paths quads = new Paths( (pathCnt + delta) * (polyCnt + 1) );
for (int i = 0; i < pathCnt - 1 + delta; i++) {
for (int j = 0; j < polyCnt; j++) {
final Path quad = new Path( 4 );
quad.add( result.get( i % pathCnt ).get( j % polyCnt ) );
quad.add( result.get( (i + 1) % pathCnt ).get( j % polyCnt ) );
quad.add( result.get( (i + 1) % pathCnt ).get( (j + 1) % polyCnt ) );
quad.add( result.get( i % pathCnt ).get( (j + 1) % polyCnt ) );
if (!quad.orientation()) {
Collections.reverse( quad );
}
quads.add( quad );
}
}
return quads;
}
public static Paths minkowskiDiff(Path poly1, Path poly2 ) {
final Paths paths = minkowski( poly1, poly2, false, true );
final DefaultClipper c = new DefaultClipper();
c.addPaths( paths, PolyType.SUBJECT, true );
c.execute( ClipType.UNION, paths, PolyFillType.NON_ZERO, PolyFillType.NON_ZERO );
return paths;
}
public static Paths minkowskiSum(Path pattern, Path path, boolean pathIsClosed ) {
final Paths paths = minkowski( pattern, path, true, pathIsClosed );
final DefaultClipper c = new DefaultClipper();
c.addPaths( paths, PolyType.SUBJECT, true );
c.execute( ClipType.UNION, paths, PolyFillType.NON_ZERO, PolyFillType.NON_ZERO );
return paths;
}
public static Paths minkowskiSum(Path pattern, Paths paths, boolean pathIsClosed ) {
final Paths solution = new Paths();
final DefaultClipper c = new DefaultClipper();
for (int i = 0; i < paths.size(); ++i) {
final Paths tmp = minkowski( pattern, paths.get( i ), true, pathIsClosed );
c.addPaths( tmp, PolyType.SUBJECT, true );
if (pathIsClosed) {
final Path path = paths.get( i ).TranslatePath( pattern.get( 0 ) );
c.addPath( path, PolyType.CLIP, true );
}
}
c.execute( ClipType.UNION, solution, PolyFillType.NON_ZERO, PolyFillType.NON_ZERO );
return solution;
}
private static boolean poly2ContainsPoly1(Path.OutPt outPt1, Path.OutPt outPt2 ) {
Path.OutPt op = outPt1;
do {
//nb: PointInPolygon returns 0 if false, +1 if true, -1 if pt on polygon
final int res = isPointInPolygon( op.getPt(), outPt2 );
if (res >= 0) {
return res > 0;
}
op = op.next;
}
while (op != outPt1);
return true;
}
//------------------------------------------------------------------------------
// SimplifyPolygon functions ...
// Convert self-intersecting polygons into simple polygons
//------------------------------------------------------------------------------
public static Paths simplifyPolygon(Path poly ) {
return simplifyPolygon( poly, PolyFillType.EVEN_ODD );
}
public static Paths simplifyPolygon(Path poly, PolyFillType fillType ) {
final Paths result = new Paths();
final DefaultClipper c = new DefaultClipper( STRICTLY_SIMPLE );
c.addPath( poly, PolyType.SUBJECT, true );
c.execute( ClipType.UNION, result, fillType, fillType );
return result;
}
public static Paths simplifyPolygons(Paths polys ) {
return simplifyPolygons( polys, PolyFillType.EVEN_ODD );
}
public static Paths simplifyPolygons(Paths polys, PolyFillType fillType ) {
final Paths result = new Paths();
final DefaultClipper c = new DefaultClipper( STRICTLY_SIMPLE );
c.addPaths( polys, PolyType.SUBJECT, true );
c.execute( ClipType.UNION, result, fillType, fillType );
return result;
}
private ClipType clipType;
private Maxima maxima;
private Edge sortedEdges;
private final List<IntersectNode> intersectList;
private final Comparator<IntersectNode> intersectNodeComparer;
private PolyFillType clipFillType;
//------------------------------------------------------------------------------
private PolyFillType subjFillType;
//------------------------------------------------------------------------------
private final List<Join> joins;
//------------------------------------------------------------------------------
private final List<Join> ghostJoins;
private boolean usingPolyTree;
private ZFillCallback zFillFunction;
//------------------------------------------------------------------------------
private final boolean reverseSolution;
//------------------------------------------------------------------------------
private final boolean strictlySimple;
private final static Logger LOGGER = Logger.getLogger( DefaultClipper.class.getName() );
public DefaultClipper() {
this( 0 );
}
public DefaultClipper( int InitOptions ) //constructor
{
super( (PRESERVE_COLINEAR & InitOptions) != 0 );
scanbeam = null;
maxima = null;
activeEdges = null;
sortedEdges = null;
intersectList = new ArrayList<>();
intersectNodeComparer = ( node1, node2 ) -> {
final long i = node2.getPt().getY() - node1.getPt().getY();
if (i > 0) {
return 1;
}
else if (i < 0) {
return -1;
}
else {
return 0;
}
};
usingPolyTree = false;
joins = new ArrayList<>();
ghostJoins = new ArrayList<>();
reverseSolution = (REVERSE_SOLUTION & InitOptions) != 0;
strictlySimple = (STRICTLY_SIMPLE & InitOptions) != 0;
zFillFunction = null;
}
private void addEdgeToSEL( Edge edge ) {
LOGGER.entering( DefaultClipper.class.getName(), "addEdgeToSEL" );
//SEL pointers in PEdge are use to build transient lists of horizontal edges.
//However, since we don't need to worry about processing order, all additions
//are made to the front of the list ...
if (sortedEdges == null) {
sortedEdges = edge;
edge.prevInSEL = null;
edge.nextInSEL = null;
}
else {
edge.nextInSEL = sortedEdges;
edge.prevInSEL = null;
sortedEdges.prevInSEL = edge;
sortedEdges = edge;
}
}
private void addGhostJoin(Path.OutPt Op, LongPoint OffPt ) {
final Join j = new Join();
j.outPt1 = Op;
j.setOffPt( new LongPoint( OffPt ) );
ghostJoins.add( j );
}
//------------------------------------------------------------------------------
private void addJoin(Path.OutPt Op1, Path.OutPt Op2, LongPoint OffPt ) {
LOGGER.entering( DefaultClipper.class.getName(), "addJoin" );
final Join j = new Join();
j.outPt1 = Op1;
j.outPt2 = Op2;
j.setOffPt( new LongPoint( OffPt ) );
joins.add( j );
}
//------------------------------------------------------------------------------
private void addLocalMaxPoly(Edge e1, Edge e2, LongPoint pt ) {
addOutPt( e1, pt );
if (e2.windDelta == 0) {
addOutPt( e2, pt );
}
if (e1.outIdx == e2.outIdx) {
e1.outIdx = Edge.UNASSIGNED;
e2.outIdx = Edge.UNASSIGNED;
}
else if (e1.outIdx < e2.outIdx) {
appendPolygon( e1, e2 );
}
else {
appendPolygon( e2, e1 );
}
}
//------------------------------------------------------------------------------
private Path.OutPt addLocalMinPoly(Edge e1, Edge e2, LongPoint pt ) {
LOGGER.entering( DefaultClipper.class.getName(), "addLocalMinPoly" );
Path.OutPt result;
Edge e, prevE;
if (e2.isHorizontal() || e1.deltaX > e2.deltaX) {
result = addOutPt( e1, pt );
e2.outIdx = e1.outIdx;
e1.side = Edge.Side.LEFT;
e2.side = Edge.Side.RIGHT;
e = e1;
if (e.prevInAEL == e2) {
prevE = e2.prevInAEL;
}
else {
prevE = e.prevInAEL;
}
}
else {
result = addOutPt( e2, pt );
e1.outIdx = e2.outIdx;
e1.side = Edge.Side.RIGHT;
e2.side = Edge.Side.LEFT;
e = e2;
if (e.prevInAEL == e1) {
prevE = e1.prevInAEL;
}
else {
prevE = e.prevInAEL;
}
}
if (prevE != null && prevE.outIdx >= 0 && prevE.getTop().getY() < pt.getY() && e.getTop().getY() < pt.getY()) {
long xPrev = Edge.topX( prevE, pt.getY() );
long xE = Edge.topX( e, pt.getY() );
if (xPrev == xE && e.windDelta != 0 && prevE.windDelta != 0 &&
Point.slopesEqual( new LongPoint( xPrev, pt.getY() ), prevE.getTop(), new LongPoint( xE, pt.getY() ), e.getTop() )) {
final Path.OutPt outPt = addOutPt( prevE, pt );
addJoin( result, outPt, e.getTop() );
}
}
return result;
}
private Path.OutPt addOutPt(Edge e, LongPoint pt ) {
LOGGER.entering( DefaultClipper.class.getName(), "addOutPt" );
if (e.outIdx < 0) {
final OutRec outRec = createOutRec();
outRec.isOpen = e.windDelta == 0;
final Path.OutPt newOp = new Path.OutPt();
outRec.setPoints( newOp );
newOp.idx = outRec.Idx;
newOp.setPt( new LongPoint( pt ) );
newOp.next = newOp;
newOp.prev = newOp;
if (!outRec.isOpen) {
setHoleState( e, outRec );
}
e.outIdx = outRec.Idx; //nb: do this after SetZ !
return newOp;
}
else {
final OutRec outRec = polyOuts.get( e.outIdx );
//OutRec.Pts is the 'Left-most' point & OutRec.Pts.Prev is the 'Right-most'
final Path.OutPt op = outRec.getPoints();
final boolean ToFront = e.side == Edge.Side.LEFT;
if (LOGGER.isLoggable( Level.FINEST )) {
LOGGER.finest( "op=" + Path.OutPt.getPointCount( op ) );
LOGGER.finest( ToFront + " " + pt + " " + op.getPt() );
}
if (ToFront && pt.equals( op.getPt() )) {
return op;
}
else if (!ToFront && pt.equals( op.prev.getPt() )) {
return op.prev;
}
final Path.OutPt newOp = new Path.OutPt();
newOp.idx = outRec.Idx;
newOp.setPt( new LongPoint( pt ) );
newOp.next = op;
newOp.prev = op.prev;
newOp.prev.next = newOp;
op.prev = newOp;
if (ToFront) {
outRec.setPoints( newOp );
}
return newOp;
}
}
private Path.OutPt getLastOutPt(Edge e) {
OutRec outRec = polyOuts.get( e.outIdx );
if (e.side == Edge.Side.LEFT)
return outRec.getPoints();
else
return outRec.getPoints().prev;
}
private void appendPolygon(Edge e1, Edge e2 ) {
LOGGER.entering( DefaultClipper.class.getName(), "appendPolygon" );
final OutRec outRec1 = polyOuts.get( e1.outIdx );
final OutRec outRec2 = polyOuts.get( e2.outIdx );
LOGGER.finest( "" + e1.outIdx );
LOGGER.finest( "" + e2.outIdx );
OutRec holeStateRec;
if (isOutRec1RightOfOutRec2( outRec1, outRec2 )) {
holeStateRec = outRec2;
}
else if (isOutRec1RightOfOutRec2( outRec2, outRec1 )) {
holeStateRec = outRec1;
}
else {
holeStateRec = Path.OutPt.getLowerMostRec( outRec1, outRec2 );
}
//get the start and ends of both output polygons and
//join E2 poly onto E1 poly and delete pointers to E2 ...
final Path.OutPt p1_lft = outRec1.getPoints();
final Path.OutPt p1_rt = p1_lft.prev;
final Path.OutPt p2_lft = outRec2.getPoints();
final Path.OutPt p2_rt = p2_lft.prev;
LOGGER.finest( "p1_lft.getPointCount() = " + Path.OutPt.getPointCount( p1_lft ) );
LOGGER.finest( "p1_rt.getPointCount() = " + Path.OutPt.getPointCount( p1_rt ) );
LOGGER.finest( "p2_lft.getPointCount() = " + Path.OutPt.getPointCount( p2_lft ) );
LOGGER.finest( "p2_rt.getPointCount() = " + Path.OutPt.getPointCount( p2_rt ) );
//join e2 poly onto e1 poly and delete pointers to e2 ...
if (e1.side == Edge.Side.LEFT) {
if (e2.side == Edge.Side.LEFT) {
//z y x a b c
p2_lft.reversePolyPtLinks();
p2_lft.next = p1_lft;
p1_lft.prev = p2_lft;
p1_rt.next = p2_rt;
p2_rt.prev = p1_rt;
outRec1.setPoints( p2_rt );
}
else {
//x y z a b c
p2_rt.next = p1_lft;
p1_lft.prev = p2_rt;
p2_lft.prev = p1_rt;
p1_rt.next = p2_lft;
outRec1.setPoints( p2_lft );
}
}
else {
if (e2.side == Edge.Side.RIGHT) {
//a b c z y x
p2_lft.reversePolyPtLinks();
p1_rt.next = p2_rt;
p2_rt.prev = p1_rt;
p2_lft.next = p1_lft;
p1_lft.prev = p2_lft;
}
else {
//a b c x y z
p1_rt.next = p2_lft;
p2_lft.prev = p1_rt;
p1_lft.prev = p2_rt;
p2_rt.next = p1_lft;
}
}
outRec1.bottomPt = null;
if (holeStateRec.equals( outRec2 )) {
if (outRec2.firstLeft != outRec1) {
outRec1.firstLeft = outRec2.firstLeft;
}
outRec1.isHole = outRec2.isHole;
}
outRec2.setPoints( null );
outRec2.bottomPt = null;
outRec2.firstLeft = outRec1;
final int OKIdx = e1.outIdx;
final int ObsoleteIdx = e2.outIdx;
e1.outIdx = Edge.UNASSIGNED; //nb: safe because we only get here via AddLocalMaxPoly
e2.outIdx = Edge.UNASSIGNED;
Edge e = activeEdges;
while (e != null) {
if (e.outIdx == ObsoleteIdx) {
e.outIdx = OKIdx;
e.side = e1.side;
break;
}
e = e.nextInAEL;
}
outRec2.Idx = outRec1.Idx;
}
//------------------------------------------------------------------------------
private void buildIntersectList( long topY ) {
if (activeEdges == null) {
return;
}
//prepare for sorting ...
Edge e = activeEdges;
sortedEdges = e;
while (e != null) {
e.prevInSEL = e.prevInAEL;
e.nextInSEL = e.nextInAEL;
e.getCurrent().setX( Edge.topX( e, topY ) );
e = e.nextInAEL;
}
//bubblesort ...
boolean isModified = true;
while (isModified && sortedEdges != null) {
isModified = false;
e = sortedEdges;
while (e.nextInSEL != null) {
final Edge eNext = e.nextInSEL;
final LongPoint[] pt = new LongPoint[1];
if (e.getCurrent().getX() > eNext.getCurrent().getX()) {
intersectPoint( e, eNext, pt );
if (pt[0].getY() < topY) {
pt[0] = new LongPoint( Edge.topX( e, topY ), topY );
}
final IntersectNode newNode = new IntersectNode();
newNode.edge1 = e;
newNode.Edge2 = eNext;
newNode.setPt( new LongPoint( pt[0] ) ); // TODO is new instance necessary?
intersectList.add( newNode );
swapPositionsInSEL( e, eNext );
isModified = true;
}
else {
e = eNext;
}
}
if (e.prevInSEL != null) {
e.prevInSEL.nextInSEL = null;
}
else {
break;
}
}
sortedEdges = null;
}
//------------------------------------------------------------------------------
private void buildResult( Paths polyg ) {
polyg.clear();
for (int i = 0; i < polyOuts.size(); i++) {
final OutRec outRec = polyOuts.get( i );
if (outRec.getPoints() == null) {
continue;
}
Path.OutPt p = outRec.getPoints().prev;
final int cnt = Path.OutPt.getPointCount( p );
LOGGER.finest( "cnt = " + cnt );
if (cnt < 2) {
continue;
}
final Path pg = new Path( cnt );
for (int j = 0; j < cnt; j++) {
pg.add( new LongPoint( p.getPt() ) );
p = p.prev;
}
polyg.add( pg );
}
}
private void buildResult2( PolyTree polytree ) {
polytree.Clear();
//add each output polygon/contour to polytree ...
for (int i = 0; i < polyOuts.size(); i++) {
final OutRec outRec = polyOuts.get( i );
final int cnt = Path.OutPt.getPointCount( outRec.getPoints() );
if (outRec.isOpen && cnt < 2 || !outRec.isOpen && cnt < 3) {
continue;
}
outRec.fixHoleLinkage();
final PolyNode pn = new PolyNode();
polytree.getAllPolys().add( pn );
outRec.polyNode = pn;
Path.OutPt op = outRec.getPoints().prev;
for (int j = 0; j < cnt; j++) {
pn.getPolygon().add( op.getPt() );
op = op.prev;
}
}
//fixup PolyNode links etc ...
for (int i = 0; i < polyOuts.size(); i++) {
final OutRec outRec = polyOuts.get( i );
if (outRec.polyNode == null) {
continue;
}
else if (outRec.isOpen) {
outRec.polyNode.setOpen( true );
polytree.addChild( outRec.polyNode );
}
else if (outRec.firstLeft != null && outRec.firstLeft.polyNode != null) {
outRec.firstLeft.polyNode.addChild( outRec.polyNode );
}
else {
polytree.addChild( outRec.polyNode );
}
}
}
private void copyAELToSEL() {
Edge e = activeEdges;
sortedEdges = e;
while (e != null) {
e.prevInSEL = e.prevInAEL;
e.nextInSEL = e.nextInAEL;
e = e.nextInAEL;
}
}
private boolean deleteFromSEL( Edge[] e ) {
LOGGER.entering( DefaultClipper.class.getName(), "deleteFromSEL" );
//Pop edge from front of SEL (ie SEL is a FILO list)
e[0] = sortedEdges;
if (e[0] == null) {
return false;
}
final Edge oldE = e[0];
sortedEdges = e[0].nextInSEL;
if (sortedEdges != null) {
sortedEdges.prevInSEL = null;
}
oldE.nextInSEL = null;
oldE.prevInSEL = null;
return true;
}
private boolean doHorzSegmentsOverlap( long seg1a, long seg1b, long seg2a, long seg2b ) {
if (seg1a > seg1b) {
final long tmp = seg1a;
seg1a = seg1b;
seg1b = tmp;
}
if (seg2a > seg2b) {
final long tmp = seg2a;
seg2a = seg2b;
seg2b = tmp;
}
return seg1a < seg2b && seg2a < seg1b;
}
private void doMaxima( Edge e ) {
final Edge eMaxPair = e.getMaximaPairEx();
if (eMaxPair == null) {
if (e.outIdx >= 0) {