@ac-kit/math-geometry 0.3.0 → 0.5.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 (75) hide show
  1. package/CHANGELOG.md +177 -0
  2. package/README.md +76 -5
  3. package/dist/_polygon3-cross.d.ts +23 -0
  4. package/dist/_polygon3-cross.d.ts.map +1 -0
  5. package/dist/_polygon3-cross.js +418 -0
  6. package/dist/_polygon3-cross.js.map +1 -0
  7. package/dist/_polyline-simplify.d.ts +11 -0
  8. package/dist/_polyline-simplify.d.ts.map +1 -0
  9. package/dist/_polyline-simplify.js +92 -0
  10. package/dist/_polyline-simplify.js.map +1 -0
  11. package/dist/_triangle3-cross.d.ts +66 -0
  12. package/dist/_triangle3-cross.d.ts.map +1 -0
  13. package/dist/_triangle3-cross.js +405 -0
  14. package/dist/_triangle3-cross.js.map +1 -0
  15. package/dist/convex-hull2.d.ts +26 -0
  16. package/dist/convex-hull2.d.ts.map +1 -0
  17. package/dist/convex-hull2.js +88 -0
  18. package/dist/convex-hull2.js.map +1 -0
  19. package/dist/index.d.ts +2 -0
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +2 -0
  22. package/dist/index.js.map +1 -1
  23. package/dist/line2.d.ts +4 -0
  24. package/dist/line2.d.ts.map +1 -1
  25. package/dist/line2.js +26 -5
  26. package/dist/line2.js.map +1 -1
  27. package/dist/mesh/_index-kernels-u16.d.ts +46 -11
  28. package/dist/mesh/_index-kernels-u16.d.ts.map +1 -1
  29. package/dist/mesh/_index-kernels-u16.js +1442 -132
  30. package/dist/mesh/_index-kernels-u16.js.map +1 -1
  31. package/dist/mesh/_index-kernels-u32.d.ts +46 -11
  32. package/dist/mesh/_index-kernels-u32.d.ts.map +1 -1
  33. package/dist/mesh/_index-kernels-u32.js +1442 -132
  34. package/dist/mesh/_index-kernels-u32.js.map +1 -1
  35. package/dist/mesh/mesh3-kernels.d.ts +104 -20
  36. package/dist/mesh/mesh3-kernels.d.ts.map +1 -1
  37. package/dist/mesh/mesh3-kernels.js +123 -25
  38. package/dist/mesh/mesh3-kernels.js.map +1 -1
  39. package/dist/plane3.d.ts +4 -3
  40. package/dist/plane3.d.ts.map +1 -1
  41. package/dist/plane3.js +18 -35
  42. package/dist/plane3.js.map +1 -1
  43. package/dist/polygon2.d.ts +76 -4
  44. package/dist/polygon2.d.ts.map +1 -1
  45. package/dist/polygon2.js +366 -7
  46. package/dist/polygon2.js.map +1 -1
  47. package/dist/polygon3.d.ts +19 -3
  48. package/dist/polygon3.d.ts.map +1 -1
  49. package/dist/polygon3.js +40 -16
  50. package/dist/polygon3.js.map +1 -1
  51. package/dist/polyline2.d.ts +20 -0
  52. package/dist/polyline2.d.ts.map +1 -1
  53. package/dist/polyline2.js +25 -0
  54. package/dist/polyline2.js.map +1 -1
  55. package/dist/polyline3.d.ts +17 -0
  56. package/dist/polyline3.d.ts.map +1 -1
  57. package/dist/polyline3.js +22 -0
  58. package/dist/polyline3.js.map +1 -1
  59. package/dist/predicates2.d.ts +51 -0
  60. package/dist/predicates2.d.ts.map +1 -0
  61. package/dist/predicates2.js +172 -0
  62. package/dist/predicates2.js.map +1 -0
  63. package/dist/quadtree.d.ts +40 -23
  64. package/dist/quadtree.d.ts.map +1 -1
  65. package/dist/quadtree.js +196 -119
  66. package/dist/quadtree.js.map +1 -1
  67. package/dist/segment2.d.ts +6 -1
  68. package/dist/segment2.d.ts.map +1 -1
  69. package/dist/segment2.js +20 -4
  70. package/dist/segment2.js.map +1 -1
  71. package/dist/triangle3.d.ts +20 -1
  72. package/dist/triangle3.d.ts.map +1 -1
  73. package/dist/triangle3.js +38 -18
  74. package/dist/triangle3.js.map +1 -1
  75. package/package.json +8 -8
