Debian Codegraph @

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

raw
1 unit Clipper.Minkowski;
2
3 (*******************************************************************************
4 * Author : Angus Johnson *
5 * Date : 21 December 2023 *
6 * Copyright : Angus Johnson 2010-2022 *
7 * Purpose : Minkowski Addition and Difference *
8 * License : http://www.boost.org/LICENSE_1_0.txt *
9 *******************************************************************************)
10
11 {$I Clipper.inc}
12
13 interface
14
15 uses
16 Classes, Math, Clipper.Core;
17
18 function MinkowskiSum(const Pattern, Path: TPath64;
19 PathIsClosed: Boolean): TPaths64; overload;
20 function MinkowskiSum(const Pattern, Path: TPathD;
21 PathIsClosed: Boolean; decimalPlaces: integer = 2): TPathsD; overload;
22 function MinkowskiDiff(const path1, path2: TPath64): TPaths64; overload;
23 function MinkowskiDiff(const Pattern, Path: TPathD;
24 PathIsClosed: Boolean; decimalPlaces: integer): TPathsD; overload;
25
26 implementation
27
28 uses
29 Clipper;
30
31 function AddPoints(val1, val2: TPoint64): TPoint64;
32 {$IFDEF INLINING} inline; {$ENDIF}
33 begin
34 Result.X := val1.X + val2.X;
35 Result.Y := val1.Y + val2.Y;
36 end;
37 //------------------------------------------------------------------------------
38
39 function SubtractPoints(val1, val2: TPoint64): TPoint64;
40 {$IFDEF INLINING} inline; {$ENDIF}
41 begin
42 Result.X := val1.X - val2.X;
43 Result.Y := val1.Y - val2.Y;
44 end;
45 //------------------------------------------------------------------------------
46
47 function Minkowski(const Base, Path: TPath64;
48 IsSum: Boolean; IsClosed: Boolean): TPaths64;
49 var
50 i,j,k,g,h, delta, baseLen, pathLen: integer;
51 tmp: TPaths64;
52 quad: TPath64;
53 begin
54 delta := Iif(IsClosed, 0 , 1);
55 baseLen := Length(Base);
56 pathLen := Length(Path);
57 setLength(tmp, pathLen);
58
59 for i := 0 to pathLen -1 do
60 begin
61 setLength(tmp[i], baseLen);
62 if IsSum then
63 for j := 0 to baseLen -1 do
64 tmp[i][j] := AddPoints(Path[i], Base[j])
65 else
66 for j := 0 to baseLen -1 do
67 tmp[i][j] := SubtractPoints(Path[i], Base[j]);
68 end;
69
70 SetLength(quad, 4);
71 SetLength(Result, (pathLen - delta) * baseLen);
72 g := Iif(IsClosed, pathLen - 1, 0);
73
74 for i := delta to pathLen - 1 do
75 begin
76 h := baseLen - 1;
77 k := (i - delta) * baseLen;
78 for j := 0 to baseLen - 1 do
79 begin
80 quad[0] := tmp[g][h];
81 quad[1] := tmp[i][h];
82 quad[2] := tmp[i][(j)];
83 quad[3] := tmp[g][(j)];
84 if not IsPositive(quad) then
85 Result[k + j] := ReversePath(quad) else
86 Result[k + j] := copy(quad, 0, 4);
87 h := j;
88 end;
89 g := i;
90 end;
91 end;
92 //------------------------------------------------------------------------------
93
94 function MinkowskiSum(const Pattern, Path: TPath64; PathIsClosed: Boolean): TPaths64;
95 begin
96 Result := Union( Minkowski(Pattern, Path, true, PathIsClosed), frNonZero);
97 end;
98 //------------------------------------------------------------------------------
99
100 function MinkowskiSum(const Pattern, Path: TPathD;
101 PathIsClosed: Boolean; decimalPlaces: integer): TPathsD;
102 var
103 tmp: TPaths64;
104 scale: double;
105 begin
106 scale := Power(10, decimalPlaces);
107 tmp := Union( Minkowski(ScalePath(Pattern, scale),
108 ScalePath(Path, scale), true, PathIsClosed), frNonZero);
109 Result := ScalePathsD(tmp, 1/scale);
110 end;
111 //------------------------------------------------------------------------------
112
113 function MinkowskiDiff(const path1, path2: TPath64): TPaths64;
114 begin
115 Result := Union( Minkowski(path1, path2, false, true), frNonZero);
116 end;
117 //------------------------------------------------------------------------------
118
119 function MinkowskiDiff(const Pattern, Path: TPathD;
120 PathIsClosed: Boolean; decimalPlaces: integer): TPathsD;
121 var
122 tmp: TPaths64;
123 scale: double;
124 begin
125 scale := Power(10, decimalPlaces);
126 tmp := Union( Minkowski(ScalePath(Pattern, scale),
127 ScalePath(Path, scale), false, PathIsClosed), frNonZero);
128 Result := ScalePathsD(tmp, 1/scale);
129 end;
130 //------------------------------------------------------------------------------
131
132 end.
133