| 1 |
unit Clipper.Engine; |
| 2 |
|
| 3 |
(******************************************************************************* |
| 4 |
* Author : Angus Johnson * |
| 5 |
* Date : 22 November 2024 * |
| 6 |
* Website : http://www.angusj.com * |
| 7 |
* Copyright : Angus Johnson 2010-2024 * |
| 8 |
* Purpose : This is the main polygon clipping module * |
| 9 |
* License : http://www.boost.org/LICENSE_1_0.txt * |
| 10 |
*******************************************************************************) |
| 11 |
|
| 12 |
interface |
| 13 |
|
| 14 |
{$I Clipper.inc} |
| 15 |
|
| 16 |
uses |
| 17 |
Classes, Math, Clipper.Core; |
| 18 |
|
| 19 |
type |
| 20 |
//PathType: |
| 21 |
// 1. only subject paths may be open |
| 22 |
// 2. for closed paths, all boolean clipping operations except for |
| 23 |
// Difference are commutative. (In other words, subjects and clips |
| 24 |
// could be swapped and the same solution will be returned.) |
| 25 |
TPathType = (ptSubject, ptClip); |
| 26 |
|
| 27 |
// Vertex: a pre-clipping data structure. It is used to separate polygons |
| 28 |
// into ascending and descending 'bounds' (or sides) that start at local |
| 29 |
// minima and ascend to a local maxima, before descending again. |
| 30 |
|
| 31 |
TVertexFlag = (vfOpenStart, vfOpenEnd, vfLocMax, vfLocMin); |
| 32 |
TVertexFlags = set of TVertexFlag; |
| 33 |
|
| 34 |
PVertex = ^TVertex; |
| 35 |
TVertex = record |
| 36 |
pt : TPoint64; |
| 37 |
next : PVertex; |
| 38 |
prev : PVertex; |
| 39 |
flags : TVertexFlags; |
| 40 |
end; |
| 41 |
|
| 42 |
PPLocalMinima = ^PLocalMinima; |
| 43 |
PLocalMinima = ^TLocalMinima; |
| 44 |
TLocalMinima = record |
| 45 |
vertex : PVertex; |
| 46 |
polytype : TPathType; |
| 47 |
isOpen : Boolean; |
| 48 |
end; |
| 49 |
|
| 50 |
TLocMinList = class(TListEx) |
| 51 |
public |
| 52 |
function Add: PLocalMinima; |
| 53 |
procedure Clear; override; |
| 54 |
end; |
| 55 |
|
| 56 |
TReuseableDataContainer64 = class |
| 57 |
private |
| 58 |
FLocMinList : TLocMinList; |
| 59 |
FVertexArrayList : TList; |
| 60 |
public |
| 61 |
constructor Create; |
| 62 |
destructor Destroy; override; |
| 63 |
procedure Clear; |
| 64 |
procedure AddPaths(const paths: TPaths64; |
| 65 |
pathType: TPathType; isOpen: Boolean); |
| 66 |
end; |
| 67 |
|
| 68 |
// forward declarations |
| 69 |
POutRec = ^TOutRec; |
| 70 |
PHorzSegment = ^THorzSegment; |
| 71 |
PHorzJoin = ^THorzJoin; |
| 72 |
PActive = ^TActive; |
| 73 |
TPolyPathBase = class; |
| 74 |
TPolyTree64 = class; |
| 75 |
TPolyTreeD = class; |
| 76 |
|
| 77 |
// OutPt: vertex data structure for clipping solutions |
| 78 |
POutPt = ^TOutPt; |
| 79 |
TOutPt = record |
| 80 |
pt : TPoint64; |
| 81 |
next : POutPt; |
| 82 |
prev : POutPt; |
| 83 |
outrec : POutRec; |
| 84 |
horz : PHorzSegment; |
| 85 |
end; |
| 86 |
|
| 87 |
TOutRecArray = array of POutRec; |
| 88 |
THorzPosition = (hpBottom, hpMiddle, hpTop); |
| 89 |
|
| 90 |
// OutRec: path data structure for clipping solutions |
| 91 |
TOutRec = record |
| 92 |
idx : Integer; |
| 93 |
owner : POutRec; |
| 94 |
frontE : PActive; |
| 95 |
backE : PActive; |
| 96 |
pts : POutPt; |
| 97 |
polypath : TPolyPathBase; |
| 98 |
splits : TOutRecArray; |
| 99 |
recursiveCheck : POutRec; |
| 100 |
bounds : TRect64; |
| 101 |
path : TPath64; |
| 102 |
isOpen : Boolean; |
| 103 |
end; |
| 104 |
|
| 105 |
TOutRecList = class(TListEx) |
| 106 |
public |
| 107 |
function Add: POutRec; |
| 108 |
procedure Clear; override; |
| 109 |
end; |
| 110 |
|
| 111 |
THorzSegment = record |
| 112 |
leftOp : POutPt; |
| 113 |
rightOp : POutPt; |
| 114 |
leftToRight : Boolean; |
| 115 |
end; |
| 116 |
|
| 117 |
THorzSegList = class(TListEx) |
| 118 |
public |
| 119 |
procedure Clear; override; |
| 120 |
procedure Add(op: POutPt); |
| 121 |
end; |
| 122 |
|
| 123 |
THorzJoin = record |
| 124 |
op1: POutPt; |
| 125 |
op2: POutPt; |
| 126 |
end; |
| 127 |
|
| 128 |
THorzJoinList = class(TListEx) |
| 129 |
public |
| 130 |
procedure Clear; override; |
| 131 |
function Add(op1, op2: POutPt): PHorzJoin; |
| 132 |
end; |
| 133 |
|
| 134 |
|
| 135 |
/////////////////////////////////////////////////////////////////// |
| 136 |
// Important: UP and DOWN here are premised on Y-axis positive down |
| 137 |
// displays, which is the orientation used in Clipper's development. |
| 138 |
/////////////////////////////////////////////////////////////////// |
| 139 |
|
| 140 |
TJoinWith = (jwNone, jwLeft, jwRight); |
| 141 |
|
| 142 |
// Active: represents an edge in the Active Edge Table (Vatti's AET) |
| 143 |
TActive = record |
| 144 |
bot : TPoint64; |
| 145 |
top : TPoint64; |
| 146 |
currX : Int64; // x relative to *top* of current scanbeam |
| 147 |
dx : Double; // inverse of edge slope (zero = vertical) |
| 148 |
windDx : Integer; // wind direction (ascending: +1; descending: -1) |
| 149 |
windCnt : Integer; // current wind count |
| 150 |
windCnt2 : Integer; // current wind count of the opposite TPolyType |
| 151 |
outrec : POutRec; |
| 152 |
// AEL: 'active edge list' (Vatti's AET - active edge table) |
| 153 |
// a linked list of all edges (from left to right) that are present |
| 154 |
// (or 'active') within the current scanbeam (a horizontal 'beam' that |
| 155 |
// sweeps from bottom to top over the paths in the clipping operation). |
| 156 |
prevInAEL : PActive; |
| 157 |
nextInAEL : PActive; |
| 158 |
// SEL: 'sorted edge list' (Vatti's ST - sorted table) |
| 159 |
// linked list used when sorting edges into their new positions at the |
| 160 |
// top of scanbeams, but also (re)used to process horizontals. |
| 161 |
prevInSEL : PActive; |
| 162 |
nextInSEL : PActive; |
| 163 |
jump : PActive; // fast merge sorting (see BuildIntersectList()) |
| 164 |
vertTop : PVertex; |
| 165 |
locMin : PLocalMinima; // the bottom of a 'bound' (also Vatti) |
| 166 |
isLeftB : Boolean; |
| 167 |
joinedWith : TJoinWith; |
| 168 |
end; |
| 169 |
|
| 170 |
// IntersectNode: a structure representing 2 intersecting edges. |
| 171 |
// Intersections must be sorted so they are processed from the largest |
| 172 |
// Y coordinates to the smallest while keeping edges adjacent. |
| 173 |
PPIntersectNode = ^PIntersectNode; |
| 174 |
PIntersectNode = ^TIntersectNode; |
| 175 |
TIntersectNode = record |
| 176 |
active1 : PActive; |
| 177 |
active2 : PActive; |
| 178 |
pt : TPoint64; |
| 179 |
end; |
| 180 |
|
| 181 |
// Scanline: a virtual line representing current position |
| 182 |
// while processing edges using a "sweep line" algorithm. |
| 183 |
PScanLine = ^TScanLine; |
| 184 |
TScanLine = record |
| 185 |
y : Int64; |
| 186 |
next : PScanLine; |
| 187 |
end; |
| 188 |
|
| 189 |
{$IFDEF USINGZ} |
| 190 |
TZCallback64 = procedure (const bot1, top1, bot2, top2: TPoint64; |
| 191 |
var intersectPt: TPoint64) of object; |
| 192 |
TZCallbackD = procedure (const bot1, top1, bot2, top2: TPointD; |
| 193 |
var intersectPt: TPointD) of object; |
| 194 |
{$ENDIF} |
| 195 |
|
| 196 |
|
| 197 |
// ClipperBase: abstract base |
| 198 |
TClipperBase = class |
| 199 |
{$IFDEF STRICT}strict{$ENDIF} private |
| 200 |
FBotY : Int64; |
| 201 |
FScanLine : PScanLine; |
| 202 |
FCurrentLocMinIdx : Integer; |
| 203 |
FClipType : TClipType; |
| 204 |
FFillRule : TFillRule; |
| 205 |
FPreserveCollinear : Boolean; |
| 206 |
FIntersectList : TList; |
| 207 |
FOutRecList : TOutRecList; |
| 208 |
FLocMinList : TLocMinList; |
| 209 |
FHorzSegList : THorzSegList; |
| 210 |
FHorzJoinList : THorzJoinList; |
| 211 |
FVertexArrayList : TList; |
| 212 |
// FActives: see AEL above |
| 213 |
FActives : PActive; |
| 214 |
// FSel: see SEL above. |
| 215 |
// BUT also used to store horz. edges for later processing |
| 216 |
FSel : PActive; |
| 217 |
FHasOpenPaths : Boolean; |
| 218 |
FLocMinListSorted : Boolean; |
| 219 |
FSucceeded : Boolean; |
| 220 |
FReverseSolution : Boolean; |
| 221 |
{$IFDEF USINGZ} |
| 222 |
fDefaultZ : Ztype; |
| 223 |
fZCallback : TZCallback64; |
| 224 |
{$ENDIF} |
| 225 |
procedure Reset; |
| 226 |
procedure InsertScanLine(const Y: Int64); |
| 227 |
function PopScanLine(out Y: Int64): Boolean; |
| 228 |
function PopLocalMinima(Y: Int64; |
| 229 |
out localMinima: PLocalMinima): Boolean; |
| 230 |
procedure DisposeScanLineList; |
| 231 |
procedure DisposeVerticesAndLocalMinima; |
| 232 |
function IsContributingClosed(e: PActive): Boolean; |
| 233 |
function IsContributingOpen(e: PActive): Boolean; |
| 234 |
procedure SetWindCountForClosedPathEdge(e: PActive); |
| 235 |
procedure SetWindCountForOpenPathEdge(e: PActive); |
| 236 |
procedure InsertLocalMinimaIntoAEL(const botY: Int64); |
| 237 |
procedure InsertLeftEdge(e: PActive); |
| 238 |
procedure PushHorz(e: PActive); {$IFDEF INLINING} inline; {$ENDIF} |
| 239 |
function PopHorz(out e: PActive): Boolean; {$IFDEF INLINING} inline; {$ENDIF} |
| 240 |
function StartOpenPath(e: PActive; const pt: TPoint64): POutPt; |
| 241 |
procedure UpdateEdgeIntoAEL(var e: PActive); |
| 242 |
procedure IntersectEdges(e1, e2: PActive; pt: TPoint64); |
| 243 |
procedure DeleteEdges(var e: PActive); |
| 244 |
procedure DeleteFromAEL(e: PActive); |
| 245 |
procedure AdjustCurrXAndCopyToSEL(topY: Int64); |
| 246 |
procedure ConvertHorzSegsToJoins; |
| 247 |
procedure ProcessHorzJoins; |
| 248 |
procedure DoIntersections(const topY: Int64); |
| 249 |
procedure DisposeIntersectNodes; |
| 250 |
procedure AddNewIntersectNode(e1, e2: PActive; topY: Int64); |
| 251 |
function BuildIntersectList(const topY: Int64): Boolean; |
| 252 |
procedure ProcessIntersectList; |
| 253 |
procedure SwapPositionsInAEL(e1, e2: PActive); |
| 254 |
function AddOutPt(e: PActive; const pt: TPoint64): POutPt; |
| 255 |
procedure UndoJoin(e: PActive; const currPt: TPoint64); |
| 256 |
procedure CheckJoinLeft(e: PActive; |
| 257 |
const pt: TPoint64; checkCurrX: Boolean = false); |
| 258 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 259 |
procedure CheckJoinRight(e: PActive; |
| 260 |
const pt: TPoint64; checkCurrX: Boolean = false); |
| 261 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 262 |
function AddLocalMinPoly(e1, e2: PActive; |
| 263 |
const pt: TPoint64; IsNew: Boolean = false): POutPt; |
| 264 |
function AddLocalMaxPoly(e1, e2: PActive; const pt: TPoint64): POutPt; |
| 265 |
procedure JoinOutrecPaths(e1, e2: PActive); |
| 266 |
function DoMaxima(e: PActive): PActive; |
| 267 |
procedure DoHorizontal(horzEdge: PActive); |
| 268 |
procedure DoTopOfScanbeam(Y: Int64); |
| 269 |
procedure CleanCollinear(outRec: POutRec); |
| 270 |
procedure DoSplitOp(outrec: POutRec; splitOp: POutPt); |
| 271 |
procedure FixSelfIntersects(outrec: POutRec); |
| 272 |
function CheckBounds(outrec: POutRec): Boolean; |
| 273 |
function CheckSplitOwner(outrec: POutRec; const splits: TOutRecArray): Boolean; |
| 274 |
procedure RecursiveCheckOwners(outrec: POutRec; polytree: TPolyPathBase); |
| 275 |
protected |
| 276 |
FUsingPolytree : Boolean; |
| 277 |
procedure AddPath(const path: TPath64; |
| 278 |
pathType: TPathType; isOpen: Boolean); |
| 279 |
procedure AddPaths(const paths: TPaths64; |
| 280 |
pathType: TPathType; isOpen: Boolean); |
| 281 |
procedure AddReuseableData(const reuseableData: TReuseableDataContainer64); |
| 282 |
function ClearSolutionOnly: Boolean; |
| 283 |
procedure ExecuteInternal(clipType: TClipType; |
| 284 |
fillRule: TFillRule; usingPolytree: Boolean); |
| 285 |
function BuildPaths(var closedPaths, openPaths: TPaths64): Boolean; |
| 286 |
function BuildTree(polytree: TPolyPathBase; out openPaths: TPaths64): Boolean; |
| 287 |
{$IFDEF USINGZ} |
| 288 |
procedure SetZ( e1, e2: PActive; var intersectPt: TPoint64); |
| 289 |
property ZCallback : TZCallback64 read fZCallback write fZCallback; |
| 290 |
property DefaultZ : Ztype read fDefaultZ write fDefaultZ; |
| 291 |
{$ENDIF} |
| 292 |
property Succeeded : Boolean read FSucceeded; |
| 293 |
public |
| 294 |
constructor Create; virtual; |
| 295 |
destructor Destroy; override; |
| 296 |
procedure Clear; |
| 297 |
function GetBounds: TRect64; |
| 298 |
property PreserveCollinear: Boolean read |
| 299 |
FPreserveCollinear write FPreserveCollinear; |
| 300 |
property ReverseSolution: Boolean read |
| 301 |
FReverseSolution write FReverseSolution; |
| 302 |
end; |
| 303 |
|
| 304 |
TClipper64 = class(TClipperBase) // for integer coordinates |
| 305 |
public |
| 306 |
procedure AddReuseableData(const reuseableData: TReuseableDataContainer64); |
| 307 |
procedure AddSubject(const subject: TPath64); overload; |
| 308 |
procedure AddSubject(const subjects: TPaths64); overload; |
| 309 |
procedure AddOpenSubject(const subject: TPath64); overload; |
| 310 |
procedure AddOpenSubject(const subjects: TPaths64); overload; |
| 311 |
procedure AddClip(const clip: TPath64); overload; |
| 312 |
procedure AddClip(const clips: TPaths64); overload; |
| 313 |
function Execute(clipType: TClipType; fillRule: TFillRule; |
| 314 |
out closedSolutions: TPaths64): Boolean; overload; virtual; |
| 315 |
function Execute(clipType: TClipType; fillRule: TFillRule; |
| 316 |
out closedSolutions, openSolutions: TPaths64): Boolean; overload; virtual; |
| 317 |
function Execute(clipType: TClipType; fillRule: TFillRule; |
| 318 |
var solutionTree: TPolyTree64; out openSolutions: TPaths64): Boolean; overload; virtual; |
| 319 |
{$IFDEF USINGZ} |
| 320 |
property ZCallback; |
| 321 |
{$ENDIF} |
| 322 |
end; |
| 323 |
|
| 324 |
// PolyPathBase: ancestor of TPolyPath and TPolyPathD |
| 325 |
TPolyPathBase = class |
| 326 |
{$IFDEF STRICT}strict{$ENDIF} private |
| 327 |
FParent : TPolyPathBase; |
| 328 |
FChildList : TList; |
| 329 |
function GetChildCnt: Integer; |
| 330 |
function GetIsHole: Boolean; |
| 331 |
function GetLevel: Integer; |
| 332 |
protected |
| 333 |
function GetChild(index: Integer): TPolyPathBase; |
| 334 |
function AddChild(const path: TPath64): TPolyPathBase; virtual; abstract; |
| 335 |
property ChildList: TList read FChildList; |
| 336 |
property Parent: TPolyPathBase read FParent write FParent; |
| 337 |
public |
| 338 |
constructor Create; virtual; |
| 339 |
destructor Destroy; override; |
| 340 |
procedure Clear; virtual; |
| 341 |
property IsHole: Boolean read GetIsHole; |
| 342 |
property Count: Integer read GetChildCnt; |
| 343 |
property Child[index: Integer]: TPolyPathBase read GetChild; default; |
| 344 |
property Level: Integer read GetLevel; |
| 345 |
end; |
| 346 |
|
| 347 |
TPolyPath64 = class(TPolyPathBase) |
| 348 |
{$IFDEF STRICT}strict{$ENDIF} private |
| 349 |
FPath : TPath64; |
| 350 |
function GetChild64(index: Integer): TPolyPath64; |
| 351 |
public |
| 352 |
function AddChild(const path: TPath64): TPolyPathBase; override; |
| 353 |
property Child[index: Integer]: TPolyPath64 read GetChild64; default; |
| 354 |
property Polygon: TPath64 read FPath; |
| 355 |
end; |
| 356 |
|
| 357 |
// PolyTree: is intended as a READ-ONLY data structure to receive closed path |
| 358 |
// solutions to clipping operations. While this structure is more complex than |
| 359 |
// the alternative TPaths structure, it does model path ownership (ie paths |
| 360 |
// that are contained by other paths). This will be useful to some users. |
| 361 |
TPolyTree64 = class(TPolyPath64); |
| 362 |
|
| 363 |
// FLOATING POINT POLYGON COORDINATES (D suffix to indicate double precision) |
| 364 |
// To preserve numerical robustness, clipping must be done using integer |
| 365 |
// coordinates. Consequently, polygons that are defined with floating point |
| 366 |
// coordinates will need these converted into integer values together with |
| 367 |
// scaling to achieve the desired floating point precision. |
| 368 |
|
| 369 |
TClipperD = class(TClipperBase) // for floating point coordinates |
| 370 |
{$IFDEF STRICT}strict{$ENDIF} private |
| 371 |
FScale: double; |
| 372 |
FInvScale: double; |
| 373 |
{$IFDEF USINGZ} |
| 374 |
fZCallback : TZCallbackD; |
| 375 |
procedure ZCB(const bot1, top1, bot2, top2: TPoint64; var intersectPt: TPoint64); |
| 376 |
procedure CheckCallback; |
| 377 |
{$ENDIF} |
| 378 |
public |
| 379 |
procedure AddSubject(const pathD: TPathD); overload; |
| 380 |
procedure AddSubject(const pathsD: TPathsD); overload; |
| 381 |
procedure AddOpenSubject(const pathD: TPathD); overload; |
| 382 |
procedure AddOpenSubject(const pathsD: TPathsD); overload; |
| 383 |
procedure AddClip(const pathD: TPathD); overload; |
| 384 |
procedure AddClip(const pathsD: TPathsD); overload; |
| 385 |
constructor Create(precision: integer = 2); |
| 386 |
reintroduce; overload; |
| 387 |
function Execute(clipType: TClipType; fillRule: TFillRule; |
| 388 |
out closedSolutions: TPathsD): Boolean; overload; |
| 389 |
function Execute(clipType: TClipType; fillRule: TFillRule; |
| 390 |
out closedSolutions, openSolutions: TPathsD): Boolean; overload; |
| 391 |
function Execute(clipType: TClipType; fillRule: TFillRule; |
| 392 |
var solutionsTree: TPolyTreeD; out openSolutions: TPathsD): Boolean; overload; |
| 393 |
{$IFDEF USINGZ} |
| 394 |
property ZCallback : TZCallbackD read fZCallback write fZCallback; |
| 395 |
{$ENDIF} |
| 396 |
end; |
| 397 |
|
| 398 |
TPolyPathD = class(TPolyPathBase) |
| 399 |
{$IFDEF STRICT}strict{$ENDIF} private |
| 400 |
FPath : TPathD; |
| 401 |
function GetChildD(index: Integer): TPolyPathD; |
| 402 |
protected |
| 403 |
FScale : double; |
| 404 |
public |
| 405 |
function AddChild(const path: TPath64): TPolyPathBase; overload; override; |
| 406 |
function AddChild(const path: TPathD): TPolyPathBase; reintroduce; overload; |
| 407 |
property Polygon: TPathD read FPath; |
| 408 |
property Child[index: Integer]: TPolyPathD read GetChildD; default; |
| 409 |
end; |
| 410 |
|
| 411 |
TPolyTreeD = class(TPolyPathD) |
| 412 |
protected |
| 413 |
procedure SetScale(value: double); // alternative to friend class |
| 414 |
public |
| 415 |
property Scale: double read FScale; |
| 416 |
end; |
| 417 |
|
| 418 |
resourcestring |
| 419 |
rsClipper_PolyTreeErr = 'The TPolyTree parameter must be assigned.'; |
| 420 |
rsClipper_ClippingErr = 'Undefined clipping error'; |
| 421 |
|
| 422 |
implementation |
| 423 |
|
| 424 |
//OVERFLOWCHECKS OFF is a necessary workaround for a compiler bug that very |
| 425 |
//occasionally reports incorrect overflow errors in Delphi versions before 10.2. |
| 426 |
//see https://forums.embarcadero.com/message.jspa?messageID=871444 |
| 427 |
{$OVERFLOWCHECKS OFF} |
| 428 |
|
| 429 |
const |
| 430 |
DefaultClipperDScale = 100; |
| 431 |
|
| 432 |
//------------------------------------------------------------------------------ |
| 433 |
// TLocMinList class |
| 434 |
//------------------------------------------------------------------------------ |
| 435 |
|
| 436 |
function TLocMinList.Add: PLocalMinima; |
| 437 |
begin |
| 438 |
new(Result); |
| 439 |
inherited Add(Result); |
| 440 |
end; |
| 441 |
//------------------------------------------------------------------------------ |
| 442 |
|
| 443 |
procedure TLocMinList.Clear; |
| 444 |
var |
| 445 |
i: integer; |
| 446 |
begin |
| 447 |
for i := 0 to Count -1 do |
| 448 |
Dispose(PLocalMinima(UnsafeGet(i))); |
| 449 |
inherited; |
| 450 |
end; |
| 451 |
|
| 452 |
//------------------------------------------------------------------------------ |
| 453 |
// TOutRecList class |
| 454 |
//------------------------------------------------------------------------------ |
| 455 |
|
| 456 |
function TOutRecList.Add: POutRec; |
| 457 |
begin |
| 458 |
new(Result); |
| 459 |
FillChar(Result^, SizeOf(TOutRec), 0); |
| 460 |
Result.idx := inherited Add(Result); |
| 461 |
end; |
| 462 |
//------------------------------------------------------------------------------ |
| 463 |
|
| 464 |
procedure TOutRecList.Clear; |
| 465 |
var |
| 466 |
i: integer; |
| 467 |
por: POutRec; |
| 468 |
op, tmpPp: POutPt; |
| 469 |
begin |
| 470 |
for i := 0 to Count -1 do |
| 471 |
begin |
| 472 |
por := UnsafeGet(i); |
| 473 |
if Assigned(por.pts) then |
| 474 |
begin |
| 475 |
op := por.pts; |
| 476 |
op.prev.next := nil; |
| 477 |
while Assigned(op) do |
| 478 |
begin |
| 479 |
tmpPp := op; |
| 480 |
op := op.next; |
| 481 |
Dispose(tmpPp); |
| 482 |
end; |
| 483 |
end; |
| 484 |
Dispose(por); |
| 485 |
end; |
| 486 |
inherited; |
| 487 |
end; |
| 488 |
|
| 489 |
//------------------------------------------------------------------------------ |
| 490 |
// THorzSegList |
| 491 |
//------------------------------------------------------------------------------ |
| 492 |
|
| 493 |
procedure THorzSegList.Clear; |
| 494 |
var |
| 495 |
i: integer; |
| 496 |
begin |
| 497 |
for i := 0 to Count -1 do |
| 498 |
Dispose(PHorzSegment(UnsafeGet(i))); |
| 499 |
inherited; |
| 500 |
end; |
| 501 |
//------------------------------------------------------------------------------ |
| 502 |
|
| 503 |
procedure THorzSegList.Add(op: POutPt); |
| 504 |
var |
| 505 |
hs: PHorzSegment; |
| 506 |
begin |
| 507 |
if (op.outrec.isOpen) then Exit; |
| 508 |
new(hs); |
| 509 |
hs.leftOp := op; |
| 510 |
op.horz := nil; |
| 511 |
inherited Add(hs); |
| 512 |
end; |
| 513 |
|
| 514 |
//------------------------------------------------------------------------------ |
| 515 |
// THorzJoinList |
| 516 |
//------------------------------------------------------------------------------ |
| 517 |
|
| 518 |
procedure THorzJoinList.Clear; |
| 519 |
var |
| 520 |
i: integer; |
| 521 |
begin |
| 522 |
for i := 0 to Count -1 do |
| 523 |
Dispose(PHorzJoin(UnsafeGet(i))); |
| 524 |
inherited; |
| 525 |
end; |
| 526 |
//------------------------------------------------------------------------------ |
| 527 |
|
| 528 |
function THorzJoinList.Add(op1, op2: POutPt): PHorzJoin; |
| 529 |
begin |
| 530 |
new(Result); |
| 531 |
Result.op1 := op1; |
| 532 |
Result.op2 := op2; |
| 533 |
inherited Add(Result); |
| 534 |
end; |
| 535 |
|
| 536 |
//------------------------------------------------------------------------------ |
| 537 |
// Miscellaneous Functions ... |
| 538 |
//------------------------------------------------------------------------------ |
| 539 |
|
| 540 |
function UnsafeGet(List: TList; Index: Integer): Pointer; |
| 541 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 542 |
begin |
| 543 |
Result := List.List[Index]; |
| 544 |
end; |
| 545 |
//------------------------------------------------------------------------------ |
| 546 |
|
| 547 |
function IsOpen(e: PActive): Boolean; overload; {$IFDEF INLINING} inline; {$ENDIF} |
| 548 |
begin |
| 549 |
Result := e.locMin.isOpen; |
| 550 |
end; |
| 551 |
//------------------------------------------------------------------------------ |
| 552 |
|
| 553 |
function IsOpenEnd(v: PVertex): Boolean; overload; {$IFDEF INLINING} inline; {$ENDIF} |
| 554 |
begin |
| 555 |
Result := (v.flags * [vfOpenStart, vfOpenEnd] <> []); |
| 556 |
end; |
| 557 |
//------------------------------------------------------------------------------ |
| 558 |
|
| 559 |
function IsHotEdge(e: PActive): Boolean; {$IFDEF INLINING} inline; {$ENDIF} |
| 560 |
begin |
| 561 |
Result := assigned(e.outrec); |
| 562 |
end; |
| 563 |
//------------------------------------------------------------------------------ |
| 564 |
|
| 565 |
function GetPrevHotEdge(e: PActive): PActive; {$IFDEF INLINING} inline; {$ENDIF} |
| 566 |
begin |
| 567 |
Result := e.prevInAEL; |
| 568 |
while assigned(Result) and (IsOpen(Result) or not IsHotEdge(Result)) do |
| 569 |
Result := Result.prevInAEL; |
| 570 |
end; |
| 571 |
//------------------------------------------------------------------------------ |
| 572 |
|
| 573 |
function IsFront(e: PActive): Boolean; {$IFDEF INLINING} inline; {$ENDIF} |
| 574 |
begin |
| 575 |
Result := (e = e.outrec.frontE); |
| 576 |
end; |
| 577 |
//------------------------------------------------------------------------------ |
| 578 |
|
| 579 |
function NewOutPt(const pt: TPoint64; |
| 580 |
outrec: POutRec; prev, next: POutPt): POutPt; overload; |
| 581 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 582 |
begin |
| 583 |
new(Result); |
| 584 |
Result.pt := pt; |
| 585 |
Result.next := next; |
| 586 |
Result.prev := prev; |
| 587 |
Result.outrec := outrec; |
| 588 |
Result.horz := nil; |
| 589 |
end; |
| 590 |
//------------------------------------------------------------------------------ |
| 591 |
|
| 592 |
function NewOutPt(const pt: TPoint64; outrec: POutRec): POutPt; overload; |
| 593 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 594 |
begin |
| 595 |
new(Result); |
| 596 |
Result.pt := pt; |
| 597 |
Result.next := Result; |
| 598 |
Result.prev := Result; |
| 599 |
Result.outrec := outrec; |
| 600 |
Result.horz := nil; |
| 601 |
end; |
| 602 |
//------------------------------------------------------------------------------ |
| 603 |
|
| 604 |
function DuplicateOp(op:POutPt; insertNext: Boolean): POutPt; |
| 605 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 606 |
begin |
| 607 |
new(Result); |
| 608 |
Result.pt := op.pt; |
| 609 |
Result.outrec := op.outrec; |
| 610 |
Result.horz := nil; |
| 611 |
if insertNext then |
| 612 |
begin |
| 613 |
Result.next := op.next; |
| 614 |
Result.next.prev := Result; |
| 615 |
Result.prev := op; |
| 616 |
op.next := Result; |
| 617 |
end else |
| 618 |
begin |
| 619 |
Result.prev := op.prev; |
| 620 |
Result.prev.next := Result; |
| 621 |
Result.next := op; |
| 622 |
op.prev := Result; |
| 623 |
end; |
| 624 |
end; |
| 625 |
//------------------------------------------------------------------------------ |
| 626 |
|
| 627 |
function GetRealOutRec(outRec: POutRec): POutRec; |
| 628 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 629 |
begin |
| 630 |
Result := outRec; |
| 631 |
while Assigned(Result) and not Assigned(Result.pts) do |
| 632 |
Result := Result.owner; |
| 633 |
end; |
| 634 |
//------------------------------------------------------------------------------ |
| 635 |
|
| 636 |
function IsValidOwner(outRec, TestOwner: POutRec): Boolean; |
| 637 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 638 |
begin |
| 639 |
while Assigned(TestOwner) and (outrec <> TestOwner) do |
| 640 |
TestOwner := TestOwner.owner; |
| 641 |
Result := not Assigned(TestOwner); |
| 642 |
end; |
| 643 |
//------------------------------------------------------------------------------ |
| 644 |
|
| 645 |
function PtsReallyClose(const pt1, pt2: TPoint64): Boolean; |
| 646 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 647 |
begin |
| 648 |
Result := (abs(pt1.X - pt2.X) < 2) and (abs(pt1.Y - pt2.Y) < 2); |
| 649 |
end; |
| 650 |
//------------------------------------------------------------------------------ |
| 651 |
|
| 652 |
function IsVerySmallTriangle(op: POutPt): Boolean; |
| 653 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 654 |
begin |
| 655 |
//also treat inconsequential polygons as invalid |
| 656 |
Result := (op.next.next = op.prev) and |
| 657 |
(PtsReallyClose(op.prev.pt, op.next.pt) or |
| 658 |
PtsReallyClose(op.pt, op.next.pt) or |
| 659 |
PtsReallyClose(op.pt, op.prev.pt)); |
| 660 |
end; |
| 661 |
//------------------------------------------------------------------------------ |
| 662 |
|
| 663 |
function IsValidClosedPath(op: POutPt): Boolean; {$IFDEF INLINING} inline; {$ENDIF} |
| 664 |
begin |
| 665 |
result := assigned(op) and (op.next <> op) and |
| 666 |
(op.next <> op.prev) and not IsVerySmallTriangle(op); |
| 667 |
end; |
| 668 |
//------------------------------------------------------------------------------ |
| 669 |
|
| 670 |
(******************************************************************************* |
| 671 |
* Dx: 0(90deg) * |
| 672 |
* | * |
| 673 |
* +inf (180deg) <--- o ---> -inf (0deg) * |
| 674 |
*******************************************************************************) |
| 675 |
|
| 676 |
function GetDx(const pt1, pt2: TPoint64): double; |
| 677 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 678 |
var |
| 679 |
dy: Int64; |
| 680 |
begin |
| 681 |
dy := (pt2.Y - pt1.Y); |
| 682 |
if dy <> 0 then result := (pt2.X - pt1.X) / dy |
| 683 |
else if (pt2.X > pt1.X) then result := NegInfinity |
| 684 |
else result := Infinity; |
| 685 |
end; |
| 686 |
//------------------------------------------------------------------------------ |
| 687 |
|
| 688 |
function TopX(e: PActive; const currentY: Int64): Int64; overload; |
| 689 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 690 |
begin |
| 691 |
if (currentY = e.top.Y) or (e.top.X = e.bot.X) then Result := e.top.X |
| 692 |
else if (currentY = e.bot.Y) then Result := e.bot.X |
| 693 |
else Result := e.bot.X + Round(e.dx*(currentY - e.bot.Y)); |
| 694 |
end; |
| 695 |
//------------------------------------------------------------------------------ |
| 696 |
|
| 697 |
function IsHorizontal(e: PActive): Boolean; {$IFDEF INLINING} inline; {$ENDIF} |
| 698 |
begin |
| 699 |
Result := (e.top.Y = e.bot.Y); |
| 700 |
end; |
| 701 |
//------------------------------------------------------------------------------ |
| 702 |
|
| 703 |
function IsHeadingRightHorz(e: PActive): Boolean; {$IFDEF INLINING} inline; {$ENDIF} |
| 704 |
begin |
| 705 |
Result := (e.dx = NegInfinity); |
| 706 |
end; |
| 707 |
//------------------------------------------------------------------------------ |
| 708 |
|
| 709 |
function IsHeadingLeftHorz(e: PActive): Boolean; {$IFDEF INLINING} inline; {$ENDIF} |
| 710 |
begin |
| 711 |
Result := (e.dx = Infinity); |
| 712 |
end; |
| 713 |
//------------------------------------------------------------------------------ |
| 714 |
|
| 715 |
function IsJoined(e: PActive): Boolean; {$IFDEF INLINING} inline; {$ENDIF} |
| 716 |
begin |
| 717 |
Result := e.joinedWith <> jwNone; |
| 718 |
end; |
| 719 |
//------------------------------------------------------------------------------ |
| 720 |
|
| 721 |
procedure SwapActives(var e1, e2: PActive); {$IFDEF INLINING} inline; {$ENDIF} |
| 722 |
var |
| 723 |
e: PActive; |
| 724 |
begin |
| 725 |
e := e1; e1 := e2; e2 := e; |
| 726 |
end; |
| 727 |
//------------------------------------------------------------------------------ |
| 728 |
|
| 729 |
function GetPolyType(const e: PActive): TPathType; |
| 730 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 731 |
begin |
| 732 |
Result := e.locMin.polytype; |
| 733 |
end; |
| 734 |
//------------------------------------------------------------------------------ |
| 735 |
|
| 736 |
function IsSamePolyType(const e1, e2: PActive): Boolean; |
| 737 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 738 |
begin |
| 739 |
Result := e1.locMin.polytype = e2.locMin.polytype; |
| 740 |
end; |
| 741 |
//------------------------------------------------------------------------------ |
| 742 |
|
| 743 |
procedure SetDx(e: PActive); {$IFDEF INLINING} inline; {$ENDIF} |
| 744 |
begin |
| 745 |
e.dx := GetDx(e.bot, e.top); |
| 746 |
end; |
| 747 |
//------------------------------------------------------------------------------ |
| 748 |
|
| 749 |
function IsLeftBound(e: PActive): Boolean; {$IFDEF INLINING} inline; {$ENDIF} |
| 750 |
begin |
| 751 |
Result := e.isLeftB; |
| 752 |
end; |
| 753 |
//------------------------------------------------------------------------------ |
| 754 |
|
| 755 |
function NextVertex(e: PActive): PVertex; // ie heading (inverted Y-axis) "up" |
| 756 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 757 |
begin |
| 758 |
if e.windDx > 0 then |
| 759 |
Result := e.vertTop.next else |
| 760 |
Result := e.vertTop.prev; |
| 761 |
end; |
| 762 |
//------------------------------------------------------------------------------ |
| 763 |
|
| 764 |
//PrevPrevVertex: useful to get the (inverted Y-axis) top of the |
| 765 |
//alternate edge (ie left or right bound) during edge insertion. |
| 766 |
function PrevPrevVertex(e: PActive): PVertex; |
| 767 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 768 |
begin |
| 769 |
if e.windDx > 0 then |
| 770 |
Result := e.vertTop.prev.prev else |
| 771 |
Result := e.vertTop.next.next; |
| 772 |
end; |
| 773 |
//------------------------------------------------------------------------------ |
| 774 |
|
| 775 |
function IsMaxima(vertex: PVertex): Boolean; overload; |
| 776 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 777 |
begin |
| 778 |
Result := vfLocMax in vertex.flags; |
| 779 |
end; |
| 780 |
//------------------------------------------------------------------------------ |
| 781 |
|
| 782 |
function IsMaxima(e: PActive): Boolean; overload; |
| 783 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 784 |
begin |
| 785 |
Result := vfLocMax in e.vertTop.flags; |
| 786 |
end; |
| 787 |
//------------------------------------------------------------------------------ |
| 788 |
|
| 789 |
function GetCurrYMaximaVertexOpen(e: PActive): PVertex; |
| 790 |
begin |
| 791 |
Result := e.vertTop; |
| 792 |
if e.windDx > 0 then |
| 793 |
while (Result.next.pt.Y = Result.pt.Y) and |
| 794 |
(Result.flags * [vfOpenEnd, vfLocMax] = []) do |
| 795 |
Result := Result.next |
| 796 |
else |
| 797 |
while (Result.prev.pt.Y = Result.pt.Y) and |
| 798 |
(Result.flags * [vfOpenEnd, vfLocMax] = []) do |
| 799 |
Result := Result.prev; |
| 800 |
if not IsMaxima(Result) then Result := nil; // not a maxima |
| 801 |
end; |
| 802 |
//------------------------------------------------------------------------------ |
| 803 |
|
| 804 |
function GetCurrYMaximaVertex(e: PActive): PVertex; |
| 805 |
begin |
| 806 |
// nb: function not safe with open paths |
| 807 |
Result := e.vertTop; |
| 808 |
if e.windDx > 0 then |
| 809 |
while Result.next.pt.Y = Result.pt.Y do Result := Result.next |
| 810 |
else |
| 811 |
while Result.prev.pt.Y = Result.pt.Y do Result := Result.prev; |
| 812 |
if not IsMaxima(Result) then Result := nil; // not a maxima |
| 813 |
end; |
| 814 |
//------------------------------------------------------------------------------ |
| 815 |
|
| 816 |
function GetMaximaPair(e: PActive): PActive; |
| 817 |
begin |
| 818 |
Result := e.nextInAEL; |
| 819 |
while assigned(Result) do |
| 820 |
begin |
| 821 |
if Result.vertTop = e.vertTop then Exit; // Found! |
| 822 |
Result := Result.nextInAEL; |
| 823 |
end; |
| 824 |
Result := nil; |
| 825 |
end; |
| 826 |
//------------------------------------------------------------------------------ |
| 827 |
|
| 828 |
function PointCount(pts: POutPt): Integer; {$IFDEF INLINING} inline; {$ENDIF} |
| 829 |
var |
| 830 |
p: POutPt; |
| 831 |
begin |
| 832 |
Result := 0; |
| 833 |
if not Assigned(pts) then Exit; |
| 834 |
p := pts; |
| 835 |
repeat |
| 836 |
Inc(Result); |
| 837 |
p := p.next; |
| 838 |
until p = pts; |
| 839 |
end; |
| 840 |
//------------------------------------------------------------------------------ |
| 841 |
|
| 842 |
function GetCleanPath(op: POutPt): TPath64; |
| 843 |
var |
| 844 |
cnt: integer; |
| 845 |
op2, prevOp: POutPt; |
| 846 |
begin |
| 847 |
cnt := 0; |
| 848 |
SetLength(Result, PointCount(op)); |
| 849 |
op2 := op; |
| 850 |
while ((op2.next <> op) and |
| 851 |
(((op2.pt.X = op2.next.pt.X) and (op2.pt.X = op2.prev.pt.X)) or |
| 852 |
((op2.pt.Y = op2.next.pt.Y) and (op2.pt.Y = op2.prev.pt.Y)))) do |
| 853 |
op2 := op2.next; |
| 854 |
result[cnt] := op2.pt; |
| 855 |
inc(cnt); |
| 856 |
prevOp := op2; |
| 857 |
op2 := op2.next; |
| 858 |
while (op2 <> op) do |
| 859 |
begin |
| 860 |
if (((op2.pt.X <> op2.next.pt.X) or (op2.pt.X <> prevOp.pt.X)) and |
| 861 |
((op2.pt.Y <> op2.next.pt.Y) or (op2.pt.Y <> prevOp.pt.Y))) then |
| 862 |
begin |
| 863 |
result[cnt] := op2.pt; |
| 864 |
inc(cnt); |
| 865 |
prevOp := op2; |
| 866 |
end; |
| 867 |
op2 := op2.next; |
| 868 |
end; |
| 869 |
SetLength(Result, cnt); |
| 870 |
end; |
| 871 |
|
| 872 |
function PointInOpPolygon(const pt: TPoint64; op: POutPt): TPointInPolygonResult; |
| 873 |
var |
| 874 |
val: Integer; |
| 875 |
op2: POutPt; |
| 876 |
isAbove, startingAbove: Boolean; |
| 877 |
d: double; // avoids integer overflow |
| 878 |
begin |
| 879 |
result := pipOutside; |
| 880 |
if (op = op.next) or (op.prev = op.next) then Exit; |
| 881 |
|
| 882 |
op2 := op; |
| 883 |
repeat |
| 884 |
if (op.pt.Y <> pt.Y) then break; |
| 885 |
op := op.next; |
| 886 |
until op = op2; |
| 887 |
if (op.pt.Y = pt.Y) then Exit; // not a proper polygon |
| 888 |
|
| 889 |
isAbove := op.pt.Y < pt.Y; |
| 890 |
startingAbove := isAbove; |
| 891 |
Result := pipOn; |
| 892 |
val := 0; |
| 893 |
op2 := op.next; |
| 894 |
while (op2 <> op) do |
| 895 |
begin |
| 896 |
if isAbove then |
| 897 |
while (op2 <> op) and (op2.pt.Y < pt.Y) do op2 := op2.next |
| 898 |
else |
| 899 |
while (op2 <> op) and (op2.pt.Y > pt.Y) do op2 := op2.next; |
| 900 |
if (op2 = op) then break; |
| 901 |
|
| 902 |
// must have touched or crossed the pt.Y horizonal |
| 903 |
// and this must happen an even number of times |
| 904 |
|
| 905 |
if (op2.pt.Y = pt.Y) then // touching the horizontal |
| 906 |
begin |
| 907 |
if (op2.pt.X = pt.X) or ((op2.pt.Y = op2.prev.pt.Y) and |
| 908 |
((pt.X < op2.prev.pt.X) <> (pt.X < op2.pt.X))) then Exit; |
| 909 |
op2 := op2.next; |
| 910 |
if (op2 = op) then break; |
| 911 |
Continue; |
| 912 |
end; |
| 913 |
|
| 914 |
if (pt.X < op2.pt.X) and (pt.X < op2.prev.pt.X) then |
| 915 |
// do nothing because |
| 916 |
// we're only interested in edges crossing on the left |
| 917 |
else if((pt.X > op2.prev.pt.X) and (pt.X > op2.pt.X)) then |
| 918 |
val := 1 - val // toggle val |
| 919 |
else |
| 920 |
begin |
| 921 |
d := CrossProduct(op2.prev.pt, op2.pt, pt); |
| 922 |
if d = 0 then Exit; // ie point on path |
| 923 |
if (d < 0) = isAbove then val := 1 - val; |
| 924 |
end; |
| 925 |
isAbove := not isAbove; |
| 926 |
op2 := op2.next; |
| 927 |
end; |
| 928 |
|
| 929 |
if (isAbove <> startingAbove) then |
| 930 |
begin |
| 931 |
d := CrossProduct(op2.prev.pt, op2.pt, pt); |
| 932 |
if d = 0 then Exit; // ie point on path |
| 933 |
if (d < 0) = isAbove then val := 1 - val; |
| 934 |
end; |
| 935 |
|
| 936 |
if val = 0 then |
| 937 |
result := pipOutside else |
| 938 |
result := pipInside; |
| 939 |
end; |
| 940 |
//------------------------------------------------------------------------------ |
| 941 |
|
| 942 |
function Path1InsidePath2(const op1, op2: POutPt): Boolean; |
| 943 |
var |
| 944 |
op: POutPt; |
| 945 |
mp: TPoint64; |
| 946 |
path: TPath64; |
| 947 |
pipResult: TPointInPolygonResult; |
| 948 |
outsideCnt: integer; |
| 949 |
begin |
| 950 |
// precondition - the twi paths or1 & pr2 don't intersect |
| 951 |
// we need to make some accommodation for rounding errors |
| 952 |
// so we won't jump if the first vertex is found outside |
| 953 |
outsideCnt := 0; |
| 954 |
op := op1; |
| 955 |
repeat |
| 956 |
pipResult := PointInOpPolygon(op.pt, op2); |
| 957 |
if pipResult = pipOutside then inc(outsideCnt) |
| 958 |
else if pipResult = pipInside then dec(outsideCnt); |
| 959 |
op := op.next; |
| 960 |
until (op = op1) or (Abs(outsideCnt) = 2); |
| 961 |
if (Abs(outsideCnt) < 2) then |
| 962 |
begin |
| 963 |
// if path1's location is still equivocal then check its midpoint |
| 964 |
path := GetCleanPath(op1); |
| 965 |
mp := Clipper.Core.GetBounds(path).MidPoint; |
| 966 |
path := GetCleanPath(op2); |
| 967 |
Result := PointInPolygon(mp, path) <> pipOutside; |
| 968 |
end |
| 969 |
else |
| 970 |
Result := (outsideCnt < 0); |
| 971 |
end; |
| 972 |
//------------------------------------------------------------------------------ |
| 973 |
|
| 974 |
procedure UncoupleOutRec(e: PActive); |
| 975 |
var |
| 976 |
outRec: POutRec; |
| 977 |
begin |
| 978 |
if not Assigned(e.outrec) then Exit; |
| 979 |
outRec := e.outrec; |
| 980 |
outRec.frontE.outrec := nil; |
| 981 |
outRec.backE.outrec := nil; |
| 982 |
outRec.frontE := nil; |
| 983 |
outRec.backE := nil; |
| 984 |
end; |
| 985 |
//------------------------------------------------------------------------------ |
| 986 |
|
| 987 |
procedure AddPathsToVertexList(const paths: TPaths64; |
| 988 |
polyType: TPathType; isOpen: Boolean; |
| 989 |
vertexList: TList; LocMinList: TLocMinList); |
| 990 |
var |
| 991 |
i, j, len, totalVerts: integer; |
| 992 |
p: PPoint64; |
| 993 |
v, va0, vaCurr, vaPrev: PVertex; |
| 994 |
ascending, ascending0: Boolean; |
| 995 |
|
| 996 |
procedure AddLocMin(vert: PVertex); |
| 997 |
var |
| 998 |
lm: PLocalMinima; |
| 999 |
begin |
| 1000 |
if vfLocMin in vert.flags then Exit; // ie already added |
| 1001 |
Include(vert.flags, vfLocMin); |
| 1002 |
lm := LocMinList.Add; |
| 1003 |
lm.vertex := vert; |
| 1004 |
lm.polytype := polyType; |
| 1005 |
lm.isOpen := isOpen; |
| 1006 |
end; |
| 1007 |
//--------------------------------------------------------- |
| 1008 |
|
| 1009 |
begin |
| 1010 |
// count the total (maximum) number of vertices required |
| 1011 |
totalVerts := 0; |
| 1012 |
for i := 0 to High(paths) do |
| 1013 |
totalVerts := totalVerts + Length(paths[i]); |
| 1014 |
if (totalVerts = 0) then Exit; |
| 1015 |
// allocate memory |
| 1016 |
GetMem(v, sizeof(TVertex) * totalVerts); |
| 1017 |
vertexList.Add(v); |
| 1018 |
|
| 1019 |
{$IF not defined(FPC) and (CompilerVersion <= 26.0)} |
| 1020 |
// Delphi 7-XE5 have a problem with "continue" and the |
| 1021 |
// code analysis, marking "ascending" as "not initialized" |
| 1022 |
ascending := False; |
| 1023 |
{$IFEND} |
| 1024 |
for i := 0 to High(paths) do |
| 1025 |
begin |
| 1026 |
len := Length(paths[i]); |
| 1027 |
if (len < 3) and (not isOpen or (len < 2)) then Continue; |
| 1028 |
p := @paths[i][0]; |
| 1029 |
va0 := v; vaCurr := v; |
| 1030 |
vaCurr.pt := p^; |
| 1031 |
vaCurr.prev := nil; |
| 1032 |
inc(p); |
| 1033 |
vaCurr.flags := []; |
| 1034 |
vaPrev := vaCurr; |
| 1035 |
inc(vaCurr); |
| 1036 |
for j := 1 to len -1 do |
| 1037 |
begin |
| 1038 |
if PointsEqual(vaPrev.pt, p^) then |
| 1039 |
begin |
| 1040 |
inc(p); |
| 1041 |
Continue; // skips duplicates |
| 1042 |
end; |
| 1043 |
vaPrev.next := vaCurr; |
| 1044 |
vaCurr.prev := vaPrev; |
| 1045 |
vaCurr.pt := p^; |
| 1046 |
vaCurr.flags := []; |
| 1047 |
vaPrev := vaCurr; |
| 1048 |
inc(vaCurr); |
| 1049 |
inc(p); |
| 1050 |
end; |
| 1051 |
if not Assigned(vaPrev.prev) then Continue; |
| 1052 |
if not isOpen and PointsEqual(vaPrev.pt, va0.pt) then |
| 1053 |
vaPrev := vaPrev.prev; |
| 1054 |
|
| 1055 |
vaPrev.next := va0; |
| 1056 |
va0.prev := vaPrev; |
| 1057 |
v := vaCurr; // ie get ready for next path |
| 1058 |
if isOpen and (va0.next = va0) then Continue; |
| 1059 |
|
| 1060 |
// now find and assign local minima |
| 1061 |
if (isOpen) then |
| 1062 |
begin |
| 1063 |
vaCurr := va0.next; |
| 1064 |
while (vaCurr <> va0) and (vaCurr.pt.Y = va0.pt.Y) do |
| 1065 |
vaCurr := vaCurr.next; |
| 1066 |
ascending := vaCurr.pt.Y <= va0.pt.Y; |
| 1067 |
if (ascending) then |
| 1068 |
begin |
| 1069 |
va0.flags := [vfOpenStart]; |
| 1070 |
AddLocMin(va0); |
| 1071 |
end |
| 1072 |
else |
| 1073 |
va0.flags := [vfOpenStart, vfLocMax]; |
| 1074 |
end else |
| 1075 |
begin |
| 1076 |
// closed path |
| 1077 |
vaPrev := va0.prev; |
| 1078 |
while (vaPrev <> va0) and (vaPrev.pt.Y = va0.pt.Y) do |
| 1079 |
vaPrev := vaPrev.prev; |
| 1080 |
if (vaPrev = va0) then |
| 1081 |
Continue; // only open paths can be completely flat |
| 1082 |
ascending := vaPrev.pt.Y > va0.pt.Y; |
| 1083 |
end; |
| 1084 |
|
| 1085 |
ascending0 := ascending; |
| 1086 |
vaPrev := va0; |
| 1087 |
vaCurr := va0.next; |
| 1088 |
while (vaCurr <> va0) do |
| 1089 |
begin |
| 1090 |
if (vaCurr.pt.Y > vaPrev.pt.Y) and ascending then |
| 1091 |
begin |
| 1092 |
Include(vaPrev.flags, vfLocMax); |
| 1093 |
ascending := false; |
| 1094 |
end |
| 1095 |
else if (vaCurr.pt.Y < vaPrev.pt.Y) and not ascending then |
| 1096 |
begin |
| 1097 |
ascending := true; |
| 1098 |
AddLocMin(vaPrev); |
| 1099 |
end; |
| 1100 |
vaPrev := vaCurr; |
| 1101 |
vaCurr := vaCurr.next; |
| 1102 |
end; |
| 1103 |
|
| 1104 |
if (isOpen) then |
| 1105 |
begin |
| 1106 |
Include(vaPrev.flags, vfOpenEnd); |
| 1107 |
if ascending then |
| 1108 |
Include(vaPrev.flags, vfLocMax) else |
| 1109 |
AddLocMin(vaPrev); |
| 1110 |
end |
| 1111 |
else if (ascending <> ascending0) then |
| 1112 |
begin |
| 1113 |
if (ascending0) then AddLocMin(vaPrev) |
| 1114 |
else Include(vaPrev.flags, vfLocMax); |
| 1115 |
end; |
| 1116 |
end; |
| 1117 |
end; |
| 1118 |
//------------------------------------------------------------------------------ |
| 1119 |
|
| 1120 |
function BuildPath(op: POutPt; reverse, isOpen: Boolean; |
| 1121 |
out path: TPath64): Boolean; |
| 1122 |
var |
| 1123 |
i,j, cnt: integer; |
| 1124 |
begin |
| 1125 |
cnt := PointCount(op); |
| 1126 |
if (cnt < 3) and (not isOpen or (Cnt < 2)) then |
| 1127 |
begin |
| 1128 |
Result := false; |
| 1129 |
Exit; |
| 1130 |
end; |
| 1131 |
|
| 1132 |
if (cnt = 3) and not IsOpen and IsVerySmallTriangle(op) then |
| 1133 |
begin |
| 1134 |
Result := false; |
| 1135 |
Exit; |
| 1136 |
end; |
| 1137 |
|
| 1138 |
setLength(path, cnt); |
| 1139 |
if reverse then |
| 1140 |
begin |
| 1141 |
path[0] := op.pt; |
| 1142 |
op := op.prev; |
| 1143 |
end else |
| 1144 |
begin |
| 1145 |
op := op.next; |
| 1146 |
path[0] := op.pt; |
| 1147 |
op := op.next; |
| 1148 |
end; |
| 1149 |
j := 0; |
| 1150 |
for i := 0 to cnt -2 do |
| 1151 |
begin |
| 1152 |
if not PointsEqual(path[j], op.pt) then |
| 1153 |
begin |
| 1154 |
inc(j); |
| 1155 |
path[j] := op.pt; |
| 1156 |
end; |
| 1157 |
if reverse then op := op.prev else op := op.next; |
| 1158 |
end; |
| 1159 |
|
| 1160 |
setLength(path, j+1); |
| 1161 |
if isOpen then |
| 1162 |
Result := (j > 0) else |
| 1163 |
Result := (j > 1); |
| 1164 |
end; |
| 1165 |
//------------------------------------------------------------------------------ |
| 1166 |
|
| 1167 |
function DisposeOutPt(op: POutPt): POutPt; |
| 1168 |
begin |
| 1169 |
if op.next = op then |
| 1170 |
Result := nil else |
| 1171 |
Result := op.next; |
| 1172 |
op.prev.next := op.next; |
| 1173 |
op.next.prev := op.prev; |
| 1174 |
Dispose(Op); |
| 1175 |
end; |
| 1176 |
//------------------------------------------------------------------------------ |
| 1177 |
|
| 1178 |
function LocMinListSort(item1, item2: Pointer): Integer; |
| 1179 |
var |
| 1180 |
q: Int64; |
| 1181 |
lm1: PLocalMinima absolute item1; |
| 1182 |
lm2: PLocalMinima absolute item2; |
| 1183 |
begin |
| 1184 |
q := lm2.vertex.pt.Y - lm1.vertex.pt.Y; |
| 1185 |
if q < 0 then |
| 1186 |
Result := -1 |
| 1187 |
else if q > 0 then |
| 1188 |
Result := 1 |
| 1189 |
else |
| 1190 |
begin |
| 1191 |
q := lm2.vertex.pt.X - lm1.vertex.pt.X; |
| 1192 |
if q < 0 then Result := 1 |
| 1193 |
else if q > 0 then Result := -1 |
| 1194 |
else Result := 0; |
| 1195 |
end; |
| 1196 |
end; |
| 1197 |
//------------------------------------------------------------------------------ |
| 1198 |
|
| 1199 |
function HorzSegListSort(item1, item2: Pointer): Integer; |
| 1200 |
var |
| 1201 |
q: Int64; |
| 1202 |
h1: PHorzSegment absolute item1; |
| 1203 |
h2: PHorzSegment absolute item2; |
| 1204 |
begin |
| 1205 |
q := h2.leftOp.pt.X - h1.leftOp.pt.X; |
| 1206 |
if q > 0 then Result := -1 |
| 1207 |
else if q < 0 then Result := 1 |
| 1208 |
else Result := h1.leftOp.outrec.idx - h2.leftOp.outrec.idx; |
| 1209 |
end; |
| 1210 |
//------------------------------------------------------------------------------ |
| 1211 |
|
| 1212 |
procedure SetSides(outRec: POutRec; startEdge, endEdge: PActive); |
| 1213 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 1214 |
begin |
| 1215 |
outRec.frontE := startEdge; |
| 1216 |
outRec.backE := endEdge; |
| 1217 |
end; |
| 1218 |
//------------------------------------------------------------------------------ |
| 1219 |
|
| 1220 |
procedure SwapOutRecs(e1, e2: PActive); |
| 1221 |
var |
| 1222 |
or1, or2: POutRec; |
| 1223 |
e: PActive; |
| 1224 |
begin |
| 1225 |
or1 := e1.outrec; |
| 1226 |
or2 := e2.outrec; |
| 1227 |
if (or1 = or2) then |
| 1228 |
begin |
| 1229 |
// nb: at least one edge is 'hot' |
| 1230 |
e := or1.frontE; |
| 1231 |
or1.frontE := or1.backE; |
| 1232 |
or1.backE := e; |
| 1233 |
Exit; |
| 1234 |
end; |
| 1235 |
if assigned(or1) then |
| 1236 |
begin |
| 1237 |
if e1 = or1.frontE then |
| 1238 |
or1.frontE := e2 else |
| 1239 |
or1.backE := e2; |
| 1240 |
end; |
| 1241 |
if assigned(or2) then |
| 1242 |
begin |
| 1243 |
if e2 = or2.frontE then |
| 1244 |
or2.frontE := e1 else |
| 1245 |
or2.backE := e1; |
| 1246 |
end; |
| 1247 |
e1.outrec := or2; |
| 1248 |
e2.outrec := or1; |
| 1249 |
end; |
| 1250 |
//------------------------------------------------------------------------------ |
| 1251 |
|
| 1252 |
procedure Swap(p1, p2: PPointer); overload; {$IFDEF INLINING} inline; {$ENDIF} |
| 1253 |
var |
| 1254 |
p: Pointer; |
| 1255 |
begin |
| 1256 |
if p1^ = p2^ then Exit; |
| 1257 |
p := p1^; |
| 1258 |
p1^ := p2^; |
| 1259 |
p2^ := p; |
| 1260 |
end; |
| 1261 |
//------------------------------------------------------------------------------ |
| 1262 |
|
| 1263 |
function Area(op: POutPt): Double; |
| 1264 |
var |
| 1265 |
op2: POutPt; |
| 1266 |
d: double; |
| 1267 |
begin |
| 1268 |
// https://en.wikipedia.org/wiki/Shoelace_formula |
| 1269 |
Result := 0; |
| 1270 |
if not Assigned(op) then Exit; |
| 1271 |
op2 := op; |
| 1272 |
repeat |
| 1273 |
d := (op2.prev.pt.Y + op2.pt.Y); |
| 1274 |
Result := Result + d * (op2.prev.pt.X - op2.pt.X); |
| 1275 |
op2 := op2.next; |
| 1276 |
until op2 = op; |
| 1277 |
Result := Result * 0.5; |
| 1278 |
end; |
| 1279 |
//------------------------------------------------------------------------------ |
| 1280 |
|
| 1281 |
function AreaTriangle(const pt1, pt2, pt3: TPoint64): double; |
| 1282 |
var |
| 1283 |
d1,d2,d3,d4,d5,d6: double; |
| 1284 |
begin |
| 1285 |
d1 := (pt3.y + pt1.y); |
| 1286 |
d2 := (pt3.x - pt1.x); |
| 1287 |
d3 := (pt1.y + pt2.y); |
| 1288 |
d4 := (pt1.x - pt2.x); |
| 1289 |
d5 := (pt2.y + pt3.y); |
| 1290 |
d6 := (pt2.x - pt3.x); |
| 1291 |
result := d1 * d2 + d3 *d4 + d5 *d6; |
| 1292 |
end; |
| 1293 |
//------------------------------------------------------------------------------ |
| 1294 |
|
| 1295 |
function OutrecIsAscending(hotEdge: PActive): Boolean; |
| 1296 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 1297 |
begin |
| 1298 |
Result := (hotEdge = hotEdge.outrec.frontE); |
| 1299 |
end; |
| 1300 |
//------------------------------------------------------------------------------ |
| 1301 |
|
| 1302 |
procedure SwapFrontBackSides(outRec: POutRec); {$IFDEF INLINING} inline; {$ENDIF} |
| 1303 |
var |
| 1304 |
e2: PActive; |
| 1305 |
begin |
| 1306 |
// while this proc. is needed for open paths |
| 1307 |
// it's almost never needed for closed paths |
| 1308 |
e2 := outRec.frontE; |
| 1309 |
outRec.frontE := outRec.backE; |
| 1310 |
outRec.backE := e2; |
| 1311 |
outRec.pts := outRec.pts.next; |
| 1312 |
end; |
| 1313 |
//------------------------------------------------------------------------------ |
| 1314 |
|
| 1315 |
function EdgesAdjacentInAEL(node: PIntersectNode): Boolean; |
| 1316 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 1317 |
var |
| 1318 |
active1, active2: PActive; |
| 1319 |
begin |
| 1320 |
active1 := node.active1; |
| 1321 |
active2 := node.active2; |
| 1322 |
Result := (active1.nextInAEL = active2) or (active1.prevInAEL = active2); |
| 1323 |
end; |
| 1324 |
//------------------------------------------------------------------------------ |
| 1325 |
|
| 1326 |
procedure SetOwner(outrec, newOwner: POutRec); |
| 1327 |
var |
| 1328 |
tmp: POutRec; |
| 1329 |
begin |
| 1330 |
//precondition1: new_owner is never null |
| 1331 |
while Assigned(newOwner.owner) and |
| 1332 |
not Assigned(newOwner.owner.pts) do |
| 1333 |
newOwner.owner := newOwner.owner.owner; |
| 1334 |
//make sure that outrec isn't an owner of newOwner |
| 1335 |
tmp := newOwner; |
| 1336 |
while Assigned(tmp) and (tmp <> outrec) do |
| 1337 |
tmp := tmp.owner; |
| 1338 |
if Assigned(tmp) then |
| 1339 |
newOwner.owner := outrec.owner; |
| 1340 |
outrec.owner := newOwner; |
| 1341 |
end; |
| 1342 |
|
| 1343 |
//------------------------------------------------------------------------------ |
| 1344 |
// TReuseableDataContainer64 methods ... |
| 1345 |
//------------------------------------------------------------------------------ |
| 1346 |
|
| 1347 |
constructor TReuseableDataContainer64.Create; |
| 1348 |
begin |
| 1349 |
FLocMinList := TLocMinList.Create; |
| 1350 |
FVertexArrayList := TList.Create; |
| 1351 |
end; |
| 1352 |
//------------------------------------------------------------------------------ |
| 1353 |
|
| 1354 |
destructor TReuseableDataContainer64.Destroy; |
| 1355 |
begin |
| 1356 |
Clear; |
| 1357 |
FLocMinList.Free; |
| 1358 |
FVertexArrayList.Free; |
| 1359 |
inherited; |
| 1360 |
end; |
| 1361 |
//------------------------------------------------------------------------------ |
| 1362 |
|
| 1363 |
procedure TReuseableDataContainer64.Clear; |
| 1364 |
var |
| 1365 |
i: integer; |
| 1366 |
begin |
| 1367 |
FLocMinList.Clear; |
| 1368 |
for i := 0 to FVertexArrayList.Count -1 do |
| 1369 |
FreeMem(UnsafeGet(FVertexArrayList, i)); |
| 1370 |
FVertexArrayList.Clear; |
| 1371 |
end; |
| 1372 |
//------------------------------------------------------------------------------ |
| 1373 |
|
| 1374 |
procedure TReuseableDataContainer64.AddPaths(const paths: TPaths64; |
| 1375 |
pathType: TPathType; isOpen: Boolean); |
| 1376 |
begin |
| 1377 |
AddPathsToVertexList(paths, pathType, isOpen, |
| 1378 |
FVertexArrayList, FLocMinList); |
| 1379 |
end; |
| 1380 |
|
| 1381 |
//------------------------------------------------------------------------------ |
| 1382 |
// TClipperBase methods ... |
| 1383 |
//------------------------------------------------------------------------------ |
| 1384 |
|
| 1385 |
constructor TClipperBase.Create; |
| 1386 |
begin |
| 1387 |
FLocMinList := TLocMinList.Create(4); |
| 1388 |
FOutRecList := TOutRecList.Create(4); |
| 1389 |
FIntersectList := TList.Create; |
| 1390 |
FVertexArrayList := TList.Create; |
| 1391 |
FHorzSegList := THorzSegList.Create; |
| 1392 |
FHorzJoinList := THorzJoinList.Create; |
| 1393 |
FPreserveCollinear := true; |
| 1394 |
FReverseSolution := false; |
| 1395 |
end; |
| 1396 |
//------------------------------------------------------------------------------ |
| 1397 |
|
| 1398 |
destructor TClipperBase.Destroy; |
| 1399 |
begin |
| 1400 |
Clear; |
| 1401 |
FLocMinList.Free; |
| 1402 |
FOutRecList.Free; |
| 1403 |
FIntersectList.Free; |
| 1404 |
FHorzSegList.Free; |
| 1405 |
FHorzJoinList.Free; |
| 1406 |
FVertexArrayList.Free; |
| 1407 |
inherited; |
| 1408 |
end; |
| 1409 |
//------------------------------------------------------------------------------ |
| 1410 |
|
| 1411 |
function TClipperBase.ClearSolutionOnly: Boolean; |
| 1412 |
var |
| 1413 |
dummy: Int64; |
| 1414 |
begin |
| 1415 |
try |
| 1416 |
// in case of exceptions ... |
| 1417 |
DeleteEdges(FActives); |
| 1418 |
while assigned(FScanLine) do PopScanLine(dummy); |
| 1419 |
DisposeIntersectNodes; |
| 1420 |
DisposeScanLineList; |
| 1421 |
FOutRecList.Clear; |
| 1422 |
FHorzSegList.Clear; |
| 1423 |
FHorzJoinList.Clear; |
| 1424 |
Result := true; |
| 1425 |
except |
| 1426 |
Result := false; |
| 1427 |
end; |
| 1428 |
end; |
| 1429 |
//------------------------------------------------------------------------------ |
| 1430 |
|
| 1431 |
procedure TClipperBase.Clear; |
| 1432 |
begin |
| 1433 |
ClearSolutionOnly; |
| 1434 |
DisposeVerticesAndLocalMinima; |
| 1435 |
FCurrentLocMinIdx := 0; |
| 1436 |
FLocMinListSorted := false; |
| 1437 |
FHasOpenPaths := False; |
| 1438 |
end; |
| 1439 |
//------------------------------------------------------------------------------ |
| 1440 |
|
| 1441 |
procedure TClipperBase.Reset; |
| 1442 |
var |
| 1443 |
i: Integer; |
| 1444 |
begin |
| 1445 |
if not FLocMinListSorted then |
| 1446 |
begin |
| 1447 |
FLocMinList.Sort(LocMinListSort); |
| 1448 |
FLocMinListSorted := true; |
| 1449 |
end; |
| 1450 |
|
| 1451 |
for i := FLocMinList.Count -1 downto 0 do |
| 1452 |
InsertScanLine(PLocalMinima(FLocMinList.UnsafeGet(i)).vertex.pt.Y); |
| 1453 |
FCurrentLocMinIdx := 0; |
| 1454 |
FActives := nil; |
| 1455 |
FSel := nil; |
| 1456 |
FSucceeded := true; |
| 1457 |
end; |
| 1458 |
//------------------------------------------------------------------------------ |
| 1459 |
|
| 1460 |
{$IFDEF USINGZ} |
| 1461 |
function XYCoordsEqual(const pt1, pt2: TPoint64): Boolean; |
| 1462 |
begin |
| 1463 |
Result := (pt1.X = pt2.X) and (pt1.Y = pt2.Y); |
| 1464 |
end; |
| 1465 |
//------------------------------------------------------------------------------ |
| 1466 |
|
| 1467 |
procedure TClipperBase.SetZ(e1, e2: PActive; var intersectPt: TPoint64); |
| 1468 |
begin |
| 1469 |
if not Assigned(fZCallback) then |
| 1470 |
begin |
| 1471 |
intersectPt.Z := 0; |
| 1472 |
Exit; |
| 1473 |
end; |
| 1474 |
|
| 1475 |
// prioritize subject vertices over clip vertices |
| 1476 |
// and pass the subject vertices before clip vertices in the callback |
| 1477 |
if (GetPolyType(e1) = ptSubject) then |
| 1478 |
begin |
| 1479 |
if (XYCoordsEqual(intersectPt, e1.bot)) then intersectPt.Z := e1.bot.Z |
| 1480 |
else if (XYCoordsEqual(intersectPt, e1.top)) then intersectPt.Z := e1.top.Z |
| 1481 |
else if (XYCoordsEqual(intersectPt, e2.bot)) then intersectPt.Z := e2.bot.Z |
| 1482 |
else if (XYCoordsEqual(intersectPt, e2.top)) then intersectPt.Z := e2.top.Z |
| 1483 |
else intersectPt.Z := fDefaultZ; |
| 1484 |
fZCallback(e1.bot, e1.top, e2.bot, e2.top, intersectPt); |
| 1485 |
end else |
| 1486 |
begin |
| 1487 |
if (XYCoordsEqual(intersectPt, e2.bot)) then intersectPt.Z := e2.bot.Z |
| 1488 |
else if (XYCoordsEqual(intersectPt, e2.top)) then intersectPt.Z := e2.top.Z |
| 1489 |
else if (XYCoordsEqual(intersectPt, e1.bot)) then intersectPt.Z := e1.bot.Z |
| 1490 |
else if (XYCoordsEqual(intersectPt, e1.top)) then intersectPt.Z := e1.top.Z |
| 1491 |
else intersectPt.Z := fDefaultZ; |
| 1492 |
fZCallback(e2.bot, e2.top, e1.bot, e1.top, intersectPt); |
| 1493 |
end; |
| 1494 |
end; |
| 1495 |
//------------------------------------------------------------------------------ |
| 1496 |
{$ENDIF} |
| 1497 |
|
| 1498 |
procedure TClipperBase.InsertScanLine(const Y: Int64); |
| 1499 |
var |
| 1500 |
newSl, sl: PScanLine; |
| 1501 |
begin |
| 1502 |
// The scanline list is a single-linked list of all the Y coordinates of |
| 1503 |
// subject and clip vertices in the clipping operation (sorted descending). |
| 1504 |
// However, only scanline Y's at Local Minima are inserted before clipping |
| 1505 |
// starts. While scanlines are removed sequentially during the sweep operation, |
| 1506 |
// new scanlines are only inserted whenever edge bounds are updated. This keeps |
| 1507 |
// the scanline list relatively short, optimising performance. |
| 1508 |
if not Assigned(FScanLine) then |
| 1509 |
begin |
| 1510 |
new(newSl); |
| 1511 |
newSl.y := Y; |
| 1512 |
FScanLine := newSl; |
| 1513 |
newSl.next := nil; |
| 1514 |
end else if Y > FScanLine.y then |
| 1515 |
begin |
| 1516 |
new(newSl); |
| 1517 |
newSl.y := Y; |
| 1518 |
newSl.next := FScanLine; |
| 1519 |
FScanLine := newSl; |
| 1520 |
end else |
| 1521 |
begin |
| 1522 |
sl := FScanLine; |
| 1523 |
while Assigned(sl.next) and (Y <= sl.next.y) do |
| 1524 |
sl := sl.next; |
| 1525 |
if Y = sl.y then Exit; // skip duplicates |
| 1526 |
new(newSl); |
| 1527 |
newSl.y := Y; |
| 1528 |
newSl.next := sl.next; |
| 1529 |
sl.next := newSl; |
| 1530 |
end; |
| 1531 |
end; |
| 1532 |
//------------------------------------------------------------------------------ |
| 1533 |
|
| 1534 |
function TClipperBase.PopScanLine(out Y: Int64): Boolean; |
| 1535 |
var |
| 1536 |
sl: PScanLine; |
| 1537 |
begin |
| 1538 |
Result := assigned(FScanLine); |
| 1539 |
if not Result then Exit; |
| 1540 |
Y := FScanLine.y; |
| 1541 |
sl := FScanLine; |
| 1542 |
FScanLine := FScanLine.next; |
| 1543 |
dispose(sl); |
| 1544 |
end; |
| 1545 |
//------------------------------------------------------------------------------ |
| 1546 |
|
| 1547 |
function TClipperBase.PopLocalMinima(Y: Int64; |
| 1548 |
out localMinima: PLocalMinima): Boolean; |
| 1549 |
begin |
| 1550 |
Result := false; |
| 1551 |
if FCurrentLocMinIdx = FLocMinList.Count then Exit; |
| 1552 |
localMinima := PLocalMinima(FLocMinList.UnsafeGet(FCurrentLocMinIdx)); |
| 1553 |
if (localMinima.vertex.pt.Y = Y) then |
| 1554 |
begin |
| 1555 |
inc(FCurrentLocMinIdx); |
| 1556 |
Result := true; |
| 1557 |
end; |
| 1558 |
end; |
| 1559 |
//------------------------------------------------------------------------------ |
| 1560 |
|
| 1561 |
procedure TClipperBase.DisposeScanLineList; |
| 1562 |
var |
| 1563 |
sl: PScanLine; |
| 1564 |
begin |
| 1565 |
while Assigned(FScanLine) do |
| 1566 |
begin |
| 1567 |
sl := FScanLine.next; |
| 1568 |
Dispose(FScanLine); |
| 1569 |
FScanLine := sl; |
| 1570 |
end; |
| 1571 |
end; |
| 1572 |
//------------------------------------------------------------------------------ |
| 1573 |
|
| 1574 |
procedure TClipperBase.DisposeVerticesAndLocalMinima; |
| 1575 |
var |
| 1576 |
i: Integer; |
| 1577 |
begin |
| 1578 |
FLocMinList.Clear; |
| 1579 |
for i := 0 to FVertexArrayList.Count -1 do |
| 1580 |
FreeMem(UnsafeGet(FVertexArrayList, i)); |
| 1581 |
FVertexArrayList.Clear; |
| 1582 |
end; |
| 1583 |
//------------------------------------------------------------------------------ |
| 1584 |
|
| 1585 |
procedure TClipperBase.AddPath(const path: TPath64; |
| 1586 |
pathType: TPathType; isOpen: Boolean); |
| 1587 |
var |
| 1588 |
pp: TPaths64; |
| 1589 |
begin |
| 1590 |
SetLength(pp, 1); |
| 1591 |
pp[0] := path; |
| 1592 |
AddPaths(pp, pathType, isOpen); |
| 1593 |
end; |
| 1594 |
//------------------------------------------------------------------------------ |
| 1595 |
|
| 1596 |
procedure TClipperBase.AddPaths(const paths: TPaths64; |
| 1597 |
pathType: TPathType; isOpen: Boolean); |
| 1598 |
begin |
| 1599 |
if isOpen then FHasOpenPaths := true; |
| 1600 |
FLocMinListSorted := false; |
| 1601 |
AddPathsToVertexList(paths, pathType, isOpen, |
| 1602 |
FVertexArrayList, FLocMinList); |
| 1603 |
end; |
| 1604 |
//------------------------------------------------------------------------------ |
| 1605 |
|
| 1606 |
procedure TClipperBase.AddReuseableData(const reuseableData: TReuseableDataContainer64); |
| 1607 |
var |
| 1608 |
i: integer; |
| 1609 |
lm: PLocalMinima; |
| 1610 |
begin |
| 1611 |
if reuseableData.FLocMinList.Count = 0 then Exit; |
| 1612 |
// nb: reuseableData will continue to own the vertices |
| 1613 |
// and will remain responsible for their clean up. |
| 1614 |
// Consequently, it's important that the reuseableData object isn't |
| 1615 |
// destroyed before the Clipper object that's using the data. |
| 1616 |
FLocMinListSorted := false; |
| 1617 |
for i := 0 to reuseableData.FLocMinList.Count -1 do |
| 1618 |
with PLocalMinima(reuseableData.FLocMinList[i])^ do |
| 1619 |
begin |
| 1620 |
lm := self.FLocMinList.Add; |
| 1621 |
lm.vertex := vertex; |
| 1622 |
lm.polytype := polytype; |
| 1623 |
lm.isOpen := isOpen; |
| 1624 |
if isOpen then FHasOpenPaths := true; |
| 1625 |
end; |
| 1626 |
end; |
| 1627 |
//------------------------------------------------------------------------------ |
| 1628 |
|
| 1629 |
function TClipperBase.IsContributingClosed(e: PActive): Boolean; |
| 1630 |
begin |
| 1631 |
Result := false; |
| 1632 |
case FFillRule of |
| 1633 |
frNonZero: if abs(e.windCnt) <> 1 then Exit; |
| 1634 |
frPositive: if (e.windCnt <> 1) then Exit; |
| 1635 |
frNegative: if (e.windCnt <> -1) then Exit; |
| 1636 |
end; |
| 1637 |
|
| 1638 |
case FClipType of |
| 1639 |
ctIntersection: |
| 1640 |
case FFillRule of |
| 1641 |
frPositive: Result := (e.windCnt2 > 0); |
| 1642 |
frNegative: Result := (e.windCnt2 < 0); |
| 1643 |
else Result := (e.windCnt2 <> 0); |
| 1644 |
end; |
| 1645 |
ctUnion: |
| 1646 |
case FFillRule of |
| 1647 |
frPositive: Result := (e.windCnt2 <= 0); |
| 1648 |
frNegative: Result := (e.windCnt2 >= 0); |
| 1649 |
else Result := (e.windCnt2 = 0); |
| 1650 |
end; |
| 1651 |
ctDifference: |
| 1652 |
begin |
| 1653 |
case FFillRule of |
| 1654 |
frPositive: Result := (e.windCnt2 <= 0); |
| 1655 |
frNegative: Result := (e.windCnt2 >= 0); |
| 1656 |
else Result := (e.windCnt2 = 0); |
| 1657 |
end; |
| 1658 |
if GetPolyType(e) <> ptSubject then Result := not Result; |
| 1659 |
end; |
| 1660 |
ctXor: |
| 1661 |
Result := true; |
| 1662 |
end; |
| 1663 |
end; |
| 1664 |
//------------------------------------------------------------------------------ |
| 1665 |
|
| 1666 |
function TClipperBase.IsContributingOpen(e: PActive): Boolean; |
| 1667 |
var |
| 1668 |
isInSubj, isInClip: Boolean; |
| 1669 |
begin |
| 1670 |
case FFillRule of |
| 1671 |
frPositive: |
| 1672 |
begin |
| 1673 |
isInSubj := e.windCnt > 0; |
| 1674 |
isInClip := e.windCnt2 > 0; |
| 1675 |
end; |
| 1676 |
frNegative: |
| 1677 |
begin |
| 1678 |
isInSubj := e.windCnt < 0; |
| 1679 |
isInClip := e.windCnt2 < 0; |
| 1680 |
end; |
| 1681 |
else |
| 1682 |
begin |
| 1683 |
isInSubj := e.windCnt <> 0; |
| 1684 |
isInClip := e.windCnt2 <> 0; |
| 1685 |
end; |
| 1686 |
end; |
| 1687 |
|
| 1688 |
case FClipType of |
| 1689 |
ctIntersection: Result := isInClip; |
| 1690 |
ctUnion: Result := not isInSubj and not isInClip; |
| 1691 |
else Result := not isInClip; |
| 1692 |
end; |
| 1693 |
end; |
| 1694 |
//------------------------------------------------------------------------------ |
| 1695 |
|
| 1696 |
procedure TClipperBase.SetWindCountForClosedPathEdge(e: PActive); |
| 1697 |
var |
| 1698 |
e2: PActive; |
| 1699 |
begin |
| 1700 |
// Wind counts refer to polygon regions not edges, so here an edge's WindCnt |
| 1701 |
// indicates the higher of the wind counts for the two regions touching the |
| 1702 |
// edge. (nb: Adjacent regions can only ever have their wind counts differ by |
| 1703 |
// one. Also, open paths have no meaningful wind directions or counts.) |
| 1704 |
|
| 1705 |
e2 := e.prevInAEL; |
| 1706 |
// find the nearest closed path edge of the same PolyType in AEL (heading left) |
| 1707 |
while Assigned(e2) and (not IsSamePolyType(e2, e) or IsOpen(e2)) do |
| 1708 |
e2 := e2.prevInAEL; |
| 1709 |
|
| 1710 |
if not Assigned(e2) then |
| 1711 |
begin |
| 1712 |
e.windCnt := e.windDx; |
| 1713 |
e2 := FActives; |
| 1714 |
end |
| 1715 |
else if (FFillRule = frEvenOdd) then |
| 1716 |
begin |
| 1717 |
e.windCnt := e.windDx; |
| 1718 |
e.windCnt2 := e2.windCnt2; |
| 1719 |
e2 := e2.nextInAEL; |
| 1720 |
end else |
| 1721 |
begin |
| 1722 |
// NonZero, positive, or negative filling here ... |
| 1723 |
// when e2's WindCnt is in the SAME direction as its WindDx, |
| 1724 |
// then polygon will fill on the right of 'e2' (and 'e' will be inside) |
| 1725 |
// nb: neither e2.WindCnt nor e2.WindDx should ever be 0. |
| 1726 |
if (e2.windCnt * e2.windDx < 0) then |
| 1727 |
begin |
| 1728 |
// opposite directions so 'e' is outside 'e2' ... |
| 1729 |
if (Abs(e2.windCnt) > 1) then |
| 1730 |
begin |
| 1731 |
// outside prev poly but still inside another. |
| 1732 |
e.windCnt := Iif(e2.windDx * e.windDx < 0, |
| 1733 |
e2.windCnt, // reversing direction so use the same WC |
| 1734 |
e2.windCnt + e.windDx); |
| 1735 |
end |
| 1736 |
// now outside all polys of same polytype so set own WC ... |
| 1737 |
else e.windCnt := e.windDx; |
| 1738 |
end else |
| 1739 |
begin |
| 1740 |
//'e' must be inside 'e2' |
| 1741 |
e.windCnt := Iif(e2.windDx * e.windDx < 0, |
| 1742 |
e2.windCnt, // reversing direction so use the same WC |
| 1743 |
e2.windCnt + e.windDx); // else keep 'increasing' the WC |
| 1744 |
end; |
| 1745 |
e.windCnt2 := e2.windCnt2; |
| 1746 |
e2 := e2.nextInAEL; |
| 1747 |
end; |
| 1748 |
|
| 1749 |
// update WindCnt2 ... |
| 1750 |
if FFillRule = frEvenOdd then |
| 1751 |
while (e2 <> e) do |
| 1752 |
begin |
| 1753 |
if IsSamePolyType(e2, e) or IsOpen(e2) then // do nothing |
| 1754 |
else if e.windCnt2 = 0 then e.windCnt2 := 1 |
| 1755 |
else e.windCnt2 := 0; |
| 1756 |
e2 := e2.nextInAEL; |
| 1757 |
end |
| 1758 |
else |
| 1759 |
while (e2 <> e) do |
| 1760 |
begin |
| 1761 |
if not IsSamePolyType(e2, e) and not IsOpen(e2) then |
| 1762 |
Inc(e.windCnt2, e2.windDx); |
| 1763 |
e2 := e2.nextInAEL; |
| 1764 |
end; |
| 1765 |
end; |
| 1766 |
//------------------------------------------------------------------------------ |
| 1767 |
|
| 1768 |
procedure TClipperBase.SetWindCountForOpenPathEdge(e: PActive); |
| 1769 |
var |
| 1770 |
e2: PActive; |
| 1771 |
cnt1, cnt2: Integer; |
| 1772 |
begin |
| 1773 |
e2 := FActives; |
| 1774 |
if FFillRule = frEvenOdd then |
| 1775 |
begin |
| 1776 |
cnt1 := 0; |
| 1777 |
cnt2 := 0; |
| 1778 |
while (e2 <> e) do |
| 1779 |
begin |
| 1780 |
if (GetPolyType(e2) = ptClip) then inc(cnt2) |
| 1781 |
else if not IsOpen(e2) then inc(cnt1); |
| 1782 |
e2 := e2.nextInAEL; |
| 1783 |
end; |
| 1784 |
e.windCnt := Iif(Odd(cnt1), 1, 0); |
| 1785 |
e.windCnt2 := Iif(Odd(cnt2), 1, 0); |
| 1786 |
end else |
| 1787 |
begin |
| 1788 |
// if FClipType in [ctUnion, ctDifference] then e.WindCnt := e.WindDx; |
| 1789 |
while (e2 <> e) do |
| 1790 |
begin |
| 1791 |
if (GetPolyType(e2) = ptClip) then inc(e.windCnt2, e2.windDx) |
| 1792 |
else if not IsOpen(e2) then inc(e.windCnt, e2.windDx); |
| 1793 |
e2 := e2.nextInAEL; |
| 1794 |
end; |
| 1795 |
end; |
| 1796 |
end; |
| 1797 |
//------------------------------------------------------------------------------ |
| 1798 |
|
| 1799 |
function IsValidAelOrder(resident, newcomer: PActive): Boolean; |
| 1800 |
var |
| 1801 |
botY: Int64; |
| 1802 |
newcomerIsLeft: Boolean; |
| 1803 |
d: double; |
| 1804 |
begin |
| 1805 |
if (newcomer.currX <> resident.currX) then |
| 1806 |
begin |
| 1807 |
Result := newcomer.currX > resident.currX; |
| 1808 |
Exit; |
| 1809 |
end; |
| 1810 |
|
| 1811 |
// get the turning direction a1.top, a2.bot, a2.top |
| 1812 |
d := CrossProduct(resident.top, newcomer.bot, newcomer.top); |
| 1813 |
if d <> 0 then |
| 1814 |
begin |
| 1815 |
Result := d < 0; |
| 1816 |
Exit; |
| 1817 |
end; |
| 1818 |
|
| 1819 |
// edges must be collinear to get here |
| 1820 |
|
| 1821 |
if not IsMaxima(resident) and |
| 1822 |
(resident.top.Y > newcomer.top.Y) then |
| 1823 |
begin |
| 1824 |
Result := CrossProduct(newcomer.bot, |
| 1825 |
resident.top, NextVertex(resident).pt) <= 0; |
| 1826 |
Exit; |
| 1827 |
end |
| 1828 |
else if not IsMaxima(newcomer) and |
| 1829 |
(newcomer.top.Y > resident.top.Y) then |
| 1830 |
begin |
| 1831 |
Result := CrossProduct(newcomer.bot, |
| 1832 |
newcomer.top, NextVertex(newcomer).pt) >= 0; |
| 1833 |
Exit; |
| 1834 |
end; |
| 1835 |
|
| 1836 |
botY := newcomer.bot.Y; |
| 1837 |
newcomerIsLeft := IsLeftBound(newcomer); |
| 1838 |
|
| 1839 |
if (resident.bot.Y <> botY) or |
| 1840 |
(resident.locMin.vertex.pt.Y <> botY) then |
| 1841 |
Result := newcomerIsLeft |
| 1842 |
// resident must also have just been inserted |
| 1843 |
else if IsLeftBound(resident) <> newcomerIsLeft then |
| 1844 |
Result := newcomerIsLeft |
| 1845 |
else if IsCollinear(PrevPrevVertex(resident).pt, |
| 1846 |
resident.bot, resident.top) then |
| 1847 |
Result := true |
| 1848 |
else |
| 1849 |
// otherwise compare turning direction of the alternate bound |
| 1850 |
Result := (CrossProduct(PrevPrevVertex(resident).pt, |
| 1851 |
newcomer.bot, PrevPrevVertex(newcomer).pt) > 0) = newcomerIsLeft; |
| 1852 |
end; |
| 1853 |
//------------------------------------------------------------------------------ |
| 1854 |
|
| 1855 |
procedure TClipperBase.InsertLeftEdge(e: PActive); |
| 1856 |
var |
| 1857 |
e2: PActive; |
| 1858 |
begin |
| 1859 |
if not Assigned(FActives) then |
| 1860 |
begin |
| 1861 |
e.prevInAEL := nil; |
| 1862 |
e.nextInAEL := nil; |
| 1863 |
FActives := e; |
| 1864 |
end |
| 1865 |
else if not IsValidAelOrder(FActives, e) then |
| 1866 |
begin |
| 1867 |
e.prevInAEL := nil; |
| 1868 |
e.nextInAEL := FActives; |
| 1869 |
FActives.prevInAEL := e; |
| 1870 |
FActives := e; |
| 1871 |
end else |
| 1872 |
begin |
| 1873 |
e2 := FActives; |
| 1874 |
while Assigned(e2.nextInAEL) and IsValidAelOrder(e2.nextInAEL, e) do |
| 1875 |
e2 := e2.nextInAEL; |
| 1876 |
//don't separate joined edges |
| 1877 |
if e2.joinedWith = jwRight then e2 := e2.nextInAEL; |
| 1878 |
|
| 1879 |
e.nextInAEL := e2.nextInAEL; |
| 1880 |
if Assigned(e2.nextInAEL) then e2.nextInAEL.prevInAEL := e; |
| 1881 |
e.prevInAEL := e2; |
| 1882 |
e2.nextInAEL := e; |
| 1883 |
end; |
| 1884 |
end; |
| 1885 |
//---------------------------------------------------------------------- |
| 1886 |
|
| 1887 |
procedure InsertRightEdge(e, e2: PActive); |
| 1888 |
begin |
| 1889 |
e2.nextInAEL := e.nextInAEL; |
| 1890 |
if Assigned(e.nextInAEL) then e.nextInAEL.prevInAEL := e2; |
| 1891 |
e2.prevInAEL := e; |
| 1892 |
e.nextInAEL := e2; |
| 1893 |
end; |
| 1894 |
//---------------------------------------------------------------------- |
| 1895 |
|
| 1896 |
procedure TClipperBase.InsertLocalMinimaIntoAEL(const botY: Int64); |
| 1897 |
var |
| 1898 |
leftB, rightB, rbn: PActive; |
| 1899 |
locMin: PLocalMinima; |
| 1900 |
contributing: Boolean; |
| 1901 |
begin |
| 1902 |
// Add local minima (if any) at BotY ... |
| 1903 |
// nb: horizontal local minima edges should contain locMin.Vertex.prev |
| 1904 |
|
| 1905 |
while PopLocalMinima(botY, locMin) do |
| 1906 |
begin |
| 1907 |
if (vfOpenStart in locMin.vertex.flags) then |
| 1908 |
begin |
| 1909 |
leftB := nil; |
| 1910 |
end else |
| 1911 |
begin |
| 1912 |
new(leftB); |
| 1913 |
FillChar(leftB^, sizeof(TActive), 0); |
| 1914 |
leftB.locMin := locMin; |
| 1915 |
leftB.outrec := nil; |
| 1916 |
leftB.joinedWith := jwNone; |
| 1917 |
leftB.bot := locMin.vertex.pt; |
| 1918 |
leftB.windDx := -1; |
| 1919 |
leftB.vertTop := locMin.vertex.prev; |
| 1920 |
leftB.top := leftB.vertTop.pt; |
| 1921 |
leftB.currX := leftB.bot.X; |
| 1922 |
SetDx(leftB); |
| 1923 |
end; |
| 1924 |
|
| 1925 |
if (vfOpenEnd in locMin.vertex.flags) then |
| 1926 |
begin |
| 1927 |
rightB := nil; |
| 1928 |
end else |
| 1929 |
begin |
| 1930 |
new(rightB); |
| 1931 |
FillChar(rightB^, sizeof(TActive), 0); |
| 1932 |
rightB.locMin := locMin; |
| 1933 |
rightB.outrec := nil; |
| 1934 |
rightB.joinedWith := jwNone; |
| 1935 |
rightB.bot := locMin.vertex.pt; |
| 1936 |
rightB.windDx := 1; |
| 1937 |
rightB.vertTop := locMin.vertex.next; |
| 1938 |
rightB.top := rightB.vertTop.pt; |
| 1939 |
rightB.currX := rightB.bot.X; |
| 1940 |
SetDx(rightB); |
| 1941 |
end; |
| 1942 |
// Currently LeftB is just descending and RightB is ascending, |
| 1943 |
// so now we swap them if LeftB isn't actually on the left. |
| 1944 |
if assigned(leftB) and assigned(rightB) then |
| 1945 |
begin |
| 1946 |
if IsHorizontal(leftB) then |
| 1947 |
begin |
| 1948 |
if IsHeadingRightHorz(leftB) then SwapActives(leftB, rightB); |
| 1949 |
end |
| 1950 |
else if IsHorizontal(rightB) then |
| 1951 |
begin |
| 1952 |
if IsHeadingLeftHorz(rightB) then SwapActives(leftB, rightB); |
| 1953 |
end |
| 1954 |
else if (leftB.dx < rightB.dx) then SwapActives(leftB, rightB); |
| 1955 |
//so when leftB has windDx == 1, the polygon will be oriented |
| 1956 |
//counter-clockwise in Cartesian coords (clockwise with inverted Y). |
| 1957 |
end |
| 1958 |
else if not assigned(leftB) then |
| 1959 |
begin |
| 1960 |
leftB := rightB; |
| 1961 |
rightB := nil; |
| 1962 |
end; |
| 1963 |
LeftB.isLeftB := true; // nb: we can't use winddx instead |
| 1964 |
|
| 1965 |
InsertLeftEdge(leftB); //////////////// |
| 1966 |
|
| 1967 |
if IsOpen(leftB) then |
| 1968 |
begin |
| 1969 |
SetWindCountForOpenPathEdge(leftB); |
| 1970 |
contributing := IsContributingOpen(leftB); |
| 1971 |
end else |
| 1972 |
begin |
| 1973 |
SetWindCountForClosedPathEdge(leftB); |
| 1974 |
contributing := IsContributingClosed(leftB); |
| 1975 |
end; |
| 1976 |
|
| 1977 |
|
| 1978 |
if assigned(rightB) then |
| 1979 |
begin |
| 1980 |
rightB.windCnt := leftB.windCnt; |
| 1981 |
rightB.windCnt2 := leftB.windCnt2; |
| 1982 |
InsertRightEdge(leftB, rightB); //////////////// |
| 1983 |
|
| 1984 |
if contributing then |
| 1985 |
begin |
| 1986 |
AddLocalMinPoly(leftB, rightB, leftB.bot, true); |
| 1987 |
if not IsHorizontal(leftB) then |
| 1988 |
CheckJoinLeft(leftB, leftB.bot); |
| 1989 |
end; |
| 1990 |
|
| 1991 |
while Assigned(rightB.nextInAEL) and |
| 1992 |
IsValidAelOrder(rightB.nextInAEL, rightB) do |
| 1993 |
begin |
| 1994 |
rbn := rightB.nextInAEL; |
| 1995 |
IntersectEdges(rightB, rbn, rightB.bot); |
| 1996 |
SwapPositionsInAEL(rightB, rightB.nextInAEL); |
| 1997 |
end; |
| 1998 |
|
| 1999 |
if IsHorizontal(rightB) then |
| 2000 |
PushHorz(rightB) |
| 2001 |
else |
| 2002 |
begin |
| 2003 |
if IsHotEdge(rightB) then |
| 2004 |
CheckJoinRight(rightB, rightB.bot); |
| 2005 |
InsertScanLine(rightB.top.Y); |
| 2006 |
end; |
| 2007 |
end |
| 2008 |
else if contributing then |
| 2009 |
StartOpenPath(leftB, leftB.bot); |
| 2010 |
|
| 2011 |
if IsHorizontal(leftB) then |
| 2012 |
PushHorz(leftB) else |
| 2013 |
InsertScanLine(leftB.top.Y); |
| 2014 |
end; |
| 2015 |
end; |
| 2016 |
//------------------------------------------------------------------------------ |
| 2017 |
|
| 2018 |
procedure TClipperBase.PushHorz(e: PActive); |
| 2019 |
begin |
| 2020 |
if assigned(FSel) then |
| 2021 |
e.nextInSEL := FSel else |
| 2022 |
e.nextInSEL := nil; |
| 2023 |
FSel := e; |
| 2024 |
end; |
| 2025 |
//------------------------------------------------------------------------------ |
| 2026 |
|
| 2027 |
function TClipperBase.PopHorz(out e: PActive): Boolean; |
| 2028 |
begin |
| 2029 |
Result := assigned(FSel); |
| 2030 |
if not Result then Exit; |
| 2031 |
e := FSel; |
| 2032 |
FSel := FSel.nextInSEL; |
| 2033 |
end; |
| 2034 |
//------------------------------------------------------------------------------ |
| 2035 |
|
| 2036 |
function TClipperBase.AddLocalMinPoly(e1, e2: PActive; |
| 2037 |
const pt: TPoint64; IsNew: Boolean = false): POutPt; |
| 2038 |
var |
| 2039 |
newOr: POutRec; |
| 2040 |
prevHotEdge: PActive; |
| 2041 |
begin |
| 2042 |
newOr := FOutRecList.Add; |
| 2043 |
newOr.owner := nil; |
| 2044 |
e1.outrec := newOr; |
| 2045 |
e2.outrec := newOr; |
| 2046 |
|
| 2047 |
if IsOpen(e1) then |
| 2048 |
begin |
| 2049 |
newOr.isOpen := true; |
| 2050 |
if e1.windDx > 0 then |
| 2051 |
SetSides(newOr, e1, e2) else |
| 2052 |
SetSides(newOr, e2, e1); |
| 2053 |
end else |
| 2054 |
begin |
| 2055 |
prevHotEdge := GetPrevHotEdge(e1); |
| 2056 |
newOr.isOpen := false; |
| 2057 |
// e.windDx is the winding direction of the **input** paths |
| 2058 |
// and unrelated to the winding direction of output polygons. |
| 2059 |
// Output orientation is determined by e.outrec.frontE which is |
| 2060 |
// the ascending edge (see AddLocalMinPoly). |
| 2061 |
if Assigned(prevHotEdge) then |
| 2062 |
begin |
| 2063 |
if FUsingPolytree then |
| 2064 |
SetOwner(newOr, prevHotEdge.outrec); |
| 2065 |
if OutrecIsAscending(prevHotEdge) = isNew then |
| 2066 |
SetSides(newOr, e2, e1) else |
| 2067 |
SetSides(newOr, e1, e2); |
| 2068 |
end else |
| 2069 |
begin |
| 2070 |
if isNew then |
| 2071 |
SetSides(newOr, e1, e2) else |
| 2072 |
SetSides(newOr, e2, e1); |
| 2073 |
end; |
| 2074 |
end; |
| 2075 |
Result := NewOutPt(pt, newOr); |
| 2076 |
newOr.pts := Result; |
| 2077 |
end; |
| 2078 |
//------------------------------------------------------------------------------ |
| 2079 |
|
| 2080 |
procedure DisposeOutPts(outrec: POutRec); {$IFDEF INLINING} inline; {$ENDIF} |
| 2081 |
var |
| 2082 |
op, tmpOp: POutPt; |
| 2083 |
begin |
| 2084 |
op := outrec.pts; |
| 2085 |
op.prev.next := nil; |
| 2086 |
while Assigned(op) do |
| 2087 |
begin |
| 2088 |
tmpOp := op; |
| 2089 |
op := op.next; |
| 2090 |
Dispose(tmpOp); |
| 2091 |
end; |
| 2092 |
outrec.pts := nil; |
| 2093 |
end; |
| 2094 |
//------------------------------------------------------------------------------ |
| 2095 |
|
| 2096 |
procedure TClipperBase.CleanCollinear(outRec: POutRec); |
| 2097 |
var |
| 2098 |
op2, startOp: POutPt; |
| 2099 |
begin |
| 2100 |
outRec := GetRealOutRec(outRec); |
| 2101 |
if not Assigned(outRec) or outRec.isOpen then Exit; |
| 2102 |
if not IsValidClosedPath(outRec.pts) then |
| 2103 |
begin |
| 2104 |
DisposeOutPts(outRec); |
| 2105 |
Exit; |
| 2106 |
end; |
| 2107 |
|
| 2108 |
startOp := outRec.pts; |
| 2109 |
op2 := startOp; |
| 2110 |
while true do |
| 2111 |
begin |
| 2112 |
// trim if collinear AND one of |
| 2113 |
// a duplicate point OR |
| 2114 |
// not preserving collinear points OR |
| 2115 |
// is a 180 degree 'spike' |
| 2116 |
if IsCollinear(op2.prev.pt, op2.pt, op2.next.pt) and |
| 2117 |
(PointsEqual(op2.pt,op2.prev.pt) or |
| 2118 |
PointsEqual(op2.pt,op2.next.pt) or |
| 2119 |
not FPreserveCollinear or |
| 2120 |
(DotProduct(op2.prev.pt, op2.pt, op2.next.pt) < 0)) then |
| 2121 |
begin |
| 2122 |
if op2 = outRec.pts then outRec.pts := op2.prev; |
| 2123 |
op2 := DisposeOutPt(op2); |
| 2124 |
if not IsValidClosedPath(op2) then |
| 2125 |
begin |
| 2126 |
DisposeOutPts(outRec); |
| 2127 |
Exit; |
| 2128 |
end; |
| 2129 |
startOp := op2; |
| 2130 |
Continue; |
| 2131 |
end; |
| 2132 |
op2 := op2.next; |
| 2133 |
if op2 = startOp then Break; |
| 2134 |
end; |
| 2135 |
FixSelfIntersects(outRec); |
| 2136 |
end; |
| 2137 |
//------------------------------------------------------------------------------ |
| 2138 |
|
| 2139 |
procedure AddSplit(oldOr, newOr: POutRec); |
| 2140 |
var |
| 2141 |
i: integer; |
| 2142 |
begin |
| 2143 |
i := Length(oldOr.splits); |
| 2144 |
SetLength(oldOr.splits, i +1); |
| 2145 |
oldOr.splits[i] := newOr; |
| 2146 |
end; |
| 2147 |
//------------------------------------------------------------------------------ |
| 2148 |
|
| 2149 |
procedure TClipperBase.DoSplitOp(outrec: POutRec; splitOp: POutPt); |
| 2150 |
var |
| 2151 |
newOp, newOp2, prevOp, nextNextOp: POutPt; |
| 2152 |
ip: TPoint64; |
| 2153 |
area1, area2, absArea1, absArea2: double; |
| 2154 |
newOutRec: POutRec; |
| 2155 |
begin |
| 2156 |
// splitOp.prev <=> splitOp && |
| 2157 |
// splitOp.next <=> splitOp.next.next are intersecting |
| 2158 |
prevOp := splitOp.prev; |
| 2159 |
nextNextOp := splitOp.next.next; |
| 2160 |
outrec.pts := prevOp; |
| 2161 |
GetSegmentIntersectPt( |
| 2162 |
prevOp.pt, splitOp.pt, splitOp.next.pt, nextNextOp.pt, ip); |
| 2163 |
{$IFDEF USINGZ} |
| 2164 |
if Assigned(fZCallback) then |
| 2165 |
fZCallback(prevOp.Pt, splitOp.Pt, splitOp.Next.Pt, nextNextOp.Pt, ip); |
| 2166 |
{$ENDIF} |
| 2167 |
area1 := Area(outrec.pts); |
| 2168 |
absArea1 := abs(area1); |
| 2169 |
|
| 2170 |
if absArea1 < 2 then |
| 2171 |
begin |
| 2172 |
DisposeOutPts(outrec); |
| 2173 |
Exit; |
| 2174 |
end; |
| 2175 |
|
| 2176 |
area2 := AreaTriangle(ip, splitOp.pt, splitOp.next.pt); |
| 2177 |
absArea2 := abs(area2); |
| 2178 |
|
| 2179 |
// de-link splitOp and splitOp.next from the path |
| 2180 |
// while inserting the intersection point |
| 2181 |
if PointsEqual(ip, prevOp.pt) or |
| 2182 |
PointsEqual(ip, nextNextOp.pt) then |
| 2183 |
begin |
| 2184 |
nextNextOp.prev := prevOp; |
| 2185 |
prevOp.next := nextNextOp; |
| 2186 |
end else |
| 2187 |
begin |
| 2188 |
newOp2 := NewOutPt(ip, outrec, prevOp, nextNextOp); |
| 2189 |
nextNextOp.prev := newOp2; |
| 2190 |
prevOp.next := newOp2; |
| 2191 |
end; |
| 2192 |
|
| 2193 |
// nb: area1 is the path's area *before* splitting, whereas area2 is |
| 2194 |
// the area of the triangle containing splitOp & splitOp.next. |
| 2195 |
// So the only way for these areas to have the same sign is if |
| 2196 |
// the split triangle is larger than the path containing prevOp or |
| 2197 |
// if there's more than one self-intersection. |
| 2198 |
if (absArea2 > 1) and |
| 2199 |
((absArea2 > absArea1) or |
| 2200 |
((area2 > 0) = (area1 > 0))) then |
| 2201 |
begin |
| 2202 |
newOutRec := FOutRecList.Add; |
| 2203 |
newOutRec.owner := outrec.owner; |
| 2204 |
|
| 2205 |
splitOp.outrec := newOutRec; |
| 2206 |
splitOp.next.outrec := newOutRec; |
| 2207 |
newOp := NewOutPt(ip, newOutRec, splitOp.next, splitOp); |
| 2208 |
splitOp.prev := newOp; |
| 2209 |
splitOp.next.next := newOp; |
| 2210 |
newOutRec.pts := newOp; |
| 2211 |
|
| 2212 |
if FUsingPolytree then |
| 2213 |
begin |
| 2214 |
if (Path1InsidePath2(prevOp, newOp)) then |
| 2215 |
AddSplit(newOutRec, outrec) else |
| 2216 |
AddSplit(outrec, newOutRec); |
| 2217 |
end; |
| 2218 |
|
| 2219 |
end else |
| 2220 |
begin |
| 2221 |
Dispose(splitOp.next); |
| 2222 |
Dispose(splitOp); |
| 2223 |
end; |
| 2224 |
end; |
| 2225 |
//------------------------------------------------------------------------------ |
| 2226 |
|
| 2227 |
procedure TClipperBase.FixSelfIntersects(outrec: POutRec); |
| 2228 |
var |
| 2229 |
op2: POutPt; |
| 2230 |
begin |
| 2231 |
op2 := outrec.pts; |
| 2232 |
while true do |
| 2233 |
begin |
| 2234 |
// triangles can't self-intersect |
| 2235 |
if (op2.prev = op2.next.next) then |
| 2236 |
Break |
| 2237 |
else if SegmentsIntersect(op2.prev.pt, op2.pt, |
| 2238 |
op2.next.pt, op2.next.next.pt) then |
| 2239 |
begin |
| 2240 |
DoSplitOp(outrec, op2); |
| 2241 |
if not assigned(outrec.pts) then Break; |
| 2242 |
op2 := outrec.pts; |
| 2243 |
Continue; |
| 2244 |
end else |
| 2245 |
op2 := op2.next; |
| 2246 |
if (op2 = outrec.pts) then Break; |
| 2247 |
end; |
| 2248 |
end; |
| 2249 |
//------------------------------------------------------------------------------ |
| 2250 |
|
| 2251 |
function TClipperBase.AddLocalMaxPoly(e1, e2: PActive; const pt: TPoint64): POutPt; |
| 2252 |
var |
| 2253 |
e: PActive; |
| 2254 |
outRec: POutRec; |
| 2255 |
begin |
| 2256 |
|
| 2257 |
if IsJoined(e1) then UndoJoin(e1, pt); |
| 2258 |
if IsJoined(e2) then UndoJoin(e2, pt); |
| 2259 |
|
| 2260 |
if (IsFront(e1) = IsFront(e2)) then |
| 2261 |
begin |
| 2262 |
if IsOpenEnd(e1.vertTop) then |
| 2263 |
SwapFrontBackSides(e1.outrec) |
| 2264 |
else if IsOpenEnd(e2.vertTop) then |
| 2265 |
SwapFrontBackSides(e2.outrec) |
| 2266 |
else |
| 2267 |
begin |
| 2268 |
FSucceeded := false; |
| 2269 |
Result := nil; |
| 2270 |
Exit; |
| 2271 |
end; |
| 2272 |
end; |
| 2273 |
|
| 2274 |
Result := AddOutPt(e1, pt); |
| 2275 |
if (e1.outrec = e2.outrec) then |
| 2276 |
begin |
| 2277 |
outRec := e1.outrec; |
| 2278 |
outRec.pts := Result; |
| 2279 |
|
| 2280 |
if FUsingPolytree then |
| 2281 |
begin |
| 2282 |
e := GetPrevHotEdge(e1); |
| 2283 |
if not Assigned(e) then |
| 2284 |
outRec.owner := nil else |
| 2285 |
SetOwner(outRec, e.outrec); |
| 2286 |
// nb: outRec.owner here is likely NOT the real |
| 2287 |
// owner but this will be checked in DeepCheckOwner() |
| 2288 |
end; |
| 2289 |
UncoupleOutRec(e1); |
| 2290 |
end |
| 2291 |
else if IsOpen(e1) then |
| 2292 |
begin |
| 2293 |
// preserve the winding orientation of Outrec |
| 2294 |
if e1.windDx < 0 then |
| 2295 |
JoinOutrecPaths(e1, e2) else |
| 2296 |
JoinOutrecPaths(e2, e1); |
| 2297 |
end |
| 2298 |
else if e1.outrec.idx < e2.outrec.idx then |
| 2299 |
JoinOutrecPaths(e1, e2) |
| 2300 |
else |
| 2301 |
JoinOutrecPaths(e2, e1); |
| 2302 |
end; |
| 2303 |
//------------------------------------------------------------------------------ |
| 2304 |
|
| 2305 |
procedure TClipperBase.JoinOutrecPaths(e1, e2: PActive); |
| 2306 |
var |
| 2307 |
p1_start, p1_end, p2_start, p2_end: POutPt; |
| 2308 |
begin |
| 2309 |
// join e2 outrec path onto e1 outrec path and then delete e2 outrec path |
| 2310 |
// pointers. (see joining_outpt.svg) |
| 2311 |
p1_start := e1.outrec.pts; |
| 2312 |
p2_start := e2.outrec.pts; |
| 2313 |
p1_end := p1_start.next; |
| 2314 |
p2_end := p2_start.next; |
| 2315 |
|
| 2316 |
if IsFront(e1) then |
| 2317 |
begin |
| 2318 |
p2_end.prev := p1_start; |
| 2319 |
p1_start.next := p2_end; |
| 2320 |
p2_start.next := p1_end; |
| 2321 |
p1_end.prev := p2_start; |
| 2322 |
e1.outrec.pts := p2_start; |
| 2323 |
// nb: if IsOpen(e1) then e1 & e2 must be a 'maximaPair' |
| 2324 |
e1.outrec.frontE := e2.outrec.frontE; |
| 2325 |
if Assigned(e1.outrec.frontE) then |
| 2326 |
e1.outrec.frontE.outrec := e1.outrec; |
| 2327 |
end else |
| 2328 |
begin |
| 2329 |
p1_end.prev := p2_start; |
| 2330 |
p2_start.next := p1_end; |
| 2331 |
p1_start.next := p2_end; |
| 2332 |
p2_end.prev := p1_start; |
| 2333 |
|
| 2334 |
e1.outrec.backE := e2.outrec.backE; |
| 2335 |
if Assigned(e1.outrec.backE) then |
| 2336 |
e1.outrec.backE.outrec := e1.outrec; |
| 2337 |
end; |
| 2338 |
|
| 2339 |
// after joining, the e2.OutRec mustn't contains vertices |
| 2340 |
e2.outrec.frontE := nil; |
| 2341 |
e2.outrec.backE := nil; |
| 2342 |
e2.outrec.pts := nil; |
| 2343 |
|
| 2344 |
if IsOpenEnd(e1.vertTop) then |
| 2345 |
begin |
| 2346 |
e2.outrec.pts := e1.outrec.pts; |
| 2347 |
e1.outrec.pts := nil; |
| 2348 |
end |
| 2349 |
else |
| 2350 |
SetOwner(e2.outrec, e1.outrec); |
| 2351 |
|
| 2352 |
// and e1 and e2 are maxima and are about to be dropped from the Actives list. |
| 2353 |
e1.outrec := nil; |
| 2354 |
e2.outrec := nil; |
| 2355 |
end; |
| 2356 |
//------------------------------------------------------------------------------ |
| 2357 |
|
| 2358 |
procedure TClipperBase.UndoJoin(e: PActive; const currPt: TPoint64); |
| 2359 |
begin |
| 2360 |
if e.joinedWith = jwRight then |
| 2361 |
begin |
| 2362 |
e.nextInAEL.joinedWith := jwNone; |
| 2363 |
e.joinedWith := jwNone; |
| 2364 |
AddLocalMinPoly(e, e.nextInAEL, currPt, true); |
| 2365 |
end else |
| 2366 |
begin |
| 2367 |
e.prevInAEL.joinedWith := jwNone; |
| 2368 |
e.joinedWith := jwNone; |
| 2369 |
AddLocalMinPoly(e.prevInAEL, e, currPt, true); |
| 2370 |
end; |
| 2371 |
end; |
| 2372 |
//------------------------------------------------------------------------------ |
| 2373 |
|
| 2374 |
procedure TClipperBase.CheckJoinLeft(e: PActive; |
| 2375 |
const pt: TPoint64; checkCurrX: Boolean); |
| 2376 |
var |
| 2377 |
prev: PActive; |
| 2378 |
begin |
| 2379 |
prev := e.prevInAEL; |
| 2380 |
if not Assigned(prev) or |
| 2381 |
not IsHotEdge(e) or not IsHotEdge(prev) or |
| 2382 |
IsHorizontal(e) or IsHorizontal(prev) or |
| 2383 |
IsOpen(e) or IsOpen(prev) then Exit; |
| 2384 |
if ((pt.Y < e.top.Y +2) or (pt.Y < prev.top.Y +2)) and |
| 2385 |
((e.bot.Y > pt.Y) or (prev.bot.Y > pt.Y)) then Exit; // (#490) |
| 2386 |
|
| 2387 |
if checkCurrX then |
| 2388 |
begin |
| 2389 |
if PerpendicDistFromLineSqrd(pt, prev.bot, prev.top) > 0.25 then Exit |
| 2390 |
end else if (e.currX <> prev.currX) then Exit; |
| 2391 |
|
| 2392 |
if not IsCollinear(e.top, pt, prev.top) then Exit; |
| 2393 |
|
| 2394 |
if (e.outrec.idx = prev.outrec.idx) then |
| 2395 |
AddLocalMaxPoly(prev, e, pt) |
| 2396 |
else if e.outrec.idx < prev.outrec.idx then |
| 2397 |
JoinOutrecPaths(e, prev) |
| 2398 |
else |
| 2399 |
JoinOutrecPaths(prev, e); |
| 2400 |
prev.joinedWith := jwRight; |
| 2401 |
e.joinedWith := jwLeft; |
| 2402 |
end; |
| 2403 |
//------------------------------------------------------------------------------ |
| 2404 |
|
| 2405 |
procedure TClipperBase.CheckJoinRight(e: PActive; |
| 2406 |
const pt: TPoint64; checkCurrX: Boolean); |
| 2407 |
var |
| 2408 |
next: PActive; |
| 2409 |
begin |
| 2410 |
next := e.nextInAEL; |
| 2411 |
if not Assigned(next) or |
| 2412 |
not IsHotEdge(e) or not IsHotEdge(next) or |
| 2413 |
IsHorizontal(e) or IsHorizontal(next) or |
| 2414 |
IsOpen(e) or IsOpen(next) then Exit; |
| 2415 |
if ((pt.Y < e.top.Y +2) or (pt.Y < next.top.Y +2)) and |
| 2416 |
((e.bot.Y > pt.Y) or (next.bot.Y > pt.Y)) then Exit; // (#490) |
| 2417 |
|
| 2418 |
if (checkCurrX) then |
| 2419 |
begin |
| 2420 |
if PerpendicDistFromLineSqrd(pt, next.bot, next.top) > 0.25 then Exit |
| 2421 |
end |
| 2422 |
else if (e.currX <> next.currX) then Exit; |
| 2423 |
|
| 2424 |
if not IsCollinear(e.top, pt, next.top) then Exit; |
| 2425 |
if e.outrec.idx = next.outrec.idx then |
| 2426 |
AddLocalMaxPoly(e, next, pt) |
| 2427 |
else if e.outrec.idx < next.outrec.idx then |
| 2428 |
JoinOutrecPaths(e, next) |
| 2429 |
else |
| 2430 |
JoinOutrecPaths(next, e); |
| 2431 |
|
| 2432 |
e.joinedWith := jwRight; |
| 2433 |
next.joinedWith := jwLeft; |
| 2434 |
end; |
| 2435 |
//------------------------------------------------------------------------------ |
| 2436 |
|
| 2437 |
function TClipperBase.AddOutPt(e: PActive; const pt: TPoint64): POutPt; |
| 2438 |
var |
| 2439 |
opFront, opBack: POutPt; |
| 2440 |
toFront: Boolean; |
| 2441 |
outrec: POutRec; |
| 2442 |
begin |
| 2443 |
// Outrec.OutPts: a circular doubly-linked-list of POutPt where ... |
| 2444 |
// opFront[.Prev]* ~~~> opBack & opBack == opFront.Next |
| 2445 |
outrec := e.outrec; |
| 2446 |
toFront := IsFront(e); |
| 2447 |
opFront := outrec.pts; |
| 2448 |
opBack := opFront.next; |
| 2449 |
|
| 2450 |
if toFront and PointsEqual(pt, opFront.pt) then |
| 2451 |
begin |
| 2452 |
result := opFront; |
| 2453 |
end |
| 2454 |
else if not toFront and PointsEqual(pt, opBack.pt) then |
| 2455 |
begin |
| 2456 |
result := opBack; |
| 2457 |
end else |
| 2458 |
begin |
| 2459 |
Result := NewOutPt(pt, outrec, opFront, opBack); |
| 2460 |
opBack.prev := Result; |
| 2461 |
opFront.next := Result; |
| 2462 |
if toFront then outrec.pts := Result; |
| 2463 |
end; |
| 2464 |
end; |
| 2465 |
//------------------------------------------------------------------------------ |
| 2466 |
|
| 2467 |
function TClipperBase.StartOpenPath(e: PActive; const pt: TPoint64): POutPt; |
| 2468 |
var |
| 2469 |
newOr: POutRec; |
| 2470 |
begin |
| 2471 |
newOr := FOutRecList.Add; |
| 2472 |
newOr.isOpen := true; |
| 2473 |
|
| 2474 |
if e.windDx > 0 then |
| 2475 |
begin |
| 2476 |
newOr.frontE := e; |
| 2477 |
newOr.backE := nil; |
| 2478 |
end else |
| 2479 |
begin |
| 2480 |
newOr.frontE := nil; |
| 2481 |
newOr.backE := e; |
| 2482 |
end; |
| 2483 |
e.outrec := newOr; |
| 2484 |
|
| 2485 |
Result := NewOutPt(pt, newOr); |
| 2486 |
newOr.pts := Result; |
| 2487 |
end; |
| 2488 |
//------------------------------------------------------------------------------ |
| 2489 |
|
| 2490 |
procedure TrimHorz(horzEdge: PActive; preserveCollinear: Boolean); |
| 2491 |
var |
| 2492 |
pt: TPoint64; |
| 2493 |
wasTrimmed: Boolean; |
| 2494 |
begin |
| 2495 |
wasTrimmed := false; |
| 2496 |
pt := NextVertex(horzEdge).pt; |
| 2497 |
while (pt.Y = horzEdge.top.Y) do |
| 2498 |
begin |
| 2499 |
// always trim 180 deg. spikes (in closed paths) |
| 2500 |
// but otherwise break if preserveCollinear = true |
| 2501 |
if preserveCollinear and |
| 2502 |
((pt.X < horzEdge.top.X) <> (horzEdge.bot.X < horzEdge.top.X)) then |
| 2503 |
break; |
| 2504 |
|
| 2505 |
horzEdge.vertTop := NextVertex(horzEdge); |
| 2506 |
horzEdge.top := pt; |
| 2507 |
wasTrimmed := true; |
| 2508 |
if IsMaxima(horzEdge) then Break; |
| 2509 |
pt := NextVertex(horzEdge).pt; |
| 2510 |
end; |
| 2511 |
if wasTrimmed then SetDx(horzEdge); // +/-infinity |
| 2512 |
end; |
| 2513 |
//------------------------------------------------------------------------------ |
| 2514 |
|
| 2515 |
procedure TClipperBase.UpdateEdgeIntoAEL(var e: PActive); |
| 2516 |
begin |
| 2517 |
e.bot := e.top; |
| 2518 |
e.vertTop := NextVertex(e); |
| 2519 |
e.top := e.vertTop.pt; |
| 2520 |
e.currX := e.bot.X; |
| 2521 |
SetDx(e); |
| 2522 |
|
| 2523 |
if IsJoined(e) then UndoJoin(e, e.bot); |
| 2524 |
|
| 2525 |
if IsHorizontal(e) then |
| 2526 |
begin |
| 2527 |
if not IsOpen(e) then TrimHorz(e, PreserveCollinear); |
| 2528 |
Exit; |
| 2529 |
end; |
| 2530 |
InsertScanLine(e.top.Y); |
| 2531 |
|
| 2532 |
CheckJoinLeft(e, e.bot); |
| 2533 |
CheckJoinRight(e, e.bot, true); // (#500) |
| 2534 |
end; |
| 2535 |
//------------------------------------------------------------------------------ |
| 2536 |
|
| 2537 |
function FindEdgeWithMatchingLocMin(e: PActive): PActive; |
| 2538 |
begin |
| 2539 |
Result := e.nextInAEL; |
| 2540 |
while Assigned(Result) do |
| 2541 |
begin |
| 2542 |
if (Result.locMin = e.locMin) then Exit; |
| 2543 |
if not IsHorizontal(Result) and |
| 2544 |
not PointsEqual(e.bot, Result.bot) then Result := nil |
| 2545 |
else Result := Result.nextInAEL; |
| 2546 |
end; |
| 2547 |
Result := e.prevInAEL; |
| 2548 |
while Assigned(Result) do |
| 2549 |
begin |
| 2550 |
if (Result.locMin = e.locMin) then Exit; |
| 2551 |
if not IsHorizontal(Result) and |
| 2552 |
not PointsEqual(e.bot, Result.bot) then Result := nil |
| 2553 |
else |
| 2554 |
Result := Result.prevInAEL; |
| 2555 |
end; |
| 2556 |
end; |
| 2557 |
//------------------------------------------------------------------------------ |
| 2558 |
|
| 2559 |
{$IFNDEF USINGZ} |
| 2560 |
{$HINTS OFF} |
| 2561 |
{$ENDIF} |
| 2562 |
procedure TClipperBase.IntersectEdges(e1, e2: PActive; pt: TPoint64); |
| 2563 |
var |
| 2564 |
e1WindCnt, e2WindCnt, e1WindCnt2, e2WindCnt2: Integer; |
| 2565 |
e3: PActive; |
| 2566 |
op, op2: POutPt; |
| 2567 |
begin |
| 2568 |
// MANAGE OPEN PATH INTERSECTIONS SEPARATELY ... |
| 2569 |
if FHasOpenPaths and (IsOpen(e1) or IsOpen(e2)) then |
| 2570 |
begin |
| 2571 |
if IsOpen(e1) and IsOpen(e2) then Exit; |
| 2572 |
// the following line avoids duplicating quite a bit of code |
| 2573 |
if IsOpen(e2) then SwapActives(e1, e2); |
| 2574 |
|
| 2575 |
// e1 is open and e2 is closed |
| 2576 |
|
| 2577 |
if IsJoined(e2) then UndoJoin(e2, pt); // needed for safety |
| 2578 |
|
| 2579 |
case FClipType of |
| 2580 |
ctUnion: if not IsHotEdge(e2) then Exit; |
| 2581 |
else if e2.locMin.polytype = ptSubject then Exit; |
| 2582 |
end; |
| 2583 |
case FFillRule of |
| 2584 |
frPositive: if e2.windCnt <> 1 then Exit; |
| 2585 |
frNegative: if e2.windCnt <> -1 then Exit; |
| 2586 |
else if (abs(e2.windCnt) <> 1) then Exit; |
| 2587 |
end; |
| 2588 |
|
| 2589 |
// toggle contribution ... |
| 2590 |
if IsHotEdge(e1) then |
| 2591 |
begin |
| 2592 |
op := AddOutPt(e1, pt); |
| 2593 |
if IsFront(e1) then |
| 2594 |
e1.outrec.frontE := nil else |
| 2595 |
e1.outrec.backE := nil; |
| 2596 |
e1.outrec := nil; |
| 2597 |
// e1 is no longer 'hot' |
| 2598 |
end |
| 2599 |
// horizontal edges can pass under open paths at a LocMins |
| 2600 |
else if PointsEqual(pt, e1.locMin.vertex.pt) and |
| 2601 |
(e1.locMin.vertex.flags * [vfOpenStart, vfOpenEnd] = []) then |
| 2602 |
begin |
| 2603 |
//todo: recheck if this code block is still needed |
| 2604 |
|
| 2605 |
// find the other side of the LocMin and |
| 2606 |
// if it's 'hot' join up with it ... |
| 2607 |
e3 := FindEdgeWithMatchingLocMin(e1); |
| 2608 |
if assigned(e3) and IsHotEdge(e3) then |
| 2609 |
begin |
| 2610 |
e1.outrec := e3.outrec; |
| 2611 |
if e1.windDx > 0 then |
| 2612 |
SetSides(e3.outrec, e1, e3) else |
| 2613 |
SetSides(e3.outrec, e3, e1); |
| 2614 |
Exit; |
| 2615 |
end else |
| 2616 |
op := StartOpenPath(e1, pt); |
| 2617 |
end else |
| 2618 |
op := StartOpenPath(e1, pt); |
| 2619 |
|
| 2620 |
{$IFDEF USINGZ} |
| 2621 |
SetZ(e1, e2, op.pt); |
| 2622 |
{$ENDIF} |
| 2623 |
Exit; |
| 2624 |
end; |
| 2625 |
|
| 2626 |
// MANAGING CLOSED PATHS FROM HERE ON |
| 2627 |
if IsJoined(e1) then UndoJoin(e1, pt); |
| 2628 |
if IsJoined(e2) then UndoJoin(e2, pt); |
| 2629 |
|
| 2630 |
// FIRST, UPDATE WINDING COUNTS |
| 2631 |
if IsSamePolyType(e1, e2) then |
| 2632 |
begin |
| 2633 |
if FFillRule = frEvenOdd then |
| 2634 |
begin |
| 2635 |
e1WindCnt := e1.windCnt; |
| 2636 |
e1.windCnt := e2.windCnt; |
| 2637 |
e2.windCnt := e1WindCnt; |
| 2638 |
end else |
| 2639 |
begin |
| 2640 |
e1.windCnt := Iif(e1.windCnt + e2.windDx = 0, |
| 2641 |
-e1.windCnt, e1.windCnt + e2.windDx); |
| 2642 |
e2.windCnt := Iif(e2.windCnt - e1.windDx = 0, |
| 2643 |
-e2.windCnt, e2.windCnt - e1.windDx); |
| 2644 |
end; |
| 2645 |
end else |
| 2646 |
begin |
| 2647 |
if FFillRule <> frEvenOdd then Inc(e1.windCnt2, e2.windDx) |
| 2648 |
else if e1.windCnt2 = 0 then e1.windCnt2 := 1 |
| 2649 |
else e1.windCnt2 := 0; |
| 2650 |
|
| 2651 |
if FFillRule <> frEvenOdd then Dec(e2.windCnt2, e1.windDx) |
| 2652 |
else if e2.windCnt2 = 0 then e2.windCnt2 := 1 |
| 2653 |
else e2.windCnt2 := 0; |
| 2654 |
end; |
| 2655 |
|
| 2656 |
case FFillRule of |
| 2657 |
frPositive: |
| 2658 |
begin |
| 2659 |
e1WindCnt := e1.windCnt; |
| 2660 |
e2WindCnt := e2.windCnt; |
| 2661 |
end; |
| 2662 |
frNegative: |
| 2663 |
begin |
| 2664 |
e1WindCnt := -e1.windCnt; |
| 2665 |
e2WindCnt := -e2.windCnt; |
| 2666 |
end; |
| 2667 |
else |
| 2668 |
begin |
| 2669 |
e1WindCnt := abs(e1.windCnt); |
| 2670 |
e2WindCnt := abs(e2.windCnt); |
| 2671 |
end; |
| 2672 |
end; |
| 2673 |
|
| 2674 |
if (not IsHotEdge(e1) and not (e1WindCnt in [0,1])) or |
| 2675 |
(not IsHotEdge(e2) and not (e2WindCnt in [0,1])) then Exit; |
| 2676 |
|
| 2677 |
// NOW PROCESS THE INTERSECTION |
| 2678 |
|
| 2679 |
// if both edges are 'hot' ... |
| 2680 |
if IsHotEdge(e1) and IsHotEdge(e2) then |
| 2681 |
begin |
| 2682 |
if not (e1WindCnt in [0,1]) or not (e2WindCnt in [0,1]) or |
| 2683 |
(not IsSamePolyType(e1, e2) and (fClipType <> ctXor)) then |
| 2684 |
begin |
| 2685 |
op := AddLocalMaxPoly(e1, e2, pt); |
| 2686 |
{$IFDEF USINGZ} |
| 2687 |
if Assigned(op) then SetZ(e1, e2, op.pt); |
| 2688 |
{$ENDIF} |
| 2689 |
|
| 2690 |
end else if IsFront(e1) or (e1.outrec = e2.outrec) then |
| 2691 |
begin |
| 2692 |
// this 'else if' condition isn't strictly needed but |
| 2693 |
// it's sensible to split polygons that ony touch at |
| 2694 |
// a common vertex (not at common edges). |
| 2695 |
op := AddLocalMaxPoly(e1, e2, pt); |
| 2696 |
{$IFDEF USINGZ} |
| 2697 |
op2 := AddLocalMinPoly(e1, e2, pt); |
| 2698 |
if Assigned(op) then SetZ(e1, e2, op.pt); |
| 2699 |
SetZ(e1, e2, op2.pt); |
| 2700 |
{$ELSE} |
| 2701 |
AddLocalMinPoly(e1, e2, pt); |
| 2702 |
{$ENDIF} |
| 2703 |
end else |
| 2704 |
begin |
| 2705 |
// can't treat as maxima & minima |
| 2706 |
op := AddOutPt(e1, pt); |
| 2707 |
{$IFDEF USINGZ} |
| 2708 |
op2 := AddOutPt(e2, pt); |
| 2709 |
SetZ(e1, e2, op.pt); |
| 2710 |
SetZ(e1, e2, op2.pt); |
| 2711 |
{$ELSE} |
| 2712 |
AddOutPt(e2, pt); |
| 2713 |
{$ENDIF} |
| 2714 |
SwapOutRecs(e1, e2); |
| 2715 |
end; |
| 2716 |
end |
| 2717 |
|
| 2718 |
// if one or other edge is 'hot' ... |
| 2719 |
else if IsHotEdge(e1) then |
| 2720 |
begin |
| 2721 |
op := AddOutPt(e1, pt); |
| 2722 |
{$IFDEF USINGZ} |
| 2723 |
SetZ(e1, e2, op.pt); |
| 2724 |
{$ENDIF} |
| 2725 |
SwapOutRecs(e1, e2); |
| 2726 |
end |
| 2727 |
else if IsHotEdge(e2) then |
| 2728 |
begin |
| 2729 |
op := AddOutPt(e2, pt); |
| 2730 |
{$IFDEF USINGZ} |
| 2731 |
SetZ(e1, e2, op.pt); |
| 2732 |
{$ENDIF} |
| 2733 |
SwapOutRecs(e1, e2); |
| 2734 |
end |
| 2735 |
|
| 2736 |
// else neither edge is 'hot' |
| 2737 |
else |
| 2738 |
begin |
| 2739 |
case FFillRule of |
| 2740 |
frPositive: |
| 2741 |
begin |
| 2742 |
e1WindCnt2 := e1.windCnt2; |
| 2743 |
e2WindCnt2 := e2.windCnt2; |
| 2744 |
end; |
| 2745 |
frNegative: |
| 2746 |
begin |
| 2747 |
e1WindCnt2 := -e1.windCnt2; |
| 2748 |
e2WindCnt2 := -e2.windCnt2; |
| 2749 |
end; |
| 2750 |
else |
| 2751 |
begin |
| 2752 |
e1WindCnt2 := abs(e1.windCnt2); |
| 2753 |
e2WindCnt2 := abs(e2.windCnt2); |
| 2754 |
end; |
| 2755 |
end; |
| 2756 |
|
| 2757 |
if not IsSamePolyType(e1, e2) then |
| 2758 |
begin |
| 2759 |
op := AddLocalMinPoly(e1, e2, pt, false); |
| 2760 |
{$IFDEF USINGZ} |
| 2761 |
SetZ(e1, e2, op.pt); |
| 2762 |
{$ENDIF} |
| 2763 |
end |
| 2764 |
else if (e1WindCnt = 1) and (e2WindCnt = 1) then |
| 2765 |
begin |
| 2766 |
op := nil; |
| 2767 |
case FClipType of |
| 2768 |
ctIntersection: |
| 2769 |
if (e1WindCnt2 <= 0) or (e2WindCnt2 <= 0) then Exit |
| 2770 |
else op := AddLocalMinPoly(e1, e2, pt, false); |
| 2771 |
ctUnion: |
| 2772 |
if (e1WindCnt2 <= 0) and (e2WindCnt2 <= 0) then |
| 2773 |
op := AddLocalMinPoly(e1, e2, pt, false); |
| 2774 |
ctDifference: |
| 2775 |
if ((GetPolyType(e1) = ptClip) and |
| 2776 |
(e1WindCnt2 > 0) and (e2WindCnt2 > 0)) or |
| 2777 |
((GetPolyType(e1) = ptSubject) and |
| 2778 |
(e1WindCnt2 <= 0) and (e2WindCnt2 <= 0)) then |
| 2779 |
op := AddLocalMinPoly(e1, e2, pt, false); |
| 2780 |
else // xOr |
| 2781 |
op := AddLocalMinPoly(e1, e2, pt, false); |
| 2782 |
end; |
| 2783 |
{$IFDEF USINGZ} |
| 2784 |
if assigned(op) then SetZ(e1, e2, op.pt); |
| 2785 |
{$ENDIF} |
| 2786 |
end; |
| 2787 |
end; |
| 2788 |
end; |
| 2789 |
//------------------------------------------------------------------------------ |
| 2790 |
{$IFNDEF USINGZ} |
| 2791 |
{$HINTS ON} |
| 2792 |
{$ENDIF} |
| 2793 |
|
| 2794 |
procedure TClipperBase.DeleteEdges(var e: PActive); |
| 2795 |
var |
| 2796 |
e2: PActive; |
| 2797 |
begin |
| 2798 |
while Assigned(e) do |
| 2799 |
begin |
| 2800 |
e2 := e; |
| 2801 |
e := e.nextInAEL; |
| 2802 |
Dispose(e2); |
| 2803 |
end; |
| 2804 |
end; |
| 2805 |
//------------------------------------------------------------------------------ |
| 2806 |
|
| 2807 |
procedure TClipperBase.DeleteFromAEL(e: PActive); |
| 2808 |
var |
| 2809 |
aelPrev, aelNext: PActive; |
| 2810 |
begin |
| 2811 |
aelPrev := e.prevInAEL; |
| 2812 |
aelNext := e.nextInAEL; |
| 2813 |
if not Assigned(aelPrev) and not Assigned(aelNext) and |
| 2814 |
(e <> FActives) then Exit; // already deleted |
| 2815 |
if Assigned(aelPrev) then aelPrev.nextInAEL := aelNext |
| 2816 |
else FActives := aelNext; |
| 2817 |
if Assigned(aelNext) then aelNext.prevInAEL := aelPrev; |
| 2818 |
Dispose(e); |
| 2819 |
end; |
| 2820 |
//------------------------------------------------------------------------------ |
| 2821 |
|
| 2822 |
procedure TClipperBase.AdjustCurrXAndCopyToSEL(topY: Int64); |
| 2823 |
var |
| 2824 |
e: PActive; |
| 2825 |
begin |
| 2826 |
FSel := FActives; |
| 2827 |
e := FActives; |
| 2828 |
while Assigned(e) do |
| 2829 |
begin |
| 2830 |
e.prevInSEL := e.prevInAEL; |
| 2831 |
e.nextInSEL := e.nextInAEL; |
| 2832 |
e.jump := e.nextInSEL; |
| 2833 |
if (e.joinedWith = jwLeft) then |
| 2834 |
e.currX := e.prevInAEL.currX // this also avoids complications |
| 2835 |
else |
| 2836 |
e.currX := TopX(e, topY); |
| 2837 |
e := e.nextInAEL; |
| 2838 |
end; |
| 2839 |
end; |
| 2840 |
//------------------------------------------------------------------------------ |
| 2841 |
|
| 2842 |
procedure TClipperBase.ExecuteInternal(clipType: TClipType; |
| 2843 |
fillRule: TFillRule; usingPolytree: Boolean); |
| 2844 |
var |
| 2845 |
Y: Int64; |
| 2846 |
e: PActive; |
| 2847 |
begin |
| 2848 |
if clipType = ctNoClip then Exit; |
| 2849 |
FFillRule := fillRule; |
| 2850 |
FClipType := clipType; |
| 2851 |
Reset; |
| 2852 |
if not PopScanLine(Y) then Exit; |
| 2853 |
while FSucceeded do |
| 2854 |
begin |
| 2855 |
InsertLocalMinimaIntoAEL(Y); |
| 2856 |
while PopHorz(e) do DoHorizontal(e); |
| 2857 |
if FHorzSegList.Count > 0 then |
| 2858 |
begin |
| 2859 |
if FHorzSegList.Count > 1 then ConvertHorzSegsToJoins; |
| 2860 |
FHorzSegList.Clear; |
| 2861 |
end; |
| 2862 |
FBotY := Y; // FBotY == bottom of current scanbeam |
| 2863 |
if not PopScanLine(Y) then Break; // Y == top of current scanbeam |
| 2864 |
DoIntersections(Y); |
| 2865 |
DoTopOfScanbeam(Y); |
| 2866 |
while PopHorz(e) do DoHorizontal(e); |
| 2867 |
end; |
| 2868 |
if Succeeded then ProcessHorzJoins; |
| 2869 |
end; |
| 2870 |
//------------------------------------------------------------------------------ |
| 2871 |
|
| 2872 |
procedure FixOutRecPts(outrec: POutrec); overload; |
| 2873 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 2874 |
var |
| 2875 |
op: POutPt; |
| 2876 |
begin |
| 2877 |
op := outrec.pts; |
| 2878 |
repeat |
| 2879 |
op.outrec := outrec; |
| 2880 |
op := op.next; |
| 2881 |
until op = outrec.pts; |
| 2882 |
end; |
| 2883 |
//------------------------------------------------------------------------------ |
| 2884 |
|
| 2885 |
procedure SetOutRecPts(op: POutPt; newOR: POutrec); overload; |
| 2886 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 2887 |
var |
| 2888 |
op2: POutPt; |
| 2889 |
begin |
| 2890 |
op2 := op; |
| 2891 |
repeat |
| 2892 |
op2.outrec := newOR; |
| 2893 |
op2 := op2.next; |
| 2894 |
until op2 = op; |
| 2895 |
end; |
| 2896 |
//------------------------------------------------------------------------------ |
| 2897 |
|
| 2898 |
function HorzOverlapWithLRSet(const left1, right1, left2, right2: TPoint64): boolean; |
| 2899 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 2900 |
begin |
| 2901 |
Result := (left1.X < right2.X) and (right1.X > left2.X); |
| 2902 |
end; |
| 2903 |
//------------------------------------------------------------------------------ |
| 2904 |
|
| 2905 |
function HorzontalsOverlap(const horz1a, horz1b, horz2a, horz2b: TPoint64): boolean; |
| 2906 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 2907 |
begin |
| 2908 |
if horz1a.X < horz1b.X then |
| 2909 |
begin |
| 2910 |
Result := Iif(horz2a.X < horz2b.X, |
| 2911 |
HorzOverlapWithLRSet(horz1a, horz1b, horz2a, horz2b), |
| 2912 |
HorzOverlapWithLRSet(horz1a, horz1b, horz2b, horz2a)); |
| 2913 |
end else |
| 2914 |
begin |
| 2915 |
Result := Iif(horz2a.X < horz2b.X, |
| 2916 |
HorzOverlapWithLRSet(horz1b, horz1a, horz2a, horz2b), |
| 2917 |
HorzOverlapWithLRSet(horz1b, horz1a, horz2b, horz2a)); |
| 2918 |
end; |
| 2919 |
end; |
| 2920 |
//------------------------------------------------------------------------------ |
| 2921 |
|
| 2922 |
procedure SetRealOutRec(op: POutPt); {$IFDEF INLINING} inline; {$ENDIF} |
| 2923 |
begin |
| 2924 |
op.outrec := GetRealOutRec(op.outrec); |
| 2925 |
end; |
| 2926 |
//------------------------------------------------------------------------------ |
| 2927 |
|
| 2928 |
function SetHorzSegHeadingForward(hs: PHorzSegment; opP, opN: POutPt): Boolean; |
| 2929 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 2930 |
begin |
| 2931 |
Result := opP.pt.X <> opN.pt.X; |
| 2932 |
if not Result then Exit; |
| 2933 |
if opP.pt.X < opN.pt.X then |
| 2934 |
begin |
| 2935 |
hs.leftOp := opP; |
| 2936 |
hs.rightOp := opN; |
| 2937 |
hs.leftToRight := true; |
| 2938 |
end else |
| 2939 |
begin |
| 2940 |
hs.leftOp := opN; |
| 2941 |
hs.rightOp := opP; |
| 2942 |
hs.leftToRight := false; |
| 2943 |
end; |
| 2944 |
end; |
| 2945 |
//------------------------------------------------------------------------------ |
| 2946 |
|
| 2947 |
function UpdateHorzSegment(hs: PHorzSegment): Boolean; |
| 2948 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 2949 |
var |
| 2950 |
op, opP, opN, opA, opZ: POutPt; |
| 2951 |
outrec: POutrec; |
| 2952 |
currY: Int64; |
| 2953 |
outrecHasEdges: Boolean; |
| 2954 |
begin |
| 2955 |
op := hs.leftOp; |
| 2956 |
outrec := GetRealOutRec(op.outrec); |
| 2957 |
outrecHasEdges := Assigned(outrec.frontE); |
| 2958 |
// nb: it's possible that both opA and opZ are below op |
| 2959 |
// (eg when there's been an intermediate maxima horz. join) |
| 2960 |
currY := op.pt.Y; |
| 2961 |
opP := op; opN := op; |
| 2962 |
if outrecHasEdges then |
| 2963 |
begin |
| 2964 |
opA := outrec.pts; |
| 2965 |
opZ := opA.next; |
| 2966 |
while (opP <> opZ) and (opP.prev.pt.Y = currY) do |
| 2967 |
opP := opP.prev; |
| 2968 |
while (opN <> opA) and (opN.next.pt.Y = currY) do |
| 2969 |
opN := opN.next; |
| 2970 |
end else |
| 2971 |
begin |
| 2972 |
while (opP.prev <> opN) and (opP.prev.pt.Y = currY) do |
| 2973 |
opP := opP.prev; |
| 2974 |
while (opN.next <> opP) and (opN.next.pt.Y = currY) do |
| 2975 |
opN := opN.next; |
| 2976 |
end; |
| 2977 |
Result := SetHorzSegHeadingForward(hs, opP, opN) and |
| 2978 |
not Assigned(hs.leftOp.horz); |
| 2979 |
if Result then hs.leftOp.horz := hs; |
| 2980 |
end; |
| 2981 |
//------------------------------------------------------------------------------ |
| 2982 |
|
| 2983 |
procedure TClipperBase.ConvertHorzSegsToJoins; |
| 2984 |
var |
| 2985 |
i, j: integer; |
| 2986 |
currY: Int64; |
| 2987 |
hs1, hs2: PHorzSegment; |
| 2988 |
begin |
| 2989 |
j := 0; |
| 2990 |
for i := 0 to FHorzSegList.Count -1 do |
| 2991 |
begin |
| 2992 |
hs1 := FHorzSegList.UnsafeGet(i); |
| 2993 |
if UpdateHorzSegment(hs1) then |
| 2994 |
begin |
| 2995 |
if (j < i) then |
| 2996 |
FHorzSegList.UnsafeSet(j, hs1); |
| 2997 |
inc(j); |
| 2998 |
end else |
| 2999 |
Dispose(hs1); |
| 3000 |
end; |
| 3001 |
FHorzSegList.Resize(j); |
| 3002 |
if j < 2 then Exit; |
| 3003 |
FHorzSegList.Sort(HorzSegListSort); |
| 3004 |
|
| 3005 |
// find overlaps |
| 3006 |
for i := 0 to FHorzSegList.Count -2 do |
| 3007 |
begin |
| 3008 |
hs1 := FHorzSegList.UnsafeGet(i); |
| 3009 |
for j := i+1 to FHorzSegList.Count -1 do |
| 3010 |
begin |
| 3011 |
hs2 := FHorzSegList.UnsafeGet(j); |
| 3012 |
|
| 3013 |
if (hs2.leftOp.pt.X >= hs1.rightOp.pt.X) or |
| 3014 |
(hs2.leftToRight = hs1.leftToRight) or |
| 3015 |
(hs2.rightOp.pt.X <= hs1.leftOp.pt.X) then Continue; |
| 3016 |
|
| 3017 |
currY := hs1.leftOp.pt.Y; |
| 3018 |
if hs1.leftToRight then |
| 3019 |
begin |
| 3020 |
while (hs1.leftOp.next.pt.Y = currY) and |
| 3021 |
(hs1.leftOp.next.pt.X <= hs2.leftOp.pt.X) do |
| 3022 |
hs1.leftOp := hs1.leftOp.next; |
| 3023 |
while (hs2.leftOp.prev.pt.Y = currY) and |
| 3024 |
(hs2.leftOp.prev.pt.X <= hs1.leftOp.pt.X) do |
| 3025 |
hs2.leftOp := hs2.leftOp.prev; |
| 3026 |
FHorzJoinList.Add( |
| 3027 |
DuplicateOp(hs1.leftOp, true), |
| 3028 |
DuplicateOp(hs2.leftOp, false)); |
| 3029 |
end else |
| 3030 |
begin |
| 3031 |
while (hs1.leftOp.prev.pt.Y = currY) and |
| 3032 |
(hs1.leftOp.prev.pt.X <= hs2.leftOp.pt.X) do |
| 3033 |
hs1.leftOp := hs1.leftOp.prev; |
| 3034 |
while (hs2.leftOp.next.pt.Y = currY) and |
| 3035 |
(hs2.leftOp.next.pt.X <= hs1.leftOp.pt.X) do |
| 3036 |
hs2.leftOp := hs2.leftOp.next; |
| 3037 |
FHorzJoinList.Add( |
| 3038 |
DuplicateOp(hs2.leftOp, true), |
| 3039 |
DuplicateOp(hs1.leftOp, false)); |
| 3040 |
end; |
| 3041 |
end; |
| 3042 |
end; |
| 3043 |
end; |
| 3044 |
//------------------------------------------------------------------------------ |
| 3045 |
|
| 3046 |
procedure MoveSplits(fromOr, toOr: POutRec); |
| 3047 |
var |
| 3048 |
i: integer; |
| 3049 |
begin |
| 3050 |
if not assigned(fromOr.splits) then Exit; |
| 3051 |
for i := 0 to High(fromOr.splits) do |
| 3052 |
AddSplit(toOr, fromOr.splits[i]); |
| 3053 |
fromOr.splits := nil; |
| 3054 |
end; |
| 3055 |
//------------------------------------------------------------------------------ |
| 3056 |
|
| 3057 |
procedure TClipperBase.ProcessHorzJoins; |
| 3058 |
var |
| 3059 |
i: integer; |
| 3060 |
or1, or2: POutRec; |
| 3061 |
op1b, op2b, tmp: POutPt; |
| 3062 |
begin |
| 3063 |
for i := 0 to FHorzJoinList.Count -1 do |
| 3064 |
with PHorzJoin(FHorzJoinList[i])^ do |
| 3065 |
begin |
| 3066 |
or1 := GetRealOutRec(op1.outrec); |
| 3067 |
or2 := GetRealOutRec(op2.outrec); |
| 3068 |
|
| 3069 |
// op1 >>> op1b |
| 3070 |
// op2 <<< op2b |
| 3071 |
op1b := op1.next; |
| 3072 |
op2b := op2.prev; |
| 3073 |
op1.next := op2; |
| 3074 |
op2.prev := op1; |
| 3075 |
op1b.prev := op2b; |
| 3076 |
op2b.next := op1b; |
| 3077 |
|
| 3078 |
if or1 = or2 then // 'join' is really a split |
| 3079 |
begin |
| 3080 |
or2 := FOutRecList.Add; |
| 3081 |
or2.pts := op1b; |
| 3082 |
FixOutRecPts(or2); |
| 3083 |
|
| 3084 |
//if or1->pts has moved to or2 then update or1->pts!! |
| 3085 |
if or1.pts.outrec = or2 then |
| 3086 |
begin |
| 3087 |
or1.pts := op1; |
| 3088 |
or1.pts.outrec := or1; |
| 3089 |
end; |
| 3090 |
|
| 3091 |
if FUsingPolytree then //#498, #520, #584, D#576, #618 |
| 3092 |
begin |
| 3093 |
if Path1InsidePath2(or1.pts, or2.pts) then |
| 3094 |
begin |
| 3095 |
//swap or1's & or2's pts |
| 3096 |
tmp := or1.pts; |
| 3097 |
or1.pts := or2.pts; |
| 3098 |
or2.pts := tmp; |
| 3099 |
FixOutRecPts(or1); |
| 3100 |
FixOutRecPts(or2); |
| 3101 |
//or2 is now inside or1 |
| 3102 |
or2.owner := or1; |
| 3103 |
end |
| 3104 |
else if Path1InsidePath2(or2.pts, or1.pts) then |
| 3105 |
begin |
| 3106 |
or2.owner := or1; |
| 3107 |
end |
| 3108 |
else |
| 3109 |
or2.owner := or1.owner; |
| 3110 |
|
| 3111 |
AddSplit(or1, or2); |
| 3112 |
end |
| 3113 |
else |
| 3114 |
or2.owner := or1; |
| 3115 |
end else |
| 3116 |
begin |
| 3117 |
or2.pts := nil; |
| 3118 |
if FUsingPolytree then |
| 3119 |
begin |
| 3120 |
SetOwner(or2, or1); |
| 3121 |
MoveSplits(or2, or1); //#618 |
| 3122 |
end else |
| 3123 |
or2.owner := or1; |
| 3124 |
end; |
| 3125 |
end; |
| 3126 |
end; |
| 3127 |
//------------------------------------------------------------------------------ |
| 3128 |
|
| 3129 |
procedure TClipperBase.DoIntersections(const topY: Int64); |
| 3130 |
begin |
| 3131 |
if BuildIntersectList(topY) then |
| 3132 |
try |
| 3133 |
ProcessIntersectList; |
| 3134 |
finally |
| 3135 |
DisposeIntersectNodes; |
| 3136 |
end; |
| 3137 |
end; |
| 3138 |
//------------------------------------------------------------------------------ |
| 3139 |
|
| 3140 |
procedure TClipperBase.DisposeIntersectNodes; |
| 3141 |
var |
| 3142 |
i: Integer; |
| 3143 |
begin |
| 3144 |
for i := 0 to FIntersectList.Count - 1 do |
| 3145 |
Dispose(PIntersectNode(UnsafeGet(FIntersectList,i))); |
| 3146 |
FIntersectList.Clear; |
| 3147 |
end; |
| 3148 |
//------------------------------------------------------------------------------ |
| 3149 |
|
| 3150 |
procedure TClipperBase.AddNewIntersectNode(e1, e2: PActive; topY: Int64); |
| 3151 |
var |
| 3152 |
ip: TPoint64; |
| 3153 |
absDx1, absDx2: double; |
| 3154 |
node: PIntersectNode; |
| 3155 |
begin |
| 3156 |
if not GetSegmentIntersectPt(e1.bot, e1.top, e2.bot, e2.top, ip) then |
| 3157 |
ip := Point64(e1.currX, topY); |
| 3158 |
// Rounding errors can occasionally place the calculated intersection |
| 3159 |
// point either below or above the scanbeam, so check and correct ... |
| 3160 |
if (ip.Y > FBotY) or (ip.Y < topY) then |
| 3161 |
begin |
| 3162 |
absDx1 := Abs(e1.dx); |
| 3163 |
absDx2 := Abs(e2.dx); |
| 3164 |
if (absDx1 > 100) and (absDx2 > 100) then |
| 3165 |
begin |
| 3166 |
if (absDx1 > absDx2) then |
| 3167 |
ip := GetClosestPointOnSegment(ip, e1.bot, e1.top) else |
| 3168 |
ip := GetClosestPointOnSegment(ip, e2.bot, e2.top); |
| 3169 |
end |
| 3170 |
else if (absDx1 > 100) then |
| 3171 |
ip := GetClosestPointOnSegment(ip, e1.bot, e1.top) |
| 3172 |
else if (absDx2 > 100) then |
| 3173 |
ip := GetClosestPointOnSegment(ip, e2.bot, e2.top) |
| 3174 |
else |
| 3175 |
begin |
| 3176 |
ip.Y := Iif(ip.Y < topY, topY , fBotY); |
| 3177 |
ip.X := Iif(absDx1 < absDx2, TopX(e1, ip.Y), TopX(e2, ip.Y)); |
| 3178 |
end; |
| 3179 |
end; |
| 3180 |
new(node); |
| 3181 |
node.active1 := e1; |
| 3182 |
node.active2 := e2; |
| 3183 |
node.pt := ip; |
| 3184 |
FIntersectList.Add(node); |
| 3185 |
end; |
| 3186 |
//------------------------------------------------------------------------------ |
| 3187 |
|
| 3188 |
function ExtractFromSEL(edge: PActive): PActive; |
| 3189 |
begin |
| 3190 |
// nb: edge.PrevInSEL is always assigned |
| 3191 |
Result := edge.nextInSEL; |
| 3192 |
if Assigned(Result) then |
| 3193 |
Result.prevInSEL := edge.prevInSEL; |
| 3194 |
edge.prevInSEL.nextInSEL := Result; |
| 3195 |
end; |
| 3196 |
//------------------------------------------------------------------------------ |
| 3197 |
|
| 3198 |
procedure Insert1Before2InSEL(edge1, edge2: PActive); |
| 3199 |
begin |
| 3200 |
edge1.prevInSEL := edge2.prevInSEL; |
| 3201 |
if Assigned(edge1.prevInSEL) then |
| 3202 |
edge1.prevInSEL.nextInSEL := edge1; |
| 3203 |
edge1.nextInSEL := edge2; |
| 3204 |
edge2.prevInSEL := edge1; |
| 3205 |
end; |
| 3206 |
//------------------------------------------------------------------------------ |
| 3207 |
|
| 3208 |
function TClipperBase.BuildIntersectList(const topY: Int64): Boolean; |
| 3209 |
var |
| 3210 |
e, base,prevBase,left,right, lend, rend: PActive; |
| 3211 |
begin |
| 3212 |
result := false; |
| 3213 |
if not Assigned(FActives) or not Assigned(FActives.nextInAEL) then Exit; |
| 3214 |
|
| 3215 |
// Calculate edge positions at the top of the current scanbeam, and from this |
| 3216 |
// we will determine the intersections required to reach these new positions. |
| 3217 |
AdjustCurrXAndCopyToSEL(topY); |
| 3218 |
|
| 3219 |
// Find all edge intersections in the current scanbeam using a stable merge |
| 3220 |
// sort that ensures only adjacent edges are intersecting. Intersect info is |
| 3221 |
// stored in FIntersectList ready to be processed in ProcessIntersectList. |
| 3222 |
left := FSel; |
| 3223 |
while Assigned(left.jump) do |
| 3224 |
begin |
| 3225 |
prevBase := nil; |
| 3226 |
while Assigned(left) and Assigned(left.jump) do |
| 3227 |
begin |
| 3228 |
base := left; |
| 3229 |
right := left.jump; |
| 3230 |
rend := right.jump; |
| 3231 |
left.jump := rend; |
| 3232 |
lend := right; rend := right.jump; |
| 3233 |
while (left <> lend) and (right <> rend) do |
| 3234 |
begin |
| 3235 |
if right.currX < left.currX then |
| 3236 |
begin |
| 3237 |
// save edge intersections |
| 3238 |
e := right.prevInSEL; |
| 3239 |
while true do |
| 3240 |
begin |
| 3241 |
AddNewIntersectNode(e, right, topY); |
| 3242 |
if e = left then Break; |
| 3243 |
e := e.prevInSEL; |
| 3244 |
end; |
| 3245 |
|
| 3246 |
// now move the out of place edge on the right |
| 3247 |
// to its new ordered place on the left. |
| 3248 |
e := right; |
| 3249 |
right := ExtractFromSEL(e); // ie returns the new right |
| 3250 |
lend := right; |
| 3251 |
Insert1Before2InSEL(e, left); |
| 3252 |
if left = base then |
| 3253 |
begin |
| 3254 |
base := e; |
| 3255 |
base.jump := rend; |
| 3256 |
if Assigned(prevBase) then |
| 3257 |
prevBase.jump := base else |
| 3258 |
FSel := base; |
| 3259 |
end; |
| 3260 |
end else |
| 3261 |
left := left.nextInSEL; |
| 3262 |
end; |
| 3263 |
prevBase := base; |
| 3264 |
left := rend; |
| 3265 |
end; |
| 3266 |
left := FSel; |
| 3267 |
end; |
| 3268 |
result := FIntersectList.Count > 0; |
| 3269 |
end; |
| 3270 |
//------------------------------------------------------------------------------ |
| 3271 |
|
| 3272 |
function IntersectListSort(node1, node2: Pointer): Integer; |
| 3273 |
var |
| 3274 |
pt1, pt2: PPoint64; |
| 3275 |
i: Int64; |
| 3276 |
begin |
| 3277 |
if node1 = node2 then |
| 3278 |
begin |
| 3279 |
Result := 0; |
| 3280 |
Exit; |
| 3281 |
end; |
| 3282 |
pt1 := @PIntersectNode(node1).pt; |
| 3283 |
pt2 := @PIntersectNode(node2).pt; |
| 3284 |
i := pt2.Y - pt1.Y; |
| 3285 |
// note to self - can't return int64 values :) |
| 3286 |
if i > 0 then Result := 1 |
| 3287 |
else if i < 0 then Result := -1 |
| 3288 |
else if (pt1 = pt2) then Result := 0 |
| 3289 |
else |
| 3290 |
begin |
| 3291 |
// Sort by X too. Not essential, but it significantly |
| 3292 |
// speeds up the secondary sort in ProcessIntersectList . |
| 3293 |
i := pt1.X - pt2.X; |
| 3294 |
if i > 0 then Result := 1 |
| 3295 |
else if i < 0 then Result := -1 |
| 3296 |
else Result := 0; |
| 3297 |
end; |
| 3298 |
end; |
| 3299 |
//------------------------------------------------------------------------------ |
| 3300 |
|
| 3301 |
procedure TClipperBase.ProcessIntersectList; |
| 3302 |
var |
| 3303 |
i: Integer; |
| 3304 |
nodeI, nodeJ: PPIntersectNode; |
| 3305 |
begin |
| 3306 |
// The list of required intersections now needs to be processed in a |
| 3307 |
// specific order such that intersection points with the largest Y coords |
| 3308 |
// are processed before those with the smallest Y coords. However, |
| 3309 |
// it's critical that edges are adjacent at the time of intersection, but |
| 3310 |
// that can only be checked during processing (when edge positions change). |
| 3311 |
|
| 3312 |
// First we do a quicksort so that intersections will be processed |
| 3313 |
// mostly from largest Y to smallest |
| 3314 |
FIntersectList.Sort(IntersectListSort); |
| 3315 |
nodeI := @FIntersectList.List[0]; |
| 3316 |
for i := 0 to FIntersectList.Count - 1 do |
| 3317 |
begin |
| 3318 |
// during processing, make sure edges are adjacent before |
| 3319 |
// proceeding, and swapping the order if they aren't adjacent. |
| 3320 |
if not EdgesAdjacentInAEL(nodeI^) then |
| 3321 |
begin |
| 3322 |
nodeJ := nodeI; |
| 3323 |
repeat |
| 3324 |
inc(nodeJ); |
| 3325 |
until EdgesAdjacentInAEL(nodeJ^); |
| 3326 |
// now swap intersection order |
| 3327 |
Swap(PPointer(nodeI), PPointer(nodeJ)); |
| 3328 |
end; |
| 3329 |
|
| 3330 |
// now process the intersection |
| 3331 |
with nodeI^^ do |
| 3332 |
begin |
| 3333 |
IntersectEdges(active1, active2, pt); |
| 3334 |
SwapPositionsInAEL(active1, active2); |
| 3335 |
active1.currX := pt.X; |
| 3336 |
active2.currX := pt.X; |
| 3337 |
CheckJoinLeft(active2, pt, true); |
| 3338 |
CheckJoinRight(active1, pt, true); |
| 3339 |
end; |
| 3340 |
inc(nodeI); |
| 3341 |
end; |
| 3342 |
// Edges should once again be correctly ordered (left to right) in the AEL. |
| 3343 |
end; |
| 3344 |
//------------------------------------------------------------------------------ |
| 3345 |
|
| 3346 |
procedure TClipperBase.SwapPositionsInAEL(e1, e2: PActive); |
| 3347 |
var |
| 3348 |
prev, next: PActive; |
| 3349 |
begin |
| 3350 |
// preconditon: e1 must be immediately prior to e2 |
| 3351 |
next := e2.nextInAEL; |
| 3352 |
if Assigned(next) then next.prevInAEL := e1; |
| 3353 |
prev := e1.prevInAEL; |
| 3354 |
if Assigned(prev) then prev.nextInAEL := e2; |
| 3355 |
e2.prevInAEL := prev; |
| 3356 |
e2.nextInAEL := e1; |
| 3357 |
e1.prevInAEL := e2; |
| 3358 |
e1.nextInAEL := next; |
| 3359 |
if not Assigned(e2.prevInAEL) then FActives := e2; |
| 3360 |
end; |
| 3361 |
//------------------------------------------------------------------------------ |
| 3362 |
|
| 3363 |
function GetLastOp(hotEdge: PActive): POutPt; |
| 3364 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 3365 |
var |
| 3366 |
outrec: POutRec; |
| 3367 |
begin |
| 3368 |
outrec := hotEdge.outrec; |
| 3369 |
Result := outrec.pts; |
| 3370 |
if hotEdge <> outrec.frontE then |
| 3371 |
Result := Result.next; |
| 3372 |
end; |
| 3373 |
//------------------------------------------------------------------------------ |
| 3374 |
|
| 3375 |
procedure TClipperBase.DoHorizontal(horzEdge: PActive); |
| 3376 |
var |
| 3377 |
maxVertex: PVertex; |
| 3378 |
horzLeft, horzRight: Int64; |
| 3379 |
|
| 3380 |
function ResetHorzDirection: Boolean; |
| 3381 |
var |
| 3382 |
e: PActive; |
| 3383 |
begin |
| 3384 |
if (horzEdge.bot.X = horzEdge.top.X) then |
| 3385 |
begin |
| 3386 |
// the horizontal edge is going nowhere ... |
| 3387 |
horzLeft := horzEdge.currX; |
| 3388 |
horzRight := horzEdge.currX; |
| 3389 |
e := horzEdge.nextInAEL; |
| 3390 |
while assigned(e) and (e.vertTop <> maxVertex) do |
| 3391 |
e := e.nextInAEL; |
| 3392 |
Result := assigned(e); |
| 3393 |
// nb: this block isn't yet redundant |
| 3394 |
end |
| 3395 |
else if horzEdge.currX < horzEdge.top.X then |
| 3396 |
begin |
| 3397 |
horzLeft := horzEdge.currX; |
| 3398 |
horzRight := horzEdge.top.X; |
| 3399 |
Result := true; |
| 3400 |
end else |
| 3401 |
begin |
| 3402 |
horzLeft := horzEdge.top.X; |
| 3403 |
horzRight := horzEdge.currX; |
| 3404 |
Result := false; |
| 3405 |
end; |
| 3406 |
end; |
| 3407 |
//------------------------------------------------------------------------ |
| 3408 |
|
| 3409 |
var |
| 3410 |
Y: Int64; |
| 3411 |
e: PActive; |
| 3412 |
pt: TPoint64; |
| 3413 |
op: POutPt; |
| 3414 |
isLeftToRight, horzIsOpen: Boolean; |
| 3415 |
begin |
| 3416 |
(******************************************************************************* |
| 3417 |
* Notes: Horizontal edges (HEs) at scanline intersections (ie at the top or * |
| 3418 |
* bottom of a scanbeam) are processed as if layered. The order in which HEs * |
| 3419 |
* are processed doesn't matter. HEs intersect with the bottom vertices of * |
| 3420 |
* other HEs [#] and with non-horizontal edges [*]. Once these intersections * |
| 3421 |
* are completed, intermediate HEs are 'promoted' to the next edge in their * |
| 3422 |
* bounds, and they in turn may be intersected [%] by other HEs. * |
| 3423 |
* * |
| 3424 |
* eg: 3 horizontals at a scanline: / | / / * |
| 3425 |
* | / | (HE3) o=========%==========o * |
| 3426 |
* o=======o (HE2) / | / / * |
| 3427 |
* o============#=========*======*========#=========o (HE1) * |
| 3428 |
* / | / | / * |
| 3429 |
*******************************************************************************) |
| 3430 |
|
| 3431 |
horzIsOpen := IsOpen(horzEdge); |
| 3432 |
Y := horzEdge.bot.Y; |
| 3433 |
maxVertex := nil; |
| 3434 |
|
| 3435 |
if horzIsOpen then |
| 3436 |
maxVertex := GetCurrYMaximaVertexOpen(horzEdge) else |
| 3437 |
maxVertex := GetCurrYMaximaVertex(horzEdge); |
| 3438 |
|
| 3439 |
isLeftToRight := ResetHorzDirection; |
| 3440 |
|
| 3441 |
// nb: TrimHorz above hence not using Bot.X here |
| 3442 |
if IsHotEdge(horzEdge) then |
| 3443 |
begin |
| 3444 |
{$IFDEF USINGZ} |
| 3445 |
op := AddOutPt(horzEdge, Point64(horzEdge.currX, Y, horzEdge.bot.Z)); |
| 3446 |
{$ELSE} |
| 3447 |
op := AddOutPt(horzEdge, Point64(horzEdge.currX, Y)); |
| 3448 |
{$ENDIF} |
| 3449 |
FHorzSegList.Add(op); |
| 3450 |
end; |
| 3451 |
|
| 3452 |
while true do // loop through consec. horizontal edges |
| 3453 |
begin |
| 3454 |
if isLeftToRight then |
| 3455 |
e := horzEdge.nextInAEL else |
| 3456 |
e := horzEdge.prevInAEL; |
| 3457 |
|
| 3458 |
while assigned(e) do |
| 3459 |
begin |
| 3460 |
if (e.vertTop = maxVertex) then |
| 3461 |
begin |
| 3462 |
if IsHotEdge(horzEdge) and IsJoined(e) then |
| 3463 |
UndoJoin(e, e.top); |
| 3464 |
|
| 3465 |
if IsHotEdge(horzEdge) then |
| 3466 |
begin |
| 3467 |
|
| 3468 |
while (horzEdge.vertTop <> maxVertex) do |
| 3469 |
begin |
| 3470 |
AddOutPt(horzEdge, horzEdge.top); |
| 3471 |
UpdateEdgeIntoAEL(horzEdge); |
| 3472 |
end; |
| 3473 |
|
| 3474 |
if isLeftToRight then |
| 3475 |
AddLocalMaxPoly(horzEdge, e, horzEdge.top) else |
| 3476 |
AddLocalMaxPoly(e, horzEdge, horzEdge.top); |
| 3477 |
end; |
| 3478 |
// remove horzEdge's maxPair from AEL |
| 3479 |
DeleteFromAEL(e); |
| 3480 |
DeleteFromAEL(horzEdge); |
| 3481 |
Exit; |
| 3482 |
end; |
| 3483 |
|
| 3484 |
// if horzEdge is a maxima, keep going until we reach |
| 3485 |
// its maxima pair, otherwise check for Break conditions |
| 3486 |
if (maxVertex <> horzEdge.vertTop) or IsOpenEnd(horzEdge.vertTop) then |
| 3487 |
begin |
| 3488 |
// otherwise stop when 'e' is beyond the end of the horizontal line |
| 3489 |
if (isLeftToRight and (e.currX > horzRight)) or |
| 3490 |
(not isLeftToRight and (e.currX < horzLeft)) then Break; |
| 3491 |
|
| 3492 |
if (e.currX = horzEdge.top.X) and not IsHorizontal(e) then |
| 3493 |
begin |
| 3494 |
pt := NextVertex(horzEdge).pt; |
| 3495 |
|
| 3496 |
// to maximize the possibility of putting open edges into |
| 3497 |
// solutions, we'll only break if it's past HorzEdge's end |
| 3498 |
if IsOpen(E) and not IsSamePolyType(E, horzEdge) and |
| 3499 |
not IsHotEdge(e) then |
| 3500 |
begin |
| 3501 |
if (isLeftToRight and (TopX(E, pt.Y) > pt.X)) or |
| 3502 |
(not isLeftToRight and (TopX(E, pt.Y) < pt.X)) then Break; |
| 3503 |
end |
| 3504 |
// otherwise for edges at horzEdge's end, only stop when horzEdge's |
| 3505 |
// outslope is greater than e's slope when heading right or when |
| 3506 |
// horzEdge's outslope is less than e's slope when heading left. |
| 3507 |
else if (isLeftToRight and (TopX(E, pt.Y) >= pt.X)) or |
| 3508 |
(not isLeftToRight and (TopX(E, pt.Y) <= pt.X)) then Break; |
| 3509 |
end; |
| 3510 |
end; |
| 3511 |
|
| 3512 |
pt := Point64(e.currX, Y); |
| 3513 |
|
| 3514 |
if (isLeftToRight) then |
| 3515 |
begin |
| 3516 |
IntersectEdges(horzEdge, e, pt); |
| 3517 |
SwapPositionsInAEL(horzEdge, e); |
| 3518 |
CheckJoinLeft(e, pt); |
| 3519 |
horzEdge.currX := e.currX; |
| 3520 |
e := horzEdge.nextInAEL; |
| 3521 |
end else |
| 3522 |
begin |
| 3523 |
IntersectEdges(e, horzEdge, pt); |
| 3524 |
SwapPositionsInAEL(e, horzEdge); |
| 3525 |
CheckJoinRight(e, pt); |
| 3526 |
horzEdge.currX := e.currX; |
| 3527 |
e := horzEdge.prevInAEL; |
| 3528 |
end; |
| 3529 |
if IsHotEdge(horzEdge) then |
| 3530 |
begin |
| 3531 |
//nb: The outrec containining the op returned by IntersectEdges |
| 3532 |
//above may no longer be associated with horzEdge. |
| 3533 |
FHorzSegList.Add(GetLastOp(horzEdge)); |
| 3534 |
end; |
| 3535 |
end; // we've reached the end of this horizontal |
| 3536 |
|
| 3537 |
// check if we've finished looping through consecutive horizontals |
| 3538 |
if horzIsOpen and IsOpenEnd(horzEdge.vertTop) then |
| 3539 |
begin |
| 3540 |
if IsHotEdge(horzEdge) then |
| 3541 |
begin |
| 3542 |
AddOutPt(horzEdge, horzEdge.top); |
| 3543 |
if IsFront(horzEdge) then |
| 3544 |
horzEdge.outrec.frontE := nil else |
| 3545 |
horzEdge.outrec.backE := nil; |
| 3546 |
horzEdge.outrec := nil; |
| 3547 |
end; |
| 3548 |
DeleteFromAEL(horzEdge); // ie open at top |
| 3549 |
Exit; |
| 3550 |
end; |
| 3551 |
|
| 3552 |
if (NextVertex(horzEdge).pt.Y <> horzEdge.top.Y) then |
| 3553 |
Break; // end of an intermediate horizontal |
| 3554 |
|
| 3555 |
// there must be a following (consecutive) horizontal |
| 3556 |
|
| 3557 |
if IsHotEdge(horzEdge) then |
| 3558 |
AddOutPt(horzEdge, horzEdge.top); |
| 3559 |
UpdateEdgeIntoAEL(horzEdge); |
| 3560 |
isLeftToRight := ResetHorzDirection; |
| 3561 |
end; // end while horizontal |
| 3562 |
|
| 3563 |
if IsHotEdge(horzEdge) then |
| 3564 |
begin |
| 3565 |
op := AddOutPt(horzEdge, horzEdge.top); |
| 3566 |
FHorzSegList.Add(op); // Disc.#546 |
| 3567 |
end; |
| 3568 |
|
| 3569 |
UpdateEdgeIntoAEL(horzEdge); // this is the end of an intermediate horiz. |
| 3570 |
end; |
| 3571 |
//------------------------------------------------------------------------------ |
| 3572 |
|
| 3573 |
procedure TClipperBase.DoTopOfScanbeam(Y: Int64); |
| 3574 |
var |
| 3575 |
e: PActive; |
| 3576 |
begin |
| 3577 |
// FSel is reused to flag horizontals (see PushHorz below) |
| 3578 |
FSel := nil; |
| 3579 |
e := FActives; |
| 3580 |
while Assigned(e) do |
| 3581 |
begin |
| 3582 |
// nb: 'e' will never be horizontal here |
| 3583 |
if (e.top.Y = Y) then |
| 3584 |
begin |
| 3585 |
e.currX := e.top.X; |
| 3586 |
if IsMaxima(e) then |
| 3587 |
begin |
| 3588 |
e := DoMaxima(e); // TOP OF BOUND (MAXIMA) |
| 3589 |
Continue; |
| 3590 |
end else |
| 3591 |
begin |
| 3592 |
// INTERMEDIATE VERTEX ... |
| 3593 |
if IsHotEdge(e) then |
| 3594 |
AddOutPt(e, e.top); |
| 3595 |
UpdateEdgeIntoAEL(e); |
| 3596 |
if IsHorizontal(e) then |
| 3597 |
PushHorz(e); |
| 3598 |
end; |
| 3599 |
end else |
| 3600 |
e.currX := TopX(e, Y); |
| 3601 |
e := e.nextInAEL; |
| 3602 |
end; |
| 3603 |
end; |
| 3604 |
//------------------------------------------------------------------------------ |
| 3605 |
|
| 3606 |
function TClipperBase.DoMaxima(e: PActive): PActive; |
| 3607 |
var |
| 3608 |
eNext, ePrev, eMaxPair: PActive; |
| 3609 |
begin |
| 3610 |
ePrev := e.prevInAEL; |
| 3611 |
eNext := e.nextInAEL; |
| 3612 |
Result := eNext; |
| 3613 |
|
| 3614 |
if IsOpenEnd(e.vertTop) then |
| 3615 |
begin |
| 3616 |
if IsHotEdge(e) then AddOutPt(e, e.top); |
| 3617 |
if not IsHorizontal(e) then |
| 3618 |
begin |
| 3619 |
if IsHotEdge(e) then |
| 3620 |
begin |
| 3621 |
if IsFront(e) then |
| 3622 |
e.outrec.frontE := nil else |
| 3623 |
e.outrec.backE := nil; |
| 3624 |
e.outrec := nil; |
| 3625 |
end; |
| 3626 |
DeleteFromAEL(e); |
| 3627 |
end; |
| 3628 |
Exit; |
| 3629 |
end else |
| 3630 |
begin |
| 3631 |
eMaxPair := GetMaximaPair(e); |
| 3632 |
if not assigned(eMaxPair) then Exit; // EMaxPair is a horizontal ... |
| 3633 |
end; |
| 3634 |
|
| 3635 |
if IsJoined(e) then UndoJoin(e, e.top); |
| 3636 |
if IsJoined(eMaxPair) then UndoJoin(eMaxPair, eMaxPair.top); |
| 3637 |
|
| 3638 |
// only non-horizontal maxima here. |
| 3639 |
// process any edges between maxima pair ... |
| 3640 |
while (eNext <> eMaxPair) do |
| 3641 |
begin |
| 3642 |
IntersectEdges(e, eNext, e.top); |
| 3643 |
SwapPositionsInAEL(e, eNext); |
| 3644 |
eNext := e.nextInAEL; |
| 3645 |
end; |
| 3646 |
|
| 3647 |
if IsOpen(e) then |
| 3648 |
begin |
| 3649 |
// must be in the middle of an open path |
| 3650 |
if IsHotEdge(e) then |
| 3651 |
AddLocalMaxPoly(e, eMaxPair, e.top); |
| 3652 |
DeleteFromAEL(eMaxPair); |
| 3653 |
DeleteFromAEL(e); |
| 3654 |
|
| 3655 |
if assigned(ePrev) then |
| 3656 |
Result := ePrev.nextInAEL else |
| 3657 |
Result := FActives; |
| 3658 |
end else |
| 3659 |
begin |
| 3660 |
// e.NextInAEL == eMaxPair |
| 3661 |
if IsHotEdge(e) then |
| 3662 |
AddLocalMaxPoly(e, eMaxPair, e.top); |
| 3663 |
|
| 3664 |
DeleteFromAEL(e); |
| 3665 |
DeleteFromAEL(eMaxPair); |
| 3666 |
if assigned(ePrev) then |
| 3667 |
Result := ePrev.nextInAEL else |
| 3668 |
Result := FActives; |
| 3669 |
end; |
| 3670 |
end; |
| 3671 |
//------------------------------------------------------------------------------ |
| 3672 |
|
| 3673 |
function TClipperBase.BuildPaths(var closedPaths, openPaths: TPaths64): Boolean; |
| 3674 |
var |
| 3675 |
i: Integer; |
| 3676 |
closedCnt, openCnt: integer; |
| 3677 |
outRec: POutRec; |
| 3678 |
begin |
| 3679 |
closedCnt := Length(closedPaths); |
| 3680 |
openCnt := Length(openPaths); |
| 3681 |
try |
| 3682 |
i := 0; |
| 3683 |
while i < FOutRecList.Count do |
| 3684 |
begin |
| 3685 |
outRec := FOutRecList.UnsafeGet(i); |
| 3686 |
inc(i); |
| 3687 |
if not assigned(outRec.pts) then Continue; |
| 3688 |
|
| 3689 |
if outRec.isOpen then |
| 3690 |
begin |
| 3691 |
SetLength(openPaths, openCnt +1); |
| 3692 |
if BuildPath(outRec.pts, FReverseSolution, |
| 3693 |
true, openPaths[openCnt]) then inc(openCnt); |
| 3694 |
end else |
| 3695 |
begin |
| 3696 |
// nb: CleanCollinear can add to FOutRecList |
| 3697 |
CleanCollinear(outRec); |
| 3698 |
// closed paths should always return a Positive orientation |
| 3699 |
// except when ReverseSolution == true |
| 3700 |
SetLength(closedPaths, closedCnt +1); |
| 3701 |
if BuildPath(outRec.pts, FReverseSolution, |
| 3702 |
false, closedPaths[closedCnt]) then |
| 3703 |
inc(closedCnt); |
| 3704 |
end; |
| 3705 |
end; |
| 3706 |
result := true; |
| 3707 |
except |
| 3708 |
result := false; |
| 3709 |
end; |
| 3710 |
end; |
| 3711 |
//------------------------------------------------------------------------------ |
| 3712 |
|
| 3713 |
function TClipperBase.CheckBounds(outrec: POutRec): Boolean; |
| 3714 |
begin |
| 3715 |
if not Assigned(outrec.pts) then |
| 3716 |
Result := false |
| 3717 |
else if not outrec.bounds.IsEmpty then |
| 3718 |
Result := true |
| 3719 |
else |
| 3720 |
begin |
| 3721 |
CleanCollinear(outrec); |
| 3722 |
result := Assigned(outrec.pts) and |
| 3723 |
BuildPath(outrec.pts, FReverseSolution, false, outrec.path); |
| 3724 |
if not Result then Exit; |
| 3725 |
outrec.bounds := Clipper.Core.GetBounds(outrec.path); |
| 3726 |
end; |
| 3727 |
end; |
| 3728 |
//------------------------------------------------------------------------------ |
| 3729 |
|
| 3730 |
function TClipperBase.CheckSplitOwner(outrec: POutRec; const splits: TOutRecArray): Boolean; |
| 3731 |
var |
| 3732 |
i : integer; |
| 3733 |
split : POutrec; |
| 3734 |
begin |
| 3735 |
// returns true if a valid owner is found in splits |
| 3736 |
// (and also assigns it to outrec.owner) |
| 3737 |
Result := true; |
| 3738 |
for i := 0 to High(splits) do |
| 3739 |
begin |
| 3740 |
split := GetRealOutRec(splits[i]); |
| 3741 |
if (split = nil) or |
| 3742 |
(split = outrec) or |
| 3743 |
(split.recursiveCheck = outrec) then Continue; |
| 3744 |
|
| 3745 |
split.recursiveCheck := outrec; // prevent infinite loops |
| 3746 |
if Assigned(split.splits) and |
| 3747 |
CheckSplitOwner(outrec, split.splits) then Exit |
| 3748 |
else if IsValidOwner(outrec, split) and |
| 3749 |
CheckBounds(split) and |
| 3750 |
(split.bounds.Contains(outrec.bounds) and |
| 3751 |
Path1InsidePath2(outrec.pts, split.pts)) then |
| 3752 |
begin |
| 3753 |
outrec.owner := split; |
| 3754 |
Exit; |
| 3755 |
end; |
| 3756 |
end; |
| 3757 |
Result := false; |
| 3758 |
end; |
| 3759 |
//------------------------------------------------------------------------------ |
| 3760 |
|
| 3761 |
procedure TClipperBase.RecursiveCheckOwners(outrec: POutRec; polytree: TPolyPathBase); |
| 3762 |
begin |
| 3763 |
// pre-condition: outrec will have valid bounds |
| 3764 |
// post-condition: if a valid path, outrec will have a polypath |
| 3765 |
|
| 3766 |
if Assigned(outrec.polypath) or |
| 3767 |
outrec.bounds.IsEmpty then |
| 3768 |
Exit; |
| 3769 |
|
| 3770 |
while Assigned(outrec.owner) do |
| 3771 |
begin |
| 3772 |
if Assigned(outrec.owner.splits) and |
| 3773 |
CheckSplitOwner(outrec, outrec.owner.splits) then Break; |
| 3774 |
if Assigned(outrec.owner.pts) and |
| 3775 |
CheckBounds(outrec.owner) and |
| 3776 |
(outrec.owner.bounds.Contains(outrec.bounds) and |
| 3777 |
Path1InsidePath2(outrec.pts, outrec.owner.pts)) then break; |
| 3778 |
outrec.owner := outrec.owner.owner; |
| 3779 |
end; |
| 3780 |
|
| 3781 |
if Assigned(outrec.owner) then |
| 3782 |
begin |
| 3783 |
if not Assigned(outrec.owner.polypath) then |
| 3784 |
RecursiveCheckOwners(outrec.owner, polytree); |
| 3785 |
outrec.polypath := outrec.owner.polypath.AddChild(outrec.path) |
| 3786 |
end else |
| 3787 |
outrec.polypath := polytree.AddChild(outrec.path); |
| 3788 |
end; |
| 3789 |
//------------------------------------------------------------------------------ |
| 3790 |
|
| 3791 |
function TClipperBase.BuildTree(polytree: TPolyPathBase; |
| 3792 |
out openPaths: TPaths64): Boolean; |
| 3793 |
var |
| 3794 |
i : Integer; |
| 3795 |
cntOpen : Integer; |
| 3796 |
outrec : POutRec; |
| 3797 |
openPath : TPath64; |
| 3798 |
begin |
| 3799 |
try |
| 3800 |
polytree.Clear; |
| 3801 |
if FHasOpenPaths then |
| 3802 |
setLength(openPaths, FOutRecList.Count); |
| 3803 |
cntOpen := 0; |
| 3804 |
|
| 3805 |
i := 0; |
| 3806 |
// FOutRecList.Count is not static here because |
| 3807 |
// CheckBounds below can indirectly add additional |
| 3808 |
// OutRec (via FixOutRecPts & CleanCollinear) |
| 3809 |
while i < FOutRecList.Count do |
| 3810 |
begin |
| 3811 |
outrec := FOutRecList.UnsafeGet(i); |
| 3812 |
inc(i); |
| 3813 |
if not assigned(outrec.pts) or |
| 3814 |
assigned(outrec.polypath) then Continue; |
| 3815 |
|
| 3816 |
if outrec.isOpen then |
| 3817 |
begin |
| 3818 |
if BuildPath(outrec.pts, |
| 3819 |
FReverseSolution, true, openPath) then |
| 3820 |
begin |
| 3821 |
openPaths[cntOpen] := openPath; |
| 3822 |
inc(cntOpen); |
| 3823 |
end; |
| 3824 |
Continue; |
| 3825 |
end; |
| 3826 |
|
| 3827 |
if CheckBounds(outrec) then |
| 3828 |
RecursiveCheckOwners(outrec, polytree); |
| 3829 |
end; |
| 3830 |
setLength(openPaths, cntOpen); |
| 3831 |
Result := FSucceeded; |
| 3832 |
except |
| 3833 |
Result := false; |
| 3834 |
end; |
| 3835 |
end; |
| 3836 |
//------------------------------------------------------------------------------ |
| 3837 |
|
| 3838 |
function TClipperBase.GetBounds: TRect64; |
| 3839 |
var |
| 3840 |
i: Integer; |
| 3841 |
v, vStart: PVertex; |
| 3842 |
begin |
| 3843 |
Result := Rect64(MaxInt64, MaxInt64, -MaxInt64, -MaxInt64); |
| 3844 |
for i := 0 to FVertexArrayList.Count -1 do |
| 3845 |
begin |
| 3846 |
vStart := UnsafeGet(FVertexArrayList, i); |
| 3847 |
v := vStart; |
| 3848 |
repeat |
| 3849 |
if v.pt.X < Result.Left then Result.Left := v.pt.X |
| 3850 |
else if v.pt.X > Result.Right then Result.Right := v.pt.X; |
| 3851 |
if v.pt.Y < Result.Top then Result.Top := v.pt.Y |
| 3852 |
else if v.pt.Y > Result.Bottom then Result.Bottom := v.pt.Y; |
| 3853 |
v := v.next; |
| 3854 |
until v = vStart; |
| 3855 |
end; |
| 3856 |
if Result.Left > Result.Right then Result := NullRect64; |
| 3857 |
end; |
| 3858 |
|
| 3859 |
//------------------------------------------------------------------------------ |
| 3860 |
// TClipper methods |
| 3861 |
//------------------------------------------------------------------------------ |
| 3862 |
|
| 3863 |
procedure TClipper64.AddReuseableData(const reuseableData: TReuseableDataContainer64); |
| 3864 |
begin |
| 3865 |
inherited AddReuseableData(reuseableData); |
| 3866 |
end; |
| 3867 |
//------------------------------------------------------------------------------ |
| 3868 |
|
| 3869 |
procedure TClipper64.AddSubject(const subject: TPath64); |
| 3870 |
begin |
| 3871 |
AddPath(subject, ptSubject, false); |
| 3872 |
end; |
| 3873 |
//------------------------------------------------------------------------------ |
| 3874 |
|
| 3875 |
procedure TClipper64.AddSubject(const subjects: TPaths64); |
| 3876 |
begin |
| 3877 |
AddPaths(subjects, ptSubject, false); |
| 3878 |
end; |
| 3879 |
//------------------------------------------------------------------------------ |
| 3880 |
|
| 3881 |
procedure TClipper64.AddOpenSubject(const subject: TPath64); |
| 3882 |
begin |
| 3883 |
AddPath(subject, ptSubject, true); |
| 3884 |
end; |
| 3885 |
//------------------------------------------------------------------------------ |
| 3886 |
|
| 3887 |
procedure TClipper64.AddOpenSubject(const subjects: TPaths64); |
| 3888 |
begin |
| 3889 |
AddPaths(subjects, ptSubject, true); |
| 3890 |
end; |
| 3891 |
//------------------------------------------------------------------------------ |
| 3892 |
|
| 3893 |
procedure TClipper64.AddClip(const clip: TPath64); |
| 3894 |
begin |
| 3895 |
AddPath(clip, ptClip, false); |
| 3896 |
end; |
| 3897 |
//------------------------------------------------------------------------------ |
| 3898 |
|
| 3899 |
procedure TClipper64.AddClip(const clips: TPaths64); |
| 3900 |
begin |
| 3901 |
AddPaths(clips, ptClip, false); |
| 3902 |
end; |
| 3903 |
//------------------------------------------------------------------------------ |
| 3904 |
|
| 3905 |
function TClipper64.Execute(clipType: TClipType; |
| 3906 |
fillRule: TFillRule; out closedSolutions: TPaths64): Boolean; |
| 3907 |
var |
| 3908 |
dummy: TPaths64; |
| 3909 |
begin |
| 3910 |
FUsingPolytree := false; |
| 3911 |
closedSolutions := nil; |
| 3912 |
try try |
| 3913 |
ExecuteInternal(clipType, fillRule, false); |
| 3914 |
Result := Succeeded and |
| 3915 |
BuildPaths(closedSolutions, dummy); |
| 3916 |
except |
| 3917 |
Result := false; |
| 3918 |
end; |
| 3919 |
finally |
| 3920 |
if not ClearSolutionOnly then Result := false; |
| 3921 |
end; |
| 3922 |
end; |
| 3923 |
//------------------------------------------------------------------------------ |
| 3924 |
|
| 3925 |
function TClipper64.Execute(clipType: TClipType; fillRule: TFillRule; |
| 3926 |
out closedSolutions, openSolutions: TPaths64): Boolean; |
| 3927 |
begin |
| 3928 |
closedSolutions := nil; |
| 3929 |
openSolutions := nil; |
| 3930 |
FUsingPolytree := false; |
| 3931 |
try try |
| 3932 |
ExecuteInternal(clipType, fillRule, false); |
| 3933 |
Result := Succeeded and |
| 3934 |
BuildPaths(closedSolutions, openSolutions); |
| 3935 |
except |
| 3936 |
Result := false; |
| 3937 |
end; |
| 3938 |
finally |
| 3939 |
if not ClearSolutionOnly then Result := false; |
| 3940 |
end; |
| 3941 |
end; |
| 3942 |
//------------------------------------------------------------------------------ |
| 3943 |
|
| 3944 |
function TClipper64.Execute(clipType: TClipType; fillRule: TFillRule; |
| 3945 |
var solutionTree: TPolyTree64; out openSolutions: TPaths64): Boolean; |
| 3946 |
begin |
| 3947 |
if not assigned(solutionTree) then |
| 3948 |
Raise EClipper2LibException(rsClipper_PolyTreeErr); |
| 3949 |
solutionTree.Clear; |
| 3950 |
FUsingPolytree := true; |
| 3951 |
openSolutions := nil; |
| 3952 |
try try |
| 3953 |
ExecuteInternal(clipType, fillRule, true); |
| 3954 |
Result := Succeeded and |
| 3955 |
BuildTree(solutionTree, openSolutions); |
| 3956 |
except |
| 3957 |
Result := false; |
| 3958 |
end; |
| 3959 |
finally |
| 3960 |
if not ClearSolutionOnly then Result := false; |
| 3961 |
end; |
| 3962 |
end; |
| 3963 |
|
| 3964 |
//------------------------------------------------------------------------------ |
| 3965 |
// TPolyPathBase methods |
| 3966 |
//------------------------------------------------------------------------------ |
| 3967 |
|
| 3968 |
constructor TPolyPathBase.Create; |
| 3969 |
begin |
| 3970 |
FChildList := TList.Create; |
| 3971 |
end; |
| 3972 |
//------------------------------------------------------------------------------ |
| 3973 |
|
| 3974 |
destructor TPolyPathBase.Destroy; |
| 3975 |
begin |
| 3976 |
Clear; |
| 3977 |
FChildList.Free; |
| 3978 |
inherited Destroy; |
| 3979 |
end; |
| 3980 |
//------------------------------------------------------------------------------ |
| 3981 |
|
| 3982 |
type |
| 3983 |
PPolyPathBase = ^TPolyPathBase; |
| 3984 |
|
| 3985 |
procedure TPolyPathBase.Clear; |
| 3986 |
var |
| 3987 |
i: integer; |
| 3988 |
ppb: PPolyPathBase; |
| 3989 |
begin |
| 3990 |
if FChildList.Count = 0 then Exit; |
| 3991 |
ppb := @FChildList.List[0]; |
| 3992 |
for i := 0 to FChildList.Count -1 do |
| 3993 |
begin |
| 3994 |
ppb^.Free; |
| 3995 |
inc(ppb); |
| 3996 |
end; |
| 3997 |
FChildList.Clear; |
| 3998 |
end; |
| 3999 |
//------------------------------------------------------------------------------ |
| 4000 |
|
| 4001 |
function TPolyPathBase.GetChild(index: Integer): TPolyPathBase; |
| 4002 |
begin |
| 4003 |
if (index < 0) or (index >= FChildList.Count) then |
| 4004 |
Result := nil else |
| 4005 |
Result := FChildList[index]; |
| 4006 |
end; |
| 4007 |
//------------------------------------------------------------------------------ |
| 4008 |
|
| 4009 |
function TPolyPathBase.GetLevel: Integer; |
| 4010 |
var |
| 4011 |
pp: TPolyPathBase; |
| 4012 |
begin |
| 4013 |
Result := 0; |
| 4014 |
pp := Parent; |
| 4015 |
while Assigned(pp) do |
| 4016 |
begin |
| 4017 |
inc(Result); |
| 4018 |
pp := pp.Parent; |
| 4019 |
end; |
| 4020 |
end; |
| 4021 |
//------------------------------------------------------------------------------ |
| 4022 |
|
| 4023 |
function TPolyPathBase.GetIsHole: Boolean; |
| 4024 |
begin |
| 4025 |
Result := Iif(Assigned(Parent), not Odd(GetLevel), false); |
| 4026 |
end; |
| 4027 |
//------------------------------------------------------------------------------ |
| 4028 |
|
| 4029 |
function TPolyPathBase.GetChildCnt: Integer; |
| 4030 |
begin |
| 4031 |
Result := FChildList.Count; |
| 4032 |
end; |
| 4033 |
|
| 4034 |
//------------------------------------------------------------------------------ |
| 4035 |
//TPolyPath method |
| 4036 |
//------------------------------------------------------------------------------ |
| 4037 |
|
| 4038 |
function TPolyPath64.AddChild(const path: TPath64): TPolyPathBase; |
| 4039 |
begin |
| 4040 |
Result := TPolyPath64.Create; |
| 4041 |
Result.Parent := self; |
| 4042 |
TPolyPath64(Result).FPath := path;; |
| 4043 |
ChildList.Add(Result); |
| 4044 |
end; |
| 4045 |
//------------------------------------------------------------------------------ |
| 4046 |
|
| 4047 |
function TPolyPath64.GetChild64(index: Integer): TPolyPath64; |
| 4048 |
begin |
| 4049 |
Result := TPolyPath64(GetChild(index)); |
| 4050 |
end; |
| 4051 |
|
| 4052 |
//------------------------------------------------------------------------------ |
| 4053 |
// TClipperD methods |
| 4054 |
//------------------------------------------------------------------------------ |
| 4055 |
|
| 4056 |
constructor TClipperD.Create(precision: integer); |
| 4057 |
begin |
| 4058 |
inherited Create; |
| 4059 |
CheckPrecisionRange(precision); |
| 4060 |
FScale := Math.Power(10, precision); |
| 4061 |
FInvScale := 1/FScale; |
| 4062 |
end; |
| 4063 |
//------------------------------------------------------------------------------ |
| 4064 |
|
| 4065 |
{$IFDEF USINGZ} |
| 4066 |
procedure TClipperD.CheckCallback; |
| 4067 |
begin |
| 4068 |
// only when the user defined ZCallback function has been assigned |
| 4069 |
// do we assign the proxy callback ZCB to ClipperBase |
| 4070 |
if Assigned(ZCallback) then |
| 4071 |
inherited ZCallback := ZCB else |
| 4072 |
inherited ZCallback := nil; |
| 4073 |
end; |
| 4074 |
//------------------------------------------------------------------------------ |
| 4075 |
|
| 4076 |
procedure TClipperD.ZCB(const bot1, top1, bot2, top2: TPoint64; |
| 4077 |
var intersectPt: TPoint64); |
| 4078 |
var |
| 4079 |
tmp: TPointD; |
| 4080 |
begin |
| 4081 |
if not assigned(fZCallback) then Exit; |
| 4082 |
// de-scale (x & y) |
| 4083 |
// temporarily convert integers to their initial float values |
| 4084 |
// this will slow clipping marginally but will make it much easier |
| 4085 |
// to understand the coordinates passed to the callback function |
| 4086 |
tmp := ScalePoint(intersectPt, FInvScale); |
| 4087 |
//do the callback |
| 4088 |
fZCallback( |
| 4089 |
ScalePoint(bot1, FInvScale), |
| 4090 |
ScalePoint(top1, FInvScale), |
| 4091 |
ScalePoint(bot2, FInvScale), |
| 4092 |
ScalePoint(top2, FInvScale), tmp); |
| 4093 |
intersectPt.Z := tmp.Z; |
| 4094 |
end; |
| 4095 |
//------------------------------------------------------------------------------ |
| 4096 |
{$ENDIF} |
| 4097 |
|
| 4098 |
procedure TClipperD.AddSubject(const pathD: TPathD); |
| 4099 |
var |
| 4100 |
p: TPath64; |
| 4101 |
begin |
| 4102 |
if FScale = 0 then FScale := DefaultClipperDScale; |
| 4103 |
p := ScalePath(pathD, FScale); |
| 4104 |
AddPath(p, ptSubject, false); |
| 4105 |
end; |
| 4106 |
//------------------------------------------------------------------------------ |
| 4107 |
|
| 4108 |
procedure TClipperD.AddSubject(const pathsD: TPathsD); |
| 4109 |
var |
| 4110 |
pp: TPaths64; |
| 4111 |
begin |
| 4112 |
if FScale = 0 then FScale := DefaultClipperDScale; |
| 4113 |
pp := ScalePaths(pathsD, FScale); |
| 4114 |
AddPaths(pp, ptSubject, false); |
| 4115 |
end; |
| 4116 |
//------------------------------------------------------------------------------ |
| 4117 |
|
| 4118 |
procedure TClipperD.AddOpenSubject(const pathD: TPathD); |
| 4119 |
var |
| 4120 |
p: TPath64; |
| 4121 |
begin |
| 4122 |
if FScale = 0 then FScale := DefaultClipperDScale; |
| 4123 |
p := ScalePath(pathD, FScale); |
| 4124 |
AddPath(p, ptSubject, true); |
| 4125 |
end; |
| 4126 |
//------------------------------------------------------------------------------ |
| 4127 |
|
| 4128 |
procedure TClipperD.AddOpenSubject(const pathsD: TPathsD); |
| 4129 |
var |
| 4130 |
pp: TPaths64; |
| 4131 |
begin |
| 4132 |
if FScale = 0 then FScale := DefaultClipperDScale; |
| 4133 |
pp := ScalePaths(pathsD, FScale); |
| 4134 |
AddPaths(pp, ptSubject, true); |
| 4135 |
end; |
| 4136 |
//------------------------------------------------------------------------------ |
| 4137 |
|
| 4138 |
procedure TClipperD.AddClip(const pathD: TPathD); |
| 4139 |
var |
| 4140 |
p: TPath64; |
| 4141 |
begin |
| 4142 |
if FScale = 0 then FScale := DefaultClipperDScale; |
| 4143 |
p := ScalePath(pathD, FScale); |
| 4144 |
AddPath(p, ptClip, false); |
| 4145 |
end; |
| 4146 |
//------------------------------------------------------------------------------ |
| 4147 |
|
| 4148 |
procedure TClipperD.AddClip(const pathsD: TPathsD); |
| 4149 |
var |
| 4150 |
pp: TPaths64; |
| 4151 |
begin |
| 4152 |
if FScale = 0 then FScale := DefaultClipperDScale; |
| 4153 |
pp := ScalePaths(pathsD, FScale); |
| 4154 |
AddPaths(pp, ptClip, false); |
| 4155 |
end; |
| 4156 |
//------------------------------------------------------------------------------ |
| 4157 |
|
| 4158 |
function TClipperD.Execute(clipType: TClipType; fillRule: TFillRule; |
| 4159 |
out closedSolutions: TPathsD): Boolean; |
| 4160 |
var |
| 4161 |
dummy: TPathsD; |
| 4162 |
begin |
| 4163 |
Result := Execute(clipType, fillRule, closedSolutions, dummy); |
| 4164 |
end; |
| 4165 |
//------------------------------------------------------------------------------ |
| 4166 |
|
| 4167 |
function TClipperD.Execute(clipType: TClipType; fillRule: TFillRule; |
| 4168 |
out closedSolutions, openSolutions: TPathsD): Boolean; |
| 4169 |
var |
| 4170 |
solClosed, solOpen: TPaths64; |
| 4171 |
begin |
| 4172 |
{$IFDEF USINGZ} |
| 4173 |
CheckCallback; |
| 4174 |
{$ENDIF} |
| 4175 |
closedSolutions := nil; |
| 4176 |
openSolutions := nil; |
| 4177 |
try try |
| 4178 |
ExecuteInternal(clipType, fillRule, false); |
| 4179 |
Result := BuildPaths(solClosed, solOpen); |
| 4180 |
if not Result then Exit; |
| 4181 |
closedSolutions := ScalePathsD(solClosed, FInvScale); |
| 4182 |
openSolutions := ScalePathsD(solOpen, FInvScale); |
| 4183 |
except |
| 4184 |
Result := false; |
| 4185 |
end; |
| 4186 |
finally |
| 4187 |
if not ClearSolutionOnly then Result := false; |
| 4188 |
end; |
| 4189 |
end; |
| 4190 |
//------------------------------------------------------------------------------ |
| 4191 |
|
| 4192 |
function TClipperD.Execute(clipType: TClipType; fillRule: TFillRule; |
| 4193 |
var solutionsTree: TPolyTreeD; out openSolutions: TPathsD): Boolean; |
| 4194 |
var |
| 4195 |
open_Paths: TPaths64; |
| 4196 |
begin |
| 4197 |
if not assigned(solutionsTree) then |
| 4198 |
Raise EClipper2LibException(rsClipper_PolyTreeErr); |
| 4199 |
{$IFDEF USINGZ} |
| 4200 |
CheckCallback; |
| 4201 |
{$ENDIF} |
| 4202 |
solutionsTree.Clear; |
| 4203 |
FUsingPolytree := true; |
| 4204 |
solutionsTree.SetScale(fScale); |
| 4205 |
openSolutions := nil; |
| 4206 |
try try |
| 4207 |
ExecuteInternal(clipType, fillRule, true); |
| 4208 |
BuildTree(solutionsTree, open_Paths); |
| 4209 |
openSolutions := ScalePathsD(open_Paths, FInvScale); |
| 4210 |
Result := true; |
| 4211 |
except |
| 4212 |
Result := false; |
| 4213 |
end; |
| 4214 |
finally |
| 4215 |
if not ClearSolutionOnly then Result := false; |
| 4216 |
end; |
| 4217 |
end; |
| 4218 |
|
| 4219 |
//------------------------------------------------------------------------------ |
| 4220 |
// TPolyPathD methods |
| 4221 |
//------------------------------------------------------------------------------ |
| 4222 |
|
| 4223 |
function TPolyPathD.AddChild(const path: TPath64): TPolyPathBase; |
| 4224 |
begin |
| 4225 |
Result := TPolyPathD.Create; |
| 4226 |
Result.Parent := self; |
| 4227 |
TPolyPathD(Result).fScale := fScale; |
| 4228 |
TPolyPathD(Result).FPath := ScalePathD(path, 1/FScale); |
| 4229 |
ChildList.Add(Result); |
| 4230 |
end; |
| 4231 |
//------------------------------------------------------------------------------ |
| 4232 |
|
| 4233 |
function TPolyPathD.AddChild(const path: TPathD): TPolyPathBase; |
| 4234 |
begin |
| 4235 |
Result := TPolyPathD.Create; |
| 4236 |
Result.Parent := self; |
| 4237 |
TPolyPathD(Result).fScale := fScale; |
| 4238 |
TPolyPathD(Result).FPath := path; |
| 4239 |
ChildList.Add(Result); |
| 4240 |
end; |
| 4241 |
//------------------------------------------------------------------------------ |
| 4242 |
|
| 4243 |
function TPolyPathD.GetChildD(index: Integer): TPolyPathD; |
| 4244 |
begin |
| 4245 |
Result := TPolyPathD(GetChild(index)); |
| 4246 |
end; |
| 4247 |
|
| 4248 |
//------------------------------------------------------------------------------ |
| 4249 |
// TPolyTreeD |
| 4250 |
//------------------------------------------------------------------------------ |
| 4251 |
|
| 4252 |
procedure TPolyTreeD.SetScale(value: double); |
| 4253 |
begin |
| 4254 |
FScale := value; |
| 4255 |
end; |
| 4256 |
//------------------------------------------------------------------------------ |
| 4257 |
|
| 4258 |
end. |
| 4259 |
|