Skip to content

Commit

Permalink
feat(geom-accel): fix #463, add grid resolution check
Browse files Browse the repository at this point in the history
- update ASpatialGrid ctor to check for min res
- add/rename internal helpers
  • Loading branch information
postspectacular committed Apr 20, 2024
1 parent 7a7b5ab commit 8688ad5
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
5 changes: 3 additions & 2 deletions packages/geom-accel/src/aspatial-grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { div } from "@thi.ng/vectors/div";
import { equals } from "@thi.ng/vectors/equals";
import { floor } from "@thi.ng/vectors/floor";
import { subN } from "@thi.ng/vectors/subn";
import { into } from "./utils.js";
import { __ensureRes, __into } from "./utils.js";

/**
* Common base class for {@link SpatialGrid2} and {@link SpatialGrid3}.
Expand All @@ -28,6 +28,7 @@ export abstract class ASpatialGrid<K extends ReadonlyVec, V>
protected _size: ReadonlyVec,
protected _res: ReadonlyVec
) {
__ensureRes(_res);
floor(null, this._res);
this._res1 = subN([], this._res, 1);
this._invSize = div([], this._res, _size);
Expand Down Expand Up @@ -94,7 +95,7 @@ export abstract class ASpatialGrid<K extends ReadonlyVec, V>
}

into(pairs: Iterable<Pair<K, V>>, eps = EPS) {
return into(this, pairs, eps);
return __into(this, pairs, eps);
}

remove(k: K) {
Expand Down
6 changes: 3 additions & 3 deletions packages/geom-accel/src/kd-tree-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { EPS } from "@thi.ng/math/api";
import { map } from "@thi.ng/transducers/map";
import type { DistanceFn, ReadonlyVec, Vec } from "@thi.ng/vectors";
import { distSq } from "@thi.ng/vectors/distsq";
import { CMP, addResults, into } from "./utils.js";
import { CMP, __addResults, __into } from "./utils.js";

type MaybeKdNode<K extends ReadonlyVec, V> = Maybe<KdNode<K, V>>;

Expand Down Expand Up @@ -151,7 +151,7 @@ export class KdTreeMap<K extends ReadonlyVec, V>
}

into(pairs: Iterable<Pair<K, V>>, eps = EPS) {
return into(this, pairs, eps);
return __into(this, pairs, eps);
}

remove(key: K) {
Expand Down Expand Up @@ -233,7 +233,7 @@ export class KdTreeMap<K extends ReadonlyVec, V>
}
);
nearest(q, nodes, this.dim, maxNum, this.root!, this.distanceFn);
return addResults(f, nodes.values, acc);
return __addResults(f, nodes.values, acc);
}
return acc;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/geom-accel/src/nd-quadtree-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { madd } from "@thi.ng/vectors/madd";
import { mulN } from "@thi.ng/vectors/muln";
import { submN } from "@thi.ng/vectors/submn";
import { vop } from "@thi.ng/vectors/vop";
import { CMP, addResults, into } from "./utils.js";
import { CMP, __addResults, __into } from "./utils.js";

type MaybeNdQtNode<K extends ReadonlyVec, V> = Maybe<NdQtNode<K, V>>;

Expand Down Expand Up @@ -87,7 +87,7 @@ export class NdQtNode<K extends ReadonlyVec, V> {
acc: T[],
distFn: DistanceFn
) {
return addResults(
return __addResults(
fn,
this.doQuery(
p,
Expand Down Expand Up @@ -295,7 +295,7 @@ export class NdQuadtreeMap<K extends ReadonlyVec, V>
}

into(pairs: Iterable<Pair<K, V>>, eps = EPS) {
return into(this, pairs, eps);
return __into(this, pairs, eps);
}

remove(p: K) {
Expand Down
4 changes: 2 additions & 2 deletions packages/geom-accel/src/spatial-grid2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { addN2 } from "@thi.ng/vectors/addn";
import { distSq2 } from "@thi.ng/vectors/distsq";
import { subN2 } from "@thi.ng/vectors/subn";
import { ASpatialGrid } from "./aspatial-grid.js";
import { addResults, CMP } from "./utils.js";
import { __addResults, CMP } from "./utils.js";

const TMP: Vec = [];

Expand Down Expand Up @@ -57,7 +57,7 @@ export class SpatialGrid2<K extends ReadonlyVec, V> extends ASpatialGrid<K, V> {
c && c.length && this.queryCell(distSq2, heap, c, k, limit);
}
}
return addResults(fn, sel, acc);
return __addResults(fn, sel, acc);
}

protected findIndex(k: ReadonlyVec) {
Expand Down
4 changes: 2 additions & 2 deletions packages/geom-accel/src/spatial-grid3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { addN3 } from "@thi.ng/vectors/addn";
import { distSq3 } from "@thi.ng/vectors/distsq";
import { subN3 } from "@thi.ng/vectors/subn";
import { ASpatialGrid } from "./aspatial-grid.js";
import { addResults, CMP } from "./utils.js";
import { __addResults, CMP } from "./utils.js";

const TMP: Vec = [];

Expand Down Expand Up @@ -64,7 +64,7 @@ export class SpatialGrid3<K extends ReadonlyVec, V> extends ASpatialGrid<K, V> {
}
}
}
return addResults(fn, sel, acc);
return __addResults(fn, sel, acc);
}

protected findIndex(k: ReadonlyVec) {
Expand Down
12 changes: 10 additions & 2 deletions packages/geom-accel/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// thing:no-export
import type { Fn, Fn3, Nullable, Pair } from "@thi.ng/api";
import { assert } from "@thi.ng/errors/assert";
import type { ReadonlyVec } from "@thi.ng/vectors";

/** @internal */
export const CMP = (a: [number, any?], b: [number, any?]) => b[0] - a[0];

/** @internal */
export const addResults = <A, B>(
export const __addResults = <A, B>(
fn: Fn<A, B>,
sel: [number, Nullable<A>?][],
acc: B[]
Expand All @@ -26,7 +28,7 @@ export const addResults = <A, B>(
*
* @internal
*/
export const into = <K, V>(
export const __into = <K, V>(
map: { set: Fn3<K, V, number, boolean> },
pairs: Iterable<Pair<K, V>>,
eps: number
Expand All @@ -37,3 +39,9 @@ export const into = <K, V>(
}
return ok;
};

/** @internal */
export const __ensureRes = (res: ReadonlyVec, min = 1) => {
for (let i = res.length; i-- > 0; )
assert(res[i] >= min, `invalid grid res: ${res}, require min=${min}`);
};

0 comments on commit 8688ad5

Please sign in to comment.