-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclipper.cs
4457 lines (4081 loc) · 153 KB
/
clipper.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
/*******************************************************************************
* *
* Author : Angus Johnson *
* Version : 6.1.3 (float) - Experimental *
* Date : 16 January 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_xyz: adds a Z member to IntPoint. Adds a minor cost to performance.
//#define use_xyz
//use_lines: Enables line clipping. Adds a very minor cost to performance.
//#define use_lines
//use_deprecated: Enables support for the obsolete OffsetPaths() function
//which has been replace with the ClipperOffset class.
#define use_deprecated
using System;
using System.Collections.Generic;
//using System.Text; //for Int128.AsString() & StringBuilder
//using System.IO; //debugging with streamReader & StreamWriter
//using System.Windows.Forms; //debugging to clipboard
namespace ClipperLib
{
using Path = List<FPoint>;
using Paths = List<List<FPoint>>;
//------------------------------------------------------------------------------
// PolyTree & PolyNode classes
//------------------------------------------------------------------------------
public class PolyTree : PolyNode
{
internal List<PolyNode> m_AllPolys = new List<PolyNode>();
~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 { return m_AllPolys.Count; }
}
}
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 { get; set; }
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
public struct FPoint
{
public double X;
public double Y;
#if use_xyz
public Int64 Z;
public FPoint(double x, double y, Int64 z = 0)
{
this.X = x; this.Y = y; this.Z = z;
}
public FPoint(FPoint pt)
{
this.X = pt.X; this.Y = pt.Y; this.Z = pt.Z;
}
#else
public FPoint(double X, double Y)
{
this.X = X; this.Y = Y;
}
public FPoint(FPoint pt)
{
this.X = pt.X; this.Y = pt.Y;
}
#endif
public static bool operator ==(FPoint a, FPoint b)
{
return Global.IsAlmostEqual(a.X, b.X) &&
Global.IsAlmostEqual(a.Y, b.Y);
}
public static bool operator !=(FPoint a, FPoint b)
{
return !Global.IsAlmostEqual(a.X, b.X) ||
!Global.IsAlmostEqual(a.Y, b.Y);
}
public override bool Equals(object obj)
{
if (obj == null) return false;
if (obj is FPoint)
{
FPoint a = (FPoint)obj;
return (this == a);
}
else return false;
}
public override int GetHashCode()
{
//simply prevents a compiler warning
return base.GetHashCode();
}
}
public struct FRect
{
public double left;
public double top;
public double right;
public double bottom;
public FRect(double l, double t, double r, double b)
{
this.left = l; this.top = t;
this.right = r; this.bottom = b;
}
public FRect(FRect 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 };
#if use_deprecated
public enum EndType_ { etClosed, etButt, etSquare, etRound };
#endif
internal enum EdgeSide {esLeft, esRight};
internal enum Direction {dRightToLeft, dLeftToRight};
internal class TEdge {
internal FPoint Bot;
internal FPoint Curr;
internal FPoint Top;
internal FPoint Delta;
internal double Dx;
internal PolyType PolyTyp;
internal EdgeSide Side;
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 FPoint Pt;
};
public class MyIntersectNodeSort : IComparer<IntersectNode>
{
public int Compare(IntersectNode node1, IntersectNode node2)
{
double diff = node2.Pt.Y - node1.Pt.Y;
if (diff == 0) return 0;
else if (diff > 0) return 1;
else return -1;
}
}
internal class LocalMinima
{
internal double Y;
internal TEdge LeftBound;
internal TEdge RightBound;
internal LocalMinima Next;
};
internal class Scanbeam
{
internal double Y;
internal Scanbeam Next;
};
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 FPoint Pt;
internal OutPt Next;
internal OutPt Prev;
};
internal class Join
{
internal OutPt OutPt1;
internal OutPt OutPt2;
internal FPoint OffPt;
};
internal static class Global
{
internal static bool IsAlmostEqual(double A, double B)
{
//http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
const int maxUlps = 10000;
Int64 aInt = BitConverter.DoubleToInt64Bits(A);
if (aInt < 0) aInt = Int64.MinValue - aInt;
Int64 bInt = BitConverter.DoubleToInt64Bits(B);
if (bInt < 0) bInt = Int64.MinValue - bInt;
Int64 sub = unchecked(aInt - bInt);
if (sub > aInt != bInt < 0) return false;
return (sub <= 0 && sub > -maxUlps) || (sub > 0 && sub < maxUlps);
}
//----------------------------------------------------------------------
};
public class ClipperBase
{
protected const double horizontal = -3.4E+38;
protected const int Skip = -2;
protected const int Unassigned = -1;
internal LocalMinima m_MinimaList;
internal LocalMinima m_CurrentLM;
internal List<List<TEdge>> m_edges = new List<List<TEdge>>();
internal bool m_HasOpenPaths;
//------------------------------------------------------------------------------
public bool PreserveCollinear
{
get;
set;
}
//------------------------------------------------------------------------------
internal static bool IsHorizontal(TEdge e)
{
return e.Delta.Y == 0;
}
//------------------------------------------------------------------------------
internal bool PointIsVertex(FPoint pt, OutPt pp)
{
OutPt pp2 = pp;
do
{
if (pp2.Pt == pt) return true;
pp2 = pp2.Next;
}
while (pp2 != pp);
return false;
}
//------------------------------------------------------------------------------
internal bool PointOnLineSegment(FPoint pt,
FPoint linePt1, FPoint linePt2)
{
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(FPoint pt, OutPt pp)
{
OutPt pp2 = pp;
while (true)
{
if (PointOnLineSegment(pt, pp2.Pt, pp2.Next.Pt))
return true;
pp2 = pp2.Next;
if (pp2 == pp) break;
}
return false;
}
//------------------------------------------------------------------------------
internal static bool SlopesEqual(TEdge e1, TEdge e2)
{
return Global.IsAlmostEqual(e1.Delta.Y * e2.Delta.X, e1.Delta.X * e2.Delta.Y);
}
//------------------------------------------------------------------------------
protected static bool SlopesEqual(FPoint pt1, FPoint pt2, FPoint pt3)
{
return Global.IsAlmostEqual((pt1.Y - pt2.Y) * (pt2.X - pt3.X),
(pt1.X - pt2.X) * (pt2.Y - pt3.Y));
}
//------------------------------------------------------------------------------
protected static bool SlopesEqual(FPoint pt1, FPoint pt2, FPoint pt3, FPoint pt4)
{
return Global.IsAlmostEqual((pt1.Y - pt2.Y) * (pt3.X - pt4.X),
(pt1.X - pt2.X) * (pt3.Y - pt4.Y));
}
//------------------------------------------------------------------------------
internal ClipperBase() //constructor (nb: no external instantiation)
{
m_MinimaList = null;
m_CurrentLM = null;
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_HasOpenPaths = false;
}
//------------------------------------------------------------------------------
private void DisposeLocalMinimaList()
{
while( m_MinimaList != null )
{
LocalMinima tmpLm = m_MinimaList.Next;
m_MinimaList = null;
m_MinimaList = tmpLm;
}
m_CurrentLM = null;
}
//------------------------------------------------------------------------------
private void InitEdge(TEdge e, TEdge eNext,
TEdge ePrev, FPoint 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 IsClockwise)
{
TEdge EStart = E, Result = E;
TEdge Horz;
double StartX;
if (E.Dx == horizontal)
{
//it's possible for adjacent overlapping horz edges to start heading left
//before finishing right, so ...
if (IsClockwise) StartX = E.Prev.Bot.X;
else StartX = E.Next.Bot.X;
if (E.Bot.X != StartX) ReverseHorizontal(E);
}
if (Result.OutIdx != Skip)
{
if (IsClockwise)
{
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)
{
if (!IsClockwise) Result = Horz.Prev;
}
else 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)
{
if (!IsClockwise) Result = Horz.Next;
}
else if (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
}
}
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)
{
while (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 ...
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 (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;
LocalMinima locMin = new LocalMinima();
locMin.Next = null;
locMin.Y = E.Bot.Y;
locMin.LeftBound = null;
locMin.RightBound = E;
locMin.RightBound.WindDelta = 0;
Result = ProcessBound(locMin.RightBound, IsClockwise);
InsertLocalMinima(locMin);
}
}
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];
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)
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 (;;)
{
if (E.Curr == E.Next.Curr)
{
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) &&
(!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) 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 ...
TEdge eHighest = eStart;
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;
if (E.Prev.Bot.X < E.Prev.Top.X) ReverseHorizontal(E.Prev);
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;
while (E.Next.OutIdx != Skip)
{
E.NextInLML = E.Next;
if (E.Bot.X != E.Prev.Top.X) ReverseHorizontal(E);
E = E.Next;
}
InsertLocalMinima(locMin);
m_edges.Add(edges);
return true;
}
m_edges.Add(edges);
bool clockwise;
TEdge EMin = null;
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;
clockwise = false; //Q.nextInLML = Q.prev
} else
{
locMin.LeftBound = E;
locMin.RightBound = E.Prev;
clockwise = 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, clockwise);
TEdge E2 = ProcessBound(locMin.RightBound, !clockwise);
if (locMin.LeftBound.OutIdx == Skip)
locMin.LeftBound = null;
else if (locMin.RightBound.OutIdx == Skip)
locMin.RightBound = null;
InsertLocalMinima(locMin);
if (!clockwise) E = E2;
}
return true;
}
//------------------------------------------------------------------------------
public bool AddPaths(Paths ppg, PolyType polyType, bool closed)
{
bool result = false;
for (int i = 0; i < ppg.Count; ++i)
if (AddPath(ppg[i], polyType, closed)) result = true;
return result;
}
//------------------------------------------------------------------------------
internal bool Pt2IsBetweenPt1AndPt3(FPoint pt1, FPoint pt2, FPoint pt3)
{
if ((pt1 == pt3) || (pt1 == pt2) || (pt3 == pt2)) return false;
else if (pt1.X != pt3.X) return (pt2.X > pt1.X) == (pt2.X < pt3.X);
else return (pt2.Y > pt1.Y) == (pt2.Y < pt3.Y);
}
//------------------------------------------------------------------------------
TEdge RemoveEdge(TEdge e)
{
//removes e from double_linked_list (but without removing from memory)
e.Prev.Next = e.Next;
e.Next.Prev = e.Prev;
TEdge result = e.Next;
e.Prev = null; //flag as removed (see ClipperBase.Clear)
return result;
}
//------------------------------------------------------------------------------
private void SetDx(TEdge e)
{
e.Delta.X = (e.Top.X - e.Bot.X);
e.Delta.Y = (e.Top.Y - e.Bot.Y);
if (e.Delta.Y == 0) e.Dx = horizontal;
else e.Dx = (double)(e.Delta.X) / (e.Delta.Y);
}
//---------------------------------------------------------------------------
private void InsertLocalMinima(LocalMinima newLm)
{
if( m_MinimaList == null )
{
m_MinimaList = newLm;
}
else if( newLm.Y >= m_MinimaList.Y )
{
newLm.Next = m_MinimaList;
m_MinimaList = newLm;
} else
{
LocalMinima tmpLm = m_MinimaList;
while( tmpLm.Next != null && ( newLm.Y < tmpLm.Next.Y ) )
tmpLm = tmpLm.Next;
newLm.Next = tmpLm.Next;
tmpLm.Next = newLm;
}
}
//------------------------------------------------------------------------------
protected void PopLocalMinima()
{
if (m_CurrentLM == null) return;
m_CurrentLM = m_CurrentLM.Next;
}
//------------------------------------------------------------------------------
private void ReverseHorizontal(TEdge e)
{
//swap horizontal edges' top and bottom x's so they follow the natural
//progression of the bounds - ie so their xbots will align with the
//adjoining lower edge. [Helpful in the ProcessHorizontal() method.]
double tmp = e.Top.X;
e.Top.X = e.Bot.X;
e.Bot.X = tmp;
#if use_xyz
Int64 tmp2 = e.Top.Z;
e.Top.Z = e.Bot.Z;
e.Bot.Z = tmp2;
#endif
}
//------------------------------------------------------------------------------
protected virtual void Reset()
{
m_CurrentLM = m_MinimaList;
if (m_CurrentLM == null) return; //ie nothing to process
//reset all edges ...
LocalMinima lm = m_MinimaList;
while (lm != null)
{
TEdge e = lm.LeftBound;
if (e != null)
{
e.Curr = e.Bot;
e.Side = EdgeSide.esLeft;
e.OutIdx = Unassigned;
}
e = lm.RightBound;
if (e != null)
{
e.Curr = e.Bot;
e.Side = EdgeSide.esRight;
e.OutIdx = Unassigned;
}
lm = lm.Next;
}
}
//------------------------------------------------------------------------------
public static FRect GetBounds(Paths paths)
{
int i = 0, cnt = paths.Count;
while (i < cnt && paths[i].Count == 0) i++;
if (i == cnt) return new FRect(0,0,0,0);
FRect result = new FRect();
result.left = paths[i][0].X;
result.right = result.left;
result.top = paths[i][0].Y;
result.bottom = result.top;
for (; i < cnt; i++)
for (int j = 0; j < paths[i].Count; j++)
{
if (paths[i][j].X < result.left) result.left = paths[i][j].X;
else if (paths[i][j].X > result.right) result.right = paths[i][j].X;
if (paths[i][j].Y < result.top) result.top = paths[i][j].Y;
else if (paths[i][j].Y > result.bottom) result.bottom = paths[i][j].Y;
}
return result;
}
} //end ClipperBase
public class Clipper : ClipperBase
{
//InitOptions that can be passed to the constructor ...
public const int ioReverseSolution = 1;
public const int ioStrictlySimple = 2;
public const int ioPreserveCollinear = 4;
private List<OutRec> m_PolyOuts;
private ClipType m_ClipType;
private Scanbeam m_Scanbeam;
private TEdge m_ActiveEdges;
private TEdge m_SortedEdges;
private List<IntersectNode> m_IntersectList;
IComparer<IntersectNode> m_IntersectNodeComparer;
private bool m_ExecuteLocked;
private PolyFillType m_ClipFillType;
private PolyFillType m_SubjFillType;
private List<Join> m_Joins;
private List<Join> m_GhostJoins;
private bool m_UsingPolyTree;
#if use_xyz
public delegate void TZFillCallback(FPoint vert1, FPoint vert2, ref FPoint intersectPt);
public TZFillCallback ZFillFunction { get; set; }
#endif
public Clipper(int InitOptions = 0): base() //constructor
{
m_Scanbeam = null;
m_ActiveEdges = null;
m_SortedEdges = null;
m_IntersectList = new List<IntersectNode>();
m_IntersectNodeComparer = new MyIntersectNodeSort();
m_ExecuteLocked = false;
m_UsingPolyTree = false;
m_PolyOuts = new List<OutRec>();
m_Joins = new List<Join>();
m_GhostJoins = new List<Join>();
ReverseSolution = (ioReverseSolution & InitOptions) != 0;
StrictlySimple = (ioStrictlySimple & InitOptions) != 0;
PreserveCollinear = (ioPreserveCollinear & InitOptions) != 0;
#if use_xyz
ZFillFunction = null;
#endif
}
//------------------------------------------------------------------------------
public override void Clear()
{
if (m_edges.Count == 0) return; //avoids problems with ClipperBase destructor
DisposeAllPolyPts();
base.Clear();
}
//------------------------------------------------------------------------------
void DisposeScanbeamList()
{
while ( m_Scanbeam != null ) {
Scanbeam sb2 = m_Scanbeam.Next;
m_Scanbeam = null;
m_Scanbeam = sb2;
}
}
//------------------------------------------------------------------------------
protected override void Reset()