-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathProjectF.GameUtils.Clipper.pas
5636 lines (5192 loc) · 174 KB
/
ProjectF.GameUtils.Clipper.pas
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
unit ProjectF.GameUtils.Clipper;
(*******************************************************************************
* *
* Author : Angus Johnson *
* Version : 6.4.2 *
* Date : 27 February 2017 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2017 *
* *
* 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 *
* *
*******************************************************************************)
//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 (with only a minor cost to performance)
//{$DEFINE use_xyz}
//use_lines: Enables open path clipping (with a very minor cost to performance)
{$DEFINE use_lines}
{$IFDEF FPC}
{$DEFINE INLINING}
{$DEFINE UInt64Support}
{$ELSE}
// enable LEGACYIFEND for Delphi XE4+
{$IF CompilerVersion >= 25.0}
{$LEGACYIFEND ON}
{$IFEND}
// use generic lists for NextGen compiler
{$IFDEF NEXTGEN}
{$DEFINE USEGENERICS}
{$ENDIF}
{$IFDEF ConditionalExpressions}
{$IF CompilerVersion >= 15} //Delphi 7
{$DEFINE UInt64Support} //nb: Delphi7 only marginally supports UInt64.
{$IFEND}
{$IF CompilerVersion >= 18} //Delphi 2007
//Inline has been supported since D2005.
//However D2005 and D2006 have an Inline codegen bug (QC41166).
//http://www.thedelphigeek.com/2007/02/nasty-inline-codegen-bug-in-bds-2006.html
{$DEFINE INLINING}
{$IFEND}
{$ENDIF}
{$ENDIF}
interface
uses
SysUtils, Types, Classes,
{$IFDEF USEGENERICS}
Generics.Collections, Generics.Defaults,
{$ENDIF}
Math;
const
def_arc_tolerance = 0.25;
type
{$IFDEF use_int32}
{$IF CompilerVersion < 20} //Delphi 2009
cInt = Integer; //Int32 supported since D2009.
{$ELSE}
cInt = Int32;
{$IFEND}
{$ELSE}
cInt = Int64;
{$ENDIF}
PIntPoint = ^TIntPoint;
{$IFDEF use_xyz}
TIntPoint = record X, Y, Z: cInt; end;
{$ELSE}
TIntPoint = record X, Y: cInt; end;
{$ENDIF}
TIntRect = record Left, Top, Right, Bottom: cInt; end;
TDoublePoint = record X, Y: Double; end;
TArrayOfDoublePoint = array of TDoublePoint;
{$IFDEF use_xyz}
TZFillCallback =
procedure (const E1Bot, E1Top, E2Bot, E2Top: TIntPoint; var Pt: TIntPoint);
{$ENDIF}
TInitOption = (ioReverseSolution, ioStrictlySimple, ioPreserveCollinear);
TInitOptions = set of TInitOption;
TClipType = (ctIntersection, ctUnion, ctDifference, ctXor);
TPolyType = (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
TPolyFillType = (pftEvenOdd, pftNonZero, pftPositive, pftNegative);
//TJoinType & TEndType are used by OffsetPaths()
TJoinType = (jtSquare, jtRound, jtMiter);
TEndType = (etClosedPolygon, etClosedLine,
etOpenButt, etOpenSquare, etOpenRound); //and etSingle still to come
TPath = array of TIntPoint;
TPaths = array of TPath;
TPolyNode = class;
TArrayOfPolyNode = array of TPolyNode;
TPolyNode = class
private
FPath : TPath;
FParent : TPolyNode;
FIndex : Integer;
FCount : Integer;
FBuffLen : Integer;
FIsOpen : Boolean;
FChilds : TArrayOfPolyNode;
FJoinType: TJoinType; //used by ClipperOffset only
FEndType : TEndType; //used by ClipperOffset only
function GetChild(Index: Integer): TPolyNode;
function IsHoleNode: boolean;
procedure AddChild(PolyNode: TPolyNode);
function GetNextSiblingUp: TPolyNode;
public
function GetNext: TPolyNode;
property ChildCount: Integer read FCount;
property Childs[index: Integer]: TPolyNode read GetChild;
property Parent: TPolyNode read FParent;
property IsHole: Boolean read IsHoleNode;
property IsOpen: Boolean read FIsOpen;
property Contour: TPath read FPath;
end;
TPolyTree = class(TPolyNode)
private
FAllNodes: TArrayOfPolyNode; //container for ALL PolyNodes
function GetTotal: Integer;
public
procedure Clear;
function GetFirst: TPolyNode;
destructor Destroy; override;
property Total: Integer read GetTotal;
end;
//the definitions below are used internally ...
TEdgeSide = (esLeft, esRight);
TDirection = (dRightToLeft, dLeftToRight);
POutPt = ^TOutPt;
PEdge = ^TEdge;
TEdge = record
Bot : TIntPoint; //bottom
Curr : TIntPoint; //current (updated for every new scanbeam)
Top : TIntPoint; //top
Dx : Double; //inverse of slope
PolyType : TPolyType;
Side : TEdgeSide; //side only refers to current side of solution poly
WindDelta: Integer; //1 or -1 depending on winding direction
WindCnt : Integer;
WindCnt2 : Integer; //winding count of the opposite PolyType
OutIdx : Integer;
Next : PEdge;
Prev : PEdge;
NextInLML: PEdge;
PrevInAEL: PEdge;
NextInAEL: PEdge;
PrevInSEL: PEdge;
NextInSEL: PEdge;
end;
PEdgeArray = ^TEdgeArray;
TEdgeArray = array[0.. MaxInt div sizeof(TEdge) -1] of TEdge;
PScanbeam = ^TScanbeam;
TScanbeam = record
Y : cInt;
Next : PScanbeam;
end;
PMaxima = ^TMaxima;
TMaxima = record
X : cInt;
Next : PMaxima;
Prev : PMaxima;
end;
PIntersectNode = ^TIntersectNode;
TIntersectNode = record
Edge1: PEdge;
Edge2: PEdge;
Pt : TIntPoint;
end;
PLocalMinimum = ^TLocalMinimum;
TLocalMinimum = record
Y : cInt;
LeftBound : PEdge;
RightBound: PEdge;
end;
//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.
POutRec = ^TOutRec;
TOutRec = record
Idx : Integer;
BottomPt : POutPt;
IsHole : Boolean;
IsOpen : Boolean;
//The 'FirstLeft' field points to another OutRec that contains or is the
//'parent' of OutRec. It is 'first left' because the ActiveEdgeList (AEL) is
//parsed left from the current edge (owning OutRec) until the owner OutRec
//is found. This field simplifies sorting the polygons into a tree structure
//which reflects the parent/child relationships of all polygons.
//This field should be renamed Parent, and will be later.
FirstLeft : POutRec;
Pts : POutPt;
PolyNode : TPolyNode;
end;
TOutPt = record
Idx : Integer;
Pt : TIntPoint;
Next : POutPt;
Prev : POutPt;
end;
PJoin = ^TJoin;
TJoin = record
OutPt1 : POutPt;
OutPt2 : POutPt;
OffPt : TIntPoint; //offset point (provides slope of common edges)
end;
{$IFDEF USEGENERICS}
TEgdeList = TList<PEdgeArray>;
TLocMinList = TList<PLocalMinimum>;
TPolyOutList = TList<POutRec>;
TJoinList = TList<PJoin>;
TIntersecList = TList<PIntersectNode>;
{$ELSE}
TEgdeList = TList;
TLocMinList = TList;
TPolyOutList = TList;
TJoinList = TList;
TIntersecList = TList;
{$ENDIF}
TClipperBase = class
private
FEdgeList : TEgdeList;
FPolyOutList : TPolyOutList;
FScanbeam : PScanbeam; //scanbeam list
FUse64BitRange : Boolean; //see LoRange and HiRange consts notes below
FHasOpenPaths : Boolean;
procedure DisposeLocalMinimaList;
procedure DisposePolyPts(PP: POutPt);
function ProcessBound(E: PEdge; NextIsForward: Boolean): PEdge;
protected
FLocMinList : TLocMinList;
FCurrentLocMinIdx : Integer;
FPreserveCollinear : Boolean;
FActiveEdges : PEdge; //active Edge list
procedure Reset; virtual;
procedure InsertScanbeam(const Y: cInt);
function PopScanbeam(out Y: cInt): Boolean;
function LocalMinimaPending: Boolean;
function PopLocalMinima(Y: cInt;
out LocalMinima: PLocalMinimum): Boolean;
procedure DisposeScanbeamList;
function CreateOutRec: POutRec;
procedure DisposeOutRec(Index: Integer);
procedure DisposeAllOutRecs;
procedure SwapPositionsInAEL(E1, E2: PEdge);
procedure DeleteFromAEL(E: PEdge);
procedure UpdateEdgeIntoAEL(var E: PEdge);
property HasOpenPaths: Boolean read FHasOpenPaths;
public
constructor Create; virtual;
destructor Destroy; override;
procedure Clear; virtual;
function AddPath(const Path: TPath; PolyType: TPolyType; Closed: Boolean): Boolean; virtual;
function AddPaths(const Paths: TPaths; PolyType: TPolyType; Closed: Boolean): Boolean;
//PreserveCollinear: Prevents removal of 'inner' vertices when three or
//more vertices are collinear in solution polygons.
property PreserveCollinear: Boolean
read FPreserveCollinear write FPreserveCollinear;
end;
TClipper = class(TClipperBase)
private
FJoinList : TJoinList;
FGhostJoinList : TJoinList;
FIntersectList : TIntersecList;
FSortedEdges : PEdge; //used for temporary sorting
FClipType : TClipType;
FMaxima : PMaxima; //maxima XPos list
FClipFillType : TPolyFillType;
FSubjFillType : TPolyFillType;
FExecuteLocked : Boolean;
FReverseOutput : Boolean;
FStrictSimple : Boolean;
FUsingPolyTree : Boolean;
{$IFDEF use_xyz}
FZFillCallback : TZFillCallback;
{$ENDIF}
procedure InsertMaxima(const X: cInt);
procedure DisposeMaximaList;
procedure SetWindingCount(Edge: PEdge);
function IsEvenOddFillType(Edge: PEdge): Boolean;
function IsEvenOddAltFillType(Edge: PEdge): Boolean;
procedure AddEdgeToSEL(Edge: PEdge);
function PopEdgeFromSEL(out E: PEdge): Boolean;
procedure CopyAELToSEL;
procedure InsertLocalMinimaIntoAEL(const BotY: cInt);
procedure SwapPositionsInSEL(E1, E2: PEdge);
procedure ProcessHorizontal(HorzEdge: PEdge);
procedure ProcessHorizontals;
function ProcessIntersections(const TopY: cInt): Boolean;
procedure BuildIntersectList(const TopY: cInt);
procedure ProcessIntersectList;
procedure IntersectEdges(E1,E2: PEdge; Pt: TIntPoint);
procedure DoMaxima(E: PEdge);
function FixupIntersectionOrder: Boolean;
procedure ProcessEdgesAtTopOfScanbeam(const TopY: cInt);
function IsContributing(Edge: PEdge): Boolean;
function GetLastOutPt(E: PEdge): POutPt;
procedure AddLocalMaxPoly(E1, E2: PEdge; const Pt: TIntPoint);
function AddLocalMinPoly(E1, E2: PEdge; const Pt: TIntPoint): POutPt;
function AddOutPt(E: PEdge; const Pt: TIntPoint): POutPt;
function GetOutRec(Idx: integer): POutRec;
procedure AppendPolygon(E1, E2: PEdge);
procedure DisposeIntersectNodes;
function BuildResult: TPaths;
function BuildResult2(PolyTree: TPolyTree): Boolean;
procedure FixupOutPolygon(OutRec: POutRec);
procedure FixupOutPolyline(OutRec: POutRec);
procedure SetHoleState(E: PEdge; OutRec: POutRec);
procedure AddJoin(Op1, Op2: POutPt; const OffPt: TIntPoint);
procedure ClearJoins;
procedure AddGhostJoin(OutPt: POutPt; const OffPt: TIntPoint);
procedure ClearGhostJoins;
function JoinPoints(Jr: PJoin; OutRec1, OutRec2: POutRec): Boolean;
procedure FixupFirstLefts1(OldOutRec, NewOutRec: POutRec);
procedure FixupFirstLefts2(InnerOutRec, OuterOutRec: POutRec);
procedure FixupFirstLefts3(OldOutRec, NewOutRec: POutRec);
procedure DoSimplePolygons;
procedure JoinCommonEdges;
procedure FixHoleLinkage(OutRec: POutRec);
protected
function ExecuteInternal: Boolean; virtual;
public
function Execute(clipType: TClipType;
out solution: TPaths;
FillType: TPolyFillType = pftEvenOdd): Boolean; overload;
function Execute(clipType: TClipType;
out solution: TPaths;
subjFillType: TPolyFillType;
clipFillType: TPolyFillType): Boolean; overload;
function Execute(clipType: TClipType;
out PolyTree: TPolyTree;
FillType: TPolyFillType = pftEvenOdd): Boolean; overload;
function Execute(clipType: TClipType;
out PolyTree: TPolyTree;
subjFillType: TPolyFillType;
clipFillType: TPolyFillType): Boolean; overload;
constructor Create(InitOptions: TInitOptions = []); reintroduce; overload;
destructor Destroy; override;
//ReverseSolution: reverses the default orientation
property ReverseSolution: Boolean read FReverseOutput write FReverseOutput;
//StrictlySimple: when false (the default) solutions are 'weakly' simple
property StrictlySimple: Boolean read FStrictSimple write FStrictSimple;
{$IFDEF use_xyz}
property ZFillFunction: TZFillCallback read FZFillCallback write FZFillCallback;
{$ENDIF}
end;
TClipperOffset = class
private
FDelta: Double;
FSinA, FSin, FCos: Extended;
FMiterLim, FStepsPerRad: Double;
FNorms: TArrayOfDoublePoint;
FSolution: TPaths;
FOutPos: Integer;
FInP: TPath;
FOutP: TPath;
FLowest: TIntPoint; //X = Path index, Y = Path offset (to lowest point)
FPolyNodes: TPolyNode;
FMiterLimit: Double;
FArcTolerance: Double;
procedure AddPoint(const Pt: TIntPoint);
procedure DoSquare(J, K: Integer);
procedure DoMiter(J, K: Integer; R: Double);
procedure DoRound(J, K: Integer);
procedure OffsetPoint(J: Integer;
var K: Integer; JoinType: TJoinType);
procedure FixOrientations;
procedure DoOffset(Delta: Double);
public
constructor Create(MiterLimit: Double = 2; ArcTolerance: Double = def_arc_tolerance);
destructor Destroy; override;
procedure AddPath(const Path: TPath; JoinType: TJoinType; EndType: TEndType);
procedure AddPaths(const Paths: TPaths; JoinType: TJoinType; EndType: TEndType);
procedure Clear;
procedure Execute(out solution: TPaths; Delta: Double); overload;
procedure Execute(out solution: TPolyTree; Delta: Double); overload;
property MiterLimit: double read FMiterLimit write FMiterLimit;
property ArcTolerance: double read FArcTolerance write FArcTolerance;
end;
function Orientation(const Pts: TPath): Boolean; overload;
function Area(const Pts: TPath): Double; overload;
function PointInPolygon (const pt: TIntPoint; const poly: TPath): Integer; overload;
function GetBounds(const polys: TPaths): TIntRect;
{$IFDEF use_xyz}
function IntPoint(const X, Y: Int64; Z: Int64 = 0): TIntPoint; overload;
function IntPoint(const X, Y: Double; Z: Double = 0): TIntPoint; overload;
{$ELSE}
function IntPoint(const X, Y: cInt): TIntPoint; overload;
function IntPoint(const X, Y: Double): TIntPoint; overload;
{$ENDIF}
function DoublePoint(const X, Y: Double): TDoublePoint; overload;
function DoublePoint(const Ip: TIntPoint): TDoublePoint; overload;
function ReversePath(const Pts: TPath): TPath;
function ReversePaths(const Pts: TPaths): TPaths;
//SimplifyPolygon converts a self-intersecting polygon into a simple polygon.
function SimplifyPolygon(const Poly: TPath; FillType: TPolyFillType = pftEvenOdd): TPaths;
function SimplifyPolygons(const Polys: TPaths; FillType: TPolyFillType = pftEvenOdd): TPaths;
//CleanPolygon removes adjacent vertices closer than the specified distance.
function CleanPolygon(const Poly: TPath; Distance: double = 1.415): TPath;
function CleanPolygons(const Polys: TPaths; Distance: double = 1.415): TPaths;
function MinkowskiSum(const Pattern, Path: TPath; PathIsClosed: Boolean): TPaths; overload;
function MinkowskiSum(const Pattern: TPath; const Paths: TPaths;
PathFillType: TPolyFillType; PathIsClosed: Boolean): TPaths; overload;
function MinkowskiDiff(const Poly1, Poly2: TPath): TPaths;
function PolyTreeToPaths(PolyTree: TPolyTree): TPaths;
function ClosedPathsFromPolyTree(PolyTree: TPolyTree): TPaths;
function OpenPathsFromPolyTree(PolyTree: TPolyTree): TPaths;
const
//The SlopesEqual function places the most limits on coordinate values
//So, to avoid overflow errors, they must not exceed the following values...
//Also, if all coordinates are within +/-LoRange, then calculations will be
//faster. Otherwise using Int128 math will render the library ~10-15% slower.
{$IFDEF use_int32}
LoRange: cInt = 46340;
HiRange: cInt = 46340;
{$ELSE}
LoRange: cInt = $B504F333; //3.0e+9
HiRange: cInt = $3FFFFFFFFFFFFFFF; //9.2e+18
{$ENDIF}
implementation
//NOTE: The Clipper library has been developed with software that uses an
//inverted Y axis display. Therefore 'above' and 'below' in the code's comments
//will reflect this. For example: given coord A (0,20) and coord B (0,10),
//A.Y would be considered BELOW B.Y to correctly understand the comments.
const
Horizontal: Double = -3.4e+38;
Unassigned : Integer = -1;
Skip : Integer = -2; //flag for the edge that closes an open path
Tolerance : double = 1.0E-15;
Two_Pi : double = 2 * PI;
resourcestring
rsDoMaxima = 'DoMaxima error';
rsUpdateEdgeIntoAEL = 'UpdateEdgeIntoAEL error';
rsHorizontal = 'ProcessHorizontal error';
rsInvalidInt = 'Coordinate exceeds range bounds';
rsIntersect = 'Intersection error';
rsOpenPath = 'AddPath: Open paths must be subject.';
rsOpenPath2 = 'AddPath: Open paths have been disabled.';
rsOpenPath3 = 'Error: TPolyTree struct is needed for open path clipping.';
rsPolylines = 'Error intersecting polylines';
rsClipperOffset = 'Error: No PolyTree assigned';
//------------------------------------------------------------------------------
// TPolyNode methods ...
//------------------------------------------------------------------------------
function TPolyNode.GetChild(Index: Integer): TPolyNode;
begin
if (Index < 0) or (Index >= FCount) then
raise Exception.Create('TPolyNode range error: ' + inttostr(Index));
Result := FChilds[Index];
end;
//------------------------------------------------------------------------------
procedure TPolyNode.AddChild(PolyNode: TPolyNode);
begin
if FCount = FBuffLen then
begin
Inc(FBuffLen, 16);
SetLength(FChilds, FBuffLen);
end;
PolyNode.FParent := self;
PolyNode.FIndex := FCount;
FChilds[FCount] := PolyNode;
Inc(FCount);
end;
//------------------------------------------------------------------------------
function TPolyNode.IsHoleNode: boolean;
var
Node: TPolyNode;
begin
Result := True;
Node := FParent;
while Assigned(Node) do
begin
Result := not Result;
Node := Node.FParent;
end;
end;
//------------------------------------------------------------------------------
function TPolyNode.GetNext: TPolyNode;
begin
if FCount > 0 then
Result := FChilds[0] else
Result := GetNextSiblingUp;
end;
//------------------------------------------------------------------------------
function TPolyNode.GetNextSiblingUp: TPolyNode;
begin
if not Assigned(FParent) then //protects against TPolyTree.GetNextSiblingUp()
Result := nil
else if FIndex = FParent.FCount -1 then
Result := FParent.GetNextSiblingUp
else
Result := FParent.Childs[FIndex +1];
end;
//------------------------------------------------------------------------------
// TPolyTree methods ...
//------------------------------------------------------------------------------
destructor TPolyTree.Destroy;
begin
Clear;
inherited;
end;
//------------------------------------------------------------------------------
procedure TPolyTree.Clear;
var
I: Integer;
begin
for I := 0 to high(FAllNodes) do FAllNodes[I].Free;
FAllNodes := nil;
FBuffLen := 16;
SetLength(FChilds, FBuffLen);
FCount := 0;
end;
//------------------------------------------------------------------------------
function TPolyTree.GetFirst: TPolyNode;
begin
if FCount > 0 then
Result := FChilds[0] else
Result := nil;
end;
//------------------------------------------------------------------------------
function TPolyTree.GetTotal: Integer;
begin
Result := length(FAllNodes);
//with negative offsets, ignore the hidden outer polygon ...
if (Result > 0) and (FAllNodes[0] <> FChilds[0]) then dec(Result);
end;
{$IFNDEF use_int32}
//------------------------------------------------------------------------------
// UInt64 math support for Delphi 6
//------------------------------------------------------------------------------
{$OVERFLOWCHECKS OFF}
{$IFNDEF UInt64Support}
function CompareUInt64(const i, j: Int64): Integer;
begin
if Int64Rec(i).Hi < Int64Rec(j).Hi then
Result := -1
else if Int64Rec(i).Hi > Int64Rec(j).Hi then
Result := 1
else if Int64Rec(i).Lo < Int64Rec(j).Lo then
Result := -1
else if Int64Rec(i).Lo > Int64Rec(j).Lo then
Result := 1
else
Result := 0;
end;
{$ENDIF}
function UInt64LT(const i, j: Int64): Boolean; {$IFDEF INLINING} inline; {$ENDIF}
begin
{$IFDEF UInt64Support}
Result := UInt64(i) < UInt64(j);
{$ELSE}
Result := CompareUInt64(i, j) = -1;
{$ENDIF}
end;
function UInt64GT(const i, j: Int64): Boolean; {$IFDEF INLINING} inline; {$ENDIF}
begin
{$IFDEF UInt64Support}
Result := UInt64(i) > UInt64(j);
{$ELSE}
Result := CompareUInt64(i, j) = 1;
{$ENDIF}
end;
{$OVERFLOWCHECKS ON}
//------------------------------------------------------------------------------
// Int128 Functions ...
//------------------------------------------------------------------------------
const
Mask32Bits = $FFFFFFFF;
type
//nb: TInt128.Lo is typed Int64 instead of UInt64 to provide Delphi 7
//compatability. However while UInt64 isn't a recognised type in
//Delphi 7, it can still be used in typecasts.
TInt128 = record
Hi : Int64;
Lo : Int64;
end;
{$OVERFLOWCHECKS OFF}
procedure Int128Negate(var Val: TInt128);
begin
if Val.Lo = 0 then
begin
Val.Hi := -Val.Hi;
end else
begin
Val.Lo := -Val.Lo;
Val.Hi := not Val.Hi;
end;
end;
//------------------------------------------------------------------------------
function Int128(const val: Int64): TInt128; overload;
begin
Result.Lo := val;
if val < 0 then
Result.Hi := -1 else
Result.Hi := 0;
end;
//------------------------------------------------------------------------------
function Int128Equal(const Int1, Int2: TInt128): Boolean;
begin
Result := (Int1.Lo = Int2.Lo) and (Int1.Hi = Int2.Hi);
end;
//------------------------------------------------------------------------------
function Int128LessThan(const Int1, Int2: TInt128): Boolean;
begin
if (Int1.Hi <> Int2.Hi) then Result := Int1.Hi < Int2.Hi
else Result := UInt64LT(Int1.Lo, Int2.Lo);
end;
//------------------------------------------------------------------------------
function Int128IsNegative(const Int: TInt128): Boolean;
begin
Result := Int.Hi < 0;
end;
//------------------------------------------------------------------------------
function Int128IsPositive(const Int: TInt128): Boolean;
begin
Result := (Int.Hi > 0) or ((Int.Hi = 0) and (Int.Lo <> 0));
end;
//------------------------------------------------------------------------------
function Int128Add(const Int1, Int2: TInt128): TInt128;
begin
Result.Lo := Int1.Lo + Int2.Lo;
Result.Hi := Int1.Hi + Int2.Hi;
if UInt64LT(Result.Lo, Int1.Lo) then Inc(Result.Hi);
end;
//------------------------------------------------------------------------------
function Int128Sub(const Int1, Int2: TInt128): TInt128;
begin
Result.Hi := Int1.Hi - Int2.Hi;
Result.Lo := Int1.Lo - Int2.Lo;
if UInt64GT(Result.Lo, Int1.Lo) then Dec(Result.Hi);
end;
//------------------------------------------------------------------------------
function Int128Mul(Int1, Int2: Int64): TInt128;
var
A, B, C: Int64;
Int1Hi, Int1Lo, Int2Hi, Int2Lo: Int64;
Negate: Boolean;
begin
//save the Result's sign before clearing both sign bits ...
Negate := (Int1 < 0) <> (Int2 < 0);
if Int1 < 0 then Int1 := -Int1;
if Int2 < 0 then Int2 := -Int2;
Int1Hi := Int1 shr 32;
Int1Lo := Int1 and Mask32Bits;
Int2Hi := Int2 shr 32;
Int2Lo := Int2 and Mask32Bits;
A := Int1Hi * Int2Hi;
B := Int1Lo * Int2Lo;
//because the high (sign) bits in both int1Hi & int2Hi have been zeroed,
//there's no risk of 64 bit overflow in the following assignment
//(ie: $7FFFFFFF*$FFFFFFFF + $7FFFFFFF*$FFFFFFFF < 64bits)
C := Int1Hi*Int2Lo + Int2Hi*Int1Lo;
//Result = A shl 64 + C shl 32 + B ...
Result.Hi := A + (C shr 32);
A := C shl 32;
Result.Lo := A + B;
if UInt64LT(Result.Lo, A) then
Inc(Result.Hi);
if Negate then Int128Negate(Result);
end;
//------------------------------------------------------------------------------
function Int128Div(Dividend, Divisor: TInt128{; out Remainder: TInt128}): TInt128;
var
Cntr: TInt128;
Negate: Boolean;
begin
if (Divisor.Lo = 0) and (Divisor.Hi = 0) then
raise Exception.create('int128Div error: divide by zero');
Negate := (Divisor.Hi < 0) <> (Dividend.Hi < 0);
if Dividend.Hi < 0 then Int128Negate(Dividend);
if Divisor.Hi < 0 then Int128Negate(Divisor);
if Int128LessThan(Divisor, Dividend) then
begin
Result.Hi := 0;
Result.Lo := 0;
Cntr.Lo := 1;
Cntr.Hi := 0;
//while (Dividend >= Divisor) do
while not Int128LessThan(Dividend, Divisor) do
begin
//divisor := divisor shl 1;
Divisor.Hi := Divisor.Hi shl 1;
if Divisor.Lo < 0 then Inc(Divisor.Hi);
Divisor.Lo := Divisor.Lo shl 1;
//Cntr := Cntr shl 1;
Cntr.Hi := Cntr.Hi shl 1;
if Cntr.Lo < 0 then Inc(Cntr.Hi);
Cntr.Lo := Cntr.Lo shl 1;
end;
//Divisor := Divisor shr 1;
Divisor.Lo := Divisor.Lo shr 1;
if Divisor.Hi and $1 = $1 then
Int64Rec(Divisor.Lo).Hi := Cardinal(Int64Rec(Divisor.Lo).Hi) or $80000000;
Divisor.Hi := Divisor.Hi shr 1;
//Cntr := Cntr shr 1;
Cntr.Lo := Cntr.Lo shr 1;
if Cntr.Hi and $1 = $1 then
Int64Rec(Cntr.Lo).Hi := Cardinal(Int64Rec(Cntr.Lo).Hi) or $80000000;
Cntr.Hi := Cntr.Hi shr 1;
//while (Cntr > 0) do
while not ((Cntr.Hi = 0) and (Cntr.Lo = 0)) do
begin
//if ( Dividend >= Divisor) then
if not Int128LessThan(Dividend, Divisor) then
begin
//Dividend := Dividend - Divisor;
Dividend := Int128Sub(Dividend, Divisor);
//Result := Result or Cntr;
Result.Hi := Result.Hi or Cntr.Hi;
Result.Lo := Result.Lo or Cntr.Lo;
end;
//Divisor := Divisor shr 1;
Divisor.Lo := Divisor.Lo shr 1;
if Divisor.Hi and $1 = $1 then
Int64Rec(Divisor.Lo).Hi := Cardinal(Int64Rec(Divisor.Lo).Hi) or $80000000;
Divisor.Hi := Divisor.Hi shr 1;
//Cntr := Cntr shr 1;
Cntr.Lo := Cntr.Lo shr 1;
if Cntr.Hi and $1 = $1 then
Int64Rec(Cntr.Lo).Hi := Cardinal(Int64Rec(Cntr.Lo).Hi) or $80000000;
Cntr.Hi := Cntr.Hi shr 1;
end;
if Negate then Int128Negate(Result);
//Remainder := Dividend;
end
else if (Divisor.Hi = Dividend.Hi) and (Divisor.Lo = Dividend.Lo) then
begin
if Negate then Result := Int128(-1) else Result := Int128(1);
end else
begin
Result := Int128(0);
end;
end;
//------------------------------------------------------------------------------
function Int128AsDouble(val: TInt128): Double;
const
shift64: Double = 18446744073709551616.0;
var
lo: Int64;
begin
if (val.Hi < 0) then
begin
lo := -val.Lo;
if lo = 0 then
Result := val.Hi * shift64 else
Result := -(not val.Hi * shift64 + UInt64(lo));
end else
Result := val.Hi * shift64 + UInt64(val.Lo);
end;
//------------------------------------------------------------------------------
{$OVERFLOWCHECKS ON}
{$ENDIF}
//------------------------------------------------------------------------------
// Miscellaneous Functions ...
//------------------------------------------------------------------------------
function PointCount(Pts: POutPt): Integer;
var
P: POutPt;
begin
Result := 0;
if not Assigned(Pts) then Exit;
P := Pts;
repeat
Inc(Result);
P := P.Next;
until P = Pts;
end;
//------------------------------------------------------------------------------
function PointsEqual(const P1, P2: TIntPoint): Boolean; {$IFDEF INLINING} inline; {$ENDIF}
begin
Result := (P1.X = P2.X) and (P1.Y = P2.Y);
end;
//------------------------------------------------------------------------------
{$IFDEF use_xyz}
function IntPoint(const X, Y: Int64; Z: Int64 = 0): TIntPoint;
begin
Result.X := X;
Result.Y := Y;
Result.Z := Z;
end;
//------------------------------------------------------------------------------
function IntPoint(const X, Y: Double; Z: Double = 0): TIntPoint;
begin
Result.X := Round(X);
Result.Y := Round(Y);
Result.Z := Round(Z);
end;
//------------------------------------------------------------------------------
{$ELSE}
function IntPoint(const X, Y: cInt): TIntPoint;
begin
Result.X := X;
Result.Y := Y;
end;
//------------------------------------------------------------------------------
function IntPoint(const X, Y: Double): TIntPoint;
begin
Result.X := Round(X);
Result.Y := Round(Y);
end;
{$ENDIF}
//------------------------------------------------------------------------------
function DoublePoint(const X, Y: Double): TDoublePoint;
begin
Result.X := X;
Result.Y := Y;
end;
//------------------------------------------------------------------------------
function DoublePoint(const Ip: TIntPoint): TDoublePoint;
begin
Result.X := Ip.X;
Result.Y := Ip.Y;
end;
//------------------------------------------------------------------------------
function Area(const Pts: TPath): Double;
var
I, J, Cnt: Integer;
D: Double;
begin
Result := 0.0;
Cnt := Length(Pts);
if (Cnt < 3) then Exit;
J := cnt - 1;
for I := 0 to Cnt -1 do
begin
D := (Pts[j].X + Pts[i].X);
Result := Result + D * (Pts[j].Y - Pts[i].Y);
J := I;
end;
Result := -Result * 0.5;
end;
//------------------------------------------------------------------------------
function Area(Op: POutPt): Double; overload;
var
op2: POutPt;
d2: Double;
begin
Result := 0;
op2 := op;
if Assigned(op2) then
repeat
d2 := op2.Prev.Pt.X + op2.Pt.X;
Result := Result + d2 * (op2.Prev.Pt.Y - op2.Pt.Y);
op2 := op2.Next;
until op2 = op;
Result := Result * 0.5;
end;
//------------------------------------------------------------------------------
function Area(OutRec: POutRec): Double; overload;
begin
result := Area(OutRec.Pts);
end;
//------------------------------------------------------------------------------
function Orientation(const Pts: TPath): Boolean;
begin
Result := Area(Pts) >= 0;
end;
//------------------------------------------------------------------------------
function ReversePath(const Pts: TPath): TPath;
var
I, HighI: Integer;
begin
HighI := high(Pts);
SetLength(Result, HighI +1);