forked from mourner/rbush-knn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbush-3d-knn.js
138 lines (110 loc) · 3.8 KB
/
rbush-3d-knn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global["rbush-3d-knn"] = factory());
})(this, (function () { 'use strict';
class TinyQueue {
constructor(data = [], compare = defaultCompare) {
this.data = data;
this.length = this.data.length;
this.compare = compare;
if (this.length > 0) {
for (let i = (this.length >> 1) - 1; i >= 0; i--) this._down(i);
}
}
push(item) {
this.data.push(item);
this.length++;
this._up(this.length - 1);
}
pop() {
if (this.length === 0) return undefined;
const top = this.data[0];
const bottom = this.data.pop();
this.length--;
if (this.length > 0) {
this.data[0] = bottom;
this._down(0);
}
return top;
}
peek() {
return this.data[0];
}
_up(pos) {
const {data, compare} = this;
const item = data[pos];
while (pos > 0) {
const parent = (pos - 1) >> 1;
const current = data[parent];
if (compare(item, current) >= 0) break;
data[pos] = current;
pos = parent;
}
data[pos] = item;
}
_down(pos) {
const {data, compare} = this;
const halfLength = this.length >> 1;
const item = data[pos];
while (pos < halfLength) {
let left = (pos << 1) + 1;
let best = data[left];
const right = left + 1;
if (right < this.length && compare(data[right], best) < 0) {
left = right;
best = data[right];
}
if (compare(best, item) >= 0) break;
data[pos] = best;
pos = left;
}
data[pos] = item;
}
}
function defaultCompare(a, b) {
return a < b ? -1 : a > b ? 1 : 0;
}
function knn(tree, x, y, z, n, predicate, maxDistance) {
var node = tree.data,
result = [],
i, child, dist, candidate;
var queue = new TinyQueue(undefined, compareDist);
while (node) {
for (i = 0; i < node.children.length; i++) {
child = node.children[i];
dist = boxDist(x, y, z, node.children[i]);
// console.log(dist);
if (!maxDistance || dist <= maxDistance) {
queue.push({
node: child,
isItem: node.leaf,
dist: dist
});
}
}
while (queue.length && queue.peek().isItem) {
candidate = queue.pop().node;
if (!predicate || predicate(candidate))
result.push(candidate);
if (n && result.length === n) return result;
}
node = queue.pop();
if (node) node = node.node;
}
return result;
}
function compareDist(a, b) {
return a.dist - b.dist;
}
function boxDist(x, y, z, box) {
var dx = axisDist(x, box.minX, box.maxX),
dy = axisDist(y, box.minY, box.maxY),
dz = axisDist(z, box.minZ, box.maxZ);
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
function axisDist(k, min, max) {
return k < min ? min - k : k <= max ? 0 : k - max;
}
return knn;
}));