1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
|
// 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 new 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(this:goog.structs.QuadTree, 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 new 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 new 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 new 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;
};
|