Port ol.structs.RTree to new extents

This commit is contained in:
Tom Payne
2013-04-16 16:26:07 +02:00
parent 305089d84f
commit 1d4eaf6824
2 changed files with 39 additions and 44 deletions
+20 -24
View File
@@ -1,24 +1,23 @@
goog.provide('ol.structs.RTree'); goog.provide('ol.structs.RTree');
goog.require('goog.object'); goog.require('goog.object');
goog.require('ol.Rectangle'); goog.require('ol.extent');
/** /**
* @private * @private
* @constructor * @constructor
* @param {number} minX Minimum X. * @param {ol.Extent} bounds Extent.
* @param {number} minY Minimum Y.
* @param {number} maxX Maximum X.
* @param {number} maxY Maximum Y.
* @param {ol.structs.RTreeNode_} parent Parent node. * @param {ol.structs.RTreeNode_} parent Parent node.
* @param {number} level Level in the tree hierarchy. * @param {number} level Level in the tree hierarchy.
* @extends {ol.Rectangle}
*/ */
ol.structs.RTreeNode_ = function(minX, minY, maxX, maxY, parent, level) { ol.structs.RTreeNode_ = function(bounds, parent, level) {
goog.base(this, minX, minY, maxX, maxY); /**
* @type {ol.Extent}
*/
this.bounds = bounds;
/** /**
* @type {Object} * @type {Object}
@@ -51,17 +50,16 @@ ol.structs.RTreeNode_ = function(minX, minY, maxX, maxY, parent, level) {
this.children = []; this.children = [];
}; };
goog.inherits(ol.structs.RTreeNode_, ol.Rectangle);
/** /**
* Find all objects intersected by a rectangle. * Find all objects intersected by a rectangle.
* @param {ol.Rectangle} bounds Bounding box. * @param {ol.Extent} bounds Bounding box.
* @param {Object.<string, Object>} results Target object for results. * @param {Object.<string, Object>} results Target object for results.
* @param {string=} opt_type Type for another indexing dimension. * @param {string=} opt_type Type for another indexing dimension.
*/ */
ol.structs.RTreeNode_.prototype.find = function(bounds, results, opt_type) { ol.structs.RTreeNode_.prototype.find = function(bounds, results, opt_type) {
if (this.intersects(bounds) && if (ol.extent.intersects(this.bounds, bounds) &&
(!goog.isDef(opt_type) || this.types[opt_type] === true)) { (!goog.isDef(opt_type) || this.types[opt_type] === true)) {
var numChildren = this.children.length; var numChildren = this.children.length;
if (numChildren === 0) { if (numChildren === 0) {
@@ -79,11 +77,11 @@ ol.structs.RTreeNode_.prototype.find = function(bounds, results, opt_type) {
/** /**
* Find the appropriate node for insertion. * Find the appropriate node for insertion.
* @param {ol.Rectangle} bounds Bounding box. * @param {ol.Extent} bounds Bounding box.
* @return {ol.structs.RTreeNode_|undefined} Matching node. * @return {ol.structs.RTreeNode_|undefined} Matching node.
*/ */
ol.structs.RTreeNode_.prototype.get = function(bounds) { ol.structs.RTreeNode_.prototype.get = function(bounds) {
if (this.intersects(bounds)) { if (ol.extent.intersects(this.bounds, bounds)) {
var numChildren = this.children.length; var numChildren = this.children.length;
if (numChildren === 0) { if (numChildren === 0) {
return goog.isNull(this.parent) ? this : this.parent; return goog.isNull(this.parent) ? this : this.parent;
@@ -102,10 +100,10 @@ ol.structs.RTreeNode_.prototype.get = function(bounds) {
/** /**
* Update boxes up to the root to ensure correct bounding * Update boxes up to the root to ensure correct bounding
* @param {ol.Rectangle} bounds Bounding box. * @param {ol.Extent} bounds Bounding box.
*/ */
ol.structs.RTreeNode_.prototype.update = function(bounds) { ol.structs.RTreeNode_.prototype.update = function(bounds) {
this.extend(bounds); ol.extent.extend(this.bounds, bounds);
if (!goog.isNull(this.parent)) { if (!goog.isNull(this.parent)) {
this.parent.update(bounds); this.parent.update(bounds);
} }
@@ -129,15 +127,15 @@ ol.structs.RTreeNode_.prototype.divide = function() {
for (var i = 0; i < numChildren; ++i) { for (var i = 0; i < numChildren; ++i) {
child = this.children[i]; child = this.children[i];
if (i % half === 0) { if (i % half === 0) {
node = new ol.structs.RTreeNode_(child.minX, child.minY, node = new ol.structs.RTreeNode_(
child.maxX, child.maxY, this, this.level + 1); child.bounds.slice(), this, this.level + 1);
goog.object.extend(this.types, node.types); goog.object.extend(this.types, node.types);
this.children.push(node); this.children.push(node);
} }
child.parent = /** @type {ol.structs.RTreeNode_} */ (node); child.parent = /** @type {ol.structs.RTreeNode_} */ (node);
goog.object.extend(node.types, child.types); goog.object.extend(node.types, child.types);
node.children.push(child); node.children.push(child);
node.extend(child); ol.extent.extend(node.bounds, child.bounds);
} }
}; };
@@ -153,13 +151,13 @@ ol.structs.RTree = function() {
* @type {ol.structs.RTreeNode_} * @type {ol.structs.RTreeNode_}
*/ */
this.root_ = new ol.structs.RTreeNode_( this.root_ = new ol.structs.RTreeNode_(
-Infinity, -Infinity, Infinity, Infinity, null, 0); [-Infinity, Infinity, -Infinity, Infinity], null, 0);
}; };
/** /**
* @param {ol.Rectangle} bounds Bounding box. * @param {ol.Extent} bounds Bounding box.
* @param {string=} opt_type Type for another indexing dimension. * @param {string=} opt_type Type for another indexing dimension.
* @return {Object.<string, Object>} Results for the passed bounding box. * @return {Object.<string, Object>} Results for the passed bounding box.
*/ */
@@ -171,16 +169,14 @@ ol.structs.RTree.prototype.find = function(bounds, opt_type) {
/** /**
* @param {ol.Rectangle} bounds Bounding box. * @param {ol.Extent} bounds Bounding box.
* @param {Object} object Object to store with the passed bounds. * @param {Object} object Object to store with the passed bounds.
* @param {string=} opt_type Type for another indexing dimension. * @param {string=} opt_type Type for another indexing dimension.
*/ */
ol.structs.RTree.prototype.put = function(bounds, object, opt_type) { ol.structs.RTree.prototype.put = function(bounds, object, opt_type) {
var found = this.root_.get(bounds); var found = this.root_.get(bounds);
if (found) { if (found) {
var node = new ol.structs.RTreeNode_( var node = new ol.structs.RTreeNode_(bounds, found, found.level + 1);
bounds.minX, bounds.minY, bounds.maxX, bounds.maxY,
found, found.level + 1);
node.object = object; node.object = object;
node.objectId = goog.getUid(object).toString(); node.objectId = goog.getUid(object).toString();
+19 -20
View File
@@ -5,44 +5,44 @@ describe('ol.structs.RTree', function() {
describe('put and find', function() { describe('put and find', function() {
var rTree = new ol.structs.RTree(); var rTree = new ol.structs.RTree();
rTree.put(new ol.Rectangle(0, 0, 1, 1), 1); rTree.put([0, 1, 0, 1], 1);
rTree.put(new ol.Rectangle(1, 1, 4, 4), 2); rTree.put([1, 4, 1, 4], 2);
rTree.put(new ol.Rectangle(2, 2, 3, 3), 3); rTree.put([2, 3, 2, 3], 3);
rTree.put(new ol.Rectangle(-5, -5, -4, -4), 4); rTree.put([-5, -4, -5, -4], 4);
rTree.put(new ol.Rectangle(-4, -4, -1, -1), 5); rTree.put([-4, -1, -4, -1], 5);
rTree.put(new ol.Rectangle(-3, -3, -2, -2), 6); rTree.put([-3, -2, -3, -2], 6);
it('stores items', function() { it('stores items', function() {
expect(goog.object.getCount(rTree.find(new ol.Rectangle( expect(goog.object.getCount(rTree.find([
Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY,
Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY)))).to.be(6); Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY
]))).to.be(6);
}); });
it('filters by rectangle', function() { it('filters by rectangle', function() {
var result; var result;
result = goog.object.getValues(rTree.find(new ol.Rectangle(2, 2, 3, 3))); result = goog.object.getValues(rTree.find([2, 3, 2, 3]));
expect(result).to.contain(2); expect(result).to.contain(2);
expect(result).to.contain(3); expect(result).to.contain(3);
expect(result.length).to.be(2); expect(result.length).to.be(2);
result = goog.object.getValues( result = goog.object.getValues(rTree.find([-1, 2, -1, 2]));
rTree.find(new ol.Rectangle(-1, -1, 2, 2)));
expect(result).to.contain(1); expect(result).to.contain(1);
expect(result).to.contain(2); expect(result).to.contain(2);
expect(result).to.contain(3); expect(result).to.contain(3);
expect(result).to.contain(5); expect(result).to.contain(5);
expect(result.length).to.be(4); expect(result.length).to.be(4);
expect(goog.object.getCount(rTree.find(new ol.Rectangle(5, 5, 6, 6)))) expect(goog.object.getCount(rTree.find([5, 6, 5, 6]))).to.be(0);
.to.be(0);
}); });
it('can store thousands of items and find fast', function() { it('can store thousands of items and find fast', function() {
for (var i = 7; i <= 10000; ++i) { for (var i = 7; i <= 10000; ++i) {
rTree.put(new ol.Rectangle(Math.random() * -10, Math.random() * -10, rTree.put([
Math.random() * 10, Math.random() * 10), i); Math.random() * -10, Math.random() * 10,
Math.random() * -10, Math.random() * 10
], i);
} }
expect(goog.object.getCount( expect(goog.object.getCount(rTree.find([-10, 10, -10, 10]))).to.be(10000);
rTree.find(new ol.Rectangle(-10, -10, 10, 10)))).to.be(10000); var result = rTree.find([0, 0, 0, 0]);
var result = rTree.find(new ol.Rectangle(0, 0, 0, 0));
expect(goog.object.getCount(result)).to.be(9995); expect(goog.object.getCount(result)).to.be(9995);
var values = goog.object.getValues(result); var values = goog.object.getValues(result);
expect(values).to.contain(1); expect(values).to.contain(1);
@@ -59,5 +59,4 @@ describe('ol.structs.RTree', function() {
}); });
goog.require('goog.object'); goog.require('goog.object');
goog.require('ol.Rectangle');
goog.require('ol.structs.RTree'); goog.require('ol.structs.RTree');