@ac-kit/math-geometry 0.2.3 → 0.4.0

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 (379) hide show
  1. package/CHANGELOG.md +321 -0
  2. package/README.md +363 -52
  3. package/dist/_brand.d.ts +14 -0
  4. package/dist/_brand.d.ts.map +1 -0
  5. package/dist/_brand.js +2 -0
  6. package/dist/_brand.js.map +1 -0
  7. package/dist/_ear-clipping.d.ts +20 -0
  8. package/dist/_ear-clipping.d.ts.map +1 -0
  9. package/dist/_ear-clipping.js +360 -0
  10. package/dist/_ear-clipping.js.map +1 -0
  11. package/dist/_polygon3-cross.d.ts +23 -0
  12. package/dist/_polygon3-cross.d.ts.map +1 -0
  13. package/dist/_polygon3-cross.js +418 -0
  14. package/dist/_polygon3-cross.js.map +1 -0
  15. package/dist/_ray3-intersect.d.ts +19 -0
  16. package/dist/_ray3-intersect.d.ts.map +1 -0
  17. package/dist/_ray3-intersect.js +122 -0
  18. package/dist/_ray3-intersect.js.map +1 -0
  19. package/dist/_triangle3-cross.d.ts +66 -0
  20. package/dist/_triangle3-cross.d.ts.map +1 -0
  21. package/dist/_triangle3-cross.js +396 -0
  22. package/dist/_triangle3-cross.js.map +1 -0
  23. package/dist/box2-array-boundary.d.ts +50 -0
  24. package/dist/box2-array-boundary.d.ts.map +1 -0
  25. package/dist/box2-array-boundary.js +97 -0
  26. package/dist/box2-array-boundary.js.map +1 -0
  27. package/dist/box2-array.d.ts +42 -0
  28. package/dist/box2-array.d.ts.map +1 -0
  29. package/dist/box2-array.js +134 -0
  30. package/dist/box2-array.js.map +1 -0
  31. package/dist/box2-componentwise.d.ts +110 -0
  32. package/dist/box2-componentwise.d.ts.map +1 -0
  33. package/dist/box2-componentwise.js +325 -0
  34. package/dist/box2-componentwise.js.map +1 -0
  35. package/dist/box2-grid-index.d.ts +67 -0
  36. package/dist/box2-grid-index.d.ts.map +1 -0
  37. package/dist/box2-grid-index.js +198 -0
  38. package/dist/box2-grid-index.js.map +1 -0
  39. package/dist/box2-record.d.ts +83 -0
  40. package/dist/box2-record.d.ts.map +1 -0
  41. package/dist/box2-record.js +124 -0
  42. package/dist/box2-record.js.map +1 -0
  43. package/dist/box2.d.ts +38 -0
  44. package/dist/box2.d.ts.map +1 -0
  45. package/dist/box2.js +97 -0
  46. package/dist/box2.js.map +1 -0
  47. package/dist/box3-array-boundary.d.ts +50 -0
  48. package/dist/box3-array-boundary.d.ts.map +1 -0
  49. package/dist/box3-array-boundary.js +107 -0
  50. package/dist/box3-array-boundary.js.map +1 -0
  51. package/dist/box3-componentwise.d.ts +110 -0
  52. package/dist/box3-componentwise.d.ts.map +1 -0
  53. package/dist/box3-componentwise.js +392 -0
  54. package/dist/box3-componentwise.js.map +1 -0
  55. package/dist/box3-record.d.ts +92 -0
  56. package/dist/box3-record.d.ts.map +1 -0
  57. package/dist/box3-record.js +147 -0
  58. package/dist/box3-record.js.map +1 -0
  59. package/dist/box3.d.ts +19 -16
  60. package/dist/box3.d.ts.map +1 -1
  61. package/dist/box3.js +73 -76
  62. package/dist/box3.js.map +1 -1
  63. package/dist/box4-array-boundary.d.ts +50 -0
  64. package/dist/box4-array-boundary.d.ts.map +1 -0
  65. package/dist/box4-array-boundary.js +117 -0
  66. package/dist/box4-array-boundary.js.map +1 -0
  67. package/dist/box4-componentwise.d.ts +110 -0
  68. package/dist/box4-componentwise.d.ts.map +1 -0
  69. package/dist/box4-componentwise.js +459 -0
  70. package/dist/box4-componentwise.js.map +1 -0
  71. package/dist/box4-record.d.ts +96 -0
  72. package/dist/box4-record.d.ts.map +1 -0
  73. package/dist/box4-record.js +167 -0
  74. package/dist/box4-record.js.map +1 -0
  75. package/dist/box4.d.ts +13 -8
  76. package/dist/box4.d.ts.map +1 -1
  77. package/dist/box4.js +27 -5
  78. package/dist/box4.js.map +1 -1
  79. package/dist/circle2-array-boundary.d.ts +51 -0
  80. package/dist/circle2-array-boundary.d.ts.map +1 -0
  81. package/dist/circle2-array-boundary.js +93 -0
  82. package/dist/circle2-array-boundary.js.map +1 -0
  83. package/dist/circle2-array.d.ts +17 -0
  84. package/dist/circle2-array.d.ts.map +1 -0
  85. package/dist/circle2-array.js +44 -0
  86. package/dist/circle2-array.js.map +1 -0
  87. package/dist/circle2-record.d.ts +81 -0
  88. package/dist/circle2-record.d.ts.map +1 -0
  89. package/dist/circle2-record.js +114 -0
  90. package/dist/circle2-record.js.map +1 -0
  91. package/dist/circle2.d.ts +27 -82
  92. package/dist/circle2.d.ts.map +1 -1
  93. package/dist/circle2.js +59 -104
  94. package/dist/circle2.js.map +1 -1
  95. package/dist/coordinates/cylindrical-record.d.ts +86 -0
  96. package/dist/coordinates/cylindrical-record.d.ts.map +1 -0
  97. package/dist/coordinates/cylindrical-record.js +114 -0
  98. package/dist/coordinates/cylindrical-record.js.map +1 -0
  99. package/dist/coordinates/horizontal-record.d.ts +86 -0
  100. package/dist/coordinates/horizontal-record.d.ts.map +1 -0
  101. package/dist/coordinates/horizontal-record.js +114 -0
  102. package/dist/coordinates/horizontal-record.js.map +1 -0
  103. package/dist/coordinates/horizontal.d.ts +48 -0
  104. package/dist/coordinates/horizontal.d.ts.map +1 -0
  105. package/dist/coordinates/horizontal.js +100 -0
  106. package/dist/coordinates/horizontal.js.map +1 -0
  107. package/dist/coordinates/index.d.ts +10 -0
  108. package/dist/coordinates/index.d.ts.map +1 -0
  109. package/dist/coordinates/index.js +10 -0
  110. package/dist/coordinates/index.js.map +1 -0
  111. package/dist/coordinates/longitude-latitude-record.d.ts +81 -0
  112. package/dist/coordinates/longitude-latitude-record.d.ts.map +1 -0
  113. package/dist/coordinates/longitude-latitude-record.js +104 -0
  114. package/dist/coordinates/longitude-latitude-record.js.map +1 -0
  115. package/dist/coordinates/longitude-latitude.d.ts +33 -0
  116. package/dist/coordinates/longitude-latitude.d.ts.map +1 -0
  117. package/dist/coordinates/longitude-latitude.js +78 -0
  118. package/dist/coordinates/longitude-latitude.js.map +1 -0
  119. package/dist/coordinates/polar-record.d.ts +77 -0
  120. package/dist/coordinates/polar-record.d.ts.map +1 -0
  121. package/dist/coordinates/polar-record.js +104 -0
  122. package/dist/coordinates/polar-record.js.map +1 -0
  123. package/dist/coordinates/polar.d.ts +28 -0
  124. package/dist/coordinates/polar.d.ts.map +1 -0
  125. package/dist/coordinates/polar.js +68 -0
  126. package/dist/coordinates/polar.js.map +1 -0
  127. package/dist/coordinates/spherical-cylindrical.d.ts +49 -0
  128. package/dist/coordinates/spherical-cylindrical.d.ts.map +1 -0
  129. package/dist/coordinates/spherical-cylindrical.js +110 -0
  130. package/dist/coordinates/spherical-cylindrical.js.map +1 -0
  131. package/dist/coordinates/spherical-record.d.ts +86 -0
  132. package/dist/coordinates/spherical-record.d.ts.map +1 -0
  133. package/dist/coordinates/spherical-record.js +114 -0
  134. package/dist/coordinates/spherical-record.js.map +1 -0
  135. package/dist/frustum3-record.d.ts +132 -0
  136. package/dist/frustum3-record.d.ts.map +1 -0
  137. package/dist/frustum3-record.js +333 -0
  138. package/dist/frustum3-record.js.map +1 -0
  139. package/dist/frustum3.d.ts +60 -0
  140. package/dist/frustum3.d.ts.map +1 -0
  141. package/dist/frustum3.js +186 -0
  142. package/dist/frustum3.js.map +1 -0
  143. package/dist/index.d.ts +36 -16
  144. package/dist/index.d.ts.map +1 -1
  145. package/dist/index.js +36 -16
  146. package/dist/index.js.map +1 -1
  147. package/dist/line2-record.d.ts +87 -0
  148. package/dist/line2-record.d.ts.map +1 -0
  149. package/dist/line2-record.js +123 -0
  150. package/dist/line2-record.js.map +1 -0
  151. package/dist/line2.d.ts +24 -33
  152. package/dist/line2.d.ts.map +1 -1
  153. package/dist/line2.js +78 -52
  154. package/dist/line2.js.map +1 -1
  155. package/dist/line3-record.d.ts +91 -0
  156. package/dist/line3-record.d.ts.map +1 -0
  157. package/dist/line3-record.js +146 -0
  158. package/dist/line3-record.js.map +1 -0
  159. package/dist/line3.d.ts +11 -24
  160. package/dist/line3.d.ts.map +1 -1
  161. package/dist/line3.js +32 -36
  162. package/dist/line3.js.map +1 -1
  163. package/dist/mesh/_generator-kit.d.ts +14 -0
  164. package/dist/mesh/_generator-kit.d.ts.map +1 -0
  165. package/dist/mesh/_generator-kit.js +39 -0
  166. package/dist/mesh/_generator-kit.js.map +1 -0
  167. package/dist/mesh/_index-kernels-u16.d.ts +85 -0
  168. package/dist/mesh/_index-kernels-u16.d.ts.map +1 -0
  169. package/dist/mesh/_index-kernels-u16.js +1045 -0
  170. package/dist/mesh/_index-kernels-u16.js.map +1 -0
  171. package/dist/mesh/_index-kernels-u32.d.ts +85 -0
  172. package/dist/mesh/_index-kernels-u32.d.ts.map +1 -0
  173. package/dist/mesh/_index-kernels-u32.js +1045 -0
  174. package/dist/mesh/_index-kernels-u32.js.map +1 -0
  175. package/dist/mesh/generate-box.d.ts +16 -0
  176. package/dist/mesh/generate-box.d.ts.map +1 -0
  177. package/dist/mesh/generate-box.js +79 -0
  178. package/dist/mesh/generate-box.js.map +1 -0
  179. package/dist/mesh/generate-cylinder.d.ts +25 -0
  180. package/dist/mesh/generate-cylinder.d.ts.map +1 -0
  181. package/dist/mesh/generate-cylinder.js +117 -0
  182. package/dist/mesh/generate-cylinder.js.map +1 -0
  183. package/dist/mesh/generate-heightfield.d.ts +24 -0
  184. package/dist/mesh/generate-heightfield.d.ts.map +1 -0
  185. package/dist/mesh/generate-heightfield.js +48 -0
  186. package/dist/mesh/generate-heightfield.js.map +1 -0
  187. package/dist/mesh/generate-plane.d.ts +18 -0
  188. package/dist/mesh/generate-plane.d.ts.map +1 -0
  189. package/dist/mesh/generate-plane.js +39 -0
  190. package/dist/mesh/generate-plane.js.map +1 -0
  191. package/dist/mesh/generate-sphere.d.ts +50 -0
  192. package/dist/mesh/generate-sphere.d.ts.map +1 -0
  193. package/dist/mesh/generate-sphere.js +244 -0
  194. package/dist/mesh/generate-sphere.js.map +1 -0
  195. package/dist/mesh/index.d.ts +8 -0
  196. package/dist/mesh/index.d.ts.map +1 -0
  197. package/dist/mesh/index.js +8 -0
  198. package/dist/mesh/index.js.map +1 -0
  199. package/dist/mesh/mesh3-kernels.d.ts +122 -0
  200. package/dist/mesh/mesh3-kernels.d.ts.map +1 -0
  201. package/dist/mesh/mesh3-kernels.js +220 -0
  202. package/dist/mesh/mesh3-kernels.js.map +1 -0
  203. package/dist/mesh/mesh3.d.ts +66 -0
  204. package/dist/mesh/mesh3.d.ts.map +1 -0
  205. package/dist/mesh/mesh3.js +128 -0
  206. package/dist/mesh/mesh3.js.map +1 -0
  207. package/dist/plane3-record.d.ts +88 -0
  208. package/dist/plane3-record.d.ts.map +1 -0
  209. package/dist/plane3-record.js +123 -0
  210. package/dist/plane3-record.js.map +1 -0
  211. package/dist/plane3.d.ts +43 -28
  212. package/dist/plane3.d.ts.map +1 -1
  213. package/dist/plane3.js +149 -48
  214. package/dist/plane3.js.map +1 -1
  215. package/dist/polygon2.d.ts +132 -29
  216. package/dist/polygon2.d.ts.map +1 -1
  217. package/dist/polygon2.js +529 -96
  218. package/dist/polygon2.js.map +1 -1
  219. package/dist/polygon3.d.ts +56 -21
  220. package/dist/polygon3.d.ts.map +1 -1
  221. package/dist/polygon3.js +239 -91
  222. package/dist/polygon3.js.map +1 -1
  223. package/dist/polyline2.d.ts +37 -0
  224. package/dist/polyline2.d.ts.map +1 -0
  225. package/dist/polyline2.js +146 -0
  226. package/dist/polyline2.js.map +1 -0
  227. package/dist/polyline3.d.ts +35 -0
  228. package/dist/polyline3.d.ts.map +1 -0
  229. package/dist/polyline3.js +159 -0
  230. package/dist/polyline3.js.map +1 -0
  231. package/dist/quadtree.d.ts +255 -0
  232. package/dist/quadtree.d.ts.map +1 -0
  233. package/dist/quadtree.js +1096 -0
  234. package/dist/quadtree.js.map +1 -0
  235. package/dist/ray2-record.d.ts +86 -0
  236. package/dist/ray2-record.d.ts.map +1 -0
  237. package/dist/ray2-record.js +123 -0
  238. package/dist/ray2-record.js.map +1 -0
  239. package/dist/ray2.d.ts +38 -13
  240. package/dist/ray2.d.ts.map +1 -1
  241. package/dist/ray2.js +85 -22
  242. package/dist/ray2.js.map +1 -1
  243. package/dist/ray3-array.d.ts +27 -0
  244. package/dist/ray3-array.d.ts.map +1 -0
  245. package/dist/ray3-array.js +89 -0
  246. package/dist/ray3-array.js.map +1 -0
  247. package/dist/ray3-record.d.ts +90 -0
  248. package/dist/ray3-record.d.ts.map +1 -0
  249. package/dist/ray3-record.js +146 -0
  250. package/dist/ray3-record.js.map +1 -0
  251. package/dist/ray3.d.ts +72 -25
  252. package/dist/ray3.d.ts.map +1 -1
  253. package/dist/ray3.js +233 -64
  254. package/dist/ray3.js.map +1 -1
  255. package/dist/segment2-record.d.ts +80 -0
  256. package/dist/segment2-record.d.ts.map +1 -0
  257. package/dist/segment2-record.js +122 -0
  258. package/dist/segment2-record.js.map +1 -0
  259. package/dist/segment2.d.ts +39 -19
  260. package/dist/segment2.d.ts.map +1 -1
  261. package/dist/segment2.js +104 -42
  262. package/dist/segment2.js.map +1 -1
  263. package/dist/segment3-record.d.ts +90 -0
  264. package/dist/segment3-record.d.ts.map +1 -0
  265. package/dist/segment3-record.js +146 -0
  266. package/dist/segment3-record.js.map +1 -0
  267. package/dist/segment3.d.ts +24 -12
  268. package/dist/segment3.d.ts.map +1 -1
  269. package/dist/segment3.js +67 -30
  270. package/dist/segment3.js.map +1 -1
  271. package/dist/size.d.ts +40 -0
  272. package/dist/size.d.ts.map +1 -0
  273. package/dist/size.js +72 -0
  274. package/dist/size.js.map +1 -0
  275. package/dist/sphere3-array-boundary.d.ts +51 -0
  276. package/dist/sphere3-array-boundary.d.ts.map +1 -0
  277. package/dist/sphere3-array-boundary.js +98 -0
  278. package/dist/sphere3-array-boundary.js.map +1 -0
  279. package/dist/sphere3-record.d.ts +88 -0
  280. package/dist/sphere3-record.d.ts.map +1 -0
  281. package/dist/sphere3-record.js +123 -0
  282. package/dist/sphere3-record.js.map +1 -0
  283. package/dist/sphere3.d.ts +55 -12
  284. package/dist/sphere3.d.ts.map +1 -1
  285. package/dist/sphere3.js +233 -24
  286. package/dist/sphere3.js.map +1 -1
  287. package/dist/spherical/cap.d.ts +56 -0
  288. package/dist/spherical/cap.d.ts.map +1 -0
  289. package/dist/spherical/cap.js +363 -0
  290. package/dist/spherical/cap.js.map +1 -0
  291. package/dist/spherical/great-circle.d.ts +39 -0
  292. package/dist/spherical/great-circle.d.ts.map +1 -0
  293. package/dist/spherical/great-circle.js +158 -0
  294. package/dist/spherical/great-circle.js.map +1 -0
  295. package/dist/spherical/index.d.ts +3 -0
  296. package/dist/spherical/index.d.ts.map +1 -0
  297. package/dist/spherical/index.js +3 -0
  298. package/dist/spherical/index.js.map +1 -0
  299. package/dist/triangle2-record.d.ts +91 -0
  300. package/dist/triangle2-record.d.ts.map +1 -0
  301. package/dist/triangle2-record.js +146 -0
  302. package/dist/triangle2-record.js.map +1 -0
  303. package/dist/triangle2.d.ts +29 -14
  304. package/dist/triangle2.d.ts.map +1 -1
  305. package/dist/triangle2.js +86 -22
  306. package/dist/triangle2.js.map +1 -1
  307. package/dist/triangle3-record.d.ts +97 -0
  308. package/dist/triangle3-record.d.ts.map +1 -0
  309. package/dist/triangle3-record.js +178 -0
  310. package/dist/triangle3-record.js.map +1 -0
  311. package/dist/triangle3.d.ts +60 -17
  312. package/dist/triangle3.d.ts.map +1 -1
  313. package/dist/triangle3.js +287 -38
  314. package/dist/triangle3.js.map +1 -1
  315. package/package.json +15 -8
  316. package/dist/angles.d.ts +0 -31
  317. package/dist/angles.d.ts.map +0 -1
  318. package/dist/angles.js +0 -53
  319. package/dist/angles.js.map +0 -1
  320. package/dist/coordinate-spaces.d.ts +0 -39
  321. package/dist/coordinate-spaces.d.ts.map +0 -1
  322. package/dist/coordinate-spaces.js +0 -52
  323. package/dist/coordinate-spaces.js.map +0 -1
  324. package/dist/coordinates-vec2.d.ts +0 -7
  325. package/dist/coordinates-vec2.d.ts.map +0 -1
  326. package/dist/coordinates-vec2.js +0 -35
  327. package/dist/coordinates-vec2.js.map +0 -1
  328. package/dist/coordinates-vec3.d.ts +0 -11
  329. package/dist/coordinates-vec3.d.ts.map +0 -1
  330. package/dist/coordinates-vec3.js +0 -61
  331. package/dist/coordinates-vec3.js.map +0 -1
  332. package/dist/coordinates.d.ts +0 -54
  333. package/dist/coordinates.d.ts.map +0 -1
  334. package/dist/coordinates.js +0 -2
  335. package/dist/coordinates.js.map +0 -1
  336. package/dist/euler-angles-quaternion.d.ts +0 -7
  337. package/dist/euler-angles-quaternion.d.ts.map +0 -1
  338. package/dist/euler-angles-quaternion.js +0 -100
  339. package/dist/euler-angles-quaternion.js.map +0 -1
  340. package/dist/euler-angles.d.ts +0 -33
  341. package/dist/euler-angles.d.ts.map +0 -1
  342. package/dist/euler-angles.js +0 -35
  343. package/dist/euler-angles.js.map +0 -1
  344. package/dist/geometry-vectors.d.ts +0 -11
  345. package/dist/geometry-vectors.d.ts.map +0 -1
  346. package/dist/geometry-vectors.js +0 -19
  347. package/dist/geometry-vectors.js.map +0 -1
  348. package/dist/mesh3.d.ts +0 -32
  349. package/dist/mesh3.d.ts.map +0 -1
  350. package/dist/mesh3.js +0 -142
  351. package/dist/mesh3.js.map +0 -1
  352. package/dist/point2.d.ts +0 -41
  353. package/dist/point2.d.ts.map +0 -1
  354. package/dist/point2.js +0 -92
  355. package/dist/point2.js.map +0 -1
  356. package/dist/point3.d.ts +0 -43
  357. package/dist/point3.d.ts.map +0 -1
  358. package/dist/point3.js +0 -103
  359. package/dist/point3.js.map +0 -1
  360. package/dist/point4.d.ts +0 -12
  361. package/dist/point4.d.ts.map +0 -1
  362. package/dist/point4.js +0 -28
  363. package/dist/point4.js.map +0 -1
  364. package/dist/rect2.d.ts +0 -54
  365. package/dist/rect2.d.ts.map +0 -1
  366. package/dist/rect2.js +0 -149
  367. package/dist/rect2.js.map +0 -1
  368. package/dist/size2.d.ts +0 -33
  369. package/dist/size2.d.ts.map +0 -1
  370. package/dist/size2.js +0 -81
  371. package/dist/size2.js.map +0 -1
  372. package/dist/size3.d.ts +0 -21
  373. package/dist/size3.d.ts.map +0 -1
  374. package/dist/size3.js +0 -59
  375. package/dist/size3.js.map +0 -1
  376. package/dist/size4.d.ts +0 -10
  377. package/dist/size4.d.ts.map +0 -1
  378. package/dist/size4.js +0 -16
  379. package/dist/size4.js.map +0 -1
