@math.gl/geometry 5.0.0-alpha.17 → 5.0.0-alpha.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (294) hide show
  1. package/README.md +7 -1
  2. package/dist/culling/constants.d.ts +3 -0
  3. package/dist/culling/constants.d.ts.map +1 -0
  4. package/dist/culling/constants.js +5 -0
  5. package/dist/culling/constants.js.map +1 -0
  6. package/dist/culling/index.cjs +2022 -0
  7. package/dist/culling/index.cjs.map +6 -0
  8. package/dist/culling/index.d.ts +28 -0
  9. package/dist/culling/index.d.ts.map +1 -0
  10. package/dist/culling/index.js +22 -0
  11. package/dist/culling/index.js.map +1 -0
  12. package/dist/culling/lib/algorithms/bounding-box-from-points.d.ts +15 -0
  13. package/dist/culling/lib/algorithms/bounding-box-from-points.d.ts.map +1 -0
  14. package/dist/culling/lib/algorithms/bounding-box-from-points.js +137 -0
  15. package/dist/culling/lib/algorithms/bounding-box-from-points.js.map +1 -0
  16. package/dist/culling/lib/algorithms/bounding-sphere-from-points.d.ts +14 -0
  17. package/dist/culling/lib/algorithms/bounding-sphere-from-points.d.ts.map +1 -0
  18. package/dist/culling/lib/algorithms/bounding-sphere-from-points.js +146 -0
  19. package/dist/culling/lib/algorithms/bounding-sphere-from-points.js.map +1 -0
  20. package/dist/culling/lib/algorithms/compute-eigen-decomposition.d.ts +37 -0
  21. package/dist/culling/lib/algorithms/compute-eigen-decomposition.d.ts.map +1 -0
  22. package/dist/culling/lib/algorithms/compute-eigen-decomposition.js +137 -0
  23. package/dist/culling/lib/algorithms/compute-eigen-decomposition.js.map +1 -0
  24. package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +56 -0
  25. package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.d.ts.map +1 -0
  26. package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.js +116 -0
  27. package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.js.map +1 -0
  28. package/dist/culling/lib/bounding-volumes/bounding-sphere.d.ts +40 -0
  29. package/dist/culling/lib/bounding-volumes/bounding-sphere.d.ts.map +1 -0
  30. package/dist/culling/lib/bounding-volumes/bounding-sphere.js +123 -0
  31. package/dist/culling/lib/bounding-volumes/bounding-sphere.js.map +1 -0
  32. package/dist/culling/lib/bounding-volumes/bounding-volume.d.ts +30 -0
  33. package/dist/culling/lib/bounding-volumes/bounding-volume.d.ts.map +1 -0
  34. package/dist/culling/lib/bounding-volumes/bounding-volume.js +7 -0
  35. package/dist/culling/lib/bounding-volumes/bounding-volume.js.map +1 -0
  36. package/dist/culling/lib/bounding-volumes/oriented-bounding-box.d.ts +68 -0
  37. package/dist/culling/lib/bounding-volumes/oriented-bounding-box.d.ts.map +1 -0
  38. package/dist/culling/lib/bounding-volumes/oriented-bounding-box.js +259 -0
  39. package/dist/culling/lib/bounding-volumes/oriented-bounding-box.js.map +1 -0
  40. package/dist/culling/lib/culling-volume.d.ts +46 -0
  41. package/dist/culling/lib/culling-volume.d.ts.map +1 -0
  42. package/dist/culling/lib/culling-volume.js +126 -0
  43. package/dist/culling/lib/culling-volume.js.map +1 -0
  44. package/dist/culling/lib/perspective-frustum.d.ts +153 -0
  45. package/dist/culling/lib/perspective-frustum.d.ts.map +1 -0
  46. package/dist/culling/lib/perspective-frustum.js +191 -0
  47. package/dist/culling/lib/perspective-frustum.js.map +1 -0
  48. package/dist/culling/lib/perspective-off-center-frustum.d.ts +158 -0
  49. package/dist/culling/lib/perspective-off-center-frustum.d.ts.map +1 -0
  50. package/dist/culling/lib/perspective-off-center-frustum.js +276 -0
  51. package/dist/culling/lib/perspective-off-center-frustum.js.map +1 -0
  52. package/dist/culling/lib/plane.d.ts +35 -0
  53. package/dist/culling/lib/plane.d.ts.map +1 -0
  54. package/dist/culling/lib/plane.js +90 -0
  55. package/dist/culling/lib/plane.js.map +1 -0
  56. package/dist/culling/lib/ray.d.ts +13 -0
  57. package/dist/culling/lib/ray.d.ts.map +1 -0
  58. package/dist/culling/lib/ray.js +30 -0
  59. package/dist/culling/lib/ray.js.map +1 -0
  60. package/dist/culling/lib/shapes/box-shape.d.ts +19 -0
  61. package/dist/culling/lib/shapes/box-shape.d.ts.map +1 -0
  62. package/dist/culling/lib/shapes/box-shape.js +69 -0
  63. package/dist/culling/lib/shapes/box-shape.js.map +1 -0
  64. package/dist/culling/lib/shapes/capsule-shape.d.ts +23 -0
  65. package/dist/culling/lib/shapes/capsule-shape.d.ts.map +1 -0
  66. package/dist/culling/lib/shapes/capsule-shape.js +151 -0
  67. package/dist/culling/lib/shapes/capsule-shape.js.map +1 -0
  68. package/dist/culling/lib/shapes/cylinder-shape.d.ts +22 -0
  69. package/dist/culling/lib/shapes/cylinder-shape.d.ts.map +1 -0
  70. package/dist/culling/lib/shapes/cylinder-shape.js +99 -0
  71. package/dist/culling/lib/shapes/cylinder-shape.js.map +1 -0
  72. package/dist/culling/lib/shapes/implicit-shape.d.ts +56 -0
  73. package/dist/culling/lib/shapes/implicit-shape.d.ts.map +1 -0
  74. package/dist/culling/lib/shapes/implicit-shape.js +142 -0
  75. package/dist/culling/lib/shapes/implicit-shape.js.map +1 -0
  76. package/dist/culling/lib/shapes/plane-shape.d.ts +27 -0
  77. package/dist/culling/lib/shapes/plane-shape.d.ts.map +1 -0
  78. package/dist/culling/lib/shapes/plane-shape.js +71 -0
  79. package/dist/culling/lib/shapes/plane-shape.js.map +1 -0
  80. package/dist/culling/lib/shapes/sphere-shape.d.ts +18 -0
  81. package/dist/culling/lib/shapes/sphere-shape.d.ts.map +1 -0
  82. package/dist/culling/lib/shapes/sphere-shape.js +43 -0
  83. package/dist/culling/lib/shapes/sphere-shape.js.map +1 -0
  84. package/dist/culling/oriented-box-2d.cjs +73 -0
  85. package/dist/culling/oriented-box-2d.cjs.map +6 -0
  86. package/dist/culling/oriented-box-2d.d.ts +22 -0
  87. package/dist/culling/oriented-box-2d.d.ts.map +1 -0
  88. package/dist/culling/oriented-box-2d.js +86 -0
  89. package/dist/culling/oriented-box-2d.js.map +1 -0
  90. package/dist/culling/queries.cjs +161 -0
  91. package/dist/culling/queries.cjs.map +6 -0
  92. package/dist/culling/queries.d.ts +20 -0
  93. package/dist/culling/queries.d.ts.map +1 -0
  94. package/dist/culling/queries.js +139 -0
  95. package/dist/culling/queries.js.map +1 -0
  96. package/dist/curves/bezier-curves.d.ts +18 -0
  97. package/dist/curves/bezier-curves.d.ts.map +1 -0
  98. package/dist/curves/bezier-curves.js +54 -0
  99. package/dist/curves/bezier-curves.js.map +1 -0
  100. package/dist/curves/catmull-rom-curve.d.ts +15 -0
  101. package/dist/curves/catmull-rom-curve.d.ts.map +1 -0
  102. package/dist/curves/catmull-rom-curve.js +77 -0
  103. package/dist/curves/catmull-rom-curve.js.map +1 -0
  104. package/dist/curves/curve-arc-length.d.ts +17 -0
  105. package/dist/curves/curve-arc-length.d.ts.map +1 -0
  106. package/dist/curves/curve-arc-length.js +58 -0
  107. package/dist/curves/curve-arc-length.js.map +1 -0
  108. package/dist/curves/curve.d.ts +23 -0
  109. package/dist/curves/curve.d.ts.map +1 -0
  110. package/dist/curves/curve.js +77 -0
  111. package/dist/curves/curve.js.map +1 -0
  112. package/dist/curves/hermite-curve.d.ts +9 -0
  113. package/dist/curves/hermite-curve.d.ts.map +1 -0
  114. package/dist/curves/hermite-curve.js +24 -0
  115. package/dist/curves/hermite-curve.js.map +1 -0
  116. package/dist/curves/index.cjs +293 -0
  117. package/dist/curves/index.cjs.map +6 -0
  118. package/dist/curves/index.d.ts +8 -0
  119. package/dist/curves/index.d.ts.map +1 -0
  120. package/dist/curves/index.js +9 -0
  121. package/dist/curves/index.js.map +1 -0
  122. package/dist/lib/geometry.d.ts +1 -1
  123. package/dist/lib/geometry.d.ts.map +1 -1
  124. package/dist/polygon/cut-by-grid.d.ts +24 -0
  125. package/dist/polygon/cut-by-grid.d.ts.map +1 -0
  126. package/dist/polygon/cut-by-grid.js +221 -0
  127. package/dist/polygon/cut-by-grid.js.map +1 -0
  128. package/dist/polygon/cut-by-mercator-bounds.d.ts +17 -0
  129. package/dist/polygon/cut-by-mercator-bounds.d.ts.map +1 -0
  130. package/dist/polygon/cut-by-mercator-bounds.js +149 -0
  131. package/dist/polygon/cut-by-mercator-bounds.js.map +1 -0
  132. package/dist/polygon/earcut.d.ts +13 -0
  133. package/dist/polygon/earcut.d.ts.map +1 -0
  134. package/dist/polygon/earcut.js +627 -0
  135. package/dist/polygon/earcut.js.map +1 -0
  136. package/dist/polygon/index.cjs +1414 -0
  137. package/dist/polygon/index.cjs.map +6 -0
  138. package/dist/polygon/index.d.ts +12 -0
  139. package/dist/polygon/index.d.ts.map +1 -0
  140. package/dist/polygon/index.js +12 -0
  141. package/dist/polygon/index.js.map +1 -0
  142. package/dist/polygon/lineclip.d.ts +31 -0
  143. package/dist/polygon/lineclip.d.ts.map +1 -0
  144. package/dist/polygon/lineclip.js +184 -0
  145. package/dist/polygon/lineclip.js.map +1 -0
  146. package/dist/polygon/polygon-utils.d.ts +103 -0
  147. package/dist/polygon/polygon-utils.d.ts.map +1 -0
  148. package/dist/polygon/polygon-utils.js +155 -0
  149. package/dist/polygon/polygon-utils.js.map +1 -0
  150. package/dist/polygon/polygon.d.ts +41 -0
  151. package/dist/polygon/polygon.d.ts.map +1 -0
  152. package/dist/polygon/polygon.js +76 -0
  153. package/dist/polygon/polygon.js.map +1 -0
  154. package/dist/polygon/subdivide-polyline.d.ts +42 -0
  155. package/dist/polygon/subdivide-polyline.d.ts.map +1 -0
  156. package/dist/polygon/subdivide-polyline.js +109 -0
  157. package/dist/polygon/subdivide-polyline.js.map +1 -0
  158. package/dist/polygon/subdivide-triangle-mesh.d.ts +68 -0
  159. package/dist/polygon/subdivide-triangle-mesh.d.ts.map +1 -0
  160. package/dist/polygon/subdivide-triangle-mesh.js +228 -0
  161. package/dist/polygon/subdivide-triangle-mesh.js.map +1 -0
  162. package/dist/polygon/utils.d.ts +5 -0
  163. package/dist/polygon/utils.d.ts.map +1 -0
  164. package/dist/polygon/utils.js +38 -0
  165. package/dist/polygon/utils.js.map +1 -0
  166. package/dist/spatial-index/box-index.cjs +328 -0
  167. package/dist/spatial-index/box-index.cjs.map +6 -0
  168. package/dist/spatial-index/box-index.d.ts +65 -0
  169. package/dist/spatial-index/box-index.d.ts.map +1 -0
  170. package/dist/spatial-index/box-index.js +263 -0
  171. package/dist/spatial-index/box-index.js.map +1 -0
  172. package/dist/spatial-index/index.cjs +494 -0
  173. package/dist/spatial-index/index.cjs.map +6 -0
  174. package/dist/spatial-index/index.d.ts +5 -0
  175. package/dist/spatial-index/index.d.ts.map +1 -0
  176. package/dist/spatial-index/index.js +6 -0
  177. package/dist/spatial-index/index.js.map +1 -0
  178. package/dist/spatial-index/triangle-bvh.d.ts +53 -0
  179. package/dist/spatial-index/triangle-bvh.d.ts.map +1 -0
  180. package/dist/spatial-index/triangle-bvh.js +102 -0
  181. package/dist/spatial-index/triangle-bvh.js.map +1 -0
  182. package/dist/utils/attribute-compression.d.ts +50 -0
  183. package/dist/utils/attribute-compression.d.ts.map +1 -0
  184. package/dist/utils/attribute-compression.js +186 -0
  185. package/dist/utils/attribute-compression.js.map +1 -0
  186. package/dist/utils/attribute-iterator.d.ts +8 -0
  187. package/dist/utils/attribute-iterator.d.ts.map +1 -0
  188. package/dist/utils/attribute-iterator.js +25 -0
  189. package/dist/utils/attribute-iterator.js.map +1 -0
  190. package/dist/utils/compute-vertex-normals.d.ts +4 -0
  191. package/dist/utils/compute-vertex-normals.d.ts.map +1 -0
  192. package/dist/utils/compute-vertex-normals.js +54 -0
  193. package/dist/utils/compute-vertex-normals.js.map +1 -0
  194. package/dist/utils/constants.d.ts +52 -0
  195. package/dist/utils/constants.d.ts.map +1 -0
  196. package/dist/utils/constants.js +39 -0
  197. package/dist/utils/constants.js.map +1 -0
  198. package/dist/utils/coordinates.d.ts +3 -0
  199. package/dist/utils/coordinates.d.ts.map +1 -0
  200. package/dist/utils/coordinates.js +8 -0
  201. package/dist/utils/coordinates.js.map +1 -0
  202. package/dist/utils/geometry.d.ts +20 -0
  203. package/dist/utils/geometry.d.ts.map +1 -0
  204. package/dist/utils/geometry.js +31 -0
  205. package/dist/utils/geometry.js.map +1 -0
  206. package/dist/utils/gl-type.d.ts +17 -0
  207. package/dist/utils/gl-type.d.ts.map +1 -0
  208. package/dist/utils/gl-type.js +69 -0
  209. package/dist/utils/gl-type.js.map +1 -0
  210. package/dist/utils/index.cjs +965 -0
  211. package/dist/utils/index.cjs.map +6 -0
  212. package/dist/utils/index.d.ts +18 -0
  213. package/dist/utils/index.d.ts.map +1 -0
  214. package/dist/utils/index.js +16 -0
  215. package/dist/utils/index.js.map +1 -0
  216. package/dist/utils/interpolate-packed-attributes.cjs +176 -0
  217. package/dist/utils/interpolate-packed-attributes.cjs.map +6 -0
  218. package/dist/utils/interpolate-packed-attributes.d.ts +43 -0
  219. package/dist/utils/interpolate-packed-attributes.d.ts.map +1 -0
  220. package/dist/utils/interpolate-packed-attributes.js +192 -0
  221. package/dist/utils/interpolate-packed-attributes.js.map +1 -0
  222. package/dist/utils/mesh-processing.d.ts +18 -0
  223. package/dist/utils/mesh-processing.d.ts.map +1 -0
  224. package/dist/utils/mesh-processing.js +300 -0
  225. package/dist/utils/mesh-processing.js.map +1 -0
  226. package/dist/utils/primitive-iterator.d.ts +21 -0
  227. package/dist/utils/primitive-iterator.d.ts.map +1 -0
  228. package/dist/utils/primitive-iterator.js +117 -0
  229. package/dist/utils/primitive-iterator.js.map +1 -0
  230. package/dist/utils/rgb565.d.ts +5 -0
  231. package/dist/utils/rgb565.d.ts.map +1 -0
  232. package/dist/utils/rgb565.js +24 -0
  233. package/dist/utils/rgb565.js.map +1 -0
  234. package/dist/utils/typed-array-utils.d.ts +3 -0
  235. package/dist/utils/typed-array-utils.d.ts.map +1 -0
  236. package/dist/utils/typed-array-utils.js +16 -0
  237. package/dist/utils/typed-array-utils.js.map +1 -0
  238. package/package.json +49 -3
  239. package/src/culling/constants.ts +6 -0
  240. package/src/culling/index.ts +40 -0
  241. package/src/culling/lib/algorithms/bounding-box-from-points.ts +174 -0
  242. package/src/culling/lib/algorithms/bounding-sphere-from-points.ts +175 -0
  243. package/src/culling/lib/algorithms/compute-eigen-decomposition.ts +171 -0
  244. package/src/culling/lib/bounding-volumes/axis-aligned-bounding-box.ts +153 -0
  245. package/src/culling/lib/bounding-volumes/bounding-sphere.ts +154 -0
  246. package/src/culling/lib/bounding-volumes/bounding-volume.ts +38 -0
  247. package/src/culling/lib/bounding-volumes/oriented-bounding-box.ts +355 -0
  248. package/src/culling/lib/culling-volume.ts +163 -0
  249. package/src/culling/lib/perspective-frustum.ts +288 -0
  250. package/src/culling/lib/perspective-off-center-frustum.ts +381 -0
  251. package/src/culling/lib/plane.ts +118 -0
  252. package/src/culling/lib/ray.ts +33 -0
  253. package/src/culling/lib/shapes/box-shape.ts +78 -0
  254. package/src/culling/lib/shapes/capsule-shape.ts +232 -0
  255. package/src/culling/lib/shapes/cylinder-shape.ts +133 -0
  256. package/src/culling/lib/shapes/implicit-shape.ts +209 -0
  257. package/src/culling/lib/shapes/plane-shape.ts +78 -0
  258. package/src/culling/lib/shapes/sphere-shape.ts +60 -0
  259. package/src/culling/oriented-box-2d.ts +165 -0
  260. package/src/culling/queries.ts +155 -0
  261. package/src/curves/bezier-curves.ts +62 -0
  262. package/src/curves/catmull-rom-curve.ts +89 -0
  263. package/src/curves/curve-arc-length.ts +70 -0
  264. package/src/curves/curve.ts +96 -0
  265. package/src/curves/hermite-curve.ts +34 -0
  266. package/src/curves/index.ts +15 -0
  267. package/src/lib/geometry-utils.ts +1 -1
  268. package/src/lib/geometry.ts +1 -1
  269. package/src/polygon/cut-by-grid.ts +313 -0
  270. package/src/polygon/cut-by-mercator-bounds.ts +204 -0
  271. package/src/polygon/earcut.ts +798 -0
  272. package/src/polygon/index.ts +31 -0
  273. package/src/polygon/lineclip.ts +218 -0
  274. package/src/polygon/polygon-utils.ts +273 -0
  275. package/src/polygon/polygon.ts +105 -0
  276. package/src/polygon/subdivide-polyline.ts +160 -0
  277. package/src/polygon/subdivide-triangle-mesh.ts +322 -0
  278. package/src/polygon/utils.ts +50 -0
  279. package/src/spatial-index/box-index.ts +343 -0
  280. package/src/spatial-index/index.ts +18 -0
  281. package/src/spatial-index/triangle-bvh.ts +162 -0
  282. package/src/utils/attribute-compression.ts +234 -0
  283. package/src/utils/attribute-iterator.ts +28 -0
  284. package/src/utils/compute-vertex-normals.ts +63 -0
  285. package/src/utils/constants.ts +42 -0
  286. package/src/utils/coordinates.ts +8 -0
  287. package/src/utils/geometry.ts +49 -0
  288. package/src/utils/gl-type.ts +87 -0
  289. package/src/utils/index.ts +55 -0
  290. package/src/utils/interpolate-packed-attributes.ts +284 -0
  291. package/src/utils/mesh-processing.ts +377 -0
  292. package/src/utils/primitive-iterator.ts +145 -0
  293. package/src/utils/rgb565.ts +26 -0
  294. package/src/utils/typed-array-utils.ts +16 -0
