@ac-kit/math-geometry 0.3.0 → 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.
- package/CHANGELOG.md +104 -0
- package/README.md +43 -3
- package/dist/_polygon3-cross.d.ts +23 -0
- package/dist/_polygon3-cross.d.ts.map +1 -0
- package/dist/_polygon3-cross.js +418 -0
- package/dist/_polygon3-cross.js.map +1 -0
- package/dist/_triangle3-cross.d.ts +66 -0
- package/dist/_triangle3-cross.d.ts.map +1 -0
- package/dist/_triangle3-cross.js +396 -0
- package/dist/_triangle3-cross.js.map +1 -0
- package/dist/line2.d.ts +4 -0
- package/dist/line2.d.ts.map +1 -1
- package/dist/line2.js +26 -5
- package/dist/line2.js.map +1 -1
- package/dist/mesh/_index-kernels-u16.d.ts +51 -9
- package/dist/mesh/_index-kernels-u16.d.ts.map +1 -1
- package/dist/mesh/_index-kernels-u16.js +905 -124
- package/dist/mesh/_index-kernels-u16.js.map +1 -1
- package/dist/mesh/_index-kernels-u32.d.ts +51 -9
- package/dist/mesh/_index-kernels-u32.d.ts.map +1 -1
- package/dist/mesh/_index-kernels-u32.js +905 -124
- package/dist/mesh/_index-kernels-u32.js.map +1 -1
- package/dist/mesh/mesh3-kernels.d.ts +56 -16
- package/dist/mesh/mesh3-kernels.d.ts.map +1 -1
- package/dist/mesh/mesh3-kernels.js +56 -16
- package/dist/mesh/mesh3-kernels.js.map +1 -1
- package/dist/plane3.d.ts +4 -3
- package/dist/plane3.d.ts.map +1 -1
- package/dist/plane3.js +18 -35
- package/dist/plane3.js.map +1 -1
- package/dist/polygon2.d.ts +54 -4
- package/dist/polygon2.d.ts.map +1 -1
- package/dist/polygon2.js +297 -7
- package/dist/polygon2.js.map +1 -1
- package/dist/polygon3.d.ts +19 -3
- package/dist/polygon3.d.ts.map +1 -1
- package/dist/polygon3.js +40 -16
- package/dist/polygon3.js.map +1 -1
- package/dist/quadtree.d.ts +40 -23
- package/dist/quadtree.d.ts.map +1 -1
- package/dist/quadtree.js +196 -119
- package/dist/quadtree.js.map +1 -1
- package/dist/segment2.d.ts +6 -1
- package/dist/segment2.d.ts.map +1 -1
- package/dist/segment2.js +20 -4
- package/dist/segment2.js.map +1 -1
- package/dist/triangle3.d.ts +20 -1
- package/dist/triangle3.d.ts.map +1 -1
- package/dist/triangle3.js +38 -18
- package/dist/triangle3.js.map +1 -1
- package/package.json +7 -7
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
|
|
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`.
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
*
|
|
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
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
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
|
-
|
|
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
|
-
|
|
251
|
+
}
|
|
252
|
+
if (x > maxX) {
|
|
241
253
|
maxX = x;
|
|
242
|
-
|
|
254
|
+
}
|
|
255
|
+
if (y < minY) {
|
|
243
256
|
minY = y;
|
|
244
|
-
|
|
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.
|
|
253
|
-
this.
|
|
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.
|
|
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.
|
|
288
|
-
let cy0 = this.
|
|
289
|
-
let cx1 = this.
|
|
290
|
-
let cy1 = this.
|
|
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
|
-
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
299
321
|
cx1 = midX;
|
|
300
|
-
|
|
322
|
+
}
|
|
323
|
+
if (bottom) {
|
|
301
324
|
cy0 = midY;
|
|
302
|
-
|
|
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.
|
|
341
|
-
this.
|
|
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.
|
|
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.
|
|
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.
|
|
408
|
+
return this.root[0];
|
|
386
409
|
}
|
|
387
410
|
get minY() {
|
|
388
|
-
return this.
|
|
411
|
+
return this.root[1];
|
|
389
412
|
}
|
|
390
413
|
get maxX() {
|
|
391
|
-
return this.
|
|
414
|
+
return this.root[2];
|
|
392
415
|
}
|
|
393
416
|
get maxY() {
|
|
394
|
-
return this.
|
|
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
|
-
|
|
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.
|
|
498
|
-
this.
|
|
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
|
-
|
|
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 <
|
|
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
|
-
|
|
639
|
+
this.deadCount = 0;
|
|
607
640
|
}
|
|
608
641
|
/**
|
|
609
|
-
* Grows the root cell until it holds
|
|
610
|
-
* as the doubled cell has the old root as an exact quadrant;
|
|
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(
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
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
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
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
|
-
|
|
662
|
+
target[2] = target[0];
|
|
663
|
+
target[3] = target[1];
|
|
664
|
+
this.reroot();
|
|
625
665
|
}
|
|
626
666
|
return;
|
|
627
667
|
}
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
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
|
|
637
|
-
* Refused when rounding would make the old root anything but
|
|
638
|
-
* quadrant as every traversal computes it: the existing cells
|
|
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(
|
|
641
|
-
const
|
|
642
|
-
const
|
|
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 = (
|
|
645
|
-
const x0 = left ?
|
|
646
|
-
const x1 = left ?
|
|
647
|
-
const y0 = top ?
|
|
648
|
-
const y1 = top ?
|
|
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 ?
|
|
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
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
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
|
|
666
|
-
* multiples of a quarter of its power-of-two
|
|
667
|
-
* representable; later doublings then stay exact
|
|
668
|
-
* about 2⁵⁰-fold or past the box's magnitude.
|
|
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(
|
|
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
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
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.
|
|
760
|
-
let cy0 = this.
|
|
761
|
-
let cx1 = this.
|
|
762
|
-
let cy1 = this.
|
|
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
|
-
|
|
831
|
+
}
|
|
832
|
+
else {
|
|
771
833
|
cx1 = midX;
|
|
772
|
-
|
|
834
|
+
}
|
|
835
|
+
if (bottom) {
|
|
773
836
|
cy0 = midY;
|
|
774
|
-
|
|
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
|
-
|
|
880
|
+
}
|
|
881
|
+
else {
|
|
816
882
|
cx1 = midX;
|
|
817
|
-
|
|
883
|
+
}
|
|
884
|
+
if (newBottom) {
|
|
818
885
|
cy0 = midY;
|
|
819
|
-
|
|
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
|
|
837
|
-
const {
|
|
906
|
+
findWith(walk) {
|
|
907
|
+
const { point } = walk;
|
|
838
908
|
const childKeys = this.childKeys;
|
|
839
|
-
point[0]
|
|
840
|
-
point[1]
|
|
841
|
-
let best =
|
|
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
|
-
|
|
901
|
-
walk.bounds[
|
|
902
|
-
walk.bounds[
|
|
903
|
-
walk.bounds[
|
|
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
|
-
|
|
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
|
-
|
|
963
|
-
visited[
|
|
964
|
-
visited[
|
|
965
|
-
visited[
|
|
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;
|