@thi.ng/geom-accel
Version:
n-D spatial indexing data structures with a shared ES6 Map/Set-like API
93 lines (92 loc) • 2.16 kB
JavaScript
import { EPS } from "@thi.ng/math/api";
import { addmN } from "@thi.ng/vectors/addmn";
import { submN } from "@thi.ng/vectors/submn";
import { NdQuadtreeMap } from "./nd-quadtree-map.js";
class NdQuadtreeSet {
/**
* Returns a new point-based `NdQuadtreeSet` for nD keys in given
* region defined by `min` / `max` coordinates. The dimensionality
* of the tree is implicitly defined by the provided coordinates.
* Only points within that region can be indexed.
*
* @remarks
* Due to exponentially growing lookup tables, currently only
* supports up to 16 dimensions.
*/
static fromMinMax(min, max) {
return new NdQuadtreeSet(
addmN([], min, max, 0.5),
submN([], max, min, 0.5)
);
}
tree;
_size;
constructor(pos, ext, keys, distanceFn) {
this.tree = new NdQuadtreeMap(pos, ext, void 0, distanceFn);
this._size = 0;
keys && this.into(keys);
}
[Symbol.iterator]() {
return this.tree.keys();
}
keys() {
return this.tree.keys();
}
values() {
return this.tree.values();
}
get size() {
return this._size;
}
copy() {
return new NdQuadtreeSet(
this.tree.root.pos,
this.tree.root.ext,
this,
this.tree.distanceFn
);
}
clear() {
this.tree.clear();
}
empty() {
return new NdQuadtreeSet(
this.tree.root.pos,
this.tree.root.ext,
void 0,
this.tree.distanceFn
);
}
add(key, eps = EPS) {
return this.tree.set(key, key, eps);
}
into(keys, eps = EPS) {
let ok = true;
const tree = this.tree;
for (let k of keys) {
ok = tree.set(k, k, eps) && ok;
}
return ok;
}
remove(key) {
return this.tree.remove(key);
}
has(key, eps = EPS) {
return this.tree.has(key, eps);
}
get(key, eps) {
return this.tree.get(key, eps);
}
query(q, maxDist, limit, acc) {
return this.tree.query(q, maxDist, limit, acc);
}
queryKeys(q, maxDist, limit, acc) {
return this.tree.queryKeys(q, maxDist, limit, acc);
}
queryValues(q, maxDist, limit, acc) {
return this.tree.queryKeys(q, maxDist, limit, acc);
}
}
export {
NdQuadtreeSet
};