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