GR32_Clipper2.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. unit GR32_Clipper2;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is GR32_Clipper
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Angus Johnson
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2012-2022
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. *
  32. * ***** END LICENSE BLOCK ***** *)
  33. interface
  34. uses Gr32, Gr32_Polygons,
  35. Clipper, Clipper.Core, Clipper.Engine, Clipper.Offset;
  36. function Gr32BoolOp(clipType: TClipType; fillMode: TPolyFillMode;
  37. const subject, clip: Gr32.TArrayOfArrayOfFixedPoint):
  38. Gr32.TArrayOfArrayOfFixedPoint; overload;
  39. function Gr32_Intersect(const subject, clip: Gr32.TArrayOfArrayOfFixedPoint;
  40. fillMode: TPolyFillMode): Gr32.TArrayOfArrayOfFixedPoint; overload;
  41. function Gr32_Union(const subject, clip: Gr32.TArrayOfArrayOfFixedPoint;
  42. fillMode: TPolyFillMode): Gr32.TArrayOfArrayOfFixedPoint; overload;
  43. function Gr32_Difference(const subject, clip: Gr32.TArrayOfArrayOfFixedPoint;
  44. fillMode: TPolyFillMode): Gr32.TArrayOfArrayOfFixedPoint; overload;
  45. function Gr32_XOR(const subject, clip: Gr32.TArrayOfArrayOfFixedPoint;
  46. fillMode: TPolyFillMode): Gr32.TArrayOfArrayOfFixedPoint; overload;
  47. function Gr32_Inflate(const paths: Gr32.TArrayOfArrayOfFixedPoint;
  48. delta: double; jointType: TJoinType; endType: TEndType;
  49. miterLimit: double = 2): Gr32.TArrayOfArrayOfFixedPoint; overload;
  50. function Gr32BoolOp(clipType: TClipType; fillMode: TPolyFillMode;
  51. const subject, clip: Gr32.TArrayOfArrayOfFloatPoint):
  52. Gr32.TArrayOfArrayOfFloatPoint; overload;
  53. function Gr32_Intersect(const subject, clip: Gr32.TArrayOfArrayOfFloatPoint;
  54. fillMode: TPolyFillMode): Gr32.TArrayOfArrayOfFloatPoint; overload;
  55. function Gr32_Union(const subject, clip: Gr32.TArrayOfArrayOfFloatPoint;
  56. fillMode: TPolyFillMode): Gr32.TArrayOfArrayOfFloatPoint; overload;
  57. function Gr32_Difference(const subject, clip: Gr32.TArrayOfArrayOfFloatPoint;
  58. fillMode: TPolyFillMode): Gr32.TArrayOfArrayOfFloatPoint; overload;
  59. function Gr32_XOR(const subject, clip: Gr32.TArrayOfArrayOfFloatPoint;
  60. fillMode: TPolyFillMode): Gr32.TArrayOfArrayOfFloatPoint; overload;
  61. function Gr32_Inflate(const paths: Gr32.TArrayOfArrayOfFloatPoint;
  62. delta: double; jointType: TJoinType; endType: TEndType;
  63. miterLimit: double = 2): Gr32.TArrayOfArrayOfFloatPoint; overload;
  64. function FixedPointsToPath64(const pathFixed: Gr32.TArrayOfFixedPoint): Clipper.TPath64;
  65. function FloatPointsToPath64(const pathFloat: Gr32.TArrayOfFloatPoint): Clipper.TPath64;
  66. function FixedPointsToPaths64(const pathsFixed: Gr32.TArrayOfArrayOfFixedPoint): Clipper.TPaths64;
  67. function FloatPointsToPaths64(const pathsFloat: Gr32.TArrayOfArrayOfFloatPoint): Clipper.TPaths64;
  68. function Paths64ToFixedPoints(const paths: Clipper.TPaths64): Gr32.TArrayOfArrayOfFixedPoint;
  69. function Paths64ToFloatPoints(const paths: Clipper.TPaths64): Gr32.TArrayOfArrayOfFloatPoint;
  70. function FloatRect(const r: TRect64): GR32.TFloatRect;
  71. type
  72. TClipper = Clipper.Engine.TClipper64;
  73. TClipper64 = Clipper.Engine.TClipper64;
  74. TPoint64 = Clipper.Core.TPoint64;
  75. TRect64 = Clipper.Core.TRect64;
  76. TPath64 = Clipper.Core.TPath64;
  77. TPaths64 = Clipper.Core.TPaths64;
  78. TPointD = Clipper.Core.TPointD;
  79. TRectD = Clipper.Core.TRectD;
  80. TPathD = Clipper.Core.TPathD;
  81. TPathsD = Clipper.Core.TPathsD;
  82. TFillRule = Clipper.Core.TFillRule;
  83. TPolyTree64 = Clipper.Engine.TPolyTree64;
  84. TPolyTreeD = Clipper.Engine.TPolyTreeD;
  85. TJoinType = Clipper.Offset.TJoinType;
  86. TEndType = Clipper.Offset.TEndType;
  87. TClipType = Clipper.Core.TClipType;
  88. const
  89. frEvenOdd = Clipper.Core.frEvenOdd;
  90. frNonZero = Clipper.Core.frNonZero;
  91. frPositive = Clipper.Core.frPositive;
  92. frNegative = Clipper.Core.frNegative;
  93. jtSquare = Clipper.Offset.jtSquare;
  94. jtRound = Clipper.Offset.jtRound;
  95. jtMiter = Clipper.Offset.jtMiter;
  96. etPolygon = Clipper.Offset.etPolygon;
  97. etJoined = Clipper.Offset.etJoined;
  98. etButt = Clipper.Offset.etButt;
  99. etSquare = Clipper.Offset.etSquare;
  100. etRound = Clipper.Offset.etRound;
  101. ctNone = Clipper.Core.ctNone;
  102. ctIntersection = Clipper.Core.ctIntersection;
  103. ctUnion = Clipper.Core.ctUnion;
  104. ctDifference = Clipper.Core.ctDifference;
  105. ctXor = Clipper.Core.ctXor;
  106. function ClipperFloatScale: Double;
  107. function SetClipperFloatScale(Value: Double): Double;
  108. implementation
  109. var
  110. FClipperFloatScale: Double;
  111. FClipperInvFloatScale: Double;
  112. function ClipperFloatScale: Double;
  113. begin
  114. Result := FClipperFloatScale;
  115. end;
  116. function SetClipperFloatScale(Value: Double): Double;
  117. begin
  118. Result := FClipperFloatScale;
  119. if (Value <> 0) then
  120. begin
  121. FClipperFloatScale := Value;
  122. FClipperInvFloatScale := 1/FClipperFloatScale;
  123. end;
  124. end;
  125. function DblToInt64(val: double): Int64; {$IFDEF INLINE} inline; {$ENDIF}
  126. var
  127. exp: integer;
  128. i64: UInt64 absolute val;
  129. begin
  130. //https://en.wikipedia.org/wiki/Double-precision_floating-point_format
  131. Result := 0;
  132. if i64 = 0 then Exit;
  133. exp := Integer(Cardinal(i64 shr 52) and $7FF) - 1023;
  134. //nb: when exp == 1024 then val == INF or NAN.
  135. if exp < 0 then Exit;
  136. Result := ((i64 and $1FFFFFFFFFFFFF) shr (52 - exp)) or (UInt64(1) shl exp);
  137. if val < 0 then Result := -Result;
  138. end;
  139. function FloatRect(const r: TRect64): GR32.TFloatRect;
  140. begin
  141. Result.Left := DblToInt64(r.Left * FClipperFloatScale);
  142. Result.Top := DblToInt64(r.Top * FClipperFloatScale);
  143. Result.Right := DblToInt64(r.Right * FClipperFloatScale);
  144. Result.Bottom := DblToInt64(r.Bottom * FClipperFloatScale);
  145. end;
  146. function FixedPointsToPath64(const pathFixed: Gr32.TArrayOfFixedPoint): Clipper.TPath64;
  147. var
  148. i, len: integer;
  149. begin
  150. len := Length(pathFixed);
  151. SetLength(Result, len);
  152. for i := 0 to len -1 do
  153. begin
  154. Result[i].X := pathFixed[i].X;
  155. Result[i].Y := pathFixed[i].Y;
  156. end;
  157. end;
  158. function FloatPointsToPath64(const pathFloat: Gr32.TArrayOfFloatPoint): Clipper.TPath64;
  159. var
  160. i, len: integer;
  161. begin
  162. len := Length(pathFloat);
  163. SetLength(Result, len);
  164. for i := 0 to len -1 do
  165. begin
  166. Result[i].X := DblToInt64(pathFloat[i].X * FClipperFloatScale);
  167. Result[i].Y := DblToInt64(pathFloat[i].Y * FClipperFloatScale);
  168. end;
  169. end;
  170. function FixedPointsToPaths64(
  171. const pathsFixed: Gr32.TArrayOfArrayOfFixedPoint): Clipper.TPaths64;
  172. var
  173. i, len: integer;
  174. begin
  175. len := Length(pathsFixed);
  176. SetLength(Result, len);
  177. for i := 0 to len -1 do
  178. Result[i] := FixedPointsToPath64(pathsFixed[i]);
  179. end;
  180. function FloatPointsToPaths64(
  181. const pathsFloat: Gr32.TArrayOfArrayOfFloatPoint): Clipper.TPaths64;
  182. var
  183. i, len: integer;
  184. begin
  185. len := Length(pathsFloat);
  186. SetLength(Result, len);
  187. for i := 0 to len -1 do
  188. Result[i] := FloatPointsToPath64(pathsFloat[i]);
  189. end;
  190. function Path64ToFixedPoints(const path: Clipper.TPath64): Gr32.TArrayOfFixedPoint;
  191. var
  192. i, len: integer;
  193. begin
  194. len := Length(path);
  195. SetLength(Result, len);
  196. for i := 0 to len -1 do
  197. begin
  198. Result[i].X := TFixed(path[i].X);
  199. Result[i].Y := TFixed(path[i].Y);
  200. end;
  201. end;
  202. function Path64ToFloatPoints(const path: Clipper.TPath64): Gr32.TArrayOfFloatPoint;
  203. var
  204. i, len: integer;
  205. begin
  206. len := Length(path);
  207. SetLength(Result, len);
  208. for i := 0 to len -1 do
  209. begin
  210. Result[i].X := path[i].X * FClipperInvFloatScale;
  211. Result[i].Y := path[i].Y * FClipperInvFloatScale;
  212. end;
  213. end;
  214. function Paths64ToFixedPoints(const paths: Clipper.TPaths64): Gr32.TArrayOfArrayOfFixedPoint;
  215. var
  216. i, len: integer;
  217. begin
  218. len := Length(paths);
  219. SetLength(Result, len);
  220. for i := 0 to len -1 do
  221. Result[i] := Path64ToFixedPoints(paths[i]);
  222. end;
  223. function Paths64ToFloatPoints(const paths: Clipper.TPaths64): Gr32.TArrayOfArrayOfFloatPoint;
  224. var
  225. i, len: integer;
  226. begin
  227. len := Length(paths);
  228. SetLength(Result, len);
  229. for i := 0 to len -1 do
  230. Result[i] := Path64ToFloatPoints(paths[i]);
  231. end;
  232. function Gr32BoolOp(clipType: TClipType; fillMode: TPolyFillMode;
  233. const subject, clip: Gr32.TArrayOfArrayOfFixedPoint):
  234. Gr32.TArrayOfArrayOfFixedPoint;
  235. var
  236. sub, clp, sol: TPaths64;
  237. begin
  238. sub := FixedPointsToPaths64(subject);
  239. clp := FixedPointsToPaths64(clip);
  240. sol := Clipper.BooleanOp(clipType, sub, clp, TFillRule(fillMode));
  241. Result := Paths64ToFixedPoints(sol);
  242. end;
  243. function Gr32BoolOp(clipType: TClipType; fillMode: TPolyFillMode;
  244. const subject, clip: Gr32.TArrayOfArrayOfFloatPoint):
  245. Gr32.TArrayOfArrayOfFloatPoint;
  246. var
  247. sub, clp, sol: TPaths64;
  248. begin
  249. sub := FloatPointsToPaths64(subject);
  250. clp := FloatPointsToPaths64(clip);
  251. sol := Clipper.BooleanOp(clipType, sub, clp, TFillRule(fillMode));
  252. Result := Paths64ToFloatPoints(sol);
  253. end;
  254. function Gr32_Intersect(const subject, clip: Gr32.TArrayOfArrayOfFixedPoint;
  255. fillMode: TPolyFillMode): Gr32.TArrayOfArrayOfFixedPoint;
  256. begin
  257. Result := Gr32BoolOp(ctIntersection, fillMode, subject, clip);
  258. end;
  259. function Gr32_Union(const subject, clip: Gr32.TArrayOfArrayOfFixedPoint;
  260. fillMode: TPolyFillMode): Gr32.TArrayOfArrayOfFixedPoint;
  261. begin
  262. Result := Gr32BoolOp(ctUnion, fillMode, subject, clip);
  263. end;
  264. function Gr32_Difference(const subject, clip: Gr32.TArrayOfArrayOfFixedPoint;
  265. fillMode: TPolyFillMode): Gr32.TArrayOfArrayOfFixedPoint;
  266. begin
  267. Result := Gr32BoolOp(ctDifference, fillMode, subject, clip);
  268. end;
  269. function Gr32_XOR(const subject, clip: Gr32.TArrayOfArrayOfFixedPoint;
  270. fillMode: TPolyFillMode): Gr32.TArrayOfArrayOfFixedPoint;
  271. begin
  272. Result := Gr32BoolOp(ctXor, fillMode, subject, clip);
  273. end;
  274. function Gr32_Inflate(const paths: Gr32.TArrayOfArrayOfFixedPoint;
  275. delta: double; jointType: TJoinType; endType: TEndType;
  276. miterLimit: double = 2): Gr32.TArrayOfArrayOfFixedPoint;
  277. var
  278. sub, sol: TPaths64;
  279. begin
  280. sub := FixedPointsToPaths64(paths);
  281. sol := Clipper.InflatePaths(sub, delta * FixedOne,
  282. jointType, endType, miterLimit);
  283. sol := RamerDouglasPeucker(sol, 10);
  284. Result := Paths64ToFixedPoints(sol);
  285. end;
  286. function Gr32_Intersect(const subject, clip: Gr32.TArrayOfArrayOfFloatPoint;
  287. fillMode: TPolyFillMode): Gr32.TArrayOfArrayOfFloatPoint;
  288. begin
  289. Result := Gr32BoolOp(ctIntersection, fillMode, subject, clip);
  290. end;
  291. function Gr32_Union(const subject, clip: Gr32.TArrayOfArrayOfFloatPoint;
  292. fillMode: TPolyFillMode): Gr32.TArrayOfArrayOfFloatPoint;
  293. begin
  294. Result := Gr32BoolOp(ctUnion, fillMode, subject, clip);
  295. end;
  296. function Gr32_Difference(const subject, clip: Gr32.TArrayOfArrayOfFloatPoint;
  297. fillMode: TPolyFillMode): Gr32.TArrayOfArrayOfFloatPoint;
  298. begin
  299. Result := Gr32BoolOp(ctDifference, fillMode, subject, clip);
  300. end;
  301. function Gr32_XOR(const subject, clip: Gr32.TArrayOfArrayOfFloatPoint;
  302. fillMode: TPolyFillMode): Gr32.TArrayOfArrayOfFloatPoint;
  303. begin
  304. Result := Gr32BoolOp(ctXor, fillMode, subject, clip);
  305. end;
  306. function Gr32_Inflate(const paths: Gr32.TArrayOfArrayOfFloatPoint;
  307. delta: double; jointType: TJoinType; endType: TEndType;
  308. miterLimit: double = 2): Gr32.TArrayOfArrayOfFloatPoint;
  309. var
  310. sub, sol: TPaths64;
  311. begin
  312. sub := FloatPointsToPaths64(paths);
  313. sol := Clipper.InflatePaths(sub, delta * FClipperFloatScale,
  314. jointType, endType, miterLimit);
  315. sol := RamerDouglasPeucker(sol, 1);
  316. Result := Paths64ToFloatPoints(sol);
  317. end;
  318. initialization
  319. SetClipperFloatScale(100);
  320. end.