package/dist/quadtree.js CHANGED
@@ -1,9 +1,8 @@
1
1
  import { describeValue } from "@ac-kit/core";
2
2
  import { assertFinite, isIntegerInRange, ulp } from "@ac-kit/core";
3
3
  import { vec2Create, vec2LengthScaled } from "@ac-kit/math-linear";
4
- import { box2DistanceToPoint, box2IntersectsBox2, } from "./box2-componentwise.js";
4
+ import { box2ContainsPoint, box2DistanceToPoint, box2IntersectsBox2, } from "./box2-componentwise.js";
5
5
  import { box2Create } from "./box2-record.js";
6
- import { box2ContainsXy } from "./box2.js";
7
6
  /** Every coordinate a tree accepts lies within ±this; see {@link Quadtree}. */
8
7
  const COORDINATE_LIMIT = 1e150;
9
8
  /** Marks a node being freed while the tree is compacted. */
@@ -61,8 +60,8 @@ class Walk {
61
60
  this.keys = keys;
62
61
  }
63
62
  /**
64
- * Makes frame `frame`'s cell the parent for `pushChild` and `setCell`; the
65
- * frame may be overwritten afterwards.
63
+ * Makes frame `frame`'s cell the parent for `pushChild` and for the child
64
+ * cells `find` measures; the frame may be overwritten afterwards.
66
65
  */
