Debian Codegraph @

components/Image32/source/Clipper2/Clipper.pas

raw
1 unit Clipper;
2
3 (*******************************************************************************
4 * Author : Angus Johnson *
5 * Date : 7 May 2024 *
6 * Website : http://www.angusj.com *
7 * Copyright : Angus Johnson 2010-2024 *
8 * Purpose : This module provides a simple interface to the Clipper Library *
9 * License : http://www.boost.org/LICENSE_1_0.txt *
10 *******************************************************************************)
11
12 interface
13
14 {$I Clipper.inc}
15
16 uses
17 Math, SysUtils, Classes,
18 Clipper.Core, Clipper.Engine, Clipper.Offset, Clipper.RectClip;
19
20 // A number of structures defined in other units are redeclared here
21 // so those units won't also need to be declared in your own units clauses.
22 type
23 TClipper = Clipper.Engine.TClipper64;
24 TClipper64 = Clipper.Engine.TClipper64;
25 TPoint64 = Clipper.Core.TPoint64;
26 TRect64 = Clipper.Core.TRect64;
27 TPath64 = Clipper.Core.TPath64;
28 TPaths64 = Clipper.Core.TPaths64;
29 TPointD = Clipper.Core.TPointD;
30 TRectD = Clipper.Core.TRectD;
31 TPathD = Clipper.Core.TPathD;
32 TPathsD = Clipper.Core.TPathsD;
33 TFillRule = Clipper.Core.TFillRule;
34 TPolyTree64 = Clipper.Engine.TPolyTree64;
35 TPolyTreeD = Clipper.Engine.TPolyTreeD;
36 TJoinType = Clipper.Offset.TJoinType;
37 TEndType = Clipper.Offset.TEndType;
38
39 TArrayOfInt64 = array of Int64;
40 const
41 frEvenOdd = Clipper.Core.frEvenOdd;
42 frNonZero = Clipper.Core.frNonZero;
43 frPositive = Clipper.Core.frPositive;
44 frNegative = Clipper.Core.frNegative;
45 jtBevel = Clipper.Offset.jtBevel;
46 jtSquare = Clipper.Offset.jtSquare;
47 jtRound = Clipper.Offset.jtRound;
48 jtMiter = Clipper.Offset.jtMiter;
49 etPolygon = Clipper.Offset.etPolygon;
50 etJoined = Clipper.Offset.etJoined;
51 etButt = Clipper.Offset.etButt;
52 etSquare = Clipper.Offset.etSquare;
53 etRound = Clipper.Offset.etRound;
54
55 ctNone = Clipper.Core.ctNoClip;
56 ctIntersection = Clipper.Core.ctIntersection;
57 ctUnion = Clipper.Core.ctUnion;
58 ctDifference = Clipper.Core.ctDifference;
59 ctXor = Clipper.Core.ctXor;
60
61 function BooleanOp(clipType: TClipType;
62 const subjects, clips: TPaths64; fillRule: TFillRule): TPaths64; overload;
63 function BooleanOp(clipType: TClipType; const subjects, clips:
64 TPathsD; fillRule: TFillRule; decimalPrec: integer = 2): TPathsD; overload;
65 procedure BooleanOp(clipType: TClipType; const subjects, clips: TPaths64;
66 fillRule: TFillRule; polytree: TPolyTree64); overload;
67
68 function Intersect(const subjects, clips: TPaths64;
69 fillRule: TFillRule): TPaths64; overload;
70 function Union(const subjects, clips: TPaths64;
71 fillRule: TFillRule): TPaths64; overload;
72 function Union(const subjects: TPaths64;
73 fillRule: TFillRule): TPaths64; overload;
74 function Difference(const subjects, clips: TPaths64;
75 fillRule: TFillRule): TPaths64; overload;
76 function XOR_(const subjects, clips: TPaths64;
77 fillRule: TFillRule): TPaths64; overload;
78
79 function Intersect(const subjects, clips: TPathsD;
80 fillRule: TFillRule; decimalPrec: integer = 2): TPathsD; overload;
81 function Union(const subjects: TPathsD;
82 fillRule: TFillRule; decimalPrec: integer = 2): TPathsD; overload;
83 function Union(const subjects, clips: TPathsD;
84 fillRule: TFillRule; decimalPrec: integer = 2): TPathsD; overload;
85 function Difference(const subjects, clips: TPathsD;
86 fillRule: TFillRule; decimalPrec: integer = 2): TPathsD; overload;
87 function XOR_(const subjects, clips: TPathsD;
88 fillRule: TFillRule; decimalPrec: integer = 2): TPathsD; overload;
89
90 function InflatePaths(const paths: TPaths64; delta: Double;
91 jt: TJoinType = jtRound; et: TEndType = etPolygon;
92 MiterLimit: double = 2.0; ArcTolerance: double = 0.0): TPaths64; overload;
93 function InflatePaths(const paths: TPathsD; delta: Double;
94 jt: TJoinType = jtRound; et: TEndType = etPolygon;
95 miterLimit: double = 2.0; precision: integer = 2;
96 ArcTolerance: double = 0.0): TPathsD; overload;
97
98 // RectClip: for closed paths only (otherwise use RectClipLines)
99 function RectClip(const rect: TRect64; const path: TPath64): TPath64; overload;
100 function RectClip(const rect: TRect64; const paths: TPaths64): TPaths64; overload;
101 function RectClip(const rect: TRectD; const path: TPathD; precision: integer = 2): TPathD; overload;
102 function RectClip(const rect: TRectD; const paths: TPathsD; precision: integer = 2): TPathsD; overload;
103
104 function RectClipLines(const rect: TRect64;
105 const path: TPath64): TPaths64; overload;
106 function RectClipLines(const rect: TRect64;
107 const paths: TPaths64): TPaths64; overload;
108 function RectClipLines(const rect: TRectD; const path: TPathD;
109 precision: integer = 2): TPathsD; overload;
110 function RectClipLines(const rect: TRectD; const paths: TPathsD;
111 precision: integer = 2): TPathsD; overload;
112
113 function TranslatePath(const path: TPath64; dx, dy: Int64): TPath64; overload;
114 function TranslatePath(const path: TPathD; dx, dy: double): TPathD; overload;
115 function TranslatePaths(const paths: TPaths64; dx, dy: Int64): TPaths64; overload;
116 function TranslatePaths(const paths: TPathsD; dx, dy: double): TPathsD; overload;
117
118 function MinkowskiSum(const pattern, path: TPath64;
119 pathIsClosed: Boolean): TPaths64; overload;
120 function MinkowskiSum(const pattern, path: TPathD;
121 pathIsClosed: Boolean): TPathsD; overload;
122
123 function PolyTreeToPaths64(PolyTree: TPolyTree64): TPaths64;
124 function PolyTreeToPathsD(PolyTree: TPolyTreeD): TPathsD;
125
126 function PathToString(const p: TPath64;
127 indentSpaces: integer = 0; pointsPerRow: integer = 0): string; overload;
128 function PathToString(const p: TPathD; decimals: integer;
129 indentSpaces: integer = 0; pointsPerRow: integer = 0): string; overload;
130 function PathsToString(const p: TPaths64;
131 indentSpaces: integer = 0; pointsPerRow: integer = 0): string; overload;
132 function PathsToString(const p: TPathsD; decimals: integer;
133 indentSpaces: integer = 0; pointsPerRow: integer = 0): string; overload;
134
135 //ShowPolyTreeStructure: only useful when debugging
136 procedure ShowPolyTreeStructure(polytree: TPolyTree64; strings: TStrings); overload;
137 procedure ShowPolyTreeStructure(polytree: TPolyTreeD; strings: TStrings); overload;
138
139 function MakePath(const ints: array of Int64): TPath64; overload;
140 function MakePathD(const dbls: array of double): TPathD; overload;
141
142 function TrimCollinear(const p: TPath64;
143 isOpenPath: Boolean = false): TPath64; overload;
144 function TrimCollinear(const path: TPathD;
145 precision: integer; isOpenPath: Boolean = false): TPathD; overload;
146
147 function PointInPolygon(const pt: TPoint64; const polygon: TPath64):
148 TPointInPolygonResult;
149
150 function SimplifyPath(const path: TPath64;
151 shapeTolerance: double; isClosedPath: Boolean = true): TPath64; overload;
152 function SimplifyPaths(const paths: TPaths64;
153 shapeTolerance: double; isClosedPath: Boolean = true): TPaths64; overload;
154 function SimplifyPath(const path: TPathD; shapeTolerance: double;
155 isClosedPath: Boolean = true; decimalPrecision: integer = 2): TPathD; overload;
156 function SimplifyPaths(const paths: TPathsD; shapeTolerance: double;
157 isClosedPath: Boolean = true; decimalPrecision: integer = 2): TPathsD; overload;
158
159 implementation
160
161 uses
162 Clipper.Minkowski;
163
164 //------------------------------------------------------------------------------
165 //------------------------------------------------------------------------------
166
167 {$IFDEF USINGZ}
168 function MakePath(const ints: array of Int64): TPath64;
169 var
170 i, len: integer;
171 begin
172 len := length(ints) div 3;
173 SetLength(Result, len);
174 for i := 0 to len -1 do
175 begin
176 Result[i].X := ints[i*3];
177 Result[i].Y := ints[i*3 +1];
178 Result[i].z := ints[i*3 +2];
179 end;
180 end;
181 //------------------------------------------------------------------------------
182
183 function MakePathD(const dbls: array of double): TPathD; overload;
184 var
185 i, len: integer;
186 begin
187 len := length(dbls) div 3;
188 SetLength(Result, len);
189 for i := 0 to len -1 do
190 begin
191 Result[i].X := dbls[i*3];
192 Result[i].Y := dbls[i*3 +1];
193 Result[i].Z := Round(dbls[i*3 +2]);
194 end;
195 end;
196 //------------------------------------------------------------------------------
197 {$ELSE}
198
199 function MakePath(const ints: array of Int64): TPath64;
200 var
201 i, len: integer;
202 begin
203 len := length(ints) div 2;
204 SetLength(Result, len);
205 for i := 0 to len -1 do
206 begin
207 Result[i].X := ints[i*2];
208 Result[i].Y := ints[i*2 +1];
209 end;
210 end;
211 //------------------------------------------------------------------------------
212
213 function MakePathD(const dbls: array of double): TPathD; overload;
214 var
215 i, len: integer;
216 begin
217 len := length(dbls) div 2;
218 SetLength(Result, len);
219 for i := 0 to len -1 do
220 begin
221 Result[i].X := dbls[i*2];
222 Result[i].Y := dbls[i*2 +1];
223 end;
224 end;
225 //------------------------------------------------------------------------------
226 {$ENDIF}
227
228 procedure AddPolyNodeToPaths(Poly: TPolyPath64; var Paths: TPaths64);
229 var
230 i: Integer;
231 begin
232 if (Length(Poly.Polygon) > 0) then
233 begin
234 i := Length(Paths);
235 SetLength(Paths, i +1);
236 Paths[i] := Poly.Polygon;
237 end;
238 for i := 0 to Poly.Count - 1 do
239 AddPolyNodeToPaths(Poly[i], Paths);
240 end;
241 //------------------------------------------------------------------------------
242
243 function PolyTreeToPaths64(PolyTree: TPolyTree64): TPaths64;
244 begin
245 Result := nil;
246 AddPolyNodeToPaths(PolyTree, Result);
247 end;
248 //------------------------------------------------------------------------------
249
250 procedure AddPolyNodeToPathsD(Poly: TPolyPathD; var Paths: TPathsD);
251 var
252 i: Integer;
253 begin
254 if (Length(Poly.Polygon) > 0) then
255 begin
256 i := Length(Paths);
257 SetLength(Paths, i +1);
258 Paths[i] := Poly.Polygon;
259 end;
260 for i := 0 to Poly.Count - 1 do
261 AddPolyNodeToPathsD(Poly[i], Paths);
262 end;
263 //------------------------------------------------------------------------------
264
265 function PolyTreeToPathsD(PolyTree: TPolyTreeD): TPathsD;
266 begin
267 Result := nil;
268 AddPolyNodeToPathsD(PolyTree, Result);
269 end;
270 //------------------------------------------------------------------------------
271 //------------------------------------------------------------------------------
272
273 function BooleanOp(clipType: TClipType;
274 const subjects, clips: TPaths64; fillRule: TFillRule): TPaths64;
275 begin
276 with TClipper64.Create do
277 try
278 AddSubject(subjects);
279 AddClip(clips);
280 Execute(clipType, fillRule, Result);
281 finally
282 Free;
283 end;
284 end;
285 //------------------------------------------------------------------------------
286
287 function BooleanOp(clipType: TClipType; const subjects, clips: TPathsD;
288 fillRule: TFillRule; decimalPrec: integer = 2): TPathsD;
289 begin
290 with TClipperD.Create(decimalPrec) do
291 try
292 AddSubject(subjects);
293 AddClip(clips);
294 Execute(clipType, fillRule, Result);
295 finally
296 Free;
297 end;
298 end;
299 //------------------------------------------------------------------------------
300
301 procedure BooleanOp(clipType: TClipType; const subjects, clips: TPaths64;
302 fillRule: TFillRule; polytree: TPolyTree64);
303 var
304 dummy: TPaths64;
305 begin
306 with TClipper64.Create do
307 try
308 AddSubject(subjects);
309 AddClip(clips);
310 Execute(clipType, fillRule, polytree, dummy);
311 finally
312 Free;
313 end;
314 end;
315 //------------------------------------------------------------------------------
316
317 function Intersect(const subjects, clips: TPaths64; fillRule: TFillRule): TPaths64;
318 begin
319 Result := BooleanOp(ctIntersection, subjects, clips, fillRule);
320 end;
321 //------------------------------------------------------------------------------
322
323 function Union(const subjects, clips: TPaths64; fillRule: TFillRule): TPaths64;
324 begin
325 Result := BooleanOp(ctUnion, subjects, clips, fillRule);
326 end;
327 //------------------------------------------------------------------------------
328
329 function Union(const subjects: TPaths64; fillRule: TFillRule): TPaths64;
330 begin
331 Result := BooleanOp(ctUnion, subjects, nil, fillRule);
332 end;
333 //------------------------------------------------------------------------------
334
335 function Difference(const subjects, clips: TPaths64; fillRule: TFillRule): TPaths64;
336 begin
337 Result := BooleanOp(ctDifference, subjects, clips, fillRule);
338 end;
339 //------------------------------------------------------------------------------
340
341 function XOR_(const subjects, clips: TPaths64; fillRule: TFillRule): TPaths64;
342 begin
343 Result := BooleanOp(ctXor, subjects, clips, fillRule);
344 end;
345 //------------------------------------------------------------------------------
346
347 function Intersect(const subjects, clips: TPathsD;
348 fillRule: TFillRule; decimalPrec: integer = 2): TPathsD;
349 begin
350 Result := BooleanOp(ctIntersection, subjects, clips, fillRule, decimalPrec);
351 end;
352 //------------------------------------------------------------------------------
353
354 function Union(const subjects, clips: TPathsD;
355 fillRule: TFillRule; decimalPrec: integer = 2): TPathsD;
356 begin
357 Result := BooleanOp(ctUnion, subjects, clips, fillRule, decimalPrec);
358 end;
359 //------------------------------------------------------------------------------
360
361 function Union(const subjects: TPathsD;
362 fillRule: TFillRule; decimalPrec: integer = 2): TPathsD;
363 begin
364 Result := BooleanOp(ctUnion, subjects, nil, fillRule, decimalPrec);
365 end;
366 //------------------------------------------------------------------------------
367
368 function Difference(const subjects, clips: TPathsD;
369 fillRule: TFillRule; decimalPrec: integer = 2): TPathsD;
370 begin
371 Result := BooleanOp(ctDifference, subjects, clips, fillRule, decimalPrec);
372 end;
373 //------------------------------------------------------------------------------
374
375 function XOR_(const subjects, clips: TPathsD;
376 fillRule: TFillRule; decimalPrec: integer = 2): TPathsD;
377 begin
378 Result := BooleanOp(ctXor, subjects, clips, fillRule, decimalPrec);
379 end;
380 //------------------------------------------------------------------------------
381 //------------------------------------------------------------------------------
382
383 function InflatePaths(const paths: TPaths64; delta: Double;
384 jt: TJoinType; et: TEndType; MiterLimit: double;
385 ArcTolerance: double): TPaths64;
386 var
387 co: TClipperOffset;
388 begin
389 co := TClipperOffset.Create(MiterLimit, ArcTolerance);
390 try
391 co.AddPaths(paths, jt, et);
392 co.Execute(delta, Result);
393 finally
394 co.free;
395 end;
396 end;
397 //------------------------------------------------------------------------------
398
399 function InflatePaths(const paths: TPathsD; delta: Double;
400 jt: TJoinType; et: TEndType; miterLimit: double;
401 precision: integer; ArcTolerance: double): TPathsD;
402 var
403 pp: TPaths64;
404 scale, invScale: double;
405 begin
406 CheckPrecisionRange(precision);
407 scale := Power(10, precision);
408 invScale := 1/scale;
409 pp := ScalePaths(paths, scale, scale);
410
411 with TClipperOffset.Create(miterLimit, ArcTolerance) do
412 try
413 AddPaths(pp, jt, et);
414 Execute(delta * scale, pp); // reuse pp to receive the solution.
415 finally
416 free;
417 end;
418 Result := ScalePathsD(pp, invScale, invScale);
419 end;
420 //------------------------------------------------------------------------------
421
422 function RectClip(const rect: TRect64;
423 const path: TPath64): TPath64;
424 var
425 paths: TPaths64;
426 begin
427 SetLength(paths, 1);
428 paths[0] := path;
429 paths := RectClip(rect, paths);
430 if Assigned(paths) then
431 Result := paths[0] else
432 Result := nil;
433 end;
434 //------------------------------------------------------------------------------
435
436 function RectClip(const rect: TRect64; const paths: TPaths64): TPaths64;
437 begin
438 Result := nil;
439 if rect.IsEmpty then Exit;
440 with TRectClip64.Create(rect) do
441 try
442 Result := Execute(paths);
443 finally
444 Free;
445 end;
446 end;
447 //------------------------------------------------------------------------------
448
449 function RectClip(const rect: TRectD; const path: TPathD; precision: integer): TPathD;
450 var
451 scale: double;
452 tmpPath: TPath64;
453 rec: TRect64;
454 begin
455 Result := nil;
456 if not rect.Intersects(GetBounds(path)) then Exit;
457 CheckPrecisionRange(precision);
458 scale := Math.Power(10, precision);
459 rec := Rect64(ScaleRect(rect, scale));
460 tmpPath := ScalePath(path, scale);
461 tmpPath := RectClip(rec, tmpPath);
462 Result := ScalePathD(tmpPath, 1/scale);
463 end;
464 //------------------------------------------------------------------------------
465
466 function RectClip(const rect: TRectD; const paths: TPathsD; precision: integer): TPathsD;
467 var
468 scale: double;
469 tmpPaths: TPaths64;
470 rec: TRect64;
471 begin
472 CheckPrecisionRange(precision);
473 scale := Math.Power(10, precision);
474 rec := Rect64(ScaleRect(rect, scale));
475
476 tmpPaths := ScalePaths(paths, scale);
477 with TRectClip64.Create(rec) do
478 try
479 tmpPaths := Execute(tmpPaths);
480 finally
481 Free;
482 end;
483 Result := ScalePathsD(tmpPaths, 1/scale);
484 end;
485 //------------------------------------------------------------------------------
486
487 function RectClipLines(const rect: TRect64; const path: TPath64): TPaths64;
488 var
489 tmp: TPaths64;
490 begin
491 Result := nil;
492 SetLength(tmp, 1);
493 tmp[0] := path;
494 with TRectClipLines64.Create(rect) do
495 try
496 Result := Execute(tmp);
497 finally
498 Free;
499 end;
500 end;
501 //------------------------------------------------------------------------------
502
503 function RectClipLines(const rect: TRect64; const paths: TPaths64): TPaths64;
504 begin
505 Result := nil;
506 if rect.IsEmpty then Exit;
507 with TRectClipLines64.Create(rect) do
508 try
509 Result := Execute(paths);
510 finally
511 Free;
512 end;
513 end;
514 //------------------------------------------------------------------------------
515
516 function RectClipLines(const rect: TRectD;
517 const path: TPathD; precision: integer): TPathsD;
518 var
519 scale: double;
520 tmpPath: TPath64;
521 tmpPaths: TPaths64;
522 rec: TRect64;
523 begin
524 Result := nil;
525 if not rect.Intersects(GetBounds(path)) then Exit;
526 CheckPrecisionRange(precision);
527 scale := Math.Power(10, precision);
528 rec := Rect64(ScaleRect(rect, scale));
529 tmpPath := ScalePath(path, scale);
530 tmpPaths := RectClipLines(rec, tmpPath);
531 Result := ScalePathsD(tmpPaths, 1/scale);
532 end;
533 //------------------------------------------------------------------------------
534
535 function RectClipLines(const rect: TRectD; const paths: TPathsD;
536 precision: integer = 2): TPathsD;
537 var
538 scale: double;
539 tmpPaths: TPaths64;
540 rec: TRect64;
541 begin
542 Result := nil;
543 if rect.IsEmpty then Exit;
544 CheckPrecisionRange(precision);
545 scale := Math.Power(10, precision);
546 rec := Rect64(ScaleRect(rect, scale));
547 tmpPaths := ScalePaths(paths, scale);
548 with TRectClipLines64.Create(rec) do
549 try
550 tmpPaths := Execute(tmpPaths);
551 finally
552 Free;
553 end;
554 Result := ScalePathsD(tmpPaths, 1/scale);
555 end;
556 //------------------------------------------------------------------------------
557
558 function TranslatePath(const path: TPath64; dx, dy: Int64): TPath64;
559 var
560 i, len: integer;
561 begin
562 len := length(path);
563 setLength(result, len);
564 for i := 0 to len -1 do
565 begin
566 result[i].x := path[i].x + dx;
567 result[i].y := path[i].y + dy;
568 end;
569 end;
570 //------------------------------------------------------------------------------
571
572 function TranslatePath(const path: TPathD; dx, dy: double): TPathD;
573 var
574 i, len: integer;
575 begin
576 len := length(path);
577 setLength(result, len);
578 for i := 0 to len -1 do
579 begin
580 result[i].x := path[i].x + dx;
581 result[i].y := path[i].y + dy;
582 end;
583 end;
584 //------------------------------------------------------------------------------
585
586 function TranslatePaths(const paths: TPaths64; dx, dy: Int64): TPaths64;
587 var
588 i, len: integer;
589 begin
590 len := length(paths);
591 setLength(result, len);
592 for i := 0 to len -1 do
593 begin
594 result[i] := TranslatePath(paths[i], dx, dy);
595 end;
596 end;
597 //------------------------------------------------------------------------------
598
599 function TranslatePaths(const paths: TPathsD; dx, dy: double): TPathsD;
600 var
601 i, len: integer;
602 begin
603 len := length(paths);
604 setLength(result, len);
605 for i := 0 to len -1 do
606 begin
607 result[i] := TranslatePath(paths[i], dx, dy);
608 end;
609 end;
610 //------------------------------------------------------------------------------
611
612 function MinkowskiSum(const pattern, path: TPath64;
613 pathIsClosed: Boolean): TPaths64;
614 begin
615 Result := Clipper.Minkowski.MinkowskiSum(pattern, path, pathIsClosed);
616 end;
617 //------------------------------------------------------------------------------
618
619 function MinkowskiSum(const pattern, path: TPathD;
620 pathIsClosed: Boolean): TPathsD;
621 begin
622 Result := Clipper.Minkowski.MinkowskiSum(pattern, path, pathIsClosed);
623 end;
624 //------------------------------------------------------------------------------
625
626 function PathToString(const p: TPath64;
627 indentSpaces: integer; pointsPerRow: integer): string;
628 var
629 i, highI: Integer;
630 spaces: string;
631 begin
632 spaces := StringOfChar(' ', indentSpaces);
633 Result := spaces;
634 highI := high(p);
635 if highI < 0 then Exit;
636 for i := 0 to highI -1 do
637 begin
638 Result := Result + format('%d,%d, ',[p[i].X,p[i].Y]);
639 if (pointsPerRow > 0) and ((i + 1) mod pointsPerRow = 0) then
640 Result := Result + #10 + spaces;
641 end;
642 Result := Result + format('%d,%d',[p[highI].X,p[highI].Y]);
643 end;
644 //------------------------------------------------------------------------------
645
646 function PathToString(const p: TPathD; decimals: integer;
647 indentSpaces: integer; pointsPerRow: integer): string;
648 var
649 i, highI: Integer;
650 spaces: string;
651 begin
652 spaces := StringOfChar(' ', indentSpaces);
653 Result := '';
654 highI := high(p);
655 if highI < 0 then Exit;
656 for i := 0 to highI -1 do
657 Result := Result + format('%1.*n,%1.*n, ',
658 [decimals, p[i].X, decimals, p[i].Y]);
659 Result := Result + format('%1.*n,%1.*n',[
660 decimals, p[highI].X, decimals, p[highI].Y]);
661 end;
662 //------------------------------------------------------------------------------
663
664 function PathsToString(const p: TPaths64;
665 indentSpaces: integer = 0; pointsPerRow: integer = 0): string;
666 var
667 i: integer;
668 begin
669 Result := '';
670 for i := 0 to High(p) do
671 Result := Result + PathToString(p[i], indentSpaces, pointsPerRow) + #10#10;
672 end;
673 //------------------------------------------------------------------------------
674
675 function PathsToString(const p: TPathsD; decimals: integer;
676 indentSpaces: integer = 0; pointsPerRow: integer = 0): string;
677 var
678 i: integer;
679 begin
680 Result := '';
681 for i := 0 to High(p) do
682 Result := Result + PathToString(p[i], indentSpaces, pointsPerRow) + #10#10;
683 end;
684 //------------------------------------------------------------------------------
685
686 procedure ShowPolyPathStructure64(pp: TPolyPath64; level: integer;
687 strings: TStrings);
688 var
689 i: integer;
690 spaces, plural: string;
691 begin
692 spaces := StringOfChar(' ', level * 2);
693 if pp.Count = 1 then plural := '' else plural := 's';
694 if pp.IsHole then
695 strings.Add(Format('%sA hole containing %d polygon%s', [spaces, pp.Count, plural]))
696 else
697 strings.Add(Format('%sA polygon containing %d hole%s', [spaces, pp.Count, plural]));
698 for i := 0 to pp.Count -1 do
699 if pp.child[i].Count> 0 then
700 ShowPolyPathStructure64(pp.child[i], level + 1, strings);
701 end;
702 //------------------------------------------------------------------------------
703
704 procedure ShowPolyTreeStructure(polytree: TPolyTree64; strings: TStrings);
705 var
706 i: integer;
707 begin
708 if polytree.Count = 1 then
709 strings.Add('Polytree with just 1 polygon.') else
710 strings.Add(Format('Polytree with just %d polygons.', [polytree.Count]));
711 for i := 0 to polytree.Count -1 do
712 if polytree[i].Count > 0 then
713 ShowPolyPathStructure64(polytree[i], 1, strings);
714 end;
715 //------------------------------------------------------------------------------
716
717 procedure ShowPolyPathStructureD(pp: TPolyPathD; level: integer; strings: TStrings);
718 var
719 i: integer;
720 spaces, plural: string;
721 begin
722 spaces := StringOfChar(' ', level * 2);
723 if pp.Count = 1 then plural := '' else plural := 's';
724 if pp.IsHole then
725 strings.Add(Format('%sA hole containing %d polygon%s', [spaces, pp.Count, plural]))
726 else
727 strings.Add(Format('%sA polygon containing %d hole%s', [spaces, pp.Count, plural]));
728 for i := 0 to pp.Count -1 do
729 if pp.child[i].Count> 0 then
730 ShowPolyPathStructureD(pp.child[i], level + 1, strings);
731 end;
732 //------------------------------------------------------------------------------
733
734 procedure ShowPolyTreeStructure(polytree: TPolyTreeD; strings: TStrings);
735 var
736 i: integer;
737 begin
738 if polytree.Count = 1 then
739 strings.Add('Polytree with just 1 polygon.') else
740 strings.Add(Format('Polytree with just %d polygons.', [polytree.Count]));
741 for i := 0 to polytree.Count -1 do
742 if polytree[i].Count > 0 then
743 ShowPolyPathStructureD(polytree[i], 1, strings);
744 end;
745 //------------------------------------------------------------------------------
746
747 function TrimCollinear(const p: TPath64; isOpenPath: Boolean = false): TPath64;
748 var
749 i,j, len: integer;
750 begin
751 len := Length(p);
752
753 i := 0;
754 if not isOpenPath then
755 begin
756 while (i < len -1) and
757 IsCollinear(p[len -1], p[i], p[i+1]) do inc(i);
758 while (i < len -1) and
759 IsCollinear(p[len -2], p[len -1], p[i]) do dec(len);
760 end;
761 if (len - i < 3) then
762 begin
763 if not isOpenPath or (len < 2) or PointsEqual(p[0], p[1]) then
764 Result := nil else
765 Result := p;
766 Exit;
767 end;
768
769 SetLength(Result, len -i);
770
771 Result[0] := p[i];
772 j := 0;
773 for i := i+1 to len -2 do
774 if not IsCollinear(result[j], p[i], p[i+1]) then
775 begin
776 inc(j);
777 result[j] := p[i];
778 end;
779
780 if isOpenPath then
781 begin
782 inc(j);
783 result[j] := p[len-1];
784 end
785 else if not IsCollinear(result[j], p[len-1], result[0]) then
786 begin
787 inc(j);
788 result[j] := p[len-1];
789 end else
790 begin
791 while (j > 1) and
792 IsCollinear(result[j-1], result[j], result[0]) do dec(j);
793 if j < 2 then j := -1;
794 end;
795 SetLength(Result, j +1);
796 end;
797 //------------------------------------------------------------------------------
798
799 function TrimCollinear(const path: TPathD;
800 precision: integer; isOpenPath: Boolean = false): TPathD;
801 var
802 p: TPath64;
803 scale: double;
804 begin
805 scale := power(10, precision);
806 p := ScalePath(path, scale);
807 p := TrimCollinear(p, isOpenPath);
808 Result := ScalePathD(p, 1/scale);
809 end;
810 //------------------------------------------------------------------------------
811
812 function PointInPolygon(const pt: TPoint64;
813 const polygon: TPath64): TPointInPolygonResult;
814 begin
815 Result := Clipper.Core.PointInPolygon(pt, polygon);
816 end;
817 //------------------------------------------------------------------------------
818
819 function DistanceSqrd(const pt1, pt2: TPoint64): double;
820 {$IFDEF INLINE} inline; {$ENDIF}
821 var
822 x1,y1,x2,y2: double;
823 begin
824 // nb: older versions of Delphi don't allow explicit typcasting
825 x1 := pt1.X; y1 := pt1.Y;
826 x2 := pt2.X; y2 := pt2.Y;
827 result := Sqr(x1 - x2) + Sqr(y1 - y2);
828 end;
829 //------------------------------------------------------------------------------
830
831 function PerpendicDistSqrd(const pt, line1, line2: TPoint64): double;
832 {$IFDEF INLINE} inline; {$ENDIF}
833 var
834 a,b,c,d: double;
835 begin
836 a := pt.X - line1.X;
837 b := pt.Y - line1.Y;
838 c := line2.X - line1.X;
839 d := line2.Y - line1.Y;
840 result := Iif((c = 0) and (d = 0),
841 0, Sqr(a * d - c * b) / (c * c + d * d));
842 end;
843
844 //------------------------------------------------------------------------------
845 //------------------------------------------------------------------------------
846
847 type
848 PSimplifyRec = ^TSimplifyRec;
849 TSimplifyRec = record
850 pt : TPoint64;
851 pdSqrd : double;
852 prev : PSimplifyRec;
853 next : PSimplifyRec;
854 //isEnd : Boolean;
855 end;
856
857 function SimplifyPath(const path: TPath64;
858 shapeTolerance: double; isClosedPath: Boolean): TPath64;
859 var
860 i, highI, minHigh: integer;
861 tolSqrd: double;
862 srArray: array of TSimplifyRec;
863 first, last: PSimplifyRec;
864 begin
865 Result := nil;
866 highI := High(path);
867
868 minHigh := Iif(isClosedPath, 2, 1);
869 if highI < minHigh then Exit;
870
871 SetLength(srArray, highI +1);
872 with srArray[0] do
873 begin
874 pt := path[0];
875 prev := @srArray[highI];
876 next := @srArray[1];
877 pdSqrd := Iif(isClosedPath,
878 PerpendicDistSqrd(path[0], path[highI], path[1]), invalidD);
879 end;
880
881 with srArray[highI] do
882 begin
883 pt := path[highI];
884 prev := @srArray[highI-1];
885 next := @srArray[0];
886 pdSqrd := Iif(isClosedPath,
887 PerpendicDistSqrd(path[highI], path[highI-1], path[0]), invalidD);
888 end;
889
890 for i := 1 to highI -1 do
891 with srArray[i] do
892 begin
893 pt := path[i];
894 prev := @srArray[i-1];
895 next := @srArray[i+1];
896 pdSqrd := PerpendicDistSqrd(path[i], path[i-1], path[i+1]);
897 end;
898
899 first := @srArray[0];
900 last := first.prev;
901
902 tolSqrd := Sqr(shapeTolerance);
903 while first <> last do
904 begin
905 if (first.pdSqrd > tolSqrd) or
906 (first.next.pdSqrd < first.pdSqrd) then
907 begin
908 first := first.next;
909 Continue;
910 end;
911 dec(highI);
912 first.prev.next := first.next;
913 first.next.prev := first.prev;
914 last := first.prev;
915 first := last.next;
916 if first.next = first.prev then break;
917 last.pdSqrd := PerpendicDistSqrd(last.pt, last.prev.pt, first.pt);
918 first.pdSqrd := PerpendicDistSqrd(first.pt, last.pt, first.next.pt);
919 end;
920 if highI < minHigh then Exit;
921 if not isClosedPath then first := @srArray[0];
922 SetLength(Result, highI +1);
923 for i := 0 to HighI do
924 begin
925 Result[i] := first.pt;
926 first := first.next;
927 end;
928 end;
929 //------------------------------------------------------------------------------
930
931 function SimplifyPaths(const paths: TPaths64;
932 shapeTolerance: double; isClosedPath: Boolean): TPaths64;
933 var
934 i, len: integer;
935 begin
936 len := Length(paths);
937 SetLength(Result, len);
938 for i := 0 to len -1 do
939 result[i] := SimplifyPath(paths[i], shapeTolerance, isClosedPath);
940 end;
941 //------------------------------------------------------------------------------
942
943 function SimplifyPath(const path: TPathD; shapeTolerance: double;
944 isClosedPath: Boolean; decimalPrecision: integer): TPathD;
945 var
946 p: TPath64;
947 scale: double;
948 begin
949 scale := power(10, decimalPrecision);
950 p := ScalePath(path, scale);
951 p := SimplifyPath(p, shapeTolerance, isClosedPath);
952 Result := ScalePathD(p, 1/scale);
953 end;
954 //------------------------------------------------------------------------------
955
956 function SimplifyPaths(const paths: TPathsD; shapeTolerance: double;
957 isClosedPath: Boolean; decimalPrecision: integer): TPathsD;
958 var
959 pp: TPaths64;
960 scale: double;
961 begin
962 scale := power(10, decimalPrecision);
963 pp := ScalePaths(paths, scale);
964 pp := SimplifyPaths(pp, shapeTolerance, isClosedPath);
965 Result := ScalePathsD(pp, 1/scale);
966 end;
967 //------------------------------------------------------------------------------
968
969
970 end.
971
972