@math.gl/geometry 5.0.0-alpha.9 → 5.0.0-beta.10

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 (457) hide show
  1. package/LICENSE +97 -49
  2. package/README.md +77 -1
  3. package/THIRD-PARTY-NOTICES.md +18 -0
  4. package/dist/color/color-lut-types.d.ts +16 -0
  5. package/dist/color/color-lut-types.d.ts.map +1 -0
  6. package/dist/color/color-lut-types.js +5 -0
  7. package/dist/color/color-lut-types.js.map +1 -0
  8. package/dist/color/index.cjs +132 -0
  9. package/dist/color/index.cjs.map +6 -0
  10. package/dist/color/index.d.ts +4 -0
  11. package/dist/color/index.d.ts.map +1 -0
  12. package/dist/color/index.js +5 -0
  13. package/dist/color/index.js.map +1 -0
  14. package/dist/color/sample-lut.d.ts +13 -0
  15. package/dist/color/sample-lut.d.ts.map +1 -0
  16. package/dist/color/sample-lut.js +132 -0
  17. package/dist/color/sample-lut.js.map +1 -0
  18. package/dist/culling/bounds.d.ts +20 -0
  19. package/dist/culling/bounds.d.ts.map +1 -0
  20. package/dist/culling/bounds.js +121 -0
  21. package/dist/culling/bounds.js.map +1 -0
  22. package/dist/culling/constants.d.ts +3 -0
  23. package/dist/culling/constants.d.ts.map +1 -0
  24. package/dist/culling/constants.js +5 -0
  25. package/dist/culling/constants.js.map +1 -0
  26. package/dist/culling/frustum-planes.d.ts +5 -0
  27. package/dist/culling/frustum-planes.d.ts.map +1 -0
  28. package/dist/culling/frustum-planes.js +37 -0
  29. package/dist/culling/frustum-planes.js.map +1 -0
  30. package/dist/culling/geometric-queries.d.ts +34 -0
  31. package/dist/culling/geometric-queries.d.ts.map +1 -0
  32. package/dist/culling/geometric-queries.js +208 -0
  33. package/dist/culling/geometric-queries.js.map +1 -0
  34. package/dist/culling/index.cjs +2560 -0
  35. package/dist/culling/index.cjs.map +6 -0
  36. package/dist/culling/index.d.ts +37 -0
  37. package/dist/culling/index.d.ts.map +1 -0
  38. package/dist/culling/index.js +29 -0
  39. package/dist/culling/index.js.map +1 -0
  40. package/dist/culling/lib/algorithms/bounding-box-from-points.d.ts +15 -0
  41. package/dist/culling/lib/algorithms/bounding-box-from-points.d.ts.map +1 -0
  42. package/dist/culling/lib/algorithms/bounding-box-from-points.js +137 -0
  43. package/dist/culling/lib/algorithms/bounding-box-from-points.js.map +1 -0
  44. package/dist/culling/lib/algorithms/bounding-sphere-from-points.d.ts +14 -0
  45. package/dist/culling/lib/algorithms/bounding-sphere-from-points.d.ts.map +1 -0
  46. package/dist/culling/lib/algorithms/bounding-sphere-from-points.js +146 -0
  47. package/dist/culling/lib/algorithms/bounding-sphere-from-points.js.map +1 -0
  48. package/dist/culling/lib/algorithms/compute-eigen-decomposition.d.ts +37 -0
  49. package/dist/culling/lib/algorithms/compute-eigen-decomposition.d.ts.map +1 -0
  50. package/dist/culling/lib/algorithms/compute-eigen-decomposition.js +137 -0
  51. package/dist/culling/lib/algorithms/compute-eigen-decomposition.js.map +1 -0
  52. package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.d.ts +7 -0
  53. package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.d.ts.map +1 -0
  54. package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.js +26 -0
  55. package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.js.map +1 -0
  56. package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +56 -0
  57. package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.d.ts.map +1 -0
  58. package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.js +133 -0
  59. package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.js.map +1 -0
  60. package/dist/culling/lib/bounding-volumes/bounding-sphere.d.ts +40 -0
  61. package/dist/culling/lib/bounding-volumes/bounding-sphere.d.ts.map +1 -0
  62. package/dist/culling/lib/bounding-volumes/bounding-sphere.js +123 -0
  63. package/dist/culling/lib/bounding-volumes/bounding-sphere.js.map +1 -0
  64. package/dist/culling/lib/bounding-volumes/bounding-volume.d.ts +30 -0
  65. package/dist/culling/lib/bounding-volumes/bounding-volume.d.ts.map +1 -0
  66. package/dist/culling/lib/bounding-volumes/bounding-volume.js +7 -0
  67. package/dist/culling/lib/bounding-volumes/bounding-volume.js.map +1 -0
  68. package/dist/culling/lib/bounding-volumes/oriented-bounding-box.d.ts +62 -0
  69. package/dist/culling/lib/bounding-volumes/oriented-bounding-box.d.ts.map +1 -0
  70. package/dist/culling/lib/bounding-volumes/oriented-bounding-box.js +230 -0
  71. package/dist/culling/lib/bounding-volumes/oriented-bounding-box.js.map +1 -0
  72. package/dist/culling/lib/culling-volume.d.ts +49 -0
  73. package/dist/culling/lib/culling-volume.d.ts.map +1 -0
  74. package/dist/culling/lib/culling-volume.js +133 -0
  75. package/dist/culling/lib/culling-volume.js.map +1 -0
  76. package/dist/culling/lib/perspective-frustum.d.ts +153 -0
  77. package/dist/culling/lib/perspective-frustum.d.ts.map +1 -0
  78. package/dist/culling/lib/perspective-frustum.js +191 -0
  79. package/dist/culling/lib/perspective-frustum.js.map +1 -0
  80. package/dist/culling/lib/perspective-off-center-frustum.d.ts +158 -0
  81. package/dist/culling/lib/perspective-off-center-frustum.d.ts.map +1 -0
  82. package/dist/culling/lib/perspective-off-center-frustum.js +276 -0
  83. package/dist/culling/lib/perspective-off-center-frustum.js.map +1 -0
  84. package/dist/culling/lib/plane.d.ts +35 -0
  85. package/dist/culling/lib/plane.d.ts.map +1 -0
  86. package/dist/culling/lib/plane.js +90 -0
  87. package/dist/culling/lib/plane.js.map +1 -0
  88. package/dist/culling/lib/ray.d.ts +18 -0
  89. package/dist/culling/lib/ray.d.ts.map +1 -0
  90. package/dist/culling/lib/ray.js +51 -0
  91. package/dist/culling/lib/ray.js.map +1 -0
  92. package/dist/culling/lib/shapes/box-shape.d.ts +19 -0
  93. package/dist/culling/lib/shapes/box-shape.d.ts.map +1 -0
  94. package/dist/culling/lib/shapes/box-shape.js +69 -0
  95. package/dist/culling/lib/shapes/box-shape.js.map +1 -0
  96. package/dist/culling/lib/shapes/capsule-shape.d.ts +23 -0
  97. package/dist/culling/lib/shapes/capsule-shape.d.ts.map +1 -0
  98. package/dist/culling/lib/shapes/capsule-shape.js +151 -0
  99. package/dist/culling/lib/shapes/capsule-shape.js.map +1 -0
  100. package/dist/culling/lib/shapes/cylinder-shape.d.ts +22 -0
  101. package/dist/culling/lib/shapes/cylinder-shape.d.ts.map +1 -0
  102. package/dist/culling/lib/shapes/cylinder-shape.js +99 -0
  103. package/dist/culling/lib/shapes/cylinder-shape.js.map +1 -0
  104. package/dist/culling/lib/shapes/implicit-shape.d.ts +56 -0
  105. package/dist/culling/lib/shapes/implicit-shape.d.ts.map +1 -0
  106. package/dist/culling/lib/shapes/implicit-shape.js +142 -0
  107. package/dist/culling/lib/shapes/implicit-shape.js.map +1 -0
  108. package/dist/culling/lib/shapes/plane-shape.d.ts +27 -0
  109. package/dist/culling/lib/shapes/plane-shape.d.ts.map +1 -0
  110. package/dist/culling/lib/shapes/plane-shape.js +71 -0
  111. package/dist/culling/lib/shapes/plane-shape.js.map +1 -0
  112. package/dist/culling/lib/shapes/sphere-shape.d.ts +18 -0
  113. package/dist/culling/lib/shapes/sphere-shape.d.ts.map +1 -0
  114. package/dist/culling/lib/shapes/sphere-shape.js +43 -0
  115. package/dist/culling/lib/shapes/sphere-shape.js.map +1 -0
  116. package/dist/culling/oriented-box-2d.d.ts +22 -0
  117. package/dist/culling/oriented-box-2d.d.ts.map +1 -0
  118. package/dist/culling/oriented-box-2d.js +86 -0
  119. package/dist/culling/oriented-box-2d.js.map +1 -0
  120. package/dist/culling/queries.d.ts +44 -0
  121. package/dist/culling/queries.d.ts.map +1 -0
  122. package/dist/culling/queries.js +259 -0
  123. package/dist/culling/queries.js.map +1 -0
  124. package/dist/culling/screen-ray.d.ts +22 -0
  125. package/dist/culling/screen-ray.d.ts.map +1 -0
  126. package/dist/culling/screen-ray.js +55 -0
  127. package/dist/culling/screen-ray.js.map +1 -0
  128. package/dist/curves/adaptive-sampling.d.ts +16 -0
  129. package/dist/curves/adaptive-sampling.d.ts.map +1 -0
  130. package/dist/curves/adaptive-sampling.js +59 -0
  131. package/dist/curves/adaptive-sampling.js.map +1 -0
  132. package/dist/curves/bezier-curves.d.ts +18 -0
  133. package/dist/curves/bezier-curves.d.ts.map +1 -0
  134. package/dist/curves/bezier-curves.js +54 -0
  135. package/dist/curves/bezier-curves.js.map +1 -0
  136. package/dist/curves/catmull-rom-curve.d.ts +15 -0
  137. package/dist/curves/catmull-rom-curve.d.ts.map +1 -0
  138. package/dist/curves/catmull-rom-curve.js +77 -0
  139. package/dist/curves/catmull-rom-curve.js.map +1 -0
  140. package/dist/curves/curve-arc-length.d.ts +17 -0
  141. package/dist/curves/curve-arc-length.d.ts.map +1 -0
  142. package/dist/curves/curve-arc-length.js +58 -0
  143. package/dist/curves/curve-arc-length.js.map +1 -0
  144. package/dist/curves/curve.d.ts +23 -0
  145. package/dist/curves/curve.d.ts.map +1 -0
  146. package/dist/curves/curve.js +77 -0
  147. package/dist/curves/curve.js.map +1 -0
  148. package/dist/curves/hermite-curve.d.ts +9 -0
  149. package/dist/curves/hermite-curve.d.ts.map +1 -0
  150. package/dist/curves/hermite-curve.js +24 -0
  151. package/dist/curves/hermite-curve.js.map +1 -0
  152. package/dist/curves/index.d.ts +12 -0
  153. package/dist/curves/index.d.ts.map +1 -0
  154. package/dist/curves/index.js +11 -0
  155. package/dist/curves/index.js.map +1 -0
  156. package/dist/curves/parallel-transport.d.ts +12 -0
  157. package/dist/curves/parallel-transport.d.ts.map +1 -0
  158. package/dist/curves/parallel-transport.js +83 -0
  159. package/dist/curves/parallel-transport.js.map +1 -0
  160. package/dist/geometries/box-geometry.js +1 -1
  161. package/dist/geometries/box-geometry.js.map +1 -1
  162. package/dist/geometries/capsule-geometry.js +1 -1
  163. package/dist/geometries/capsule-geometry.js.map +1 -1
  164. package/dist/geometries/cylinder-geometry.js +1 -1
  165. package/dist/geometries/cylinder-geometry.js.map +1 -1
  166. package/dist/geometries/geometry-helpers.js +1 -1
  167. package/dist/geometries/geometry-helpers.js.map +1 -1
  168. package/dist/geometries/ico-sphere-geometry.js +1 -1
  169. package/dist/geometries/ico-sphere-geometry.js.map +1 -1
  170. package/dist/geometries/plane-geometry.js +1 -1
  171. package/dist/geometries/plane-geometry.js.map +1 -1
  172. package/dist/geometries/primitive-mesh-builder.d.ts +24 -0
  173. package/dist/geometries/primitive-mesh-builder.d.ts.map +1 -0
  174. package/dist/geometries/primitive-mesh-builder.js +74 -0
  175. package/dist/geometries/primitive-mesh-builder.js.map +1 -0
  176. package/dist/geometries/prism-geometry.d.ts +13 -0
  177. package/dist/geometries/prism-geometry.d.ts.map +1 -0
  178. package/dist/geometries/prism-geometry.js +34 -0
  179. package/dist/geometries/prism-geometry.js.map +1 -0
  180. package/dist/geometries/ring-geometry.d.ts +15 -0
  181. package/dist/geometries/ring-geometry.d.ts.map +1 -0
  182. package/dist/geometries/ring-geometry.js +64 -0
  183. package/dist/geometries/ring-geometry.js.map +1 -0
  184. package/dist/geometries/sphere-geometry.js +1 -1
  185. package/dist/geometries/sphere-geometry.js.map +1 -1
  186. package/dist/geometries/surface-of-revolution.js +1 -1
  187. package/dist/geometries/surface-of-revolution.js.map +1 -1
  188. package/dist/geometries/truncated-cone-geometry.js +1 -1
  189. package/dist/geometries/truncated-cone-geometry.js.map +1 -1
  190. package/dist/index.cjs +6993 -37
  191. package/dist/index.cjs.map +3 -3
  192. package/dist/index.d.ts +35 -0
  193. package/dist/index.d.ts.map +1 -1
  194. package/dist/index.js +20 -1
  195. package/dist/index.js.map +1 -1
  196. package/dist/lib/geometry-utils.js +1 -1
  197. package/dist/lib/geometry-utils.js.map +1 -1
  198. package/dist/lib/geometry.d.ts +1 -1
  199. package/dist/lib/geometry.d.ts.map +1 -1
  200. package/dist/lib/geometry.js +1 -1
  201. package/dist/lib/geometry.js.map +1 -1
  202. package/dist/lib/hinge-tree.d.ts +33 -0
  203. package/dist/lib/hinge-tree.d.ts.map +1 -0
  204. package/dist/lib/hinge-tree.js +144 -0
  205. package/dist/lib/hinge-tree.js.map +1 -0
  206. package/dist/lighting/index.cjs +175 -0
  207. package/dist/lighting/index.cjs.map +6 -0
  208. package/dist/lighting/index.d.ts +4 -0
  209. package/dist/lighting/index.d.ts.map +1 -0
  210. package/dist/lighting/index.js +5 -0
  211. package/dist/lighting/index.js.map +1 -0
  212. package/dist/lighting/lighting-types.d.ts +16 -0
  213. package/dist/lighting/lighting-types.d.ts.map +1 -0
  214. package/dist/lighting/lighting-types.js +5 -0
  215. package/dist/lighting/lighting-types.js.map +1 -0
  216. package/dist/lighting/photometry.d.ts +36 -0
  217. package/dist/lighting/photometry.d.ts.map +1 -0
  218. package/dist/lighting/photometry.js +181 -0
  219. package/dist/lighting/photometry.js.map +1 -0
  220. package/dist/parametric/lathe-geometry.d.ts +14 -0
  221. package/dist/parametric/lathe-geometry.d.ts.map +1 -0
  222. package/dist/parametric/lathe-geometry.js +48 -0
  223. package/dist/parametric/lathe-geometry.js.map +1 -0
  224. package/dist/parametric/parametric-geometry.d.ts +20 -0
  225. package/dist/parametric/parametric-geometry.d.ts.map +1 -0
  226. package/dist/parametric/parametric-geometry.js +88 -0
  227. package/dist/parametric/parametric-geometry.js.map +1 -0
  228. package/dist/parametric/torus-geometry.d.ts +19 -0
  229. package/dist/parametric/torus-geometry.d.ts.map +1 -0
  230. package/dist/parametric/torus-geometry.js +89 -0
  231. package/dist/parametric/torus-geometry.js.map +1 -0
  232. package/dist/parametric.d.ts +7 -0
  233. package/dist/parametric.d.ts.map +1 -0
  234. package/dist/parametric.js +7 -0
  235. package/dist/parametric.js.map +1 -0
  236. package/dist/polygon/clip-triangle-mesh.d.ts +41 -0
  237. package/dist/polygon/clip-triangle-mesh.d.ts.map +1 -0
  238. package/dist/polygon/clip-triangle-mesh.js +155 -0
  239. package/dist/polygon/clip-triangle-mesh.js.map +1 -0
  240. package/dist/polygon/cut-by-grid.d.ts +24 -0
  241. package/dist/polygon/cut-by-grid.d.ts.map +1 -0
  242. package/dist/polygon/cut-by-grid.js +221 -0
  243. package/dist/polygon/cut-by-grid.js.map +1 -0
  244. package/dist/polygon/cut-by-mercator-bounds.d.ts +17 -0
  245. package/dist/polygon/cut-by-mercator-bounds.d.ts.map +1 -0
  246. package/dist/polygon/cut-by-mercator-bounds.js +149 -0
  247. package/dist/polygon/cut-by-mercator-bounds.js.map +1 -0
  248. package/dist/polygon/earcut.d.ts +13 -0
  249. package/dist/polygon/earcut.d.ts.map +1 -0
  250. package/dist/polygon/earcut.js +693 -0
  251. package/dist/polygon/earcut.js.map +1 -0
  252. package/dist/polygon/index.d.ts +16 -0
  253. package/dist/polygon/index.d.ts.map +1 -0
  254. package/dist/polygon/index.js +14 -0
  255. package/dist/polygon/index.js.map +1 -0
  256. package/dist/polygon/lineclip.d.ts +31 -0
  257. package/dist/polygon/lineclip.d.ts.map +1 -0
  258. package/dist/polygon/lineclip.js +184 -0
  259. package/dist/polygon/lineclip.js.map +1 -0
  260. package/dist/polygon/pole-of-inaccessibility.d.ts +21 -0
  261. package/dist/polygon/pole-of-inaccessibility.d.ts.map +1 -0
  262. package/dist/polygon/pole-of-inaccessibility.js +285 -0
  263. package/dist/polygon/pole-of-inaccessibility.js.map +1 -0
  264. package/dist/polygon/polygon-utils.d.ts +103 -0
  265. package/dist/polygon/polygon-utils.d.ts.map +1 -0
  266. package/dist/polygon/polygon-utils.js +155 -0
  267. package/dist/polygon/polygon-utils.js.map +1 -0
  268. package/dist/polygon/polygon.d.ts +41 -0
  269. package/dist/polygon/polygon.d.ts.map +1 -0
  270. package/dist/polygon/polygon.js +76 -0
  271. package/dist/polygon/polygon.js.map +1 -0
  272. package/dist/polygon/subdivide-polyline.d.ts +56 -0
  273. package/dist/polygon/subdivide-polyline.d.ts.map +1 -0
  274. package/dist/polygon/subdivide-polyline.js +141 -0
  275. package/dist/polygon/subdivide-polyline.js.map +1 -0
  276. package/dist/polygon/subdivide-triangle-mesh.d.ts +78 -0
  277. package/dist/polygon/subdivide-triangle-mesh.d.ts.map +1 -0
  278. package/dist/polygon/subdivide-triangle-mesh.js +238 -0
  279. package/dist/polygon/subdivide-triangle-mesh.js.map +1 -0
  280. package/dist/polygon/utils.d.ts +5 -0
  281. package/dist/polygon/utils.d.ts.map +1 -0
  282. package/dist/polygon/utils.js +38 -0
  283. package/dist/polygon/utils.js.map +1 -0
  284. package/dist/spatial-index/box-index.d.ts +86 -0
  285. package/dist/spatial-index/box-index.d.ts.map +1 -0
  286. package/dist/spatial-index/box-index.js +335 -0
  287. package/dist/spatial-index/box-index.js.map +1 -0
  288. package/dist/spatial-index/index.d.ts +7 -0
  289. package/dist/spatial-index/index.d.ts.map +1 -0
  290. package/dist/spatial-index/index.js +6 -0
  291. package/dist/spatial-index/index.js.map +1 -0
  292. package/dist/spatial-index/triangle-bvh.d.ts +74 -0
  293. package/dist/spatial-index/triangle-bvh.d.ts.map +1 -0
  294. package/dist/spatial-index/triangle-bvh.js +221 -0
  295. package/dist/spatial-index/triangle-bvh.js.map +1 -0
  296. package/dist/utils/attribute-compression.d.ts +50 -0
  297. package/dist/utils/attribute-compression.d.ts.map +1 -0
  298. package/dist/utils/attribute-compression.js +186 -0
  299. package/dist/utils/attribute-compression.js.map +1 -0
  300. package/dist/utils/attribute-iterator.d.ts +8 -0
  301. package/dist/utils/attribute-iterator.d.ts.map +1 -0
  302. package/dist/utils/attribute-iterator.js +25 -0
  303. package/dist/utils/attribute-iterator.js.map +1 -0
  304. package/dist/utils/compose-attribute-provenance.d.ts +13 -0
  305. package/dist/utils/compose-attribute-provenance.d.ts.map +1 -0
  306. package/dist/utils/compose-attribute-provenance.js +63 -0
  307. package/dist/utils/compose-attribute-provenance.js.map +1 -0
  308. package/dist/utils/compute-vertex-normals.d.ts +4 -0
  309. package/dist/utils/compute-vertex-normals.d.ts.map +1 -0
  310. package/dist/utils/compute-vertex-normals.js +54 -0
  311. package/dist/utils/compute-vertex-normals.js.map +1 -0
  312. package/dist/utils/constants.d.ts +52 -0
  313. package/dist/utils/constants.d.ts.map +1 -0
  314. package/dist/utils/constants.js +39 -0
  315. package/dist/utils/constants.js.map +1 -0
  316. package/dist/utils/coordinates.d.ts +3 -0
  317. package/dist/utils/coordinates.d.ts.map +1 -0
  318. package/dist/utils/coordinates.js +8 -0
  319. package/dist/utils/coordinates.js.map +1 -0
  320. package/dist/utils/geometry.d.ts +20 -0
  321. package/dist/utils/geometry.d.ts.map +1 -0
  322. package/dist/utils/geometry.js +31 -0
  323. package/dist/utils/geometry.js.map +1 -0
  324. package/dist/utils/gl-type.d.ts +17 -0
  325. package/dist/utils/gl-type.d.ts.map +1 -0
  326. package/dist/utils/gl-type.js +69 -0
  327. package/dist/utils/gl-type.js.map +1 -0
  328. package/dist/utils/index.d.ts +18 -0
  329. package/dist/utils/index.d.ts.map +1 -0
  330. package/dist/utils/index.js +16 -0
  331. package/dist/utils/index.js.map +1 -0
  332. package/dist/utils/interpolate-packed-attributes.d.ts +43 -0
  333. package/dist/utils/interpolate-packed-attributes.d.ts.map +1 -0
  334. package/dist/utils/interpolate-packed-attributes.js +192 -0
  335. package/dist/utils/interpolate-packed-attributes.js.map +1 -0
  336. package/dist/utils/mesh-processing.d.ts +18 -0
  337. package/dist/utils/mesh-processing.d.ts.map +1 -0
  338. package/dist/utils/mesh-processing.js +300 -0
  339. package/dist/utils/mesh-processing.js.map +1 -0
  340. package/dist/utils/primitive-iterator.d.ts +21 -0
  341. package/dist/utils/primitive-iterator.d.ts.map +1 -0
  342. package/dist/utils/primitive-iterator.js +117 -0
  343. package/dist/utils/primitive-iterator.js.map +1 -0
  344. package/dist/utils/project-bounds.d.ts +44 -0
  345. package/dist/utils/project-bounds.d.ts.map +1 -0
  346. package/dist/utils/project-bounds.js +93 -0
  347. package/dist/utils/project-bounds.js.map +1 -0
  348. package/dist/utils/project-texture-coordinates.d.ts +19 -0
  349. package/dist/utils/project-texture-coordinates.d.ts.map +1 -0
  350. package/dist/utils/project-texture-coordinates.js +49 -0
  351. package/dist/utils/project-texture-coordinates.js.map +1 -0
  352. package/dist/utils/qualify-surface-transition.d.ts +21 -0
  353. package/dist/utils/qualify-surface-transition.d.ts.map +1 -0
  354. package/dist/utils/qualify-surface-transition.js +140 -0
  355. package/dist/utils/qualify-surface-transition.js.map +1 -0
  356. package/dist/utils/rgb565.d.ts +5 -0
  357. package/dist/utils/rgb565.d.ts.map +1 -0
  358. package/dist/utils/rgb565.js +24 -0
  359. package/dist/utils/rgb565.js.map +1 -0
  360. package/dist/utils/typed-array-utils.d.ts +3 -0
  361. package/dist/utils/typed-array-utils.d.ts.map +1 -0
  362. package/dist/utils/typed-array-utils.js +16 -0
  363. package/dist/utils/typed-array-utils.js.map +1 -0
  364. package/package.json +24 -4
  365. package/src/color/color-lut-types.ts +19 -0
  366. package/src/color/index.ts +6 -0
  367. package/src/color/sample-lut.ts +175 -0
  368. package/src/culling/bounds.ts +152 -0
  369. package/src/culling/constants.ts +6 -0
  370. package/src/culling/frustum-planes.ts +41 -0
  371. package/src/culling/geometric-queries.ts +299 -0
  372. package/src/culling/index.ts +93 -0
  373. package/src/culling/lib/algorithms/bounding-box-from-points.ts +174 -0
  374. package/src/culling/lib/algorithms/bounding-sphere-from-points.ts +175 -0
  375. package/src/culling/lib/algorithms/compute-eigen-decomposition.ts +171 -0
  376. package/src/culling/lib/algorithms/oriented-bounding-box-quaternion.ts +32 -0
  377. package/src/culling/lib/bounding-volumes/axis-aligned-bounding-box.ts +171 -0
  378. package/src/culling/lib/bounding-volumes/bounding-sphere.ts +154 -0
  379. package/src/culling/lib/bounding-volumes/bounding-volume.ts +38 -0
  380. package/src/culling/lib/bounding-volumes/oriented-bounding-box.ts +320 -0
  381. package/src/culling/lib/culling-volume.ts +171 -0
  382. package/src/culling/lib/perspective-frustum.ts +288 -0
  383. package/src/culling/lib/perspective-off-center-frustum.ts +381 -0
  384. package/src/culling/lib/plane.ts +118 -0
  385. package/src/culling/lib/ray.ts +54 -0
  386. package/src/culling/lib/shapes/box-shape.ts +78 -0
  387. package/src/culling/lib/shapes/capsule-shape.ts +232 -0
  388. package/src/culling/lib/shapes/cylinder-shape.ts +133 -0
  389. package/src/culling/lib/shapes/implicit-shape.ts +209 -0
  390. package/src/culling/lib/shapes/plane-shape.ts +78 -0
  391. package/src/culling/lib/shapes/sphere-shape.ts +60 -0
  392. package/src/culling/oriented-box-2d.ts +165 -0
  393. package/src/culling/queries.ts +332 -0
  394. package/src/culling/screen-ray.ts +75 -0
  395. package/src/curves/adaptive-sampling.ts +85 -0
  396. package/src/curves/bezier-curves.ts +62 -0
  397. package/src/curves/catmull-rom-curve.ts +89 -0
  398. package/src/curves/curve-arc-length.ts +70 -0
  399. package/src/curves/curve.ts +96 -0
  400. package/src/curves/hermite-curve.ts +34 -0
  401. package/src/curves/index.ts +19 -0
  402. package/src/curves/parallel-transport.ts +104 -0
  403. package/src/geometries/box-geometry.ts +1 -1
  404. package/src/geometries/capsule-geometry.ts +1 -1
  405. package/src/geometries/cylinder-geometry.ts +1 -1
  406. package/src/geometries/geometry-helpers.ts +1 -1
  407. package/src/geometries/ico-sphere-geometry.ts +1 -1
  408. package/src/geometries/plane-geometry.ts +1 -1
  409. package/src/geometries/primitive-mesh-builder.ts +84 -0
  410. package/src/geometries/prism-geometry.ts +57 -0
  411. package/src/geometries/ring-geometry.ts +102 -0
  412. package/src/geometries/sphere-geometry.ts +1 -1
  413. package/src/geometries/surface-of-revolution.ts +1 -1
  414. package/src/geometries/truncated-cone-geometry.ts +1 -1
  415. package/src/index.ts +226 -1
  416. package/src/lib/geometry-utils.ts +2 -2
  417. package/src/lib/geometry.ts +2 -2
  418. package/src/lib/hinge-tree.ts +170 -0
  419. package/src/lighting/index.ts +10 -0
  420. package/src/lighting/lighting-types.ts +20 -0
  421. package/src/lighting/photometry.ts +264 -0
  422. package/src/parametric/lathe-geometry.ts +70 -0
  423. package/src/parametric/parametric-geometry.ts +118 -0
  424. package/src/parametric/torus-geometry.ts +118 -0
  425. package/src/parametric.ts +9 -0
  426. package/src/polygon/clip-triangle-mesh.ts +218 -0
  427. package/src/polygon/cut-by-grid.ts +313 -0
  428. package/src/polygon/cut-by-mercator-bounds.ts +204 -0
  429. package/src/polygon/earcut.ts +859 -0
  430. package/src/polygon/index.ts +48 -0
  431. package/src/polygon/lineclip.ts +218 -0
  432. package/src/polygon/pole-of-inaccessibility.ts +330 -0
  433. package/src/polygon/polygon-utils.ts +273 -0
  434. package/src/polygon/polygon.ts +105 -0
  435. package/src/polygon/subdivide-polyline.ts +223 -0
  436. package/src/polygon/subdivide-triangle-mesh.ts +342 -0
  437. package/src/polygon/utils.ts +50 -0
  438. package/src/spatial-index/box-index.ts +452 -0
  439. package/src/spatial-index/index.ts +20 -0
  440. package/src/spatial-index/triangle-bvh.ts +345 -0
  441. package/src/utils/attribute-compression.ts +234 -0
  442. package/src/utils/attribute-iterator.ts +28 -0
  443. package/src/utils/compose-attribute-provenance.ts +74 -0
  444. package/src/utils/compute-vertex-normals.ts +63 -0
  445. package/src/utils/constants.ts +42 -0
  446. package/src/utils/coordinates.ts +8 -0
  447. package/src/utils/geometry.ts +49 -0
  448. package/src/utils/gl-type.ts +87 -0
  449. package/src/utils/index.ts +55 -0
  450. package/src/utils/interpolate-packed-attributes.ts +284 -0
  451. package/src/utils/mesh-processing.ts +377 -0
  452. package/src/utils/primitive-iterator.ts +145 -0
  453. package/src/utils/project-bounds.ts +154 -0
  454. package/src/utils/project-texture-coordinates.ts +67 -0
  455. package/src/utils/qualify-surface-transition.ts +169 -0
  456. package/src/utils/rgb565.ts +26 -0
  457. package/src/utils/typed-array-utils.ts +16 -0
