| 1 | import tree_add, {addAll as tree_addAll} from "./add.js";
|
|---|
| 2 | import tree_cover from "./cover.js";
|
|---|
| 3 | import tree_data from "./data.js";
|
|---|
| 4 | import tree_extent from "./extent.js";
|
|---|
| 5 | import tree_find from "./find.js";
|
|---|
| 6 | import tree_remove, {removeAll as tree_removeAll} from "./remove.js";
|
|---|
| 7 | import tree_root from "./root.js";
|
|---|
| 8 | import tree_size from "./size.js";
|
|---|
| 9 | import tree_visit from "./visit.js";
|
|---|
| 10 | import tree_visitAfter from "./visitAfter.js";
|
|---|
| 11 | import tree_x, {defaultX} from "./x.js";
|
|---|
| 12 | import tree_y, {defaultY} from "./y.js";
|
|---|
| 13 |
|
|---|
| 14 | export default function quadtree(nodes, x, y) {
|
|---|
| 15 | var tree = new Quadtree(x == null ? defaultX : x, y == null ? defaultY : y, NaN, NaN, NaN, NaN);
|
|---|
| 16 | return nodes == null ? tree : tree.addAll(nodes);
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | function Quadtree(x, y, x0, y0, x1, y1) {
|
|---|
| 20 | this._x = x;
|
|---|
| 21 | this._y = y;
|
|---|
| 22 | this._x0 = x0;
|
|---|
| 23 | this._y0 = y0;
|
|---|
| 24 | this._x1 = x1;
|
|---|
| 25 | this._y1 = y1;
|
|---|
| 26 | this._root = undefined;
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | function leaf_copy(leaf) {
|
|---|
| 30 | var copy = {data: leaf.data}, next = copy;
|
|---|
| 31 | while (leaf = leaf.next) next = next.next = {data: leaf.data};
|
|---|
| 32 | return copy;
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | var treeProto = quadtree.prototype = Quadtree.prototype;
|
|---|
| 36 |
|
|---|
| 37 | treeProto.copy = function() {
|
|---|
| 38 | var copy = new Quadtree(this._x, this._y, this._x0, this._y0, this._x1, this._y1),
|
|---|
| 39 | node = this._root,
|
|---|
| 40 | nodes,
|
|---|
| 41 | child;
|
|---|
| 42 |
|
|---|
| 43 | if (!node) return copy;
|
|---|
| 44 |
|
|---|
| 45 | if (!node.length) return copy._root = leaf_copy(node), copy;
|
|---|
| 46 |
|
|---|
| 47 | nodes = [{source: node, target: copy._root = new Array(4)}];
|
|---|
| 48 | while (node = nodes.pop()) {
|
|---|
| 49 | for (var i = 0; i < 4; ++i) {
|
|---|
| 50 | if (child = node.source[i]) {
|
|---|
| 51 | if (child.length) nodes.push({source: child, target: node.target[i] = new Array(4)});
|
|---|
| 52 | else node.target[i] = leaf_copy(child);
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | return copy;
|
|---|
| 58 | };
|
|---|
| 59 |
|
|---|
| 60 | treeProto.add = tree_add;
|
|---|
| 61 | treeProto.addAll = tree_addAll;
|
|---|
| 62 | treeProto.cover = tree_cover;
|
|---|
| 63 | treeProto.data = tree_data;
|
|---|
| 64 | treeProto.extent = tree_extent;
|
|---|
| 65 | treeProto.find = tree_find;
|
|---|
| 66 | treeProto.remove = tree_remove;
|
|---|
| 67 | treeProto.removeAll = tree_removeAll;
|
|---|
| 68 | treeProto.root = tree_root;
|
|---|
| 69 | treeProto.size = tree_size;
|
|---|
| 70 | treeProto.visit = tree_visit;
|
|---|
| 71 | treeProto.visitAfter = tree_visitAfter;
|
|---|
| 72 | treeProto.x = tree_x;
|
|---|
| 73 | treeProto.y = tree_y;
|
|---|