Base ol.TileBounds on ol.Rectangle instead of goog.math.Box
This commit is contained in:
@@ -1,28 +1,52 @@
|
||||
goog.provide('ol.TileBounds');
|
||||
|
||||
goog.require('goog.asserts');
|
||||
goog.require('goog.math.Box');
|
||||
goog.require('ol.Rectangle');
|
||||
goog.require('ol.TileCoord');
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
* @extends {goog.math.Box}
|
||||
* @param {number} top Top.
|
||||
* @param {number} right Right.
|
||||
* @param {number} bottom Bottom.
|
||||
* @param {number} left Left.
|
||||
* @extends {ol.Rectangle}
|
||||
* @param {number} minX Minimum X.
|
||||
* @param {number} minY Minimum Y.
|
||||
* @param {number} maxX Maximum X.
|
||||
* @param {number} maxY Maximum Y.
|
||||
*/
|
||||
ol.TileBounds = function(top, right, bottom, left) {
|
||||
|
||||
goog.asserts.assert(top <= bottom);
|
||||
goog.asserts.assert(left <= right);
|
||||
|
||||
goog.base(this, top, right, bottom, left);
|
||||
|
||||
ol.TileBounds = function(minX, minY, maxX, maxY) {
|
||||
goog.base(this, minX, minY, maxX, maxY);
|
||||
};
|
||||
goog.inherits(ol.TileBounds, ol.Rectangle);
|
||||
|
||||
|
||||
/**
|
||||
* @param {...ol.TileCoord} var_args Tile coordinates.
|
||||
* @return {!ol.TileBounds} Bounding tile box.
|
||||
*/
|
||||
ol.TileBounds.boundingTileBounds = function(var_args) {
|
||||
var tileCoord0 = arguments[0];
|
||||
var tileBounds = new ol.TileBounds(tileCoord0.x, tileCoord0.y,
|
||||
tileCoord0.x, tileCoord0.y);
|
||||
var i;
|
||||
for (i = 1; i < arguments.length; ++i) {
|
||||
var tileCoord = arguments[i];
|
||||
goog.asserts.assert(tileCoord.z == tileCoord0.z);
|
||||
tileBounds.minX = Math.min(tileBounds.minX, tileCoord.x);
|
||||
tileBounds.minY = Math.min(tileBounds.minY, tileCoord.y);
|
||||
tileBounds.maxX = Math.max(tileBounds.maxX, tileCoord.x);
|
||||
tileBounds.maxY = Math.max(tileBounds.maxY, tileCoord.y);
|
||||
}
|
||||
return tileBounds;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return {ol.TileBounds} Clone.
|
||||
*/
|
||||
ol.TileBounds.prototype.clone = function() {
|
||||
return new ol.TileBounds(this.minX, this.minY, this.maxX, this.maxY);
|
||||
};
|
||||
goog.inherits(ol.TileBounds, goog.math.Box);
|
||||
|
||||
|
||||
/**
|
||||
@@ -34,9 +58,9 @@ goog.inherits(ol.TileBounds, goog.math.Box);
|
||||
ol.TileBounds.prototype.forEachTileCoord = function(z, f, opt_obj) {
|
||||
var tileCoord = new ol.TileCoord(z, 0, 0);
|
||||
var x, y;
|
||||
for (x = this.left; x <= this.right; ++x) {
|
||||
for (x = this.minX; x <= this.maxX; ++x) {
|
||||
tileCoord.x = x;
|
||||
for (y = this.top; y <= this.bottom; ++y) {
|
||||
for (y = this.minY; y <= this.maxY; ++y) {
|
||||
tileCoord.y = y;
|
||||
if (f.call(opt_obj, tileCoord)) {
|
||||
return;
|
||||
@@ -47,24 +71,3 @@ ol.TileBounds.prototype.forEachTileCoord = function(z, f, opt_obj) {
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @param {...ol.TileCoord} var_args Tile coordinates.
|
||||
* @return {!ol.TileBounds} Bounding tile box.
|
||||
*/
|
||||
ol.TileBounds.boundingTileBounds = function(var_args) {
|
||||
var tileCoord0 = arguments[0];
|
||||
var tileBounds = new ol.TileBounds(tileCoord0.y, tileCoord0.x,
|
||||
tileCoord0.y, tileCoord0.x);
|
||||
var i;
|
||||
for (i = 1; i < arguments.length; ++i) {
|
||||
var tileCoord = arguments[i];
|
||||
goog.asserts.assert(tileCoord.z == tileCoord0.z);
|
||||
tileBounds.top = Math.min(tileBounds.top, tileCoord.y);
|
||||
tileBounds.right = Math.max(tileBounds.right, tileCoord.x);
|
||||
tileBounds.bottom = Math.max(tileBounds.bottom, tileCoord.y);
|
||||
tileBounds.left = Math.min(tileBounds.left, tileCoord.x);
|
||||
}
|
||||
return tileBounds;
|
||||
};
|
||||
|
||||
@@ -2,34 +2,46 @@ goog.require('goog.testing.jsunit');
|
||||
goog.require('ol.TileBounds');
|
||||
|
||||
|
||||
function testClone() {
|
||||
var tileBounds = new ol.TileBounds(1, 2, 3, 4);
|
||||
var clonedTileBounds = tileBounds.clone();
|
||||
assertTrue(clonedTileBounds instanceof ol.TileBounds);
|
||||
assertFalse(clonedTileBounds === tileBounds);
|
||||
assertEquals(tileBounds.minX, clonedTileBounds.minX);
|
||||
assertEquals(tileBounds.minY, clonedTileBounds.minY);
|
||||
assertEquals(tileBounds.maxX, clonedTileBounds.maxX);
|
||||
assertEquals(tileBounds.maxY, clonedTileBounds.maxY);
|
||||
}
|
||||
|
||||
|
||||
function testContainsPositive() {
|
||||
var tb = new ol.TileBounds(0, 2, 2, 0);
|
||||
var tc = new ol.TileCoord(3, 1, 1);
|
||||
assertTrue(tb.contains(tc));
|
||||
var tileBounds = new ol.TileBounds(0, 0, 2, 2);
|
||||
var tileCoord = new ol.TileCoord(3, 1, 1);
|
||||
assertTrue(tileBounds.contains(tileCoord));
|
||||
}
|
||||
|
||||
|
||||
function testContainsNegative() {
|
||||
var tb = new ol.TileBounds(0, 2, 2, 0);
|
||||
var tc = new ol.TileCoord(3, 1, 3);
|
||||
assertFalse(tb.contains(tc));
|
||||
var tileBounds = new ol.TileBounds(0, 0, 2, 2);
|
||||
var tileCoord = new ol.TileCoord(3, 1, 3);
|
||||
assertFalse(tileBounds.contains(tileCoord));
|
||||
}
|
||||
|
||||
|
||||
function testBoundingTileBounds() {
|
||||
var tb = new ol.TileBounds.boundingTileBounds(
|
||||
var tileBounds = new ol.TileBounds.boundingTileBounds(
|
||||
new ol.TileCoord(3, 1, 3),
|
||||
new ol.TileCoord(3, 2, 0));
|
||||
assertEquals(tb.top, 0);
|
||||
assertEquals(tb.right, 2);
|
||||
assertEquals(tb.bottom, 3);
|
||||
assertEquals(tb.left, 1);
|
||||
assertEquals(1, tileBounds.minX);
|
||||
assertEquals(0, tileBounds.minY);
|
||||
assertEquals(2, tileBounds.maxX);
|
||||
assertEquals(3, tileBounds.maxY);
|
||||
}
|
||||
|
||||
|
||||
function testBoundingTileBoundsMixedZ() {
|
||||
assertThrows(function() {
|
||||
var tb = new ol.TileBounds.boundingTileBounds(
|
||||
var tileBounds = new ol.TileBounds.boundingTileBounds(
|
||||
new ol.TileCoord(3, 1, 3),
|
||||
new ol.TileCoord(4, 2, 0));
|
||||
});
|
||||
@@ -38,7 +50,7 @@ function testBoundingTileBoundsMixedZ() {
|
||||
|
||||
function testForEachTileCoord() {
|
||||
|
||||
var tileBounds = new ol.TileBounds(2, 1, 3, 0);
|
||||
var tileBounds = new ol.TileBounds(0, 2, 1, 3);
|
||||
|
||||
var tileCoords = [];
|
||||
tileBounds.forEachTileCoord(5, function(tileCoord) {
|
||||
|
||||
Reference in New Issue
Block a user