@ac-kit/math-geometry 0.2.2 → 0.3.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 (371) hide show
  1. package/CHANGELOG.md +228 -0
  2. package/README.md +323 -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/_ray3-intersect.d.ts +19 -0
  12. package/dist/_ray3-intersect.d.ts.map +1 -0
  13. package/dist/_ray3-intersect.js +122 -0
  14. package/dist/_ray3-intersect.js.map +1 -0
  15. package/dist/box2-array-boundary.d.ts +50 -0
  16. package/dist/box2-array-boundary.d.ts.map +1 -0
  17. package/dist/box2-array-boundary.js +97 -0
  18. package/dist/box2-array-boundary.js.map +1 -0
  19. package/dist/box2-array.d.ts +42 -0
  20. package/dist/box2-array.d.ts.map +1 -0
  21. package/dist/box2-array.js +134 -0
  22. package/dist/box2-array.js.map +1 -0
  23. package/dist/box2-componentwise.d.ts +110 -0
  24. package/dist/box2-componentwise.d.ts.map +1 -0
  25. package/dist/box2-componentwise.js +325 -0
  26. package/dist/box2-componentwise.js.map +1 -0
  27. package/dist/box2-grid-index.d.ts +67 -0
  28. package/dist/box2-grid-index.d.ts.map +1 -0
  29. package/dist/box2-grid-index.js +198 -0
  30. package/dist/box2-grid-index.js.map +1 -0
  31. package/dist/box2-record.d.ts +83 -0
  32. package/dist/box2-record.d.ts.map +1 -0
  33. package/dist/box2-record.js +124 -0
  34. package/dist/box2-record.js.map +1 -0
  35. package/dist/box2.d.ts +38 -0
  36. package/dist/box2.d.ts.map +1 -0
  37. package/dist/box2.js +97 -0
  38. package/dist/box2.js.map +1 -0
  39. package/dist/box3-array-boundary.d.ts +50 -0
  40. package/dist/box3-array-boundary.d.ts.map +1 -0
  41. package/dist/box3-array-boundary.js +107 -0
  42. package/dist/box3-array-boundary.js.map +1 -0
  43. package/dist/box3-componentwise.d.ts +110 -0
  44. package/dist/box3-componentwise.d.ts.map +1 -0
  45. package/dist/box3-componentwise.js +392 -0
  46. package/dist/box3-componentwise.js.map +1 -0
  47. package/dist/box3-record.d.ts +92 -0
  48. package/dist/box3-record.d.ts.map +1 -0
  49. package/dist/box3-record.js +147 -0
  50. package/dist/box3-record.js.map +1 -0
  51. package/dist/box3.d.ts +19 -16
  52. package/dist/box3.d.ts.map +1 -1
  53. package/dist/box3.js +73 -76
  54. package/dist/box3.js.map +1 -1
  55. package/dist/box4-array-boundary.d.ts +50 -0
  56. package/dist/box4-array-boundary.d.ts.map +1 -0
  57. package/dist/box4-array-boundary.js +117 -0
  58. package/dist/box4-array-boundary.js.map +1 -0
  59. package/dist/box4-componentwise.d.ts +110 -0
  60. package/dist/box4-componentwise.d.ts.map +1 -0
  61. package/dist/box4-componentwise.js +459 -0
  62. package/dist/box4-componentwise.js.map +1 -0
  63. package/dist/box4-record.d.ts +96 -0
  64. package/dist/box4-record.d.ts.map +1 -0
  65. package/dist/box4-record.js +167 -0
  66. package/dist/box4-record.js.map +1 -0
  67. package/dist/box4.d.ts +13 -8
  68. package/dist/box4.d.ts.map +1 -1
  69. package/dist/box4.js +27 -5
  70. package/dist/box4.js.map +1 -1
  71. package/dist/circle2-array-boundary.d.ts +51 -0
  72. package/dist/circle2-array-boundary.d.ts.map +1 -0
  73. package/dist/circle2-array-boundary.js +93 -0
  74. package/dist/circle2-array-boundary.js.map +1 -0
  75. package/dist/circle2-array.d.ts +17 -0
  76. package/dist/circle2-array.d.ts.map +1 -0
  77. package/dist/circle2-array.js +44 -0
  78. package/dist/circle2-array.js.map +1 -0
  79. package/dist/circle2-record.d.ts +81 -0
  80. package/dist/circle2-record.d.ts.map +1 -0
  81. package/dist/circle2-record.js +114 -0
  82. package/dist/circle2-record.js.map +1 -0
  83. package/dist/circle2.d.ts +27 -82
  84. package/dist/circle2.d.ts.map +1 -1
  85. package/dist/circle2.js +59 -104
  86. package/dist/circle2.js.map +1 -1
  87. package/dist/coordinates/cylindrical-record.d.ts +86 -0
  88. package/dist/coordinates/cylindrical-record.d.ts.map +1 -0
  89. package/dist/coordinates/cylindrical-record.js +114 -0
  90. package/dist/coordinates/cylindrical-record.js.map +1 -0
  91. package/dist/coordinates/horizontal-record.d.ts +86 -0
  92. package/dist/coordinates/horizontal-record.d.ts.map +1 -0
  93. package/dist/coordinates/horizontal-record.js +114 -0
  94. package/dist/coordinates/horizontal-record.js.map +1 -0
  95. package/dist/coordinates/horizontal.d.ts +48 -0
  96. package/dist/coordinates/horizontal.d.ts.map +1 -0
  97. package/dist/coordinates/horizontal.js +100 -0
  98. package/dist/coordinates/horizontal.js.map +1 -0
  99. package/dist/coordinates/index.d.ts +10 -0
  100. package/dist/coordinates/index.d.ts.map +1 -0
  101. package/dist/coordinates/index.js +10 -0
  102. package/dist/coordinates/index.js.map +1 -0
  103. package/dist/coordinates/longitude-latitude-record.d.ts +81 -0
  104. package/dist/coordinates/longitude-latitude-record.d.ts.map +1 -0
  105. package/dist/coordinates/longitude-latitude-record.js +104 -0
  106. package/dist/coordinates/longitude-latitude-record.js.map +1 -0
  107. package/dist/coordinates/longitude-latitude.d.ts +33 -0
  108. package/dist/coordinates/longitude-latitude.d.ts.map +1 -0
  109. package/dist/coordinates/longitude-latitude.js +78 -0
  110. package/dist/coordinates/longitude-latitude.js.map +1 -0
  111. package/dist/coordinates/polar-record.d.ts +77 -0
  112. package/dist/coordinates/polar-record.d.ts.map +1 -0
  113. package/dist/coordinates/polar-record.js +104 -0
  114. package/dist/coordinates/polar-record.js.map +1 -0
  115. package/dist/coordinates/polar.d.ts +28 -0
  116. package/dist/coordinates/polar.d.ts.map +1 -0
  117. package/dist/coordinates/polar.js +68 -0
  118. package/dist/coordinates/polar.js.map +1 -0
  119. package/dist/coordinates/spherical-cylindrical.d.ts +49 -0
  120. package/dist/coordinates/spherical-cylindrical.d.ts.map +1 -0
  121. package/dist/coordinates/spherical-cylindrical.js +110 -0
  122. package/dist/coordinates/spherical-cylindrical.js.map +1 -0
  123. package/dist/coordinates/spherical-record.d.ts +86 -0
  124. package/dist/coordinates/spherical-record.d.ts.map +1 -0
  125. package/dist/coordinates/spherical-record.js +114 -0
  126. package/dist/coordinates/spherical-record.js.map +1 -0
  127. package/dist/frustum3-record.d.ts +132 -0
  128. package/dist/frustum3-record.d.ts.map +1 -0
  129. package/dist/frustum3-record.js +333 -0
  130. package/dist/frustum3-record.js.map +1 -0
  131. package/dist/frustum3.d.ts +60 -0
  132. package/dist/frustum3.d.ts.map +1 -0
  133. package/dist/frustum3.js +186 -0
  134. package/dist/frustum3.js.map +1 -0
  135. package/dist/index.d.ts +36 -16
  136. package/dist/index.d.ts.map +1 -1
  137. package/dist/index.js +36 -16
  138. package/dist/index.js.map +1 -1
  139. package/dist/line2-record.d.ts +87 -0
  140. package/dist/line2-record.d.ts.map +1 -0
  141. package/dist/line2-record.js +123 -0
  142. package/dist/line2-record.js.map +1 -0
  143. package/dist/line2.d.ts +20 -33
  144. package/dist/line2.d.ts.map +1 -1
  145. package/dist/line2.js +56 -51
  146. package/dist/line2.js.map +1 -1
  147. package/dist/line3-record.d.ts +91 -0
  148. package/dist/line3-record.d.ts.map +1 -0
  149. package/dist/line3-record.js +146 -0
  150. package/dist/line3-record.js.map +1 -0
  151. package/dist/line3.d.ts +11 -24
  152. package/dist/line3.d.ts.map +1 -1
  153. package/dist/line3.js +32 -36
  154. package/dist/line3.js.map +1 -1
  155. package/dist/mesh/_generator-kit.d.ts +14 -0
  156. package/dist/mesh/_generator-kit.d.ts.map +1 -0
  157. package/dist/mesh/_generator-kit.js +39 -0
  158. package/dist/mesh/_generator-kit.js.map +1 -0
  159. package/dist/mesh/_index-kernels-u16.d.ts +43 -0
  160. package/dist/mesh/_index-kernels-u16.d.ts.map +1 -0
  161. package/dist/mesh/_index-kernels-u16.js +264 -0
  162. package/dist/mesh/_index-kernels-u16.js.map +1 -0
  163. package/dist/mesh/_index-kernels-u32.d.ts +43 -0
  164. package/dist/mesh/_index-kernels-u32.d.ts.map +1 -0
  165. package/dist/mesh/_index-kernels-u32.js +264 -0
  166. package/dist/mesh/_index-kernels-u32.js.map +1 -0
  167. package/dist/mesh/generate-box.d.ts +16 -0
  168. package/dist/mesh/generate-box.d.ts.map +1 -0
  169. package/dist/mesh/generate-box.js +79 -0
  170. package/dist/mesh/generate-box.js.map +1 -0
  171. package/dist/mesh/generate-cylinder.d.ts +25 -0
  172. package/dist/mesh/generate-cylinder.d.ts.map +1 -0
  173. package/dist/mesh/generate-cylinder.js +117 -0
  174. package/dist/mesh/generate-cylinder.js.map +1 -0
  175. package/dist/mesh/generate-heightfield.d.ts +24 -0
  176. package/dist/mesh/generate-heightfield.d.ts.map +1 -0
  177. package/dist/mesh/generate-heightfield.js +48 -0
  178. package/dist/mesh/generate-heightfield.js.map +1 -0
  179. package/dist/mesh/generate-plane.d.ts +18 -0
  180. package/dist/mesh/generate-plane.d.ts.map +1 -0
  181. package/dist/mesh/generate-plane.js +39 -0
  182. package/dist/mesh/generate-plane.js.map +1 -0
  183. package/dist/mesh/generate-sphere.d.ts +50 -0
  184. package/dist/mesh/generate-sphere.d.ts.map +1 -0
  185. package/dist/mesh/generate-sphere.js +244 -0
  186. package/dist/mesh/generate-sphere.js.map +1 -0
  187. package/dist/mesh/index.d.ts +8 -0
  188. package/dist/mesh/index.d.ts.map +1 -0
  189. package/dist/mesh/index.js +8 -0
  190. package/dist/mesh/index.js.map +1 -0
  191. package/dist/mesh/mesh3-kernels.d.ts +82 -0
  192. package/dist/mesh/mesh3-kernels.d.ts.map +1 -0
  193. package/dist/mesh/mesh3-kernels.js +180 -0
  194. package/dist/mesh/mesh3-kernels.js.map +1 -0
  195. package/dist/mesh/mesh3.d.ts +66 -0
  196. package/dist/mesh/mesh3.d.ts.map +1 -0
  197. package/dist/mesh/mesh3.js +128 -0
  198. package/dist/mesh/mesh3.js.map +1 -0
  199. package/dist/plane3-record.d.ts +88 -0
  200. package/dist/plane3-record.d.ts.map +1 -0
  201. package/dist/plane3-record.js +123 -0
  202. package/dist/plane3-record.js.map +1 -0
  203. package/dist/plane3.d.ts +42 -28
  204. package/dist/plane3.d.ts.map +1 -1
  205. package/dist/plane3.js +166 -48
  206. package/dist/plane3.js.map +1 -1
  207. package/dist/polygon2.d.ts +82 -29
  208. package/dist/polygon2.d.ts.map +1 -1
  209. package/dist/polygon2.js +236 -93
  210. package/dist/polygon2.js.map +1 -1
  211. package/dist/polygon3.d.ts +40 -21
  212. package/dist/polygon3.d.ts.map +1 -1
  213. package/dist/polygon3.js +215 -91
  214. package/dist/polygon3.js.map +1 -1
  215. package/dist/polyline2.d.ts +37 -0
  216. package/dist/polyline2.d.ts.map +1 -0
  217. package/dist/polyline2.js +146 -0
  218. package/dist/polyline2.js.map +1 -0
  219. package/dist/polyline3.d.ts +35 -0
  220. package/dist/polyline3.d.ts.map +1 -0
  221. package/dist/polyline3.js +159 -0
  222. package/dist/polyline3.js.map +1 -0
  223. package/dist/quadtree.d.ts +238 -0
  224. package/dist/quadtree.d.ts.map +1 -0
  225. package/dist/quadtree.js +1019 -0
  226. package/dist/quadtree.js.map +1 -0
  227. package/dist/ray2-record.d.ts +86 -0
  228. package/dist/ray2-record.d.ts.map +1 -0
  229. package/dist/ray2-record.js +123 -0
  230. package/dist/ray2-record.js.map +1 -0
  231. package/dist/ray2.d.ts +38 -13
  232. package/dist/ray2.d.ts.map +1 -1
  233. package/dist/ray2.js +85 -22
  234. package/dist/ray2.js.map +1 -1
  235. package/dist/ray3-array.d.ts +27 -0
  236. package/dist/ray3-array.d.ts.map +1 -0
  237. package/dist/ray3-array.js +89 -0
  238. package/dist/ray3-array.js.map +1 -0
  239. package/dist/ray3-record.d.ts +90 -0
  240. package/dist/ray3-record.d.ts.map +1 -0
  241. package/dist/ray3-record.js +146 -0
  242. package/dist/ray3-record.js.map +1 -0
  243. package/dist/ray3.d.ts +72 -25
  244. package/dist/ray3.d.ts.map +1 -1
  245. package/dist/ray3.js +233 -64
  246. package/dist/ray3.js.map +1 -1
  247. package/dist/segment2-record.d.ts +80 -0
  248. package/dist/segment2-record.d.ts.map +1 -0
  249. package/dist/segment2-record.js +122 -0
  250. package/dist/segment2-record.js.map +1 -0
  251. package/dist/segment2.d.ts +34 -19
  252. package/dist/segment2.d.ts.map +1 -1
  253. package/dist/segment2.js +88 -42
  254. package/dist/segment2.js.map +1 -1
  255. package/dist/segment3-record.d.ts +90 -0
  256. package/dist/segment3-record.d.ts.map +1 -0
  257. package/dist/segment3-record.js +146 -0
  258. package/dist/segment3-record.js.map +1 -0
  259. package/dist/segment3.d.ts +24 -12
  260. package/dist/segment3.d.ts.map +1 -1
  261. package/dist/segment3.js +67 -30
  262. package/dist/segment3.js.map +1 -1
  263. package/dist/size.d.ts +40 -0
  264. package/dist/size.d.ts.map +1 -0
  265. package/dist/size.js +72 -0
  266. package/dist/size.js.map +1 -0
  267. package/dist/sphere3-array-boundary.d.ts +51 -0
  268. package/dist/sphere3-array-boundary.d.ts.map +1 -0
  269. package/dist/sphere3-array-boundary.js +98 -0
  270. package/dist/sphere3-array-boundary.js.map +1 -0
  271. package/dist/sphere3-record.d.ts +88 -0
  272. package/dist/sphere3-record.d.ts.map +1 -0
  273. package/dist/sphere3-record.js +123 -0
  274. package/dist/sphere3-record.js.map +1 -0
  275. package/dist/sphere3.d.ts +55 -12
  276. package/dist/sphere3.d.ts.map +1 -1
  277. package/dist/sphere3.js +233 -24
  278. package/dist/sphere3.js.map +1 -1
  279. package/dist/spherical/cap.d.ts +56 -0
  280. package/dist/spherical/cap.d.ts.map +1 -0
  281. package/dist/spherical/cap.js +363 -0
  282. package/dist/spherical/cap.js.map +1 -0
  283. package/dist/spherical/great-circle.d.ts +39 -0
  284. package/dist/spherical/great-circle.d.ts.map +1 -0
  285. package/dist/spherical/great-circle.js +158 -0
  286. package/dist/spherical/great-circle.js.map +1 -0
  287. package/dist/spherical/index.d.ts +3 -0
  288. package/dist/spherical/index.d.ts.map +1 -0
  289. package/dist/spherical/index.js +3 -0
  290. package/dist/spherical/index.js.map +1 -0
  291. package/dist/triangle2-record.d.ts +91 -0
  292. package/dist/triangle2-record.d.ts.map +1 -0
  293. package/dist/triangle2-record.js +146 -0
  294. package/dist/triangle2-record.js.map +1 -0
  295. package/dist/triangle2.d.ts +29 -14
  296. package/dist/triangle2.d.ts.map +1 -1
  297. package/dist/triangle2.js +86 -22
  298. package/dist/triangle2.js.map +1 -1
  299. package/dist/triangle3-record.d.ts +97 -0
  300. package/dist/triangle3-record.d.ts.map +1 -0
  301. package/dist/triangle3-record.js +178 -0
  302. package/dist/triangle3-record.js.map +1 -0
  303. package/dist/triangle3.d.ts +41 -17
  304. package/dist/triangle3.d.ts.map +1 -1
  305. package/dist/triangle3.js +267 -38
  306. package/dist/triangle3.js.map +1 -1
  307. package/package.json +15 -8
  308. package/dist/angles.d.ts +0 -31
  309. package/dist/angles.d.ts.map +0 -1
  310. package/dist/angles.js +0 -53
  311. package/dist/angles.js.map +0 -1
  312. package/dist/coordinate-spaces.d.ts +0 -39
  313. package/dist/coordinate-spaces.d.ts.map +0 -1
  314. package/dist/coordinate-spaces.js +0 -52
  315. package/dist/coordinate-spaces.js.map +0 -1
  316. package/dist/coordinates-vec2.d.ts +0 -7
  317. package/dist/coordinates-vec2.d.ts.map +0 -1
  318. package/dist/coordinates-vec2.js +0 -35
  319. package/dist/coordinates-vec2.js.map +0 -1
  320. package/dist/coordinates-vec3.d.ts +0 -11
  321. package/dist/coordinates-vec3.d.ts.map +0 -1
  322. package/dist/coordinates-vec3.js +0 -61
  323. package/dist/coordinates-vec3.js.map +0 -1
  324. package/dist/coordinates.d.ts +0 -54
  325. package/dist/coordinates.d.ts.map +0 -1
  326. package/dist/coordinates.js +0 -2
  327. package/dist/coordinates.js.map +0 -1
  328. package/dist/euler-angles-quaternion.d.ts +0 -7
  329. package/dist/euler-angles-quaternion.d.ts.map +0 -1
  330. package/dist/euler-angles-quaternion.js +0 -100
  331. package/dist/euler-angles-quaternion.js.map +0 -1
  332. package/dist/euler-angles.d.ts +0 -33
  333. package/dist/euler-angles.d.ts.map +0 -1
  334. package/dist/euler-angles.js +0 -35
  335. package/dist/euler-angles.js.map +0 -1
  336. package/dist/geometry-vectors.d.ts +0 -11
  337. package/dist/geometry-vectors.d.ts.map +0 -1
  338. package/dist/geometry-vectors.js +0 -19
  339. package/dist/geometry-vectors.js.map +0 -1
  340. package/dist/mesh3.d.ts +0 -32
  341. package/dist/mesh3.d.ts.map +0 -1
  342. package/dist/mesh3.js +0 -142
  343. package/dist/mesh3.js.map +0 -1
  344. package/dist/point2.d.ts +0 -41
  345. package/dist/point2.d.ts.map +0 -1
  346. package/dist/point2.js +0 -92
  347. package/dist/point2.js.map +0 -1
  348. package/dist/point3.d.ts +0 -43
  349. package/dist/point3.d.ts.map +0 -1
  350. package/dist/point3.js +0 -103
  351. package/dist/point3.js.map +0 -1
  352. package/dist/point4.d.ts +0 -12
  353. package/dist/point4.d.ts.map +0 -1
  354. package/dist/point4.js +0 -28
  355. package/dist/point4.js.map +0 -1
  356. package/dist/rect2.d.ts +0 -54
  357. package/dist/rect2.d.ts.map +0 -1
  358. package/dist/rect2.js +0 -149
  359. package/dist/rect2.js.map +0 -1
  360. package/dist/size2.d.ts +0 -33
  361. package/dist/size2.d.ts.map +0 -1
  362. package/dist/size2.js +0 -81
  363. package/dist/size2.js.map +0 -1
  364. package/dist/size3.d.ts +0 -21
  365. package/dist/size3.d.ts.map +0 -1
  366. package/dist/size3.js +0 -59
  367. package/dist/size3.js.map +0 -1
  368. package/dist/size4.d.ts +0 -10
  369. package/dist/size4.d.ts.map +0 -1
  370. package/dist/size4.js +0 -16
  371. package/dist/size4.js.map +0 -1