@@ -0,0 +1,48 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ export {Polygon} from './polygon';
6
+
7
+ export {
8
+ getPolygonSignedArea,
9
+ getPolygonWindingDirection,
10
+ forEachSegmentInPolygon,
11
+ modifyPolygonWindingDirection
12
+ } from './polygon-utils';
13
+ export type {PolygonWinding, WindingDirection} from './polygon-utils';
14
+
15
+ export {earcut} from './earcut';
16
+
17
+ export {clipPolygon, clipPolyline} from './lineclip';
18
+
19
+ export {
20
+ getPoleOfInaccessibility,
21
+ DEFAULT_POLE_SEARCH_SEGMENT_TESTS
22
+ } from './pole-of-inaccessibility';
23
+ export type {PoleSearchBudget} from './pole-of-inaccessibility';
24
+
25
+ export {cutPolygonByGrid, cutPolylineByGrid} from './cut-by-grid';
26
+
27
+ export {cutPolylineByMercatorBounds, cutPolygonByMercatorBounds} from './cut-by-mercator-bounds';
28
+
29
+ export {subdividePolyline} from './subdivide-polyline';
30
+ export type {
31
+ SubdividePolylineOptions,
32
+ SubdividedPolyline,
33
+ PolylineEdgeSampler
34
+ } from './subdivide-polyline';
35
+
36
+ export {subdivideTriangleMesh} from './subdivide-triangle-mesh';
37
+ export type {
38
+ TriangleMesh,
39
+ SubdivideTriangleMeshOptions,
40
+ SubdividedTriangleMesh
41
+ } from './subdivide-triangle-mesh';
42
+
43
+ export {clipTriangleMesh} from './clip-triangle-mesh';
44
+ export type {
45
+ MeshClipRegion,
46
+ ClipTriangleMeshOptions,
47
+ ClippedTriangleMesh
48
+ } from './clip-triangle-mesh';
@@ -0,0 +1,218 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT AND ISC
3
+ // SPDX-FileCopyrightText: Copyright (c) 2015, Mapbox
4
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
5
+ // SPDX-FileComment: Adapted from Mapbox lineclip for flat arrays and 3D positions. Original ISC notice retained below.
6
+
7
+ /*
8
+ Adapted from https://github.com/mapbox/lineclip to work with flat arrays
9
+ and 3d positions
10
+
11
+ ISC License
12
+
13
+ Copyright (c) 2015, Mapbox
14
+
15
+ Permission to use, copy, modify, and/or distribute this software for any purpose
16
+ with or without fee is hereby granted, provided that the above copyright notice
17
+ and this permission notice appear in all copies.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
20
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
21
+ FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
22
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
23
+ OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
24
+ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
25
+ THIS SOFTWARE.
26
+
27
+ */
28
+
29
+ /* eslint-disable max-statements, max-depth, complexity */
30
+
31
+ import {push, copy, getPointAtIndex} from './utils';
32
+ import type {NumericArray} from '@math.gl/core';
33
+
34
+ export type BoundingBox = [number, number, number, number];
35
+
36
+ /**
37
+ * Cohen-Sutherland line clipping algorithm, adapted to efficiently
38
+ * handle polylines rather than just segments
39
+ */
40
+ export function clipPolyline(
41
+ positions: Readonly<NumericArray>,
42
+ bbox: BoundingBox,
43
+ options?: {
44
+ size?: number;
45
+ startIndex?: number;
46
+ endIndex?: number;
47
+ }
48
+ ): number[][] {
49
+ const {size = 2, startIndex = 0, endIndex = positions.length} = options || {};
50
+ const numPoints = (endIndex - startIndex) / size;
51
+ const result: number[][] = [];
52
+ let part: number[] = [];
53
+ let a: number[];
54
+ let b: number[];
55
+ let codeA: number = -1;
56
+ let codeB: number;
57
+ let lastCode: number;
58
+
59
+ for (let i = 1; i < numPoints; i++) {
60
+ a = getPointAtIndex(positions, i - 1, size, startIndex, a);
61
+ b = getPointAtIndex(positions, i, size, startIndex, b);
62
+ if (codeA < 0) {
63
+ codeA = bitCode(a, bbox);
64
+ }
65
+ codeB = lastCode = bitCode(b, bbox);
66
+
67
+ // eslint-disable-next-line no-constant-condition
68
+ while (true) {
69
+ if (!(codeA | codeB)) {
70
+ // accept
71
+ push(part, a);
72
+
73
+ if (codeB !== lastCode) {
74
+ // segment went outside
75
+ push(part, b);
76
+
77
+ if (i < numPoints - 1) {
78
+ // start a new line
79
+ result.push(part);
80
+ part = [];
81
+ }
82
+ } else if (i === numPoints - 1) {
83
+ push(part, b);
84
+ }
85
+ break;
86
+ } else if (codeA & codeB) {
87
+ // trivial reject
88
+ break;
89
+ } else if (codeA) {
90
+ // a outside, intersect with clip edge
91
+ intersect(a, b, codeA, bbox, a);
92
+ codeA = bitCode(a, bbox);
93
+ } else {
94
+ // b outside
95
+ intersect(a, b, codeB, bbox, b);
96
+ codeB = bitCode(b, bbox);
97
+ }
98
+ }
99
+
100
+ codeA = lastCode;
101
+ }
102
+
103
+ if (part.length) result.push(part);
104
+
105
+ return result;
106
+ }
107
+
108
+ /**
109
+ * Sutherland-Hodgeman polygon clipping algorithm
110
+ * polygon must be closed (first vertex == last vertex)
111
+ */
112
+ export function clipPolygon(
113
+ positions: Readonly<NumericArray>,
114
+ bbox: BoundingBox,
115
+ options?: {
116
+ size?: number;
117
+ startIndex?: number;
118
+ endIndex?: number;
119
+ }
120
+ ): number[] {
121
+ const {size = 2, endIndex = positions.length} = options || {};
122
+ let {startIndex = 0} = options || {};
123
+ let numPoints = (endIndex - startIndex) / size;
124
+ let result: number[];
125
+ let p: number[];
126
+ let prev: number[];
127
+ let inside: boolean;
128
+ let prevInside: boolean;
129
+
130
+ // clip against each side of the clip rectangle
131
+ for (let edge = 1; edge <= 8; edge *= 2) {
132
+ result = [];
133
+ prev = getPointAtIndex(positions, numPoints - 1, size, startIndex, prev);
134
+ prevInside = !(bitCode(prev, bbox) & edge);
135
+
136
+ for (let i = 0; i < numPoints; i++) {
137
+ p = getPointAtIndex(positions, i, size, startIndex, p);
138
+ inside = !(bitCode(p, bbox) & edge);
139
+
140
+ // if segment goes through the clip window, add an intersection
141
+ if (inside !== prevInside) push(result, intersect(prev, p, edge, bbox));
142
+
143
+ if (inside) push(result, p); // add a point if it's inside
144
+
145
+ copy(prev, p);
146
+ prevInside = inside;
147
+ }
148
+
149
+ // close loop
150
+ positions = result;
151
+ startIndex = 0;
152
+ numPoints = result.length / size;
153
+
154
+ if (!numPoints) break;
155
+ }
156
+
157
+ return result;
158
+ }
159
+
160
+ /** intersect a segment against one of the 4 lines that make up the bbox */
161
+
162
+ export function intersect(
163
+ a: number[],
164
+ b: number[],
165
+ edge: number,
166
+ bbox: BoundingBox,
167
+ out: number[] = []
168
+ ): number[] {
169
+ let t;
170
+ // Forces out[snapI] to be on the bbox edge
171
+ // Interpolation introduces precision issue which may cause lineclip to be
172
+ // stuck in an infinite loop
173
+ let snap: number;
174
+ if (edge & 8) {
175
+ // top
176
+ t = (bbox[3] - a[1]) / (b[1] - a[1]);
177
+ snap = 3;
178
+ } else if (edge & 4) {
179
+ // bottom
180
+ t = (bbox[1] - a[1]) / (b[1] - a[1]);
181
+ snap = 1;
182
+ } else if (edge & 2) {
183
+ // right
184
+ t = (bbox[2] - a[0]) / (b[0] - a[0]);
185
+ snap = 2;
186
+ } else if (edge & 1) {
187
+ // left
188
+ t = (bbox[0] - a[0]) / (b[0] - a[0]);
189
+ snap = 0;
190
+ } else {
191
+ return null;
192
+ }
193
+ for (let i = 0; i < a.length; i++) {
194
+ out[i] = (snap & 1) === i ? bbox[snap] : t * (b[i] - a[i]) + a[i];
195
+ }
196
+ return out;
197
+ }
198
+
199
+ /**
200
+ * bit code reflects the point position relative to the bbox:
201
+ * left mid right
202
+ * top 1001 1000 1010
203
+ * mid 0001 0000 0010
204
+ * bottom 0101 0100 0110
205
+ */
206
+ export function bitCode(p: number[], bbox: BoundingBox): number {
207
+ let code = 0;
208
+
209
+ if (p[0] < bbox[0]) code |= 1;
210
+ // left
211
+ else if (p[0] > bbox[2]) code |= 2; // right
212
+
213
+ if (p[1] < bbox[1]) code |= 4;
214
+ // bottom
215
+ else if (p[1] > bbox[3]) code |= 8; // top
216
+
217
+ return code;
218
+ }
@@ -0,0 +1,330 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ // The pole of inaccessibility of a polygon is the interior point farthest from its outline, for
6
+ // example the position of a point label. This file finds that point with the grid refinement of
7
+ // Garcia-Castellanos and Lombardo, "Poles of inaccessibility: a calculation algorithm for the
8
+ // remotest places on Earth" (Scottish Geographical Journal 123(3), 2007), in its best-first form.
9
+ // A priority queue holds the candidate squares, ordered by the largest distance that a point in
10
+ // each square can have. The search splits a square into four only if the square can improve the
11
+ // best point by more than the precision.
12
+ //
13
+ // Polygon input can come from an untrusted source, for example a vector tile. Thus the work of
14
+ // the search has a limit. To score one candidate costs one point-to-segment test for each vertex.
15
+ // The search counts these tests against a budget, and returns the best point found when the
16
+ // budget is empty. A caller can share one budget across many polygons, so that many large
17
+ // polygons together cannot block the main thread. The first grid also has at most
18
+ // MAX_GRID_CELLS_PER_AXIS squares along the longer side, and the precision has a floor relative
19
+ // to the size of the polygon.
20
+
21
+ const MAX_GRID_CELLS_PER_AXIS = 64;
22
+ const MIN_RELATIVE_PRECISION = 1e-6;
23
+ /** Default budget for one search: point-to-segment distance tests. */
24
+ export const DEFAULT_POLE_SEARCH_SEGMENT_TESTS = 5e6;
25
+
26
+ /** Remaining work, in point-to-segment distance tests, shared by the searches that use it. */
27
+ export type PoleSearchBudget = {
28
+ segmentTests: number;
29
+ };
30
+
31
+ /**
32
+ * Returns the point inside a polygon farthest from its outline, to within `precision` in the
33
+ * polygon's units. `polygon` is GeoJSON-style: an outer ring followed by hole rings. Coordinates
34
+ * must be planar.
35
+ *
36
+ * The search spends at most `budget.segmentTests` point-to-segment distance tests and deducts
37
+ * what it spends, so one budget can bound several searches. When the budget runs out, the best
38
+ * point found so far is returned. The returned point is always strictly inside the polygon: not
39
+ * in a hole and not on an edge.
40
+ *
41
+ * Returns null for a polygon without vertices, with a non-finite coordinate, or with no area, and
42
+ * when the budget runs out before the search finds a point inside the polygon.
43
+ */
44
+ export function getPoleOfInaccessibility(
45
+ polygon: number[][][],
46
+ precision: number,
47
+ budget: PoleSearchBudget = {
48
+ segmentTests: DEFAULT_POLE_SEARCH_SEGMENT_TESTS
49
+ }
50
+ ): [number, number] | null {
51
+ const outline = polygon[0];
52
+ if (!outline || outline.length === 0) {
53
+ return null;
54
+ }
55
+ const vertexCount = countFiniteVertices(polygon);
56
+ if (vertexCount < 0) {
57
+ return null;
58
+ }
59
+
60
+ const box = getRingBox(outline);
61
+ const width = box.right - box.left;
62
+ const height = box.top - box.bottom;
63
+ if (width === 0 || height === 0) {
64
+ return null;
65
+ }
66
+ const longSide = Math.max(width, height);
67
+ const minTolerance = longSide * MIN_RELATIVE_PRECISION;
68
+ const tolerance = Math.max(
69
+ Number.isFinite(precision) && precision > 0 ? precision : 0,
70
+ minTolerance
71
+ );
72
+
73
+ const search = new PoleSearch(polygon, vertexCount, budget);
74
+
75
+ // Two cheap first guesses: the area centroid, which is inside most convex-ish shapes, and the
76
+ // middle of the bounding box.
77
+ const [centroidX, centroidY] = getAreaCentroid(polygon[0]);
78
+ if (!search.score(centroidX, centroidY, 0)) {
79
+ return null;
80
+ }
81
+ if (!search.score(box.left + width / 2, box.bottom + height / 2, 0)) {
82
+ return search.bestPoint();
83
+ }
84
+
85
+ // The first grid uses squares as wide as the polygon's short side, so a long, thin polygon is
86
+ // covered by squares that fit it. The cap keeps a sliver from starting with millions of them.
87
+ const side = Math.max(Math.min(width, height), longSide / MAX_GRID_CELLS_PER_AXIS);
88
+ const columns = Math.ceil(width / side);
89
+ const rows = Math.ceil(height / side);
90
+ for (let column = 0; column < columns; column++) {
91
+ for (let row = 0; row < rows; row++) {
92
+ const x = box.left + (column + 0.5) * side;
93
+ const y = box.bottom + (row + 0.5) * side;
94
+ if (!search.score(x, y, side / 2)) {
95
+ return search.bestPoint();
96
+ }
97
+ }
98
+ }
99
+
100
+ search.refine(tolerance, minTolerance);
101
+ return search.bestPoint();
102
+ }
103
+
104
+ /** Returns the number of vertices in all rings, or -1 if any coordinate is not finite. */
105
+ function countFiniteVertices(polygon: number[][][]): number {
106
+ let count = 0;
107
+ for (const ring of polygon) {
108
+ for (const point of ring) {
109
+ if (!Number.isFinite(point[0]) || !Number.isFinite(point[1])) {
110
+ return -1;
111
+ }
112
+ }
113
+ count += ring.length;
114
+ }
115
+ return count;
116
+ }
117
+
118
+ function getRingBox(ring: number[][]): {
119
+ left: number;
120
+ bottom: number;
121
+ right: number;
122
+ top: number;
123
+ } {
124
+ let left = Infinity;
125
+ let bottom = Infinity;
126
+ let right = -Infinity;
127
+ let top = -Infinity;
128
+ for (const point of ring) {
129
+ left = Math.min(left, point[0]);
130
+ right = Math.max(right, point[0]);
131
+ bottom = Math.min(bottom, point[1]);
132
+ top = Math.max(top, point[1]);
133
+ }
134
+ return {left, bottom, right, top};
135
+ }
136
+
137
+ /** The centroid of the area a ring encloses, or its first vertex when that area is zero. */
138
+ function getAreaCentroid(ring: number[][]): [number, number] {
139
+ let twiceArea = 0;
140
+ let sumX = 0;
141
+ let sumY = 0;
142
+ let previous = ring[ring.length - 1];
143
+ for (const current of ring) {
144
+ const cross = previous[0] * current[1] - current[0] * previous[1];
145
+ twiceArea += cross;
146
+ sumX += (previous[0] + current[0]) * cross;
147
+ sumY += (previous[1] + current[1]) * cross;
148
+ previous = current;
149
+ }
150
+ if (twiceArea === 0) {
151
+ return [ring[0][0], ring[0][1]];
152
+ }
153
+ return [sumX / (3 * twiceArea), sumY / (3 * twiceArea)];
154
+ }
155
+
156
+ /**
157
+ * The state of one search: the best point so far and a max-heap of candidate squares. The
158
+ * squares are kept in parallel arrays, and the heap holds their indices, ordered by `reach`.
159
+ */
160
+ class PoleSearch {
161
+ private readonly polygon: number[][][];
162
+ private readonly vertexCount: number;
163
+ private readonly budget: PoleSearchBudget;
164
+
165
+ private readonly centerX: number[] = [];
166
+ private readonly centerY: number[] = [];
167
+ private readonly halfSide: number[] = [];
168
+ /** The largest distance to the outline that any point of the square can have. */
169
+ private readonly reach: number[] = [];
170
+ private readonly heap: number[] = [];
171
+
172
+ private bestX = NaN;
173
+ private bestY = NaN;
174
+ private bestDistance = -Infinity;
175
+
176
+ constructor(polygon: number[][][], vertexCount: number, budget: PoleSearchBudget) {
177
+ this.polygon = polygon;
178
+ this.vertexCount = vertexCount;
179
+ this.budget = budget;
180
+ }
181
+
182
+ /** The best point so far, or null if no scored point is inside the polygon. */
183
+ bestPoint(): [number, number] | null {
184
+ return this.bestDistance > 0 ? [this.bestX, this.bestY] : null;
185
+ }
186
+
187
+ /**
188
+ * Scores the square centered at (x, y), keeps its center if it is the best so far, and queues
189
+ * the square if it has area. Returns false, without scoring, when the budget cannot pay.
190
+ */
191
+ score(x: number, y: number, half: number): boolean {
192
+ if (this.budget.segmentTests < this.vertexCount) {
193
+ return false;
194
+ }
195
+ this.budget.segmentTests -= this.vertexCount;
196
+ const distance = getSignedOutlineDistance(this.polygon, x, y);
197
+ if (distance > this.bestDistance) {
198
+ this.bestX = x;
199
+ this.bestY = y;
200
+ this.bestDistance = distance;
201
+ }
202
+ if (half > 0) {
203
+ // No point in the square is farther from the center than half its diagonal.
204
+ this.enqueue(x, y, half, distance + half * Math.SQRT2);
205
+ }
206
+ return true;
207
+ }
208
+
209
+ /**
210
+ * Splits the most promising squares until none can beat the best point by `tolerance`. While no
211
+ * scored point is inside the polygon, `tolerance` does not apply: the search continues while a
212
+ * square can hold an inside point and is larger than `minTolerance`.
213
+ */
214
+ refine(tolerance: number, minTolerance: number): void {
215
+ while (this.heap.length > 0) {
216
+ const index = this.dequeue();
217
+ const reach = this.reach[index];
218
+ if (this.bestDistance > 0 ? reach - this.bestDistance <= tolerance : reach <= 0) {
219
+ // The heap is ordered by reach, so no remaining square can do better either.
220
+ return;
221
+ }
222
+ if (this.bestDistance <= 0 && this.halfSide[index] * Math.SQRT2 <= minTolerance) {
223
+ // The square is too small to search further. Larger squares can remain in the heap.
224
+ continue;
225
+ }
226
+ const quarter = this.halfSide[index] / 2;
227
+ const x = this.centerX[index];
228
+ const y = this.centerY[index];
229
+ if (
230
+ !this.score(x - quarter, y - quarter, quarter) ||
231
+ !this.score(x + quarter, y - quarter, quarter) ||
232
+ !this.score(x - quarter, y + quarter, quarter) ||
233
+ !this.score(x + quarter, y + quarter, quarter)
234
+ ) {
235
+ return;
236
+ }
237
+ }
238
+ }
239
+
240
+ private enqueue(x: number, y: number, half: number, reach: number): void {
241
+ const index = this.reach.length;
242
+ this.centerX.push(x);
243
+ this.centerY.push(y);
244
+ this.halfSide.push(half);
245
+ this.reach.push(reach);
246
+
247
+ const {heap} = this;
248
+ let slot = heap.length;
249
+ heap.push(index);
250
+ while (slot > 0) {
251
+ const parentSlot = (slot - 1) >> 1;
252
+ if (this.reach[heap[parentSlot]] >= reach) {
253
+ break;
254
+ }
255
+ heap[slot] = heap[parentSlot];
256
+ slot = parentSlot;
257
+ }
258
+ heap[slot] = index;
259
+ }
260
+
261
+ private dequeue(): number {
262
+ const {heap, reach} = this;
263
+ const top = heap[0];
264
+ const moved = heap.pop() as number;
265
+ const size = heap.length;
266
+ if (size === 0) {
267
+ return top;
268
+ }
269
+ let slot = 0;
270
+ for (;;) {
271
+ let next = slot * 2 + 1;
272
+ if (next >= size) {
273
+ break;
274
+ }
275
+ if (next + 1 < size && reach[heap[next + 1]] > reach[heap[next]]) {
276
+ next += 1;
277
+ }
278
+ if (reach[heap[next]] <= reach[moved]) {
279
+ break;
280
+ }
281
+ heap[slot] = heap[next];
282
+ slot = next;
283
+ }
284
+ heap[slot] = moved;
285
+ return top;
286
+ }
287
+ }
288
+
289
+ /**
290
+ * Distance from (x, y) to the nearest edge of any ring: positive inside the polygon, negative
291
+ * outside, and 0 on an edge. Inside is decided by the even-odd rule, so holes count as outside.
292
+ */
293
+ function getSignedOutlineDistance(polygon: number[][][], x: number, y: number): number {
294
+ let nearestSquared = Infinity;
295
+ let crossings = 0;
296
+ for (const ring of polygon) {
297
+ let start = ring[ring.length - 1];
298
+ for (const end of ring) {
299
+ // Count edges that cross the horizontal ray from (x, y) toward +x.
300
+ if (start[1] <= y !== end[1] <= y) {
301
+ const t = (y - start[1]) / (end[1] - start[1]);
302
+ if (start[0] + t * (end[0] - start[0]) > x) {
303
+ crossings++;
304
+ }
305
+ }
306
+ nearestSquared = Math.min(nearestSquared, getSquaredSegmentDistance(x, y, start, end));
307
+ start = end;
308
+ }
309
+ }
310
+ if (nearestSquared === 0) {
311
+ return 0;
312
+ }
313
+ const distance = Math.sqrt(nearestSquared);
314
+ return crossings % 2 === 1 ? distance : -distance;
315
+ }
316
+
317
+ /** Squared distance from (x, y) to the segment from `start` to `end`. */
318
+ function getSquaredSegmentDistance(x: number, y: number, start: number[], end: number[]): number {
319
+ const edgeX = end[0] - start[0];
320
+ const edgeY = end[1] - start[1];
321
+ const lengthSquared = edgeX * edgeX + edgeY * edgeY;
322
+ // Where the perpendicular from (x, y) meets the edge's line, as a fraction of the edge.
323
+ const along =
324
+ lengthSquared === 0
325
+ ? 0
326
+ : Math.max(0, Math.min(1, ((x - start[0]) * edgeX + (y - start[1]) * edgeY) / lengthSquared));
327
+ const offsetX = start[0] + along * edgeX - x;
328
+ const offsetY = start[1] + along * edgeY - y;
329
+ return offsetX * offsetX + offsetY * offsetY;
330
+ }