forked from dkxce/KMZRebuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PolyCreatorClipper.cs
4886 lines (4470 loc) · 166 KB
/
PolyCreatorClipper.cs
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
//use_int32: When enabled 32bit ints are used instead of 64bit ints. This
//improve performance but coordinate values are limited to the range +/- 46340
//#define use_int32
//use_xyz: adds a Z member to IntPoint. Adds a minor cost to performance.
//#define use_xyz
//use_lines: Enables open path clipping. Adds a very minor cost to performance.
#define use_lines
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace ClipperLib
{
#if use_int32
using cInt = Int32;
#else
using cInt = Int64;
#endif
using Path = List<IntPoint>;
using Paths = List<List<IntPoint>>;
public struct DoublePoint
{
public double X;
public double Y;
public DoublePoint(double x, double y)
{
this.X = x; this.Y = y;
}
public DoublePoint(DoublePoint dp)
{
this.X = dp.X; this.Y = dp.Y;
}
public DoublePoint(IntPoint ip)
{
this.X = ip.X; this.Y = ip.Y;
}
};
//------------------------------------------------------------------------------
// PolyTree & PolyNode classes
//------------------------------------------------------------------------------
public class PolyTree : PolyNode
{
internal List<PolyNode> m_AllPolys = new List<PolyNode>();
//The GC probably handles this cleanup more efficiently ...
//~PolyTree(){Clear();}
public void Clear()
{
for (int i = 0; i < m_AllPolys.Count; i++)
m_AllPolys[i] = null;
m_AllPolys.Clear();
m_Childs.Clear();
}
public PolyNode GetFirst()
{
if (m_Childs.Count > 0)
return m_Childs[0];
else
return null;
}
public int Total
{
get
{
int result = m_AllPolys.Count;
//with negative offsets, ignore the hidden outer polygon ...
if (result > 0 && m_Childs[0] != m_AllPolys[0]) result--;
return result;
}
}
}
public class PolyNode
{
internal PolyNode m_Parent;
internal Path m_polygon = new Path();
internal int m_Index;
internal JoinType m_jointype;
internal EndType m_endtype;
internal List<PolyNode> m_Childs = new List<PolyNode>();
private bool IsHoleNode()
{
bool result = true;
PolyNode node = m_Parent;
while (node != null)
{
result = !result;
node = node.m_Parent;
}
return result;
}
public int ChildCount
{
get { return m_Childs.Count; }
}
public Path Contour
{
get { return m_polygon; }
}
internal void AddChild(PolyNode Child)
{
int cnt = m_Childs.Count;
m_Childs.Add(Child);
Child.m_Parent = this;
Child.m_Index = cnt;
}
public PolyNode GetNext()
{
if (m_Childs.Count > 0)
return m_Childs[0];
else
return GetNextSiblingUp();
}
internal PolyNode GetNextSiblingUp()
{
if (m_Parent == null)
return null;
else if (m_Index == m_Parent.m_Childs.Count - 1)
return m_Parent.GetNextSiblingUp();
else
return m_Parent.m_Childs[m_Index + 1];
}
public List<PolyNode> Childs
{
get { return m_Childs; }
}
public PolyNode Parent
{
get { return m_Parent; }
}
public bool IsHole
{
get { return IsHoleNode(); }
}
public bool IsOpen;
}
//------------------------------------------------------------------------------
// Int128 struct (enables safe math on signed 64bit integers)
// eg Int128 val1((Int64)9223372036854775807); //ie 2^63 -1
// Int128 val2((Int64)9223372036854775807);
// Int128 val3 = val1 * val2;
// val3.ToString => "85070591730234615847396907784232501249" (8.5e+37)
//------------------------------------------------------------------------------
internal struct Int128
{
private Int64 hi;
private UInt64 lo;
public Int128(Int64 _lo)
{
lo = (UInt64)_lo;
if (_lo < 0) hi = -1;
else hi = 0;
}
public Int128(Int64 _hi, UInt64 _lo)
{
lo = _lo;
hi = _hi;
}
public Int128(Int128 val)
{
hi = val.hi;
lo = val.lo;
}
public bool IsNegative()
{
return hi < 0;
}
public static bool operator ==(Int128 val1, Int128 val2)
{
if ((object)val1 == (object)val2) return true;
else if ((object)val1 == null || (object)val2 == null) return false;
return (val1.hi == val2.hi && val1.lo == val2.lo);
}
public static bool operator !=(Int128 val1, Int128 val2)
{
return !(val1 == val2);
}
public override bool Equals(System.Object obj)
{
if (obj == null || !(obj is Int128))
return false;
Int128 i128 = (Int128)obj;
return (i128.hi == hi && i128.lo == lo);
}
public override int GetHashCode()
{
return hi.GetHashCode() ^ lo.GetHashCode();
}
public static bool operator >(Int128 val1, Int128 val2)
{
if (val1.hi != val2.hi)
return val1.hi > val2.hi;
else
return val1.lo > val2.lo;
}
public static bool operator <(Int128 val1, Int128 val2)
{
if (val1.hi != val2.hi)
return val1.hi < val2.hi;
else
return val1.lo < val2.lo;
}
public static Int128 operator +(Int128 lhs, Int128 rhs)
{
lhs.hi += rhs.hi;
lhs.lo += rhs.lo;
if (lhs.lo < rhs.lo) lhs.hi++;
return lhs;
}
public static Int128 operator -(Int128 lhs, Int128 rhs)
{
return lhs + -rhs;
}
public static Int128 operator -(Int128 val)
{
if (val.lo == 0)
return new Int128(-val.hi, 0);
else
return new Int128(~val.hi, ~val.lo + 1);
}
public static explicit operator double(Int128 val)
{
const double shift64 = 18446744073709551616.0; //2^64
if (val.hi < 0)
{
if (val.lo == 0)
return (double)val.hi * shift64;
else
return -(double)(~val.lo + ~val.hi * shift64);
}
else
return (double)(val.lo + val.hi * shift64);
}
//nb: Constructing two new Int128 objects every time we want to multiply longs
//is slow. So, although calling the Int128Mul method doesn't look as clean, the
//code runs significantly faster than if we'd used the * operator.
public static Int128 Int128Mul(Int64 lhs, Int64 rhs)
{
bool negate = (lhs < 0) != (rhs < 0);
if (lhs < 0) lhs = -lhs;
if (rhs < 0) rhs = -rhs;
UInt64 int1Hi = (UInt64)lhs >> 32;
UInt64 int1Lo = (UInt64)lhs & 0xFFFFFFFF;
UInt64 int2Hi = (UInt64)rhs >> 32;
UInt64 int2Lo = (UInt64)rhs & 0xFFFFFFFF;
//nb: see comments in clipper.pas
UInt64 a = int1Hi * int2Hi;
UInt64 b = int1Lo * int2Lo;
UInt64 c = int1Hi * int2Lo + int1Lo * int2Hi;
UInt64 lo;
Int64 hi;
hi = (Int64)(a + (c >> 32));
unchecked { lo = (c << 32) + b; }
if (lo < b) hi++;
Int128 result = new Int128(hi, lo);
return negate ? -result : result;
}
};
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
public struct IntPoint
{
public cInt X;
public cInt Y;
#if use_xyz
public cInt Z;
public IntPoint(cInt x, cInt y, cInt z = 0)
{
this.X = x; this.Y = y; this.Z = z;
}
public IntPoint(double x, double y, double z = 0)
{
this.X = (cInt)x; this.Y = (cInt)y; this.Z = (cInt)z;
}
public IntPoint(DoublePoint dp)
{
this.X = (cInt)dp.X; this.Y = (cInt)dp.Y; this.Z = 0;
}
public IntPoint(IntPoint pt)
{
this.X = pt.X; this.Y = pt.Y; this.Z = pt.Z;
}
#else
public IntPoint(cInt X, cInt Y)
{
this.X = X; this.Y = Y;
}
public IntPoint(double x, double y)
{
this.X = (cInt)x; this.Y = (cInt)y;
}
public IntPoint(IntPoint pt)
{
this.X = pt.X; this.Y = pt.Y;
}
#endif
public static bool operator ==(IntPoint a, IntPoint b)
{
return a.X == b.X && a.Y == b.Y;
}
public static bool operator !=(IntPoint a, IntPoint b)
{
return a.X != b.X || a.Y != b.Y;
}
public override bool Equals(object obj)
{
if (obj == null) return false;
if (obj is IntPoint)
{
IntPoint a = (IntPoint)obj;
return (X == a.X) && (Y == a.Y);
}
else return false;
}
public override int GetHashCode()
{
//simply prevents a compiler warning
return base.GetHashCode();
}
}// end struct IntPoint
public struct IntRect
{
public cInt left;
public cInt top;
public cInt right;
public cInt bottom;
public IntRect(cInt l, cInt t, cInt r, cInt b)
{
this.left = l; this.top = t;
this.right = r; this.bottom = b;
}
public IntRect(IntRect ir)
{
this.left = ir.left; this.top = ir.top;
this.right = ir.right; this.bottom = ir.bottom;
}
}
public enum ClipType { ctIntersection, ctUnion, ctDifference, ctXor };
public enum PolyType { ptSubject, 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
public enum PolyFillType { pftEvenOdd, pftNonZero, pftPositive, pftNegative };
public enum JoinType { jtSquare, jtRound, jtMiter };
public enum EndType { etClosedPolygon, etClosedLine, etOpenButt, etOpenSquare, etOpenRound };
internal enum EdgeSide {esLeft, esRight};
internal enum Direction {dRightToLeft, dLeftToRight};
internal class TEdge {
internal IntPoint Bot;
internal IntPoint Curr; //current (updated for every new scanbeam)
internal IntPoint Top;
internal IntPoint Delta;
internal double Dx;
internal PolyType PolyTyp;
internal EdgeSide Side; //side only refers to current side of solution poly
internal int WindDelta; //1 or -1 depending on winding direction
internal int WindCnt;
internal int WindCnt2; //winding count of the opposite polytype
internal int OutIdx;
internal TEdge Next;
internal TEdge Prev;
internal TEdge NextInLML;
internal TEdge NextInAEL;
internal TEdge PrevInAEL;
internal TEdge NextInSEL;
internal TEdge PrevInSEL;
};
public class IntersectNode
{
internal TEdge Edge1;
internal TEdge Edge2;
internal IntPoint Pt;
};
public class MyIntersectNodeSort : IComparer<IntersectNode>
{
public int Compare(IntersectNode node1, IntersectNode node2)
{
cInt i = node2.Pt.Y - node1.Pt.Y;
if (i > 0) return 1;
else if (i < 0) return -1;
else return 0;
}
}
internal class LocalMinima
{
internal cInt Y;
internal TEdge LeftBound;
internal TEdge RightBound;
internal LocalMinima Next;
};
internal class Scanbeam
{
internal cInt Y;
internal Scanbeam Next;
};
internal class Maxima
{
internal cInt X;
internal Maxima Next;
internal Maxima Prev;
};
//OutRec: contains a path in the clipping solution. Edges in the AEL will
//carry a pointer to an OutRec when they are part of the clipping solution.
internal class OutRec
{
internal int Idx;
internal bool IsHole;
internal bool IsOpen;
internal OutRec FirstLeft; //see comments in clipper.pas
internal OutPt Pts;
internal OutPt BottomPt;
internal PolyNode PolyNode;
};
internal class OutPt
{
internal int Idx;
internal IntPoint Pt;
internal OutPt Next;
internal OutPt Prev;
};
internal class Join
{
internal OutPt OutPt1;
internal OutPt OutPt2;
internal IntPoint OffPt;
};
public class ClipperBase
{
internal const double horizontal = -3.4E+38;
internal const int Skip = -2;
internal const int Unassigned = -1;
internal const double tolerance = 1.0E-20;
internal static bool near_zero(double val){return (val > -tolerance) && (val < tolerance);}
#if use_int32
public const cInt loRange = 0x7FFF;
public const cInt hiRange = 0x7FFF;
#else
public const cInt loRange = 0x3FFFFFFF;
public const cInt hiRange = 0x3FFFFFFFFFFFFFFFL;
#endif
internal LocalMinima m_MinimaList;
internal LocalMinima m_CurrentLM;
internal List<List<TEdge>> m_edges = new List<List<TEdge>>();
internal Scanbeam m_Scanbeam;
internal List<OutRec> m_PolyOuts;
internal TEdge m_ActiveEdges;
internal bool m_UseFullRange;
internal bool m_HasOpenPaths;
//------------------------------------------------------------------------------
public bool PreserveCollinear;
//------------------------------------------------------------------------------
public void Swap(ref cInt val1, ref cInt val2)
{
cInt tmp = val1;
val1 = val2;
val2 = tmp;
}
//------------------------------------------------------------------------------
internal static bool IsHorizontal(TEdge e)
{
return e.Delta.Y == 0;
}
//------------------------------------------------------------------------------
internal bool PointIsVertex(IntPoint pt, OutPt pp)
{
OutPt pp2 = pp;
do
{
if (pp2.Pt == pt) return true;
pp2 = pp2.Next;
}
while (pp2 != pp);
return false;
}
//------------------------------------------------------------------------------
internal bool PointOnLineSegment(IntPoint pt,
IntPoint linePt1, IntPoint linePt2, bool UseFullRange)
{
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)) &&
((Int128.Int128Mul((pt.X - linePt1.X), (linePt2.Y - linePt1.Y)) ==
Int128.Int128Mul((linePt2.X - linePt1.X), (pt.Y - linePt1.Y)))));
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)));
}
//------------------------------------------------------------------------------
internal bool PointOnPolygon(IntPoint pt, OutPt pp, bool UseFullRange)
{
OutPt pp2 = pp;
while (true)
{
if (PointOnLineSegment(pt, pp2.Pt, pp2.Next.Pt, UseFullRange))
return true;
pp2 = pp2.Next;
if (pp2 == pp) break;
}
return false;
}
//------------------------------------------------------------------------------
internal static bool SlopesEqual(TEdge e1, TEdge e2, bool UseFullRange)
{
if (UseFullRange)
return Int128.Int128Mul(e1.Delta.Y, e2.Delta.X) ==
Int128.Int128Mul(e1.Delta.X, e2.Delta.Y);
else return (cInt)(e1.Delta.Y) * (e2.Delta.X) ==
(cInt)(e1.Delta.X) * (e2.Delta.Y);
}
//------------------------------------------------------------------------------
internal static bool SlopesEqual(IntPoint pt1, IntPoint pt2,
IntPoint pt3, bool UseFullRange)
{
if (UseFullRange)
return Int128.Int128Mul(pt1.Y - pt2.Y, pt2.X - pt3.X) ==
Int128.Int128Mul(pt1.X - pt2.X, pt2.Y - pt3.Y);
else return
(cInt)(pt1.Y - pt2.Y) * (pt2.X - pt3.X) - (cInt)(pt1.X - pt2.X) * (pt2.Y - pt3.Y) == 0;
}
//------------------------------------------------------------------------------
internal static bool SlopesEqual(IntPoint pt1, IntPoint pt2,
IntPoint pt3, IntPoint pt4, bool UseFullRange)
{
if (UseFullRange)
return Int128.Int128Mul(pt1.Y - pt2.Y, pt3.X - pt4.X) ==
Int128.Int128Mul(pt1.X - pt2.X, pt3.Y - pt4.Y);
else return
(cInt)(pt1.Y - pt2.Y) * (pt3.X - pt4.X) - (cInt)(pt1.X - pt2.X) * (pt3.Y - pt4.Y) == 0;
}
//------------------------------------------------------------------------------
internal ClipperBase() //constructor (nb: no external instantiation)
{
m_MinimaList = null;
m_CurrentLM = null;
m_UseFullRange = false;
m_HasOpenPaths = false;
}
//------------------------------------------------------------------------------
public virtual void Clear()
{
DisposeLocalMinimaList();
for (int i = 0; i < m_edges.Count; ++i)
{
for (int j = 0; j < m_edges[i].Count; ++j) m_edges[i][j] = null;
m_edges[i].Clear();
}
m_edges.Clear();
m_UseFullRange = false;
m_HasOpenPaths = false;
}
//------------------------------------------------------------------------------
private void DisposeLocalMinimaList()
{
while( m_MinimaList != null )
{
LocalMinima tmpLm = m_MinimaList.Next;
m_MinimaList = null;
m_MinimaList = tmpLm;
}
m_CurrentLM = null;
}
//------------------------------------------------------------------------------
void RangeTest(IntPoint Pt, ref bool useFullRange)
{
if (useFullRange)
{
if (Pt.X > hiRange || Pt.Y > hiRange || -Pt.X > hiRange || -Pt.Y > hiRange)
throw new ClipperException("Coordinate outside allowed range");
}
else if (Pt.X > loRange || Pt.Y > loRange || -Pt.X > loRange || -Pt.Y > loRange)
{
useFullRange = true;
RangeTest(Pt, ref useFullRange);
}
}
//------------------------------------------------------------------------------
private void InitEdge(TEdge e, TEdge eNext,
TEdge ePrev, IntPoint pt)
{
e.Next = eNext;
e.Prev = ePrev;
e.Curr = pt;
e.OutIdx = Unassigned;
}
//------------------------------------------------------------------------------
private void InitEdge2(TEdge e, 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;
}
SetDx(e);
e.PolyTyp = polyType;
}
//------------------------------------------------------------------------------
private TEdge FindNextLocMin(TEdge E)
{
TEdge E2;
for (;;)
{
while (E.Bot != E.Prev.Bot || E.Curr == E.Top) 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.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;
}
//------------------------------------------------------------------------------
private TEdge ProcessBound(TEdge E, bool LeftBoundIsForward)
{
TEdge EStart, Result = E;
TEdge Horz;
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.Top.Y == E.Next.Bot.Y) E = E.Next;
while (E != Result && E.Dx == horizontal) E = E.Prev;
}
else
{
while (E.Top.Y == E.Prev.Bot.Y) 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;
LocalMinima locMin = new LocalMinima();
locMin.Next = null;
locMin.Y = E.Bot.Y;
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.Dx == horizontal) //ie an adjoining horizontal skip edge
{
if (EStart.Bot.X != E.Bot.X && EStart.Top.X != E.Bot.X)
ReverseHorizontal(E);
}
else if (EStart.Bot.X != E.Bot.X)
ReverseHorizontal(E);
}
EStart = E;
if (LeftBoundIsForward)
{
while (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;
while (Horz.Prev.Dx == horizontal) Horz = Horz.Prev;
if (Horz.Prev.Top.X > Result.Next.Top.X) Result = Horz.Prev;
}
while (E != Result)
{
E.NextInLML = E.Next;
if (E.Dx == horizontal && E != EStart && E.Bot.X != E.Prev.Top.X)
ReverseHorizontal(E);
E = E.Next;
}
if (E.Dx == horizontal && E != EStart && E.Bot.X != E.Prev.Top.X)
ReverseHorizontal(E);
Result = Result.Next; //move to the edge just beyond current bound
}
else
{
while (Result.Top.Y == Result.Prev.Bot.Y && 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.Top.X == Result.Prev.Top.X ||
Horz.Next.Top.X > Result.Prev.Top.X) Result = Horz.Next;
}
while (E != Result)
{
E.NextInLML = E.Prev;
if (E.Dx == horizontal && E != EStart && E.Bot.X != E.Next.Top.X)
ReverseHorizontal(E);
E = E.Prev;
}
if (E.Dx == horizontal && E != EStart && E.Bot.X != E.Next.Top.X)
ReverseHorizontal(E);
Result = Result.Prev; //move to the edge just beyond current bound
}
return Result;
}
//------------------------------------------------------------------------------
public bool AddPath(Path pg, PolyType polyType, bool Closed)
{
#if use_lines
if (!Closed && polyType == PolyType.ptClip)
throw new ClipperException("AddPath: Open paths must be subject.");
#else
if (!Closed)
throw new ClipperException("AddPath: Open paths have been disabled.");
#endif
int highI = (int)pg.Count - 1;
if (Closed) while (highI > 0 && (pg[highI] == pg[0])) --highI;
while (highI > 0 && (pg[highI] == pg[highI - 1])) --highI;
if ((Closed && highI < 2) || (!Closed && highI < 1)) return false;
//create a new edge array ...
List<TEdge> edges = new List<TEdge>(highI+1);
for (int i = 0; i <= highI; i++) edges.Add(new TEdge());
bool IsFlat = true;
//1. Basic (first) edge initialization ...
edges[1].Curr = pg[1];
RangeTest(pg[0], ref m_UseFullRange);
RangeTest(pg[highI], ref m_UseFullRange);
InitEdge(edges[0], edges[1], edges[highI], pg[0]);
InitEdge(edges[highI], edges[0], edges[highI - 1], pg[highI]);
for (int i = highI - 1; i >= 1; --i)
{
RangeTest(pg[i], ref m_UseFullRange);
InitEdge(edges[i], edges[i + 1], edges[i - 1], pg[i]);
}
TEdge eStart = edges[0];
//2. Remove duplicate vertices, and (when closed) collinear edges ...
TEdge E = eStart, eLoopStop = eStart;
for (;;)
{
//nb: allows matching start and end points when not Closed ...
if (E.Curr == E.Next.Curr && (Closed || E.Next != eStart))
{
if (E == E.Next) break;
if (E == eStart) eStart = E.Next;
E = RemoveEdge(E);
eLoopStop = E;
continue;
}
if (E.Prev == E.Next)
break; //only two vertices
else if (Closed &&
SlopesEqual(E.Prev.Curr, E.Curr, E.Next.Curr, m_UseFullRange) &&
(!PreserveCollinear ||
!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 = 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)
{
m_HasOpenPaths = true;
eStart.Prev.OutIdx = Skip;
}
//3. Do second stage of edge initialization ...
E = eStart;
do
{
InitEdge2(E, polyType);
E = E.Next;
if (IsFlat && E.Curr.Y != eStart.Curr.Y) IsFlat = false;
}
while (E != eStart);
//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;
LocalMinima locMin = new LocalMinima();
locMin.Next = null;
locMin.Y = E.Bot.Y;
locMin.LeftBound = null;
locMin.RightBound = E;
locMin.RightBound.Side = EdgeSide.esRight;
locMin.RightBound.WindDelta = 0;
for ( ; ; )
{
if (E.Bot.X != E.Prev.Top.X) ReverseHorizontal(E);
if (E.Next.OutIdx == Skip) break;
E.NextInLML = E.Next;
E = E.Next;
}
InsertLocalMinima(locMin);
m_edges.Add(edges);
return true;
}
m_edges.Add(edges);
bool leftBoundIsForward;
TEdge 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.Bot == E.Prev.Top) E = E.Next;
for (;;)
{
E = FindNextLocMin(E);
if (E == EMin) break;
else if (EMin == null) EMin = E;
//E and E.Prev now share a local minima (left aligned if horizontal).
//Compare their slopes to find which starts which bound ...
LocalMinima locMin = new LocalMinima();
locMin.Next = null;
locMin.Y = E.Bot.Y;
if (E.Dx < E.Prev.Dx)
{
locMin.LeftBound = E.Prev;
locMin.RightBound = E;
leftBoundIsForward = false; //Q.nextInLML = Q.prev
} else
{
locMin.LeftBound = E;
locMin.RightBound = E.Prev;
leftBoundIsForward = true; //Q.nextInLML = Q.next
}
locMin.LeftBound.Side = EdgeSide.esLeft;
locMin.RightBound.Side = EdgeSide.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 = ProcessBound(locMin.LeftBound, leftBoundIsForward);
if (E.OutIdx == Skip) E = ProcessBound(E, leftBoundIsForward);
TEdge 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;
}