@@ -0,0 +1,1019 @@
1
+ import { describeValue } from "@ac-kit/core";
2
+ import { assertFinite, isIntegerInRange, ulp } from "@ac-kit/core";
3
+ import { vec2Create, vec2LengthScaled } from "@ac-kit/math-linear";
4
+ import { box2DistanceToPoint, box2IntersectsBox2, } from "./box2-componentwise.js";
5
+ import { box2Create } from "./box2-record.js";
6
+ import { box2ContainsXy } from "./box2.js";
7
+ /** Every coordinate a tree accepts lies within ±this; see {@link Quadtree}. */
8
+ const COORDINATE_LIMIT = 1e150;
9
+ /** Marks a node being freed while the tree is compacted. */
10
+ const DEAD = -2;
11
+ /**
12
+ * A cell's distance is a rounded square root; pruning tolerates a few ulps so
13
+ * ties are never lost.
14
+ */
15
+ const PRUNE_SLACK = 1 + 1e-15;
16
+ function checkCoordinate(value, method, axis, id) {
17
+ if (Math.abs(value) <= COORDINATE_LIMIT) {
18
+ return;
19
+ }
20
+ const name = id < 0 ? `${method}: ${axis}` : `${method}: point ${id} ${axis}`;
21
+ assertFinite(value, name);
22
+ throw new RangeError(`${name} must be within ±1e150, got ${value}`);
23
+ }
24
+ /** The largest multiple of `unit` (a power of two) at or below `value`. */
25
+ function alignDown_(value, unit) {
26
+ const aligned = Math.floor(value / unit) * unit;
27
+ // `value / unit` may round to −0 when it underflows.
28
+ return aligned > value ? aligned - unit : aligned;
29
+ }
30
+ /**
31
+ * Per-traversal explicit stack, one frame per entry; a nested traversal takes
32
+ * its own.
33
+ */
34
+ class Walk {
35
+ nodes = new Int32Array(64);
36
+ quadrants = new Uint8Array(64);
37
+ bounds = new Float64Array(256);
38
+ keys = new Float64Array(64);
39
+ query = box2Create();
40
+ cell = box2Create();
41
+ /** The cell handed to a visitor: a box, not four doubles a call would box. */
42
+ visited = box2Create();
43
+ point = vec2Create();
44
+ /** A candidate point minus the query, for the scaled distance. */
45
+ delta = vec2Create();
46
+ /** MinX, minY, maxX, maxY, midX, midY of the cell being expanded. */
47
+ parent = new Float64Array(6);
48
+ grow() {
49
+ const capacity = this.nodes.length * 2;
50
+ const nodes = new Int32Array(capacity);
51
+ nodes.set(this.nodes);
52
+ const quadrants = new Uint8Array(capacity);
53
+ quadrants.set(this.quadrants);
54
+ const bounds = new Float64Array(capacity * 4);
55
+ bounds.set(this.bounds);
56
+ const keys = new Float64Array(capacity);
57
+ keys.set(this.keys);
58
+ this.nodes = nodes;
59
+ this.quadrants = quadrants;
60
+ this.bounds = bounds;
61
+ this.keys = keys;
62
+ }
63
+ /**
64
+ * Makes frame `frame`'s cell the parent for `pushChild` and `setCell`; the
65
+ * frame may be overwritten afterwards.
66
+ */
67
+ openCell(frame) {
68
+ const base = frame * 4;
69
+ const parent = this.parent;
70
+ parent[0] = this.bounds[base];
71
+ parent[1] = this.bounds[base + 1];
72
+ parent[2] = this.bounds[base + 2];
73
+ parent[3] = this.bounds[base + 3];
74
+ parent[4] = (parent[0] + parent[2]) / 2;
75
+ parent[5] = (parent[1] + parent[3]) / 2;
76
+ }
77
+ /**
78
+ * Writes frame `frame` as the child of the open cell in `quadrant`. The cells
79
+ * never cross a call as arguments: boxed doubles would allocate.
80
+ */
81
+ pushChild(frame, node, quadrant) {
82
+ const parent = this.parent;
83
+ const right = (quadrant & 1) === 1;
84
+ const bottom = quadrant >= 2;
85
+ const base = frame * 4;
86
+ this.nodes[frame] = node;
87
+ this.bounds[base] = (right ? parent[4] : parent[0]);
88
+ this.bounds[base + 1] = (bottom ? parent[5] : parent[1]);
89
+ this.bounds[base + 2] = (right ? parent[2] : parent[4]);
90
+ this.bounds[base + 3] = (bottom ? parent[3] : parent[5]);
91
+ }
92
+ /** Copies the open cell's child in `quadrant` into `cell`. */
93
+ setCell(quadrant) {
94
+ const parent = this.parent;
95
+ const right = (quadrant & 1) === 1;
96
+ const bottom = quadrant >= 2;
97
+ this.cell[0] = (right ? parent[4] : parent[0]);
98
+ this.cell[1] = (bottom ? parent[5] : parent[1]);
99
+ this.cell[2] = (right ? parent[2] : parent[4]);
100
+ this.cell[3] = (bottom ? parent[3] : parent[5]);
101
+ }
102
+ }
103
+ /**
104
+ * A point quadtree over integer ids (the structure of Finkel & Bentley, "Quad
105
+ * trees: a data structure for retrieval on composite keys", Acta Informatica 4,
106
+ * 1974, with the cell conventions of d3-quadtree): each cell splits into four
107
+ * quadrants at its midpoint, a leaf holds one distinct position and the ids of
108
+ * every point at exactly that position. The root cell is a square, doubling
109
+ * outward as points arrive outside it so that the old root becomes one of its
110
+ * quadrants, cell edges and splits unchanged. While those edges are exact sums
111
+ * (integer edges below 2⁵³, so at every ordinary scale) the root starts and
112
+ * grows as d3's `cover` does, and `visit`/`visitAfter` see the same cells in
113
+ * the same order as d3 for the same points. Where d3's doubled edges would
114
+ * round (cells starting beyond 2⁵³, or a root grown more than about 2⁵⁰-fold
115
+ * past its first cell's precision), the root is instead replaced by one whose
116
+ * edges are exact multiples of a power of two and the points are reinserted, so
117
+ * no point ever sits in a cell its queries do not reach.
118
+ *
119
+ * **Stored coordinates** (construction, `assign`, `add`) are finite numbers
120
+ * with |x|, |y| ≤ 1e150, in a y-down or y-up plane (the structure has no
121
+ * orientation). Anything larger, or non-finite, is a `RangeError`, checked
122
+ * before the tree changes. Queries never throw: a `find` coordinate that is
123
+ * NaN, infinite or beyond ±1e150 lies outside the supported range and has no
124
+ * result (−1); `forEachInBox` visits what lies inside the supported range (an
125
+ * infinite box lists every id), and nothing for a NaN or inverted box. Within
126
+ * the range no midpoint, cell edge or squared cell size overflows. At the tiny
127
+ * end `find` compares scaled distances (`vec2LengthScaled`), which do not
128
+ * underflow, so only cell pruning loses precision there (a cell closer than
129
+ * about 1e-154 counts as distance 0), costing speed, never correctness.
130
+ *
131
+ * Ids index the caller's own arrays; a caller with objects keeps them in an
132
+ * array beside the tree. Ids are never reissued: storage per id is kept until
133
+ * `clear` or `assign`, so a tree that adds and removes forever grows with the
134
+ * number of ids issued, not the number held; `clear()` it, or `assign` a fresh
135
+ * set, to reset.
136
+ *
137
+ * Aggregates (Barnes–Hut charge centres, collision radii, counts) are the
138
+ * caller's: allocate `Float64Array(tree.nodeCount() * k)` and fill it in
139
+ * `visitAfter`. The visitor gets each cell as a box the traversal reuses rather
140
+ * than four numbers, so the traversal itself allocates nothing per node whether
141
+ * or not the engine inlines the visitor. This is not a hard zero-allocation
142
+ * guarantee: before the engine optimises the traversal, and in some warmed runs
143
+ * that have not been explained, a call can still allocate a few kilobytes or
144
+ * more. Node numbers are dense (`0 … nodeCount() − 1`) and **valid until the
145
+ * next mutation** (`assign`, `add`, `remove`, `clear`); mutating the tree from
146
+ * inside a visitor is not supported. Queries and traversals may nest freely.
147
+ *
148
+ * Quadrant order is d3's: 0 top-left (x < mid, y < mid), 1 x ≥ mid, 2 y ≥ mid,
149
+ * 3 both. Coincident ids in a leaf are listed in ascending id order (d3 lists
150
+ * newest first; only sums over coincident points can differ).
151
+ *
152
+ * Complexity: d is the tree depth, O(log n) for spread points and at most about
153
+ * 1 600 levels in the supported range (about 500 doublings of the root cell to
154
+ * reach 1e150, plus 1 075 halvings down to the smallest subnormal). Build
155
+ * O(n·d); `add` O(d) plus one node per doubling when the root grows, and an
156
+ * O(n·d) rebuild over the n points held (not the ids issued) on the rare
157
+ * re-rooting (at most about twice per 2⁵⁰-fold growth of the root); `remove`
158
+ * O(d), however many points share the position; `find` expected O(log n) for
159
+ * spread points and O(n) when everything is clustered; `forEachInBox` O(d +
160
+ * cells overlapped + k); `visit` O(nodes visited). Every traversal and query
161
+ * uses an explicit stack, so depth never overflows the call stack. Memory O(ids
162
+ * issued + nodes), reused across `assign` calls (0 B after the first build of a
163
+ * given size).
164
+ */
165
+ export class Quadtree {
166
+ pointXs = new Float64Array(0);
167
+ pointYs = new Float64Array(0);
168
+ nextIds = new Int32Array(0);
169
+ prevIds = new Int32Array(0);
170
+ alive = new Uint8Array(0);
171
+ /** Ids issued since the last `clear`/`assign`. */
172
+ issued = 0;
173
+ size = 0;
174
+ firstIds = new Int32Array(0);
175
+ lastIds = new Int32Array(0);
176
+ parents = new Int32Array(0);
177
+ children = new Int32Array(0);
178
+ nodes = 0;
179
+ x0 = NaN;
180
+ y0 = NaN;
181
+ x1 = NaN;
182
+ y1 = NaN;
183
+ walks = [];
184
+ walkDepth = 0;
185
+ dead = [];
186
+ childKeys = new Float64Array(4);
187
+ /** Live ids gathered for a re-rooting rebuild. */
188
+ rebuildIds = new Int32Array(0);
189
+ /**
190
+ * @param points X, y per point; ids 0…n−1. @throws {RangeError} odd length, a
191
+ * non-finite coordinate or one beyond ±1e150 (naming the id).
192
+ */
193
+ constructor(points) {
194
+ if (points) {
195
+ this.assign(points);
196
+ }
197
+ }
198
+ /** Points held. O(1). */
199
+ count() {
200
+ return this.size;
201
+ }
202
+ isEmpty() {
203
+ return this.size === 0;
204
+ }
205
+ /** Removes every point; storage kept. Ids restart at 0. */
206
+ clear() {
207
+ this.alive.fill(0, 0, this.issued);
208
+ this.issued = 0;
209
+ this.size = 0;
210
+ this.nodes = 0;
211
+ this.resetExtent();
212
+ }
213
+ /**
214
+ * Replaces the contents with `points` (ids 0…n−1), reusing storage: what a
215
+ * force step calls each tick. O(n·d). @throws {RangeError} as the
216
+ * constructor, after checking every point and before any change: the tree is
217
+ * then unchanged.
218
+ */
219
+ assign(points) {
220
+ if (points.length % 2 !== 0) {
221
+ throw new RangeError(`Quadtree.assign: odd length ${points.length}, expected x, y pairs`);
222
+ }
223
+ const count = points.length / 2;
224
+ let minX = Infinity;
225
+ let minY = Infinity;
226
+ let maxX = -Infinity;
227
+ let maxY = -Infinity;
228
+ for (let id = 0; id < count; id++) {
229
+ const x = points[2 * id];
230
+ const y = points[2 * id + 1];
231
+ // Checked inline and called out only on failure: a call would box both doubles.
232
+ if (!(Math.abs(x) <= COORDINATE_LIMIT)) {
233
+ checkCoordinate(x, "Quadtree.assign", "x", id);
234
+ }
235
+ if (!(Math.abs(y) <= COORDINATE_LIMIT)) {
236
+ checkCoordinate(y, "Quadtree.assign", "y", id);
237
+ }
238
+ if (x < minX)
239
+ minX = x;
240
+ if (x > maxX)
241
+ maxX = x;
242
+ if (y < minY)
243
+ minY = y;
244
+ if (y > maxY)
245
+ maxY = y;
246
+ }
247
+ this.clear();
248
+ if (count === 0) {
249
+ return;
250
+ }
251
+ this.reservePoints(count);
252
+ this.cover(minX, minY);
253
+ this.cover(maxX, maxY);
254
+ for (let id = 0; id < count; id++) {
255
+ this.pointXs[id] = points[2 * id];
256
+ this.pointYs[id] = points[2 * id + 1];
257
+ this.append(id);
258
+ }
259
+ }
260
+ /**
261
+ * Adds a point and returns its id (one more than the last id issued since
262
+ * `clear`/`assign`). Grows the root cell as d3's `cover`. O(d) amortised. @throws
263
+ * {RangeError} non-finite coordinate or one beyond ±1e150; the tree is
264
+ * unchanged.
265
+ */
266
+ add(x, y) {
267
+ checkCoordinate(x, "Quadtree.add", "x", -1);
268
+ checkCoordinate(y, "Quadtree.add", "y", -1);
269
+ this.cover(x, y);
270
+ return this.addPoint(x, y);
271
+ }
272
+ /**
273
+ * Removes an id; false when absent. Collapses emptied cells (d3 `remove`).
274
+ * O(d). Ids are not reissued.
275
+ */
276
+ remove(id) {
277
+ if (!this.has(id)) {
278
+ return false;
279
+ }
280
+ const x = this.pointXs[id];
281
+ const y = this.pointYs[id];
282
+ let node = 0;
283
+ let parent = -1;
284
+ let quadrant = 0;
285
+ let retainer = -1;
286
+ let retainerQuadrant = 0;
287
+ let cx0 = this.x0;
288
+ let cy0 = this.y0;
289
+ let cx1 = this.x1;
290
+ let cy1 = this.y1;
291
+ while (this.firstIds[node] === -1) {
292
+ const midX = (cx0 + cx1) / 2;
293
+ const midY = (cy0 + cy1) / 2;
294
+ const right = x >= midX ? 1 : 0;
295
+ const bottom = y >= midY ? 1 : 0;
296
+ if (right)
297
+ cx0 = midX;
298
+ else
299
+ cx1 = midX;
300
+ if (bottom)
301
+ cy0 = midY;
302
+ else
303
+ cy1 = midY;
304
+ parent = node;
305
+ quadrant = (bottom << 1) | right;
306
+ node = this.children[parent * 4 + quadrant];
307
+ if (this.firstIds[node] !== -1) {
308
+ break;
309
+ }
310
+ if (this.hasOtherChild(parent, quadrant)) {
311
+ retainer = parent;
312
+ retainerQuadrant = quadrant;
313
+ }
314
+ }
315
+ this.alive[id] = 0;
316
+ this.size--;
317
+ const next = this.nextIds[id];
318
+ const previous = this.prevIds[id];
319
+ if (previous !== -1 || next !== -1) {
320
+ if (previous === -1) {
321
+ this.firstIds[node] = next;
322
+ }
323
+ else {
324
+ this.nextIds[previous] = next;
325
+ }
326
+ if (next === -1) {
327
+ this.lastIds[node] = previous;
328
+ }
329
+ else {
330
+ this.prevIds[next] = previous;
331
+ }
332
+ return true;
333
+ }
334
+ if (parent === -1) {
335
+ this.nodes = 0;
336
+ this.resetExtent();
337
+ return true;
338
+ }
339
+ this.children[parent * 4 + quadrant] = -1;
340
+ this.dead.length = 0;
341
+ this.dead.push(node);
342
+ const remaining = this.onlyChild(parent);
343
+ if (remaining !== -1 && this.firstIds[remaining] !== -1) {
344
+ let chain = retainer === -1
345
+ ? 0
346
+ : this.children[retainer * 4 + retainerQuadrant];
347
+ for (;;) {
348
+ if (chain !== 0) {
349
+ this.dead.push(chain);
350
+ }
351
+ if (chain === parent) {
352
+ break;
353
+ }
354
+ chain = this.onlyChild(chain);
355
+ }
356
+ if (retainer === -1) {
357
+ this.firstIds[0] = this.firstIds[remaining];
358
+ this.lastIds[0] = this.lastIds[remaining];
359
+ this.children.fill(-1, 0, 4);
360
+ this.dead.push(remaining);
361
+ }
362
+ else {
363
+ this.children[retainer * 4 + retainerQuadrant] = remaining;
364
+ this.parents[remaining] = retainer;
365
+ }
366
+ }
367
+ this.releaseDead();
368
+ return true;
369
+ }
370
+ has(id) {
371
+ return isIntegerInRange(id, 0, this.issued) && this.alive[id] === 1;
372
+ }
373
+ /** @throws {RangeError} Unknown id. */
374
+ pointX(id) {
375
+ this.checkId("pointX", id);
376
+ return this.pointXs[id];
377
+ }
378
+ /** @throws {RangeError} Unknown id. */
379
+ pointY(id) {
380
+ this.checkId("pointY", id);
381
+ return this.pointYs[id];
382
+ }
383
+ /** Root cell bounds; NaN when empty. */
384
+ get minX() {
385
+ return this.x0;
386
+ }
387
+ get minY() {
388
+ return this.y0;
389
+ }
390
+ get maxX() {
391
+ return this.x1;
392
+ }
393
+ get maxY() {
394
+ return this.y1;
395
+ }
396
+ /**
397
+ * Nearest id within `maxDistance` (default Infinity, inclusive), or −1; equal
398
+ * distances → lowest id. Distances are scaled (`vec2LengthScaled`), which
399
+ * does not underflow: a radius of 0 matches only coincident points. Never
400
+ * throws: a query coordinate that is NaN, infinite or beyond ±1e150 lies
401
+ * outside the supported range and has no result (−1), as does a negative or
402
+ * NaN `maxDistance`.
403
+ */
404
+ find(x, y, maxDistance = Infinity) {
405
+ if (!(Math.abs(x) <= COORDINATE_LIMIT) ||
406
+ !(Math.abs(y) <= COORDINATE_LIMIT) ||
407
+ this.nodes === 0 ||
408
+ !(maxDistance >= 0)) {
409
+ return -1;
410
+ }
411
+ const walk = this.acquireWalk();
412
+ try {
413
+ return this.findWith(walk, x, y, maxDistance);
414
+ }
415
+ finally {
416
+ this.walkDepth--;
417
+ }
418
+ }
419
+ /**
420
+ * Calls `visitor` for every id inside the closed box, in no promised order.
421
+ * Never throws. The box may be any numbers: a box reaching beyond the
422
+ * supported range (±1e150) visits what lies inside it, and an infinite box
423
+ * lists every id; an inverted or NaN box holds nothing. O(d + cells
424
+ * overlapped + k).
425
+ */
426
+ forEachInBox(minX, minY, maxX, maxY, visitor) {
427
+ if (this.nodes === 0) {
428
+ return;
429
+ }
430
+ const walk = this.acquireWalk();
431
+ try {
432
+ walk.query[0] = minX;
433
+ walk.query[1] = minY;
434
+ walk.query[2] = maxX;
435
+ walk.query[3] = maxY;
436
+ this.boxWith(walk, visitor);
437
+ }
438
+ finally {
439
+ this.walkDepth--;
440
+ }
441
+ }
442
+ /**
443
+ * Nodes (cells holding something), 0 when empty; node 0 is the root
444
+ * otherwise.
445
+ */
446
+ nodeCount() {
447
+ return this.nodes;
448
+ }
449
+ /** Pre-order from the root, quadrants 0→3. */
450
+ visit(visitor) {
451
+ this.walk(visitor, false);
452
+ }
453
+ /** Post-order: every child before its parent, quadrants 0→3. */
454
+ visitAfter(visitor) {
455
+ this.walk(visitor, true);
456
+ }
457
+ /** @throws {RangeError} Unknown node number. */
458
+ isLeaf(node) {
459
+ this.checkNode("isLeaf", node);
460
+ return this.firstIds[node] !== -1;
461
+ }
462
+ /**
463
+ * Child node in a quadrant, −1 when that quadrant is empty or `node` is a
464
+ * leaf.
465
+ */
466
+ nodeChild(node, quadrant) {
467
+ this.checkNode("nodeChild", node);
468
+ if (this.firstIds[node] !== -1) {
469
+ return -1;
470
+ }
471
+ return this.children[node * 4 + quadrant] ?? -1;
472
+ }
473
+ /** First id of a leaf (lowest), −1 for an internal node. */
474
+ nodeFirstPoint(node) {
475
+ this.checkNode("nodeFirstPoint", node);
476
+ return this.firstIds[node];
477
+ }
478
+ /**
479
+ * Next id at the same position, −1 after the last. @throws {RangeError}
480
+ * unknown id.
481
+ */
482
+ nextPoint(id) {
483
+ this.checkId("nextPoint", id);
484
+ return this.nextIds[id];
485
+ }
486
+ checkId(method, id) {
487
+ if (!this.has(id)) {
488
+ throw new RangeError(`Quadtree.${method}: unknown id ${describeValue(id)}`);
489
+ }
490
+ }
491
+ checkNode(method, node) {
492
+ if (!isIntegerInRange(node, 0, this.nodes)) {
493
+ throw new RangeError(`Quadtree.${method}: unknown node ${describeValue(node)}`);
494
+ }
495
+ }
496
+ resetExtent() {
497
+ this.x0 = NaN;
498
+ this.y0 = NaN;
499
+ this.x1 = NaN;
500
+ this.y1 = NaN;
501
+ }
502
+ reservePoints(capacity) {
503
+ if (capacity <= this.pointXs.length) {
504
+ return;
505
+ }
506
+ const size = Math.max(capacity, this.pointXs.length * 2, 16);
507
+ const pointXs = new Float64Array(size);
508
+ pointXs.set(this.pointXs);
509
+ const pointYs = new Float64Array(size);
510
+ pointYs.set(this.pointYs);
511
+ const nextIds = new Int32Array(size);
512
+ nextIds.set(this.nextIds);
513
+ const prevIds = new Int32Array(size);
514
+ prevIds.set(this.prevIds);
515
+ const alive = new Uint8Array(size);
516
+ alive.set(this.alive);
517
+ this.pointXs = pointXs;
518
+ this.pointYs = pointYs;
519
+ this.nextIds = nextIds;
520
+ this.prevIds = prevIds;
521
+ this.alive = alive;
522
+ }
523
+ allocNode(parent, firstId) {
524
+ if (this.nodes === this.firstIds.length) {
525
+ const size = Math.max(this.nodes * 2, 16);
526
+ const firstIds = new Int32Array(size);
527
+ firstIds.set(this.firstIds);
528
+ const lastIds = new Int32Array(size);
529
+ lastIds.set(this.lastIds);
530
+ const parents = new Int32Array(size);
531
+ parents.set(this.parents);
532
+ const children = new Int32Array(size * 4);
533
+ children.set(this.children);
534
+ this.firstIds = firstIds;
535
+ this.lastIds = lastIds;
536
+ this.parents = parents;
537
+ this.children = children;
538
+ }
539
+ const node = this.nodes++;
540
+ this.firstIds[node] = firstId;
541
+ this.lastIds[node] = firstId;
542
+ this.parents[node] = parent;
543
+ this.children.fill(-1, node * 4, node * 4 + 4);
544
+ return node;
545
+ }
546
+ hasOtherChild(node, quadrant) {
547
+ for (let other = 0; other < 4; other++) {
548
+ if (other !== quadrant && this.children[node * 4 + other] !== -1) {
549
+ return true;
550
+ }
551
+ }
552
+ return false;
553
+ }
554
+ /** The single child of `node`, or −1 when it has none or several. */
555
+ onlyChild(node) {
556
+ let found = -1;
557
+ for (let quadrant = 0; quadrant < 4; quadrant++) {
558
+ const child = this.children[node * 4 + quadrant];
559
+ if (child !== -1) {
560
+ if (found !== -1) {
561
+ return -1;
562
+ }
563
+ found = child;
564
+ }
565
+ }
566
+ return found;
567
+ }
568
+ /**
569
+ * Frees the nodes in `dead` in O(their number), keeping node numbers dense by
570
+ * moving the last live node into each hole.
571
+ */
572
+ releaseDead() {
573
+ const dead = this.dead;
574
+ for (let index = 0; index < dead.length; index++) {
575
+ this.parents[dead[index]] = DEAD;
576
+ }
577
+ for (let index = 0; index < dead.length; index++) {
578
+ const hole = dead[index];
579
+ while (this.parents[this.nodes - 1] === DEAD) {
580
+ this.nodes--;
581
+ }
582
+ if (hole >= this.nodes) {
583
+ continue;
584
+ }
585
+ const last = this.nodes - 1;
586
+ this.firstIds[hole] = this.firstIds[last];
587
+ this.lastIds[hole] = this.lastIds[last];
588
+ const parent = this.parents[last];
589
+ this.parents[hole] = parent;
590
+ for (let quadrant = 0; quadrant < 4; quadrant++) {
591
+ const child = this.children[last * 4 + quadrant];
592
+ this.children[hole * 4 + quadrant] = child;
593
+ if (child !== -1) {
594
+ this.parents[child] = hole;
595
+ }
596
+ }
597
+ if (parent !== -1) {
598
+ for (let quadrant = 0; quadrant < 4; quadrant++) {
599
+ if (this.children[parent * 4 + quadrant] === last) {
600
+ this.children[parent * 4 + quadrant] = hole;
601
+ }
602
+ }
603
+ }
604
+ this.nodes--;
605
+ }
606
+ dead.length = 0;
607
+ }
608
+ /**
609
+ * Grows the root cell until it holds (x, y): d3-quadtree's `cover`, as long
610
+ * as the doubled cell has the old root as an exact quadrant; otherwise the
611
+ * tree is re-rooted on exactly representable edges and rebuilt.
612
+ */
613
+ cover(x, y) {
614
+ if (Number.isNaN(this.x0)) {
615
+ const x0 = Math.floor(x);
616
+ const y0 = Math.floor(y);
617
+ if (x0 + 1 - x0 === 1 && y0 + 1 - y0 === 1) {
618
+ this.x0 = x0;
619
+ this.y0 = y0;
620
+ this.x1 = x0 + 1;
621
+ this.y1 = y0 + 1;
622
+ }
623
+ else {
624
+ this.reroot(x, y, x, y);
625
+ }
626
+ return;
627
+ }
628
+ while (this.x0 > x || x >= this.x1 || this.y0 > y || y >= this.y1) {
629
+ if (!this.double(x, y)) {
630
+ this.reroot(Math.min(this.x0, x), Math.min(this.y0, y), Math.max(this.x1, x), Math.max(this.y1, y));
631
+ return;
632
+ }
633
+ }
634
+ }
635
+ /**
636
+ * Doubles the root cell toward (x, y), anchored on the opposite corner.
637
+ * Refused when rounding would make the old root anything but the new root's
638
+ * quadrant as every traversal computes it: the existing cells would move.
639
+ */
640
+ double(x, y) {
641
+ const left = x < this.x0;
642
+ const top = y < this.y0;
643
+ const quadrant = (top ? 2 : 0) | (left ? 1 : 0);
644
+ const size = (this.x1 - this.x0) * 2;
645
+ const x0 = left ? this.x1 - size : this.x0;
646
+ const x1 = left ? this.x1 : this.x0 + size;
647
+ const y0 = top ? this.y1 - size : this.y0;
648
+ const y1 = top ? this.y1 : this.y0 + size;
649
+ const midX = (x0 + x1) / 2;
650
+ const midY = (y0 + y1) / 2;
651
+ if (midX !== (left ? this.x0 : this.x1) ||
652
+ midY !== (top ? this.y0 : this.y1)) {
653
+ return false;
654
+ }
655
+ if (this.nodes > 0 && this.firstIds[0] === -1) {
656
+ this.wrapRoot(quadrant);
657
+ }
658
+ this.x0 = x0;
659
+ this.y0 = y0;
660
+ this.x1 = x1;
661
+ this.y1 = y1;
662
+ return true;
663
+ }
664
+ /**
665
+ * Replaces the root by a square cell holding the closed box, whose edges are
666
+ * multiples of a quarter of its power-of-two size, every one exactly
667
+ * representable; later doublings then stay exact until the root has grown
668
+ * about 2⁵⁰-fold or past the box's magnitude. Reinserts the points, in
669
+ * ascending id order.
670
+ */
671
+ reroot(loX, loY, hiX, hiY) {
672
+ const magnitude = Math.max(Math.abs(loX), Math.abs(loY), Math.abs(hiX), Math.abs(hiY));
673
+ let size = 4 * ulp(magnitude);
674
+ for (;; size *= 2) {
675
+ const unit = size / 4;
676
+ if (unit < ulp(magnitude + size)) {
677
+ continue;
678
+ }
679
+ const x0 = alignDown_(loX, unit);
680
+ const y0 = alignDown_(loY, unit);
681
+ if (x0 + size > hiX && y0 + size > hiY) {
682
+ this.x0 = x0;
683
+ this.y0 = y0;
684
+ this.x1 = x0 + size;
685
+ this.y1 = y0 + size;
686
+ break;
687
+ }
688
+ }
689
+ if (this.nodes > 1) {
690
+ this.reinsertLive();
691
+ }
692
+ }
693
+ /**
694
+ * Rebuilds the tree under the current root from the leaves' id lists, so it
695
+ * costs O(nodes + live points · d), whatever the number of ids issued. Each
696
+ * leaf's ids go back in list order, which keeps them ascending.
697
+ */
698
+ reinsertLive() {
699
+ if (this.rebuildIds.length < this.size) {
700
+ this.rebuildIds = new Int32Array(Math.max(this.size, this.rebuildIds.length * 2));
701
+ }
702
+ const ids = this.rebuildIds;
703
+ let count = 0;
704
+ for (let node = 0; node < this.nodes; node++) {
705
+ for (let id = this.firstIds[node]; id !== -1; id = this.nextIds[id]) {
706
+ ids[count++] = id;
707
+ }
708
+ }
709
+ this.nodes = 0;
710
+ for (let index = 0; index < count; index++) {
711
+ const id = ids[index];
712
+ this.nextIds[id] = -1;
713
+ this.prevIds[id] = -1;
714
+ this.insert(id);
715
+ }
716
+ }
717
+ /** Pushes the internal root down one level, so node 0 stays the root. */
718
+ wrapRoot(quadrant) {
719
+ const moved = this.allocNode(0, -1);
720
+ for (let other = 0; other < 4; other++) {
721
+ const child = this.children[other];
722
+ this.children[moved * 4 + other] = child;
723
+ if (child !== -1) {
724
+ this.parents[child] = moved;
725
+ }
726
+ this.children[other] = -1;
727
+ }
728
+ this.children[quadrant] = moved;
729
+ }
730
+ addPoint(x, y) {
731
+ const id = this.issued;
732
+ this.reservePoints(id + 1);
733
+ this.pointXs[id] = x;
734
+ this.pointYs[id] = y;
735
+ this.append(id);
736
+ return id;
737
+ }
738
+ /** Links the stored point `id` into the tree, then publishes it. */
739
+ append(id) {
740
+ this.nextIds[id] = -1;
741
+ this.prevIds[id] = -1;
742
+ this.insert(id);
743
+ this.alive[id] = 1;
744
+ this.issued = id + 1;
745
+ this.size++;
746
+ }
747
+ /**
748
+ * Inside the coordinate range every cell holds the points it receives in
749
+ * `[min, max)`, so two distinct points always separate.
750
+ */
751
+ insert(id) {
752
+ if (this.nodes === 0) {
753
+ this.allocNode(-1, id);
754
+ return;
755
+ }
756
+ const x = this.pointXs[id];
757
+ const y = this.pointYs[id];
758
+ let node = 0;
759
+ let cx0 = this.x0;
760
+ let cy0 = this.y0;
761
+ let cx1 = this.x1;
762
+ let cy1 = this.y1;
763
+ while (this.firstIds[node] === -1) {
764
+ const midX = (cx0 + cx1) / 2;
765
+ const midY = (cy0 + cy1) / 2;
766
+ const right = x >= midX ? 1 : 0;
767
+ const bottom = y >= midY ? 1 : 0;
768
+ if (right)
769
+ cx0 = midX;
770
+ else
771
+ cx1 = midX;
772
+ if (bottom)
773
+ cy0 = midY;
774
+ else
775
+ cy1 = midY;
776
+ const quadrant = (bottom << 1) | right;
777
+ const child = this.children[node * 4 + quadrant];
778
+ if (child === -1) {
779
+ const leaf = this.allocNode(node, id);
780
+ this.children[node * 4 + quadrant] = leaf;
781
+ return;
782
+ }
783
+ node = child;
784
+ }
785
+ const otherId = this.firstIds[node];
786
+ const otherX = this.pointXs[otherId];
787
+ const otherY = this.pointYs[otherId];
788
+ if (otherX === x && otherY === y) {
789
+ const last = this.lastIds[node];
790
+ this.nextIds[last] = id;
791
+ this.prevIds[id] = last;
792
+ this.lastIds[node] = id;
793
+ return;
794
+ }
795
+ const oldLeaf = this.allocNode(node, otherId);
796
+ this.lastIds[oldLeaf] = this.lastIds[node];
797
+ this.firstIds[node] = -1;
798
+ let cell = node;
799
+ for (;;) {
800
+ const midX = (cx0 + cx1) / 2;
801
+ const midY = (cy0 + cy1) / 2;
802
+ const newRight = x >= midX ? 1 : 0;
803
+ const newBottom = y >= midY ? 1 : 0;
804
+ const newQuadrant = (newBottom << 1) | newRight;
805
+ const oldQuadrant = (otherY >= midY ? 2 : 0) | (otherX >= midX ? 1 : 0);
806
+ if (newQuadrant !== oldQuadrant) {
807
+ this.children[cell * 4 + oldQuadrant] = oldLeaf;
808
+ this.parents[oldLeaf] = cell;
809
+ const leaf = this.allocNode(cell, id);
810
+ this.children[cell * 4 + newQuadrant] = leaf;
811
+ return;
812
+ }
813
+ if (newRight)
814
+ cx0 = midX;
815
+ else
816
+ cx1 = midX;
817
+ if (newBottom)
818
+ cy0 = midY;
819
+ else
820
+ cy1 = midY;
821
+ const deeper = this.allocNode(cell, -1);
822
+ this.children[cell * 4 + newQuadrant] = deeper;
823
+ cell = deeper;
824
+ }
825
+ }
826
+ acquireWalk() {
827
+ if (this.walkDepth === this.walks.length) {
828
+ this.walks.push(new Walk());
829
+ }
830
+ return this.walks[this.walkDepth++];
831
+ }
832
+ /**
833
+ * Branch and bound, nearest cell first; `walk.keys` holds each pending cell's
834
+ * distance.
835
+ */
836
+ findWith(walk, x, y, maxDistance) {
837
+ const { cell, delta, point } = walk;
838
+ const childKeys = this.childKeys;
839
+ point[0] = x;
840
+ point[1] = y;
841
+ let best = maxDistance;
842
+ let bestId = -1;
843
+ let top = 0;
844
+ this.setRoot(walk);
845
+ walk.keys[0] = 0;
846
+ while (top >= 0) {
847
+ const frame = top--;
848
+ if (walk.keys[frame] > best * PRUNE_SLACK) {
849
+ continue;
850
+ }
851
+ const node = walk.nodes[frame];
852
+ const first = this.firstIds[node];
853
+ if (first !== -1) {
854
+ delta[0] = this.pointXs[first] - x;
855
+ delta[1] = this.pointYs[first] - y;
856
+ const distance = vec2LengthScaled(delta);
857
+ if (distance < best ||
858
+ (distance === best && (bestId === -1 || first < bestId))) {
859
+ best = distance;
860
+ bestId = first;
861
+ }
862
+ continue;
863
+ }
864
+ walk.openCell(frame);
865
+ for (let quadrant = 0; quadrant < 4; quadrant++) {
866
+ if (this.children[node * 4 + quadrant] === -1) {
867
+ childKeys[quadrant] = -1;
868
+ continue;
869
+ }
870
+ walk.setCell(quadrant);
871
+ childKeys[quadrant] = box2DistanceToPoint(cell, point);
872
+ }
873
+ // Farthest first, so the nearest cell is popped next.
874
+ for (let round = 0; round < 4; round++) {
875
+ let pick = -1;
876
+ let pickKey = -1;
877
+ for (let quadrant = 0; quadrant < 4; quadrant++) {
878
+ const key = childKeys[quadrant];
879
+ if (key > pickKey) {
880
+ pick = quadrant;
881
+ pickKey = key;
882
+ }
883
+ }
884
+ if (pick === -1) {
885
+ break;
886
+ }
887
+ childKeys[pick] = -1;
888
+ top++;
889
+ if (top === walk.nodes.length) {
890
+ walk.grow();
891
+ }
892
+ walk.pushChild(top, this.children[node * 4 + pick], pick);
893
+ walk.keys[top] = pickKey;
894
+ }
895
+ }
896
+ return bestId;
897
+ }
898
+ setRoot(walk) {
899
+ walk.nodes[0] = 0;
900
+ walk.bounds[0] = this.x0;
901
+ walk.bounds[1] = this.y0;
902
+ walk.bounds[2] = this.x1;
903
+ walk.bounds[3] = this.y1;
904
+ }
905
+ boxWith(walk, visitor) {
906
+ const { cell, query } = walk;
907
+ let top = 0;
908
+ this.setRoot(walk);
909
+ while (top >= 0) {
910
+ const frame = top--;
911
+ const node = walk.nodes[frame];
912
+ const base = frame * 4;
913
+ cell[0] = walk.bounds[base];
914
+ cell[1] = walk.bounds[base + 1];
915
+ cell[2] = walk.bounds[base + 2];
916
+ cell[3] = walk.bounds[base + 3];
917
+ if (!box2IntersectsBox2(cell, query)) {
918
+ continue;
919
+ }
920
+ const first = this.firstIds[node];
921
+ if (first !== -1) {
922
+ if (box2ContainsXy(query, this.pointXs[first], this.pointYs[first])) {
923
+ for (let id = first; id !== -1; id = this.nextIds[id]) {
924
+ visitor(id);
925
+ }
926
+ }
927
+ continue;
928
+ }
929
+ walk.openCell(frame);
930
+ for (let quadrant = 3; quadrant >= 0; quadrant--) {
931
+ const child = this.children[node * 4 + quadrant];
932
+ if (child === -1) {
933
+ continue;
934
+ }
935
+ top++;
936
+ if (top === walk.nodes.length) {
937
+ walk.grow();
938
+ }
939
+ walk.pushChild(top, child, quadrant);
940
+ }
941
+ }
942
+ }
943
+ walk(visitor, after) {
944
+ if (this.nodes === 0) {
945
+ return;
946
+ }
947
+ const walk = this.acquireWalk();
948
+ try {
949
+ this.walkWith(walk, visitor, after);
950
+ }
951
+ finally {
952
+ this.walkDepth--;
953
+ }
954
+ }
955
+ walkWith(walk, visitor, after) {
956
+ const visited = walk.visited;
957
+ let depth = 0;
958
+ this.setRoot(walk);
959
+ walk.quadrants[0] = 0;
960
+ const rootIsLeaf = this.firstIds[0] !== -1;
961
+ if (!after || rootIsLeaf) {
962
+ visited[0] = this.x0;
963
+ visited[1] = this.y0;
964
+ visited[2] = this.x1;
965
+ visited[3] = this.y1;
966
+ const skipped = visitor(0, visited) === true && !after;
967
+ if (skipped || rootIsLeaf) {
968
+ return;
969
+ }
970
+ }
971
+ while (depth >= 0) {
972
+ const node = walk.nodes[depth];
973
+ const quadrant = walk.quadrants[depth];
974
+ const base = depth * 4;
975
+ const cx0 = walk.bounds[base];
976
+ const cy0 = walk.bounds[base + 1];
977
+ const cx1 = walk.bounds[base + 2];
978
+ const cy1 = walk.bounds[base + 3];
979
+ if (quadrant === 4) {
980
+ if (after) {
981
+ visited[0] = cx0;
982
+ visited[1] = cy0;
983
+ visited[2] = cx1;
984
+ visited[3] = cy1;
985
+ visitor(node, visited);
986
+ }
987
+ depth--;
988
+ continue;
989
+ }
990
+ walk.quadrants[depth] = quadrant + 1;
991
+ const child = this.children[node * 4 + quadrant];
992
+ if (child === -1) {
993
+ continue;
994
+ }
995
+ const midX = (cx0 + cx1) / 2;
996
+ const midY = (cy0 + cy1) / 2;
997
+ const right = (quadrant & 1) === 1;
998
+ const bottom = quadrant >= 2;
999
+ const isLeaf = this.firstIds[child] !== -1;
1000
+ if (isLeaf || !after) {
1001
+ visited[0] = right ? midX : cx0;
1002
+ visited[1] = bottom ? midY : cy0;
1003
+ visited[2] = right ? cx1 : midX;
1004
+ visited[3] = bottom ? cy1 : midY;
1005
+ if (visitor(child, visited) === true || isLeaf) {
1006
+ continue;
1007
+ }
1008
+ }
1009
+ depth++;
1010
+ if (depth === walk.nodes.length) {
1011
+ walk.grow();
1012
+ }
1013
+ walk.openCell(depth - 1);
1014
+ walk.pushChild(depth, child, quadrant);
1015
+ walk.quadrants[depth] = 0;
1016
+ }
1017
+ }
1018
+ }
1019
+ //# sourceMappingURL=quadtree.js.map