Debian Codegraph @

components/Image32/source/Clipper2/Clipper.Offset.pas

raw
1 unit Clipper.Offset;
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 : Path Offset (Inflate/Shrink) *
9 * License : http://www.boost.org/LICENSE_1_0.txt *
10 *******************************************************************************)
11
12 {$I Clipper.inc}
13
14 interface
15
16 uses
17 Classes, Clipper.Core, Clipper.Engine;
18
19 type
20
21 TJoinType = (jtMiter, jtSquare, jtBevel, jtRound);
22 //jtSquare: Joins are 'squared' at exactly the offset distance (complex code)
23 //jtBevel : offset distances vary depending on the angle (simple code, faster)
24
25 TEndType = (etPolygon, etJoined, etButt, etSquare, etRound);
26 // etButt : offsets both sides of a path, with square blunt ends
27 // etSquare : offsets both sides of a path, with square extended ends
28 // etRound : offsets both sides of a path, with round extended ends
29 // etJoined : offsets both sides of a path, with joined ends
30 // etPolygon: offsets only one side of a closed path
31
32 TDeltaCallback64 = function (const path: TPath64;
33 const path_norms: TPathD; currIdx, prevIdx: integer): double of object;
34
35 TDoubleArray = array of double;
36 BooleanArray = array of Boolean;
37
38 TGroup = class
39 paths : TPaths64;
40 joinType : TJoinType;
41 endType : TEndType;
42 reversed : Boolean;
43 lowestPathIdx : integer;
44 constructor Create(const pathsIn: TPaths64; jt: TJoinType; et: TEndType);
45 end;
46
47 TClipperOffset = class
48 private
49 fDelta : Double;
50 fGroupDelta : Double; //*0.5 for open paths; *-1.0 for neg areas
51 fMinLenSqrd : double;
52 fJoinType : TJoinType;
53 fEndType : TEndType;
54 fTmpLimit : Double;
55 fMiterLimit : Double;
56 fArcTolerance : Double;
57 fStepsPerRad : Double;
58 fStepSin : Double;
59 fStepCos : Double;
60 fNorms : TPathD;
61 fGroupList : TListEx;
62 fInPath : TPath64;
63 fOutPath : TPath64;
64 fOutPathLen : Integer;
65 fSolution : TPaths64;
66 fSolutionLen : Integer;
67 fSolutionTree : TPolyTree64;
68 fPreserveCollinear : Boolean;
69 fReverseSolution : Boolean;
70 fDeltaCallback64 : TDeltaCallback64;
71 {$IFDEF USINGZ}
72 fZCallback64 : TZCallback64;
73 procedure ZCB(const bot1, top1, bot2, top2: TPoint64;
74 var intersectPt: TPoint64);
75 procedure AddPoint(x,y: double; z: ZType); overload;
76 procedure AddPoint(const pt: TPoint64); overload;
77 {$IFDEF INLINING} inline; {$ENDIF}
78 procedure AddPoint(const pt: TPoint64; newZ: ZType); overload;
79 {$IFDEF INLINING} inline; {$ENDIF}
80 {$ELSE}
81 procedure AddPoint(x,y: double); overload;
82
83 procedure AddPoint(const pt: TPoint64); overload;
84 {$IFDEF INLINING} inline; {$ENDIF}
85 {$ENDIF}
86 procedure DoSquare(j, k: Integer);
87 procedure DoBevel(j, k: Integer);
88 procedure DoMiter(j, k: Integer; cosA: Double);
89 procedure DoRound(j, k: integer; angle: double);
90 procedure OffsetPoint(j: Integer; var k: integer);
91
92 procedure BuildNormals;
93 procedure DoGroupOffset(group: TGroup);
94 procedure OffsetPolygon;
95 procedure OffsetOpenJoined;
96 procedure OffsetOpenPath;
97 function CalcSolutionCapacity: integer;
98 procedure UpdateSolution; {$IFDEF INLINING} inline; {$ENDIF}
99
100 function CheckReverseOrientation: Boolean;
101 procedure ExecuteInternal(delta: Double);
102 public
103 constructor Create(miterLimit: double = 2.0;
104 arcTolerance: double = 0.0;
105 PreserveCollinear: Boolean = False;
106 ReverseSolution: Boolean = False);
107 destructor Destroy; override;
108 procedure AddPath(const path: TPath64;
109 joinType: TJoinType; endType: TEndType);
110 procedure AddPaths(const paths: TPaths64;
111 joinType: TJoinType; endType: TEndType);
112 procedure Clear;
113 procedure Execute(delta: Double; out solution: TPaths64); overload;
114 procedure Execute(delta: Double; polytree: TPolyTree64); overload;
115 procedure Execute(DeltaCallback: TDeltaCallback64; out solution: TPaths64); overload;
116
117 // MiterLimit: needed for mitered offsets (see offset_triginometry3.svg)
118 property MiterLimit: Double read fMiterLimit write fMiterLimit;
119 // ArcTolerance: needed for rounded offsets (See offset_triginometry2.svg)
120 property ArcTolerance: Double read fArcTolerance write fArcTolerance;
121 property PreserveCollinear: Boolean
122 read fPreserveCollinear write fPreserveCollinear;
123 property ReverseSolution: Boolean
124 read fReverseSolution write fReverseSolution;
125 property DeltaCallback: TDeltaCallback64 read
126 fDeltaCallback64 write fDeltaCallback64;
127 {$IFDEF USINGZ}
128 property ZCallback: TZCallback64 read fZCallback64 write fZCallback64;
129 {$ENDIF}
130 end;
131
132 implementation
133
134 uses
135 Math;
136
137 resourcestring
138 rsClipper_CoordRangeError =
139 'Offsetting will exceed the valid coordinate range';
140
141 const
142 TwoPi : Double = 2 * PI;
143 InvTwoPi : Double = 1/(2 * PI);
144
145 //------------------------------------------------------------------------------
146 // Miscellaneous offset support functions
147 //------------------------------------------------------------------------------
148
149 function DotProduct(const vec1, vec2: TPointD): double;
150 {$IFDEF INLINING} inline; {$ENDIF}
151 begin
152 result := vec1.X * vec2.X + vec1.Y * vec2.Y;
153 end;
154 //------------------------------------------------------------------------------
155
156 function ValueAlmostZero(val: double; epsilon: double = 0.001): Boolean;
157 {$IFDEF INLINE} inline; {$ENDIF}
158 begin
159 Result := Abs(val) < epsilon;
160 end;
161 //------------------------------------------------------------------------------
162
163 function NormalizeVector(const vec: TPointD): TPointD;
164 {$IFDEF INLINE} inline; {$ENDIF}
165 var
166 h, inverseHypot: Double;
167 begin
168 h := Hypot(vec.X, vec.Y);
169 if ValueAlmostZero(h) then
170 begin
171 Result := NullPointD;
172 Exit;
173 end;
174 inverseHypot := 1 / h;
175 Result.X := vec.X * inverseHypot;
176 Result.Y := vec.Y * inverseHypot;
177 end;
178 //------------------------------------------------------------------------------
179
180 function GetAvgUnitVector(const vec1, vec2: TPointD): TPointD;
181 begin
182 Result := NormalizeVector(PointD(vec1.X + vec2.X, vec1.Y + vec2.Y));
183 end;
184 //------------------------------------------------------------------------------
185
186 function GetUnitNormal(const pt1, pt2: TPoint64): TPointD;
187 var
188 dx, dy, inverseHypot: Double;
189 begin
190 dx := (pt2.X - pt1.X);
191 dy := (pt2.Y - pt1.Y);
192 if (dx = 0) and (dy = 0) then
193 begin
194 Result.X := 0;
195 Result.Y := 0;
196 end else
197 begin
198 inverseHypot := 1 / Hypot(dx, dy);
199 Result.X := dy * inverseHypot;
200 Result.Y := -dx * inverseHypot; //ie left side of vector
201 end;
202 end;
203 //------------------------------------------------------------------------------
204
205 function GetLowestPolygonIdx(const paths: TPaths64): integer;
206 var
207 i,j: integer;
208 botPt: TPoint64;
209 begin
210 Result := -1;
211 botPt := Point64(MaxInt64, MinInt64);
212 for i := 0 to High(paths) do
213 begin
214 for j := 0 to High(paths[i]) do
215 with paths[i][j] do
216 begin
217 if (Y < botPt.Y) or
218 ((Y = botPt.Y) and (X >= botPt.X)) then Continue;
219 result := i;
220 botPt.X := X;
221 botPt.Y := Y;
222 end;
223 end;
224 end;
225 //------------------------------------------------------------------------------
226
227 function UnsafeGet(List: TList; Index: Integer): Pointer;
228 {$IFDEF INLINING} inline; {$ENDIF}
229 begin
230 Result := List.List[Index];
231 end;
232
233 //------------------------------------------------------------------------------
234 // TGroup methods
235 //------------------------------------------------------------------------------
236
237 constructor TGroup.Create(const pathsIn: TPaths64; jt: TJoinType; et: TEndType);
238 var
239 i, len: integer;
240 isJoined: boolean;
241 begin
242 Self.joinType := jt;
243 Self.endType := et;
244
245 isJoined := et in [etPolygon, etJoined];
246 len := Length(pathsIn);
247 SetLength(paths, len);
248 for i := 0 to len -1 do
249 paths[i] := StripDuplicates(pathsIn[i], isJoined);
250
251 reversed := false;
252 if (et = etPolygon) then
253 begin
254 // the lowermost path must be an outer path, so if its orientation is
255 // negative, then flag that the whole group is 'reversed' (so negate
256 // delta etc.) as this is much more efficient than reversing every path.
257 lowestPathIdx := GetLowestPolygonIdx(pathsIn);
258 reversed := (lowestPathIdx >= 0) and (Area(pathsIn[lowestPathIdx]) < 0);
259 end else
260 lowestPathIdx := -1;
261 end;
262
263 //------------------------------------------------------------------------------
264 // TClipperOffset methods
265 //------------------------------------------------------------------------------
266
267 constructor TClipperOffset.Create(miterLimit: double;
268 arcTolerance: double; PreserveCollinear: Boolean;
269 ReverseSolution: Boolean);
270 begin
271 fMiterLimit := MiterLimit;
272 fArcTolerance := ArcTolerance;
273 fGroupList := TListEx.Create;
274 fPreserveCollinear := preserveCollinear;
275 fReverseSolution := ReverseSolution;
276 end;
277 //------------------------------------------------------------------------------
278
279 destructor TClipperOffset.Destroy;
280 begin
281 Clear;
282 fGroupList.Free;
283 inherited;
284 end;
285 //------------------------------------------------------------------------------
286
287 procedure TClipperOffset.Clear;
288 var
289 i: integer;
290 begin
291 for i := 0 to fGroupList.Count -1 do
292 TGroup(fGroupList[i]).Free;
293 fGroupList.Clear;
294 fSolution := nil;
295 fSolutionLen := 0;
296 end;
297 //------------------------------------------------------------------------------
298
299 procedure TClipperOffset.AddPath(const path: TPath64;
300 joinType: TJoinType; endType: TEndType);
301 var
302 paths: TPaths64;
303 begin
304 if not assigned(path) then Exit;
305 SetLength(paths, 1);
306 paths[0] := path;
307 AddPaths(Paths, joinType, endType);
308 end;
309 //------------------------------------------------------------------------------
310
311 procedure TClipperOffset.AddPaths(const paths: TPaths64;
312 joinType: TJoinType; endType: TEndType);
313 var
314 group: TGroup;
315 begin
316 if Length(paths) = 0 then Exit;
317 group := TGroup.Create(paths, joinType, endType);
318 fGroupList.Add(group);
319 end;
320 //------------------------------------------------------------------------------
321
322 function GetPerpendic(const pt: TPoint64; const norm: TPointD; delta: double): TPoint64; overload;
323 {$IFDEF INLINING} inline; {$ENDIF}
324 begin
325 result := Point64(pt.X + norm.X * delta, pt.Y + norm.Y * delta);
326 {$IFDEF USINGZ}
327 result.Z := pt.Z;
328 {$ENDIF}
329 end;
330 //------------------------------------------------------------------------------
331
332 function GetPerpendicD(const pt: TPoint64; const norm: TPointD; delta: double): TPointD; overload;
333 {$IFDEF INLINING} inline; {$ENDIF}
334 begin
335 result := PointD(pt.X + norm.X * delta, pt.Y + norm.Y * delta);
336 {$IFDEF USINGZ}
337 result.Z := pt.Z;
338 {$ENDIF}
339 end;
340 //------------------------------------------------------------------------------
341
342 procedure TClipperOffset.DoGroupOffset(group: TGroup);
343 var
344 i,j, len, steps: Integer;
345 r, stepsPer360, arcTol: Double;
346 absDelta: double;
347 rec: TRect64;
348 pt0: TPoint64;
349 begin
350
351 if group.endType = etPolygon then
352 begin
353 if (group.lowestPathIdx < 0) then fDelta := Abs(fDelta);
354 fGroupDelta := Iif(group.reversed, -fDelta, fDelta);
355 end
356 else
357 fGroupDelta := Abs(fDelta);
358
359 absDelta := Abs(fGroupDelta);
360
361 fJoinType := group.joinType;
362 fEndType := group.endType;
363
364 if (group.joinType = jtRound) or (group.endType = etRound) then
365 begin
366 // calculate the number of steps required to approximate a circle
367 // (see http://www.angusj.com/clipper2/Docs/Trigonometry.htm)
368 // arcTol - when arc_tolerance_ is undefined (0) then curve imprecision
369 // will be relative to the size of the offset (delta). Obviously very
370 //large offsets will almost always require much less precision.
371 arcTol := Iif(fArcTolerance > 0.01,
372 Min(absDelta, fArcTolerance),
373 Log10(2 + absDelta) * 0.25); // empirically derived
374
375 stepsPer360 := Pi / ArcCos(1 - arcTol / absDelta);
376 if (stepsPer360 > absDelta * Pi) then
377 stepsPer360 := absDelta * Pi; // avoid excessive precision
378 fStepSin := sin(TwoPi/stepsPer360);
379 fStepCos := cos(TwoPi/stepsPer360);
380 if (fGroupDelta < 0.0) then fStepSin := -fStepSin;
381 fStepsPerRad := stepsPer360 / TwoPi;
382 end;
383
384 for i := 0 to High(group.paths) do
385 begin
386 fInPath := group.paths[i];
387 fNorms := nil;
388 len := Length(fInPath);
389
390 //if a single vertex then build a circle or a square ...
391 if len = 1 then
392 begin
393 if fGroupDelta < 1 then Continue;
394 pt0 := fInPath[0];
395
396 if Assigned(fDeltaCallback64) then
397 begin
398 fGroupDelta := fDeltaCallback64(fInPath, fNorms, 0, 0);
399 if TGroup(fGroupList[0]).reversed then fGroupDelta := -fGroupDelta;
400 absDelta := Abs(fGroupDelta);
401 end;
402
403 if (group.endType = etRound) then
404 begin
405 r := absDelta;
406 steps := Ceil(fStepsPerRad * TwoPi); //#617
407 fOutPath := Path64(Ellipse(
408 RectD(pt0.X-r, pt0.Y-r, pt0.X+r, pt0.Y+r), steps));
409 {$IFDEF USINGZ}
410 for j := 0 to high(fOutPath) do
411 fOutPath[j].Z := pt0.Z;
412 {$ENDIF}
413 end else
414 begin
415 j := Round(absDelta);
416 rec := Rect64(pt0.X -j, pt0.Y -j, pt0.X+j, pt0.Y+j);
417 fOutPath := rec.AsPath;
418 {$IFDEF USINGZ}
419 for j := 0 to high(fOutPath) do
420 fOutPath[j].Z := pt0.Z;
421 {$ENDIF}
422 end;
423 UpdateSolution;
424 Continue;
425 end; // end of offsetting a single point
426
427 if (len = 2) and (group.endType = etJoined) then
428 begin
429 if fJoinType = jtRound then
430 fEndType := etRound else
431 fEndType := etSquare;
432 end;
433
434 BuildNormals;
435 if fEndType = etPolygon then
436 OffsetPolygon
437 else if fEndType = etJoined then
438 OffsetOpenJoined
439 else
440 OffsetOpenPath;
441 end;
442 end;
443 //------------------------------------------------------------------------------
444
445 procedure TClipperOffset.BuildNormals;
446 var
447 i, len: integer;
448 begin
449 len := Length(fInPath);
450 SetLength(fNorms, len);
451 if len = 0 then Exit;
452 for i := 0 to len-2 do
453 fNorms[i] := GetUnitNormal(fInPath[i], fInPath[i+1]);
454 fNorms[len -1] := GetUnitNormal(fInPath[len -1], fInPath[0]);
455 end;
456 //------------------------------------------------------------------------------
457
458 procedure TClipperOffset.UpdateSolution;
459 begin
460 if fOutPathLen = 0 then Exit;
461 SetLength(fOutPath, fOutPathLen);
462 fSolution[fSolutionLen] := fOutPath;
463 inc(fSolutionLen);
464 fOutPath := nil;
465 fOutPathLen := 0;
466 end;
467 //------------------------------------------------------------------------------
468
469 function TClipperOffset.CalcSolutionCapacity: integer;
470 var
471 i: integer;
472 begin
473 Result := 0;
474 for i := 0 to fGroupList.Count -1 do
475 with TGroup(fGroupList[i]) do
476 if endType = etJoined then
477 inc(Result, Length(paths) *2) else
478 inc(Result, Length(paths));
479 end;
480 //------------------------------------------------------------------------------
481
482 procedure TClipperOffset.OffsetPolygon;
483 var
484 i,j: integer;
485 begin
486 j := high(fInPath);
487 for i := 0 to high(fInPath) do
488 OffsetPoint(i, j);
489 UpdateSolution;
490 end;
491 //------------------------------------------------------------------------------
492
493 procedure TClipperOffset.OffsetOpenJoined;
494 begin
495 OffsetPolygon;
496 fInPath := ReversePath(fInPath);
497 // Rebuild normals // BuildNormals;
498 fNorms := ReversePath(fNorms);
499 fNorms := ShiftPath(fNorms, 1);
500 fNorms := NegatePath(fNorms);
501 OffsetPolygon;
502 end;
503 //------------------------------------------------------------------------------
504
505 procedure TClipperOffset.OffsetOpenPath;
506 var
507 i, k, highI: integer;
508 begin
509 highI := high(fInPath);
510
511 if Assigned(fDeltaCallback64) then
512 fGroupDelta := fDeltaCallback64(fInPath, fNorms, 0, 0);
513
514 if (Abs(fGroupDelta) < Tolerance) and
515 not Assigned(fDeltaCallback64) then
516 begin
517 inc(highI);
518 SetLength(fOutPath, highI);
519 Move(fInPath[0], fOutPath, highI + SizeOf(TPointD));
520 fOutPathLen := highI;
521 Exit;
522 end;
523
524 // do the line start cap
525 if Assigned(fDeltaCallback64) then
526 fGroupDelta := fDeltaCallback64(fInPath, fNorms, 0, 0);
527
528 if (Abs(fGroupDelta) < Tolerance) then
529 AddPoint(fInPath[0])
530 else
531 case fEndType of
532 etButt: DoBevel(0, 0);
533 etRound: DoRound(0,0, PI);
534 else DoSquare(0, 0);
535 end;
536
537 // offset the left side going forward
538 k := 0;
539 for i := 1 to highI -1 do //nb: -1 is important
540 OffsetPoint(i, k);
541
542 // reverse the normals ...
543 for i := HighI downto 1 do
544 begin
545 fNorms[i].X := -fNorms[i-1].X;
546 fNorms[i].Y := -fNorms[i-1].Y;
547 end;
548 fNorms[0] := fNorms[highI];
549
550 // do the line end cap
551
552 if Assigned(fDeltaCallback64) then
553 fGroupDelta := fDeltaCallback64(fInPath, fNorms, highI, highI);
554 if Abs(fGroupDelta) < Tolerance then
555 begin
556 AddPoint(fInPath[highI]);
557 end else
558 case fEndType of
559 etButt: DoBevel(highI, highI);
560 etRound: DoRound(highI,highI, PI);
561 else DoSquare(highI, highI);
562 end;
563
564 // offset the left side going back
565 k := highI;
566 for i := highI -1 downto 1 do //and stop at 1!
567 OffsetPoint(i, k);
568
569 UpdateSolution;
570 end;
571 //------------------------------------------------------------------------------
572
573 procedure TClipperOffset.ExecuteInternal(delta: Double);
574 var
575 i,j: integer;
576 group: TGroup;
577 pathsReversed: Boolean;
578 fillRule: TFillRule;
579 dummy: TPaths64;
580 begin
581 fSolution := nil;
582 fSolutionLen := 0;
583 if fGroupList.Count = 0 then Exit;
584 SetLength(fSolution, CalcSolutionCapacity);
585
586 fMinLenSqrd := 1;
587 if abs(delta) < Tolerance then
588 begin
589 // if delta == 0, just copy paths to Result
590 for i := 0 to fGroupList.Count -1 do
591 begin
592 group := TGroup(fGroupList[i]);
593 for j := 0 to High(group.paths) do
594 begin
595 fSolution[fSolutionLen] := group.paths[i];
596 inc(fSolutionLen);
597 end;
598 end;
599 Exit;
600 end;
601
602 fDelta := delta;
603 // Miter Limit: see offset_triginometry3.svg
604 if fMiterLimit > 1 then
605 fTmpLimit := 2 / Sqr(fMiterLimit) else
606 fTmpLimit := 2.0;
607
608 // nb: delta will depend on whether paths are polygons or open
609 for i := 0 to fGroupList.Count -1 do
610 begin
611 group := TGroup(fGroupList[i]);
612 DoGroupOffset(group);
613 end;
614 SetLength(fSolution, fSolutionLen);
615
616 pathsReversed := CheckReverseOrientation();
617 if pathsReversed then
618 fillRule := frNegative else
619 fillRule := frPositive;
620
621 // clean up self-intersections ...
622 with TClipper64.Create do
623 try
624 PreserveCollinear := fPreserveCollinear;
625 // the solution should retain the orientation of the input
626 ReverseSolution := fReverseSolution <> pathsReversed;
627 {$IFDEF USINGZ}
628 ZCallback := ZCB;
629 {$ENDIF}
630
631 AddSubject(fSolution);
632 if assigned(fSolutionTree) then
633 Execute(ctUnion, fillRule, fSolutionTree, dummy);
634 Execute(ctUnion, fillRule, fSolution);
635 finally
636 free;
637 end;
638 end;
639 //------------------------------------------------------------------------------
640
641 function TClipperOffset.CheckReverseOrientation: Boolean;
642 var
643 i: integer;
644 begin
645 Result := false;
646 // find the orientation of the first closed path
647 for i := 0 to fGroupList.Count -1 do
648 with TGroup(fGroupList[i]) do
649 if endType = etPolygon then
650 begin
651 Result := reversed;
652 break;
653 end;
654 end;
655 //------------------------------------------------------------------------------
656
657 procedure TClipperOffset.Execute(delta: Double; out solution: TPaths64);
658 begin
659 solution := nil;
660 fSolutionTree := nil;
661 if fGroupList.Count = 0 then Exit;
662 ExecuteInternal(delta);
663 solution := fSolution;
664 end;
665 //------------------------------------------------------------------------------
666
667 procedure TClipperOffset.Execute(DeltaCallback: TDeltaCallback64; out solution: TPaths64);
668 begin
669 fDeltaCallback64 := DeltaCallback;
670 Execute(1.0, solution);
671 end;
672 //------------------------------------------------------------------------------
673
674 procedure TClipperOffset.Execute(delta: Double; polytree: TPolyTree64);
675 begin
676 if not Assigned(polytree) then
677 Raise EClipper2LibException(rsClipper_PolyTreeErr);
678 fSolutionTree := polytree;
679 fSolutionTree.Clear;
680 ExecuteInternal(delta);
681 end;
682 //------------------------------------------------------------------------------
683
684 {$IFDEF USINGZ}
685 procedure TClipperOffset.ZCB(const bot1, top1, bot2, top2: TPoint64;
686 var intersectPt: TPoint64);
687 begin
688 if (bot1.Z <> 0) and
689 ((bot1.Z = bot2.Z) or (bot1.Z = top2.Z)) then intersectPt.Z := bot1.Z
690 else if (bot2.Z <> 0) and (bot2.Z = top1.Z) then intersectPt.Z := bot2.Z
691 else if (top1.Z <> 0) and (top1.Z = top2.Z) then intersectPt.Z := top1.Z
692 else if Assigned(ZCallback) then
693 ZCallback(bot1, top1, bot2, top2, intersectPt);
694 end;
695 {$ENDIF}
696 //------------------------------------------------------------------------------
697
698 {$IFDEF USINGZ}
699 procedure TClipperOffset.AddPoint(x,y: double; z: ZType);
700 {$ELSE}
701 procedure TClipperOffset.AddPoint(x,y: double);
702 {$ENDIF}
703 const
704 BuffLength = 32;
705 var
706 pt: TPoint64;
707 begin
708 {$IFDEF USINGZ}
709 pt := Point64(Round(x),Round(y), z);
710 {$ELSE}
711 pt := Point64(Round(x),Round(y));
712 {$ENDIF}
713 if fOutPathLen = length(fOutPath) then
714 SetLength(fOutPath, fOutPathLen + BuffLength);
715 if (fOutPathLen > 0) and
716 PointsEqual(fOutPath[fOutPathLen-1], pt) then Exit;
717 fOutPath[fOutPathLen] := pt;
718 Inc(fOutPathLen);
719 end;
720 //------------------------------------------------------------------------------
721
722 {$IFDEF USINGZ}
723 procedure TClipperOffset.AddPoint(const pt: TPoint64; newZ: ZType);
724 begin
725 AddPoint(pt.X, pt.Y, newZ);
726 end;
727 //------------------------------------------------------------------------------
728
729 procedure TClipperOffset.AddPoint(const pt: TPoint64);
730 begin
731 AddPoint(pt.X, pt.Y, pt.Z);
732 end;
733 //------------------------------------------------------------------------------
734
735 {$ELSE}
736 procedure TClipperOffset.AddPoint(const pt: TPoint64);
737 begin
738 AddPoint(pt.X, pt.Y);
739 end;
740 //------------------------------------------------------------------------------
741 {$ENDIF}
742
743 function IntersectPoint(const ln1a, ln1b, ln2a, ln2b: TPointD): TPointD;
744 var
745 m1,b1,m2,b2: double;
746 begin
747 result := NullPointD;
748 //see http://astronomy.swin.edu.au/~pbourke/geometry/lineline2d/
749 if (ln1B.X = ln1A.X) then
750 begin
751 if (ln2B.X = ln2A.X) then exit; //parallel lines
752 m2 := (ln2B.Y - ln2A.Y)/(ln2B.X - ln2A.X);
753 b2 := ln2A.Y - m2 * ln2A.X;
754 Result.X := ln1A.X;
755 Result.Y := m2*ln1A.X + b2;
756 end
757 else if (ln2B.X = ln2A.X) then
758 begin
759 m1 := (ln1B.Y - ln1A.Y)/(ln1B.X - ln1A.X);
760 b1 := ln1A.Y - m1 * ln1A.X;
761 Result.X := ln2A.X;
762 Result.Y := m1*ln2A.X + b1;
763 end else
764 begin
765 m1 := (ln1B.Y - ln1A.Y)/(ln1B.X - ln1A.X);
766 b1 := ln1A.Y - m1 * ln1A.X;
767 m2 := (ln2B.Y - ln2A.Y)/(ln2B.X - ln2A.X);
768 b2 := ln2A.Y - m2 * ln2A.X;
769 if m1 = m2 then exit; //parallel lines
770 Result.X := (b2 - b1)/(m1 - m2);
771 Result.Y := m1 * Result.X + b1;
772 end;
773 end;
774 //------------------------------------------------------------------------------
775
776 function ReflectPoint(const pt, pivot: TPointD): TPointD;
777 begin
778 Result.X := pivot.X + (pivot.X - pt.X);
779 Result.Y := pivot.Y + (pivot.Y - pt.Y);
780 {$IFDEF USINGZ}
781 Result.Z := pt.Z;
782 {$ENDIF}
783 end;
784 //------------------------------------------------------------------------------
785
786 procedure TClipperOffset.DoBevel(j, k: Integer);
787 var
788 absDelta: double;
789 begin
790 if k = j then
791 begin
792 absDelta := abs(fGroupDelta);
793 {$IFDEF USINGZ}
794 AddPoint(
795 fInPath[j].x - absDelta * fNorms[j].x,
796 fInPath[j].y - absDelta * fNorms[j].y, fInPath[j].z);
797 AddPoint(
798 fInPath[j].x + absDelta * fNorms[j].x,
799 fInPath[j].y + absDelta * fNorms[j].y, fInPath[j].z);
800 {$ELSE}
801 AddPoint(
802 fInPath[j].x - absDelta * fNorms[j].x,
803 fInPath[j].y - absDelta * fNorms[j].y);
804 AddPoint(
805 fInPath[j].x + absDelta * fNorms[j].x,
806 fInPath[j].y + absDelta * fNorms[j].y);
807 {$ENDIF}
808 end else
809 begin
810 {$IFDEF USINGZ}
811 AddPoint(
812 fInPath[j].x + fGroupDelta * fNorms[k].x,
813 fInPath[j].y + fGroupDelta * fNorms[k].y, fInPath[j].z);
814 AddPoint(
815 fInPath[j].x + fGroupDelta * fNorms[j].x,
816 fInPath[j].y + fGroupDelta * fNorms[j].y, fInPath[j].z);
817 {$ELSE}
818 AddPoint(
819 fInPath[j].x + fGroupDelta * fNorms[k].x,
820 fInPath[j].y + fGroupDelta * fNorms[k].y);
821 AddPoint(
822 fInPath[j].x + fGroupDelta * fNorms[j].x,
823 fInPath[j].y + fGroupDelta * fNorms[j].y);
824 {$ENDIF}
825 end;
826 end;
827 //------------------------------------------------------------------------------
828
829 procedure TClipperOffset.DoSquare(j, k: Integer);
830 var
831 vec, pt1,pt2,pt3,pt4, pt,ptQ : TPointD;
832 absDelta: double;
833 begin
834 if k = j then
835 begin
836 vec.X := fNorms[j].Y; //squaring a line end
837 vec.Y := -fNorms[j].X;
838 end else
839 begin
840 // using the reciprocal of unit normals (as unit vectors)
841 // get the average unit vector ...
842 vec := GetAvgUnitVector(
843 PointD(-fNorms[k].Y, fNorms[k].X),
844 PointD(fNorms[j].Y, -fNorms[j].X));
845 end;
846
847 absDelta := Abs(fGroupDelta);
848 // now offset the original vertex delta units along unit vector
849 ptQ := PointD(fInPath[j]);
850 ptQ := TranslatePoint(ptQ, absDelta * vec.X, absDelta * vec.Y);
851
852 // get perpendicular vertices
853 pt1 := TranslatePoint(ptQ, fGroupDelta * vec.Y, fGroupDelta * -vec.X);
854 pt2 := TranslatePoint(ptQ, fGroupDelta * -vec.Y, fGroupDelta * vec.X);
855
856 // get 2 vertices along one edge offset
857 pt3 := GetPerpendicD(fInPath[k], fNorms[k], fGroupDelta);
858
859 if (j = k) then
860 begin
861 pt4.X := pt3.X + vec.X * fGroupDelta;
862 pt4.Y := pt3.Y + vec.Y * fGroupDelta;
863 // get the intersection point
864 pt := IntersectPoint(pt1, pt2, pt3, pt4);
865 {$IFDEF USINGZ}
866 with ReflectPoint(pt, ptQ) do AddPoint(X, Y, Z);
867 AddPoint(pt.X, pt.Y, pt.Z);
868 {$ELSE}
869 with ReflectPoint(pt, ptQ) do AddPoint(X, Y);
870 AddPoint(pt.X, pt.Y);
871 {$ENDIF}
872 end else
873 begin
874 pt4 := GetPerpendicD(fInPath[j], fNorms[k], fGroupDelta);
875 // get the intersection point
876 pt := IntersectPoint(pt1, pt2, pt3, pt4);
877 {$IFDEF USINGZ}
878 AddPoint(pt.X, pt.Y, ptQ.Z);
879 //get the second intersect point through reflecion
880 with ReflectPoint(pt, ptQ) do AddPoint(X, Y, ptQ.Z);
881 {$ELSE}
882 AddPoint(pt.X, pt.Y);
883 //get the second intersect point through reflecion
884 with ReflectPoint(pt, ptQ) do AddPoint(X, Y);
885 {$ENDIF}
886 end;
887 end;
888 //------------------------------------------------------------------------------
889
890 procedure TClipperOffset.DoMiter(j, k: Integer; cosA: Double);
891 var
892 q: Double;
893 begin
894 // see offset_triginometry4.svg
895 q := fGroupDelta / (cosA +1);
896 {$IFDEF USINGZ}
897 AddPoint(fInPath[j].X + (fNorms[k].X + fNorms[j].X)*q,
898 fInPath[j].Y + (fNorms[k].Y + fNorms[j].Y)*q,
899 fInPath[j].Z);
900 {$ELSE}
901 AddPoint(fInPath[j].X + (fNorms[k].X + fNorms[j].X)*q,
902 fInPath[j].Y + (fNorms[k].Y + fNorms[j].Y)*q);
903 {$ENDIF}
904 end;
905 //------------------------------------------------------------------------------
906
907 procedure TClipperOffset.DoRound(j, k: Integer; angle: double);
908 var
909 i, steps: Integer;
910 absDelta, arcTol, stepsPer360: double;
911 pt: TPoint64;
912 offDist: TPointD;
913 begin
914
915 if Assigned(fDeltaCallback64) then
916 begin
917 // when fDeltaCallback64 is assigned, fGroupDelta won't be constant,
918 // so we'll need to do the following calculations for *every* vertex.
919 absDelta := Abs(fGroupDelta);
920 arcTol := Iif(fArcTolerance > 0.01,
921 Min(absDelta, fArcTolerance),
922 Log10(2 + absDelta) * 0.25); // empirically derived
923 //http://www.angusj.com/clipper2/Docs/Trigonometry.htm
924 stepsPer360 := Pi / ArcCos(1 - arcTol / absDelta);
925 if (stepsPer360 > absDelta * Pi) then
926 stepsPer360 := absDelta * Pi; // avoid excessive precision
927 fStepSin := sin(TwoPi/stepsPer360);
928 fStepCos := cos(TwoPi/stepsPer360);
929 if (fGroupDelta < 0.0) then fStepSin := -fStepSin;
930 fStepsPerRad := stepsPer360 / TwoPi;
931 end;
932
933 // nb: angles may be negative but this will always be a convex join
934 pt := fInPath[j];
935 offDist := ScalePoint(fNorms[k], fGroupDelta);
936 if j = k then offDist := Negate(offDist);
937 {$IFDEF USINGZ}
938 AddPoint(pt.X + offDist.X, pt.Y + offDist.Y, pt.Z);
939 {$ELSE}
940 AddPoint(pt.X + offDist.X, pt.Y + offDist.Y);
941 {$ENDIF}
942 steps := Ceil(fStepsPerRad * abs(angle)); // #448, #456
943 for i := 2 to steps do
944 begin
945 offDist := PointD(offDist.X * fStepCos - fStepSin * offDist.Y,
946 offDist.X * fStepSin + offDist.Y * fStepCos);
947 {$IFDEF USINGZ}
948 AddPoint(pt.X + offDist.X, pt.Y + offDist.Y, pt.Z);
949 {$ELSE}
950 AddPoint(pt.X + offDist.X, pt.Y + offDist.Y);
951 {$ENDIF}
952 end;
953 AddPoint(GetPerpendic(pt, fNorms[j], fGroupDelta));
954 end;
955 //------------------------------------------------------------------------------
956
957 procedure TClipperOffset.OffsetPoint(j: Integer; var k: integer);
958 var
959 sinA, cosA: Double;
960 begin
961 if PointsEqual(fInPath[j], fInPath[k]) then
962 begin
963 k := j;
964 Exit;
965 end;
966
967 // Let A = change in angle where edges join
968 // A == 0: ie no change in angle (flat join)
969 // A == PI: edges 'spike'
970 // sin(A) < 0: right turning
971 // cos(A) < 0: change in angle is more than 90 degree
972 sinA := CrossProduct(fNorms[k], fNorms[j]);
973 cosA := DotProduct(fNorms[j], fNorms[k]);
974 if (sinA > 1.0) then sinA := 1.0
975 else if (sinA < -1.0) then sinA := -1.0;
976
977 if Assigned(fDeltaCallback64) then
978 begin
979 fGroupDelta := fDeltaCallback64(fInPath, fNorms, j, k);
980 if TGroup(fGroupList[0]).reversed then fGroupDelta := -fGroupDelta;
981 end;
982
983 if Abs(fGroupDelta) <= Tolerance then
984 begin
985 AddPoint(fInPath[j]);
986 Exit;
987 end;
988
989 //test for concavity first (#593)
990 if (cosA > -0.999) and (sinA * fGroupDelta < 0) then
991 begin
992 // is concave
993 // by far the simplest way to construct concave joins, especially those
994 // joining very short segments, is to insert 3 points that produce negative
995 // regions. These regions will be removed later by the finishing union
996 // operation. This is also the best way to ensure that path reversals
997 // (ie over-shrunk paths) are removed.
998 {$IFDEF USINGZ}
999 AddPoint(GetPerpendic(fInPath[j], fNorms[k], fGroupDelta), fInPath[j].Z);
1000 AddPoint(fInPath[j]); // (#405, #873)
1001 AddPoint(GetPerpendic(fInPath[j], fNorms[j], fGroupDelta), fInPath[j].Z);
1002 {$ELSE}
1003 AddPoint(GetPerpendic(fInPath[j], fNorms[k], fGroupDelta));
1004 AddPoint(fInPath[j]); // (#405, #873)
1005 AddPoint(GetPerpendic(fInPath[j], fNorms[j], fGroupDelta));
1006 {$ENDIF}
1007 end
1008 else if (cosA > 0.999) and (fJoinType <> jtRound) then
1009 begin
1010 // almost straight - less than 2.5 degree (#424, #482, #526 & #724)
1011 DoMiter(j, k, cosA);
1012 end
1013 else if (fJoinType = jtMiter) then
1014 begin
1015 // miter unless the angle is sufficiently acute to exceed ML
1016 if (cosA > fTmpLimit -1) then DoMiter(j, k, cosA)
1017 else DoSquare(j, k);
1018 end
1019 else if (fJoinType = jtRound) then
1020 DoRound(j, k, ArcTan2(sinA, cosA))
1021 else if (fJoinType = jtBevel) then
1022 DoBevel(j, k)
1023 else
1024 DoSquare(j, k);
1025
1026 k := j;
1027 end;
1028 //------------------------------------------------------------------------------
1029 //------------------------------------------------------------------------------
1030
1031 end.