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