@@ -0,0 +1,1045 @@
1
+ // Generated from scripts/templates/index-kernels.liquid by scripts/generate-index-kernels.ts. Do not edit.
2
+ import { binaryExponent, PreciseSum, scaleByPowerOfTwoInPlace, } from "@ac-kit/core";
3
+ import { vec2Create, vec3Create, } from "@ac-kit/math-linear";
4
+ import { DotExactAccumulator, twoProductInRangeInPlace, twoSumInPlace, } from "@ac-kit/math-scalar";
5
+ import { triangle3Cross_, writeScaledComponents_, } from "../_triangle3-cross.js";
6
+ import { ray3IntersectTriangle3 } from "../ray3.js";
7
+ import { triangle3Create } from "../triangle3-record.js";
8
+ import { triangle3Normal } from "../triangle3.js";
9
+ // Scratch values, made by their creators and never handed out (S23).
10
+ const TRIANGLE_ = triangle3Create();
11
+ /** A face's corners a, b and c, for `triangle3Cross_`. */
12
+ const CORNERS_ = new Float64Array(9);
13
+ const BARYCENTRIC_ = vec2Create();
14
+ const NORMAL_ = vec3Create();
15
+ const AREA_ = new PreciseSum();
16
+ const SIX_VOLUMES_ = new PreciseSum();
17
+ /** Three times the total weight, then the sums of its products with x, y and z. */
18
+ const CENTROID_SUMS_ = [
19
+ new PreciseSum(),
20
+ new PreciseSum(),
21
+ new PreciseSum(),
22
+ new PreciseSum(),
23
+ ];
24
+ /**
25
+ * The frame of the area, volume and centroid kernels, per axis: the anchor
26
+ * (0–2), the exponent e of the axis's scale 2^−e (3–5), the factor a difference
27
+ * along the axis is multiplied by (6–8: 2^−e, or 2^(1−e) where the halves are
28
+ * subtracted), 1 when some extent is so wide that a difference of two
29
+ * coordinates may overflow (9), and a second factor where the first alone would
30
+ * leave the doubles (10–12, otherwise 1).
31
+ */
32
+ const FRAME_ = new Float64Array(13);
33
+ /** A corner relative to the anchor, scaled: x, y, z, then their low parts. */
34
+ const VERTEX_ = new Float64Array(6);
35
+ /**
36
+ * One face's doubled area as `[m, k]`, the area being m · 2^k: k is 0 when m is
37
+ * the area itself, otherwise m is between 1 and 4 (0 for a degenerate face,
38
+ * `NaN` for a non-finite one).
39
+ */
40
+ const FACE_AREA_ = new Float64Array(2);
41
+ /** One face's cross product `[x, y, z, k]`, the product being (x, y, z) · 2^k. */
42
+ const FACE_CROSS_ = new Float64Array(4);
43
+ /** A double computed and scaled in place, never boxed by a call. */
44
+ const VALUE_ = new Float64Array(1);
45
+ /** A face's scaled edges `b − a` and `c − a`. */
46
+ const EDGES_ = new Float64Array(6);
47
+ /** The exponents bounding a face's `a − o`, `b − a` and `c − a`. */
48
+ const BOUNDS_ = new Float64Array(3);
49
+ /** Two numbers TwoSum replaces by their sum and its error. */
50
+ const PAIR_ = new Float64Array(2);
51
+ /** The volume filter's operands, TwoSum and TwoProduct pairs in place. */
52
+ const FILTER_ = new Float64Array(36);
53
+ const CROSS_ = new Float64Array(12);
54
+ const TERMS_ = new Float64Array(48);
55
+ /**
56
+ * The accuracy a vertex's sum must be certified to: each component's bound
57
+ * within 2^−50 of the largest component, so that the unit normal stays within
58
+ * 2^−48 of the exact sum's direction.
59
+ */
60
+ const VERTEX_ACCURACY_ = 2 ** -50;
61
+ /**
62
+ * The bound, in units of u, on what a scaled product loses underflowing (each
63
+ * term below 2^−1070), far above it.
64
+ */
65
+ const SCALED_TERM_BOUND_ = 2 ** -1000;
66
+ /** The least exponent of a term a `DotExactAccumulator` holds. */
67
+ const EXACT_TERM_MIN_EXPONENT_ = -2166;
68
+ /** The exact sums of a vertex's components. */
69
+ const EXACT_SUMS_ = [
70
+ new DotExactAccumulator(),
71
+ new DotExactAccumulator(),
72
+ new DotExactAccumulator(),
73
+ ];
74
+ /** A term added to an exact sum, as a one-product dot product. */
75
+ const TERM_ = new Float64Array(1);
76
+ const ONE_ = Float64Array.of(1);
77
+ /** An exact sum, as significand and exponent. */
78
+ const SCALED_ = new Float64Array(2);
79
+ /** A vertex's exact sum, each `SUM_COMPONENTS_[i] · 2^SUM_EXPONENTS_[i]`. */
80
+ const SUM_COMPONENTS_ = new Float64Array(3);
81
+ const SUM_EXPONENTS_ = new Int32Array(3);
82
+ /** A vertex's exact sum as `[x, y, z, k]`. */
83
+ const VERTEX_SUM_ = new Float64Array(4);
84
+ /**
85
+ * The `Uint16Array` twin of the area-weighted vertex normals: the public
86
+ * function checks the lengths and dispatches on the index type once. Writes
87
+ * `positions.length` numbers into `out`. Each face adds its cross product,
88
+ * twice its area times its unit normal (`faceCross_`: within 2^−49 of its
89
+ * largest component, zero only for a degenerate face). While every face's
90
+ * product is plain (k = 0, its largest component in [2^−400, 2^402)), the sums
91
+ * are those products' as they come; otherwise a first pass finds, per vertex,
92
+ * the largest product among its faces, and each face adds its product scaled by
93
+ * the power of two that brings that vertex's largest to [1, 2): the weights
94
+ * keep their ratios, nothing overflows, and a vertex whose faces are all tiny
95
+ * beside other vertices' still gets its normal. Each sum keeps a running bound
96
+ * on its rounding errors (Higham, "Accuracy and Stability of Numerical
97
+ * Algorithms", §4.2: within u/(1 − u) of the partial sums' magnitudes, u =
98
+ * 2^−53); a vertex whose bound is not within 2^−50 of its largest component
99
+ * (its faces cancel, or it has very many) is summed again exactly
100
+ * (`exactVertexSums_`). O(vertices + triangles); no allocation once the
101
+ * per-vertex scratch has grown to the largest mesh.
102
+ */
103
+ export function mesh3VertexNormalsU16(out, positions, indices) {
104
+ const cross = FACE_CROSS_;
105
+ const vertexCount = out.length / 3;
106
+ if (vertexErrorsU16_.length < vertexCount) {
107
+ vertexErrorsU16_ = new Float64Array(Math.max(vertexCount, 2 * vertexErrorsU16_.length));
108
+ }
109
+ const errors = vertexErrorsU16_;
110
+ errors.fill(0, 0, vertexCount);
111
+ out.fill(0);
112
+ let isPlain = true;
113
+ for (let corner = 0; corner < indices.length; corner += 3) {
114
+ const vertexA = indices[corner];
115
+ const vertexB = indices[corner + 1];
116
+ const vertexC = indices[corner + 2];
117
+ const a = vertexA * 3;
118
+ const b = vertexB * 3;
119
+ const c = vertexC * 3;
120
+ faceCrossU16_(positions, a, b, c);
121
+ if (cross[3] !== 0) {
122
+ isPlain = false;
123
+ break;
124
+ }
125
+ const crossX = cross[0];
126
+ const crossY = cross[1];
127
+ const crossZ = cross[2];
128
+ // An index past the end reads NaN and writes nothing (or NaN in the
129
+ // scratch's unused tail).
130
+ const sumAX = out[a] + crossX;
131
+ const sumAY = out[a + 1] + crossY;
132
+ const sumAZ = out[a + 2] + crossZ;
133
+ out[a] = sumAX;
134
+ out[a + 1] = sumAY;
135
+ out[a + 2] = sumAZ;
136
+ errors[vertexA] =
137
+ errors[vertexA] +
138
+ Math.max(Math.abs(sumAX), Math.abs(sumAY), Math.abs(sumAZ));
139
+ const sumBX = out[b] + crossX;
140
+ const sumBY = out[b + 1] + crossY;
141
+ const sumBZ = out[b + 2] + crossZ;
142
+ out[b] = sumBX;
143
+ out[b + 1] = sumBY;
144
+ out[b + 2] = sumBZ;
145
+ errors[vertexB] =
146
+ errors[vertexB] +
147
+ Math.max(Math.abs(sumBX), Math.abs(sumBY), Math.abs(sumBZ));
148
+ const sumCX = out[c] + crossX;
149
+ const sumCY = out[c + 1] + crossY;
150
+ const sumCZ = out[c + 2] + crossZ;
151
+ out[c] = sumCX;
152
+ out[c + 1] = sumCY;
153
+ out[c + 2] = sumCZ;
154
+ errors[vertexC] =
155
+ errors[vertexC] +
156
+ Math.max(Math.abs(sumCX), Math.abs(sumCY), Math.abs(sumCZ));
157
+ }
158
+ if (!isPlain) {
159
+ scaledVertexSumsU16_(out, positions, indices);
160
+ }
161
+ // u / (1 − u) per partial sum, widened by 4nu for the bound's own
162
+ // roundings over the n numbers it adds.
163
+ const factor = 1.0001 * UNIT_ROUNDOFF_ * (1 + 4 * indices.length * UNIT_ROUNDOFF_);
164
+ let uncertain = 0;
165
+ for (let vertex = 0; vertex < vertexCount; vertex++) {
166
+ const at = 3 * vertex;
167
+ const largest = Math.max(Math.abs(out[at]), Math.abs(out[at + 1]), Math.abs(out[at + 2]));
168
+ if (errors[vertex] * factor > largest * VERTEX_ACCURACY_) {
169
+ errors[vertex] = -1;
170
+ uncertain++;
171
+ continue;
172
+ }
173
+ normalizeVertex_(out, at);
174
+ }
175
+ if (uncertain > 0) {
176
+ exactVertexSumsU16_(out, positions, indices);
177
+ }
178
+ }
179
+ /**
180
+ * Per vertex, the sum of the largest magnitudes of {@link mesh3VertexNormals}'s
181
+ * partial sums, the bound on every component's rounding errors in units of u,
182
+ * then −1 where that bound is too wide: grown to the largest mesh seen, then
183
+ * reused.
184
+ */
185
+ let vertexErrorsU16_ = new Float64Array(0);
186
+ /**
187
+ * Per vertex, the exponent of the largest cross product among its faces, for
188
+ * {@link scaledVertexSums_}: grown to the largest mesh seen, then reused.
189
+ */
190
+ let vertexExponentsU16_ = new Int32Array(0);
191
+ /**
192
+ * Rewrites `out` with each vertex's sum of its faces' cross products, every
193
+ * product scaled by the power of two that brings the vertex's largest to [1,
194
+ * 2): the same direction as the plain sum, with no product that overflows or
195
+ * underflows on the way. Rewrites `vertexErrors_` with the bound on the sums'
196
+ * roundings, an absolute 2^−1000 per term added for what a scaled product loses
197
+ * underflowing (each is below 2^−1070).
198
+ */
199
+ function scaledVertexSumsU16_(out, positions, indices) {
200
+ const cross = FACE_CROSS_;
201
+ const vertexCount = out.length / 3;
202
+ if (vertexExponentsU16_.length < vertexCount) {
203
+ vertexExponentsU16_ = new Int32Array(Math.max(vertexCount, 2 * vertexExponentsU16_.length));
204
+ }
205
+ const exponents = vertexExponentsU16_;
206
+ const errors = vertexErrorsU16_;
207
+ exponents.fill(NONE_, 0, vertexCount);
208
+ for (let face = 0; face < indices.length; face += 3) {
209
+ faceCrossU16_(positions, indices[face] * 3, indices[face + 1] * 3, indices[face + 2] * 3);
210
+ const largest = Math.max(Math.abs(cross[0]), Math.abs(cross[1]), Math.abs(cross[2]));
211
+ if (largest > 0 && largest < Number.POSITIVE_INFINITY) {
212
+ const exponent = cross[3] + binaryExponent(largest);
213
+ for (let corner = face; corner < face + 3; corner++) {
214
+ // A corner past the last vertex reads and writes nothing.
215
+ const vertex = indices[corner];
216
+ if (vertex < vertexCount) {
217
+ exponents[vertex] = Math.max(exponents[vertex], exponent);
218
+ }
219
+ }
220
+ }
221
+ }
222
+ out.fill(0);
223
+ errors.fill(0, 0, vertexCount);
224
+ for (let face = 0; face < indices.length; face += 3) {
225
+ faceCrossU16_(positions, indices[face] * 3, indices[face + 1] * 3, indices[face + 2] * 3);
226
+ const largest = Math.max(Math.abs(cross[0]), Math.abs(cross[1]), Math.abs(cross[2]));
227
+ if (largest === 0) {
228
+ continue;
229
+ }
230
+ for (let corner = face; corner < face + 3; corner++) {
231
+ const vertex = indices[corner];
232
+ if (vertex >= vertexCount) {
233
+ continue;
234
+ }
235
+ // A NaN product stays NaN; a product 2^1080 below the vertex's largest
236
+ // weighs nothing beside it.
237
+ const factor = largest < Number.POSITIVE_INFINITY
238
+ ? 2 ** Math.max(cross[3] - exponents[vertex], -1080)
239
+ : 1;
240
+ const at = vertex * 3;
241
+ out[at] = out[at] + cross[0] * factor;
242
+ out[at + 1] = out[at + 1] + cross[1] * factor;
243
+ out[at + 2] = out[at + 2] + cross[2] * factor;
244
+ errors[vertex] =
245
+ errors[vertex] +
246
+ Math.max(Math.abs(out[at]), Math.abs(out[at + 1]), Math.abs(out[at + 2])) +
247
+ SCALED_TERM_BOUND_;
248
+ }
249
+ }
250
+ }
251
+ /**
252
+ * Per vertex, where its faces start in `vertexFaces_`, for
253
+ * {@link exactVertexSums_}: grown to the largest mesh seen, then reused.
254
+ */
255
+ let vertexStartsU16_ = new Int32Array(0);
256
+ /** The faces (their first corner) of each vertex summed exactly, in turn. */
257
+ let vertexFacesU16_ = new Int32Array(0);
258
+ /**
259
+ * Rewrites, at each vertex `vertexErrors_` marks with −1, the unit normal of
260
+ * the exact sum of its faces' cross products (`faceCross_`'s, a face counted
261
+ * once per corner it has there), each component summed by a long accumulator
262
+ * and rounded once (`DotExactAccumulator`), at any exponent; the zero vector
263
+ * where that sum is zero. A component below 2^−2166, which no accumulator
264
+ * holds, is 2^1040 below its face's largest and well within that product's own
265
+ * bound, and is left out. The faces of those vertices are listed first
266
+ * (counted, then placed), so each is read once per corner at a marked vertex.
267
+ */
268
+ function exactVertexSumsU16_(out, positions, indices) {
269
+ const vertexCount = out.length / 3;
270
+ const errors = vertexErrorsU16_;
271
+ if (vertexStartsU16_.length < vertexCount + 2) {
272
+ vertexStartsU16_ = new Int32Array(Math.max(vertexCount + 2, 2 * vertexStartsU16_.length));
273
+ }
274
+ const starts = vertexStartsU16_;
275
+ starts.fill(0, 0, vertexCount + 2);
276
+ for (let corner = 0; corner < indices.length; corner++) {
277
+ const vertex = indices[corner];
278
+ if (vertex < vertexCount && errors[vertex] === -1) {
279
+ starts[vertex + 1] = starts[vertex + 1] + 1;
280
+ }
281
+ }
282
+ // Each vertex's end, then, placing its faces backwards, its start.
283
+ for (let vertex = 0; vertex < vertexCount; vertex++) {
284
+ starts[vertex + 1] = starts[vertex + 1] + starts[vertex];
285
+ }
286
+ const total = starts[vertexCount];
287
+ if (vertexFacesU16_.length < total) {
288
+ vertexFacesU16_ = new Int32Array(Math.max(total, 2 * vertexFacesU16_.length));
289
+ }
290
+ const faces = vertexFacesU16_;
291
+ for (let corner = 0; corner < indices.length; corner++) {
292
+ const vertex = indices[corner];
293
+ if (vertex < vertexCount && errors[vertex] === -1) {
294
+ const at = starts[vertex + 1] - 1;
295
+ starts[vertex + 1] = at;
296
+ faces[at] = corner - (corner % 3);
297
+ }
298
+ }
299
+ // Now vertex v's faces are faces[starts[v + 1]] to faces[starts[v + 2]].
300
+ starts[vertexCount + 1] = total;
301
+ const cross = FACE_CROSS_;
302
+ for (let vertex = 0; vertex < vertexCount; vertex++) {
303
+ if (errors[vertex] !== -1) {
304
+ continue;
305
+ }
306
+ EXACT_SUMS_[0].reset();
307
+ EXACT_SUMS_[1].reset();
308
+ EXACT_SUMS_[2].reset();
309
+ const end = starts[vertex + 2];
310
+ for (let at = starts[vertex + 1]; at < end; at++) {
311
+ const face = faces[at];
312
+ faceCrossU16_(positions, indices[face] * 3, indices[face + 1] * 3, indices[face + 2] * 3);
313
+ const exponent = cross[3];
314
+ for (let component = 0; component < 3; component++) {
315
+ const value = cross[component];
316
+ if (value !== 0 &&
317
+ binaryExponent(value) + exponent >= EXACT_TERM_MIN_EXPONENT_) {
318
+ TERM_[0] = value;
319
+ EXACT_SUMS_[component].addProducts(TERM_, ONE_, exponent);
320
+ }
321
+ }
322
+ }
323
+ for (let component = 0; component < 3; component++) {
324
+ EXACT_SUMS_[component].writeScaled(SCALED_, 0);
325
+ SUM_COMPONENTS_[component] = SCALED_[0];
326
+ SUM_EXPONENTS_[component] = SCALED_[1];
327
+ }
328
+ writeScaledComponents_(VERTEX_SUM_, SUM_COMPONENTS_, SUM_EXPONENTS_);
329
+ const at = 3 * vertex;
330
+ out[at] = VERTEX_SUM_[0];
331
+ out[at + 1] = VERTEX_SUM_[1];
332
+ out[at + 2] = VERTEX_SUM_[2];
333
+ normalizeVertex_(out, at);
334
+ }
335
+ }
336
+ /**
337
+ * Replaces the vector at `out[at]` by its direction: unchanged when it is zero,
338
+ * `NaN`s when a component is.
339
+ */
340
+ function normalizeVertex_(out, at) {
341
+ const x = out[at];
342
+ const y = out[at + 1];
343
+ const z = out[at + 2];
344
+ const length = Math.sqrt(x * x + y * y + z * z);
345
+ if (length > 0 && length < Number.POSITIVE_INFINITY) {
346
+ const inverse = 1 / length;
347
+ out[at] = x * inverse;
348
+ out[at + 1] = y * inverse;
349
+ out[at + 2] = z * inverse;
350
+ return;
351
+ }
352
+ // The squares underflowed or overflowed, or the vertex has no normal.
353
+ const scale = Math.max(Math.abs(x), Math.abs(y), Math.abs(z));
354
+ if (scale === 0) {
355
+ return;
356
+ }
357
+ const scaledX = x / scale;
358
+ const scaledY = y / scale;
359
+ const scaledZ = z / scale;
360
+ const scaledLength = Math.sqrt(scaledX * scaledX + scaledY * scaledY + scaledZ * scaledZ);
361
+ out[at] = scaledX / scaledLength;
362
+ out[at + 1] = scaledY / scaledLength;
363
+ out[at + 2] = scaledZ / scaledLength;
364
+ }
365
+ /**
366
+ * The `Uint16Array` twin of the face normals: one unit normal per triangle, the
367
+ * zero vector for a degenerate one. O(triangles).
368
+ */
369
+ export function mesh3FaceNormalsU16(out, positions, indices) {
370
+ for (let corner = 0; corner < indices.length; corner += 3) {
371
+ const a = indices[corner] * 3;
372
+ const b = indices[corner + 1] * 3;
373
+ const c = indices[corner + 2] * 3;
374
+ TRIANGLE_[0] = +positions[a];
375
+ TRIANGLE_[1] = +positions[a + 1];
376
+ TRIANGLE_[2] = +positions[a + 2];
377
+ TRIANGLE_[3] = +positions[b];
378
+ TRIANGLE_[4] = +positions[b + 1];
379
+ TRIANGLE_[5] = +positions[b + 2];
380
+ TRIANGLE_[6] = +positions[c];
381
+ TRIANGLE_[7] = +positions[c + 1];
382
+ TRIANGLE_[8] = +positions[c + 2];
383
+ triangle3Normal(NORMAL_, TRIANGLE_);
384
+ out[corner] = NORMAL_[0];
385
+ out[corner + 1] = NORMAL_[1];
386
+ out[corner + 2] = NORMAL_[2];
387
+ }
388
+ }
389
+ /** An exponent below every one a face can have: "no difference". */
390
+ const NONE_ = -4096;
391
+ /**
392
+ * Fills `FRAME_` for the triangles of `indices`. The anchor is the least x, y
393
+ * and z of the vertices they use: a corner of their bounding box, the same in
394
+ * every face order, and a vertex no triangle uses is never read. Each axis has
395
+ * its own e, 2^e above the box's extent along it (0 for a flat or non-finite
396
+ * one), at least −1021 so that 2^−e is a double: a corner relative to the
397
+ * anchor, scaled by its axis's 2^−e, lies in [0, 1) on every axis however thin
398
+ * the box is along the others. When an extent reaches 2^1023, so that a
399
+ * difference of two coordinates may overflow, every difference subtracts
400
+ * halves, which is exact for normal coordinates.
401
+ */
402
+ function frameU16_(positions, indices) {
403
+ let minX = Number.POSITIVE_INFINITY;
404
+ let minY = Number.POSITIVE_INFINITY;
405
+ let minZ = Number.POSITIVE_INFINITY;
406
+ let maxX = Number.NEGATIVE_INFINITY;
407
+ let maxY = Number.NEGATIVE_INFINITY;
408
+ let maxZ = Number.NEGATIVE_INFINITY;
409
+ for (let corner = 0; corner < indices.length; corner++) {
410
+ const vertex = indices[corner] * 3;
411
+ const x = positions[vertex];
412
+ const y = positions[vertex + 1];
413
+ const z = positions[vertex + 2];
414
+ minX = Math.min(minX, x);
415
+ minY = Math.min(minY, y);
416
+ minZ = Math.min(minZ, z);
417
+ maxX = Math.max(maxX, x);
418
+ maxY = Math.max(maxY, y);
419
+ maxZ = Math.max(maxZ, z);
420
+ }
421
+ // Halves, so that the extent of a box across ±Number.MAX_VALUE is finite.
422
+ const halfX = maxX * 0.5 - minX * 0.5;
423
+ const halfY = maxY * 0.5 - minY * 0.5;
424
+ const halfZ = maxZ * 0.5 - minZ * 0.5;
425
+ const frame = FRAME_;
426
+ frame[0] = minX;
427
+ frame[1] = minY;
428
+ frame[2] = minZ;
429
+ frame[3] = halfX;
430
+ frame[4] = halfY;
431
+ frame[5] = halfZ;
432
+ frame[9] =
433
+ halfX >= 2 ** 1022 || halfY >= 2 ** 1022 || halfZ >= 2 ** 1022 ? 1 : 0;
434
+ for (let axis = 3; axis < 6; axis++) {
435
+ // The half extent becomes e: 2^e is above the extent.
436
+ const half = frame[axis];
437
+ frame[axis] =
438
+ half > 0 && half < Number.POSITIVE_INFINITY
439
+ ? Math.max(binaryExponent(half) + 2, -1021)
440
+ : 0;
441
+ }
442
+ scaleFrameU16_(0);
443
+ }
444
+ /**
445
+ * Sets the factors of `FRAME_` to 2^−(e + extra) per axis, 2^(1 − e − extra)
446
+ * where halves are subtracted. `extra` is 0 or the volume's common rescaling,
447
+ * never so far below −e that the factor leaves the doubles: from −1020 to 0.
448
+ */
449
+ function scaleFrameU16_(extra) {
450
+ const shift = FRAME_[9] === 1 ? 1 : 0;
451
+ for (let axis = 0; axis < 3; axis++) {
452
+ // Beyond 2^1023 the factor is two: only an axis whose differences are
453
+ // all below 1 gets there, and those stay finite after the first.
454
+ const exponent = shift - FRAME_[3 + axis] - extra;
455
+ FRAME_[6 + axis] = 2 ** Math.min(exponent, 1023);
456
+ FRAME_[10 + axis] = 2 ** Math.max(exponent - 1023, 0);
457
+ }
458
+ }
459
+ /**
460
+ * Writes the edges `b − a` (0–2) and `c − a` (3–5) of a face into `EDGES_`,
461
+ * scaled by the frame's factors: exact where the differences are (Sterbenz) and
462
+ * the results are not subnormal. When the frame is wide, `p − q` may overflow,
463
+ * so the halves are subtracted, which is exact for every normal `p` and `q`. No
464
+ * double crosses the call, so it allocates nothing, inlined or not.
465
+ */
466
+ function scaledEdgesU16_(positions, a, b, c, isWide) {
467
+ const frame = FRAME_;
468
+ const edges = EDGES_;
469
+ for (let axis = 0; axis < 3; axis++) {
470
+ const factor = frame[6 + axis];
471
+ const second = frame[10 + axis];
472
+ const p = positions[a + axis];
473
+ const q = positions[b + axis];
474
+ const r = positions[c + axis];
475
+ edges[axis] = (isWide ? q * 0.5 - p * 0.5 : q - p) * factor * second;
476
+ edges[axis + 3] = (isWide ? r * 0.5 - p * 0.5 : r - p) * factor * second;
477
+ }
478
+ }
479
+ /**
480
+ * Writes into `BOUNDS_` the exponents, as {@link binaryExponent} reads them, of
481
+ * the largest component of `a − o`, `b − a` and `c − a` scaled into the frame
482
+ * (`NONE_` for a zero or non-finite vector): each is below 2^(exponent + 1).
483
+ */
484
+ function faceBoundsU16_(positions, a, b, c, isWide) {
485
+ const frame = FRAME_;
486
+ const bounds = BOUNDS_;
487
+ const shift = isWide ? 1 : 0;
488
+ bounds[0] = NONE_;
489
+ bounds[1] = NONE_;
490
+ bounds[2] = NONE_;
491
+ for (let axis = 0; axis < 3; axis++) {
492
+ const p = positions[a + axis];
493
+ const anchored = isWide ? p * 0.5 - frame[axis] * 0.5 : p - frame[axis];
494
+ const ab = isWide
495
+ ? positions[b + axis] * 0.5 - p * 0.5
496
+ : positions[b + axis] - p;
497
+ const ac = isWide
498
+ ? positions[c + axis] * 0.5 - p * 0.5
499
+ : positions[c + axis] - p;
500
+ const offset = shift - frame[3 + axis];
501
+ if (anchored !== 0 && anchored - anchored === 0) {
502
+ bounds[0] = Math.max(bounds[0], binaryExponent(anchored) + offset);
503
+ }
504
+ if (ab !== 0 && ab - ab === 0) {
505
+ bounds[1] = Math.max(bounds[1], binaryExponent(ab) + offset);
506
+ }
507
+ if (ac !== 0 && ac - ac === 0) {
508
+ bounds[2] = Math.max(bounds[2], binaryExponent(ac) + offset);
509
+ }
510
+ }
511
+ }
512
+ /**
513
+ * Writes the corner at `vertex` relative to the anchor, scaled by the frame's
514
+ * factors, into `VERTEX_` as high parts (0–2) and the low parts TwoSum recovers
515
+ * (3–5): their sum is the scaled difference exactly, wherever it is not
516
+ * subnormal.
517
+ */
518
+ function anchoredCornerU16_(positions, vertex, isWide) {
519
+ const pair = PAIR_;
520
+ for (let axis = 0; axis < 3; axis++) {
521
+ const factor = FRAME_[6 + axis];
522
+ const second = FRAME_[10 + axis];
523
+ pair[0] = isWide
524
+ ? positions[vertex + axis] * 0.5
525
+ : positions[vertex + axis];
526
+ pair[1] = isWide ? -(FRAME_[axis] * 0.5) : -FRAME_[axis];
527
+ twoSumInPlace(pair, 0);
528
+ VERTEX_[axis] = pair[0] * factor * second;
529
+ VERTEX_[axis + 3] = pair[1] * factor * second;
530
+ }
531
+ }
532
+ /**
533
+ * Writes the cross product `(b − a) × (c − a)` of the triangle at `a`, `b` and
534
+ * `c` into `FACE_CROSS_` as `[x, y, z, k]`, the true product being `(x, y, z) ·
535
+ * 2^k` (`triangle3Cross_`): each component within 2^−49 of the largest of the
536
+ * exact product, with its sign wherever that bound cannot reach 0, for any
537
+ * finite corners, zeros exactly for a degenerate face and `NaN`s for a
538
+ * non-finite one. k is 0 while the largest component lies in [2^−400, 2^402);
539
+ * otherwise the largest lies in [1, 2).
540
+ */
541
+ function faceCrossU16_(positions, a, b, c) {
542
+ const corners = CORNERS_;
543
+ corners[0] = +positions[a];
544
+ corners[1] = +positions[a + 1];
545
+ corners[2] = +positions[a + 2];
546
+ corners[3] = +positions[b];
547
+ corners[4] = +positions[b + 1];
548
+ corners[5] = +positions[b + 2];
549
+ corners[6] = +positions[c];
550
+ corners[7] = +positions[c + 1];
551
+ corners[8] = +positions[c + 2];
552
+ triangle3Cross_(FACE_CROSS_, corners);
553
+ }
554
+ /**
555
+ * Writes the doubled area of the triangle at `a`, `b` and `c` into `FACE_AREA_`
556
+ * as `[m, k]`, the area being m · 2^k: the length of {@link faceCross_}'s
557
+ * product, whose components never square out of range.
558
+ */
559
+ function faceAreaU16_(positions, a, b, c) {
560
+ faceCrossU16_(positions, a, b, c);
561
+ const cross = FACE_CROSS_;
562
+ const x = cross[0];
563
+ const y = cross[1];
564
+ const z = cross[2];
565
+ FACE_AREA_[0] = Math.sqrt(x * x + y * y + z * z);
566
+ FACE_AREA_[1] = cross[3];
567
+ }
568
+ /**
569
+ * The `Uint16Array` twin of the surface area: each face's area from its edges
570
+ * and cross product scaled by powers of two (`faceArea_`), so no square
571
+ * overflows or underflows at any size or distance from the origin, rounded once
572
+ * per face and summed exactly: the same number in every face order.
573
+ * O(triangles), no allocation.
574
+ */
575
+ export function mesh3SurfaceAreaU16(positions, indices) {
576
+ const sum = AREA_;
577
+ const area = FACE_AREA_;
578
+ const value = VALUE_;
579
+ sum.reset();
580
+ for (let corner = 0; corner < indices.length; corner += 3) {
581
+ faceAreaU16_(positions, indices[corner] * 3, indices[corner + 1] * 3, indices[corner + 2] * 3);
582
+ if (area[0] === 0) {
583
+ continue;
584
+ }
585
+ value[0] = area[0];
586
+ if (area[1] === 0) {
587
+ value[0] = area[0] * 0.5;
588
+ }
589
+ else {
590
+ scaleByPowerOfTwoInPlace(value, 0, area[1] - 1);
591
+ }
592
+ sum.addRange(value, 0, 1);
593
+ }
594
+ sum.writeValue(value, 0);
595
+ // `+ 0`: an empty sum is −0, as `Math.sumPrecise`'s.
596
+ return value[0] + 0;
597
+ }
598
+ /** The unit roundoff, 2^−53. */
599
+ const UNIT_ROUNDOFF_ = 2 ** -53;
600
+ /**
601
+ * The adaptive filter of {@link mesh3Volume}: six times the volume, over the
602
+ * same terms as the exact sum (`a − o` exactly as TwoSum's pair, the edges as
603
+ * rounded), in doubled precision. Each cross component is the difference of two
604
+ * TwoProducts with their errors; each term's high products are TwoProducts too
605
+ * and summed by TwoSum, and every remaining part goes into one plain sum. What
606
+ * that drops is bounded rigorously: per face, below 12u² Σ |a − o|ₖ (|p|
607
+ *
608
+ * - |q|)ₖ over the products p, q of each cross component (u = 2^−53, the
609
+ * neglected low-by-low products and the roundings of the low parts), plus the
610
+ * plain sum's rounding, γ_N Σ |parts| for its N parts (Higham, "Accuracy and
611
+ * Stability of Numerical Algorithms", §4.2), plus an absolute 2^−1066 per
612
+ * face for products in the subnormal range. When the rounded total r is
613
+ * normal and its distance to the double sum, plus that bound, stays short of
614
+ * the midpoint on that side (half its unit in the last place, a quarter below
615
+ * a power of two), the exact value rounds to r: written to `VALUE_[0]`, and
616
+ * `true`. Otherwise `false`, and the exact sum decides. The frame must be set
617
+ * and no extent may reach 2^300, so no product overflows.
618
+ */
619
+ function certifiedSixVolumeU16_(positions, indices) {
620
+ // Every TwoSum and TwoProduct runs in place on this buffer: no double
621
+ // crosses a call, so nothing is boxed whether or not the engine inlines them.
622
+ const filter = FILTER_;
623
+ let high = 0;
624
+ let low = 0;
625
+ let lowMagnitude = 0;
626
+ let magnitude = 0;
627
+ for (let corner = 0; corner < indices.length; corner += 3) {
628
+ const a = indices[corner] * 3;
629
+ const b = indices[corner + 1] * 3;
630
+ const c = indices[corner + 2] * 3;
631
+ // a − o as TwoSum's pairs, at 0–5.
632
+ for (let axis = 0; axis < 3; axis++) {
633
+ filter[2 * axis] = positions[a + axis];
634
+ filter[2 * axis + 1] = -FRAME_[axis];
635
+ twoSumInPlace(filter, 2 * axis);
636
+ }
637
+ const abX = positions[b] - positions[a];
638
+ const abY = positions[b + 1] - positions[a + 1];
639
+ const abZ = positions[b + 2] - positions[a + 2];
640
+ const acX = positions[c] - positions[a];
641
+ const acY = positions[c + 1] - positions[a + 1];
642
+ const acZ = positions[c + 2] - positions[a + 2];
643
+ // Cross component k is p − q, p at 6 + 4k and q at 8 + 4k, each with its
644
+ // TwoProduct error after it.
645
+ filter[6] = abY;
646
+ filter[7] = acZ;
647
+ filter[8] = abZ;
648
+ filter[9] = acY;
649
+ filter[10] = abZ;
650
+ filter[11] = acX;
651
+ filter[12] = abX;
652
+ filter[13] = acZ;
653
+ filter[14] = abX;
654
+ filter[15] = acY;
655
+ filter[16] = abY;
656
+ filter[17] = acX;
657
+ // One call site for all nine TwoProducts, so the engine inlines it once:
658
+ // the six of the cross components first, then the three of the terms
659
+ // (a − o)ₖ · crossₖ at 24–29, with each cross component's low part at
660
+ // 30 + k and the component itself at 33 + k.
661
+ let end = 18;
662
+ for (let phase = 0; phase < 2; phase++) {
663
+ for (let at = phase === 0 ? 6 : 24; at < end; at += 2) {
664
+ twoProductInRangeInPlace(filter, at);
665
+ }
666
+ if (phase === 1) {
667
+ break;
668
+ }
669
+ for (let axis = 0; axis < 3; axis++) {
670
+ const first = filter[6 + 4 * axis];
671
+ const second = filter[8 + 4 * axis];
672
+ filter[18] = first;
673
+ filter[19] = -second;
674
+ twoSumInPlace(filter, 18);
675
+ const anchored = filter[2 * axis];
676
+ filter[30 + axis] =
677
+ filter[19] + filter[7 + 4 * axis] - filter[9 + 4 * axis];
678
+ filter[33 + axis] = filter[18];
679
+ filter[24 + 2 * axis] = anchored;
680
+ filter[25 + 2 * axis] = filter[18];
681
+ magnitude += Math.abs(anchored) * (Math.abs(first) + Math.abs(second));
682
+ }
683
+ end = 30;
684
+ }
685
+ for (let axis = 0; axis < 3; axis++) {
686
+ filter[22] = high;
687
+ filter[23] = filter[24 + 2 * axis];
688
+ twoSumInPlace(filter, 22);
689
+ high = filter[22];
690
+ const carry = filter[23];
691
+ const error = filter[25 + 2 * axis];
692
+ const mixed = filter[2 * axis] * filter[30 + axis];
693
+ const mixedLow = filter[2 * axis + 1] * filter[33 + axis];
694
+ low += carry + error + mixed + mixedLow;
695
+ lowMagnitude +=
696
+ Math.abs(carry) +
697
+ Math.abs(error) +
698
+ Math.abs(mixed) +
699
+ Math.abs(mixedLow);
700
+ }
701
+ }
702
+ filter[22] = high;
703
+ filter[23] = low;
704
+ filter[0] = magnitude;
705
+ filter[1] = lowMagnitude;
706
+ return certifySixVolumeU16_(indices.length);
707
+ }
708
+ /**
709
+ * The end of {@link certifiedSixVolume_}, from the sums it leaves in `FILTER_`
710
+ * (the high and low parts at 22 and 23, the magnitudes of the products and of
711
+ * the low parts at 0 and 1), for `cornerCount` corners: kept apart so that
712
+ * `binaryExponent` is inlined here whatever budget the accumulation spent (a
713
+ * double passed to a call the engine does not inline is boxed).
714
+ */
715
+ function certifySixVolumeU16_(cornerCount) {
716
+ const filter = FILTER_;
717
+ const magnitude = filter[0];
718
+ const lowMagnitude = filter[1];
719
+ twoSumInPlace(filter, 22);
720
+ const total = filter[22];
721
+ const difference = filter[23];
722
+ // N parts: four per axis of each face, summed into one plain sum.
723
+ const parts = (cornerCount / 3) * 12 + 2;
724
+ const gamma = (parts * UNIT_ROUNDOFF_) / (1 - parts * UNIT_ROUNDOFF_);
725
+ if (!(gamma < 0.01)) {
726
+ return false;
727
+ }
728
+ const bound = 12 * UNIT_ROUNDOFF_ * UNIT_ROUNDOFF_ * magnitude * (1 + 2 * gamma) +
729
+ gamma * lowMagnitude * (1 + 2 * gamma) +
730
+ (cornerCount / 3) * 2 ** -1066;
731
+ const size = Math.abs(total);
732
+ if (!(size >= 2 ** -960 && size < Number.POSITIVE_INFINITY)) {
733
+ return false;
734
+ }
735
+ // The exact value lies within `bound` of total + difference; it rounds to
736
+ // `total` while that stays short of the midpoint on difference's side: half
737
+ // a unit in the last place, a quarter below a power of two.
738
+ const exponent = binaryExponent(total);
739
+ const isTowardZero = difference !== 0 && difference < 0 !== total < 0;
740
+ const isPowerOfTwo = size === 2 ** exponent;
741
+ const half = 2 ** (exponent - (isTowardZero && isPowerOfTwo ? 54 : 53));
742
+ if (!(Math.abs(difference) + bound < half)) {
743
+ return false;
744
+ }
745
+ VALUE_[0] = total;
746
+ return true;
747
+ }
748
+ /**
749
+ * The `Uint16Array` twin of the signed volume: six times it is `Σ (a − o) · ((b
750
+ * − a) × (c − a))` over the faces, o being the frame's anchor (any point gives
751
+ * the same volume for a closed mesh). The sum is a determinant per face, so
752
+ * each axis may take its own power-of-two scale: the frame brings every axis's
753
+ * extent below 1, then a first pass over the faces bounds their terms and one
754
+ * common power of two brings the largest near 2^1000, so that no product
755
+ * overflows and small faces keep their digits, however thin, wide or far from
756
+ * the origin the mesh is. Each face's term is then written as exact products:
757
+ * `a − o` as TwoSum's pair, the cross product as an expansion (each component
758
+ * two products and their rounding errors, Ogita, Rump & Oishi's TwoProduct),
759
+ * and the whole summed exactly (`PreciseSum`). With the face-local edge
760
+ * differences exact, which they are wherever a face is small beside its
761
+ * distance from the origin (Sterbenz), the result is six times the volume
762
+ * rounded once, then divided by 6: the same number in every face order and at
763
+ * every offset, and a vertex no triangle uses is never read. A term below about
764
+ * 2^−1900 of the largest may lose digits to the subnormal range; so may a
765
+ * volume that small beside the largest term. O(triangles), no allocation.
766
+ */
767
+ export function mesh3VolumeU16(positions, indices) {
768
+ const sum = SIX_VOLUMES_;
769
+ const vertex = VERTEX_;
770
+ const cross = CROSS_;
771
+ const terms = TERMS_;
772
+ const frame = FRAME_;
773
+ sum.reset();
774
+ frameU16_(positions, indices);
775
+ const isWide = frame[9] === 1;
776
+ // The filter: an ordinary mesh (no extent from 2^300 up) is summed in
777
+ // doubled precision with a rigorous error bound, and the exact sums below
778
+ // run only when that bound cannot certify the rounded result.
779
+ if (!isWide &&
780
+ frame[3] <= 300 &&
781
+ frame[4] <= 300 &&
782
+ frame[5] <= 300 &&
783
+ certifiedSixVolumeU16_(positions, indices)) {
784
+ // `+ 0`: an empty sum is −0, as `Math.sumPrecise`'s.
785
+ return VALUE_[0] / 6 + 0;
786
+ }
787
+ const bounds = BOUNDS_;
788
+ let termMax = NONE_;
789
+ let crossMax = NONE_;
790
+ let differenceMax = NONE_;
791
+ for (let corner = 0; corner < indices.length; corner += 3) {
792
+ faceBoundsU16_(positions, indices[corner] * 3, indices[corner + 1] * 3, indices[corner + 2] * 3, isWide);
793
+ const anchored = bounds[0];
794
+ const ab = bounds[1];
795
+ const ac = bounds[2];
796
+ differenceMax = Math.max(differenceMax, anchored, ab, ac);
797
+ if (ab > NONE_ && ac > NONE_) {
798
+ crossMax = Math.max(crossMax, ab + ac);
799
+ if (anchored > NONE_) {
800
+ termMax = Math.max(termMax, anchored + ab + ac);
801
+ }
802
+ }
803
+ }
804
+ // A scaled difference is below 2^(d + 1), a cross product component below
805
+ // 2^(x + 3), a term below 2^(t + 6): the common exponent keeps the three
806
+ // below 2^1021, 2^1020 and 2^1002, where TwoProduct is exact.
807
+ // Integers kept small integers (`| 0`), so that no exponent is boxed on its
808
+ // way into a call.
809
+ const extra = termMax > NONE_
810
+ ? Math.min(Math.max(Math.ceil((termMax - 996) / 3), Math.ceil((crossMax - 1017) / 2), differenceMax - 1020, -1020), 0) | 0
811
+ : 0;
812
+ scaleFrameU16_(extra);
813
+ for (let corner = 0; corner < indices.length; corner += 3) {
814
+ const a = indices[corner] * 3;
815
+ scaledEdgesU16_(positions, a, indices[corner + 1] * 3, indices[corner + 2] * 3, isWide);
816
+ anchoredCornerU16_(positions, a, isWide);
817
+ // Component k of the cross product is cross[4k] + cross[4k + 1] +
818
+ // cross[4k + 2] + cross[4k + 3], exactly: edges 0–2 are b − a, 3–5 c − a.
819
+ crossTermsU16_(cross, 0, 1, 5, 2, 4);
820
+ crossTermsU16_(cross, 4, 2, 3, 0, 5);
821
+ crossTermsU16_(cross, 8, 0, 4, 1, 3);
822
+ for (let term = 0; term < 12; term++) {
823
+ productTermsU16_(terms, 4 * term, vertex, term >> 2, cross, term);
824
+ productTermsU16_(terms, 4 * term + 2, vertex, (term >> 2) + 3, cross, term);
825
+ }
826
+ sum.addRange(terms, 0, 48);
827
+ }
828
+ const value = VALUE_;
829
+ sum.writeValue(value, 0);
830
+ value[0] = value[0] / 6;
831
+ scaleByPowerOfTwoInPlace(value, 0, (frame[3] + frame[4] + frame[5] + 3 * extra) | 0);
832
+ // `+ 0`: an empty sum is −0, as `Math.sumPrecise`'s.
833
+ return value[0] + 0;
834
+ }
835
+ /**
836
+ * Writes `x[i] · y[j]` into `out` at `at` as two numbers that sum to it exactly
837
+ * (TwoProduct, for products from 2^−969 to below 2^1023; within 5 · 2^−1074
838
+ * below). No double crosses the call.
839
+ */
840
+ function productTermsU16_(out, at, x, i, y, j) {
841
+ out[at] = x[i];
842
+ out[at + 1] = y[j];
843
+ twoProductInRangeInPlace(out, at);
844
+ }
845
+ /**
846
+ * Writes `e[i] · e[j] − e[k] · e[l]`, `e` being `EDGES_`, into `out` at `at` as
847
+ * four numbers that sum to it exactly: both products and their rounding errors,
848
+ * negated for the second.
849
+ */
850
+ function crossTermsU16_(out, at, i, j, k, l) {
851
+ const edges = EDGES_;
852
+ out[at] = edges[i];
853
+ out[at + 1] = edges[j];
854
+ twoProductInRangeInPlace(out, at);
855
+ out[at + 2] = edges[k];
856
+ out[at + 3] = edges[l];
857
+ twoProductInRangeInPlace(out, at + 2);
858
+ out[at + 2] = -out[at + 2];
859
+ out[at + 3] = -out[at + 3];
860
+ }
861
+ /**
862
+ * The `Uint16Array` twin of the surface centroid: `o + Σ A · (a + b + c − 3o) /
863
+ * 3` over `Σ A`, o being the frame's anchor and A each face's doubled area
864
+ * (`faceArea_`), scaled by one power of two that brings the largest near 1, so
865
+ * no weight overflows and only one below 2^−1070 of the largest is lost. Every
866
+ * corner minus o is TwoSum's exact pair scaled into the frame per axis, every
867
+ * product split exactly (TwoProduct) and every sum exact (`PreciseSum`), so the
868
+ * result is the same in every face order, nothing overflows at any size or
869
+ * distance from the origin, and a vertex no triangle uses is never read. A zero
870
+ * total area divides zero by zero: `NaN`. O(triangles), no allocation.
871
+ */
872
+ export function mesh3CentroidU16(out, positions, indices) {
873
+ const weights = CENTROID_SUMS_[0];
874
+ const terms = TERMS_;
875
+ const vertex = VERTEX_;
876
+ const area = FACE_AREA_;
877
+ const value = VALUE_;
878
+ const frame = FRAME_;
879
+ for (let sum = 0; sum < CENTROID_SUMS_.length; sum++) {
880
+ CENTROID_SUMS_[sum].reset();
881
+ }
882
+ frameU16_(positions, indices);
883
+ const isWide = frame[9] === 1;
884
+ let weightExponent = NONE_;
885
+ for (let corner = 0; corner < indices.length; corner += 3) {
886
+ faceAreaU16_(positions, indices[corner] * 3, indices[corner + 1] * 3, indices[corner + 2] * 3);
887
+ if (area[0] > 0) {
888
+ weightExponent = Math.max(weightExponent, area[1] + binaryExponent(area[0]));
889
+ }
890
+ }
891
+ for (let corner = 0; corner < indices.length; corner += 3) {
892
+ const a = indices[corner] * 3;
893
+ const b = indices[corner + 1] * 3;
894
+ const c = indices[corner + 2] * 3;
895
+ faceAreaU16_(positions, a, b, c);
896
+ if (area[0] === 0) {
897
+ continue;
898
+ }
899
+ value[0] = area[0];
900
+ if (area[0] > 0) {
901
+ // The largest weight is in [1, 2); a power within the normal range is
902
+ // one multiplication.
903
+ const power = area[1] - weightExponent;
904
+ if (power >= -1022) {
905
+ value[0] = area[0] * 2 ** power;
906
+ }
907
+ else {
908
+ scaleByPowerOfTwoInPlace(value, 0, power);
909
+ }
910
+ }
911
+ // Three times the weight, as w + 2w exactly: the sums then divide once.
912
+ terms[0] = value[0];
913
+ terms[1] = 2 * value[0];
914
+ weights.addRange(terms, 0, 2);
915
+ for (let index = 0; index < 3; index++) {
916
+ anchoredCornerU16_(positions, index === 0 ? a : index === 1 ? b : c, isWide);
917
+ for (let axis = 0; axis < 3; axis++) {
918
+ productTermsU16_(terms, 4 * index + 12 * axis, value, 0, vertex, axis);
919
+ productTermsU16_(terms, 4 * index + 12 * axis + 2, value, 0, vertex, axis + 3);
920
+ }
921
+ }
922
+ for (let axis = 0; axis < 3; axis++) {
923
+ CENTROID_SUMS_[axis + 1].addRange(terms, 12 * axis, 12 * axis + 12);
924
+ }
925
+ }
926
+ weights.writeValue(value, 0);
927
+ const thrice = value[0];
928
+ for (let axis = 0; axis < 3; axis++) {
929
+ CENTROID_SUMS_[axis + 1].writeValue(value, 0);
930
+ value[0] = value[0] / thrice;
931
+ // Halves where the frame is wide: the anchor plus a scaled offset may
932
+ // pass Number.MAX_VALUE on the way to a centroid that does not.
933
+ scaleByPowerOfTwoInPlace(value, 0, frame[3 + axis] - (isWide ? 1 : 0));
934
+ out[axis] = isWide
935
+ ? 2 * (frame[axis] * 0.5 + value[0])
936
+ : frame[axis] + value[0];
937
+ }
938
+ }
939
+ /**
940
+ * The `Uint16Array` twin of the nearest ray hit: the triangle index, `−1` for a
941
+ * miss; `out` is `[t, u, v]`, `NaN` for a miss. O(triangles).
942
+ */
943
+ export function mesh3IntersectRay3U16(out, ray, positions, indices, cullBackFaces) {
944
+ let nearest = -1;
945
+ let nearestT = Number.POSITIVE_INFINITY;
946
+ let nearestU = Number.NaN;
947
+ let nearestV = Number.NaN;
948
+ for (let corner = 0, triangle = 0; corner < indices.length; corner += 3, triangle++) {
949
+ const a = indices[corner] * 3;
950
+ const b = indices[corner + 1] * 3;
951
+ const c = indices[corner + 2] * 3;
952
+ TRIANGLE_[0] = +positions[a];
953
+ TRIANGLE_[1] = +positions[a + 1];
954
+ TRIANGLE_[2] = +positions[a + 2];
955
+ TRIANGLE_[3] = +positions[b];
956
+ TRIANGLE_[4] = +positions[b + 1];
957
+ TRIANGLE_[5] = +positions[b + 2];
958
+ TRIANGLE_[6] = +positions[c];
959
+ TRIANGLE_[7] = +positions[c + 1];
960
+ TRIANGLE_[8] = +positions[c + 2];
961
+ const t = ray3IntersectTriangle3(BARYCENTRIC_, ray, TRIANGLE_, cullBackFaces);
962
+ if (t < nearestT) {
963
+ nearestT = t;
964
+ nearest = triangle;
965
+ nearestU = BARYCENTRIC_[0];
966
+ nearestV = BARYCENTRIC_[1];
967
+ }
968
+ }
969
+ out[0] = nearest < 0 ? Number.NaN : nearestT;
970
+ out[1] = nearestU;
971
+ out[2] = nearestV;
972
+ return nearest;
973
+ }
974
+ /**
975
+ * The `Uint16Array` twin of the triangle depths: `z / w` of each triangle's
976
+ * centroid under `viewProjection`. A centroid whose coordinates overflow when
977
+ * summed takes halves; one whose transformed z or w overflows is scaled by a
978
+ * power of two first, which leaves `z / w` unchanged. O(triangles).
979
+ */
980
+ export function mesh3TriangleDepthsU16(out, positions, indices, viewProjection) {
981
+ const m2 = viewProjection[2];
982
+ const m3 = viewProjection[3];
983
+ const m6 = viewProjection[6];
984
+ const m7 = viewProjection[7];
985
+ const m10 = viewProjection[10];
986
+ const m11 = viewProjection[11];
987
+ const m14 = viewProjection[14];
988
+ const m15 = viewProjection[15];
989
+ for (let corner = 0, triangle = 0; corner < indices.length; corner += 3, triangle++) {
990
+ const a = indices[corner] * 3;
991
+ const b = indices[corner + 1] * 3;
992
+ const c = indices[corner + 2] * 3;
993
+ let x = (positions[a] + positions[b] + positions[c]) / 3;
994
+ let y = (positions[a + 1] + positions[b + 1] + positions[c + 1]) / 3;
995
+ let z = (positions[a + 2] + positions[b + 2] + positions[c + 2]) / 3;
996
+ if (x - x !== 0 || y - y !== 0 || z - z !== 0) {
997
+ // Quarters, when the sum overflowed: the centroid a wider sum would give.
998
+ x =
999
+ ((positions[a] * 0.25 + positions[b] * 0.25 + positions[c] * 0.25) /
1000
+ 3) *
1001
+ 4;
1002
+ y =
1003
+ ((positions[a + 1] * 0.25 +
1004
+ positions[b + 1] * 0.25 +
1005
+ positions[c + 1] * 0.25) /
1006
+ 3) *
1007
+ 4;
1008
+ z =
1009
+ ((positions[a + 2] * 0.25 +
1010
+ positions[b + 2] * 0.25 +
1011
+ positions[c + 2] * 0.25) /
1012
+ 3) *
1013
+ 4;
1014
+ }
1015
+ let depth = m2 * x + m6 * y + m10 * z + m14;
1016
+ let w = m3 * x + m7 * y + m11 * z + m15;
1017
+ if (depth - depth !== 0 || w - w !== 0) {
1018
+ // The homogeneous point (x, y, z, 1) scaled by a power of two, so that
1019
+ // its largest coordinate is below 1: the ratio is the same.
1020
+ const largest = Math.max(Math.abs(x), Math.abs(y), Math.abs(z));
1021
+ if (largest > 1 && largest < Number.POSITIVE_INFINITY) {
1022
+ const scale = 2 ** -(binaryExponent(largest) + 1);
1023
+ depth =
1024
+ m2 * (x * scale) + m6 * (y * scale) + m10 * (z * scale) + m14 * scale;
1025
+ w =
1026
+ m3 * (x * scale) + m7 * (y * scale) + m11 * (z * scale) + m15 * scale;
1027
+ }
1028
+ }
1029
+ out[triangle] = depth / w;
1030
+ }
1031
+ }
1032
+ /**
1033
+ * The `Uint16Array` twin of the triangle reordering: triangle `order[k]` of
1034
+ * `indices` becomes triangle `k` of `out`. O(triangles).
1035
+ */
1036
+ export function mesh3ReorderTrianglesU16(out, indices, order) {
1037
+ for (let triangle = 0; triangle < order.length; triangle++) {
1038
+ const from = order[triangle] * 3;
1039
+ const to = triangle * 3;
1040
+ out[to] = indices[from];
1041
+ out[to + 1] = indices[from + 1];
1042
+ out[to + 2] = indices[from + 2];
1043
+ }
1044
+ }
1045
+ //# sourceMappingURL=_index-kernels-u16.js.map