@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,118 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+ import {Geometry} from '../lib/geometry';
5
+ import {
6
+ assertPositive,
7
+ assertSegments,
8
+ makeIndices,
9
+ type PrimitiveGeometryProps
10
+ } from '../geometries/geometry-helpers';
11
+ import {ParametricGeometry} from './parametric-geometry';
12
+ export type TorusGeometryProps = PrimitiveGeometryProps & {
13
+ majorRadius?: number;
14
+ minorRadius?: number;
15
+ majorSegments?: number;
16
+ minorSegments?: number;
17
+ /** Sweep in radians, (0, 2π]. Partial sweeps have open ends unless cap is enabled. */
18
+ arc?: number;
19
+ /** Rotation in radians about Y; positive angles turn toward negative Z. */
20
+ startAngle?: number;
21
+ /** Close partial sweeps with flat end disks. Ignored for a full revolution. */
22
+ cap?: boolean;
23
+ };
24
+ /** Ring torus around Y, with outward analytic normals and separate UV seam rows. */
25
+ export class TorusGeometry extends ParametricGeometry {
26
+ constructor(props: TorusGeometryProps = {}) {
27
+ const {
28
+ majorRadius = 1,
29
+ minorRadius = 0.3,
30
+ majorSegments = 48,
31
+ minorSegments = 24,
32
+ arc = Math.PI * 2,
33
+ startAngle = 0,
34
+ cap = false
35
+ } = props;
36
+ assertPositive(majorRadius, 'majorRadius');
37
+ assertPositive(minorRadius, 'minorRadius');
38
+ if (minorRadius >= majorRadius)
39
+ throw new RangeError('Ring torus requires minorRadius < majorRadius');
40
+ if (!Number.isFinite(arc) || arc <= 0 || arc > Math.PI * 2)
41
+ throw new RangeError('arc must be in (0, 2π]');
42
+ assertSegments(majorSegments, 'majorSegments', 3);
43
+ assertSegments(minorSegments, 'minorSegments', 3);
44
+ if (!Number.isFinite(startAngle)) throw new RangeError('startAngle must be finite');
45
+ const hasCaps = cap && arc < Math.PI * 2;
46
+ super({
47
+ ...props,
48
+ attributes: hasCaps ? undefined : props.attributes,
49
+ uSegments: majorSegments,
50
+ vSegments: minorSegments,
51
+ periodicU: arc === Math.PI * 2,
52
+ periodicV: true,
53
+ sample: (u, v) => {
54
+ const theta = startAngle + u * arc,
55
+ phi = v * Math.PI * 2,
56
+ radius = majorRadius + minorRadius * Math.cos(phi);
57
+ return [radius * Math.cos(theta), minorRadius * Math.sin(phi), -radius * Math.sin(theta)];
58
+ },
59
+ normal: (u, v) => [
60
+ Math.cos(v * Math.PI * 2) * Math.cos(startAngle + u * arc),
61
+ Math.sin(v * Math.PI * 2),
62
+ -Math.cos(v * Math.PI * 2) * Math.sin(startAngle + u * arc)
63
+ ]
64
+ });
65
+ if (hasCaps) {
66
+ const surfacePositions = this.attributes['POSITION'].value;
67
+ const positions = Array.from(surfacePositions);
68
+ const normals = Array.from(this.attributes['NORMAL'].value);
69
+ const textureCoordinates = Array.from(this.attributes['TEXCOORD_0'].value);
70
+ const indices = Array.from(this.indices!.value);
71
+ for (const [angle, direction] of [
72
+ [startAngle, -1],
73
+ [startAngle + arc, 1]
74
+ ]) {
75
+ const normal = [-Math.sin(angle) * direction, 0, -Math.cos(angle) * direction];
76
+ const centerIndex = positions.length / 3;
77
+ positions.push(majorRadius * Math.cos(angle), 0, -majorRadius * Math.sin(angle));
78
+ normals.push(...normal);
79
+ textureCoordinates.push(0.5, 0.5);
80
+ for (let minorSegment = 0; minorSegment <= minorSegments; minorSegment++) {
81
+ // The final disk vertex duplicates the first exactly, matching the tube UV seam.
82
+ const tubeAngle = ((minorSegment % minorSegments) * Math.PI * 2) / minorSegments;
83
+ // Copy boundary positions so disk and tube edges match bit for bit.
84
+ const boundaryOffset =
85
+ (minorSegment * (majorSegments + 1) + (direction < 0 ? 0 : majorSegments)) * 3;
86
+ positions.push(...surfacePositions.slice(boundaryOffset, boundaryOffset + 3));
87
+ normals.push(...normal);
88
+ textureCoordinates.push((Math.cos(tubeAngle) + 1) / 2, (Math.sin(tubeAngle) + 1) / 2);
89
+ if (minorSegment < minorSegments) {
90
+ const firstIndex = centerIndex + minorSegment + 1;
91
+ const secondIndex = firstIndex + 1;
92
+ indices.push(
93
+ centerIndex,
94
+ ...(direction > 0 ? [secondIndex, firstIndex] : [firstIndex, secondIndex])
95
+ );
96
+ }
97
+ }
98
+ }
99
+ const cappedGeometry = new Geometry({
100
+ id: this.id,
101
+ topology: this.topology,
102
+ indices: makeIndices(positions.length / 3, indices),
103
+ attributes: {
104
+ POSITION: {size: 3, value: new Float32Array(positions)},
105
+ NORMAL: {size: 3, value: new Float32Array(normals)},
106
+ TEXCOORD_0: {size: 2, value: new Float32Array(textureCoordinates)},
107
+ ...props.attributes
108
+ }
109
+ });
110
+ // Finish construction with validated cap data while retaining ParametricGeometry inheritance.
111
+ Object.assign(this, {
112
+ attributes: cappedGeometry.attributes,
113
+ indices: cappedGeometry.indices,
114
+ vertexCount: cappedGeometry.vertexCount
115
+ });
116
+ }
117
+ }
118
+ }
@@ -0,0 +1,9 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+ export {ParametricGeometry} from './parametric/parametric-geometry';
5
+ export type {ParametricGeometryProps, SurfaceSampler} from './parametric/parametric-geometry';
6
+ export {TorusGeometry} from './parametric/torus-geometry';
7
+ export type {TorusGeometryProps} from './parametric/torus-geometry';
8
+ export {LatheGeometry} from './parametric/lathe-geometry';
9
+ export type {LatheGeometryProps} from './parametric/lathe-geometry';
@@ -0,0 +1,218 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ import type {TriangleMesh} from './subdivide-triangle-mesh';
6
+
7
+ /** A convex target-plane component, without a repeated closing point. Either winding is accepted. */
8
+ export type MeshClipRegion = readonly (readonly [number, number])[];
9
+ /** Clipping of a prepared, finite target-space mesh. Regions must have disjoint interiors. */
10
+ export type ClipTriangleMeshOptions = {
11
+ regions: readonly MeshClipRegion[];
12
+ /** Position components; clipping uses the first two and interpolates the third. Default 2. */
13
+ size?: 2 | 3;
14
+ /** Shift already-unwrapped triangles into a canonical periodic interval before clipping.
15
+ * Does not infer which side of a seam source edges follow. Split/unwrap before calling.
16
+ */
17
+ periodic?: {axis: 0 | 1; minimum: number; period: number};
18
+ /** Maximum emitted vertices, including duplicated region/seam vertices. Default 65536. */
19
+ maxVertices?: number;
20
+ /** Maximum emitted triangles. Default 131072. */
21
+ maxTriangles?: number;
22
+ /** Maximum triangle/copy/region intersection attempts. Default 1000000. */
23
+ maxIntersections?: number;
24
+ };
25
+ /** Clipped mesh with original-vertex provenance for interpolatePackedAttributes. */
26
+ export type ClippedTriangleMesh = {
27
+ positions: Float64Array;
28
+ indices: Uint32Array;
29
+ sourceVertexIndices: Uint32Array;
30
+ sourceVertexWeights: Float64Array;
31
+ sourceTriangleIndices: Uint32Array;
32
+ /** Integer target-axis period shift for each output triangle; zero without periodic clipping. */
33
+ periodicCopies: Int32Array;
34
+ };
35
+ type Vertex = {position: number[]; weights: number[]};
36
+
37
+ /** Intersect indexed triangles with a union of convex target-plane regions.
38
+ * Concave domains and holes must first be decomposed into disjoint convex regions (e.g. earcut
39
+ * triangles). This is planar clipping, not spherical topology repair or projection unwrapping.
40
+ * Winding and original-triangle attribution survive clipping; coincident vertices are never
41
+ * welded. Work/output limit exhaustion throws rather than returning silently incomplete geometry.
42
+ */
43
+ export function clipTriangleMesh(
44
+ mesh: TriangleMesh,
45
+ options: ClipTriangleMeshOptions
46
+ ): ClippedTriangleMesh {
47
+ const {
48
+ size = 2,
49
+ maxVertices = 65536,
50
+ maxTriangles = 131072,
51
+ maxIntersections = 1000000
52
+ } = options;
53
+ if (
54
+ (size !== 2 && size !== 3) ||
55
+ ![maxVertices, maxTriangles, maxIntersections].every(v => Number.isSafeInteger(v) && v >= 0) ||
56
+ maxVertices > 0xffffffff ||
57
+ maxTriangles > 0xffffffff ||
58
+ mesh.positions.length % size ||
59
+ mesh.indices.length % 3
60
+ ) {
61
+ throw new RangeError('Invalid mesh clipping dimensions or limits');
62
+ }
63
+ if (
64
+ !Number.isSafeInteger(mesh.positions.length) ||
65
+ mesh.positions.length < 0 ||
66
+ !Number.isSafeInteger(mesh.indices.length) ||
67
+ mesh.indices.length < 0 ||
68
+ !Array.isArray(options.regions)
69
+ )
70
+ throw new RangeError('Invalid mesh storage');
71
+ const source = Array.from(mesh.positions);
72
+ if (!source.every(Number.isFinite))
73
+ throw new RangeError('Clipping requires finite target positions');
74
+ const inputIndices = Array.from(mesh.indices);
75
+ if (!inputIndices.every(i => Number.isSafeInteger(i) && i >= 0 && i < source.length / size))
76
+ throw new RangeError('Invalid mesh index');
77
+ const regions = Array.from(options.regions, validateRegion);
78
+ const period = options.periodic;
79
+ if (period === null) throw new RangeError('Invalid clipping period');
80
+ if (
81
+ period &&
82
+ ((period.axis !== 0 && period.axis !== 1) ||
83
+ !Number.isFinite(period.minimum) ||
84
+ !Number.isFinite(period.period) ||
85
+ period.period <= 0 ||
86
+ !Number.isFinite(period.minimum + period.period) ||
87
+ period.minimum + period.period <= period.minimum)
88
+ ) {
89
+ throw new RangeError('Invalid clipping period');
90
+ }
91
+ if (
92
+ period &&
93
+ regions.some(r =>
94
+ r.some(
95
+ p => p[period.axis] < period.minimum || p[period.axis] > period.minimum + period.period
96
+ )
97
+ )
98
+ ) {
99
+ throw new RangeError('Clip regions must lie in the canonical period');
100
+ }
101
+ const positions: number[] = [],
102
+ indices: number[] = [],
103
+ sourceVertexIndices: number[] = [],
104
+ sourceVertexWeights: number[] = [];
105
+ const sourceTriangleIndices: number[] = [],
106
+ periodicCopies: number[] = [];
107
+ let intersections = 0;
108
+ for (let triangle = 0; triangle < inputIndices.length / 3; triangle++) {
109
+ const originalIndices = inputIndices.slice(triangle * 3, triangle * 3 + 3);
110
+ const vertices = originalIndices.map((index, j) => ({
111
+ position: source.slice(index * size, index * size + size),
112
+ weights: [0, 1, 2].map(k => Number(k === j))
113
+ }));
114
+ let firstCopy = 0,
115
+ lastCopy = 0;
116
+ if (period) {
117
+ const values = vertices.map(v => v.position[period.axis]);
118
+ firstCopy = Math.ceil((period.minimum - Math.max(...values)) / period.period);
119
+ lastCopy = Math.floor((period.minimum + period.period - Math.min(...values)) / period.period);
120
+ if (
121
+ !Number.isSafeInteger(firstCopy) ||
122
+ !Number.isSafeInteger(lastCopy) ||
123
+ firstCopy < -2147483648 ||
124
+ lastCopy > 2147483647
125
+ )
126
+ throw new RangeError('Unrepresentable periodic copy');
127
+ }
128
+ if (regions.length === 0) continue;
129
+ if ((lastCopy - firstCopy + 1) * regions.length > maxIntersections - intersections)
130
+ throw new RangeError('Mesh clipping exceeded maxIntersections');
131
+ for (let copy = firstCopy; copy <= lastCopy; copy++) {
132
+ const shifted = vertices.map(v => ({
133
+ weights: v.weights,
134
+ position: v.position.map(
135
+ (x, axis) => x + (period && axis === period.axis ? copy * period.period : 0)
136
+ )
137
+ }));
138
+ if (shifted.some(v => !v.position.every(Number.isFinite)))
139
+ throw new RangeError('Nonfinite periodic position');
140
+ for (const region of regions) {
141
+ intersections++;
142
+ let polygon = shifted;
143
+ for (let edge = 0; edge < region.length && polygon.length; edge++) {
144
+ const a = region[edge],
145
+ b = region[(edge + 1) % region.length];
146
+ const clipped: Vertex[] = [];
147
+ for (let i = 0; i < polygon.length; i++) {
148
+ const p = polygon[i],
149
+ q = polygon[(i + 1) % polygon.length];
150
+ const dp = cross(a, b, p.position),
151
+ dq = cross(a, b, q.position);
152
+ if (dp >= 0) clipped.push(p);
153
+ if (dp < 0 !== dq < 0) {
154
+ // Scale distances to avoid overflow in the denominator.
155
+ const scale = Math.max(Math.abs(dp), Math.abs(dq));
156
+ const t = dp / scale / (dp / scale - dq / scale);
157
+ clipped.push({
158
+ position: p.position.map((x, j) => x * (1 - t) + q.position[j] * t),
159
+ weights: p.weights.map((x, j) => x * (1 - t) + q.weights[j] * t)
160
+ });
161
+ }
162
+ }
163
+ polygon = clipped;
164
+ }
165
+ for (let j = 1; j + 1 < polygon.length; j++) {
166
+ const face = [polygon[0], polygon[j], polygon[j + 1]];
167
+ if (cross(face[0].position, face[1].position, face[2].position) === 0) continue;
168
+ if (positions.length / size + 3 > maxVertices || indices.length / 3 + 1 > maxTriangles)
169
+ throw new RangeError('Mesh clipping exceeded output limits');
170
+ for (const vertex of face) {
171
+ indices.push(positions.length / size);
172
+ positions.push(...vertex.position);
173
+ sourceVertexIndices.push(...originalIndices);
174
+ sourceVertexWeights.push(...vertex.weights);
175
+ }
176
+ sourceTriangleIndices.push(triangle);
177
+ periodicCopies.push(copy);
178
+ }
179
+ }
180
+ }
181
+ }
182
+ return {
183
+ positions: new Float64Array(positions),
184
+ indices: new Uint32Array(indices),
185
+ sourceVertexIndices: new Uint32Array(sourceVertexIndices),
186
+ sourceVertexWeights: new Float64Array(sourceVertexWeights),
187
+ sourceTriangleIndices: new Uint32Array(sourceTriangleIndices),
188
+ periodicCopies: new Int32Array(periodicCopies)
189
+ };
190
+ }
191
+
192
+ function cross(a: ArrayLike<number>, b: ArrayLike<number>, p: ArrayLike<number>): number {
193
+ const result = (b[0] - a[0]) * (p[1] - a[1]) - (b[1] - a[1]) * (p[0] - a[0]);
194
+ if (!Number.isFinite(result)) throw new RangeError('Unrepresentable clipping arithmetic');
195
+ return result;
196
+ }
197
+ function validateRegion(region: MeshClipRegion): MeshClipRegion {
198
+ if (
199
+ !Array.isArray(region) ||
200
+ region.length < 3 ||
201
+ region.length > 1024 ||
202
+ Array.from(region).some(
203
+ p => !Array.isArray(p) || p.length !== 2 || !Array.from(p).every(Number.isFinite)
204
+ )
205
+ )
206
+ throw new RangeError('Invalid convex clipping region');
207
+ let area = 0;
208
+ for (let i = 1; i + 1 < region.length; i++) area += cross(region[0], region[i], region[i + 1]);
209
+ if (!Number.isFinite(area) || area === 0) throw new RangeError('Degenerate clipping region');
210
+ const result = area > 0 ? Array.from(region) : Array.from(region).reverse();
211
+ for (let i = 0; i < result.length; i++) {
212
+ const a = result[i],
213
+ b = result[(i + 1) % result.length];
214
+ if ((a[0] === b[0] && a[1] === b[1]) || result.some(p => cross(a, b, p) < 0))
215
+ throw new RangeError('Clip region must be convex');
216
+ }
217
+ return result;
218
+ }
@@ -0,0 +1,313 @@
1
+ // math.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
4
+
5
+ /* eslint-disable max-statements, max-depth, complexity, no-unused-expressions */
6
+ import {bitCode, intersect, BoundingBox} from './lineclip';
7
+ import {getPointAtIndex, copy, push} from './utils';
8
+
9
+ import type {NumericArray} from '@math.gl/core';
10
+
11
+ export type Polygon = {
12
+ positions: Readonly<NumericArray>;
13
+ holeIndices?: Readonly<NumericArray>;
14
+ edgeTypes?: Readonly<NumericArray>;
15
+ };
16
+
17
+ export function cutPolylineByGrid(
18
+ positions: NumericArray,
19
+ options?: {
20
+ size?: number;
21
+ broken?: boolean;
22
+ gridResolution?: number;
23
+ gridOffset?: [number, number];
24
+ startIndex?: number;
25
+ endIndex?: number;
26
+ }
27
+ ): number[] | number[][] {
28
+ const {
29
+ size = 2,
30
+ broken = false,
31
+ gridResolution = 10,
32
+ gridOffset = [0, 0],
33
+ startIndex = 0,
34
+ endIndex = positions.length
35
+ } = options || {};
36
+ const numPoints = (endIndex - startIndex) / size;
37
+ let part: number[] = [];
38
+ const result: number[][] = [part];
39
+ const a: number[] = getPointAtIndex(positions, 0, size, startIndex);
40
+ let b: number[];
41
+ let codeB: number;
42
+ const cell: BoundingBox = getGridCell(a, gridResolution, gridOffset, []);
43
+ const scratchPoint: number[] = [];
44
+ push(part, a);
45
+
46
+ for (let i = 1; i < numPoints; i++) {
47
+ b = getPointAtIndex(positions, i, size, startIndex, b);
48
+ codeB = bitCode(b, cell);
49
+
50
+ while (codeB) {
51
+ // find the intersection with the current cell
52
+ intersect(a, b, codeB, cell, scratchPoint);
53
+ const codeAlt = bitCode(scratchPoint, cell);
54
+ if (codeAlt) {
55
+ intersect(a, scratchPoint, codeAlt, cell, scratchPoint);
56
+ codeB = codeAlt;
57
+ }
58
+ push(part, scratchPoint);
59
+ // move to the next cell
60
+ copy(a, scratchPoint);
61
+
62
+ moveToNeighborCell(cell, gridResolution, codeB);
63
+ if (broken && part.length > size) {
64
+ part = [];
65
+ result.push(part);
66
+ push(part, a);
67
+ }
68
+
69
+ codeB = bitCode(b, cell);
70
+ }
71
+
72
+ push(part, b);
73
+ copy(a, b);
74
+ }
75
+
76
+ return broken ? result : result[0];
77
+ }
78
+
79
+ const TYPE_INSIDE = 0;
80
+ const TYPE_BORDER = 1;
81
+
82
+ /** Internal helper type during cutting, name TBD */
83
+ type PolygonCut = {
84
+ pos: Readonly<NumericArray>;
85
+ types: number[];
86
+ holes: Readonly<NumericArray>;
87
+ };
88
+
89
+ /** Internal helper type during cutting, name TBD */
90
+ type MutablePolygonCut = {
91
+ pos: number[];
92
+ types: number[];
93
+ holes: number[];
94
+ };
95
+
96
+ /**
97
+ * Cuts a polygon by a pre-defined grid
98
+ */
99
+ export function cutPolygonByGrid(
100
+ positions: Readonly<NumericArray>,
101
+ holeIndices: Readonly<NumericArray> | null = null,
102
+ options?: {
103
+ size?: number;
104
+ gridResolution?: number;
105
+ gridOffset?: [number, number];
106
+ edgeTypes?: boolean;
107
+ }
108
+ ): Polygon[] {
109
+ if (!positions.length) {
110
+ // input is empty
111
+ return [];
112
+ }
113
+ const {size = 2, gridResolution = 10, gridOffset = [0, 0], edgeTypes = false} = options || {};
114
+ const result: Polygon[] = [];
115
+ const queue: PolygonCut[] = [
116
+ {
117
+ pos: positions,
118
+ types: edgeTypes ? (new Array(positions.length / size).fill(TYPE_BORDER) as number[]) : null,
119
+ holes: holeIndices || []
120
+ }
121
+ ];
122
+ const bbox: number[][] = [[], []];
123
+ // @ts-ignore
124
+ let cell: BoundingBox = [];
125
+
126
+ // Recursively bisect polygon until every part fit in a single grid cell
127
+ while (queue.length) {
128
+ const {pos, types, holes} = queue.shift();
129
+
130
+ // Get the bounding box of the outer polygon
131
+ getBoundingBox(pos, size, holes[0] || pos.length, bbox);
132
+ cell = getGridCell(bbox[0], gridResolution, gridOffset, cell);
133
+ const code = bitCode(bbox[1], cell);
134
+
135
+ if (code) {
136
+ // Split the outer ring at the boundary
137
+ let parts = bisectPolygon(pos, types, size, 0, holes[0] || pos.length, cell, code);
138
+ const polygonLow: MutablePolygonCut = {pos: parts[0].pos, types: parts[0].types, holes: []};
139
+ const polygonHigh: MutablePolygonCut = {pos: parts[1].pos, types: parts[1].types, holes: []};
140
+ queue.push(polygonLow, polygonHigh);
141
+
142
+ // Split each hole at the boundary
143
+ for (let i = 0; i < holes.length; i++) {
144
+ parts = bisectPolygon(pos, types, size, holes[i], holes[i + 1] || pos.length, cell, code);
145
+
146
+ if (parts[0]) {
147
+ polygonLow.holes.push(polygonLow.pos.length);
148
+ polygonLow.pos = concatInPlace(polygonLow.pos, parts[0].pos);
149
+ if (edgeTypes) {
150
+ polygonLow.types = concatInPlace(polygonLow.types, parts[0].types);
151
+ }
152
+ }
153
+ if (parts[1]) {
154
+ polygonHigh.holes.push(polygonHigh.pos.length);
155
+ polygonHigh.pos = concatInPlace(polygonHigh.pos, parts[1].pos);
156
+ if (edgeTypes) {
157
+ polygonHigh.types = concatInPlace(polygonHigh.types, parts[1].types);
158
+ }
159
+ }
160
+ }
161
+ } else {
162
+ // Polygon fits in a single cell, no more processing required
163
+ const polygon: Polygon = {positions: pos};
164
+ if (edgeTypes) {
165
+ polygon.edgeTypes = types;
166
+ }
167
+ if (holes.length) {
168
+ polygon.holeIndices = holes;
169
+ }
170
+
171
+ result.push(polygon);
172
+ }
173
+ }
174
+ return result;
175
+ }
176
+
177
+ // edgeTypes:
178
+ // TYPE_BORDER - edge from the original polygon
179
+ // TYPE_INSIDE - inside the original polygon
180
+ // eslint-disable-next-line max-params
181
+ function bisectPolygon(
182
+ positions: Readonly<NumericArray>,
183
+ edgeTypes: number[] | undefined,
184
+ size: number,
185
+ startIndex: number,
186
+ endIndex: number,
187
+ bbox: BoundingBox,
188
+ edge: number
189
+ ): {
190
+ pos: number[];
191
+ types?: number[];
192
+ }[] {
193
+ const numPoints = (endIndex - startIndex) / size;
194
+ const resultLow: number[] = [];
195
+ const resultHigh: number[] = [];
196
+ const typesLow: number[] = [];
197
+ const typesHigh: number[] = [];
198
+ const scratchPoint: number[] = [];
199
+
200
+ let p: number[];
201
+ let side: number;
202
+ let type: number;
203
+ const prev = getPointAtIndex(positions, numPoints - 1, size, startIndex);
204
+ let prevSide = Math.sign(edge & 8 ? prev[1] - bbox[3] : prev[0] - bbox[2]);
205
+ let prevType = edgeTypes && edgeTypes[numPoints - 1];
206
+ let lowPointCount = 0;
207
+ let highPointCount = 0;
208
+
209
+ for (let i = 0; i < numPoints; i++) {
210
+ p = getPointAtIndex(positions, i, size, startIndex, p);
211
+ side = Math.sign(edge & 8 ? p[1] - bbox[3] : p[0] - bbox[2]);
212
+ type = edgeTypes && edgeTypes[startIndex / size + i];
213
+
214
+ // if segment goes through the boundary, add an intersection
215
+ if (side && prevSide && prevSide !== side) {
216
+ intersect(prev, p, edge, bbox, scratchPoint);
217
+ push(resultLow, scratchPoint) && typesLow.push(prevType);
218
+ push(resultHigh, scratchPoint) && typesHigh.push(prevType);
219
+ }
220
+
221
+ if (side <= 0) {
222
+ push(resultLow, p) && typesLow.push(type);
223
+ lowPointCount -= side;
224
+ } else if (typesLow.length) {
225
+ typesLow[typesLow.length - 1] = TYPE_INSIDE;
226
+ }
227
+ if (side >= 0) {
228
+ push(resultHigh, p) && typesHigh.push(type);
229
+ highPointCount += side;
230
+ } else if (typesHigh.length) {
231
+ typesHigh[typesHigh.length - 1] = TYPE_INSIDE;
232
+ }
233
+
234
+ copy(prev, p);
235
+ prevSide = side;
236
+ prevType = type;
237
+ }
238
+
239
+ return [
240
+ lowPointCount ? {pos: resultLow, types: edgeTypes && typesLow} : null,
241
+ highPointCount ? {pos: resultHigh, types: edgeTypes && typesHigh} : null
242
+ ];
243
+ }
244
+
245
+ function getGridCell(
246
+ p: number[],
247
+ gridResolution: number,
248
+ gridOffset: [number, number],
249
+ out: number[]
250
+ ): BoundingBox {
251
+ const left = Math.floor((p[0] - gridOffset[0]) / gridResolution) * gridResolution + gridOffset[0];
252
+ const bottom =
253
+ Math.floor((p[1] - gridOffset[1]) / gridResolution) * gridResolution + gridOffset[1];
254
+ out[0] = left;
255
+ out[1] = bottom;
256
+ out[2] = left + gridResolution;
257
+ out[3] = bottom + gridResolution;
258
+ return out as BoundingBox;
259
+ }
260
+
261
+ function moveToNeighborCell(cell: number[], gridResolution: number, edge: number): void {
262
+ if (edge & 8) {
263
+ // top
264
+ cell[1] += gridResolution;
265
+ cell[3] += gridResolution;
266
+ } else if (edge & 4) {
267
+ // bottom
268
+ cell[1] -= gridResolution;
269
+ cell[3] -= gridResolution;
270
+ } else if (edge & 2) {
271
+ // right
272
+ cell[0] += gridResolution;
273
+ cell[2] += gridResolution;
274
+ } else if (edge & 1) {
275
+ // left
276
+ cell[0] -= gridResolution;
277
+ cell[2] -= gridResolution;
278
+ }
279
+ }
280
+
281
+ function getBoundingBox(
282
+ positions: Readonly<NumericArray>,
283
+ size: number,
284
+ endIndex: number,
285
+ out: number[][]
286
+ ): number[][] {
287
+ let minX = Infinity;
288
+ let maxX = -Infinity;
289
+ let minY = Infinity;
290
+ let maxY = -Infinity;
291
+
292
+ for (let i = 0; i < endIndex; i += size) {
293
+ const x = positions[i];
294
+ const y = positions[i + 1];
295
+ minX = x < minX ? x : minX;
296
+ maxX = x > maxX ? x : maxX;
297
+ minY = y < minY ? y : minY;
298
+ maxY = y > maxY ? y : maxY;
299
+ }
300
+
301
+ out[0][0] = minX;
302
+ out[0][1] = minY;
303
+ out[1][0] = maxX;
304
+ out[1][1] = maxY;
305
+ return out;
306
+ }
307
+
308
+ function concatInPlace(arr1: number[], arr2: number[]): number[] {
309
+ for (let i = 0; i < arr2.length; i++) {
310
+ arr1.push(arr2[i]);
311
+ }
312
+ return arr1;
313
+ }