|
1 | | -unit Img32.Clipper; |
2 | | - |
3 | | -(******************************************************************************* |
4 | | -* Author : Angus Johnson * |
5 | | -* Version : 2.24 * |
6 | | -* Date : 26 June 2021 * |
7 | | -* Website : http://www.angusj.com * |
8 | | -* Copyright : Angus Johnson 2019-2021 * |
9 | | -* Purpose : Wrapper module for the Clipper library * |
10 | | -* License : http://www.boost.org/LICENSE_1_0.txt * |
11 | | -*******************************************************************************) |
12 | | - |
13 | | -interface |
14 | | - |
15 | | -uses |
16 | | - ClipperCore, Clipper, ClipperOffset, |
17 | | - Img32, Img32.Draw, Img32.Vector; |
18 | | - |
19 | | -//nb: InflatePath assumes that there's consistent winding where |
20 | | -//outer paths wind in one direction and inner paths in the other |
21 | | - |
22 | | -function InflatePath(const path: TPathD; delta: Double; |
23 | | - joinStyle: TJoinStyle = jsAuto; endStyle: TEndStyle = esPolygon; |
24 | | - miterLimit: double = 2.0; arcTolerance: double = 0.0; |
25 | | - minEdgeLength: double = 0.25): TPathsD; |
26 | | - |
27 | | -function InflatePaths(const paths: TPathsD; delta: Double; |
28 | | - joinStyle: TJoinStyle = jsAuto; endStyle: TEndStyle = esPolygon; |
29 | | - miterLimit: double = 2.0; arcTolerance: double = 0.0; |
30 | | - minEdgeLength: double = 0): TPathsD; |
31 | | - |
32 | | -//UnionPolygon: removes self-intersections |
33 | | -function UnionPolygon(const polygon: TPathD; |
34 | | - fillRule: TFillRule): TPathsD; |
35 | | - |
36 | | -function UnionPolygons(const polygons: TPathsD; |
37 | | - fillRule: TFillRule): TPathsD; overload; |
38 | | -function UnionPolygons(const polygon1, polygon2: TPathD; |
39 | | - fillRule: TFillRule): TPathsD; overload; |
40 | | -function UnionPolygons(const polygons1, polygons2: TPathsD; |
41 | | - fillRule: TFillRule): TPathsD; overload; |
42 | | - |
43 | | -function IntersectPolygons(const polygons1, polygons2: TPathsD; |
44 | | - fillRule: TFillRule): TPathsD; |
45 | | - |
46 | | -function DifferencePolygons(const polygons1, polygons2: TPathsD; |
47 | | - fillRule: TFillRule): TPathsD; |
48 | | - |
49 | | -implementation |
50 | | - |
51 | | -//------------------------------------------------------------------------------ |
52 | | -//------------------------------------------------------------------------------ |
53 | | - |
54 | | -function InflatePath(const path: TPathD; |
55 | | - delta: Double; joinStyle: TJoinStyle; endStyle: TEndStyle; |
56 | | - miterLimit: double; arcTolerance: double; minEdgeLength: double): TPathsD; |
57 | | -var |
58 | | - paths: TPathsD; |
59 | | -begin |
60 | | - setLength(paths, 1); |
61 | | - paths[0] := path; |
62 | | - Result := InflatePaths(paths, delta, joinStyle, endStyle, |
63 | | - miterLimit, arcTolerance, minEdgeLength); |
64 | | -end; |
65 | | -//------------------------------------------------------------------------------ |
66 | | - |
67 | | -function InflatePaths(const paths: TPathsD; |
68 | | - delta: Double; joinStyle: TJoinStyle; endStyle: TEndStyle; |
69 | | - miterLimit: double; arcTolerance: double; minEdgeLength: double): TPathsD; |
70 | | -var |
71 | | - jt: ClipperOffset.TJoinType; |
72 | | - et: TEndType; |
73 | | -begin |
74 | | - case joinStyle of |
75 | | - jsSquare: jt := jtSquare; |
76 | | - jsMiter: jt := jtMiter; |
77 | | - jsRound: jt := jtRound; |
78 | | - else if endStyle = esRound then jt := jtRound |
79 | | - else jt := jtSquare; |
80 | | - end; |
81 | | - case endStyle of |
82 | | - esButt: et := etButt; |
83 | | - esSquare: et := etSquare; |
84 | | - esRound: et := etRound; |
85 | | - else et := etPolygon; |
86 | | - end; |
87 | | - Result := TPathsD(ClipperOffset.InflatePaths( |
88 | | - ClipperCore.TPathsD(paths), delta, |
89 | | - jt, et, miterLimit, arcTolerance, minEdgeLength)); |
90 | | -end; |
91 | | -//------------------------------------------------------------------------------ |
92 | | - |
93 | | -function UnionPolygon(const polygon: TPathD; fillRule: TFillRule): TPathsD; |
94 | | -begin |
95 | | - with TClipperD.Create do |
96 | | - try |
97 | | - AddPath(ClipperCore.TPathD(polygon)); |
98 | | - Execute(ctUnion, |
99 | | - ClipperCore.TFillRule(fillRule), ClipperCore.TPathsD(result)); |
100 | | - finally |
101 | | - Free; |
102 | | - end; |
103 | | -end; |
104 | | -//------------------------------------------------------------------------------ |
105 | | - |
106 | | -function UnionPolygons(const polygons: TPathsD; |
107 | | - fillRule: TFillRule): TPathsD; |
108 | | -begin |
109 | | - with TClipperD.Create do |
110 | | - try |
111 | | - AddPaths(ClipperCore.TPathsD(polygons)); |
112 | | - Execute(ctUnion, |
113 | | - ClipperCore.TFillRule(fillRule), ClipperCore.TPathsD(result)); |
114 | | - finally |
115 | | - Free; |
116 | | - end; |
117 | | -end; |
118 | | -//------------------------------------------------------------------------------ |
119 | | - |
120 | | -function UnionPolygons(const polygon1, polygon2: TPathD; |
121 | | - fillRule: TFillRule): TPathsD; |
122 | | -begin |
123 | | - with TClipperD.Create do |
124 | | - try |
125 | | - AddPath(ClipperCore.TPathD(polygon1), ptSubject); |
126 | | - AddPath(ClipperCore.TPathD(polygon2), ptClip); |
127 | | - Execute(ctUnion, |
128 | | - ClipperCore.TFillRule(fillRule), ClipperCore.TPathsD(result)); |
129 | | - finally |
130 | | - Free; |
131 | | - end; |
132 | | -end; |
133 | | -//------------------------------------------------------------------------------ |
134 | | - |
135 | | -function UnionPolygons(const polygons1, polygons2: TPathsD; |
136 | | - fillRule: TFillRule): TPathsD; |
137 | | -begin |
138 | | - with TClipperD.Create do |
139 | | - try |
140 | | - AddPaths(ClipperCore.TPathsD(polygons1), ptSubject); |
141 | | - AddPaths(ClipperCore.TPathsD(polygons2), ptClip); |
142 | | - Execute(ctUnion, |
143 | | - ClipperCore.TFillRule(fillRule), ClipperCore.TPathsD(result)); |
144 | | - finally |
145 | | - Free; |
146 | | - end; |
147 | | -end; |
148 | | -//------------------------------------------------------------------------------ |
149 | | - |
150 | | -function IntersectPolygons(const polygons1, polygons2: TPathsD; |
151 | | - fillRule: TFillRule): TPathsD; |
152 | | -begin |
153 | | - with TClipperD.Create do |
154 | | - try |
155 | | - AddPaths(ClipperCore.TPathsD(polygons1), ptSubject); |
156 | | - AddPaths(ClipperCore.TPathsD(polygons2), ptClip); |
157 | | - Execute(ctIntersection, |
158 | | - ClipperCore.TFillRule(fillRule), ClipperCore.TPathsD(result)); |
159 | | - finally |
160 | | - Free; |
161 | | - end; |
162 | | -end; |
163 | | -//------------------------------------------------------------------------------ |
164 | | - |
165 | | -function DifferencePolygons(const polygons1, polygons2: TPathsD; |
166 | | - fillRule: TFillRule): TPathsD; |
167 | | -begin |
168 | | - with TClipperD.Create do |
169 | | - try |
170 | | - AddPaths(ClipperCore.TPathsD(polygons1), ptSubject); |
171 | | - AddPaths(ClipperCore.TPathsD(polygons2), ptClip); |
172 | | - Execute(ctDifference, |
173 | | - ClipperCore.TFillRule(fillRule), ClipperCore.TPathsD(result)); |
174 | | - finally |
175 | | - Free; |
176 | | - end; |
177 | | -end; |
178 | | -//------------------------------------------------------------------------------ |
179 | | - |
180 | | -end. |
| 1 | +unit Img32.Clipper; |
| 2 | + |
| 3 | +(******************************************************************************* |
| 4 | +* Author : Angus Johnson * |
| 5 | +* Version : 2.24 * |
| 6 | +* Date : 26 June 2021 * |
| 7 | +* Website : http://www.angusj.com * |
| 8 | +* Copyright : Angus Johnson 2019-2021 * |
| 9 | +* Purpose : Wrapper module for the Clipper library * |
| 10 | +* License : http://www.boost.org/LICENSE_1_0.txt * |
| 11 | +*******************************************************************************) |
| 12 | + |
| 13 | +interface |
| 14 | + |
| 15 | +uses |
| 16 | + Clipper, Clipper.Core, Clipper.Engine, Clipper.Offset, |
| 17 | + Img32, Img32.Draw, Img32.Vector; |
| 18 | + |
| 19 | +//nb: InflatePath assumes that there's consistent winding where |
| 20 | +//outer paths wind in one direction and inner paths in the other |
| 21 | + |
| 22 | +function InflatePath(const path: TPathD; delta: Double; |
| 23 | + joinStyle: TJoinStyle = jsAuto; endStyle: TEndStyle = esPolygon; |
| 24 | + miterLimit: double = 2.0; arcTolerance: double = 0.0; |
| 25 | + minEdgeLength: double = 0.25): TPathsD; |
| 26 | + |
| 27 | +function InflatePaths(const paths: TPathsD; delta: Double; |
| 28 | + joinStyle: TJoinStyle = jsAuto; endStyle: TEndStyle = esPolygon; |
| 29 | + miterLimit: double = 2.0; arcTolerance: double = 0.0; |
| 30 | + minEdgeLength: double = 0): TPathsD; |
| 31 | + |
| 32 | +//UnionPolygon: removes self-intersections |
| 33 | +function UnionPolygon(const polygon: TPathD; |
| 34 | + fillRule: TFillRule): TPathsD; |
| 35 | + |
| 36 | +function UnionPolygons(const polygons: TPathsD; |
| 37 | + fillRule: TFillRule): TPathsD; overload; |
| 38 | +function UnionPolygons(const polygon1, polygon2: TPathD; |
| 39 | + fillRule: TFillRule): TPathsD; overload; |
| 40 | +function UnionPolygons(const polygons1, polygons2: TPathsD; |
| 41 | + fillRule: TFillRule): TPathsD; overload; |
| 42 | + |
| 43 | +function IntersectPolygons(const polygons1, polygons2: TPathsD; |
| 44 | + fillRule: TFillRule): TPathsD; |
| 45 | + |
| 46 | +function DifferencePolygons(const polygons1, polygons2: TPathsD; |
| 47 | + fillRule: TFillRule): TPathsD; |
| 48 | + |
| 49 | +implementation |
| 50 | + |
| 51 | +//------------------------------------------------------------------------------ |
| 52 | +//------------------------------------------------------------------------------ |
| 53 | + |
| 54 | +function InflatePath(const path: TPathD; |
| 55 | + delta: Double; joinStyle: TJoinStyle; endStyle: TEndStyle; |
| 56 | + miterLimit: double; arcTolerance: double; minEdgeLength: double): TPathsD; |
| 57 | +var |
| 58 | + paths: TPathsD; |
| 59 | +begin |
| 60 | + setLength(paths, 1); |
| 61 | + paths[0] := path; |
| 62 | + Result := InflatePaths(paths, delta, joinStyle, endStyle, |
| 63 | + miterLimit, arcTolerance, minEdgeLength); |
| 64 | +end; |
| 65 | +//------------------------------------------------------------------------------ |
| 66 | + |
| 67 | +function InflatePaths(const paths: TPathsD; |
| 68 | + delta: Double; joinStyle: TJoinStyle; endStyle: TEndStyle; |
| 69 | + miterLimit: double; arcTolerance: double; minEdgeLength: double): TPathsD; |
| 70 | +var |
| 71 | + jt: Clipper.Offset.TJoinType; |
| 72 | + et: TEndType; |
| 73 | +begin |
| 74 | + case joinStyle of |
| 75 | + jsSquare: jt := jtSquare; |
| 76 | + jsMiter: jt := jtMiter; |
| 77 | + jsRound: jt := jtRound; |
| 78 | + else if endStyle = esRound then jt := jtRound |
| 79 | + else jt := jtSquare; |
| 80 | + end; |
| 81 | + case endStyle of |
| 82 | + esButt: et := etButt; |
| 83 | + esSquare: et := etSquare; |
| 84 | + esRound: et := etRound; |
| 85 | + else et := etPolygon; |
| 86 | + end; |
| 87 | + Result := Img32.TPathsD(Clipper.InflatePaths( |
| 88 | + Clipper.Core.TPathsD(paths), delta, jt, et)); |
| 89 | +end; |
| 90 | +//------------------------------------------------------------------------------ |
| 91 | + |
| 92 | +function UnionPolygon(const polygon: TPathD; fillRule: TFillRule): TPathsD; |
| 93 | +begin |
| 94 | + with TClipperD.Create do |
| 95 | + try |
| 96 | + AddSubject(Clipper.Core.TPathD(polygon)); |
| 97 | + Execute(ctUnion, |
| 98 | + Clipper.Core.TFillRule(fillRule), Clipper.Core.TPathsD(result)); |
| 99 | + finally |
| 100 | + Free; |
| 101 | + end; |
| 102 | +end; |
| 103 | +//------------------------------------------------------------------------------ |
| 104 | + |
| 105 | +function UnionPolygons(const polygons: TPathsD; |
| 106 | + fillRule: TFillRule): TPathsD; |
| 107 | +begin |
| 108 | + with TClipperD.Create do |
| 109 | + try |
| 110 | + AddSubject(Clipper.Core.TPathsD(polygons)); |
| 111 | + Execute(ctUnion, |
| 112 | + Clipper.Core.TFillRule(fillRule), Clipper.Core.TPathsD(result)); |
| 113 | + finally |
| 114 | + Free; |
| 115 | + end; |
| 116 | +end; |
| 117 | +//------------------------------------------------------------------------------ |
| 118 | + |
| 119 | +function UnionPolygons(const polygon1, polygon2: TPathD; |
| 120 | + fillRule: TFillRule): TPathsD; |
| 121 | +begin |
| 122 | + with TClipperD.Create do |
| 123 | + try |
| 124 | + AddSubject(Clipper.Core.TPathD(polygon1)); |
| 125 | + AddClip(Clipper.Core.TPathD(polygon2)); |
| 126 | + Execute(ctUnion, |
| 127 | + Clipper.Core.TFillRule(fillRule), Clipper.Core.TPathsD(result)); |
| 128 | + finally |
| 129 | + Free; |
| 130 | + end; |
| 131 | +end; |
| 132 | +//------------------------------------------------------------------------------ |
| 133 | + |
| 134 | +function UnionPolygons(const polygons1, polygons2: TPathsD; |
| 135 | + fillRule: TFillRule): TPathsD; |
| 136 | +begin |
| 137 | + with TClipperD.Create do |
| 138 | + try |
| 139 | + AddSubject(Clipper.Core.TPathsD(polygons1)); |
| 140 | + AddClip(Clipper.Core.TPathsD(polygons2)); |
| 141 | + Execute(ctUnion, |
| 142 | + Clipper.Core.TFillRule(fillRule), Clipper.Core.TPathsD(result)); |
| 143 | + finally |
| 144 | + Free; |
| 145 | + end; |
| 146 | +end; |
| 147 | +//------------------------------------------------------------------------------ |
| 148 | + |
| 149 | +function IntersectPolygons(const polygons1, polygons2: TPathsD; |
| 150 | + fillRule: TFillRule): TPathsD; |
| 151 | +begin |
| 152 | + with TClipperD.Create do |
| 153 | + try |
| 154 | + AddSubject(Clipper.Core.TPathsD(polygons1)); |
| 155 | + AddClip(Clipper.Core.TPathsD(polygons2)); |
| 156 | + Execute(ctIntersection, |
| 157 | + Clipper.Core.TFillRule(fillRule), Clipper.Core.TPathsD(result)); |
| 158 | + finally |
| 159 | + Free; |
| 160 | + end; |
| 161 | +end; |
| 162 | +//------------------------------------------------------------------------------ |
| 163 | + |
| 164 | +function DifferencePolygons(const polygons1, polygons2: TPathsD; |
| 165 | + fillRule: TFillRule): TPathsD; |
| 166 | +begin |
| 167 | + with TClipperD.Create do |
| 168 | + try |
| 169 | + AddSubject(Clipper.Core.TPathsD(polygons1)); |
| 170 | + AddClip(Clipper.Core.TPathsD(polygons2)); |
| 171 | + Execute(ctDifference, |
| 172 | + Clipper.Core.TFillRule(fillRule), Clipper.Core.TPathsD(result)); |
| 173 | + finally |
| 174 | + Free; |
| 175 | + end; |
| 176 | +end; |
| 177 | +//------------------------------------------------------------------------------ |
| 178 | + |
| 179 | +end. |
0 commit comments