123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570 |
- // Copyright 2008 The Closure Library Authors. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS-IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- /**
- * @fileoverview Datastructure: A point Quad Tree for representing 2D data. Each
- * region has the same ratio as the bounds for the tree.
- *
- * The implementation currently requires pre-determined bounds for data as it
- * can not rebalance itself to that degree.
- *
- * @see ../demos/quadtree.html
- */
- goog.provide('goog.structs.QuadTree');
- goog.provide('goog.structs.QuadTree.Node');
- goog.provide('goog.structs.QuadTree.Point');
- goog.require('goog.math.Coordinate');
- /**
- * Constructs a new quad tree.
- * @param {number} minX Minimum x-value that can be held in tree.
- * @param {number} minY Minimum y-value that can be held in tree.
- * @param {number} maxX Maximum x-value that can be held in tree.
- * @param {number} maxY Maximum y-value that can be held in tree.
- * @constructor
- * @final
- */
- goog.structs.QuadTree = function(minX, minY, maxX, maxY) {
- /**
- * Count of the number of items in the tree.
- * @private {number}
- */
- this.count_ = 0;
- /**
- * The root node for the quad tree.
- * @private {goog.structs.QuadTree.Node}
- */
- this.root_ =
- new goog.structs.QuadTree.Node(minX, minY, maxX - minX, maxY - minY);
- };
- /**
- * Returns a reference to the tree's root node. Callers shouldn't modify nodes,
- * directly. This is a convenience for visualization and debugging purposes.
- * @return {goog.structs.QuadTree.Node} The root node.
- */
- goog.structs.QuadTree.prototype.getRootNode = function() {
- return this.root_;
- };
- /**
- * Sets the value of an (x, y) point within the quad-tree.
- * @param {number} x The x-coordinate.
- * @param {number} y The y-coordinate.
- * @param {*} value The value associated with the point.
- */
- goog.structs.QuadTree.prototype.set = function(x, y, value) {
- var root = this.root_;
- if (x < root.x || y < root.y || x > root.x + root.w || y > root.y + root.h) {
- throw Error('Out of bounds : (' + x + ', ' + y + ')');
- }
- if (this.insert_(root, new goog.structs.QuadTree.Point(x, y, value))) {
- this.count_++;
- }
- };
- /**
- * Gets the value of the point at (x, y) or null if the point is empty.
- * @param {number} x The x-coordinate.
- * @param {number} y The y-coordinate.
- * @param {*=} opt_default The default value to return if the node doesn't
- * exist.
- * @return {*} The value of the node, the default value if the node
- * doesn't exist, or undefined if the node doesn't exist and no default
- * has been provided.
- */
- goog.structs.QuadTree.prototype.get = function(x, y, opt_default) {
- var node = this.find_(this.root_, x, y);
- return node ? node.point.value : opt_default;
- };
- /**
- * Removes a point from (x, y) if it exists.
- * @param {number} x The x-coordinate.
- * @param {number} y The y-coordinate.
- * @return {*} The value of the node that was removed, or null if the
- * node doesn't exist.
- */
- goog.structs.QuadTree.prototype.remove = function(x, y) {
- var node = this.find_(this.root_, x, y);
- if (node) {
- var value = node.point.value;
- node.point = null;
- node.nodeType = goog.structs.QuadTree.NodeType.EMPTY;
- this.balance_(node);
- this.count_--;
- return value;
- } else {
- return null;
- }
- };
- /**
- * Returns true if the point at (x, y) exists in the tree.
- * @param {number} x The x-coordinate.
- * @param {number} y The y-coordinate.
- * @return {boolean} Whether the tree contains a point at (x, y).
- */
- goog.structs.QuadTree.prototype.contains = function(x, y) {
- return this.get(x, y) != null;
- };
- /**
- * @return {boolean} Whether the tree is empty.
- */
- goog.structs.QuadTree.prototype.isEmpty = function() {
- return this.root_.nodeType == goog.structs.QuadTree.NodeType.EMPTY;
- };
- /**
- * @return {number} The number of items in the tree.
- */
- goog.structs.QuadTree.prototype.getCount = function() {
- return this.count_;
- };
- /**
- * Removes all items from the tree.
- */
- goog.structs.QuadTree.prototype.clear = function() {
- this.root_.nw = this.root_.ne = this.root_.sw = this.root_.se = null;
- this.root_.nodeType = goog.structs.QuadTree.NodeType.EMPTY;
- this.root_.point = null;
- this.count_ = 0;
- };
- /**
- * Returns an array containing the coordinates of each point stored in the tree.
- * @return {!Array<goog.math.Coordinate?>} Array of coordinates.
- */
- goog.structs.QuadTree.prototype.getKeys = function() {
- var arr = [];
- this.traverse_(this.root_, function(node) {
- arr.push(new goog.math.Coordinate(node.point.x, node.point.y));
- });
- return arr;
- };
- /**
- * Returns an array containing all values stored within the tree.
- * @return {!Array<Object>} The values stored within the tree.
- */
- goog.structs.QuadTree.prototype.getValues = function() {
- var arr = [];
- this.traverse_(this.root_, function(node) {
- // Must have a point because it's a leaf.
- arr.push(node.point.value);
- });
- return arr;
- };
- /**
- * Clones the quad-tree and returns the new instance.
- * @return {!goog.structs.QuadTree} A clone of the tree.
- */
- goog.structs.QuadTree.prototype.clone = function() {
- var x1 = this.root_.x;
- var y1 = this.root_.y;
- var x2 = x1 + this.root_.w;
- var y2 = y1 + this.root_.h;
- var clone = new goog.structs.QuadTree(x1, y1, x2, y2);
- // This is inefficient as the clone needs to recalculate the structure of the
- // tree, even though we know it already. But this is easier and can be
- // optimized when/if needed.
- this.traverse_(this.root_, function(node) {
- clone.set(node.point.x, node.point.y, node.point.value);
- });
- return clone;
- };
- /**
- * Traverses the tree and calls a function on each node.
- * @param {function(?, goog.math.Coordinate, goog.structs.QuadTree)} fn
- * The function to call for every value. This function takes 3 arguments
- * (the value, the coordinate, and the tree itself) and the return value is
- * irrelevant.
- * @param {Object=} opt_obj The object to be used as the value of 'this'
- * within {@ code fn}.
- */
- goog.structs.QuadTree.prototype.forEach = function(fn, opt_obj) {
- this.traverse_(this.root_, function(node) {
- var coord = new goog.math.Coordinate(node.point.x, node.point.y);
- fn.call(opt_obj, node.point.value, coord, this);
- });
- };
- /**
- * Traverses the tree depth-first, with quadrants being traversed in clockwise
- * order (NE, SE, SW, NW). The provided function will be called for each
- * leaf node that is encountered.
- * @param {goog.structs.QuadTree.Node} node The current node.
- * @param {function(goog.structs.QuadTree.Node)} fn The function to call
- * for each leaf node. This function takes the node as an argument, and its
- * return value is irrelevant.
- * @private
- */
- goog.structs.QuadTree.prototype.traverse_ = function(node, fn) {
- switch (node.nodeType) {
- case goog.structs.QuadTree.NodeType.LEAF:
- fn.call(this, node);
- break;
- case goog.structs.QuadTree.NodeType.POINTER:
- this.traverse_(node.ne, fn);
- this.traverse_(node.se, fn);
- this.traverse_(node.sw, fn);
- this.traverse_(node.nw, fn);
- break;
- }
- };
- /**
- * Finds a leaf node with the same (x, y) coordinates as the target point, or
- * null if no point exists.
- * @param {goog.structs.QuadTree.Node} node The node to search in.
- * @param {number} x The x-coordinate of the point to search for.
- * @param {number} y The y-coordinate of the point to search for.
- * @return {goog.structs.QuadTree.Node} The leaf node that matches the target,
- * or null if it doesn't exist.
- * @private
- */
- goog.structs.QuadTree.prototype.find_ = function(node, x, y) {
- switch (node.nodeType) {
- case goog.structs.QuadTree.NodeType.EMPTY:
- return null;
- case goog.structs.QuadTree.NodeType.LEAF:
- return node.point.x == x && node.point.y == y ? node : null;
- case goog.structs.QuadTree.NodeType.POINTER:
- return this.find_(this.getQuadrantForPoint_(node, x, y), x, y);
- default:
- throw Error('Invalid nodeType');
- }
- };
- /**
- * Inserts a point into the tree, updating the tree's structure if necessary.
- * @param {goog.structs.QuadTree.Node} parent The parent to insert the point
- * into.
- * @param {goog.structs.QuadTree.Point} point The point to insert.
- * @return {boolean} True if a new node was added to the tree; False if a node
- * already existed with the correpsonding coordinates and had its value
- * reset.
- * @private
- */
- goog.structs.QuadTree.prototype.insert_ = function(parent, point) {
- switch (parent.nodeType) {
- case goog.structs.QuadTree.NodeType.EMPTY:
- this.setPointForNode_(parent, point);
- return true;
- case goog.structs.QuadTree.NodeType.LEAF:
- if (parent.point.x == point.x && parent.point.y == point.y) {
- this.setPointForNode_(parent, point);
- return false;
- } else {
- this.split_(parent);
- return this.insert_(parent, point);
- }
- case goog.structs.QuadTree.NodeType.POINTER:
- return this.insert_(
- this.getQuadrantForPoint_(parent, point.x, point.y), point);
- default:
- throw Error('Invalid nodeType in parent');
- }
- };
- /**
- * Converts a leaf node to a pointer node and reinserts the node's point into
- * the correct child.
- * @param {goog.structs.QuadTree.Node} node The node to split.
- * @private
- */
- goog.structs.QuadTree.prototype.split_ = function(node) {
- var oldPoint = node.point;
- node.point = null;
- node.nodeType = goog.structs.QuadTree.NodeType.POINTER;
- var x = node.x;
- var y = node.y;
- var hw = node.w / 2;
- var hh = node.h / 2;
- node.nw = new goog.structs.QuadTree.Node(x, y, hw, hh, node);
- node.ne = new goog.structs.QuadTree.Node(x + hw, y, hw, hh, node);
- node.sw = new goog.structs.QuadTree.Node(x, y + hh, hw, hh, node);
- node.se = new goog.structs.QuadTree.Node(x + hw, y + hh, hw, hh, node);
- this.insert_(node, oldPoint);
- };
- /**
- * Attempts to balance a node. A node will need balancing if all its children
- * are empty or it contains just one leaf.
- * @param {goog.structs.QuadTree.Node} node The node to balance.
- * @private
- */
- goog.structs.QuadTree.prototype.balance_ = function(node) {
- switch (node.nodeType) {
- case goog.structs.QuadTree.NodeType.EMPTY:
- case goog.structs.QuadTree.NodeType.LEAF:
- if (node.parent) {
- this.balance_(node.parent);
- }
- break;
- case goog.structs.QuadTree.NodeType.POINTER:
- var nw = node.nw, ne = node.ne, sw = node.sw, se = node.se;
- var firstLeaf = null;
- // Look for the first non-empty child, if there is more than one then we
- // break as this node can't be balanced.
- if (nw.nodeType != goog.structs.QuadTree.NodeType.EMPTY) {
- firstLeaf = nw;
- }
- if (ne.nodeType != goog.structs.QuadTree.NodeType.EMPTY) {
- if (firstLeaf) {
- break;
- }
- firstLeaf = ne;
- }
- if (sw.nodeType != goog.structs.QuadTree.NodeType.EMPTY) {
- if (firstLeaf) {
- break;
- }
- firstLeaf = sw;
- }
- if (se.nodeType != goog.structs.QuadTree.NodeType.EMPTY) {
- if (firstLeaf) {
- break;
- }
- firstLeaf = se;
- }
- if (!firstLeaf) {
- // All child nodes are empty: so make this node empty.
- node.nodeType = goog.structs.QuadTree.NodeType.EMPTY;
- node.nw = node.ne = node.sw = node.se = null;
- } else if (firstLeaf.nodeType == goog.structs.QuadTree.NodeType.POINTER) {
- // Only child was a pointer, therefore we can't rebalance.
- break;
- } else {
- // Only child was a leaf: so update node's point and make it a leaf.
- node.nodeType = goog.structs.QuadTree.NodeType.LEAF;
- node.nw = node.ne = node.sw = node.se = null;
- node.point = firstLeaf.point;
- }
- // Try and balance the parent as well.
- if (node.parent) {
- this.balance_(node.parent);
- }
- break;
- }
- };
- /**
- * Returns the child quadrant within a node that contains the given (x, y)
- * coordinate.
- * @param {goog.structs.QuadTree.Node} parent The node.
- * @param {number} x The x-coordinate to look for.
- * @param {number} y The y-coordinate to look for.
- * @return {goog.structs.QuadTree.Node} The child quadrant that contains the
- * point.
- * @private
- */
- goog.structs.QuadTree.prototype.getQuadrantForPoint_ = function(parent, x, y) {
- var mx = parent.x + parent.w / 2;
- var my = parent.y + parent.h / 2;
- if (x < mx) {
- return y < my ? parent.nw : parent.sw;
- } else {
- return y < my ? parent.ne : parent.se;
- }
- };
- /**
- * Sets the point for a node, as long as the node is a leaf or empty.
- * @param {goog.structs.QuadTree.Node} node The node to set the point for.
- * @param {goog.structs.QuadTree.Point} point The point to set.
- * @private
- */
- goog.structs.QuadTree.prototype.setPointForNode_ = function(node, point) {
- if (node.nodeType == goog.structs.QuadTree.NodeType.POINTER) {
- throw Error('Can not set point for node of type POINTER');
- }
- node.nodeType = goog.structs.QuadTree.NodeType.LEAF;
- node.point = point;
- };
- /**
- * Enumeration of node types.
- * @enum {number}
- */
- goog.structs.QuadTree.NodeType = {
- EMPTY: 0,
- LEAF: 1,
- POINTER: 2
- };
- /**
- * Constructs a new quad tree node.
- * @param {number} x X-coordiate of node.
- * @param {number} y Y-coordinate of node.
- * @param {number} w Width of node.
- * @param {number} h Height of node.
- * @param {goog.structs.QuadTree.Node=} opt_parent Optional parent node.
- * @constructor
- * @final
- */
- goog.structs.QuadTree.Node = function(x, y, w, h, opt_parent) {
- /**
- * The x-coordinate of the node.
- * @type {number}
- */
- this.x = x;
- /**
- * The y-coordinate of the node.
- * @type {number}
- */
- this.y = y;
- /**
- * The width of the node.
- * @type {number}
- */
- this.w = w;
- /**
- * The height of the node.
- * @type {number}
- */
- this.h = h;
- /**
- * The parent node.
- * @type {goog.structs.QuadTree.Node?}
- */
- this.parent = opt_parent || null;
- };
- /**
- * The node's type.
- * @type {goog.structs.QuadTree.NodeType}
- */
- goog.structs.QuadTree.Node.prototype.nodeType =
- goog.structs.QuadTree.NodeType.EMPTY;
- /**
- * The child node in the North-West quadrant.
- * @type {goog.structs.QuadTree.Node?}
- */
- goog.structs.QuadTree.Node.prototype.nw = null;
- /**
- * The child node in the North-East quadrant.
- * @type {goog.structs.QuadTree.Node?}
- */
- goog.structs.QuadTree.Node.prototype.ne = null;
- /**
- * The child node in the South-West quadrant.
- * @type {goog.structs.QuadTree.Node?}
- */
- goog.structs.QuadTree.Node.prototype.sw = null;
- /**
- * The child node in the South-East quadrant.
- * @type {goog.structs.QuadTree.Node?}
- */
- goog.structs.QuadTree.Node.prototype.se = null;
- /**
- * The point for the node, if it is a leaf node.
- * @type {goog.structs.QuadTree.Point?}
- */
- goog.structs.QuadTree.Node.prototype.point = null;
- /**
- * Creates a new point object.
- * @param {number} x The x-coordinate of the point.
- * @param {number} y The y-coordinate of the point.
- * @param {*=} opt_value Optional value associated with the point.
- * @constructor
- * @final
- */
- goog.structs.QuadTree.Point = function(x, y, opt_value) {
- /**
- * The x-coordinate for the point.
- * @type {number}
- */
- this.x = x;
- /**
- * The y-coordinate for the point.
- * @type {number}
- */
- this.y = y;
- /**
- * Optional value associated with the point.
- * @type {*}
- */
- this.value = goog.isDef(opt_value) ? opt_value : null;
- };
|