d3octree
Ported version of D3's Quadtree, to use with three dimensional data structures, by adding the z coordinate.
An octree recursively partitions threedimensional space into cubes, dividing each cube into eight equallysized cubes. Each distinct point exists in a unique leaf node; coincident points are represented by a linked list. Octrees can accelerate various spatial operations, such as the Barnes–Hut approximation for computing manybody forces, collision detection, and searching for nearby points.
See also d3binarytree and d3quadtree.
Installing
If you use NPM, npm install d3octree
. You can also load directly from the global npmJS registry, as a bundled standalone library. AMD, CommonJS, and vanilla environments are supported. In vanilla, a d3
global is exported:
<script src="https://unpkg.com/d3octree"></script>
<script>
var octree = d3.octree();
</script>
Try d3octree in your browser.
API Reference
# d3.octree([data[, x, y, z]]) <>
Creates a new, empty octree with an empty extent and the default x, y and zaccessors. If data is specified, adds the specified array of data to the octree. This is equivalent to:
var tree = d3.octree()
.addAll(data);
If x, y and z are also specified, sets the x, y and z accessors to the specified functions before adding the specified array of data to the octree, equivalent to:
var tree = d3.octree()
.x(x)
.y(y)
.z(z)
.addAll(data);
If x is specified, sets the current xcoordinate accessor and returns the octree. If x is not specified, returns the current xaccessor, which defaults to:
function x(d) {
return d[0];
}
The xacccessor is used to derive the xcoordinate of data when adding to and removing from the tree. It is also used when finding to reaccess the coordinates of data previously added to the tree; therefore, the x, y and zaccessors must be consistent, returning the same value given the same input.
If y is specified, sets the current ycoordinate accessor and returns the octree. If y is not specified, returns the current yaccessor, which defaults to:
function y(d) {
return d[1];
}
The yacccessor is used to derive the ycoordinate of data when adding to and removing from the tree. It is also used when finding to reaccess the coordinates of data previously added to the tree; therefore, the x, y and zaccessors must be consistent, returning the same value given the same input.
If z is specified, sets the current zcoordinate accessor and returns the octree. If z is not specified, returns the current zaccessor, which defaults to:
function z(d) {
return d[2];
}
The zacccessor is used to derive the zcoordinate of data when adding to and removing from the tree. It is also used when finding to reaccess the coordinates of data previously added to the tree; therefore, the x, y and zaccessors must be consistent, returning the same value given the same input.
If extent is specified, expands the octree to cover the specified points [[x0, y0, z0], [x1, y1, z1]] and returns the octree. If extent is not specified, returns the octree’s current extent [[x0, y0, z0], [x1, y1, z1]], where x0, y0 and z0 are the inclusive lower bounds and x1, y1 and z1 are the inclusive upper bounds, or undefined if the octree has no extent. The extent may also be expanded by calling octree.cover or octree.add.
Expands the octree to cover the specified point ⟨x,y,z⟩, and returns the octree. If the octree’s extent already covers the specified point, this method does nothing. If the octree has an extent, the extent is repeatedly doubled to cover the specified point, wrapping the root node as necessary; if the octree is empty, the extent is initialized to the extent [[⌊x⌋, ⌊y⌋, ⌊z⌋], [⌈x⌉, ⌈y⌉, ⌈z⌉]]. (Rounding is necessary such that if the extent is later doubled, the boundaries of existing octants do not change due to floating point error.)
Adds the specified datum to the octree, deriving its coordinates ⟨x,y,z⟩ using the current x, y and zaccessors, and returns the octree. If the new point is outside the current extent of the octree, the octree is automatically expanded to cover the new point.
Adds the specified array of data to the octree, deriving each element’s coordinates ⟨x,y,z⟩ using the current x, y and zaccessors, and return this octree. This is approximately equivalent to calling octree.add repeatedly:
for (var i = 0, n = data.length; i < n; ++i) {
octree.add(data[i]);
}
However, this method results in a more compact octree because the extent of the data is computed first before adding the data.
Removes the specified datum to the octree, deriving its coordinates ⟨x,y,z⟩ using the current x, y and zaccessors, and returns the octree. If the specified datum does not exist in this octree, this method does nothing.
Removes the specified data from the octree, deriving their coordinates ⟨x,y,z⟩ using the current x, y and zaccessors, and returns the octree. If a specified datum does not exist in this octree, it is ignored.
# octree.copy()
Returns a copy of the octree. All nodes in the returned octree are identical copies of the corresponding node in the octree; however, any data in the octree is shared by reference and not copied.
Returns the root node of the octree.
Returns an array of all data in the octree.
Returns the total number of data in the octree.
# octree.find(x, y, z[, radius]) <>
Returns the datum closest to the position ⟨x,y,z⟩ with the given search radius. If radius is not specified, it defaults to infinity. If there is no datum within the search area, returns undefined.
Visits each node in the octree in preorder traversal, invoking the specified callback with arguments node, x0, y0, z0, x1, y1, z1 for each node, where node is the node being visited, ⟨x0, y0, z0⟩ are the lower bounds of the node, and ⟨x1, y1, z1⟩ are the upper bounds, and returns the octree. (Assuming that positive x is right, positive y is down and positive z is far, as is typically the case, ⟨x0, y0, z0⟩ is the topleftfront corner and ⟨x1, y1, z1⟩ is the lowerrightback corner; however, the coordinate system is arbitrary, so more formally x0 <= x1, y0 <= y1 and z0 <= z1.)
If the callback returns true for a given node, then the children of that node are not visited; otherwise, all child nodes are visited. This can be used to quickly visit only parts of the tree, for example when using the Barnes–Hut approximation. Note, however, that child octants are always visited in sibling order: topleftfront, toprightfront, bottomleftfront, bottomrightfront, topleftback, toprightback, bottomleftback, bottomrightback. In cases such as search, visiting siblings in a specific order may be faster.
As an example, the following visits the octree and returns all the nodes within a cubic extent [xmin, ymin, zmin, xmax, ymax, zmax], ignoring octants that cannot possibly contain any such node:
function search(octree, xmin, ymin, zmin, xmax, ymax, zmax) {
const results = [];
octree.visit(function(node, x1, y1, z1, x2, y2, z2) {
if (!node.length) {
do {
var d = node.data;
if (d[0] >= xmin && d[0] < xmax && d[1] >= ymin && d[1] < ymax && d[2] >= zmin && d[2] < zmax) {
results.push(d);
}
} while (node = node.next);
}
return x1 >= xmax  y1 >= ymax  z1 >= zmax  x2 < xmin  y2 < ymin  z2 < zmin;
});
return results;
}
# octree.visitAfter(callback) <>
Visits each node in the octree in postorder traversal, invoking the specified callback with arguments node, x0, y0, z0, x1, y1, z1 for each node, where node is the node being visited, ⟨x0, y0, z0⟩ are the lower bounds of the node, and ⟨x1, y1, z1⟩ are the upper bounds, and returns the octree. (Assuming that positive x is right, positive y is down and positive z is far, as is typically the case, ⟨x0, y0, z0⟩ is the topleftfront corner and ⟨x1, y1, z1⟩ is the lowerrightback corner; however, the coordinate system is arbitrary, so more formally x0 <= x1, y0 <= y1 and z0 <= z1.) Returns root.
Nodes
Internal nodes of the octree are represented as eightelement arrays in lefttoright, toptobottom, fronttoback order:

0
 the topleftfront octant, if any. 
1
 the toprightfront octant, if any. 
2
 the bottomleftfront octant, if any. 
3
 the bottomrightfront octant, if any. 
4
 the topleftback octant, if any. 
5
 the toprightback octant, if any. 
6
 the bottomleftback octant, if any. 
7
 the bottomrightback octant, if any.
A child octant may be undefined if it is empty.
Leaf nodes are represented as objects with the following properties:

data
 the data associated with this point, as passed to octree.add. 
next
 the next datum in this leaf, if any.
The length
property may be used to distinguish leaf nodes from internal nodes: it is undefined for leaf nodes, and 8 for internal nodes. For example, to iterate over all data in a leaf node:
if (!node.length) do console.log(node.data); while (node = node.next);
The point’s x, y and zcoordinates must not be modified while the point is in the octree. To update a point’s position, remove the point and then readd it to the octree at the new position. Alternatively, you may discard the existing octree entirely and create a new one from scratch; this may be more efficient if many of the points have moved.