-
Notifications
You must be signed in to change notification settings - Fork 14
/
clipper.go
4914 lines (4380 loc) · 122 KB
/
clipper.go
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 : 6.1.5 *
* Date : 28 March 2014 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2014 *
* *
* 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. *
* *
*******************************************************************************/
//use_int32: When enabled 32bit ints are used instead of 64bit ints. This
//improve performance but coordinate values are limited to the range +/- 46340
package clipper
import (
"fmt"
"math"
"math/big"
"sort"
"strings"
)
type Path []*IntPoint
type Paths []Path
func NewPath() Path {
return Path(make([]*IntPoint, 0))
}
func NewPaths() Paths {
return Paths(make([]Path, 0))
}
func (p Path) String() string {
v := make([]string, len(p))
for i, pp := range p {
v[i] = pp.String()
}
return fmt.Sprintf("{%v}", strings.Join(v, ", "))
}
func (p Paths) String() string {
s := "{"
for i, pp := range p {
s += pp.String()
if i != len(p)-1 {
s += "\n"
}
}
s += "}"
return s
}
type DoublePoint struct {
X float64
Y float64
}
func NewDoublePoint(x, y float64) *DoublePoint {
dp := new(DoublePoint)
dp.X = x
dp.Y = y
return dp
}
func CopyDoublePoint(dp *DoublePoint) *DoublePoint {
dp2 := new(DoublePoint)
dp2.X, dp2.Y = dp.X, dp.Y
return dp2
}
func (ip *IntPoint) ToDoublePoint() *DoublePoint {
dp := new(DoublePoint)
dp.X = float64(ip.X)
dp.Y = float64(ip.Y)
return dp
}
//------------------------------------------------------------------------------
// PolyTree & PolyNode classes
//------------------------------------------------------------------------------
type PolyTree struct {
PolyNode
m_AllPolys []*PolyNode
}
func NewPolyTree() *PolyTree {
pt := new(PolyTree)
pt.m_AllPolys = make([]*PolyNode, 0)
return pt
}
func (tree *PolyTree) toPolyNode() *PolyNode {
node := new(PolyNode)
node.m_Parent = tree.m_Parent
node.m_polygon = tree.m_polygon
node.m_Index = tree.m_Index
node.m_jointype = tree.m_jointype
node.m_endtype = tree.m_endtype
node.m_Childs = tree.m_Childs
node.IsOpen = tree.IsOpen
return node
}
func (pt *PolyTree) Clear() {
pt.m_AllPolys = make([]*PolyNode, 0)
pt.m_Childs = make([]*PolyNode, 0)
}
func (pt *PolyTree) GetFirst() *PolyNode {
if len(pt.m_Childs) > 0 {
return pt.m_Childs[0]
} else {
return nil
}
}
func (pt *PolyTree) Total() int {
return len(pt.m_AllPolys)
}
type PolyNode struct {
m_Parent *PolyNode
m_polygon Path
m_Index int
m_jointype JoinType
m_endtype EndType
m_Childs []*PolyNode
IsOpen bool
}
func NewPolyNode() *PolyNode {
pn := new(PolyNode)
pn.m_polygon = NewPath()
pn.m_Childs = make([]*PolyNode, 0)
return pn
}
func (pn *PolyNode) IsHoleNode() bool {
result := true
node := pn.m_Parent
for node != nil {
result = !result
node = node.m_Parent
}
return result
}
func (pn *PolyNode) ChildCount() int {
return len(pn.m_Childs)
}
func (pn *PolyNode) Contour() Path {
return pn.m_polygon
}
func (pn *PolyNode) AddChild(Child *PolyNode) {
cnt := pn.ChildCount()
pn.m_Childs = append(pn.m_Childs, Child)
Child.m_Parent = pn
Child.m_Index = cnt
}
func (pn *PolyNode) GetNext() *PolyNode {
if len(pn.m_Childs) > 0 {
return pn.m_Childs[0]
} else {
return pn.GetNextSiblingUp()
}
}
func (pn *PolyNode) GetNextSiblingUp() *PolyNode {
if pn.m_Parent == nil {
return nil
} else if pn.m_Index == len(pn.m_Parent.m_Childs)-1 {
return pn.m_Parent.GetNextSiblingUp()
} else {
return pn.m_Parent.m_Childs[pn.m_Index+1]
}
}
func (pn *PolyNode) Childs() []*PolyNode {
return pn.m_Childs
}
func (pn *PolyNode) Parent() *PolyNode {
return pn.m_Parent
}
func (pn *PolyNode) IsHole() bool {
return pn.IsHoleNode()
}
//------------------------------------------------------------------------------
func Int128Mul(lhs, rhs CInt) *big.Int {
a := big.NewInt(int64(lhs))
b := big.NewInt(int64(rhs))
c := new(big.Int)
return c.Mul(a, b)
}
//------------------------------------------------------------------------------
// The == operator, which
// will also compare Z values if they exist, is usually used instead of this one.
// This may be a problem when using Z values.
func (a *IntPoint) Equals(b *IntPoint) bool {
return a.X == b.X && a.Y == b.Y
}
func (a *IntPoint) NotEqual(b *IntPoint) bool {
return a.X != b.X || a.Y != b.Y
}
type IntRect struct {
left, top, right, bottom CInt
}
func NewIntRect(l, t, r, b CInt) *IntRect {
this := new(IntRect)
this.left = l
this.top = t
this.right = r
this.bottom = b
return this
}
func (ir *IntRect) Copy() IntRect {
return IntRect{
left: ir.left,
top: ir.top,
right: ir.right,
bottom: ir.bottom,
}
}
type ClipType int
const (
CtIntersection ClipType = iota
CtUnion
CtDifference
CtXor
)
type PolyType int
const (
PtSubject PolyType = iota
PtClip
)
//By far the most widely used winding rules for polygon filling are
//EvenOdd & NonZero (GDI, GDI+, XLib, OpenGL, Cairo, AGG, Quartz, SVG, Gr32)
//Others rules include Positive, Negative and ABS_GTR_EQ_TWO (only in OpenGL)
//see http://glprogramming.com/red/chapter11.html
type PolyFillType int
const (
PftEvenOdd PolyFillType = iota
PftNonZero
PftPositive
PftNegative
)
type JoinType int
const (
JtSquare JoinType = iota
JtRound
JtMiter
)
type EndType int
const (
EtClosedPolygon EndType = iota
EtClosedLine
EtOpenButt
EtOpenSquare
EtOpenRound
)
type EdgeSide int
const (
EsLeft EdgeSide = iota
EsRight
)
type Direction int
const (
DRightToLeft Direction = iota
DLeftToRight
)
type TEdge struct {
Bot, Curr, Top, Delta IntPoint
Dx float64
PolyTyp PolyType
Side EdgeSide
WindDelta int //1 or -1 depending on winding direction
WindCnt int
WindCnt2 int //winding count of the opposite polytype
OutIdx int
Next, Prev, NextInLML, NextInAEL *TEdge
PrevInAEL, NextInSEL, PrevInSEL *TEdge
}
func (e *TEdge) String() string {
return fmt.Sprintf("Bot: %v, Curr: %v, Top: %v, Delta: %v, Dx: %v",
e.Bot, e.Curr, e.Top, e.Delta, e.Dx)
}
//func (e *TEdge) Copy() *TEdge {
// o := new(TEdge)
// o.Bot = e.Bot.Copy()
// o.Curr = e.Curr.Copy()
// o.Top = e.Top.Copy()
// o.Delta = e.Delta.Copy()
// o.Dx = e.Dx
// o.PolyTyp = e.PolyTyp
// o.Side = e.Side
// o.WindDelta, o.WindCnt, o.WindCnt2, o.OutIdx =
// e.WindDelta, e.WindCnt, e.WindCnt2, e.OutIdx
// return o
//}
func (e *TEdge) printEdges() string {
E := e
s := ""
for {
s += fmt.Sprintf("%v\n", E)
E = E.Next
if E == nil || E == e {
break
}
}
return s
}
type IntersectNode struct {
Edge1, Edge2 *TEdge
Pt *IntPoint
}
func (in *IntersectNode) String() string {
return fmt.Sprintf("Edge1: %v, Edge2: %v, Pt: %v",
in.Edge1, in.Edge2, in.Pt)
}
type IntersectNodeList []*IntersectNode
func (i IntersectNodeList) Len() int { return len(i) }
func (i IntersectNodeList) Less(a, b int) bool { return i[a].Pt.Y > i[b].Pt.Y }
func (i IntersectNodeList) Swap(a, b int) { i[a], i[b] = i[b], i[a] }
// class MyIntersectNodeSort : IComparer<IntersectNode>
// {
// int Compare(IntersectNode node1, IntersectNode node2)
// {
// return (int)(node2.Pt.Y - node1.Pt.Y);
// }
// }
type LocalMinima struct {
Y CInt
LeftBound, RightBound *TEdge
Next *LocalMinima
}
func (lm *LocalMinima) String() string {
return fmt.Sprintf("Y: %v, LeftBound: %v, RightBound: %v",
lm.Y, lm.LeftBound, lm.RightBound)
}
func (lm *LocalMinima) printLML() string {
s := ""
LM := lm
for {
s += fmt.Sprint(LM)
if LM.Next == nil {
break
}
LM = LM.Next
s += "\n"
}
return s
}
type Scanbeam struct {
Y CInt
Next *Scanbeam
}
func (s *Scanbeam) String() string {
return fmt.Sprintf("Y: %v", s.Y)
}
func (s *Scanbeam) printScanbeams() string {
str := ""
s2 := s
for s2 != nil {
str += " " + fmt.Sprint(s2)
s2 = s2.Next
}
return str
}
type OutRec struct {
Idx int
IsHole, IsOpen bool
FirstLeft *OutRec //see comments in clipper.pas
Pts *OutPt
BottomPt *OutPt
PolyNode *PolyNode
}
type OutPt struct {
Idx int
Pt *IntPoint
Next, Prev *OutPt
}
func (o *OutPt) String() string {
return fmt.Sprintf("Idx: %v, Pt: %v",
o.Idx, o.Pt)
}
type Join struct {
OutPt1, OutPt2 *OutPt
OffPt *IntPoint
}
func (j *Join) String() string {
return fmt.Sprintf("OutPt1: %v, OutPt2: %v, OffPt: %v",
j.OutPt1, j.OutPt2, j.OffPt)
}
var horizontal = math.Inf(-1)
const (
Skip int = -2
Unassigned int = -1
tolerance float64 = 1.0e-20
)
type ClipperBase struct {
m_MinimaList *LocalMinima
m_CurrentLM *LocalMinima
m_edges [][]*TEdge
m_UseFullRange, m_HasOpenPaths bool
PreserveCollinear bool
}
func NewClipperBase() *ClipperBase {
c := new(ClipperBase)
c.m_edges = make([][]*TEdge, 0)
return c
}
func near_zero(val float64) bool {
return (val > -tolerance) && (val < tolerance)
}
func (c *ClipperBase) Swap(val1, val2 *CInt) {
*val1, *val2 = *val2, *val1
}
//------------------------------------------------------------------------------
func (c *ClipperBase) IsHorizontal(e *TEdge) bool {
return e.Delta.Y == 0
}
//------------------------------------------------------------------------------
func (c *ClipperBase) PointIsVertex(pt *IntPoint, pp *OutPt) bool {
pp2 := pp
for {
if *pp2.Pt == *pt {
return true
}
pp2 = pp2.Next
if pp2 == pp {
break
}
}
return false
}
//------------------------------------------------------------------------------
func (c *ClipperBase) PointOnLineSegment(pt,
linePt1, linePt2 *IntPoint, UseFullRange bool) bool {
if UseFullRange {
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)) &&
Int128Mul((pt.X-linePt1.X), (linePt2.Y-linePt1.Y)).Cmp(
Int128Mul((linePt2.X-linePt1.X), (pt.Y-linePt1.Y))) == 0)
} else {
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)))
}
}
//------------------------------------------------------------------------------
func (c *ClipperBase) PointOnPolygon(pt *IntPoint, pp *OutPt, UseFullRange bool) bool {
pp2 := pp
for {
if c.PointOnLineSegment(pt, pp2.Pt, pp2.Next.Pt, UseFullRange) {
return true
}
pp2 = pp2.Next
if pp2 == pp {
break
}
}
return false
}
//------------------------------------------------------------------------------
func (c *ClipperBase) SlopesEqual(e1, e2 *TEdge, UseFullRange bool) bool {
if UseFullRange {
return Int128Mul(e1.Delta.Y, e2.Delta.X).Cmp(
Int128Mul(e1.Delta.X, e2.Delta.Y)) == 0
} else {
return (e1.Delta.Y)*(e2.Delta.X) ==
(e1.Delta.X)*(e2.Delta.Y)
}
}
//------------------------------------------------------------------------------
func (c *ClipperBase) SlopesEqual3(pt1, pt2,
pt3 *IntPoint, UseFullRange bool) bool {
if UseFullRange {
return Int128Mul(pt1.Y-pt2.Y, pt2.X-pt3.X).Cmp(
Int128Mul(pt1.X-pt2.X, pt2.Y-pt3.Y)) == 0
} else {
return (pt1.Y-pt2.Y)*(pt2.X-pt3.X)-
(pt1.X-pt2.X)*(pt2.Y-pt3.Y) == 0
}
}
//------------------------------------------------------------------------------
func (c *ClipperBase) SlopesEqual4(pt1, pt2,
pt3, pt4 *IntPoint, UseFullRange bool) bool {
if UseFullRange {
return Int128Mul(pt1.Y-pt2.Y, pt3.X-pt4.X).Cmp(
Int128Mul(pt1.X-pt2.X, pt3.Y-pt4.Y)) == 0
} else {
return (pt1.Y-pt2.Y)*(pt3.X-pt4.X)-
(pt1.X-pt2.X)*(pt3.Y-pt4.Y) == 0
}
}
//------------------------------------------------------------------------------
func (c *ClipperBase) Clear() {
c.m_edges = make([][]*TEdge, 0)
c.m_UseFullRange = false
c.m_HasOpenPaths = false
}
//------------------------------------------------------------------------------
func (c *ClipperBase) DisposeLocalMinimaList() {
for c.m_MinimaList != nil {
tmpLm := c.m_MinimaList.Next
c.m_MinimaList = nil
c.m_MinimaList = tmpLm
}
c.m_CurrentLM = nil
}
//------------------------------------------------------------------------------
func (c *ClipperBase) RangeTest(Pt *IntPoint, useFullRange *bool) {
if *useFullRange {
if Pt.X > hiRange || Pt.Y > hiRange || -Pt.X > hiRange || -Pt.Y > hiRange {
panic(NewClipperException("Coordinate outside allowed range"))
}
} else if Pt.X > loRange || Pt.Y > loRange || -Pt.X > loRange || -Pt.Y > loRange {
*useFullRange = true
c.RangeTest(Pt, useFullRange)
}
}
//------------------------------------------------------------------------------
func (c *ClipperBase) InitEdge(e, eNext, ePrev *TEdge, pt *IntPoint) {
e.Next = eNext
e.Prev = ePrev
e.Curr = pt.Copy()
e.OutIdx = Unassigned
}
//------------------------------------------------------------------------------
func (c *ClipperBase) InitEdge2(e *TEdge, polyType PolyType) {
if e.Curr.Y >= e.Next.Curr.Y {
e.Bot = e.Curr
e.Top = e.Next.Curr
} else {
e.Top = e.Curr
e.Bot = e.Next.Curr
}
c.SetDx(e)
e.PolyTyp = polyType
}
//------------------------------------------------------------------------------
func (c *ClipperBase) FindNextLocMin(E *TEdge) *TEdge {
var E2 *TEdge
for {
for E.Bot != E.Prev.Bot || E.Curr == E.Top {
E = E.Next
}
if E.Dx != horizontal && E.Prev.Dx != horizontal {
break
}
for E.Prev.Dx == horizontal {
E = E.Prev
}
E2 = E
for E.Dx == horizontal {
E = E.Next
}
if E.Top.Y == E.Prev.Bot.Y {
continue //ie just an intermediate horz.
}
if E2.Prev.Bot.X < E.Bot.X {
E = E2
}
break
}
return E
}
//------------------------------------------------------------------------------
func (c *ClipperBase) ProcessBound(E *TEdge, IsClockwise bool) *TEdge {
EStart := E
Result := E
var Horz *TEdge
var StartX CInt
if E.Dx == horizontal {
//first we need to be careful here with open paths because this
//may not be a true local minima (ie may be following a skip edge).
//also, watch for adjacent horz edges to start heading left
//before finishing right ...
if IsClockwise {
if E.Prev.Bot.Y == E.Bot.Y {
StartX = E.Prev.Bot.X
} else {
StartX = E.Prev.Top.X
}
} else {
if E.Next.Bot.Y == E.Bot.Y {
StartX = E.Next.Bot.X
} else {
StartX = E.Next.Top.X
}
}
if E.Bot.X != StartX {
c.ReverseHorizontal(E)
}
}
if Result.OutIdx != Skip {
if IsClockwise {
for Result.Top.Y == Result.Next.Bot.Y && 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
for Horz.Prev.Dx == horizontal {
Horz = Horz.Prev
}
if Horz.Prev.Top.X == Result.Next.Top.X {
if !IsClockwise {
Result = Horz.Prev
}
} else if Horz.Prev.Top.X > Result.Next.Top.X {
Result = Horz.Prev
}
}
for E != Result {
E.NextInLML = E.Next
if E.Dx == horizontal && E != EStart && E.Bot.X != E.Prev.Top.X {
c.ReverseHorizontal(E)
}
E = E.Next
}
if E.Dx == horizontal && E != EStart && E.Bot.X != E.Prev.Top.X {
c.ReverseHorizontal(E)
}
Result = Result.Next //move to the edge just beyond current bound
} else {
for Result.Top.Y == Result.Prev.Bot.Y && Result.Prev.OutIdx != Skip {
Result = Result.Prev
}
if Result.Dx == horizontal && Result.Prev.OutIdx != Skip {
Horz = Result
for Horz.Next.Dx == horizontal {
Horz = Horz.Next
}
if Horz.Next.Top.X == Result.Prev.Top.X {
if !IsClockwise {
Result = Horz.Next
}
} else if Horz.Next.Top.X > Result.Prev.Top.X {
Result = Horz.Next
}
}
for E != Result {
E.NextInLML = E.Prev
if E.Dx == horizontal && E != EStart && E.Bot.X != E.Next.Top.X {
c.ReverseHorizontal(E)
}
E = E.Prev
}
if E.Dx == horizontal && E != EStart && E.Bot.X != E.Next.Top.X {
c.ReverseHorizontal(E)
}
Result = Result.Prev //move to the edge just beyond current bound
}
}
if Result.OutIdx == Skip {
//if edges still remain in the current bound beyond the skip edge then
//create another LocMin and call ProcessBound once more
E = Result
if IsClockwise {
for E.Top.Y == E.Next.Bot.Y {
E = E.Next
}
//don't include top horizontals when parsing a bound a second time,
//they will be contained in the opposite bound ...
for E != Result && E.Dx == horizontal {
E = E.Prev
}
} else {
for E.Top.Y == E.Prev.Bot.Y {
E = E.Prev
}
for E != Result && E.Dx == horizontal {
E = E.Next
}
}
if E == Result {
if IsClockwise {
Result = E.Next
} else {
Result = E.Prev
}
} else {
//there are more edges in the bound beyond result starting with E
if IsClockwise {
E = Result.Next
} else {
E = Result.Prev
}
locMin := new(LocalMinima)
locMin.Y = E.Bot.Y
locMin.RightBound = E
locMin.RightBound.WindDelta = 0
Result = c.ProcessBound(locMin.RightBound, IsClockwise)
c.InsertLocalMinima(locMin)
}
}
return Result
}
//------------------------------------------------------------------------------
func (c *ClipperBase) AddPath(pg Path, polyType PolyType, Closed bool) bool {
if !Closed && polyType == PtClip {
panic(NewClipperException("AddPath: Open paths must be subject."))
}
highI := len(pg) - 1
if Closed {
for highI > 0 && (pg[highI] == pg[0]) {
highI--
}
}
for highI > 0 && (pg[highI] == pg[highI-1]) {
highI--
}
if (Closed && highI < 2) || (!Closed && highI < 1) {
return false
}
//create a new edge array ...
edges := make([]*TEdge, highI+1)
for i := 0; i <= highI; i++ {
edges[i] = new(TEdge)
}
IsFlat := true
//1. Basic (first) edge initialization ...
edges[1].Curr = pg[1].Copy()
c.RangeTest(pg[0], &c.m_UseFullRange)
c.RangeTest(pg[highI], &c.m_UseFullRange)
c.InitEdge(edges[0], edges[1], edges[highI], pg[0])
c.InitEdge(edges[highI], edges[0], edges[highI-1], pg[highI])
for i := highI - 1; i >= 1; i-- {
c.RangeTest(pg[i], &c.m_UseFullRange)
c.InitEdge(edges[i], edges[i+1], edges[i-1], pg[i])
}
eStart := edges[0]
//2. Remove duplicate vertices, and (when closed) collinear edges ...
E := eStart
eLoopStop := eStart
for {
if E.Curr == E.Next.Curr {
if E == E.Next {
break
}
if E == eStart {
eStart = E.Next
}
E = c.RemoveEdge(E)
eLoopStop = E
continue
}
if E.Prev == E.Next {
break //only two vertices
} else if Closed &&
c.SlopesEqual3(&E.Prev.Curr, &E.Curr, &E.Next.Curr, c.m_UseFullRange) &&
(!c.PreserveCollinear ||
!c.Pt2IsBetweenPt1AndPt3(&E.Prev.Curr, &E.Curr, &E.Next.Curr)) {
//Collinear edges are allowed for open paths but in closed paths
//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 closed paths.
if E == eStart {
eStart = E.Next
}
E = c.RemoveEdge(E)
E = E.Prev
eLoopStop = E
continue
}
E = E.Next
if (E == eLoopStop) || (!Closed && E.Next == eStart) {
break
}
}
if (!Closed && (E == E.Next)) || (Closed && (E.Prev == E.Next)) {
return false
}
if !Closed {
c.m_HasOpenPaths = true
eStart.Prev.OutIdx = Skip
}
//3. Do second stage of edge initialization ...
E = eStart
for {
c.InitEdge2(E, polyType)
E = E.Next
if IsFlat && E.Curr.Y != eStart.Curr.Y {
IsFlat = false
}
if E == eStart {
break
}
}
//4. Finally, add edge bounds to LocalMinima list ...
//Totally flat paths must be handled differently when adding them
//to LocalMinima list to avoid endless loops etc ...
if IsFlat {
if Closed {
return false
}
E.Prev.OutIdx = Skip
if E.Prev.Bot.X < E.Prev.Top.X {
c.ReverseHorizontal(E.Prev)
}
locMin := new(LocalMinima)
locMin.Next = nil
locMin.Y = E.Bot.Y
locMin.LeftBound = nil
locMin.RightBound = E
locMin.RightBound.Side = EsRight
locMin.RightBound.WindDelta = 0
for E.Next.OutIdx != Skip {
E.NextInLML = E.Next
if E.Bot.X != E.Prev.Top.X {
c.ReverseHorizontal(E)
}
E = E.Next
}
c.InsertLocalMinima(locMin)
c.m_edges = append(c.m_edges, edges)
return true
}
c.m_edges = append(c.m_edges, edges)
var clockwise bool
var EMin *TEdge
for {
E = c.FindNextLocMin(E)
if E == EMin {
break
} else if EMin == nil {
EMin = E
}
//E and E.Prev now share a local minima (left aligned if horizontal).
//Compare their slopes to find which starts which bound ...
locMin := new(LocalMinima)
locMin.Next = nil
locMin.Y = E.Bot.Y
if E.Dx < E.Prev.Dx {
locMin.LeftBound = E.Prev
locMin.RightBound = E
clockwise = false //Q.nextInLML = Q.prev
} else {
locMin.LeftBound = E
locMin.RightBound = E.Prev
clockwise = true //Q.nextInLML = Q.next
}
locMin.LeftBound.Side = EsLeft
locMin.RightBound.Side = EsRight
if !Closed {
locMin.LeftBound.WindDelta = 0
} else if locMin.LeftBound.Next == locMin.RightBound {
locMin.LeftBound.WindDelta = -1
} else {
locMin.LeftBound.WindDelta = 1
}
locMin.RightBound.WindDelta = -locMin.LeftBound.WindDelta
E = c.ProcessBound(locMin.LeftBound, clockwise)
E2 := c.ProcessBound(locMin.RightBound, !clockwise)
if locMin.LeftBound.OutIdx == Skip {
locMin.LeftBound = nil
} else if locMin.RightBound.OutIdx == Skip {
locMin.RightBound = nil
}
c.InsertLocalMinima(locMin)
if !clockwise {