pax_global_header00006660000000000000000000000064130605641770014523gustar00rootroot0000000000000052 comment=084b437784e7be6a81c85242958252e3673b6787 d3-quadtree-1.0.3/000077500000000000000000000000001306056417700136425ustar00rootroot00000000000000d3-quadtree-1.0.3/.eslintrc000066400000000000000000000002041306056417700154620ustar00rootroot00000000000000parserOptions: sourceType: module extends: "eslint:recommended" rules: no-cond-assign: 0 no-constant-condition: 0 d3-quadtree-1.0.3/.gitignore000066400000000000000000000001001306056417700156210ustar00rootroot00000000000000*.sublime-workspace .DS_Store build/ node_modules npm-debug.log d3-quadtree-1.0.3/.npmignore000066400000000000000000000000361306056417700156400ustar00rootroot00000000000000*.sublime-* build/*.zip test/ d3-quadtree-1.0.3/LICENSE000066400000000000000000000027031306056417700146510ustar00rootroot00000000000000Copyright 2010-2016 Mike Bostock All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. d3-quadtree-1.0.3/README.md000066400000000000000000000266021306056417700151270ustar00rootroot00000000000000# d3-quadtree A [quadtree](https://en.wikipedia.org/wiki/Quadtree) recursively partitions two-dimensional space into squares, dividing each square into four equally-sized squares. Each distinct point exists in a unique leaf [node](#nodes); coincident points are represented by a linked list. Quadtrees can accelerate various spatial operations, such as the [Barnes–Hut approximation](https://en.wikipedia.org/wiki/Barnes–Hut_simulation) for computing many-body forces, collision detection, and searching for nearby points. ## Installing If you use NPM, `npm install d3-quadtree`. Otherwise, download the [latest release](https://github.com/d3/d3-quadtree/releases/latest). You can also load directly from [d3js.org](https://d3js.org), either as a [standalone library](https://d3js.org/d3-quadtree.v1.min.js) or as part of [D3 4.0](https://github.com/d3/d3). AMD, CommonJS, and vanilla environments are supported. In vanilla, a `d3` global is exported: ```html ``` [Try d3-quadtree in your browser.](https://tonicdev.com/npm/d3-quadtree) ## API Reference # d3.quadtree([data[, x, y]]) [<>](https://github.com/d3/d3-quadtree/blob/master/src/quadtree.js#L14 "Source") Creates a new, empty quadtree with an empty [extent](#quadtree_extent) and the default [*x*-](#quadtree_x) and [*y*-](#quadtree_y)accessors. If *data* is specified, [adds](#quadtree_addAll) the specified array of data to the quadtree. This is equivalent to: ```js var tree = d3.quadtree() .addAll(data); ``` If *x* and *y* are also specified, sets the [*x*-](#quadtree_x) and [*y*-](#quadtree_y) accessors to the specified functions before adding the specified array of data to the quadtree, equivalent to: ```js var tree = d3.quadtree() .x(x) .y(y) .addAll(data); ``` # quadtree.x([x]) [<>](https://github.com/d3/d3-quadtree/blob/master/src/x.js "Source") If *x* is specified, sets the current *x*-coordinate accessor and returns the quadtree. If *x* is not specified, returns the current *x*-accessor, which defaults to: ```js function x(d) { return d[0]; } ``` The *x*-acccessor is used to derive the *x*-coordinate of data when [adding](#quadtree_add) to and [removing](#quadtree_remove) from the tree. It is also used when [finding](#quadtree_find) to re-access the coordinates of data previously added to the tree; therefore, the *x*- and *y*-accessors must be consistent, returning the same value given the same input. # quadtree.y([y]) [<>](https://github.com/d3/d3-quadtree/blob/master/src/y.js "Source") If *y* is specified, sets the current *y*-coordinate accessor and returns the quadtree. If *y* is not specified, returns the current *y*-accessor, which defaults to: ```js function y(d) { return d[1]; } ``` The *y*-acccessor is used to derive the *y*-coordinate of data when [adding](#quadtree_add) to and [removing](#quadtree_remove) from the tree. It is also used when [finding](#quadtree_find) to re-access the coordinates of data previously added to the tree; therefore, the *x*- and *y*-accessors must be consistent, returning the same value given the same input. # quadtree.extent([*extent*]) [<>](https://github.com/d3/d3-quadtree/blob/master/src/extent.js "Source") If *extent* is specified, expands the quadtree to [cover](#quadtree_cover) the specified points [[*x0*, *y0*], [*x1*, *y1*]] and returns the quadtree. If *extent* is not specified, returns the quadtree’s current extent [[*x0*, *y0*], [*x1*, *y1*]], where *x0* and *y0* are the inclusive lower bounds and *x1* and *y1* are the inclusive upper bounds, or undefined if the quadtree has no extent. The extent may also be expanded by calling [*quadtree*.cover](#quadtree_cover) or [*quadtree*.add](#quadtree_add). # quadtree.cover(x, y) [<>](https://github.com/d3/d3-quadtree/blob/master/src/cover.js "Source") Expands the quadtree to cover the specified point ⟨*x*,*y*⟩, and returns the quadtree. If the quadtree’s extent already covers the specified point, this method does nothing. If the quadtree has an extent, the extent is repeatedly doubled to cover the specified point, wrapping the [root](#quadtree_root) [node](#nodes) as necessary; if the quadtree is empty, the extent is initialized to the extent [[⌊*x*⌋, ⌊*y*⌋], [⌈*x*⌉, ⌈*y*⌉]]. (Rounding is necessary such that if the extent is later doubled, the boundaries of existing quadrants do not change due to floating point error.) # quadtree.add(datum) [<>](https://github.com/d3/d3-quadtree/blob/master/src/add.js "Source") Adds the specified *datum* to the quadtree, deriving its coordinates ⟨*x*,*y*⟩ using the current [*x*-](#quadtree_x) and [*y*-](#quadtree_y)accessors, and returns the quadtree. If the new point is outside the current [extent](#quadtree_extent) of the quadtree, the quadtree is automatically expanded to [cover](#quadtree_cover) the new point. # quadtree.addAll(data) [<>](https://github.com/d3/d3-quadtree/blob/master/src/add.js#L50 "Source") Adds the specified array of *data* to the quadtree, deriving each element’s coordinates ⟨*x*,*y*⟩ using the current [*x*-](#quadtree_x) and [*y*-](#quadtree_y)accessors, and return this quadtree. This is approximately equivalent to calling [*quadtree*.add](#quadtree_add) repeatedly: ```js for (var i = 0, n = data.length; i < n; ++i) { quadtree.add(data[i]); } ``` However, this method results in a more compact quadtree because the extent of the *data* is computed first before adding the data. # quadtree.remove(datum) [<>](https://github.com/d3/d3-quadtree/blob/master/src/remove.js "Source") Removes the specified *datum* to the quadtree, deriving its coordinates ⟨*x*,*y*⟩ using the current [*x*-](#quadtree_x) and [*y*-](#quadtree_y)accessors, and returns the quadtree. If the specified *datum* does not exist in this quadtree, this method does nothing. # quadtree.removeAll(data) [<>](https://github.com/d3/d3-quadtree/blob/master/src/remove.js#L59 "Source") … # quadtree.copy() Returns a copy of the quadtree. All [nodes](#nodes) in the returned quadtree are identical copies of the corresponding node in the quadtree; however, any data in the quadtree is shared by reference and not copied. # quadtree.root() [<>](https://github.com/d3/d3-quadtree/blob/master/src/root.js "Source") Returns the root [node](#nodes) of the quadtree. # quadtree.data() [<>](https://github.com/d3/d3-quadtree/blob/master/src/data.js "Source") Returns an array of all data in the quadtree. # quadtree.size() [<>](https://github.com/d3/d3-quadtree/blob/master/src/size.js "Source") Returns the total number of data in the quadtree. # quadtree.find(x, y[, radius]) [<>](https://github.com/d3/d3-quadtree/blob/master/src/find.js "Source") Returns the datum closest to the position ⟨*x*,*y*⟩ 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. # quadtree.visit(callback) [<>](https://github.com/d3/d3-quadtree/blob/master/src/visit.js "Source") Visits each [node](#nodes) in the quadtree in pre-order traversal, invoking the specified *callback* with arguments *node*, *x0*, *y0*, *x1*, *y1* for each node, where *node* is the node being visited, ⟨*x0*, *y0*⟩ are the lower bounds of the node, and ⟨*x1*, *y1*⟩ are the upper bounds, and returns the quadtree. (Assuming that positive *x* is right and positive *y* is down, as is typically the case in Canvas and SVG, ⟨*x0*, *y0*⟩ is the top-left corner and ⟨*x1*, *y1*⟩ is the lower-right corner; however, the coordinate system is arbitrary, so more formally *x0* <= *x1* and *y0* <= *y1*.) 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](https://en.wikipedia.org/wiki/Barnes–Hut_simulation). Note, however, that child quadrants are always visited in sibling order: top-left, top-right, bottom-left, bottom-right. In cases such as [search](#quadtree_find), visiting siblings in a specific order may be faster. # quadtree.visitAfter(callback) [<>](https://github.com/d3/d3-quadtree/blob/master/src/visitAfter.js "Source") Visits each [node](#nodes) in the quadtree in post-order traversal, invoking the specified *callback* with arguments *node*, *x0*, *y0*, *x1*, *y1* for each node, where *node* is the node being visited, ⟨*x0*, *y0*⟩ are the lower bounds of the node, and ⟨*x1*, *y1*⟩ are the upper bounds, and returns the quadtree. (Assuming that positive *x* is right and positive *y* is down, as is typically the case in Canvas and SVG, ⟨*x0*, *y0*⟩ is the top-left corner and ⟨*x1*, *y1*⟩ is the lower-right corner; however, the coordinate system is arbitrary, so more formally *x0* <= *x1* and *y0* <= *y1*.) Returns *root*. ### Nodes Internal nodes of the quadtree are represented as four-element arrays in left-to-right, top-to-bottom order: * `0` - the top-left quadrant, if any. * `1` - the top-right quadrant, if any. * `2` - the bottom-left quadrant, if any. * `3` - the bottom-right quadrant, if any. A child quadrant 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 [*quadtree*.add](#quadtree_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 4 for internal nodes. For example, to iterate over all data in a leaf node: ```js if (!node.length) do console.log(node.data); while (node = node.next); ``` The point’s *x*- and *y*-coordinates **must not be modified** while the point is in the quadtree. To update a point’s position, [remove](#quadtree_remove) the point and then re-[add](#quadtree_add) it to the quadtree at the new position. Alternatively, you may discard the existing quadtree entirely and create a new one from scratch; this may be more efficient if many of the points have moved. d3-quadtree-1.0.3/d3-quadtree.sublime-project000066400000000000000000000002711306056417700210060ustar00rootroot00000000000000{ "folders": [ { "path": ".", "file_exclude_patterns": [ "*.sublime-workspace" ], "folder_exclude_patterns": [ "build" ] } ] } d3-quadtree-1.0.3/index.js000066400000000000000000000000641306056417700153070ustar00rootroot00000000000000export {default as quadtree} from "./src/quadtree"; d3-quadtree-1.0.3/package.json000066400000000000000000000026641306056417700161400ustar00rootroot00000000000000{ "name": "d3-quadtree", "version": "1.0.3", "description": "Two-dimensional recursive spatial subdivision.", "keywords": [ "d3", "d3-module", "quadtree" ], "homepage": "https://d3js.org/d3-quadtree/", "license": "BSD-3-Clause", "author": { "name": "Mike Bostock", "url": "http://bost.ocks.org/mike" }, "main": "build/d3-quadtree.js", "module": "index", "jsnext:main": "index", "repository": { "type": "git", "url": "https://github.com/d3/d3-quadtree.git" }, "scripts": { "pretest": "rm -rf build && mkdir build && rollup --banner \"$(preamble)\" -f umd -n d3 -o build/d3-quadtree.js -- index.js", "test": "tape 'test/**/*-test.js' && eslint index.js src", "prepublish": "npm run test && uglifyjs --preamble \"$(preamble)\" build/d3-quadtree.js -c -m -o build/d3-quadtree.min.js", "postpublish": "git push && git push --tags && cd ../d3.github.com && git pull && cp ../d3-quadtree/build/d3-quadtree.js d3-quadtree.v1.js && cp ../d3-quadtree/build/d3-quadtree.min.js d3-quadtree.v1.min.js && git add d3-quadtree.v1.js d3-quadtree.v1.min.js && git commit -m \"d3-quadtree ${npm_package_version}\" && git push && cd - && zip -j build/d3-quadtree.zip -- LICENSE README.md build/d3-quadtree.js build/d3-quadtree.min.js" }, "devDependencies": { "d3-array": "1", "eslint": "3", "package-preamble": "0.0", "rollup": "0.41", "tape": "4", "uglify-js": "^2.8.11" } } d3-quadtree-1.0.3/src/000077500000000000000000000000001306056417700144315ustar00rootroot00000000000000d3-quadtree-1.0.3/src/add.js000066400000000000000000000046111306056417700155210ustar00rootroot00000000000000export default function(d) { var x = +this._x.call(null, d), y = +this._y.call(null, d); return add(this.cover(x, y), x, y, d); } function add(tree, x, y, d) { if (isNaN(x) || isNaN(y)) return tree; // ignore invalid points var parent, node = tree._root, leaf = {data: d}, x0 = tree._x0, y0 = tree._y0, x1 = tree._x1, y1 = tree._y1, xm, ym, xp, yp, right, bottom, i, j; // If the tree is empty, initialize the root as a leaf. if (!node) return tree._root = leaf, tree; // Find the existing leaf for the new point, or add it. while (node.length) { if (right = x >= (xm = (x0 + x1) / 2)) x0 = xm; else x1 = xm; if (bottom = y >= (ym = (y0 + y1) / 2)) y0 = ym; else y1 = ym; if (parent = node, !(node = node[i = bottom << 1 | right])) return parent[i] = leaf, tree; } // Is the new point is exactly coincident with the existing point? xp = +tree._x.call(null, node.data); yp = +tree._y.call(null, node.data); if (x === xp && y === yp) return leaf.next = node, parent ? parent[i] = leaf : tree._root = leaf, tree; // Otherwise, split the leaf node until the old and new point are separated. do { parent = parent ? parent[i] = new Array(4) : tree._root = new Array(4); if (right = x >= (xm = (x0 + x1) / 2)) x0 = xm; else x1 = xm; if (bottom = y >= (ym = (y0 + y1) / 2)) y0 = ym; else y1 = ym; } while ((i = bottom << 1 | right) === (j = (yp >= ym) << 1 | (xp >= xm))); return parent[j] = node, parent[i] = leaf, tree; } export function addAll(data) { var d, i, n = data.length, x, y, xz = new Array(n), yz = new Array(n), x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity; // Compute the points and their extent. for (i = 0; i < n; ++i) { if (isNaN(x = +this._x.call(null, d = data[i])) || isNaN(y = +this._y.call(null, d))) continue; xz[i] = x; yz[i] = y; if (x < x0) x0 = x; if (x > x1) x1 = x; if (y < y0) y0 = y; if (y > y1) y1 = y; } // If there were no (valid) points, inherit the existing extent. if (x1 < x0) x0 = this._x0, x1 = this._x1; if (y1 < y0) y0 = this._y0, y1 = this._y1; // Expand the tree to cover the new points. this.cover(x0, y0).cover(x1, y1); // Add the new points. for (i = 0; i < n; ++i) { add(this, xz[i], yz[i], data[i]); } return this; } d3-quadtree-1.0.3/src/cover.js000066400000000000000000000032031306056417700161030ustar00rootroot00000000000000export default function(x, y) { if (isNaN(x = +x) || isNaN(y = +y)) return this; // ignore invalid points var x0 = this._x0, y0 = this._y0, x1 = this._x1, y1 = this._y1; // If the quadtree has no extent, initialize them. // Integer extent are necessary so that if we later double the extent, // the existing quadrant boundaries don’t change due to floating point error! if (isNaN(x0)) { x1 = (x0 = Math.floor(x)) + 1; y1 = (y0 = Math.floor(y)) + 1; } // Otherwise, double repeatedly to cover. else if (x0 > x || x > x1 || y0 > y || y > y1) { var z = x1 - x0, node = this._root, parent, i; switch (i = (y < (y0 + y1) / 2) << 1 | (x < (x0 + x1) / 2)) { case 0: { do parent = new Array(4), parent[i] = node, node = parent; while (z *= 2, x1 = x0 + z, y1 = y0 + z, x > x1 || y > y1); break; } case 1: { do parent = new Array(4), parent[i] = node, node = parent; while (z *= 2, x0 = x1 - z, y1 = y0 + z, x0 > x || y > y1); break; } case 2: { do parent = new Array(4), parent[i] = node, node = parent; while (z *= 2, x1 = x0 + z, y0 = y1 - z, x > x1 || y0 > y); break; } case 3: { do parent = new Array(4), parent[i] = node, node = parent; while (z *= 2, x0 = x1 - z, y0 = y1 - z, x0 > x || y0 > y); break; } } if (this._root && this._root.length) this._root = node; } // If the quadtree covers the point already, just return. else return this; this._x0 = x0; this._y0 = y0; this._x1 = x1; this._y1 = y1; return this; } d3-quadtree-1.0.3/src/data.js000066400000000000000000000002521306056417700156770ustar00rootroot00000000000000export default function() { var data = []; this.visit(function(node) { if (!node.length) do data.push(node.data); while (node = node.next) }); return data; } d3-quadtree-1.0.3/src/extent.js000066400000000000000000000003161306056417700162760ustar00rootroot00000000000000export default function(_) { return arguments.length ? this.cover(+_[0][0], +_[0][1]).cover(+_[1][0], +_[1][1]) : isNaN(this._x0) ? undefined : [[this._x0, this._y0], [this._x1, this._y1]]; } d3-quadtree-1.0.3/src/find.js000066400000000000000000000032401306056417700157060ustar00rootroot00000000000000import Quad from "./quad"; export default function(x, y, radius) { var data, x0 = this._x0, y0 = this._y0, x1, y1, x2, y2, x3 = this._x1, y3 = this._y1, quads = [], node = this._root, q, i; if (node) quads.push(new Quad(node, x0, y0, x3, y3)); if (radius == null) radius = Infinity; else { x0 = x - radius, y0 = y - radius; x3 = x + radius, y3 = y + radius; radius *= radius; } while (q = quads.pop()) { // Stop searching if this quadrant can’t contain a closer node. if (!(node = q.node) || (x1 = q.x0) > x3 || (y1 = q.y0) > y3 || (x2 = q.x1) < x0 || (y2 = q.y1) < y0) continue; // Bisect the current quadrant. if (node.length) { var xm = (x1 + x2) / 2, ym = (y1 + y2) / 2; quads.push( new Quad(node[3], xm, ym, x2, y2), new Quad(node[2], x1, ym, xm, y2), new Quad(node[1], xm, y1, x2, ym), new Quad(node[0], x1, y1, xm, ym) ); // Visit the closest quadrant first. if (i = (y >= ym) << 1 | (x >= xm)) { q = quads[quads.length - 1]; quads[quads.length - 1] = quads[quads.length - 1 - i]; quads[quads.length - 1 - i] = q; } } // Visit this point. (Visiting coincident points isn’t necessary!) else { var dx = x - +this._x.call(null, node.data), dy = y - +this._y.call(null, node.data), d2 = dx * dx + dy * dy; if (d2 < radius) { var d = Math.sqrt(radius = d2); x0 = x - d, y0 = y - d; x3 = x + d, y3 = y + d; data = node.data; } } } return data; } d3-quadtree-1.0.3/src/quad.js000066400000000000000000000002061306056417700157170ustar00rootroot00000000000000export default function(node, x0, y0, x1, y1) { this.node = node; this.x0 = x0; this.y0 = y0; this.x1 = x1; this.y1 = y1; } d3-quadtree-1.0.3/src/quadtree.js000066400000000000000000000040351306056417700166030ustar00rootroot00000000000000import tree_add, {addAll as tree_addAll} from "./add"; import tree_cover from "./cover"; import tree_data from "./data"; import tree_extent from "./extent"; import tree_find from "./find"; import tree_remove, {removeAll as tree_removeAll} from "./remove"; import tree_root from "./root"; import tree_size from "./size"; import tree_visit from "./visit"; import tree_visitAfter from "./visitAfter"; import tree_x, {defaultX} from "./x"; import tree_y, {defaultY} from "./y"; export default function quadtree(nodes, x, y) { var tree = new Quadtree(x == null ? defaultX : x, y == null ? defaultY : y, NaN, NaN, NaN, NaN); return nodes == null ? tree : tree.addAll(nodes); } function Quadtree(x, y, x0, y0, x1, y1) { this._x = x; this._y = y; this._x0 = x0; this._y0 = y0; this._x1 = x1; this._y1 = y1; this._root = undefined; } function leaf_copy(leaf) { var copy = {data: leaf.data}, next = copy; while (leaf = leaf.next) next = next.next = {data: leaf.data}; return copy; } var treeProto = quadtree.prototype = Quadtree.prototype; treeProto.copy = function() { var copy = new Quadtree(this._x, this._y, this._x0, this._y0, this._x1, this._y1), node = this._root, nodes, child; if (!node) return copy; if (!node.length) return copy._root = leaf_copy(node), copy; nodes = [{source: node, target: copy._root = new Array(4)}]; while (node = nodes.pop()) { for (var i = 0; i < 4; ++i) { if (child = node.source[i]) { if (child.length) nodes.push({source: child, target: node.target[i] = new Array(4)}); else node.target[i] = leaf_copy(child); } } } return copy; }; treeProto.add = tree_add; treeProto.addAll = tree_addAll; treeProto.cover = tree_cover; treeProto.data = tree_data; treeProto.extent = tree_extent; treeProto.find = tree_find; treeProto.remove = tree_remove; treeProto.removeAll = tree_removeAll; treeProto.root = tree_root; treeProto.size = tree_size; treeProto.visit = tree_visit; treeProto.visitAfter = tree_visitAfter; treeProto.x = tree_x; treeProto.y = tree_y; d3-quadtree-1.0.3/src/remove.js000066400000000000000000000035521306056417700162710ustar00rootroot00000000000000export default function(d) { if (isNaN(x = +this._x.call(null, d)) || isNaN(y = +this._y.call(null, d))) return this; // ignore invalid points var parent, node = this._root, retainer, previous, next, x0 = this._x0, y0 = this._y0, x1 = this._x1, y1 = this._y1, x, y, xm, ym, right, bottom, i, j; // If the tree is empty, initialize the root as a leaf. if (!node) return this; // Find the leaf node for the point. // While descending, also retain the deepest parent with a non-removed sibling. if (node.length) while (true) { if (right = x >= (xm = (x0 + x1) / 2)) x0 = xm; else x1 = xm; if (bottom = y >= (ym = (y0 + y1) / 2)) y0 = ym; else y1 = ym; if (!(parent = node, node = node[i = bottom << 1 | right])) return this; if (!node.length) break; if (parent[(i + 1) & 3] || parent[(i + 2) & 3] || parent[(i + 3) & 3]) retainer = parent, j = i; } // Find the point to remove. while (node.data !== d) if (!(previous = node, node = node.next)) return this; if (next = node.next) delete node.next; // If there are multiple coincident points, remove just the point. if (previous) return (next ? previous.next = next : delete previous.next), this; // If this is the root point, remove it. if (!parent) return this._root = next, this; // Remove this leaf. next ? parent[i] = next : delete parent[i]; // If the parent now contains exactly one leaf, collapse superfluous parents. if ((node = parent[0] || parent[1] || parent[2] || parent[3]) && node === (parent[3] || parent[2] || parent[1] || parent[0]) && !node.length) { if (retainer) retainer[j] = node; else this._root = node; } return this; } export function removeAll(data) { for (var i = 0, n = data.length; i < n; ++i) this.remove(data[i]); return this; } d3-quadtree-1.0.3/src/root.js000066400000000000000000000000631306056417700157510ustar00rootroot00000000000000export default function() { return this._root; } d3-quadtree-1.0.3/src/size.js000066400000000000000000000002331306056417700157370ustar00rootroot00000000000000export default function() { var size = 0; this.visit(function(node) { if (!node.length) do ++size; while (node = node.next) }); return size; } d3-quadtree-1.0.3/src/visit.js000066400000000000000000000012671306056417700161330ustar00rootroot00000000000000import Quad from "./quad"; export default function(callback) { var quads = [], q, node = this._root, child, x0, y0, x1, y1; if (node) quads.push(new Quad(node, this._x0, this._y0, this._x1, this._y1)); while (q = quads.pop()) { if (!callback(node = q.node, x0 = q.x0, y0 = q.y0, x1 = q.x1, y1 = q.y1) && node.length) { var xm = (x0 + x1) / 2, ym = (y0 + y1) / 2; if (child = node[3]) quads.push(new Quad(child, xm, ym, x1, y1)); if (child = node[2]) quads.push(new Quad(child, x0, ym, xm, y1)); if (child = node[1]) quads.push(new Quad(child, xm, y0, x1, ym)); if (child = node[0]) quads.push(new Quad(child, x0, y0, xm, ym)); } } return this; } d3-quadtree-1.0.3/src/visitAfter.js000066400000000000000000000014051306056417700171070ustar00rootroot00000000000000import Quad from "./quad"; export default function(callback) { var quads = [], next = [], q; if (this._root) quads.push(new Quad(this._root, this._x0, this._y0, this._x1, this._y1)); while (q = quads.pop()) { var node = q.node; if (node.length) { var child, x0 = q.x0, y0 = q.y0, x1 = q.x1, y1 = q.y1, xm = (x0 + x1) / 2, ym = (y0 + y1) / 2; if (child = node[0]) quads.push(new Quad(child, x0, y0, xm, ym)); if (child = node[1]) quads.push(new Quad(child, xm, y0, x1, ym)); if (child = node[2]) quads.push(new Quad(child, x0, ym, xm, y1)); if (child = node[3]) quads.push(new Quad(child, xm, ym, x1, y1)); } next.push(q); } while (q = next.pop()) { callback(q.node, q.x0, q.y0, q.x1, q.y1); } return this; } d3-quadtree-1.0.3/src/x.js000066400000000000000000000002121306056417700152310ustar00rootroot00000000000000export function defaultX(d) { return d[0]; } export default function(_) { return arguments.length ? (this._x = _, this) : this._x; } d3-quadtree-1.0.3/src/y.js000066400000000000000000000002121306056417700152320ustar00rootroot00000000000000export function defaultY(d) { return d[1]; } export default function(_) { return arguments.length ? (this._y = _, this) : this._y; } d3-quadtree-1.0.3/test/000077500000000000000000000000001306056417700146215ustar00rootroot00000000000000d3-quadtree-1.0.3/test/add-test.js000066400000000000000000000056521306056417700166740ustar00rootroot00000000000000var tape = require("tape"), d3_quadtree = require("../"); tape("quadtree.add(datum) creates a new point and adds it to the quadtree", function(test) { var q = d3_quadtree.quadtree(); test.deepEqual(q.add([0, 0]).root(), {data: [0, 0]}); test.deepEqual(q.add([1, 1]).root(), [{data: [0, 0]},,, {data: [1, 1]}]); test.deepEqual(q.add([1, 0]).root(), [{data: [0, 0]}, {data: [1, 0]},, {data: [1, 1]}]); test.deepEqual(q.add([0, 1]).root(), [{data: [0, 0]}, {data: [1, 0]}, {data: [0, 1]}, {data: [1, 1]}]); test.deepEqual(q.add([0.4, 0.4]).root(), [[{data: [0, 0]},,, {data: [0.4, 0.4]}], {data: [1, 0]}, {data: [0, 1]}, {data: [1, 1]}]); test.end(); }); tape("quadtree.add(datum) handles points being on the perimeter of the quadtree bounds", function(test) { var results = [], q = d3_quadtree.quadtree().extent([[0, 0], [1, 1]]); test.deepEqual(q.add([0, 0]).root(), {data: [0, 0]}); test.deepEqual(q.add([1, 1]).root(), [{data: [0, 0]},,, {data: [1, 1]}]); test.deepEqual(q.add([1, 0]).root(), [{data: [0, 0]}, {data: [1, 0]},, {data: [1, 1]}]); test.deepEqual(q.add([0, 1]).root(), [{data: [0, 0]}, {data: [1, 0]}, {data: [0, 1]}, {data: [1, 1]}]); test.end(); }); tape("quadtree.add(datum) handles points being to the top of the quadtree bounds", function(test) { var results = [], q = d3_quadtree.quadtree().extent([[0, 0], [2, 2]]); test.deepEqual(q.add([1, -1]).extent(), [[0, -2], [4, 2]]); test.end(); }); tape("quadtree.add(datum) handles points being to the right of the quadtree bounds", function(test) { var results = [], q = d3_quadtree.quadtree().extent([[0, 0], [2, 2]]); test.deepEqual(q.add([3, 1]).extent(), [[0, 0], [4, 4]]); test.end(); }); tape("quadtree.add(datum) handles points being to the bottom of the quadtree bounds", function(test) { var results = [], q = d3_quadtree.quadtree().extent([[0, 0], [2, 2]]); test.deepEqual(q.add([1, 3]).extent(), [[0, 0], [4, 4]]); test.end(); }); tape("quadtree.add(datum) handles points being to the left of the quadtree bounds", function(test) { var results = [], q = d3_quadtree.quadtree().extent([[0, 0], [2, 2]]); test.deepEqual(q.add([-1, 1]).extent(), [[-2, 0], [2, 4]]); test.end(); }); tape("quadtree.add(datum) handles coincident points by creating a linked list", function(test) { var q = d3_quadtree.quadtree().extent([[0, 0], [1, 1]]); test.deepEqual(q.add([0, 0]).root(), {data: [0, 0]}); test.deepEqual(q.add([1, 0]).root(), [{data: [0, 0]}, {data: [1, 0]},, ]); test.deepEqual(q.add([0, 1]).root(), [{data: [0, 0]}, {data: [1, 0]}, {data: [0, 1]}, ]); test.deepEqual(q.add([0, 1]).root(), [{data: [0, 0]}, {data: [1, 0]}, {data: [0, 1], next: {data: [0, 1]}}, ]); test.end(); }); tape("quadtree.add(datum) implicitly defines trivial bounds for the first point", function(test) { var q = d3_quadtree.quadtree().add([1, 2]); test.deepEqual(q.extent(), [[1, 2], [2, 3]]); test.deepEqual(q.root(), {data: [1, 2]}); test.end(); }); d3-quadtree-1.0.3/test/addAll-test.js000066400000000000000000000034711306056417700173220ustar00rootroot00000000000000var tape = require("tape"), d3_quadtree = require("../"); tape("quadtree.addAll(data) creates new points and adds them to the quadtree", function(test) { var q = d3_quadtree.quadtree(); test.deepEqual(q.add([0, 0]).root(), {data: [0, 0]}); test.deepEqual(q.add([1, 1]).root(), [{data: [0, 0]},,, {data: [1, 1]}]); test.deepEqual(q.add([1, 0]).root(), [{data: [0, 0]}, {data: [1, 0]},, {data: [1, 1]}]); test.deepEqual(q.add([0, 1]).root(), [{data: [0, 0]}, {data: [1, 0]}, {data: [0, 1]}, {data: [1, 1]}]); test.deepEqual(q.add([0.4, 0.4]).root(), [[{data: [0, 0]},,, {data: [0.4, 0.4]}], {data: [1, 0]}, {data: [0, 1]}, {data: [1, 1]}]); test.end(); }); tape("quadtree.addAll(data) ignores points with NaN coordinates", function(test) { var q = d3_quadtree.quadtree(); test.deepEqual(q.addAll([[NaN, 0], [0, NaN]]).root(), undefined); test.equal(q.extent(), undefined); test.deepEqual(q.addAll([[0, 0], [1, 1]]).root(), [{data: [0, 0]},,, {data: [1, 1]}]); test.deepEqual(q.addAll([[NaN, 0], [0, NaN]]).root(), [{data: [0, 0]},,, {data: [1, 1]}]); test.deepEqual(q.extent(), [[0, 0], [1, 1]]); test.end(); }); tape("quadtree.addAll(data) correctly handles the empty array", function(test) { var q = d3_quadtree.quadtree(); test.deepEqual(q.addAll([]).root(), undefined); test.equal(q.extent(), undefined); test.deepEqual(q.addAll([[0, 0], [1, 1]]).root(), [{data: [0, 0]},,, {data: [1, 1]}]); test.deepEqual(q.addAll([]).root(), [{data: [0, 0]},,, {data: [1, 1]}]); test.deepEqual(q.extent(), [[0, 0], [1, 1]]); test.end(); }); tape("quadtree.addAll(data) computes the extent of the data before adding", function(test) { var q = d3_quadtree.quadtree().addAll([[0.4, 0.4], [0, 0], [1, 1]]); test.deepEqual(q.root(), [[{data: [0, 0]},,, {data: [0.4, 0.4]}],,, {data: [1, 1]}]); test.end(); }); d3-quadtree-1.0.3/test/benchmark000077500000000000000000000016341306056417700165050ustar00rootroot00000000000000#!/usr/bin/env node var d3_array = require("d3-array"), d3_quadtree = require("../"); var n = 1000000, points1 = new Array(n), points2 = new Array(n); for (var j = 0; j < n; ++j) { points1[j] = [Math.random() * 99, Math.random() * 99]; } for (var j = 0; j < n; ++j) { points2[j] = [points1[j][0] + Math.random(), points1[j][1] + Math.random()]; } var start = now(); var root = d3_quadtree.quadtree().extent([[0, 0], [100, 100]]).addAll(points1); var end = now(); console.log("create", Math.round(end - start)); var start = now(); root.visit(function(node, x0, y0, x1, y1) {}); var end = now(); console.log("iterate", Math.round(end - start)); var start = now(); for (var j = 0, p; j < n; ++j) { root.remove(points1[j]); root.add(points2[j]); } var end = now(); console.log("update", Math.round(end - start)); function now() { var now = process.hrtime(); return now[0] * 1e3 + now[1] / 1e6; } d3-quadtree-1.0.3/test/copy-test.js000066400000000000000000000033121306056417700171050ustar00rootroot00000000000000var tape = require("tape"), d3_quadtree = require("../"); tape("quadtree.copy() returns a copy of this quadtree", function(test) { var q0 = d3_quadtree.quadtree().addAll([[0, 0], [1, 0], [0, 1], [1, 1]]); test.deepEqual(q0.copy(), q0); test.end(); }); tape("quadtree.copy() isolates changes to the extent", function(test) { var q0 = d3_quadtree.quadtree().extent([[0, 0], [1, 1]]), q1 = q0.copy(); q0.add([2, 2]); test.deepEqual(q1.extent(), [[0, 0], [1, 1]]); q1.add([-1, -1]); test.deepEqual(q0.extent(), [[0, 0], [2, 2]]); test.end(); }); tape("quadtree.copy() isolates changes to the root when a leaf", function(test) { var q0 = d3_quadtree.quadtree().extent([[0, 0], [1, 1]]), q1 = q0.copy(), p0 = [2, 2]; q0.add(p0); test.equal(q1.root(), undefined); q1 = q0.copy(); test.deepEqual(q0.root(), {data: [2, 2]}); test.deepEqual(q1.root(), {data: [2, 2]}); test.equal(q0.remove(p0), q0); test.equal(q0.root(), undefined); test.deepEqual(q1.root(), {data: [2, 2]}); test.end(); }); tape("quadtree.copy() isolates changes to the root when not a leaf", function(test) { var p0 = [1, 1], p1 = [2, 2], p2 = [3, 3], q0 = d3_quadtree.quadtree().extent([[0, 0], [4, 4]]).addAll([p0, p1]), q1 = q0.copy(); q0.add(p2); test.deepEqual(q0.extent(), [[0, 0], [4, 4]]); test.deepEqual(q0.root(), [{data: [1, 1]},,, [{data: [2, 2]},,, {data: [3, 3]}]]); test.deepEqual(q1.extent(), [[0, 0], [4, 4]]); test.deepEqual(q1.root(), [{data: [1, 1]},,, {data: [2, 2]}]); q1 = q0.copy(); q0.remove(p2); test.deepEqual(q1.extent(), [[0, 0], [4, 4]]); test.deepEqual(q1.root(), [{data: [1, 1]},,, [{data: [2, 2]},,, {data: [3, 3]}]]); test.end(); }); d3-quadtree-1.0.3/test/cover-test.js000066400000000000000000000114031306056417700172510ustar00rootroot00000000000000var tape = require("tape"), d3_quadtree = require("../"); tape("quadtree.cover(x, y) sets a trivial extent if the extent was undefined", function(test) { test.deepEqual(d3_quadtree.quadtree().cover(1, 2).extent(), [[1, 2], [2, 3]]); test.end(); }); tape("quadtree.cover(x, y) sets a non-trivial squarified and centered extent if the extent was trivial", function(test) { test.deepEqual(d3_quadtree.quadtree().cover(0, 0).cover(1, 2).extent(), [[0, 0], [2, 2]]); test.end(); }); tape("quadtree.cover(x, y) ignores invalid points", function(test) { test.deepEqual(d3_quadtree.quadtree().cover(0, 0).cover(NaN, 2).extent(), [[0, 0], [1, 1]]); test.end(); }); tape("quadtree.cover(x, y) repeatedly doubles the existing extent if the extent was non-trivial", function(test) { test.deepEqual(d3_quadtree.quadtree().cover(0, 0).cover(2, 2).cover(-1, -1).extent(), [[-2, -2], [2, 2]]); test.deepEqual(d3_quadtree.quadtree().cover(0, 0).cover(2, 2).cover(1, -1).extent(), [[0, -2], [4, 2]]); test.deepEqual(d3_quadtree.quadtree().cover(0, 0).cover(2, 2).cover(3, -1).extent(), [[0, -2], [4, 2]]); test.deepEqual(d3_quadtree.quadtree().cover(0, 0).cover(2, 2).cover(3, 1).extent(), [[0, 0], [4, 4]]); test.deepEqual(d3_quadtree.quadtree().cover(0, 0).cover(2, 2).cover(3, 3).extent(), [[0, 0], [4, 4]]); test.deepEqual(d3_quadtree.quadtree().cover(0, 0).cover(2, 2).cover(1, 3).extent(), [[0, 0], [4, 4]]); test.deepEqual(d3_quadtree.quadtree().cover(0, 0).cover(2, 2).cover(-1, 3).extent(), [[-2, 0], [2, 4]]); test.deepEqual(d3_quadtree.quadtree().cover(0, 0).cover(2, 2).cover(-1, 1).extent(), [[-2, 0], [2, 4]]); test.deepEqual(d3_quadtree.quadtree().cover(0, 0).cover(2, 2).cover(-3, -3).extent(), [[-6, -6], [2, 2]]); test.deepEqual(d3_quadtree.quadtree().cover(0, 0).cover(2, 2).cover(3, -3).extent(), [[0, -6], [8, 2]]); test.deepEqual(d3_quadtree.quadtree().cover(0, 0).cover(2, 2).cover(5, -3).extent(), [[0, -6], [8, 2]]); test.deepEqual(d3_quadtree.quadtree().cover(0, 0).cover(2, 2).cover(5, 3).extent(), [[0, 0], [8, 8]]); test.deepEqual(d3_quadtree.quadtree().cover(0, 0).cover(2, 2).cover(5, 5).extent(), [[0, 0], [8, 8]]); test.deepEqual(d3_quadtree.quadtree().cover(0, 0).cover(2, 2).cover(3, 5).extent(), [[0, 0], [8, 8]]); test.deepEqual(d3_quadtree.quadtree().cover(0, 0).cover(2, 2).cover(-3, 5).extent(), [[-6, 0], [2, 8]]); test.deepEqual(d3_quadtree.quadtree().cover(0, 0).cover(2, 2).cover(-3, 3).extent(), [[-6, 0], [2, 8]]); test.end(); }); tape("quadtree.cover(x, y) repeatedly wraps the root node if it has children", function(test) { var q = d3_quadtree.quadtree().add([0, 0]).add([2, 2]); test.deepEqual(q.root(), [{data: [0, 0]},,, {data: [2, 2]}]); test.deepEqual(q.copy().cover(3, 3).root(), [[{data: [0, 0]},,, {data: [2, 2]}],,, ]); test.deepEqual(q.copy().cover(-1, 3).root(), [,[{data: [0, 0]},,, {data: [2, 2]}],, ]); test.deepEqual(q.copy().cover(3, -1).root(), [,, [{data: [0, 0]},,, {data: [2, 2]}], ]); test.deepEqual(q.copy().cover(-1, -1).root(), [,,, [{data: [0, 0]},,, {data: [2, 2]}]]); test.deepEqual(q.copy().cover(5, 5).root(), [[[{data: [0, 0]},,, {data: [2, 2]}],,, ],,, ]); test.deepEqual(q.copy().cover(-3, 5).root(), [, [,[{data: [0, 0]},,, {data: [2, 2]}],, ],, ]); test.deepEqual(q.copy().cover(5, -3).root(), [,, [,, [{data: [0, 0]},,, {data: [2, 2]}], ], ]); test.deepEqual(q.copy().cover(-3, -3).root(), [,,, [,,, [{data: [0, 0]},,, {data: [2, 2]}]]]); test.end(); }); tape("quadtree.cover(x, y) does not wrap the root node if it is a leaf", function(test) { var q = d3_quadtree.quadtree().cover(0, 0).add([2, 2]); test.deepEqual(q.root(), {data: [2, 2]}); test.deepEqual(q.copy().cover(3, 3).root(), {data: [2, 2]}); test.deepEqual(q.copy().cover(-1, 3).root(), {data: [2, 2]}); test.deepEqual(q.copy().cover(3, -1).root(), {data: [2, 2]}); test.deepEqual(q.copy().cover(-1, -1).root(), {data: [2, 2]}); test.deepEqual(q.copy().cover(5, 5).root(), {data: [2, 2]}); test.deepEqual(q.copy().cover(-3, 5).root(), {data: [2, 2]}); test.deepEqual(q.copy().cover(5, -3).root(), {data: [2, 2]}); test.deepEqual(q.copy().cover(-3, -3).root(), {data: [2, 2]}); test.end(); }); tape("quadtree.cover(x, y) does not wrap the root node if it is undefined", function(test) { var q = d3_quadtree.quadtree().cover(0, 0).cover(2, 2); test.equal(q.root(), undefined); test.equal(q.copy().cover(3, 3).root(), undefined); test.equal(q.copy().cover(-1, 3).root(), undefined); test.equal(q.copy().cover(3, -1).root(), undefined); test.equal(q.copy().cover(-1, -1).root(), undefined); test.equal(q.copy().cover(5, 5).root(), undefined); test.equal(q.copy().cover(-3, 5).root(), undefined); test.equal(q.copy().cover(5, -3).root(), undefined); test.equal(q.copy().cover(-3, -3).root(), undefined); test.end(); }); d3-quadtree-1.0.3/test/data-test.js000066400000000000000000000007741306056417700170550ustar00rootroot00000000000000var tape = require("tape"), d3_quadtree = require("../"); tape("quadtree.data() returns an array of data in the quadtree", function(test) { var q = d3_quadtree.quadtree(); test.deepEqual(q.data(), []); q.add([0, 0]).add([1, 2]); test.deepEqual(q.data(), [[0, 0], [1, 2]]); test.end(); }); tape("quadtree.data() correctly handles coincident nodes", function(test) { var q = d3_quadtree.quadtree(); q.add([0, 0]).add([0, 0]); test.deepEqual(q.data(), [[0, 0], [0, 0]]); test.end(); }); d3-quadtree-1.0.3/test/extent-test.js000066400000000000000000000043301306056417700174430ustar00rootroot00000000000000var tape = require("tape"), d3_quadtree = require("../"); tape("quadtree.extent(extent) extends the extent", function(test) { test.deepEqual(d3_quadtree.quadtree().extent([[0, 1], [2, 6]]).extent(), [[0, 1], [8, 9]]); test.end(); }); tape("quadtree.extent() can be inferred by quadtree.cover", function(test) { var q = d3_quadtree.quadtree(); test.deepEqual(q.cover(0, 0).extent(), [[0, 0], [1, 1]]); test.deepEqual(q.cover(2, 4).extent(), [[0, 0], [4, 4]]); test.end(); }); tape("quadtree.extent() can be inferred by quadtree.add", function(test) { var q = d3_quadtree.quadtree(); q.add([0, 0]); test.deepEqual(q.extent(), [[0, 0], [1, 1]]); q.add([2, 4]); test.deepEqual(q.extent(), [[0, 0], [4, 4]]); test.end(); }); tape("quadtree.extent(extent) squarifies and centers the specified extent", function(test) { test.deepEqual(d3_quadtree.quadtree().extent([[0, 1], [2, 6]]).extent(), [[0, 1], [8, 9]]); test.end(); }); tape("quadtree.extent(extent) ignores invalid extents", function(test) { test.equal(d3_quadtree.quadtree().extent([[1, NaN], [NaN, 0]]).extent(), undefined); test.equal(d3_quadtree.quadtree().extent([[NaN, 1], [0, NaN]]).extent(), undefined); test.equal(d3_quadtree.quadtree().extent([[NaN, NaN], [NaN, NaN]]).extent(), undefined); test.end(); }); tape("quadtree.extent(extent) flips inverted extents", function(test) { test.deepEqual(d3_quadtree.quadtree().extent([[1, 1], [0, 0]]).extent(), [[0, 0], [2, 2]]); test.end(); }); tape("quadtree.extent(extent) tolerates partially-valid extents", function(test) { test.deepEqual(d3_quadtree.quadtree().extent([[NaN, 0], [1, 1]]).extent(), [[1, 1], [2, 2]]); test.deepEqual(d3_quadtree.quadtree().extent([[0, NaN], [1, 1]]).extent(), [[1, 1], [2, 2]]); test.deepEqual(d3_quadtree.quadtree().extent([[0, 0], [NaN, 1]]).extent(), [[0, 0], [1, 1]]); test.deepEqual(d3_quadtree.quadtree().extent([[0, 0], [1, NaN]]).extent(), [[0, 0], [1, 1]]); test.end(); }); tape("quadtree.extent(extent) allows trivial extents", function(test) { test.deepEqual(d3_quadtree.quadtree().extent([[0, 0], [0, 0]]).extent(), [[0, 0], [1, 1]]); test.deepEqual(d3_quadtree.quadtree().extent([[1, 1], [1, 1]]).extent(), [[1, 1], [2, 2]]); test.end(); }); d3-quadtree-1.0.3/test/find-test.js000066400000000000000000000026301306056417700170550ustar00rootroot00000000000000var tape = require("tape"), d3_array = require("d3-array"), d3_quadtree = require("../"); tape("quadtree.find(x, y) returns the closest point to the given [x, y]", function(test) { var dx = 17, dy = 17, q = d3_quadtree.quadtree(); d3_array.range(dx * dy).forEach(function(i) { q.add([i % dx, i / dx | 0]); }); test.deepEqual(q.find( 0.1, 0.1), [ 0, 0]); test.deepEqual(q.find( 7.1, 7.1), [ 7, 7]); test.deepEqual(q.find( 0.1, 15.9), [ 0, 16]); test.deepEqual(q.find(15.9, 15.9), [16, 16]); test.end(); }); tape("quadtree.find(x, y, radius) returns the closest point within the search radius to the given [x, y]", function(test) { var q = d3_quadtree.quadtree([[0, 0], [100, 0], [0, 100], [100, 100]]); test.deepEqual(q.find(20, 20, Infinity), [0, 0]); test.deepEqual(q.find(20, 20, 20 * Math.SQRT2 + 1e-6), [0, 0]); test.equal(q.find(20, 20, 20 * Math.SQRT2 - 1e-6), undefined); test.deepEqual(q.find(0, 20, 20 + 1e-6), [0, 0]); test.equal(q.find(0, 20, 20 - 1e-6), undefined); test.deepEqual(q.find(20, 0, 20 + 1e-6), [0, 0]); test.equal(q.find(20, 0, 20 - 1e-6), undefined); test.end(); }); tape("quadtree.find(x, y, null) treats the given radius as Infinity", function(test) { var q = d3_quadtree.quadtree([[0, 0], [100, 0], [0, 100], [100, 100]]); test.deepEqual(q.find(20, 20, null), [0, 0]); test.deepEqual(q.find(20, 20, undefined), [0, 0]); test.end(); }); d3-quadtree-1.0.3/test/quadtree-test.js000066400000000000000000000020561306056417700177510ustar00rootroot00000000000000var tape = require("tape"), d3_quadtree = require("../"); tape("d3.quadtree() creates an empty quadtree", function(test) { var q = d3_quadtree.quadtree(); test.ok(q instanceof d3_quadtree.quadtree); test.equal(q.visit(function(node, x0, y0, x1, y1) { throw new Error; }), q); test.equal(q.size(), 0); test.equal(q.extent(), undefined); test.equal(q.root(), undefined); test.deepEqual(q.data(), []); test.end(); }); tape("d3.quadtree(nodes) is equivalent to d3.quadtree().addAll(nodes)", function(test) { var q = d3_quadtree.quadtree([[0, 0], [1, 1]]); test.ok(q instanceof d3_quadtree.quadtree); test.deepEqual(q.root(), [{data: [0, 0]},,, {data: [1, 1]}]); test.end(); }); tape("d3.quadtree(nodes, x, y) is equivalent to d3.quadtree().x(x).y(y).addAll(nodes)", function(test) { var q = d3_quadtree.quadtree([{x: 0, y: 0}, {x: 1, y: 1}], function(d) { return d.x; }, function(d) { return d.y; }); test.ok(q instanceof d3_quadtree.quadtree); test.deepEqual(q.root(), [{data: {x: 0, y: 0}},,, {data: {x: 1, y: 1}}]); test.end(); }); d3-quadtree-1.0.3/test/remove-test.js000066400000000000000000000072061306056417700174360ustar00rootroot00000000000000var tape = require("tape"), d3_quadtree = require("../"); tape("quadtree.remove(datum) removes a point and returns the quadtree", function(test) { var p0 = [1, 1], q = d3_quadtree.quadtree().add(p0); test.deepEqual(q.root(), {data: p0}); test.equal(q.remove(p0), q); test.deepEqual(q.root(), undefined); test.end(); }); tape("quadtree.remove(datum) removes the only point in the quadtree", function(test) { var p0 = [1, 1], q = d3_quadtree.quadtree().add(p0); test.equal(q.remove(p0), q); test.deepEqual(q.extent(), [[1, 1], [2, 2]]); test.deepEqual(q.root(), undefined); test.deepEqual(p0, [1, 1]); test.end(); }); tape("quadtree.remove(datum) removes a first coincident point at the root in the quadtree", function(test) { var p0 = [1, 1], p1 = [1, 1], q = d3_quadtree.quadtree().addAll([p0, p1]); test.equal(q.remove(p0), q); test.deepEqual(q.extent(), [[1, 1], [2, 2]]); test.equal(q.root().data, p1); test.deepEqual(p0, [1, 1]); test.deepEqual(p1, [1, 1]); test.end(); }); tape("quadtree.remove(datum) removes another coincident point at the root in the quadtree", function(test) { var p0 = [1, 1], p1 = [1, 1], q = d3_quadtree.quadtree().addAll([p0, p1]); test.equal(q.remove(p1), q); test.deepEqual(q.extent(), [[1, 1], [2, 2]]); test.equal(q.root().data, p0); test.deepEqual(p0, [1, 1]); test.deepEqual(p1, [1, 1]); test.end(); }); tape("quadtree.remove(datum) removes a non-root point in the quadtree", function(test) { var p0 = [0, 0], p1 = [1, 1], q = d3_quadtree.quadtree().addAll([p0, p1]); test.equal(q.remove(p0), q); test.deepEqual(q.extent(), [[0, 0], [1, 1]]); test.equal(q.root().data, p1); test.deepEqual(p0, [0, 0]); test.deepEqual(p1, [1, 1]); test.end(); }); tape("quadtree.remove(datum) removes another non-root point in the quadtree", function(test) { var p0 = [0, 0], p1 = [1, 1], q = d3_quadtree.quadtree().addAll([p0, p1]); test.equal(q.remove(p1), q); test.deepEqual(q.extent(), [[0, 0], [1, 1]]); test.equal(q.root().data, p0); test.deepEqual(p0, [0, 0]); test.deepEqual(p1, [1, 1]); test.end(); }); tape("quadtree.remove(datum) ignores a point not in the quadtree", function(test) { var p0 = [0, 0], p1 = [1, 1], q0 = d3_quadtree.quadtree().add(p0), q1 = d3_quadtree.quadtree().add(p1); test.equal(q0.remove(p1), q0); test.deepEqual(q0.extent(), [[0, 0], [1, 1]]); test.equal(q0.root().data, p0); test.end(); }); tape("quadtree.remove(datum) ignores a coincident point not in the quadtree", function(test) { var p0 = [0, 0], p1 = [0, 0], q0 = d3_quadtree.quadtree().add(p0), q1 = d3_quadtree.quadtree().add(p1); test.equal(q0.remove(p1), q0); test.deepEqual(q0.extent(), [[0, 0], [1, 1]]); test.equal(q0.root().data, p0); test.end(); }); tape("quadtree.remove(datum) removes another point in the quadtree", function(test) { var q = d3_quadtree.quadtree() .extent([[0, 0], [959, 959]]) .addAll([[630, 438], [715, 464], [523, 519], [646, 318], [434, 620], [570, 489], [520, 345], [459, 443], [346, 405], [529, 444]]); test.equal(q.remove(q.find(546, 440)), q); test.deepEqual(q.extent(), [[0, 0], [1024, 1024]]); test.deepEqual(q.root(), [ [ , , , [ , , {data: [346, 405]}, {data: [459, 443]} ] ], [ , , [ {data: [520, 345]}, {data: [646, 318]}, [ , {data: [630, 438]}, {data: [570, 489]}, ], {data: [715, 464]} ], ], {data: [434, 620]}, {data: [523, 519]} ]); test.end(); }); d3-quadtree-1.0.3/test/size-test.js000066400000000000000000000007241306056417700171110ustar00rootroot00000000000000var tape = require("tape"), d3_quadtree = require("../"); tape("quadtree.size() returns the number of points in the quadtree", function(test) { var q = d3_quadtree.quadtree(); test.equal(q.size(), 0); q.add([0, 0]).add([1, 2]); test.equal(q.size(), 2); test.end(); }); tape("quadtree.size() correctly counts coincident nodes", function(test) { var q = d3_quadtree.quadtree(); q.add([0, 0]).add([0, 0]); test.equal(q.size(), 2); test.end(); }); d3-quadtree-1.0.3/test/visit-test.js000066400000000000000000000041251306056417700172740ustar00rootroot00000000000000var tape = require("tape"), d3_quadtree = require("../"); tape("quadtree.visit(callback) visits each node in a quadtree", function(test) { var results = [], q = d3_quadtree.quadtree() .addAll([[0, 0], [1, 0], [0, 1], [1, 1]]); test.equal(q.visit(function(node, x0, y0, x1, y1) { results.push([x0, y0, x1, y1]); }), q); test.deepEqual(results, [ [0.0, 0.0, 1.0, 1.0], [0.0, 0.0, 0.5, 0.5], [0.5, 0.0, 1.0, 0.5], [0.0, 0.5, 0.5, 1.0], [0.5, 0.5, 1.0, 1.0] ]); test.end(); }); tape("quadtree.visit(callback) applies pre-order traversal", function(test) { var results = [], q = d3_quadtree.quadtree() .extent([[0, 0], [960, 960]]) .addAll([[100, 100], [200, 200], [300, 300]]); test.equal(q.visit(function(node, x0, y0, x1, y1) { results.push([x0, y0, x1, y1]); }), q); test.deepEqual(results, [ [ 0, 0, 1024, 1024], [ 0, 0, 512, 512], [ 0, 0, 256, 256], [ 0, 0, 128, 128], [128, 128, 256, 256], [256, 256, 512, 512] ]); test.end(); }); tape("quadtree.visit(callback) does not recurse if the callback returns truthy", function(test) { var results = [], q = d3_quadtree.quadtree() .extent([[0, 0], [960, 960]]) .addAll([[100, 100], [700, 700], [800, 800]]); test.equal(q.visit(function(node, x0, y0, x1, y1) { results.push([x0, y0, x1, y1]); return x0 > 0; }), q); test.deepEqual(results, [ [ 0, 0, 1024, 1024], [ 0, 0, 512, 512], [512, 512, 1024, 1024] ]); test.end(); }); tape("quadtree.visit(callback) on an empty quadtree with no bounds does nothing", function(test) { var results = [], q = d3_quadtree.quadtree(); test.equal(q.visit(function(node, x0, y0, x1, y1) { results.push([x0, y0, x1, y1]); }), q); test.equal(results.length, 0); test.end(); }); tape("quadtree.visit(callback) on an empty quadtree with bounds does nothing", function(test) { var results = [], q = d3_quadtree.quadtree() .extent([[0, 0], [960, 960]]); test.equal(q.visit(function(node, x0, y0, x1, y1) { results.push([x0, y0, x1, y1]); }), q); test.deepEqual(results.length, 0); test.end(); }); d3-quadtree-1.0.3/test/x-test.js000066400000000000000000000020331306056417700164010ustar00rootroot00000000000000var tape = require("tape"), d3_quadtree = require("../"); tape("quadtree.x(x) sets the x-accessor used by quadtree.add", function(test) { var q = d3_quadtree.quadtree().x(x).add({x: 1, 1: 2}); test.deepEqual(q.extent(), [[1, 2], [2, 3]]); test.deepEqual(q.root(), {data: {x: 1, 1: 2}}); test.end(); }); tape("quadtree.x(x) sets the x-accessor used by quadtree.addAll", function(test) { var q = d3_quadtree.quadtree().x(x).addAll([{x: 1, 1: 2}]); test.deepEqual(q.extent(), [[1, 2], [2, 3]]); test.deepEqual(q.root(), {data: {x: 1, 1: 2}}); test.end(); }); tape("quadtree.x(x) sets the x-accessor used by quadtree.remove", function(test) { var p0 = {x: 0, 1: 1}, p1 = {x: 1, 1: 2}, q = d3_quadtree.quadtree().x(x); test.deepEqual(q.add(p0).root(), {data: {x: 0, 1: 1}}); test.deepEqual(q.add(p1).root(), [{data: {x: 0, 1: 1}},,, {data: {x: 1, 1: 2}}]); test.deepEqual(q.remove(p1).root(), {data: {x: 0, 1: 1}}); test.equal(q.remove(p0).root(), undefined); test.end(); }); function x(d) { return d.x; } d3-quadtree-1.0.3/test/y-test.js000066400000000000000000000020331306056417700164020ustar00rootroot00000000000000var tape = require("tape"), d3_quadtree = require("../"); tape("quadtree.y(y) sets the y-accessor used by quadtree.add", function(test) { var q = d3_quadtree.quadtree().y(y).add({0: 1, y: 2}); test.deepEqual(q.extent(), [[1, 2], [2, 3]]); test.deepEqual(q.root(), {data: {0: 1, y: 2}}); test.end(); }); tape("quadtree.y(y) sets the y-accessor used by quadtree.addAll", function(test) { var q = d3_quadtree.quadtree().y(y).addAll([{0: 1, y: 2}]); test.deepEqual(q.extent(), [[1, 2], [2, 3]]); test.deepEqual(q.root(), {data: {0: 1, y: 2}}); test.end(); }); tape("quadtree.y(y) sets the y-accessor used by quadtree.remove", function(test) { var p0 = {0: 0, y: 1}, p1 = {0: 1, y: 2}, q = d3_quadtree.quadtree().y(y); test.deepEqual(q.add(p0).root(), {data: {0: 0, y: 1}}); test.deepEqual(q.add(p1).root(), [{data: {0: 0, y: 1}},,, {data: {0: 1, y: 2}}]); test.deepEqual(q.remove(p1).root(), {data: {0: 0, y: 1}}); test.equal(q.remove(p0).root(), undefined); test.end(); }); function y(d) { return d.y; }