Port ol.structs.RTree to new extents
This commit is contained in:
@@ -1,24 +1,23 @@
|
||||
goog.provide('ol.structs.RTree');
|
||||
|
||||
goog.require('goog.object');
|
||||
goog.require('ol.Rectangle');
|
||||
goog.require('ol.extent');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @private
|
||||
* @constructor
|
||||
* @param {number} minX Minimum X.
|
||||
* @param {number} minY Minimum Y.
|
||||
* @param {number} maxX Maximum X.
|
||||
* @param {number} maxY Maximum Y.
|
||||
* @param {ol.Extent} bounds Extent.
|
||||
* @param {ol.structs.RTreeNode_} parent Parent node.
|
||||
* @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}
|
||||
@@ -51,17 +50,16 @@ ol.structs.RTreeNode_ = function(minX, minY, maxX, maxY, parent, level) {
|
||||
this.children = [];
|
||||
|
||||
};
|
||||
goog.inherits(ol.structs.RTreeNode_, ol.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 {string=} opt_type Type for another indexing dimension.
|
||||
*/
|
||||
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)) {
|
||||
var numChildren = this.children.length;
|
||||
if (numChildren === 0) {
|
||||
@@ -79,11 +77,11 @@ ol.structs.RTreeNode_.prototype.find = function(bounds, results, opt_type) {
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
ol.structs.RTreeNode_.prototype.get = function(bounds) {
|
||||
if (this.intersects(bounds)) {
|
||||
if (ol.extent.intersects(this.bounds, bounds)) {
|
||||
var numChildren = this.children.length;
|
||||
if (numChildren === 0) {
|
||||
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
|
||||
* @param {ol.Rectangle} bounds Bounding box.
|
||||
* @param {ol.Extent} bounds Bounding box.
|
||||
*/
|
||||
ol.structs.RTreeNode_.prototype.update = function(bounds) {
|
||||
this.extend(bounds);
|
||||
ol.extent.extend(this.bounds, bounds);
|
||||
if (!goog.isNull(this.parent)) {
|
||||
this.parent.update(bounds);
|
||||
}
|
||||
@@ -129,15 +127,15 @@ ol.structs.RTreeNode_.prototype.divide = function() {
|
||||
for (var i = 0; i < numChildren; ++i) {
|
||||
child = this.children[i];
|
||||
if (i % half === 0) {
|
||||
node = new ol.structs.RTreeNode_(child.minX, child.minY,
|
||||
child.maxX, child.maxY, this, this.level + 1);
|
||||
node = new ol.structs.RTreeNode_(
|
||||
child.bounds.slice(), this, this.level + 1);
|
||||
goog.object.extend(this.types, node.types);
|
||||
this.children.push(node);
|
||||
}
|
||||
child.parent = /** @type {ol.structs.RTreeNode_} */ (node);
|
||||
goog.object.extend(node.types, child.types);
|
||||
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_}
|
||||
*/
|
||||
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.
|
||||
* @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 {string=} opt_type Type for another indexing dimension.
|
||||
*/
|
||||
ol.structs.RTree.prototype.put = function(bounds, object, opt_type) {
|
||||
var found = this.root_.get(bounds);
|
||||
if (found) {
|
||||
var node = new ol.structs.RTreeNode_(
|
||||
bounds.minX, bounds.minY, bounds.maxX, bounds.maxY,
|
||||
found, found.level + 1);
|
||||
var node = new ol.structs.RTreeNode_(bounds, found, found.level + 1);
|
||||
node.object = object;
|
||||
node.objectId = goog.getUid(object).toString();
|
||||
|
||||
|
||||
@@ -5,44 +5,44 @@ describe('ol.structs.RTree', function() {
|
||||
|
||||
describe('put and find', function() {
|
||||
var rTree = new ol.structs.RTree();
|
||||
rTree.put(new ol.Rectangle(0, 0, 1, 1), 1);
|
||||
rTree.put(new ol.Rectangle(1, 1, 4, 4), 2);
|
||||
rTree.put(new ol.Rectangle(2, 2, 3, 3), 3);
|
||||
rTree.put(new ol.Rectangle(-5, -5, -4, -4), 4);
|
||||
rTree.put(new ol.Rectangle(-4, -4, -1, -1), 5);
|
||||
rTree.put(new ol.Rectangle(-3, -3, -2, -2), 6);
|
||||
rTree.put([0, 1, 0, 1], 1);
|
||||
rTree.put([1, 4, 1, 4], 2);
|
||||
rTree.put([2, 3, 2, 3], 3);
|
||||
rTree.put([-5, -4, -5, -4], 4);
|
||||
rTree.put([-4, -1, -4, -1], 5);
|
||||
rTree.put([-3, -2, -3, -2], 6);
|
||||
|
||||
it('stores items', function() {
|
||||
expect(goog.object.getCount(rTree.find(new ol.Rectangle(
|
||||
Number.NEGATIVE_INFINITY, Number.NEGATIVE_INFINITY,
|
||||
Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY)))).to.be(6);
|
||||
expect(goog.object.getCount(rTree.find([
|
||||
Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY,
|
||||
Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY
|
||||
]))).to.be(6);
|
||||
});
|
||||
|
||||
it('filters by rectangle', function() {
|
||||
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(3);
|
||||
expect(result.length).to.be(2);
|
||||
result = goog.object.getValues(
|
||||
rTree.find(new ol.Rectangle(-1, -1, 2, 2)));
|
||||
result = goog.object.getValues(rTree.find([-1, 2, -1, 2]));
|
||||
expect(result).to.contain(1);
|
||||
expect(result).to.contain(2);
|
||||
expect(result).to.contain(3);
|
||||
expect(result).to.contain(5);
|
||||
expect(result.length).to.be(4);
|
||||
expect(goog.object.getCount(rTree.find(new ol.Rectangle(5, 5, 6, 6))))
|
||||
.to.be(0);
|
||||
expect(goog.object.getCount(rTree.find([5, 6, 5, 6]))).to.be(0);
|
||||
});
|
||||
|
||||
it('can store thousands of items and find fast', function() {
|
||||
for (var i = 7; i <= 10000; ++i) {
|
||||
rTree.put(new ol.Rectangle(Math.random() * -10, Math.random() * -10,
|
||||
Math.random() * 10, Math.random() * 10), i);
|
||||
rTree.put([
|
||||
Math.random() * -10, Math.random() * 10,
|
||||
Math.random() * -10, Math.random() * 10
|
||||
], i);
|
||||
}
|
||||
expect(goog.object.getCount(
|
||||
rTree.find(new ol.Rectangle(-10, -10, 10, 10)))).to.be(10000);
|
||||
var result = rTree.find(new ol.Rectangle(0, 0, 0, 0));
|
||||
expect(goog.object.getCount(rTree.find([-10, 10, -10, 10]))).to.be(10000);
|
||||
var result = rTree.find([0, 0, 0, 0]);
|
||||
expect(goog.object.getCount(result)).to.be(9995);
|
||||
var values = goog.object.getValues(result);
|
||||
expect(values).to.contain(1);
|
||||
@@ -59,5 +59,4 @@ describe('ol.structs.RTree', function() {
|
||||
});
|
||||
|
||||
goog.require('goog.object');
|
||||
goog.require('ol.Rectangle');
|
||||
goog.require('ol.structs.RTree');
|
||||
|
||||
Reference in New Issue
Block a user