67
66
  openCell(frame) {
68
67
  const base = frame * 4;
@@ -136,12 +135,14 @@ class Walk {
136
135
  *
137
136
  * Aggregates (Barnes–Hut charge centres, collision radii, counts) are the
138
137
  * 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
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
145
146
  * next mutation** (`assign`, `add`, `remove`, `clear`); mutating the tree from
146
147
  * inside a visitor is not supported. Queries and traversals may nest freely.
147
148
  *
@@ -176,13 +177,23 @@ export class Quadtree {
176
177
  parents = new Int32Array(0);
177
178
  children = new Int32Array(0);
178
179
  nodes = 0;
179
- x0 = NaN;
180
- y0 = NaN;
181
- x1 = NaN;
182
- y1 = NaN;
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);
183
192
  walks = [];
184
193
  walkDepth = 0;
185
- dead = [];
194
+ /** Nodes being freed by `remove`: `deadCount` of them. */
195
+ dead = new Int32Array(16);
196
+ deadCount = 0;
186
197
  childKeys = new Float64Array(4);
187
198
  /** Live ids gathered for a re-rooting rebuild. */
188
199
  rebuildIds = new Int32Array(0);
@@ -235,22 +246,30 @@ export class Quadtree {
235
246
  if (!(Math.abs(y) <= COORDINATE_LIMIT)) {
236
247
  checkCoordinate(y, "Quadtree.assign", "y", id);
237
248
  }
238
- if (x < minX)
249
+ if (x < minX) {
239
250
  minX = x;
240
- if (x > maxX)
251
+ }
252
+ if (x > maxX) {
241
253
  maxX = x;
242
- if (y < minY)
254
+ }
255
+ if (y < minY) {
243
256
  minY = y;
244
- if (y > maxY)
257
+ }
258
+ if (y > maxY) {
245
259
  maxY = y;
260
+ }
246
261
  }
247
262
  this.clear();
248
263
  if (count === 0) {
249
264
  return;
250
265
  }
251
266
  this.reservePoints(count);
252
- this.cover(minX, minY);
253
- this.cover(maxX, maxY);
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();
254
273
  for (let id = 0; id < count; id++) {
255
274
  this.pointXs[id] = points[2 * id];
256
275
  this.pointYs[id] = points[2 * id + 1];
@@ -266,7 +285,9 @@ export class Quadtree {
266
285
  add(x, y) {
267
286
  checkCoordinate(x, "Quadtree.add", "x", -1);
268
287
  checkCoordinate(y, "Quadtree.add", "y", -1);
269
- this.cover(x, y);
288
+ this.target[0] = x;
289
+ this.target[1] = y;
290
+ this.cover();
270
291
  return this.addPoint(x, y);
271
292
  }
272
293
  /**
@@ -284,23 +305,27 @@ export class Quadtree {
284
305
  let quadrant = 0;
285
306
  let retainer = -1;
286
307
  let retainerQuadrant = 0;
287
- let cx0 = this.x0;
288
- let cy0 = this.y0;
289
- let cx1 = this.x1;
290
- let cy1 = this.y1;
308
+ let cx0 = this.root[0];
309
+ let cy0 = this.root[1];
310
+ let cx1 = this.root[2];
311
+ let cy1 = this.root[3];
291
312
  while (this.firstIds[node] === -1) {
292
313
  const midX = (cx0 + cx1) / 2;
293
314
  const midY = (cy0 + cy1) / 2;
294
315
  const right = x >= midX ? 1 : 0;
295
316
  const bottom = y >= midY ? 1 : 0;
296
- if (right)
317
+ if (right) {
297
318
  cx0 = midX;
298
- else
319
+ }
320
+ else {
299
321
  cx1 = midX;
300
- if (bottom)
322
+ }
323
+ if (bottom) {
301
324
  cy0 = midY;
302
- else
325
+ }
326
+ else {
303
327
  cy1 = midY;
328
+ }
304
329
  parent = node;
305
330
  quadrant = (bottom << 1) | right;
306
331
  node = this.children[parent * 4 + quadrant];
@@ -337,16 +362,14 @@ export class Quadtree {
337
362
  return true;
338
363
  }
339
364
  this.children[parent * 4 + quadrant] = -1;
340
- this.dead.length = 0;
341
- this.dead.push(node);
365
+ this.deadCount = 0;
366
+ this.pushDead(node);
342
367
  const remaining = this.onlyChild(parent);
343
368
  if (remaining !== -1 && this.firstIds[remaining] !== -1) {
344
- let chain = retainer === -1
345
- ? 0
346
- : this.children[retainer * 4 + retainerQuadrant];
369
+ let chain = retainer === -1 ? 0 : this.children[retainer * 4 + retainerQuadrant];
347
370
  for (;;) {
348
371
  if (chain !== 0) {
349
- this.dead.push(chain);
372
+ this.pushDead(chain);
350
373
  }
351
374
  if (chain === parent) {
352
375
  break;
@@ -357,7 +380,7 @@ export class Quadtree {
357
380
  this.firstIds[0] = this.firstIds[remaining];
358
381
  this.lastIds[0] = this.lastIds[remaining];
359
382
  this.children.fill(-1, 0, 4);
360
- this.dead.push(remaining);
383
+ this.pushDead(remaining);
361
384
  }
362
385
  else {
363
386
  this.children[retainer * 4 + retainerQuadrant] = remaining;
@@ -382,16 +405,16 @@ export class Quadtree {
382
405
  }
383
406
  /** Root cell bounds; NaN when empty. */
384
407
  get minX() {
385
- return this.x0;
408
+ return this.root[0];
386
409
  }
387
410
  get minY() {
388
- return this.y0;
411
+ return this.root[1];
389
412
  }
390
413
  get maxX() {
391
- return this.x1;
414
+ return this.root[2];
392
415
  }
393
416
  get maxY() {
394
- return this.y1;
417
+ return this.root[3];
395
418
  }
396
419
  /**
397
420
  * Nearest id within `maxDistance` (default Infinity, inclusive), or −1; equal
@@ -410,7 +433,10 @@ export class Quadtree {
410
433
  }
411
434
  const walk = this.acquireWalk();
412
435
  try {
413
- return this.findWith(walk, x, y, maxDistance);
436
+ walk.point[0] = x;
437
+ walk.point[1] = y;
438
+ walk.keys[0] = maxDistance;
439
+ return this.findWith(walk);
414
440
  }
415
441
  finally {
416
442
  this.walkDepth--;
@@ -494,10 +520,8 @@ export class Quadtree {
494
520
  }
495
521
  }
496
522
  resetExtent() {
497
- this.x0 = NaN;
498
- this.y0 = NaN;
499
- this.x1 = NaN;
500
- this.y1 = NaN;
523
+ this.root.fill(NaN);
524
+ this.hasRoot = false;
501
525
  }
502
526
  reservePoints(capacity) {
503
527
  if (capacity <= this.pointXs.length) {
@@ -565,16 +589,25 @@ export class Quadtree {
565
589
  }
566
590
  return found;
567
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
+ }
568
600
  /**
569
601
  * Frees the nodes in `dead` in O(their number), keeping node numbers dense by
570
602
  * moving the last live node into each hole.
571
603
  */
572
604
  releaseDead() {
573
605
  const dead = this.dead;
574
- for (let index = 0; index < dead.length; index++) {
606
+ const count = this.deadCount;
607
+ for (let index = 0; index < count; index++) {
575
608
  this.parents[dead[index]] = DEAD;
576
609
  }
577
- for (let index = 0; index < dead.length; index++) {
610
+ for (let index = 0; index < count; index++) {
578
611
  const hole = dead[index];
579
612
  while (this.parents[this.nodes - 1] === DEAD) {
580
613
  this.nodes--;
@@ -603,72 +636,98 @@ export class Quadtree {
603
636
  }
604
637
  this.nodes--;
605
638
  }
606
- dead.length = 0;
639
+ this.deadCount = 0;
607
640
  }
608
641
  /**
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.
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.
612
647
  */
613
- cover(x, y) {
614
- if (Number.isNaN(this.x0)) {
615
- const x0 = Math.floor(x);
616
- const y0 = Math.floor(y);
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]);
617
654
  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;
655
+ root[0] = x0;
656
+ root[1] = y0;
657
+ root[2] = x0 + 1;
658
+ root[3] = y0 + 1;
659
+ this.hasRoot = true;
622
660
  }
623
661
  else {
624
- this.reroot(x, y, x, y);
662
+ target[2] = target[0];
663
+ target[3] = target[1];
664
+ this.reroot();
625
665
  }
626
666
  return;
627
667
  }
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));
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();
631
680
  return;
632
681
  }
633
682
  }
634
683
  }
