Skip to content

Commit

Permalink
use heap to optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
Eronana committed Jul 2, 2018
1 parent c3106a8 commit 4e70d21
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
37 changes: 22 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module.exports = rbush3d;
module.exports.default = rbush3d;

var quickselect = require('quickselect');
var Heap = require('heap');

function rbush3d(maxEntries, format) {
if (!(this instanceof rbush3d)) return new rbush3d(maxEntries, format);
Expand Down Expand Up @@ -86,31 +87,37 @@ rbush3d.prototype = {
var node = this.data,
toBBox = this.toBBox,
result,
dist = Infinity;
dist = length;

if (idx === Infinity && idy === Infinity && idz === Infinity) return undefined;
if (boxRayIntersects(node, ox, oy, oz, idx, idy, idz) === Infinity) return undefined;

var nodesToSearch = [],
i, len, child, childBBox;

while (node) {
for (i = 0, len = node.children.length; i < len; i++) {
child = node.children[i];
childBBox = node.leaf ? toBBox(child) : child;
var nodesToSearch = new Heap(function (a, b) {
return a.dist - b.dist;
});
nodesToSearch.push({
dist: dist,
node: node,
});
while (!nodesToSearch.empty() && nodesToSearch.top().dist <= dist) {
node = nodesToSearch.pop().node;
for (var i = 0, len = node.children.length; i < len; i++) {
var child = node.children[i];
var childBBox = node.leaf ? toBBox(child) : child;
var d = boxRayIntersects(childBBox, ox, oy, oz, idx, idy, idz);
if (d < length) {
if (d < dist) {
if (!node.leaf) {
nodesToSearch.push(child);
} else if (d < dist) {
nodesToSearch.push({
dist: d,
node: child,
});
} else {
dist = d;
result = child;
}
}
}
node = nodesToSearch.pop();
}

return result;
},

Expand Down Expand Up @@ -627,9 +634,9 @@ function boxRayIntersects(box, ox, oy, oz, idx, idy, idz) {
var x0 = Math.min(tx0, tx1);
var x1 = Math.max(tx0, tx1);

var tmin = Math.max(0, x0, y0, z0);
var tmin = Math.max(x0, y0, z0);
var tmax = Math.min(x1, y1, z1);
return tmax >= tmin ? tmin : Infinity;
return tmax < 0 || tmin > tmax ? Infinity : tmin;
}

function createNode(children) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
}
},
"dependencies": {
"heap": "^0.2.6",
"quickselect": "^1.0.0"
}
}
5 changes: 3 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ function boxRayIntersects(box, ox, oy, oz, idx, idy, idz) {
var x0 = Math.min(tx0, tx1);
var x1 = Math.max(tx0, tx1);

var tmin = Math.max(0, x0, y0, z0);
var tmin = Math.max(x0, y0, z0);
var tmax = Math.min(x1, y1, z1);
return tmax >= tmin ? tmin : Infinity;
return tmax < 0 || tmin > tmax ? Infinity : tmin;
}

function bfRaycast(data, ox, oy, oz, dx, dy, dz) {
Expand Down Expand Up @@ -573,6 +573,7 @@ t('#raycast with one bbox', function (t) {
t.equal(tree.raycast(0, 0, 0, 0, -1, -1), undefined);
t.equal(tree.raycast(0, 0, 0, -1, 0, -1), undefined);
t.equal(tree.raycast(0, 0, 0, -1, -1, -1), undefined);
t.equal(tree.raycast(50, 50, 50, 0, 0, 0), undefined);
t.notEqual(tree.raycast(0, 0, 0, 1, 1, 1), undefined);
t.notEqual(tree.raycast(20, 20, 20, 1, 1, 1), undefined);
t.notEqual(tree.raycast(50, 50, 0, 0, 0, 1), undefined);
Expand Down

0 comments on commit 4e70d21

Please sign in to comment.