| 1 |
unit Clipper.RectClip; |
| 2 |
|
| 3 |
(******************************************************************************* |
| 4 |
* Author : Angus Johnson * |
| 5 |
* Date : 5 July 2024 * |
| 6 |
* Website : http://www.angusj.com * |
| 7 |
* Copyright : Angus Johnson 2010-2024 * |
| 8 |
* Purpose : FAST rectangular clipping * |
| 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, SysUtils, Clipper.Core; |
| 18 |
|
| 19 |
type |
| 20 |
TLocation = (locLeft, locTop, locRight, locBottom, locInside); |
| 21 |
|
| 22 |
POutPt2 = ^TOutPt2; |
| 23 |
POutPtArray = ^TOutPtArray; |
| 24 |
TOutPtArray = array of POutPt2; |
| 25 |
TOutPtArrayArray = array of TOutPtArray; |
| 26 |
|
| 27 |
TOutPt2 = record |
| 28 |
ownerIdx: Cardinal; |
| 29 |
edge: POutPtArray; |
| 30 |
pt: TPoint64; |
| 31 |
next: POutPt2; |
| 32 |
prev: POutPt2; |
| 33 |
end; |
| 34 |
|
| 35 |
TRectClip64 = class |
| 36 |
procedure ExecuteInternal(const path: TPath64); |
| 37 |
function GetPath(resultIdx: integer): TPath64; |
| 38 |
protected |
| 39 |
fResults : TList; |
| 40 |
fRect : TRect64; |
| 41 |
fPathBounds : TRect64; |
| 42 |
fRectPath : TPath64; |
| 43 |
fRectMidPt : TPoint64; |
| 44 |
fEdges : TOutPtArrayArray; |
| 45 |
fStartLocs : TList; |
| 46 |
procedure DisposeResults; |
| 47 |
procedure CheckEdges; |
| 48 |
procedure TidyEdgePair(idx: integer; var cw, ccw: TOutPtArray); |
| 49 |
function Add(const pt: TPoint64; startNewPath: Boolean = false): POutPt2; |
| 50 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 51 |
procedure AddCorner(prev, curr: TLocation); overload; |
| 52 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 53 |
procedure AddCorner(var loc: TLocation; isClockwise: Boolean); overload; |
| 54 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 55 |
procedure GetNextLocation(const path: TPath64; |
| 56 |
var loc: TLocation; var i: integer; highI: integer); |
| 57 |
public |
| 58 |
constructor Create(const rect: TRect64); |
| 59 |
destructor Destroy; override; |
| 60 |
function Execute(const paths: TPaths64): TPaths64; |
| 61 |
end; |
| 62 |
|
| 63 |
TRectClipLines64 = class(TRectClip64) |
| 64 |
private |
| 65 |
procedure ExecuteInternal(const path: TPath64); |
| 66 |
function GetPath(resultIdx: integer): TPath64; |
| 67 |
public |
| 68 |
function Execute(const paths: TPaths64): TPaths64; |
| 69 |
end; |
| 70 |
|
| 71 |
implementation |
| 72 |
|
| 73 |
type |
| 74 |
PPath64 = ^TPath64; |
| 75 |
|
| 76 |
//------------------------------------------------------------------------------ |
| 77 |
// Miscellaneous functions |
| 78 |
//------------------------------------------------------------------------------ |
| 79 |
|
| 80 |
function GetLocation(const rec: TRect64; const pt: TPoint64; |
| 81 |
out loc: TLocation): Boolean; {$IFDEF INLINING} inline; {$ENDIF} |
| 82 |
begin |
| 83 |
Result := false; // only returns false when pt on rect |
| 84 |
if (pt.X = rec.Left) and |
| 85 |
(pt.Y >= rec.Top) and (pt.Y <= rec.Bottom) then |
| 86 |
begin |
| 87 |
loc := locLeft; |
| 88 |
Exit; //false |
| 89 |
end |
| 90 |
else if (pt.X = rec.Right) and |
| 91 |
(pt.Y >= rec.Top) and (pt.Y <= rec.Bottom) then |
| 92 |
begin |
| 93 |
loc := locRight; |
| 94 |
Exit; //false |
| 95 |
end |
| 96 |
else if (pt.Y = rec.Top) and |
| 97 |
(pt.X >= rec.Left) and (pt.X <= rec.Right) then |
| 98 |
begin |
| 99 |
loc := locTop; |
| 100 |
Exit; //false |
| 101 |
end |
| 102 |
else if (pt.Y = rec.Bottom) and |
| 103 |
(pt.X >= rec.Left) and (pt.X <= rec.Right) then |
| 104 |
begin |
| 105 |
loc := locBottom; |
| 106 |
Exit; //false |
| 107 |
end |
| 108 |
else if (pt.X < rec.Left) then loc := locLeft |
| 109 |
else if (pt.X > rec.Right) then loc := locRight |
| 110 |
else if (pt.Y < rec.Top) then loc := locTop |
| 111 |
else if (pt.Y > rec.Bottom) then loc := locBottom |
| 112 |
else loc := locInside; |
| 113 |
Result := true; |
| 114 |
end; |
| 115 |
//------------------------------------------------------------------------------ |
| 116 |
|
| 117 |
function IsHorizontal(pt1: TPoint64; pt2: TPoint64): Boolean; |
| 118 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 119 |
begin |
| 120 |
Result := pt1.Y = pt2.Y; |
| 121 |
end; |
| 122 |
//------------------------------------------------------------------------------ |
| 123 |
|
| 124 |
function GetSegmentIntersectPt2(p1: TPoint64; |
| 125 |
p2: TPoint64; p3: TPoint64; p4: TPoint64; out ip: TPoint64): Boolean; |
| 126 |
var |
| 127 |
res1, res2, res3, res4: double; |
| 128 |
begin |
| 129 |
res1 := CrossProduct(p1, p3, p4); |
| 130 |
res2 := CrossProduct(p2, p3, p4); |
| 131 |
if (res1 = 0) then |
| 132 |
begin |
| 133 |
ip := p1; |
| 134 |
if (res2 = 0) then |
| 135 |
result := false // segments are collinear |
| 136 |
else if PointsEqual(p1, p3) or PointsEqual(p1, p4) then |
| 137 |
result := true |
| 138 |
else if (IsHorizontal(p3, p4)) then |
| 139 |
result := ((p1.X > p3.X) = (p1.X < p4.X)) |
| 140 |
else |
| 141 |
result := (p1.Y > p3.Y) = (p1.Y < p4.Y); |
| 142 |
Exit; |
| 143 |
end; |
| 144 |
|
| 145 |
if (res2 = 0) then |
| 146 |
begin |
| 147 |
ip := p2; |
| 148 |
if PointsEqual(p2, p3) or PointsEqual(p2, p4) then |
| 149 |
Result := true |
| 150 |
else if (IsHorizontal(p3, p4)) then |
| 151 |
Result := ((p2.X > p3.X) = (p2.X < p4.X)) |
| 152 |
else Result := ((p2.Y > p3.Y) = (p2.Y < p4.Y)); |
| 153 |
Exit; |
| 154 |
end; |
| 155 |
|
| 156 |
if ((res1 > 0) = (res2 > 0)) then |
| 157 |
begin |
| 158 |
//ip := Point64(0, 0); |
| 159 |
Result := false; |
| 160 |
Exit; |
| 161 |
end; |
| 162 |
|
| 163 |
res3 := CrossProduct(p3, p1, p2); |
| 164 |
res4 := CrossProduct(p4, p1, p2); |
| 165 |
if (res3 = 0) then |
| 166 |
begin |
| 167 |
ip := p3; |
| 168 |
if PointsEqual(p3, p1) or PointsEqual(p3, p2) then |
| 169 |
Result := true |
| 170 |
else if (IsHorizontal(p1, p2)) then |
| 171 |
Result := (p3.X > p1.X) = (p3.X < p2.X) |
| 172 |
else |
| 173 |
Result := (p3.Y > p1.Y) = (p3.Y < p2.Y); |
| 174 |
end |
| 175 |
else if (res4 = 0) then |
| 176 |
begin |
| 177 |
ip := p4; |
| 178 |
if PointsEqual(p4, p1) or PointsEqual(p4, p2) then |
| 179 |
Result := true |
| 180 |
else if (IsHorizontal(p1, p2)) then |
| 181 |
Result := (p4.X > p1.X) = (p4.X < p2.X) |
| 182 |
else |
| 183 |
Result := (p4.Y > p1.Y) = (p4.Y < p2.Y); |
| 184 |
end |
| 185 |
else if ((res3 > 0) = (res4 > 0)) then |
| 186 |
begin |
| 187 |
//ip := Point64(0, 0); |
| 188 |
Result := false; |
| 189 |
end |
| 190 |
else |
| 191 |
// segments must intersect to get here |
| 192 |
Result := GetSegmentIntersectPt(p1, p2, p3, p4, ip); |
| 193 |
end; |
| 194 |
//------------------------------------------------------------------------------ |
| 195 |
|
| 196 |
function GetIntersection(const rectPath: TPath64; |
| 197 |
const p, p2: TPoint64; var loc: TLocation; out ip: TPoint64): Boolean; |
| 198 |
begin |
| 199 |
// gets the intersection closest to 'p' |
| 200 |
// when Result = false, loc will remain unchanged |
| 201 |
Result := True; |
| 202 |
case loc of |
| 203 |
locLeft: |
| 204 |
if GetSegmentIntersectPt2(p, p2, rectPath[0], rectPath[3], ip) then |
| 205 |
//Result := True |
| 206 |
else if (p.Y < rectPath[0].Y) and |
| 207 |
GetSegmentIntersectPt2(p, p2, rectPath[0], rectPath[1], ip) then |
| 208 |
loc := locTop |
| 209 |
else if GetSegmentIntersectPt2(p, p2, rectPath[2], rectPath[3], ip) then |
| 210 |
loc := locBottom |
| 211 |
else |
| 212 |
Result := False; |
| 213 |
|
| 214 |
locRight: |
| 215 |
if GetSegmentIntersectPt2(p, p2, rectPath[1], rectPath[2], ip) then |
| 216 |
//Result := True |
| 217 |
else if (p.Y < rectPath[0].Y) and |
| 218 |
GetSegmentIntersectPt2(p, p2, rectPath[0], rectPath[1], ip) then |
| 219 |
loc := locTop |
| 220 |
else if GetSegmentIntersectPt2(p, p2, rectPath[2], rectPath[3], ip) then |
| 221 |
loc := locBottom |
| 222 |
else |
| 223 |
Result := False; |
| 224 |
|
| 225 |
locTop: |
| 226 |
if GetSegmentIntersectPt2(p, p2, rectPath[0], rectPath[1], ip) then |
| 227 |
//Result := True |
| 228 |
else if (p.X < rectPath[0].X) and |
| 229 |
GetSegmentIntersectPt2(p, p2, rectPath[0], rectPath[3], ip) then |
| 230 |
loc := locLeft |
| 231 |
else if (p.X > rectPath[1].X) and |
| 232 |
GetSegmentIntersectPt2(p, p2, rectPath[1], rectPath[2], ip) then |
| 233 |
loc := locRight |
| 234 |
else |
| 235 |
Result := False; |
| 236 |
|
| 237 |
locBottom: |
| 238 |
if GetSegmentIntersectPt2(p, p2, rectPath[2], rectPath[3], ip) then |
| 239 |
//Result := True |
| 240 |
else if (p.X < rectPath[3].X) and |
| 241 |
GetSegmentIntersectPt2(p, p2, rectPath[0], rectPath[3], ip) then |
| 242 |
loc := locLeft |
| 243 |
else if (p.X > rectPath[2].X) and |
| 244 |
GetSegmentIntersectPt2(p, p2, rectPath[1], rectPath[2], ip) then |
| 245 |
loc := locRight |
| 246 |
else |
| 247 |
Result := False; |
| 248 |
|
| 249 |
else // loc = rInside |
| 250 |
begin |
| 251 |
if GetSegmentIntersectPt2(p, p2, rectPath[0], rectPath[3], ip) then |
| 252 |
loc := locLeft |
| 253 |
else if GetSegmentIntersectPt2(p, p2, rectPath[0], rectPath[1], ip) then |
| 254 |
loc := locTop |
| 255 |
else if GetSegmentIntersectPt2(p, p2, rectPath[1], rectPath[2], ip) then |
| 256 |
loc := locRight |
| 257 |
else if GetSegmentIntersectPt2(p, p2, rectPath[2], rectPath[3], ip) then |
| 258 |
loc := locBottom |
| 259 |
else |
| 260 |
Result := False; |
| 261 |
end; |
| 262 |
end; |
| 263 |
end; |
| 264 |
//------------------------------------------------------------------------------ |
| 265 |
|
| 266 |
function AreOpposites(prev, curr: TLocation): Boolean; |
| 267 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 268 |
begin |
| 269 |
Result := Abs(Ord(prev) - Ord(curr)) = 2; |
| 270 |
end; |
| 271 |
//------------------------------------------------------------------------------ |
| 272 |
|
| 273 |
function HeadingClockwise(prev, curr: TLocation): Boolean; |
| 274 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 275 |
begin |
| 276 |
Result := (Ord(prev) + 1) mod 4 = Ord(curr); |
| 277 |
end; |
| 278 |
//------------------------------------------------------------------------------ |
| 279 |
|
| 280 |
function GetAdjacentLocation(loc: TLocation; isClockwise: Boolean): TLocation; |
| 281 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 282 |
var |
| 283 |
delta: integer; |
| 284 |
begin |
| 285 |
delta := Iif(isClockwise, 1 , 3); |
| 286 |
Result := TLocation((Ord(loc) + delta) mod 4); |
| 287 |
end; |
| 288 |
//------------------------------------------------------------------------------ |
| 289 |
|
| 290 |
function IsClockwise(prev, curr: TLocation; |
| 291 |
const prevPt, currPt, rectMidPt: TPoint64): Boolean; |
| 292 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 293 |
begin |
| 294 |
Result := Iif(AreOpposites(prev, curr), |
| 295 |
CrossProduct(prevPt, rectMidPt, currPt) < 0, |
| 296 |
HeadingClockwise(prev, curr)); |
| 297 |
end; |
| 298 |
//------------------------------------------------------------------------------ |
| 299 |
|
| 300 |
function CountOp(op: POutPt2): integer; |
| 301 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 302 |
var |
| 303 |
op2: POutPt2; |
| 304 |
begin |
| 305 |
if not Assigned(op) then |
| 306 |
begin |
| 307 |
Result := 0; |
| 308 |
Exit; |
| 309 |
end; |
| 310 |
Result := 1; |
| 311 |
op2 := op; |
| 312 |
while op2.next <> op do |
| 313 |
begin |
| 314 |
inc(Result); |
| 315 |
op2 := op2.next; |
| 316 |
end; |
| 317 |
end; |
| 318 |
//------------------------------------------------------------------------------ |
| 319 |
|
| 320 |
procedure SetNewOwner(op: POutPt2; newIdx: integer); |
| 321 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 322 |
var |
| 323 |
op2: POutPt2; |
| 324 |
begin |
| 325 |
op.ownerIdx := newIdx; |
| 326 |
op2 := op.next; |
| 327 |
while op2 <> op do |
| 328 |
begin |
| 329 |
op2.ownerIdx := newIdx; |
| 330 |
op2 := op2.next; |
| 331 |
end; |
| 332 |
end; |
| 333 |
//------------------------------------------------------------------------------ |
| 334 |
|
| 335 |
procedure AddToEdge(var edge: TOutPtArray; op: POutPt2); |
| 336 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 337 |
var |
| 338 |
len: integer; |
| 339 |
begin |
| 340 |
if Assigned(op.edge) then Exit; |
| 341 |
op.edge := @edge; |
| 342 |
len := Length(edge); |
| 343 |
SetLength(edge, len+1); |
| 344 |
edge[len] := op; |
| 345 |
end; |
| 346 |
//------------------------------------------------------------------------------ |
| 347 |
|
| 348 |
function HasHorzOverlap(const left1, right1, left2, right2: TPoint64): boolean; |
| 349 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 350 |
begin |
| 351 |
Result := (left1.X < right2.X) and (right1.X > left2.X); |
| 352 |
end; |
| 353 |
//------------------------------------------------------------------------------ |
| 354 |
|
| 355 |
function HasVertOverlap(const top1, bottom1, top2, bottom2: TPoint64): boolean; |
| 356 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 357 |
begin |
| 358 |
Result := (top1.Y < bottom2.Y) and (bottom1.Y > top2.Y); |
| 359 |
end; |
| 360 |
//------------------------------------------------------------------------------ |
| 361 |
|
| 362 |
procedure UncoupleEdge(op: POutPt2); {$IFDEF INLINING} inline; {$ENDIF} |
| 363 |
var |
| 364 |
i: integer; |
| 365 |
begin |
| 366 |
if not Assigned(op.edge) then Exit; |
| 367 |
for i := 0 to High(POutPtArray(op.edge)^) do |
| 368 |
if POutPtArray(op.edge)^[i] = op then |
| 369 |
begin |
| 370 |
POutPtArray(op.edge)^[i] := nil; |
| 371 |
Break; |
| 372 |
end; |
| 373 |
op.edge := nil; |
| 374 |
end; |
| 375 |
//------------------------------------------------------------------------------ |
| 376 |
|
| 377 |
function DisposeOp(op: POutPt2): POutPt2; |
| 378 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 379 |
begin |
| 380 |
if op.next = op then |
| 381 |
Result := nil else |
| 382 |
Result := op.next; |
| 383 |
op.prev.next := op.next; |
| 384 |
op.next.prev := op.prev; |
| 385 |
Dispose(op); |
| 386 |
end; |
| 387 |
//------------------------------------------------------------------------------ |
| 388 |
|
| 389 |
function DisposeOpBack(op: POutPt2): POutPt2; |
| 390 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 391 |
begin |
| 392 |
if op.prev = op then |
| 393 |
Result := nil else |
| 394 |
Result := op.prev; |
| 395 |
op.prev.next := op.next; |
| 396 |
op.next.prev := op.prev; |
| 397 |
Dispose(op); |
| 398 |
end; |
| 399 |
//------------------------------------------------------------------------------ |
| 400 |
|
| 401 |
function GetEdgesForPt(const pt: TPoint64; const rec: TRect64): cardinal; |
| 402 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 403 |
begin |
| 404 |
if pt.X = rec.Left then |
| 405 |
Result := 1 |
| 406 |
else if pt.X = rec.Right then |
| 407 |
Result := 4 |
| 408 |
else |
| 409 |
Result := 0; |
| 410 |
|
| 411 |
if pt.Y = rec.Top then |
| 412 |
inc(Result, 2) |
| 413 |
else if pt.Y = rec.Bottom then |
| 414 |
inc(Result, 8); |
| 415 |
end; |
| 416 |
//------------------------------------------------------------------------------ |
| 417 |
|
| 418 |
function IsHeadingClockwise(const pt1, pt2: TPoint64; edgeIdx: integer): Boolean; |
| 419 |
{$IFDEF INLINING} inline; {$ENDIF} |
| 420 |
begin |
| 421 |
case edgeIdx of |
| 422 |
0: Result := pt2.Y < pt1.Y; |
| 423 |
1: Result := pt2.X > pt1.X; |
| 424 |
2: Result := pt2.Y > pt1.Y; |
| 425 |
else Result := pt2.X < pt1.X; |
| 426 |
end; |
| 427 |
end; |
| 428 |
|
| 429 |
//------------------------------------------------------------------------------ |
| 430 |
// TRectClip64 class |
| 431 |
//------------------------------------------------------------------------------ |
| 432 |
|
| 433 |
constructor TRectClip64.Create(const rect: TRect64); |
| 434 |
begin |
| 435 |
fResults := TList.Create; |
| 436 |
|
| 437 |
fRect := rect; |
| 438 |
fRectPath := fRect.AsPath; |
| 439 |
fRectMidPt := rect.MidPoint; |
| 440 |
fStartLocs := TList.Create; |
| 441 |
SetLength(fEdges, 8); |
| 442 |
end; |
| 443 |
//------------------------------------------------------------------------------ |
| 444 |
|
| 445 |
destructor TRectClip64.Destroy; |
| 446 |
begin |
| 447 |
fStartLocs.Free; |
| 448 |
fResults.Free; |
| 449 |
end; |
| 450 |
//------------------------------------------------------------------------------ |
| 451 |
|
| 452 |
procedure DisposeOps(op: POutPt2); |
| 453 |
var |
| 454 |
tmp: POutPt2; |
| 455 |
begin |
| 456 |
if not Assigned(op) then Exit; |
| 457 |
op.prev.next := nil; |
| 458 |
while assigned(op) do |
| 459 |
begin |
| 460 |
tmp := op; |
| 461 |
op := op.next; |
| 462 |
Dispose(tmp); |
| 463 |
end; |
| 464 |
end; |
| 465 |
//------------------------------------------------------------------------------ |
| 466 |
|
| 467 |
procedure TRectClip64.DisposeResults; |
| 468 |
var |
| 469 |
i: integer; |
| 470 |
begin |
| 471 |
for i := 0 to fResults.Count -1 do |
| 472 |
DisposeOps(fResults[i]); |
| 473 |
fResults.Clear; |
| 474 |
end; |
| 475 |
//------------------------------------------------------------------------------ |
| 476 |
|
| 477 |
function TRectClip64.Add(const pt: TPoint64; startNewPath: Boolean): POutPt2; |
| 478 |
var |
| 479 |
currIdx: integer; |
| 480 |
prevOp: POutPt2; |
| 481 |
begin |
| 482 |
// this method is only called by InternalExecute. |
| 483 |
// Later splitting and rejoining won't create additional op's, |
| 484 |
// though they will change the (non-storage) fResults count. |
| 485 |
currIdx := fResults.Count -1; |
| 486 |
if (currIdx < 0) or startNewPath then |
| 487 |
begin |
| 488 |
new(Result); |
| 489 |
Result.pt := pt; |
| 490 |
Result.edge := nil; |
| 491 |
Result.ownerIdx := fResults.Add(Result); |
| 492 |
Result.next := Result; |
| 493 |
Result.prev := Result; |
| 494 |
end else |
| 495 |
begin |
| 496 |
prevOp := fResults[currIdx]; |
| 497 |
if PointsEqual(prevOp.pt, pt) then |
| 498 |
begin |
| 499 |
Result := prevOp; |
| 500 |
Exit; |
| 501 |
end; |
| 502 |
new(Result); |
| 503 |
Result.pt := pt; |
| 504 |
Result.edge := nil; |
| 505 |
Result.ownerIdx := currIdx; |
| 506 |
Result.next := prevOp.next; |
| 507 |
prevOp.next.prev := Result; |
| 508 |
prevOp.next := Result; |
| 509 |
Result.prev := prevOp; |
| 510 |
fResults[currIdx] := Result; |
| 511 |
end; |
| 512 |
end; |
| 513 |
//------------------------------------------------------------------------------ |
| 514 |
|
| 515 |
procedure TRectClip64.AddCorner(prev, curr: TLocation); |
| 516 |
var |
| 517 |
cnrIdx: integer; |
| 518 |
begin |
| 519 |
if prev = curr then Exit; |
| 520 |
cnrIdx := Iif(HeadingClockwise(prev, curr), Ord(prev), Ord(curr)); |
| 521 |
Add(fRectPath[cnrIdx]); |
| 522 |
end; |
| 523 |
//------------------------------------------------------------------------------ |
| 524 |
|
| 525 |
procedure TRectClip64.AddCorner(var loc: TLocation; isClockwise: Boolean); |
| 526 |
begin |
| 527 |
if (isClockwise) then |
| 528 |
begin |
| 529 |
Add(fRectPath[Ord(loc)]); |
| 530 |
loc := GetAdjacentLocation(loc, true); |
| 531 |
end else |
| 532 |
begin |
| 533 |
loc := GetAdjacentLocation(loc, false); |
| 534 |
Add(fRectPath[Ord(loc)]); |
| 535 |
end; |
| 536 |
end; |
| 537 |
//------------------------------------------------------------------------------ |
| 538 |
|
| 539 |
procedure TRectClip64.GetNextLocation(const path: TPath64; |
| 540 |
var loc: TLocation; var i: integer; highI: integer); |
| 541 |
begin |
| 542 |
case loc of |
| 543 |
locLeft: |
| 544 |
begin |
| 545 |
while (i <= highI) and (path[i].X <= fRect.Left) do inc(i); |
| 546 |
if (i > highI) then Exit; |
| 547 |
if path[i].X >= fRect.Right then loc := locRight |
| 548 |
else if path[i].Y <= fRect.Top then loc := locTop |
| 549 |
else if path[i].Y >= fRect.Bottom then loc := locBottom |
| 550 |
else loc := locInside; |
| 551 |
end; |
| 552 |
locTop: |
| 553 |
begin |
| 554 |
while (i <= highI) and (path[i].Y <= fRect.Top) do inc(i); |
| 555 |
if (i > highI) then Exit; |
| 556 |
if path[i].Y >= fRect.Bottom then loc := locBottom |
| 557 |
else if path[i].X <= fRect.Left then loc := locLeft |
| 558 |
else if path[i].X >= fRect.Right then loc := locRight |
| 559 |
else loc := locInside; |
| 560 |
end; |
| 561 |
locRight: |
| 562 |
begin |
| 563 |
while (i <= highI) and (path[i].X >= fRect.Right) do inc(i); |
| 564 |
if (i > highI) then Exit; |
| 565 |
if path[i].X <= fRect.Left then loc := locLeft |
| 566 |
else if path[i].Y <= fRect.Top then loc := locTop |
| 567 |
else if path[i].Y >= fRect.Bottom then loc := locBottom |
| 568 |
else loc := locInside; |
| 569 |
end; |
| 570 |
locBottom: |
| 571 |
begin |
| 572 |
while (i <= highI) and (path[i].Y >= fRect.Bottom) do inc(i); |
| 573 |
if (i > highI) then Exit; |
| 574 |
if path[i].Y <= fRect.Top then loc := locTop |
| 575 |
else if path[i].X <= fRect.Left then loc := locLeft |
| 576 |
else if path[i].X >= fRect.Right then loc := locRight |
| 577 |
else loc := locInside; |
| 578 |
end; |
| 579 |
locInside: |
| 580 |
begin |
| 581 |
while (i <= highI) do |
| 582 |
begin |
| 583 |
if path[i].X < fRect.Left then loc := locLeft |
| 584 |
else if path[i].X > fRect.Right then loc := locRight |
| 585 |
else if path[i].Y > fRect.Bottom then loc := locBottom |
| 586 |
else if path[i].Y < fRect.Top then loc := locTop |
| 587 |
else begin Add(path[i]); inc(i); continue; end; |
| 588 |
break; //inner loop |
| 589 |
end; |
| 590 |
end; |
| 591 |
end; |
| 592 |
end; |
| 593 |
//------------------------------------------------------------------------------ |
| 594 |
|
| 595 |
function Path1ContainsPath2(const path1, path2: TPath64): Boolean; |
| 596 |
var |
| 597 |
i, ioCount: integer; |
| 598 |
pip: TPointInPolygonResult; |
| 599 |
begin |
| 600 |
ioCount := 0; |
| 601 |
for i := 0 to High(path2) do |
| 602 |
begin |
| 603 |
pip := PointInPolygon(path2[i], path1); |
| 604 |
case pip of |
| 605 |
pipOn: Continue; |
| 606 |
pipInside: dec(ioCount); |
| 607 |
pipOutside: inc(ioCount); |
| 608 |
end; |
| 609 |
if abs(ioCount) > 1 then break; |
| 610 |
end; |
| 611 |
Result := ioCount <= 0; |
| 612 |
end; |
| 613 |
//------------------------------------------------------------------------------ |
| 614 |
|
| 615 |
function TRectClip64.Execute(const paths: TPaths64): TPaths64; |
| 616 |
var |
| 617 |
i,j, len: integer; |
| 618 |
path: TPath64; |
| 619 |
begin |
| 620 |
result := nil; |
| 621 |
len:= Length(paths); |
| 622 |
for i := 0 to len -1 do |
| 623 |
begin |
| 624 |
path := paths[i]; |
| 625 |
if (Length(path) < 3) then Continue; |
| 626 |
|
| 627 |
fPathBounds := GetBounds(path); |
| 628 |
if not fRect.Intersects(fPathBounds) then |
| 629 |
Continue // the path must be completely outside fRect |
| 630 |
else if fRect.Contains(fPathBounds) then |
| 631 |
begin |
| 632 |
// the path must be completely inside fRect |
| 633 |
AppendPath(Result, path); |
| 634 |
Continue; |
| 635 |
end; |
| 636 |
|
| 637 |
ExecuteInternal(path); |
| 638 |
CheckEdges; |
| 639 |
for j := 0 to 3 do |
| 640 |
TidyEdgePair(j, fEdges[j*2], fEdges[j*2 +1]); |
| 641 |
|
| 642 |
for j := 0 to fResults.Count -1 do |
| 643 |
AppendPath(Result, GetPath(j)); |
| 644 |
|
| 645 |
//clean up after every loop |
| 646 |
DisposeResults; |
| 647 |
fEdges := nil; |
| 648 |
SetLength(fEdges, 8); |
| 649 |
end; |
| 650 |
end; |
| 651 |
//------------------------------------------------------------------------------ |
| 652 |
|
| 653 |
function StartLocsAreClockwise(const startLocs: TList): Boolean; |
| 654 |
var |
| 655 |
i,j, res: integer; |
| 656 |
begin |
| 657 |
res := 0; |
| 658 |
for i := 1 to startLocs.Count -1 do |
| 659 |
begin |
| 660 |
j := Ord(TLocation(startLocs[i])) - Ord(TLocation(startLocs[i - 1])); |
| 661 |
case j of |
| 662 |
-1: dec(res); |
| 663 |
1: inc(res); |
| 664 |
-3: inc(res); |
| 665 |
3: dec(res); |
| 666 |
end; |
| 667 |
end; |
| 668 |
result := res > 0; |
| 669 |
end; |
| 670 |
//------------------------------------------------------------------------------ |
| 671 |
|
| 672 |
procedure TRectClip64.ExecuteInternal(const path: TPath64); |
| 673 |
var |
| 674 |
i,j, highI : integer; |
| 675 |
prevPt,ip,ip2 : TPoint64; |
| 676 |
loc, prevLoc : TLocation; |
| 677 |
loc2 : TLocation; |
| 678 |
startingLoc : TLocation; |
| 679 |
firstCrossLoc : TLocation; |
| 680 |
crossingLoc : TLocation; |
| 681 |
prevCrossLoc : TLocation; |
| 682 |
isCw : Boolean; |
| 683 |
startLocsCW : Boolean; |
| 684 |
begin |
| 685 |
if (Length(path) < 3) then Exit; |
| 686 |
fStartLocs.Clear; |
| 687 |
crossingLoc := locInside; |
| 688 |
firstCrossLoc := locInside; |
| 689 |
prevLoc := locInside; |
| 690 |
|
| 691 |
highI := Length(path) -1; |
| 692 |
if not GetLocation(fRect, path[highI], loc) then |
| 693 |
begin |
| 694 |
i := highI - 1; |
| 695 |
while (i >= 0) and |
| 696 |
not GetLocation(fRect, path[i], prevLoc) do |
| 697 |
dec(i); |
| 698 |
|
| 699 |
if (i < 0) then |
| 700 |
begin |
| 701 |
// all of path must be inside fRect |
| 702 |
for i := 0 to highI do Add(path[i]); |
| 703 |
Exit; |
| 704 |
end; |
| 705 |
|
| 706 |
if (prevLoc = locInside) then |
| 707 |
loc := locInside; |
| 708 |
end; |
| 709 |
startingLoc := loc; |
| 710 |
|
| 711 |
/////////////////////////////////////////////////// |
| 712 |
i := 0; |
| 713 |
while i <= highI do |
| 714 |
begin |
| 715 |
prevLoc := loc; |
| 716 |
prevCrossLoc := crossingLoc; |
| 717 |
GetNextLocation(path, loc, i, highI); |
| 718 |
if i > highI then Break; |
| 719 |
|
| 720 |
if i = 0 then |
| 721 |
prevPt := path[highI] else |
| 722 |
prevPt := path[i-1]; |
| 723 |
crossingLoc := loc; |
| 724 |
if not GetIntersection(fRectPath, path[i], prevPt, crossingLoc, ip) then |
| 725 |
begin |
| 726 |
// ie remains outside (and crossingLoc still == loc) |
| 727 |
if (prevCrossLoc = locInside) then //ie rect still uncrossed |
| 728 |
begin |
| 729 |
isCw := IsClockwise(prevLoc, loc, prevPt, path[i], fRectMidPt); |
| 730 |
repeat |
| 731 |
fStartLocs.Add(Pointer(prevLoc)); |
| 732 |
prevLoc := GetAdjacentLocation(prevLoc, isCw); |
| 733 |
until prevLoc = loc; |
| 734 |
crossingLoc := prevCrossLoc; // because still not crossed |
| 735 |
end |
| 736 |
else if (prevLoc <> locInside) and (prevLoc <> loc) then |
| 737 |
begin |
| 738 |
isCw := IsClockwise(prevLoc, loc, prevPt, path[i], fRectMidPt); |
| 739 |
repeat |
| 740 |
AddCorner(prevLoc, isCw); |
| 741 |
until prevLoc = loc; |
| 742 |
end; |
| 743 |
inc(i); |
| 744 |
Continue; |
| 745 |
end; |
| 746 |
|
| 747 |
//////////////////////////////////////////////////// |
| 748 |
// we must be crossing the rect boundary to get here |
| 749 |
//////////////////////////////////////////////////// |
| 750 |
|
| 751 |
if (loc = locInside) then // path must be entering rect |
| 752 |
begin |
| 753 |
if (firstCrossLoc = locInside) then |
| 754 |
begin |
| 755 |
firstCrossLoc := crossingLoc; |
| 756 |
fStartLocs.Add(Pointer(prevLoc)); |
| 757 |
end |
| 758 |
else if (prevLoc <> crossingLoc) then |
| 759 |
begin |
| 760 |
isCw := IsClockwise(prevLoc, crossingLoc, prevPt, path[i], fRectMidPt); |
| 761 |
repeat |
| 762 |
AddCorner(prevLoc, isCw); |
| 763 |
until prevLoc = crossingLoc; |
| 764 |
end; |
| 765 |
end |
| 766 |
else if (prevLoc <> locInside) then |
| 767 |
begin |
| 768 |
// passing right through rect. 'ip' here will be the second |
| 769 |
// intersect pt but we'll also need the first intersect pt (ip2) |
| 770 |
loc := prevLoc; |
| 771 |
GetIntersection(fRectPath, prevPt, path[i], loc, ip2); |
| 772 |
if (prevCrossLoc <> locInside) and (prevCrossLoc <> loc) then //#579 |
| 773 |
AddCorner(prevCrossLoc, loc); |
| 774 |
|
| 775 |
if (firstCrossLoc = locInside) then |
| 776 |
begin |
| 777 |
firstCrossLoc := loc; |
| 778 |
fStartLocs.Add(Pointer(prevLoc)); |
| 779 |
end; |
| 780 |
|
| 781 |
//////////////////////////////// |
| 782 |
Add(ip2); |
| 783 |
//////////////////////////////// |
| 784 |
|
| 785 |
loc := crossingLoc; |
| 786 |
if PointsEqual(ip, ip2) then |
| 787 |
begin |
| 788 |
// it's very likely that path[i] is on rect |
| 789 |
GetLocation(fRect, path[i], loc); |
| 790 |
AddCorner(crossingLoc, loc); |
| 791 |
crossingLoc := loc; |
| 792 |
Continue; |
| 793 |
end; |
| 794 |
end else // path must be exiting rect |
| 795 |
begin |
| 796 |
loc := crossingLoc; |
| 797 |
if (firstCrossLoc = locInside) then |
| 798 |
firstCrossLoc := crossingLoc; |
| 799 |
end; |
| 800 |
|
| 801 |
//////////////////////////////// |
| 802 |
Add(ip); |
| 803 |
//////////////////////////////// |
| 804 |
|
| 805 |
end; //while i <= highI |
| 806 |
/////////////////////////////////////////////////// |
| 807 |
|
| 808 |
if (firstCrossLoc = locInside) then |
| 809 |
begin |
| 810 |
// path never intersects |
| 811 |
if startingLoc <> locInside then |
| 812 |
begin |
| 813 |
// path is outside rect |
| 814 |
// but being outside, it still may not contain rect |
| 815 |
if fPathBounds.Contains(fRect) and |
| 816 |
Path1ContainsPath2(path, fRectPath) then |
| 817 |
begin |
| 818 |
// yep, the path does fully contain rect |
| 819 |
// so add rect to the solution |
| 820 |
startLocsCW := StartLocsAreClockwise(fStartLocs); |
| 821 |
for i := 0 to 3 do |
| 822 |
begin |
| 823 |
if startLocsCW then j := i else j := 3 - i; |
| 824 |
Add(fRectPath[j]); |
| 825 |
AddToEdge(fEdges[j*2], fResults[0]); |
| 826 |
end; |
| 827 |
end; |
| 828 |
end; |
| 829 |
end |
| 830 |
else if (loc <> locInside) and |
| 831 |
((loc <> firstCrossLoc) or |
| 832 |
(fStartLocs.Count > 2)) then |
| 833 |
begin |
| 834 |
if (fStartLocs.Count > 0) then |
| 835 |
begin |
| 836 |
prevLoc := loc; |
| 837 |
for i := 0 to fStartLocs.Count -1 do |
| 838 |
begin |
| 839 |
loc2 := TLocation(fStartLocs[i]); |
| 840 |
if (prevLoc = loc2) then Continue; |
| 841 |
AddCorner(prevLoc, HeadingClockwise(prevLoc, loc2)); |
| 842 |
prevLoc := loc2; |
| 843 |
end; |
| 844 |
loc := prevLoc; |
| 845 |
end; |
| 846 |
if (loc <> firstCrossLoc) then |
| 847 |
AddCorner(loc, HeadingClockwise(loc, firstCrossLoc)); |
| 848 |
end; |
| 849 |
end; |
| 850 |
//------------------------------------------------------------------------------ |
| 851 |
|
| 852 |
procedure TRectClip64.CheckEdges; |
| 853 |
var |
| 854 |
i,j: integer; |
| 855 |
edgeSet1, edgeSet2, combinedSet: Cardinal; |
| 856 |
op, op2: POutPt2; |
| 857 |
begin |
| 858 |
for i := 0 to fResults.Count -1 do |
| 859 |
begin |
| 860 |
op := fResults[i]; |
| 861 |
if not assigned(op) then Continue; |
| 862 |
|
| 863 |
op2 := op; |
| 864 |
repeat |
| 865 |
if IsCollinear(op2.prev.pt, op2.pt, op2.next.pt) then |
| 866 |
begin |
| 867 |
if op2 = op then |
| 868 |
begin |
| 869 |
op2 := DisposeOpBack(op2); |
| 870 |
if not assigned(op2) then break; |
| 871 |
op := op2.prev; |
| 872 |
end else |
| 873 |
begin |
| 874 |
op2 := DisposeOpBack(op2); |
| 875 |
if not assigned(op2) then break; |
| 876 |
end; |
| 877 |
end else |
| 878 |
op2 := op2.next; |
| 879 |
until (op2 = op); |
| 880 |
|
| 881 |
if not assigned(op2) then |
| 882 |
begin |
| 883 |
fResults[i] := nil; |
| 884 |
Continue; |
| 885 |
end; |
| 886 |
fResults[i] := op; // safety first |
| 887 |
|
| 888 |
edgeSet1 := GetEdgesForPt(op.prev.pt, fRect); |
| 889 |
op2 := op; |
| 890 |
repeat |
| 891 |
edgeSet2 := GetEdgesForPt(op2.pt, fRect); |
| 892 |
if (edgeSet2 <> 0) and not Assigned(op2.edge) then |
| 893 |
begin |
| 894 |
combinedSet := edgeSet1 and edgeSet2; |
| 895 |
for j := 0 to 3 do |
| 896 |
if combinedSet and (1 shl j) <> 0 then |
| 897 |
begin |
| 898 |
if IsHeadingClockwise(op2.prev.pt, op2.pt, j) then |
| 899 |
AddToEdge(fEdges[j*2], op2) |
| 900 |
else |
| 901 |
AddToEdge(fEdges[j*2+1], op2); |
| 902 |
end; |
| 903 |
end; |
| 904 |
edgeSet1 := edgeSet2; |
| 905 |
op2 := op2.next; |
| 906 |
until op2 = op; |
| 907 |
end; |
| 908 |
end; |
| 909 |
//------------------------------------------------------------------------------ |
| 910 |
|
| 911 |
procedure TRectClip64.TidyEdgePair(idx: integer; var cw, ccw: TOutPtArray); |
| 912 |
var |
| 913 |
isHorz, cwIsTowardLarger: Boolean; |
| 914 |
i, j, highJ, newIdx: integer; |
| 915 |
op, op2, p1, p2, p1a, p2a: POutPt2; |
| 916 |
isRejoining, opIsLarger, op2IsLarger: Boolean; |
| 917 |
begin |
| 918 |
// cw and ccw must be passed as var params |
| 919 |
// otherwise they'll only be local copies. |
| 920 |
// Alternatively cw and ccw could be POutPtArray locals, |
| 921 |
// but these require lots of dereferencing. |
| 922 |
if not Assigned(ccw) then Exit; |
| 923 |
isHorz := idx in [1,3]; |
| 924 |
cwIsTowardLarger := idx in [1,2]; |
| 925 |
i := 0; j := 0; |
| 926 |
while (i <= High(cw)) do |
| 927 |
begin |
| 928 |
|
| 929 |
p1 := cw[i]; |
| 930 |
if not Assigned(p1) or (p1.next = p1.prev) then |
| 931 |
begin |
| 932 |
cw[i] := nil; |
| 933 |
inc(i); |
| 934 |
j := 0; |
| 935 |
Continue; |
| 936 |
end; |
| 937 |
|
| 938 |
highJ := high(ccw); |
| 939 |
while (j <= highJ) and |
| 940 |
(not Assigned(ccw[j]) or (ccw[j].next = ccw[j].prev)) do |
| 941 |
inc(j); |
| 942 |
|
| 943 |
if (j > highJ) then |
| 944 |
begin |
| 945 |
inc(i); |
| 946 |
j := 0; |
| 947 |
Continue; |
| 948 |
end; |
| 949 |
|
| 950 |
if cwIsTowardLarger then |
| 951 |
begin |
| 952 |
// p1 >>>> p1a; |
| 953 |
// p2 <<<< p2a; |
| 954 |
p1 := cw[i].prev; |
| 955 |
p1a := cw[i]; |
| 956 |
p2 := ccw[j]; |
| 957 |
p2a := ccw[j].prev; |
| 958 |
end else |
| 959 |
begin |
| 960 |
// p1 <<<< p1a; |
| 961 |
// p2 >>>> p2a; |
| 962 |
p1 := cw[i]; |
| 963 |
p1a := cw[i].prev; |
| 964 |
p2 := ccw[j].prev; |
| 965 |
p2a := ccw[j]; |
| 966 |
end; |
| 967 |
|
| 968 |
if (isHorz and not HasHorzOverlap(p1.pt, p1a.pt, p2.pt, p2a.pt)) or |
| 969 |
(not isHorz and not HasVertOverlap(p1.pt, p1a.pt, p2.pt, p2a.pt)) then |
| 970 |
begin |
| 971 |
inc(j); |
| 972 |
Continue; |
| 973 |
end; |
| 974 |
|
| 975 |
// to get here we're either splitting or rejoining |
| 976 |
isRejoining := cw[i].ownerIdx <> ccw[j].ownerIdx; |
| 977 |
|
| 978 |
if isRejoining then |
| 979 |
begin |
| 980 |
fResults[p2.ownerIdx] := nil; |
| 981 |
SetNewOwner(p2, p1.ownerIdx); |
| 982 |
end; |
| 983 |
|
| 984 |
// do the split or re-join |
| 985 |
if cwIsTowardLarger then |
| 986 |
begin |
| 987 |
// p1 >> | >> p1a; |
| 988 |
// p2 << | << p2a; |
| 989 |
p1.next := p2; |
| 990 |
p2.prev := p1; |
| 991 |
p1a.prev := p2a; |
| 992 |
p2a.next := p1a; |
| 993 |
end else |
| 994 |
begin |
| 995 |
// p1 << | << p1a; |
| 996 |
// p2 >> | >> p2a; |
| 997 |
p1.prev := p2; |
| 998 |
p2.next := p1; |
| 999 |
p1a.next := p2a; |
| 1000 |
p2a.prev := p1a; |
| 1001 |
end; |
| 1002 |
|
| 1003 |
if not isRejoining then |
| 1004 |
begin |
| 1005 |
NewIdx := fResults.Add(p1a); |
| 1006 |
SetNewOwner(p1a, newIdx); |
| 1007 |
end; |
| 1008 |
|
| 1009 |
if cwIsTowardLarger then |
| 1010 |
begin |
| 1011 |
op := p2; |
| 1012 |
op2 := p1a; |
| 1013 |
end else |
| 1014 |
begin |
| 1015 |
op := p1; |
| 1016 |
op2 := p2a; |
| 1017 |
end; |
| 1018 |
|
| 1019 |
fResults[op.ownerIdx] := op; |
| 1020 |
fResults[op2.ownerIdx] := op2; |
| 1021 |
|
| 1022 |
// and now lots of work to get ready for the next loop |
| 1023 |
|
| 1024 |
if isHorz then // X |
| 1025 |
begin |
| 1026 |
opIsLarger := op.pt.X > op.prev.pt.X; |
| 1027 |
op2IsLarger := op2.pt.X > op2.prev.pt.X; |
| 1028 |
end else // Y |
| 1029 |
begin |
| 1030 |
opIsLarger := op.pt.Y > op.prev.pt.Y; |
| 1031 |
op2IsLarger := op2.pt.Y > op2.prev.pt.Y; |
| 1032 |
end; |
| 1033 |
|
| 1034 |
if (op.next = op.prev) or |
| 1035 |
PointsEqual(op.pt, op.prev.pt) then |
| 1036 |
begin |
| 1037 |
if op2IsLarger = cwIsTowardLarger then |
| 1038 |
begin |
| 1039 |
cw[i] := op2; |
| 1040 |
ccw[j] := nil; |
| 1041 |
inc(j); |
| 1042 |
end else |
| 1043 |
begin |
| 1044 |
ccw[j] := op2; |
| 1045 |
cw[i] := nil; |
| 1046 |
inc(i); |
| 1047 |
end; |
| 1048 |
end |
| 1049 |
else if (op2.next = op2.prev) or |
| 1050 |
PointsEqual(op2.pt, op2.prev.pt) then |
| 1051 |
begin |
| 1052 |
if opIsLarger = cwIsTowardLarger then |
| 1053 |
begin |
| 1054 |
cw[i] := op; |
| 1055 |
ccw[j] := nil; |
| 1056 |
inc(j); |
| 1057 |
end else |
| 1058 |
begin |
| 1059 |
ccw[j] := op; |
| 1060 |
cw[i] := nil; |
| 1061 |
inc(i); |
| 1062 |
end; |
| 1063 |
end |
| 1064 |
else if opIsLarger = op2IsLarger then |
| 1065 |
begin |
| 1066 |
if opIsLarger = cwIsTowardLarger then |
| 1067 |
begin |
| 1068 |
cw[i] := op; |
| 1069 |
UncoupleEdge(op2); |
| 1070 |
AddToEdge(cw, op2); |
| 1071 |
ccw[j] := nil; |
| 1072 |
inc(j); |
| 1073 |
end else |
| 1074 |
begin |
| 1075 |
cw[i] := nil; |
| 1076 |
ccw[j] := op2; |
| 1077 |
UncoupleEdge(op); |
| 1078 |
AddToEdge(ccw, op); |
| 1079 |
inc(i); |
| 1080 |
j := 0; |
| 1081 |
end; |
| 1082 |
end else |
| 1083 |
begin |
| 1084 |
if opIsLarger = cwIsTowardLarger then |
| 1085 |
cw[i] := op else |
| 1086 |
ccw[j] := op; |
| 1087 |
if op2IsLarger = cwIsTowardLarger then |
| 1088 |
cw[i] := op2 else |
| 1089 |
ccw[j] := op2; |
| 1090 |
end; |
| 1091 |
end; |
| 1092 |
end; |
| 1093 |
//------------------------------------------------------------------------------ |
| 1094 |
|
| 1095 |
function TRectClip64.GetPath(resultIdx: integer): TPath64; |
| 1096 |
var |
| 1097 |
i, len: integer; |
| 1098 |
op, op2: POutPt2; |
| 1099 |
begin |
| 1100 |
result := nil; |
| 1101 |
op := fResults[resultIdx]; |
| 1102 |
if not Assigned(op) or (op.next = op.prev) then Exit; |
| 1103 |
|
| 1104 |
op2 := op.next; |
| 1105 |
while Assigned(op2) and (op2 <> op) do |
| 1106 |
begin |
| 1107 |
if IsCollinear(op2.prev.pt, op2.pt, op2.next.pt) then |
| 1108 |
begin |
| 1109 |
op := op2.prev; |
| 1110 |
op2 := DisposeOp(op2); |
| 1111 |
end else |
| 1112 |
op2 := op2.next; |
| 1113 |
end; |
| 1114 |
fResults[resultIdx] := op2; // needed for op cleanup |
| 1115 |
if not Assigned(op2) then Exit; |
| 1116 |
|
| 1117 |
len := CountOp(op); |
| 1118 |
SetLength(result, len); |
| 1119 |
for i := 0 to len -1 do |
| 1120 |
begin |
| 1121 |
Result[i] := op.pt; |
| 1122 |
op := op.next; |
| 1123 |
end; |
| 1124 |
end; |
| 1125 |
|
| 1126 |
//------------------------------------------------------------------------------ |
| 1127 |
// TRectClipLines64 |
| 1128 |
//------------------------------------------------------------------------------ |
| 1129 |
|
| 1130 |
function TRectClipLines64.Execute(const paths: TPaths64): TPaths64; |
| 1131 |
var |
| 1132 |
i,j, len: integer; |
| 1133 |
pathrec: TRect64; |
| 1134 |
begin |
| 1135 |
result := nil; |
| 1136 |
|
| 1137 |
len:= Length(paths); |
| 1138 |
for i := 0 to len -1 do |
| 1139 |
begin |
| 1140 |
pathrec := GetBounds(paths[i]); |
| 1141 |
|
| 1142 |
if not fRect.Intersects(pathRec) then |
| 1143 |
Continue; // the path must be completely outside fRect |
| 1144 |
// Apart from that, we can't be sure whether the path |
| 1145 |
// is completely outside or completed inside or intersects |
| 1146 |
// fRect, simply by comparing path bounds with fRect. |
| 1147 |
|
| 1148 |
ExecuteInternal(paths[i]); |
| 1149 |
|
| 1150 |
for j := 0 to fResults.Count -1 do |
| 1151 |
AppendPath(Result, GetPath(j)); |
| 1152 |
DisposeResults; |
| 1153 |
fEdges := nil; |
| 1154 |
SetLength(fEdges, 8); |
| 1155 |
end; |
| 1156 |
end; |
| 1157 |
//------------------------------------------------------------------------------ |
| 1158 |
|
| 1159 |
procedure TRectClipLines64.ExecuteInternal(const path: TPath64); |
| 1160 |
var |
| 1161 |
i, highI : integer; |
| 1162 |
prevPt,ip,ip2 : TPoint64; |
| 1163 |
loc, prev : TLocation; |
| 1164 |
crossingLoc : TLocation; |
| 1165 |
begin |
| 1166 |
if (Length(path) < 2) or fRect.IsEmpty then Exit; |
| 1167 |
|
| 1168 |
i := 1; |
| 1169 |
highI := Length(path) -1; |
| 1170 |
|
| 1171 |
if not GetLocation(fRect, path[0], loc) then |
| 1172 |
begin |
| 1173 |
while (i <= highI) and |
| 1174 |
not GetLocation(fRect, path[i], prev) do |
| 1175 |
inc(i); |
| 1176 |
if (i > highI) then |
| 1177 |
begin |
| 1178 |
for i := 0 to High(path) do Add(path[i]); |
| 1179 |
Exit; |
| 1180 |
end; |
| 1181 |
if (prev = locInside) then |
| 1182 |
loc := locInside; |
| 1183 |
i := 1; |
| 1184 |
end; |
| 1185 |
if loc = locInside then Add(path[0]); |
| 1186 |
|
| 1187 |
/////////////////////////////////////////////////// |
| 1188 |
while i <= highI do |
| 1189 |
begin |
| 1190 |
prev := loc; |
| 1191 |
GetNextLocation(path, loc, i, highI); |
| 1192 |
if i > highI then Break; |
| 1193 |
prevPt := path[i-1]; |
| 1194 |
crossingLoc := loc; |
| 1195 |
if not GetIntersection(fRectPath, path[i], prevPt, crossingLoc, ip) then |
| 1196 |
begin |
| 1197 |
// must be remaining outside |
| 1198 |
inc(i); |
| 1199 |
Continue; |
| 1200 |
end; |
| 1201 |
|
| 1202 |
//////////////////////////////////////////////////// |
| 1203 |
// we must be crossing the rect boundary to get here |
| 1204 |
//////////////////////////////////////////////////// |
| 1205 |
|
| 1206 |
if (loc = locInside) then // path must be entering rect |
| 1207 |
begin |
| 1208 |
Add(ip, true); |
| 1209 |
end |
| 1210 |
else if (prev <> locInside) then |
| 1211 |
begin |
| 1212 |
// passing right through rect. 'ip' here will be the second |
| 1213 |
// intersect pt but we'll also need the first intersect pt (ip2) |
| 1214 |
crossingLoc := prev; |
| 1215 |
GetIntersection(fRectPath, prevPt, path[i], crossingLoc, ip2); |
| 1216 |
Add(ip2, true); |
| 1217 |
Add(ip); |
| 1218 |
end else // path must be exiting rect |
| 1219 |
Add(ip); |
| 1220 |
end; //while i <= highI |
| 1221 |
/////////////////////////////////////////////////// |
| 1222 |
end; |
| 1223 |
//------------------------------------------------------------------------------ |
| 1224 |
|
| 1225 |
function TRectClipLines64.GetPath(resultIdx: integer): TPath64; |
| 1226 |
var |
| 1227 |
i, len: integer; |
| 1228 |
op: POutPt2; |
| 1229 |
begin |
| 1230 |
result := nil; |
| 1231 |
op := fResults[resultIdx]; |
| 1232 |
if not Assigned(op) or (op = op.prev) then Exit; |
| 1233 |
len := CountOp(op); |
| 1234 |
op := op.next; // ie start at first not last |
| 1235 |
SetLength(result, len); |
| 1236 |
for i := 0 to len -1 do |
| 1237 |
begin |
| 1238 |
Result[i] := op.pt; |
| 1239 |
op := op.next; |
| 1240 |
end; |
| 1241 |
end; |
| 1242 |
|
| 1243 |
//------------------------------------------------------------------------------ |
| 1244 |
//------------------------------------------------------------------------------ |
| 1245 |
|
| 1246 |
end. |
| 1247 |
|