635
684
  /**
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.
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.
639
689
  */
640
- double(x, y) {
641
- const left = x < this.x0;
642
- const top = y < this.y0;
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;
643
698
  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;
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;
649
704
  const midX = (x0 + x1) / 2;
650
705
  const midY = (y0 + y1) / 2;
651
- if (midX !== (left ? this.x0 : this.x1) ||
652
- midY !== (top ? this.y0 : this.y1)) {
706
+ if (midX !== (left ? rx0 : rx1) || midY !== (top ? ry0 : ry1)) {
653
707
  return false;
654
708
  }
655
709
  if (this.nodes > 0 && this.firstIds[0] === -1) {
656
710
  this.wrapRoot(quadrant);
657
711
  }
658
- this.x0 = x0;
659
- this.y0 = y0;
660
- this.x1 = x1;
661
- this.y1 = y1;
712
+ root[0] = x0;
713
+ root[1] = y0;
714
+ root[2] = x1;
715
+ root[3] = y1;
662
716
  return true;
663
717
  }
664
718
  /**
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.
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.
670
724
  */
671
- reroot(loX, loY, hiX, hiY) {
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];
672
731
  const magnitude = Math.max(Math.abs(loX), Math.abs(loY), Math.abs(hiX), Math.abs(hiY));
673
732
  let size = 4 * ulp(magnitude);
674
733
  for (;; size *= 2) {
@@ -679,10 +738,12 @@ export class Quadtree {
679
738
  const x0 = alignDown_(loX, unit);
680
739
  const y0 = alignDown_(loY, unit);
681
740
  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;
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;
686
747
  break;
687
748
  }
688
749
  }
@@ -756,23 +817,27 @@ export class Quadtree {
756
817
  const x = this.pointXs[id];
757
818
  const y = this.pointYs[id];
758
819
  let node = 0;
759
- let cx0 = this.x0;
760
- let cy0 = this.y0;
761
- let cx1 = this.x1;
762
- let cy1 = this.y1;
820
+ let cx0 = this.root[0];
821
+ let cy0 = this.root[1];
822
+ let cx1 = this.root[2];
823
+ let cy1 = this.root[3];
763
824
  while (this.firstIds[node] === -1) {
764
825
  const midX = (cx0 + cx1) / 2;
765
826
  const midY = (cy0 + cy1) / 2;
766
827
  const right = x >= midX ? 1 : 0;
767
828
  const bottom = y >= midY ? 1 : 0;
768
- if (right)
829
+ if (right) {
769
830
  cx0 = midX;
770
- else
831
+ }
832
+ else {
771
833
  cx1 = midX;
772
- if (bottom)
834
+ }
835
+ if (bottom) {
773
836
  cy0 = midY;
774
- else
837
+ }
838
+ else {
775
839
  cy1 = midY;
840
+ }
776
841
  const quadrant = (bottom << 1) | right;
777
842
  const child = this.children[node * 4 + quadrant];
778
843
  if (child === -1) {
@@ -810,14 +875,18 @@ export class Quadtree {
810
875
  this.children[cell * 4 + newQuadrant] = leaf;
811
876
  return;
812
877
  }
813
- if (newRight)
878
+ if (newRight) {
814
879
  cx0 = midX;
815
- else
880
+ }
881
+ else {
816
882
  cx1 = midX;
817
- if (newBottom)
883
+ }
884
+ if (newBottom) {
818
885
  cy0 = midY;
819
- else
886
+ }
887
+ else {
820
888
  cy1 = midY;
889
+ }
821
890
  const deeper = this.allocNode(cell, -1);
822
891
  this.children[cell * 4 + newQuadrant] = deeper;
823
892
  cell = deeper;
@@ -831,14 +900,15 @@ export class Quadtree {
831
900
  }
832
901
  /**
833
902
  * Branch and bound, nearest cell first; `walk.keys` holds each pending cell's
834
- * distance.
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.
835
905
  */
836
- findWith(walk, x, y, maxDistance) {
837
- const { cell, delta, point } = walk;
906
+ findWith(walk) {
907
+ const { point } = walk;
838
908
  const childKeys = this.childKeys;
839
- point[0] = x;
840
- point[1] = y;
841
- let best = maxDistance;
909
+ const x = point[0];
910
+ const y = point[1];
911
+ let best = walk.keys[0];
842
912
  let bestId = -1;
843
913
  let top = 0;
844
914
  this.setRoot(walk);
@@ -851,6 +921,7 @@ export class Quadtree {
851
921
  const node = walk.nodes[frame];
852
922
  const first = this.firstIds[node];
853
923
  if (first !== -1) {
924
+ const delta = walk.delta;
854
925
  delta[0] = this.pointXs[first] - x;
855
926
  delta[1] = this.pointYs[first] - y;
856
927
  const distance = vec2LengthScaled(delta);
@@ -868,7 +939,7 @@ export class Quadtree {
868
939
  continue;
869
940
  }
870
941
  walk.setCell(quadrant);
871
- childKeys[quadrant] = box2DistanceToPoint(cell, point);
942
+ childKeys[quadrant] = box2DistanceToPoint(walk.cell, point);
872
943
  }
873
944
  // Farthest first, so the nearest cell is popped next.
874
945
  for (let round = 0; round < 4; round++) {
@@ -897,10 +968,11 @@ export class Quadtree {
897
968
  }
898
969
  setRoot(walk) {
899
970
  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;
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];
904
976
  }
905
977
  boxWith(walk, visitor) {
906
978
  const { cell, query } = walk;
@@ -919,7 +991,11 @@ export class Quadtree {
919
991
  }
920
992
  const first = this.firstIds[node];
921
993
  if (first !== -1) {
922
- if (box2ContainsXy(query, this.pointXs[first], this.pointYs[first])) {
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)) {
923
999
  for (let id = first; id !== -1; id = this.nextIds[id]) {
924
1000
  visitor(id);
925
1001
  }
@@ -959,10 +1035,11 @@ export class Quadtree {
959
1035
  walk.quadrants[0] = 0;
960
1036
  const rootIsLeaf = this.firstIds[0] !== -1;
961
1037
  if (!after || rootIsLeaf) {
962
- visited[0] = this.x0;
963
- visited[1] = this.y0;
964
- visited[2] = this.x1;
965
- visited[3] = this.y1;
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];
966
1043
  const skipped = visitor(0, visited) === true && !after;
967
1044
  if (skipped || rootIsLeaf) {
968
1045
  return;