Skip to content

Commit

Permalink
refactor(geom-accel): update to use new vectors package
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 19, 2019
1 parent 46bb873 commit 485051d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/geom-accel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"scripts": {
"build": "yarn clean && yarn build:es6 && yarn build:bundle",
"build:es6": "tsc --declaration",
"build:bundle": "../../scripts/bundle-module geomAccel api heaps math transducers vectors",
"build:bundle": "../../scripts/bundle-module geomAccel api heaps math transducers vectors3",
"test": "rimraf build && tsc -p test/tsconfig.json && nyc mocha build/test/*.js",
"clean": "rimraf *.js *.d.ts .nyc_output build coverage doc lib internal",
"cover": "yarn test && nyc report --reporter=lcov",
Expand All @@ -36,7 +36,7 @@
"@thi.ng/heaps": "^0.3.1",
"@thi.ng/math": "^0.2.2",
"@thi.ng/transducers": "^2.3.2",
"@thi.ng/vectors": "^1.4.12"
"@thi.ng/vectors3": "^0.0.1"
},
"keywords": [
"ES6",
Expand Down
34 changes: 16 additions & 18 deletions packages/geom-accel/src/kdtree.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { ICopy, IEmpty, Pair } from "@thi.ng/api";
import { ICopy, Pair } from "@thi.ng/api";
import { Heap } from "@thi.ng/heaps";
import { EPS } from "@thi.ng/math";
import { ensureArray } from "@thi.ng/transducers";
import { IDistance } from "@thi.ng/vectors";

export type KdIndexable<T> = IDistance<T> & IEmpty<T>;
import { distSq, empty, ReadonlyVec } from "@thi.ng/vectors3";

const CMP = (a, b) => b[0] - a[0];

export class KdNode<K extends KdIndexable<K>, V> {
export class KdNode<K extends ReadonlyVec, V> {

parent: KdNode<K, V>;
l: KdNode<K, V>;
r: KdNode<K, V>;
d: number;
k: Readonly<K>;
k: K;
v: V;

constructor(parent: KdNode<K, V>, dim: number, key: Readonly<K>, val: V) {
constructor(parent: KdNode<K, V>, dim: number, key: K, val: V) {
this.parent = parent;
this.d = dim;
this.k = key;
Expand Down Expand Up @@ -62,7 +60,7 @@ export class KdNode<K extends KdIndexable<K>, V> {
* https://github.com/ubilabs/kd-tree-javascript
*
*/
export class KdTree<K extends KdIndexable<K>, V>
export class KdTree<K extends ReadonlyVec, V>
implements ICopy<KdTree<K, V>> {

root: KdNode<K, V>;
Expand Down Expand Up @@ -96,11 +94,11 @@ export class KdTree<K extends KdIndexable<K>, V>
return new KdTree(this.dim, this);
}

add(p: Readonly<K>, v: V, eps = EPS) {
add(p: K, v: V, eps = EPS) {
eps *= eps;
const search = (node: KdNode<K, V>, parent: KdNode<K, V>): KdNode<K, V> | false =>
node ?
p.distSq(node.k) > eps ?
distSq(p, node.k) > eps ?
search(
p[node.d] < node.k[node.d] ? node.l : node.r,
node
Expand Down Expand Up @@ -217,14 +215,14 @@ export class KdTree<K extends KdIndexable<K>, V>
* @param node
* @param epsSq squared epsilon / tolerance
*/
const find = <K extends KdIndexable<K>, V>(p: K, node: KdNode<K, V>, epsSq: number) => {
const find = <K extends ReadonlyVec, V>(p: K, node: KdNode<K, V>, epsSq: number) => {
if (!node) return;
return p.distSq(node.k) <= epsSq ?
return distSq(p, node.k) <= epsSq ?
node :
find(p, p[node.d] < node.k[node.d] ? node.l : node.r, epsSq);
};

const findMin = <K extends KdIndexable<K>, V>(node: KdNode<K, V>, dim: number): KdNode<K, V> => {
const findMin = <K extends ReadonlyVec, V>(node: KdNode<K, V>, dim: number): KdNode<K, V> => {
if (!node) return;
if (node.d === dim) {
return node.l ?
Expand All @@ -249,7 +247,7 @@ const findMin = <K extends KdIndexable<K>, V>(node: KdNode<K, V>, dim: number):
*
* @param node
*/
const remove = <K extends KdIndexable<K>, V>(node: KdNode<K, V>) => {
const remove = <K extends ReadonlyVec, V>(node: KdNode<K, V>) => {
if (!node.l && !node.r) {
if (!node.parent) {
return true;
Expand All @@ -276,9 +274,9 @@ const remove = <K extends KdIndexable<K>, V>(node: KdNode<K, V>) => {
}
};

const nearest = <K extends KdIndexable<K>, V>(q: K, acc: Heap<[number, KdNode<K, V>]>, dims: number, maxNum: number, node: KdNode<K, V>) => {
const nearest = <K extends ReadonlyVec, V>(q: K, acc: Heap<[number, KdNode<K, V>]>, dims: number, maxNum: number, node: KdNode<K, V>) => {
const p = node.k;
const ndist = q.distSq(p);
const ndist = distSq(p, q);
if (!node.l && !node.r) {
if (ndist < acc.peek()[0]) {
if (acc.length >= maxNum) {
Expand All @@ -290,11 +288,11 @@ const nearest = <K extends KdIndexable<K>, V>(q: K, acc: Heap<[number, KdNode<K,
return;
}
const ndim = node.d;
const tp: K = q.empty();
const tp = empty(q);
for (let i = dims; --i >= 0;) {
tp[i] = i === ndim ? q[i] : p[i];
}
const tdist = tp.distSq(p);
const tdist = distSq(p, tp);
let best =
!node.r ?
node.l :
Expand Down

0 comments on commit 485051d

Please sign in to comment.