@@ -0,0 +1,798 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT AND ISC
3
+ // SPDX-FileCopyrightText: Copyright (c) 2016, Mapbox
4
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
5
+ // SPDX-FileComment: Adapted from Mapbox earcut. Original ISC notice retained below.
6
+
7
+ /*
8
+ Adapted from https://github.com/mapbox/earcut to allow passing in
9
+ of outline and hole areas using the `areas` parameter. As the
10
+ areas are calcuted as part of classifying the polygon rings
11
+ we can pass them in again to avoid recomputation
12
+
13
+ ISC License
14
+
15
+ Copyright (c) 2016, Mapbox
16
+
17
+ Permission to use, copy, modify, and/or distribute this software for any purpose
18
+ with or without fee is hereby granted, provided that the above copyright notice
19
+ and this permission notice appear in all copies.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
22
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
23
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
24
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
25
+ OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
26
+ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
27
+ THIS SOFTWARE.
28
+
29
+ */
30
+
31
+ // @ts-nocheck
32
+
33
+ /* eslint-disable */
34
+
35
+ import type {NumericArray} from '@math.gl/core';
36
+ import {getPolygonSignedArea, DimIndex, Plane2D} from './polygon-utils';
37
+
38
+ /**
39
+ * Computes a triangulation of a polygon
40
+ * @param positions a flat array of the vertex positions that define the polygon.
41
+ * @param holeIndices an array of hole indices if any (e.g. [5, 8] for a 12-vertex input would mean one hole with vertices 5–7 and another with 8–11).
42
+ * @param dim the number of elements in each vertex. Size `2` will interpret `positions` as `[x0, y0, x1, y1, ...]` and size `3` will interpret `positions` as `[x0, y0, z0, x1, y1, z1, ...]`. Default `2`.
43
+ * @param areas areas of outer polygon and holes as computed by `getPolygonSignedArea()`. Can be optionally supplied to speed up triangulation
44
+ * @returns array of indices into the `positions` array that describes the triangulation of the polygon
45
+ * Adapted from https://github.com/mapbox/earcut
46
+ */
47
+ export function earcut(
48
+ positions: NumericArray,
49
+ holeIndices?: NumericArray,
50
+ dim: number = 2,
51
+ areas?: NumericArray,
52
+ plane: Plane2D = 'xy'
53
+ ): number[] {
54
+ const hasHoles = holeIndices && holeIndices.length;
55
+ const outerLen = hasHoles ? holeIndices[0] * dim : positions.length;
56
+ let outerNode = linkedList(positions, 0, outerLen, dim, true, areas && areas[0], plane);
57
+ const triangles = [];
58
+
59
+ if (!outerNode || outerNode.next === outerNode.prev) return triangles;
60
+
61
+ let invSize;
62
+ let maxX;
63
+ let maxY;
64
+ let minX;
65
+ let minY;
66
+ let x;
67
+ let y;
68
+
69
+ if (hasHoles) outerNode = eliminateHoles(positions, holeIndices, outerNode, dim, areas, plane);
70
+
71
+ // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
72
+ if (positions.length > 80 * dim) {
73
+ minX = maxX = positions[0];
74
+ minY = maxY = positions[1];
75
+
76
+ for (let i = dim; i < outerLen; i += dim) {
77
+ x = positions[i];
78
+ y = positions[i + 1];
79
+ if (x < minX) minX = x;
80
+ if (y < minY) minY = y;
81
+ if (x > maxX) maxX = x;
82
+ if (y > maxY) maxY = y;
83
+ }
84
+
85
+ // minX, minY and invSize are later used to transform coords into integers for z-order calculation
86
+ invSize = Math.max(maxX - minX, maxY - minY);
87
+ invSize = invSize !== 0 ? 32767 / invSize : 0;
88
+ }
89
+
90
+ earcutLinked(outerNode, triangles, dim, minX, minY, invSize, 0);
91
+
92
+ return triangles;
93
+ }
94
+
95
+ // create a circular doubly linked list from polygon points in the specified winding order
96
+ function linkedList(
97
+ data: NumericArray,
98
+ start: number,
99
+ end: number,
100
+ dim: number,
101
+ clockwise: boolean,
102
+ area: number | undefined,
103
+ plane: Plane2D
104
+ ): Vertex {
105
+ let i;
106
+ let last;
107
+ if (area === undefined) {
108
+ area = getPolygonSignedArea(data, {start, end, size: dim, plane});
109
+ }
110
+
111
+ let i0 = DimIndex[plane[0]];
112
+ let i1 = DimIndex[plane[1]];
113
+ // Note that the signed area calculation in math.gl
114
+ // has the opposite sign to that which was originally
115
+ // present in earcut, thus the `< 0` is reversed
116
+ if (clockwise === area < 0) {
117
+ for (i = start; i < end; i += dim) last = insertNode(i, data[i + i0], data[i + i1], last);
118
+ } else {
119
+ for (i = end - dim; i >= start; i -= dim)
120
+ last = insertNode(i, data[i + i0], data[i + i1], last);
121
+ }
122
+
123
+ if (last && equals(last, last.next)) {
124
+ removeNode(last);
125
+ last = last.next;
126
+ }
127
+
128
+ return last;
129
+ }
130
+
131
+ // eliminate colinear or duplicate points
132
+ function filterPoints(start, end?) {
133
+ if (!start) return start;
134
+ if (!end) end = start;
135
+
136
+ let p = start;
137
+ let again;
138
+ do {
139
+ again = false;
140
+
141
+ if (!p.steiner && (equals(p, p.next) || area(p.prev, p, p.next) === 0)) {
142
+ removeNode(p);
143
+ p = end = p.prev;
144
+ if (p === p.next) break;
145
+ again = true;
146
+ } else {
147
+ p = p.next;
148
+ }
149
+ } while (again || p !== end);
150
+
151
+ return end;
152
+ }
153
+
154
+ // main ear slicing loop which triangulates a polygon (given as a linked list)
155
+ function earcutLinked(ear, triangles, dim, minX, minY, invSize, pass?) {
156
+ if (!ear) return;
157
+
158
+ // interlink polygon nodes in z-order
159
+ if (!pass && invSize) indexCurve(ear, minX, minY, invSize);
160
+
161
+ let stop = ear;
162
+ let prev;
163
+ let next;
164
+
165
+ // iterate through ears, slicing them one by one
166
+ while (ear.prev !== ear.next) {
167
+ prev = ear.prev;
168
+ next = ear.next;
169
+
170
+ if (invSize ? isEarHashed(ear, minX, minY, invSize) : isEar(ear)) {
171
+ // cut off the triangle
172
+ triangles.push((prev.i / dim) | 0);
173
+ triangles.push((ear.i / dim) | 0);
174
+ triangles.push((next.i / dim) | 0);
175
+
176
+ removeNode(ear);
177
+
178
+ // skipping the next vertex leads to less sliver triangles
179
+ ear = next.next;
180
+ stop = next.next;
181
+
182
+ continue;
183
+ }
184
+
185
+ ear = next;
186
+
187
+ // if we looped through the whole remaining polygon and can't find any more ears
188
+ if (ear === stop) {
189
+ // try filtering points and slicing again
190
+ if (!pass) {
191
+ earcutLinked(filterPoints(ear), triangles, dim, minX, minY, invSize, 1);
192
+
193
+ // if this didn't work, try curing all small self-intersections locally
194
+ } else if (pass === 1) {
195
+ ear = cureLocalIntersections(filterPoints(ear), triangles, dim);
196
+ earcutLinked(ear, triangles, dim, minX, minY, invSize, 2);
197
+
198
+ // as a last resort, try splitting the remaining polygon into two
199
+ } else if (pass === 2) {
200
+ splitEarcut(ear, triangles, dim, minX, minY, invSize);
201
+ }
202
+
203
+ break;
204
+ }
205
+ }
206
+ }
207
+
208
+ // check whether a polygon node forms a valid ear with adjacent nodes
209
+ function isEar(ear) {
210
+ const a = ear.prev;
211
+ const b = ear;
212
+ const c = ear.next;
213
+
214
+ if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
215
+
216
+ // now make sure we don't have other points inside the potential ear
217
+ const ax = a.x;
218
+ const bx = b.x;
219
+ const cx = c.x;
220
+ const ay = a.y;
221
+ const by = b.y;
222
+ const cy = c.y;
223
+
224
+ // triangle bbox; min & max are calculated like this for speed
225
+ const x0 = ax < bx ? (ax < cx ? ax : cx) : bx < cx ? bx : cx;
226
+ const y0 = ay < by ? (ay < cy ? ay : cy) : by < cy ? by : cy;
227
+ const x1 = ax > bx ? (ax > cx ? ax : cx) : bx > cx ? bx : cx;
228
+ const y1 = ay > by ? (ay > cy ? ay : cy) : by > cy ? by : cy;
229
+
230
+ let p = c.next;
231
+ while (p !== a) {
232
+ if (
233
+ p.x >= x0 &&
234
+ p.x <= x1 &&
235
+ p.y >= y0 &&
236
+ p.y <= y1 &&
237
+ pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) &&
238
+ area(p.prev, p, p.next) >= 0
239
+ )
240
+ return false;
241
+ p = p.next;
242
+ }
243
+
244
+ return true;
245
+ }
246
+
247
+ function isEarHashed(ear, minX, minY, invSize) {
248
+ const a = ear.prev;
249
+ const b = ear;
250
+ const c = ear.next;
251
+
252
+ if (area(a, b, c) >= 0) return false; // reflex, can't be an ear
253
+
254
+ const ax = a.x;
255
+ const bx = b.x;
256
+ const cx = c.x;
257
+ const ay = a.y;
258
+ const by = b.y;
259
+ const cy = c.y;
260
+
261
+ // triangle bbox; min & max are calculated like this for speed
262
+ const x0 = ax < bx ? (ax < cx ? ax : cx) : bx < cx ? bx : cx;
263
+ const y0 = ay < by ? (ay < cy ? ay : cy) : by < cy ? by : cy;
264
+ const x1 = ax > bx ? (ax > cx ? ax : cx) : bx > cx ? bx : cx;
265
+ const y1 = ay > by ? (ay > cy ? ay : cy) : by > cy ? by : cy;
266
+
267
+ // z-order range for the current triangle bbox;
268
+ const minZ = zOrder(x0, y0, minX, minY, invSize);
269
+ const maxZ = zOrder(x1, y1, minX, minY, invSize);
270
+
271
+ let p = ear.prevZ;
272
+ let n = ear.nextZ;
273
+
274
+ // look for points inside the triangle in both directions
275
+ while (p && p.z >= minZ && n && n.z <= maxZ) {
276
+ if (
277
+ p.x >= x0 &&
278
+ p.x <= x1 &&
279
+ p.y >= y0 &&
280
+ p.y <= y1 &&
281
+ p !== a &&
282
+ p !== c &&
283
+ pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) &&
284
+ area(p.prev, p, p.next) >= 0
285
+ )
286
+ return false;
287
+ p = p.prevZ;
288
+
289
+ if (
290
+ n.x >= x0 &&
291
+ n.x <= x1 &&
292
+ n.y >= y0 &&
293
+ n.y <= y1 &&
294
+ n !== a &&
295
+ n !== c &&
296
+ pointInTriangle(ax, ay, bx, by, cx, cy, n.x, n.y) &&
297
+ area(n.prev, n, n.next) >= 0
298
+ )
299
+ return false;
300
+ n = n.nextZ;
301
+ }
302
+
303
+ // look for remaining points in decreasing z-order
304
+ while (p && p.z >= minZ) {
305
+ if (
306
+ p.x >= x0 &&
307
+ p.x <= x1 &&
308
+ p.y >= y0 &&
309
+ p.y <= y1 &&
310
+ p !== a &&
311
+ p !== c &&
312
+ pointInTriangle(ax, ay, bx, by, cx, cy, p.x, p.y) &&
313
+ area(p.prev, p, p.next) >= 0
314
+ )
315
+ return false;
316
+ p = p.prevZ;
317
+ }
318
+
319
+ // look for remaining points in increasing z-order
320
+ while (n && n.z <= maxZ) {
321
+ if (
322
+ n.x >= x0 &&
323
+ n.x <= x1 &&
324
+ n.y >= y0 &&
325
+ n.y <= y1 &&
326
+ n !== a &&
327
+ n !== c &&
328
+ pointInTriangle(ax, ay, bx, by, cx, cy, n.x, n.y) &&
329
+ area(n.prev, n, n.next) >= 0
330
+ )
331
+ return false;
332
+ n = n.nextZ;
333
+ }
334
+
335
+ return true;
336
+ }
337
+
338
+ // go through all polygon nodes and cure small local self-intersections
339
+ function cureLocalIntersections(start, triangles, dim) {
340
+ let p = start;
341
+ do {
342
+ const a = p.prev;
343
+ const b = p.next.next;
344
+
345
+ if (
346
+ !equals(a, b) &&
347
+ intersects(a, p, p.next, b) &&
348
+ locallyInside(a, b) &&
349
+ locallyInside(b, a)
350
+ ) {
351
+ triangles.push((a.i / dim) | 0);
352
+ triangles.push((p.i / dim) | 0);
353
+ triangles.push((b.i / dim) | 0);
354
+
355
+ // remove two nodes involved
356
+ removeNode(p);
357
+ removeNode(p.next);
358
+
359
+ p = start = b;
360
+ }
361
+ p = p.next;
362
+ } while (p !== start);
363
+
364
+ return filterPoints(p);
365
+ }
366
+
367
+ // try splitting polygon into two and triangulate them independently
368
+ function splitEarcut(start, triangles, dim, minX, minY, invSize) {
369
+ // look for a valid diagonal that divides the polygon into two
370
+ let a = start;
371
+ do {
372
+ let b = a.next.next;
373
+ while (b !== a.prev) {
374
+ if (a.i !== b.i && isValidDiagonal(a, b)) {
375
+ // split the polygon in two by the diagonal
376
+ let c = splitPolygon(a, b);
377
+
378
+ // filter colinear points around the cuts
379
+ a = filterPoints(a, a.next);
380
+ c = filterPoints(c, c.next);
381
+
382
+ // run earcut on each half
383
+ earcutLinked(a, triangles, dim, minX, minY, invSize, 0);
384
+ earcutLinked(c, triangles, dim, minX, minY, invSize, 0);
385
+ return;
386
+ }
387
+ b = b.next;
388
+ }
389
+ a = a.next;
390
+ } while (a !== start);
391
+ }
392
+
393
+ // link every hole into the outer loop, producing a single-ring polygon without holes
394
+ function eliminateHoles(
395
+ data: NumericArray,
396
+ holeIndices: NumericArray,
397
+ outerNode: Vertex,
398
+ dim: number,
399
+ areas: NumericArray | undefined,
400
+ plane: Plane2D
401
+ ): Vertex {
402
+ const queue = [];
403
+ let i;
404
+ let len;
405
+ let start;
406
+ let end;
407
+ let list;
408
+
409
+ for (i = 0, len = holeIndices.length; i < len; i++) {
410
+ start = holeIndices[i] * dim;
411
+ end = i < len - 1 ? holeIndices[i + 1] * dim : data.length;
412
+ list = linkedList(data, start, end, dim, false, areas && areas[i + 1], plane);
413
+ if (list === list.next) list.steiner = true;
414
+ queue.push(getLeftmost(list));
415
+ }
416
+
417
+ queue.sort(compareX);
418
+
419
+ // process holes from left to right
420
+ for (i = 0; i < queue.length; i++) {
421
+ outerNode = eliminateHole(queue[i], outerNode);
422
+ }
423
+
424
+ return outerNode;
425
+ }
426
+
427
+ function compareX(a, b) {
428
+ return a.x - b.x;
429
+ }
430
+
431
+ // find a bridge between vertices that connects hole with an outer ring and and link it
432
+ function eliminateHole(hole, outerNode) {
433
+ const bridge = findHoleBridge(hole, outerNode);
434
+ if (!bridge) {
435
+ return outerNode;
436
+ }
437
+
438
+ const bridgeReverse = splitPolygon(bridge, hole);
439
+
440
+ // filter collinear points around the cuts
441
+ filterPoints(bridgeReverse, bridgeReverse.next);
442
+ return filterPoints(bridge, bridge.next);
443
+ }
444
+
445
+ // David Eberly's algorithm for finding a bridge between hole and outer polygon
446
+ function findHoleBridge(hole, outerNode) {
447
+ let p = outerNode;
448
+ const hx = hole.x;
449
+ const hy = hole.y;
450
+ let qx = -Infinity;
451
+ let m;
452
+
453
+ // find a segment intersected by a ray from the hole's leftmost point to the left;
454
+ // segment's endpoint with lesser x will be potential connection point
455
+ do {
456
+ if (hy <= p.y && hy >= p.next.y && p.next.y !== p.y) {
457
+ const x = p.x + ((hy - p.y) * (p.next.x - p.x)) / (p.next.y - p.y);
458
+ if (x <= hx && x > qx) {
459
+ qx = x;
460
+ m = p.x < p.next.x ? p : p.next;
461
+ if (x === hx) return m; // hole touches outer segment; pick leftmost endpoint
462
+ }
463
+ }
464
+ p = p.next;
465
+ } while (p !== outerNode);
466
+
467
+ if (!m) return null;
468
+
469
+ // look for points inside the triangle of hole point, segment intersection and endpoint;
470
+ // if there are no points found, we have a valid connection;
471
+ // otherwise choose the point of the minimum angle with the ray as connection point
472
+
473
+ const stop = m;
474
+ const mx = m.x;
475
+ const my = m.y;
476
+ let tanMin = Infinity;
477
+ let tan;
478
+
479
+ p = m;
480
+
481
+ do {
482
+ if (
483
+ hx >= p.x &&
484
+ p.x >= mx &&
485
+ hx !== p.x &&
486
+ pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y)
487
+ ) {
488
+ tan = Math.abs(hy - p.y) / (hx - p.x); // tangential
489
+
490
+ if (
491
+ locallyInside(p, hole) &&
492
+ (tan < tanMin ||
493
+ (tan === tanMin && (p.x > m.x || (p.x === m.x && sectorContainsSector(m, p)))))
494
+ ) {
495
+ m = p;
496
+ tanMin = tan;
497
+ }
498
+ }
499
+
500
+ p = p.next;
501
+ } while (p !== stop);
502
+
503
+ return m;
504
+ }
505
+
506
+ // whether sector in vertex m contains sector in vertex p in the same coordinates
507
+ function sectorContainsSector(m, p) {
508
+ return area(m.prev, m, p.prev) < 0 && area(p.next, m, m.next) < 0;
509
+ }
510
+
511
+ // interlink polygon nodes in z-order
512
+ function indexCurve(start, minX, minY, invSize) {
513
+ let p = start;
514
+ do {
515
+ if (p.z === 0) p.z = zOrder(p.x, p.y, minX, minY, invSize);
516
+ p.prevZ = p.prev;
517
+ p.nextZ = p.next;
518
+ p = p.next;
519
+ } while (p !== start);
520
+
521
+ p.prevZ.nextZ = null;
522
+ p.prevZ = null;
523
+
524
+ sortLinked(p);
525
+ }
526
+
527
+ // Simon Tatham's linked list merge sort algorithm
528
+ // http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
529
+ function sortLinked(list) {
530
+ let e;
531
+ let i;
532
+ let inSize = 1;
533
+ let numMerges;
534
+ let p;
535
+ let pSize;
536
+ let q;
537
+ let qSize;
538
+ let tail;
539
+
540
+ do {
541
+ p = list;
542
+ list = null;
543
+ tail = null;
544
+ numMerges = 0;
545
+
546
+ while (p) {
547
+ numMerges++;
548
+ q = p;
549
+ pSize = 0;
550
+ for (i = 0; i < inSize; i++) {
551
+ pSize++;
552
+ q = q.nextZ;
553
+ if (!q) break;
554
+ }
555
+ qSize = inSize;
556
+
557
+ while (pSize > 0 || (qSize > 0 && q)) {
558
+ if (pSize !== 0 && (qSize === 0 || !q || p.z <= q.z)) {
559
+ e = p;
560
+ p = p.nextZ;
561
+ pSize--;
562
+ } else {
563
+ e = q;
564
+ q = q.nextZ;
565
+ qSize--;
566
+ }
567
+
568
+ if (tail) tail.nextZ = e;
569
+ else list = e;
570
+
571
+ e.prevZ = tail;
572
+ tail = e;
573
+ }
574
+
575
+ p = q;
576
+ }
577
+
578
+ tail.nextZ = null;
579
+ inSize *= 2;
580
+ } while (numMerges > 1);
581
+
582
+ return list;
583
+ }
584
+
585
+ // z-order of a point given coords and inverse of the longer side of data bbox
586
+ function zOrder(x, y, minX, minY, invSize) {
587
+ // coords are transformed into non-negative 15-bit integer range
588
+ x = ((x - minX) * invSize) | 0;
589
+ y = ((y - minY) * invSize) | 0;
590
+
591
+ x = (x | (x << 8)) & 0x00ff00ff;
592
+ x = (x | (x << 4)) & 0x0f0f0f0f;
593
+ x = (x | (x << 2)) & 0x33333333;
594
+ x = (x | (x << 1)) & 0x55555555;
595
+
596
+ y = (y | (y << 8)) & 0x00ff00ff;
597
+ y = (y | (y << 4)) & 0x0f0f0f0f;
598
+ y = (y | (y << 2)) & 0x33333333;
599
+ y = (y | (y << 1)) & 0x55555555;
600
+
601
+ return x | (y << 1);
602
+ }
603
+
604
+ // find the leftmost node of a polygon ring
605
+ function getLeftmost(start) {
606
+ let p = start;
607
+ let leftmost = start;
608
+ do {
609
+ if (p.x < leftmost.x || (p.x === leftmost.x && p.y < leftmost.y)) leftmost = p;
610
+ p = p.next;
611
+ } while (p !== start);
612
+
613
+ return leftmost;
614
+ }
615
+
616
+ // check if a point lies within a convex triangle
617
+ function pointInTriangle(ax, ay, bx, by, cx, cy, px, py) {
618
+ return (
619
+ (cx - px) * (ay - py) >= (ax - px) * (cy - py) &&
620
+ (ax - px) * (by - py) >= (bx - px) * (ay - py) &&
621
+ (bx - px) * (cy - py) >= (cx - px) * (by - py)
622
+ );
623
+ }
624
+
625
+ // check if a diagonal between two polygon nodes is valid (lies in polygon interior)
626
+ function isValidDiagonal(a, b) {
627
+ return (
628
+ a.next.i !== b.i &&
629
+ a.prev.i !== b.i &&
630
+ !intersectsPolygon(a, b) && // dones't intersect other edges
631
+ ((locallyInside(a, b) &&
632
+ locallyInside(b, a) &&
633
+ middleInside(a, b) && // locally visible
634
+ (area(a.prev, a, b.prev) || area(a, b.prev, b))) || // does not create opposite-facing sectors
635
+ (equals(a, b) && area(a.prev, a, a.next) > 0 && area(b.prev, b, b.next) > 0))
636
+ ); // special zero-length case
637
+ }
638
+
639
+ // signed area of a triangle
640
+ function area(p, q, r) {
641
+ return (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
642
+ }
643
+
644
+ // check if two points are equal
645
+ function equals(p1, p2) {
646
+ return p1.x === p2.x && p1.y === p2.y;
647
+ }
648
+
649
+ // check if two segments intersect
650
+ function intersects(p1, q1, p2, q2) {
651
+ const o1 = sign(area(p1, q1, p2));
652
+ const o2 = sign(area(p1, q1, q2));
653
+ const o3 = sign(area(p2, q2, p1));
654
+ const o4 = sign(area(p2, q2, q1));
655
+
656
+ if (o1 !== o2 && o3 !== o4) return true; // general case
657
+
658
+ if (o1 === 0 && onSegment(p1, p2, q1)) return true; // p1, q1 and p2 are collinear and p2 lies on p1q1
659
+ if (o2 === 0 && onSegment(p1, q2, q1)) return true; // p1, q1 and q2 are collinear and q2 lies on p1q1
660
+ if (o3 === 0 && onSegment(p2, p1, q2)) return true; // p2, q2 and p1 are collinear and p1 lies on p2q2
661
+ if (o4 === 0 && onSegment(p2, q1, q2)) return true; // p2, q2 and q1 are collinear and q1 lies on p2q2
662
+
663
+ return false;
664
+ }
665
+
666
+ // for collinear points p, q, r, check if point q lies on segment pr
667
+ function onSegment(p, q, r) {
668
+ return (
669
+ q.x <= Math.max(p.x, r.x) &&
670
+ q.x >= Math.min(p.x, r.x) &&
671
+ q.y <= Math.max(p.y, r.y) &&
672
+ q.y >= Math.min(p.y, r.y)
673
+ );
674
+ }
675
+
676
+ function sign(num) {
677
+ return num > 0 ? 1 : num < 0 ? -1 : 0;
678
+ }
679
+
680
+ // check if a polygon diagonal intersects any polygon segments
681
+ function intersectsPolygon(a, b) {
682
+ let p = a;
683
+ do {
684
+ if (
685
+ p.i !== a.i &&
686
+ p.next.i !== a.i &&
687
+ p.i !== b.i &&
688
+ p.next.i !== b.i &&
689
+ intersects(p, p.next, a, b)
690
+ )
691
+ return true;
692
+ p = p.next;
693
+ } while (p !== a);
694
+
695
+ return false;
696
+ }
697
+
698
+ // check if a polygon diagonal is locally inside the polygon
699
+ function locallyInside(a, b) {
700
+ return area(a.prev, a, a.next) < 0
701
+ ? area(a, b, a.next) >= 0 && area(a, a.prev, b) >= 0
702
+ : area(a, b, a.prev) < 0 || area(a, a.next, b) < 0;
703
+ }
704
+
705
+ // check if the middle point of a polygon diagonal is inside the polygon
706
+ function middleInside(a, b) {
707
+ let p = a;
708
+ let inside = false;
709
+ const px = (a.x + b.x) / 2;
710
+ const py = (a.y + b.y) / 2;
711
+ do {
712
+ if (
713
+ p.y > py !== p.next.y > py &&
714
+ p.next.y !== p.y &&
715
+ px < ((p.next.x - p.x) * (py - p.y)) / (p.next.y - p.y) + p.x
716
+ )
717
+ inside = !inside;
718
+ p = p.next;
719
+ } while (p !== a);
720
+
721
+ return inside;
722
+ }
723
+
724
+ // link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
725
+ // if one belongs to the outer ring and another to a hole, it merges it into a single ring
726
+ function splitPolygon(a, b) {
727
+ const a2 = new Vertex(a.i, a.x, a.y);
728
+ const b2 = new Vertex(b.i, b.x, b.y);
729
+ const an = a.next;
730
+ const bp = b.prev;
731
+
732
+ a.next = b;
733
+ b.prev = a;
734
+
735
+ a2.next = an;
736
+ an.prev = a2;
737
+
738
+ b2.next = a2;
739
+ a2.prev = b2;
740
+
741
+ bp.next = b2;
742
+ b2.prev = bp;
743
+
744
+ return b2;
745
+ }
746
+
747
+ // create a node and optionally link it with previous one (in a circular doubly linked list)
748
+ function insertNode(i, x, y, last) {
749
+ const p = new Vertex(i, x, y);
750
+
751
+ if (!last) {
752
+ p.prev = p;
753
+ p.next = p;
754
+ } else {
755
+ p.next = last.next;
756
+ p.prev = last;
757
+ last.next.prev = p;
758
+ last.next = p;
759
+ }
760
+ return p;
761
+ }
762
+
763
+ function removeNode(p) {
764
+ p.next.prev = p.prev;
765
+ p.prev.next = p.next;
766
+
767
+ if (p.prevZ) p.prevZ.nextZ = p.nextZ;
768
+ if (p.nextZ) p.nextZ.prevZ = p.prevZ;
769
+ }
770
+
771
+ class Vertex {
772
+ // vertex index in coordinates array
773
+ i: number;
774
+
775
+ // vertex coordinates
776
+ x: number;
777
+ y: number;
778
+
779
+ // previous and next vertex nodes in a polygon ring
780
+ prev: Vertex = null;
781
+ next: Vertex = null;
782
+
783
+ // z-order curve value
784
+ z: number = 0;
785
+
786
+ // previous and next nodes in z-order
787
+ prevZ: Vertex = null;
788
+ nextZ: Vertex = null;
789
+
790
+ // indicates whether this is a steiner point
791
+ steiner: boolean = false;
792
+
793
+ constructor(i: number, x: number, y: number) {
794
+ this.i = i;
795
+ this.x = x;
796
+ this.y = y;
797
+ }
798
+ }