From 16f70ba1a0b4c4bf48b4f946d48e966e051dde0b Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Tue, 16 Apr 2013 16:10:04 +0200 Subject: [PATCH] Replace ol.Extent and ol.Rectangle with Array. --- src/ol/extent.exports | 27 ++- src/ol/extent.js | 359 ++++++++++++++++++++++++--------- src/ol/rectangle.js | 171 ---------------- test/spec/ol/extent.test.js | 300 ++++++++++++++++++--------- test/spec/ol/rectangle.test.js | 125 ------------ 5 files changed, 487 insertions(+), 495 deletions(-) delete mode 100644 src/ol/rectangle.js delete mode 100644 test/spec/ol/rectangle.test.js diff --git a/src/ol/extent.exports b/src/ol/extent.exports index 9ba8337eaa..74acd74e1b 100644 --- a/src/ol/extent.exports +++ b/src/ol/extent.exports @@ -1,11 +1,16 @@ -@exportSymbol ol.Extent -@exportProperty ol.Extent.prototype.getCenter -@exportProperty ol.Extent.prototype.getHeight -@exportProperty ol.Extent.prototype.getWidth -@exportProperty ol.Extent.prototype.containsCoordinate -@exportProperty ol.Extent.prototype.containsExtent -@exportProperty ol.Extent.prototype.getBottomLeft -@exportProperty ol.Extent.prototype.getBottomRight -@exportProperty ol.Extent.prototype.getTopLeft -@exportProperty ol.Extent.prototype.getTopRight -@exportProperty ol.Extent.prototype.transform +@exportSymbol ol.extent.boundingExtent +@exportSymbol ol.extent.containsCoordinate +@exportSymbol ol.extent.containsExtent +@exportSymbol ol.extent.equals +@exportSymbol ol.extent.extend +@exportSymbol ol.extent.getBottomLeft +@exportSymbol ol.extent.getBottomRight +@exportSymbol ol.extent.getCenter +@exportSymbol ol.extent.getHeight +@exportSymbol ol.extent.getSize +@exportSymbol ol.extent.getTopLeft +@exportSymbol ol.extent.getTopRight +@exportSymbol ol.extent.getWidth +@exportSymbol ol.extent.intersects +@exportSymbol ol.extent.isEmpty +@exportSymbol ol.extent.transform diff --git a/src/ol/extent.js b/src/ol/extent.js index c5e38e7a91..c620d226ef 100644 --- a/src/ol/extent.js +++ b/src/ol/extent.js @@ -1,52 +1,193 @@ goog.provide('ol.Extent'); +goog.provide('ol.extent'); +goog.require('goog.asserts'); goog.require('ol.Coordinate'); -goog.require('ol.Rectangle'); +goog.require('ol.Size'); goog.require('ol.TransformFunction'); - /** - * Rectangular extent which is not rotated. An extent does not know its - * projection. - * - * @constructor - * @extends {ol.Rectangle} - * @param {number} minX Minimum X. - * @param {number} minY Minimum Y. - * @param {number} maxX Maximum X. - * @param {number} maxY Maximum Y. + * @typedef {Array.} */ -ol.Extent = function(minX, minY, maxX, maxY) { - goog.base(this, minX, minY, maxX, maxY); -}; -goog.inherits(ol.Extent, ol.Rectangle); +ol.Extent; /** * Builds an extent that includes all given coordinates. * - * @param {...ol.Coordinate} var_args Coordinates. - * @return {!ol.Extent} Bounding extent. + * @param {Array.} coordinates Coordinates. + * @return {ol.Extent} Bounding extent. */ -ol.Extent.boundingExtent = function(var_args) { - var coordinate0 = arguments[0]; - var extent = new ol.Extent(coordinate0[0], coordinate0[1], - coordinate0[0], coordinate0[1]); +ol.extent.boundingExtent = function(coordinates) { + var extent = ol.extent.createEmptyExtent(); + var n = coordinates.length; var i; - for (i = 1; i < arguments.length; ++i) { - var coordinate = arguments[i]; - extent.extendXY(coordinate[0], coordinate[1]); + for (i = 0; i < n; ++i) { + ol.extent.extendCoordinate(extent, coordinates[i]); } return extent; }; +/** + * @param {Array.} xs Xs. + * @param {Array.} ys Ys. + * @param {ol.Extent=} opt_extent Destination extent. + * @private + * @return {ol.Extent} + */ +ol.extent.boundingExtentXYs_ = function(xs, ys, opt_extent) { + goog.asserts.assert(xs.length > 0); + goog.asserts.assert(ys.length > 0); + var minX = Math.min.apply(null, xs); + var maxX = Math.max.apply(null, xs); + var minY = Math.min.apply(null, ys); + var maxY = Math.max.apply(null, ys); + if (goog.isDef(opt_extent)) { + opt_extent[0] = minX; + opt_extent[1] = maxX; + opt_extent[2] = minY; + opt_extent[3] = maxY; + return opt_extent; + } else { + return [minX, maxX, minY, maxY]; + } +}; + + +/** + * Checks if the passed coordinate is contained or on the edge + * of the extent. + * + * @param {ol.Extent} extent Extent. + * @param {ol.Coordinate} coordinate Coordinate. + * @return {boolean} Contains. + */ +ol.extent.containsCoordinate = function(extent, coordinate) { + return extent[0] <= coordinate[0] && coordinate[0] <= extent[1] && + extent[2] <= coordinate[1] && coordinate[1] <= extent[3]; +}; + + +/** + * Checks if the passed extent is contained or on the edge of the + * extent. + * + * @param {ol.Extent} extent1 Extent 1. + * @param {ol.Extent} extent2 Extent 2. + * @return {boolean} Contains. + */ +ol.extent.containsExtent = function(extent1, extent2) { + return extent1[0] <= extent2[0] && extent2[1] <= extent1[1] && + extent1[2] <= extent2[2] && extent2[3] <= extent1[3]; +}; + + /** * @return {ol.Extent} Empty extent. */ -ol.Extent.createEmptyExtent = function() { - return new ol.Extent(Infinity, Infinity, -Infinity, -Infinity); +ol.extent.createEmptyExtent = function() { + return [Infinity, -Infinity, Infinity, -Infinity]; +}; + + +/** + * @param {number} minX Minimum X. + * @param {number} maxX Maximum X. + * @param {number} minY Minimum Y. + * @param {number} maxY Maximum Y. + * @param {ol.Extent|undefined} extent Extent. + * @return {ol.Extent} Extent. + */ +ol.extent.createOrUpdate = function(minX, maxX, minY, maxY, extent) { + if (goog.isDef(extent)) { + extent[0] = minX; + extent[1] = maxX; + extent[2] = minY; + extent[3] = maxY; + return extent; + } else { + return [minX, maxX, minY, maxY]; + } +}; + + +/** + * @param {ol.Extent} extent1 Extent 1. + * @param {ol.Extent} extent2 Extent 2. + * @return {boolean} Equals. + */ +ol.extent.equals = function(extent1, extent2) { + return extent1[0] == extent2[0] && extent1[1] == extent2[1] && + extent1[2] == extent2[2] && extent1[3] == extent2[3]; +}; + + +/** + * @param {ol.Extent} extent1 Extent 1. + * @param {ol.Extent} extent2 Extent 2. + */ +ol.extent.extend = function(extent1, extent2) { + if (extent2[0] < extent1[0]) { + extent1[0] = extent2[0]; + } + if (extent2[1] > extent1[1]) { + extent1[1] = extent2[1]; + } + if (extent2[2] < extent1[2]) { + extent1[2] = extent2[2]; + } + if (extent2[3] > extent1[3]) { + extent1[3] = extent2[3]; + } +}; + + +/** + * @param {ol.Extent} extent Extent. + * @param {ol.Coordinate} coordinate Coordinate. + */ +ol.extent.extendCoordinate = function(extent, coordinate) { + if (coordinate[0] < extent[0]) { + extent[0] = coordinate[0]; + } + if (coordinate[0] > extent[1]) { + extent[1] = coordinate[0]; + } + if (coordinate[1] < extent[2]) { + extent[2] = coordinate[1]; + } + if (coordinate[1] > extent[3]) { + extent[3] = coordinate[1]; + } +}; + + +/** + * @param {ol.Extent} extent Extent. + * @return {ol.Coordinate} Bottom left coordinate. + */ +ol.extent.getBottomLeft = function(extent) { + return [extent[0], extent[2]]; +}; + + +/** + * @param {ol.Extent} extent Extent. + * @return {ol.Coordinate} Bottom right coordinate. + */ +ol.extent.getBottomRight = function(extent) { + return [extent[1], extent[2]]; +}; + + +/** + * @param {ol.Extent} extent Extent. + * @return {ol.Coordinate} Center. + */ +ol.extent.getCenter = function(extent) { + return [(extent[0] + extent[1]) / 2, (extent[2] + extent[3]) / 2]; }; @@ -55,9 +196,11 @@ ol.Extent.createEmptyExtent = function() { * @param {number} resolution Resolution. * @param {number} rotation Rotation. * @param {ol.Size} size Size. + * @param {ol.Extent=} opt_extent Destination extent. * @return {ol.Extent} Extent. */ -ol.Extent.getForView2DAndSize = function(center, resolution, rotation, size) { +ol.extent.getForView2DAndSize = + function(center, resolution, rotation, size, opt_extent) { var dx = resolution * size.width / 2; var dy = resolution * size.height / 2; var cosRotation = Math.cos(rotation); @@ -71,102 +214,128 @@ ol.Extent.getForView2DAndSize = function(center, resolution, rotation, size) { xs[i] = center[0] + x * cosRotation - y * sinRotation; ys[i] = center[1] + x * sinRotation + y * cosRotation; } - var minX = Math.min.apply(null, xs); - var minY = Math.min.apply(null, ys); - var maxX = Math.max.apply(null, xs); - var maxY = Math.max.apply(null, ys); - return new ol.Extent(minX, minY, maxX, maxY); + return ol.extent.boundingExtentXYs_(xs, ys, opt_extent); }; /** - * Checks if the passed coordinate is contained or on the edge - * of the extent. - * - * @param {ol.Coordinate} coordinate Coordinate. - * @return {boolean} Contains. - */ -ol.Extent.prototype.containsCoordinate = function(coordinate) { - return this.minX <= coordinate[0] && coordinate[0] <= this.maxX && - this.minY <= coordinate[1] && coordinate[1] <= this.maxY; -}; - - -/** - * @param {number} minX Minimum X. - * @param {number} minY Minimum Y. - * @param {number} maxX Maximum X. - * @param {number} maxY Maximum Y. - * @param {ol.Extent|undefined} extent Extent. - * @return {ol.Extent} Extent. - */ -ol.Extent.createOrUpdate = function(minX, minY, maxX, maxY, extent) { - if (goog.isDef(extent)) { - extent.minX = minX; - extent.minY = minY; - extent.maxX = maxX; - extent.maxY = maxY; - return extent; - } else { - return new ol.Extent(minX, minY, maxX, maxY); - } -}; - - -/** - * Checks if the passed extent is contained or on the edge of the - * extent. - * * @param {ol.Extent} extent Extent. - * @return {boolean} Contains. + * @return {number} Height. */ -ol.Extent.prototype.containsExtent = function(extent) { - return this.minX <= extent.minX && extent.maxX <= this.maxX && - this.minY <= extent.minY && extent.maxY <= this.maxY; +ol.extent.getHeight = function(extent) { + return extent[3] - extent[2]; }; /** - * @return {ol.Coordinate} Bottom left coordinate. + * @param {ol.Extent} extent Extent. + * @return {ol.Size} Size. */ -ol.Extent.prototype.getBottomLeft = function() { - return [this.minX, this.minY]; -}; - - -/** - * @return {ol.Coordinate} Bottom right coordinate. - */ -ol.Extent.prototype.getBottomRight = function() { - return [this.maxX, this.minY]; +ol.extent.getSize = function(extent) { + return new ol.Size(extent[1] - extent[0], extent[3] - extent[2]); }; /** + * @param {ol.Extent} extent Extent. * @return {ol.Coordinate} Top left coordinate. */ -ol.Extent.prototype.getTopLeft = function() { - return [this.minX, this.maxY]; +ol.extent.getTopLeft = function(extent) { + return [extent[0], extent[3]]; }; /** + * @param {ol.Extent} extent Extent. * @return {ol.Coordinate} Top right coordinate. */ -ol.Extent.prototype.getTopRight = function() { - return [this.maxX, this.maxY]; +ol.extent.getTopRight = function(extent) { + return [extent[1], extent[3]]; }; /** + * @param {ol.Extent} extent Extent. + * @return {number} Width. + */ +ol.extent.getWidth = function(extent) { + return extent[1] - extent[0]; +}; + + +/** + * @param {ol.Extent} extent1 Extent 1. + * @param {ol.Extent} extent2 Extent. + * @return {boolean} Intersects. + */ +ol.extent.intersects = function(extent1, extent2) { + return extent1[0] <= extent2[1] && + extent1[1] >= extent2[0] && + extent1[2] <= extent2[3] && + extent1[3] >= extent2[2]; +}; + + +/** + * @param {ol.Extent} extent Extent. + * @return {boolean} Is empty. + */ +ol.extent.isEmpty = function(extent) { + return extent[1] < extent[0] || extent[3] < extent[2]; +}; + + +/** + * @param {ol.Extent} extent Extent. + * @param {ol.Coordinate} coordinate Coordinate. + * @return {ol.Coordinate} Coordinate. + */ +ol.extent.normalize = function(extent, coordinate) { + return [ + (coordinate[0] - extent[0]) / (extent[1] - extent[0]), + (coordinate[1] - extent[2]) / (extent[3] - extent[2]) + ]; +}; + + +/** + * @param {ol.Extent} extent Extent. + * @param {number} value Value. + */ +ol.extent.scaleFromCenter = function(extent, value) { + var deltaX = ((extent[1] - extent[0]) / 2) * (value - 1); + var deltaY = ((extent[3] - extent[2]) / 2) * (value - 1); + extent[0] -= deltaX; + extent[1] += deltaX; + extent[2] -= deltaY; + extent[3] += deltaY; +}; + + +/** + * @param {ol.Extent} extent Extent. + * @return {string} String. + */ +ol.extent.toString = function(extent) { + return '(' + [extent[0], extent[1], extent[2], extent[3]].join(', ') + ')'; +}; + + +/** + * @param {ol.Extent} extent Extent. * @param {ol.TransformFunction} transformFn Transform function. + * @param {ol.Extent=} opt_extent Destination extent. * @return {ol.Extent} Extent. */ -ol.Extent.prototype.transform = function(transformFn) { - var input = [this.minX, this.minY, this.maxX, this.maxY]; - input = transformFn(input, input, 2); - return new ol.Extent(Math.min(input[0], input[2]), - Math.min(input[1], input[3]), - Math.max(input[0], input[2]), - Math.max(input[1], input[3])); +ol.extent.transform = function(extent, transformFn, opt_extent) { + var coordinates = [ + extent[0], extent[2], + extent[0], extent[3], + extent[1], extent[2], + extent[1], extent[3] + ]; + transformFn(coordinates, coordinates, 2); + var xs = [coordinates[0], coordinates[2], coordinates[4], coordinates[6]]; + var ys = [coordinates[1], coordinates[3], coordinates[5], coordinates[7]]; + return ol.extent.boundingExtentXYs_(xs, ys, opt_extent); }; diff --git a/src/ol/rectangle.js b/src/ol/rectangle.js deleted file mode 100644 index 3a9d696413..0000000000 --- a/src/ol/rectangle.js +++ /dev/null @@ -1,171 +0,0 @@ -goog.provide('ol.Rectangle'); - -goog.require('goog.asserts'); -goog.require('ol.Size'); - - - -/** - * @constructor - * @param {number} minX Minimum X. - * @param {number} minY Minimum Y. - * @param {number} maxX Maximum X. - * @param {number} maxY Maximum Y. - */ -ol.Rectangle = function(minX, minY, maxX, maxY) { - - /** - * @type {number} - */ - this.minX = minX; - - /** - * @type {number} - */ - this.minY = minY; - - /** - * @type {number} - */ - this.maxX = maxX; - - /** - * @type {number} - */ - this.maxY = maxY; - -}; - - -/** - * @param {ol.Rectangle} rectangle Rectangle. - * @return {boolean} Equals. - */ -ol.Rectangle.prototype.equals = function(rectangle) { - return this.minX == rectangle.minX && this.minY == rectangle.minY && - this.maxX == rectangle.maxX && this.maxY == rectangle.maxY; -}; - - -/** - * @param {ol.Rectangle} rectangle Rectangle. - */ -ol.Rectangle.prototype.extend = function(rectangle) { - if (rectangle.minX < this.minX) { - this.minX = rectangle.minX; - } - if (rectangle.minY < this.minY) { - this.minY = rectangle.minY; - } - if (rectangle.maxX > this.maxX) { - this.maxX = rectangle.maxX; - } - if (rectangle.maxY > this.maxY) { - this.maxY = rectangle.maxY; - } -}; - - -/** - * @param {number} x X. - * @param {number} y Y. - */ -ol.Rectangle.prototype.extendXY = function(x, y) { - if (x < this.minX) { - this.minX = x; - } - if (y < this.minY) { - this.minY = y; - } - if (x > this.maxX) { - this.maxX = x; - } - if (y > this.maxY) { - this.maxY = y; - } -}; - - -/** - * @return {ol.Coordinate} Center. - */ -ol.Rectangle.prototype.getCenter = function() { - return [(this.minX + this.maxX) / 2, (this.minY + this.maxY) / 2]; -}; - - -/** - * @return {number} Height. - */ -ol.Rectangle.prototype.getHeight = function() { - return this.maxY - this.minY; -}; - - -/** - * @return {ol.Size} Size. - */ -ol.Rectangle.prototype.getSize = function() { - return new ol.Size(this.getWidth(), this.getHeight()); -}; - - -/** - * @return {number} Width. - */ -ol.Rectangle.prototype.getWidth = function() { - return this.maxX - this.minX; -}; - - -/** - * @param {ol.Rectangle} rectangle Rectangle. - * @return {boolean} Intersects. - */ -ol.Rectangle.prototype.intersects = function(rectangle) { - return this.minX <= rectangle.maxX && - this.maxX >= rectangle.minX && - this.minY <= rectangle.maxY && - this.maxY >= rectangle.minY; -}; - - -/** - * @return {boolean} Is empty. - */ -ol.Rectangle.prototype.isEmpty = function() { - return this.maxX < this.minX || this.maxY < this.minY; -}; - - -/** - * @param {ol.Coordinate} coordinate Coordinate. - * @return {ol.Coordinate} Coordinate. - */ -ol.Rectangle.prototype.normalize = function(coordinate) { - return [ - (coordinate[0] - this.minX) / this.getWidth(), - (coordinate[1] - this.minY) / this.getHeight() - ]; -}; - - -/** - * @return {string} String. - */ -ol.Rectangle.prototype.toString = function() { - return '(' + [this.minX, this.minY, this.maxX, this.maxY].join(', ') + ')'; -}; - - -/** - * @param {number} value Value. - */ -ol.Rectangle.prototype.scaleFromCenter = function(value) { - var deltaX = (this.getWidth() / 2.0) * (value - 1); - var deltaY = (this.getHeight() / 2.0) * (value - 1); - this.minX -= deltaX; - this.minY -= deltaY; - this.maxX += deltaX; - this.maxY += deltaY; -}; diff --git a/test/spec/ol/extent.test.js b/test/spec/ol/extent.test.js index bb2e8c745b..078f19a645 100644 --- a/test/spec/ol/extent.test.js +++ b/test/spec/ol/extent.test.js @@ -1,129 +1,243 @@ -goog.provide('ol.test.Extent'); +goog.provide('ol.test.extent'); -describe('ol.Extent', function() { + +describe('ol.extent', function() { describe('containsCoordinate', function() { describe('positive', function() { it('returns true', function() { - var extent = new ol.Extent(1, 2, 3, 4); - expect(extent.containsCoordinate([1, 2])).to.be.ok(); - expect(extent.containsCoordinate([1, 3])).to.be.ok(); - expect(extent.containsCoordinate([1, 4])).to.be.ok(); - expect(extent.containsCoordinate([2, 2])).to.be.ok(); - expect(extent.containsCoordinate([2, 3])).to.be.ok(); - expect(extent.containsCoordinate([2, 4])).to.be.ok(); - expect(extent.containsCoordinate([3, 2])).to.be.ok(); - expect(extent.containsCoordinate([3, 3])).to.be.ok(); - expect(extent.containsCoordinate([3, 4])).to.be.ok(); + var extent = [1, 3, 2, 4]; + expect(ol.extent.containsCoordinate(extent, [1, 2])).to.be.ok(); + expect(ol.extent.containsCoordinate(extent, [1, 3])).to.be.ok(); + expect(ol.extent.containsCoordinate(extent, [1, 4])).to.be.ok(); + expect(ol.extent.containsCoordinate(extent, [2, 2])).to.be.ok(); + expect(ol.extent.containsCoordinate(extent, [2, 3])).to.be.ok(); + expect(ol.extent.containsCoordinate(extent, [2, 4])).to.be.ok(); + expect(ol.extent.containsCoordinate(extent, [3, 2])).to.be.ok(); + expect(ol.extent.containsCoordinate(extent, [3, 3])).to.be.ok(); + expect(ol.extent.containsCoordinate(extent, [3, 4])).to.be.ok(); }); }); describe('negative', function() { it('returns false', function() { - var extent = new ol.Extent(1, 2, 3, 4); - expect(extent.containsCoordinate([0, 1])).to.not.be(); - expect(extent.containsCoordinate([0, 2])).to.not.be(); - expect(extent.containsCoordinate([0, 3])).to.not.be(); - expect(extent.containsCoordinate([0, 4])).to.not.be(); - expect(extent.containsCoordinate([0, 5])).to.not.be(); - expect(extent.containsCoordinate([1, 1])).to.not.be(); - expect(extent.containsCoordinate([1, 5])).to.not.be(); - expect(extent.containsCoordinate([2, 1])).to.not.be(); - expect(extent.containsCoordinate([2, 5])).to.not.be(); - expect(extent.containsCoordinate([3, 1])).to.not.be(); - expect(extent.containsCoordinate([3, 5])).to.not.be(); - expect(extent.containsCoordinate([4, 1])).to.not.be(); - expect(extent.containsCoordinate([4, 2])).to.not.be(); - expect(extent.containsCoordinate([4, 3])).to.not.be(); - expect(extent.containsCoordinate([4, 4])).to.not.be(); - expect(extent.containsCoordinate([4, 5])).to.not.be(); + var extent = [1, 3, 2, 4]; + expect(ol.extent.containsCoordinate(extent, [0, 1])).to.not.be(); + expect(ol.extent.containsCoordinate(extent, [0, 2])).to.not.be(); + expect(ol.extent.containsCoordinate(extent, [0, 3])).to.not.be(); + expect(ol.extent.containsCoordinate(extent, [0, 4])).to.not.be(); + expect(ol.extent.containsCoordinate(extent, [0, 5])).to.not.be(); + expect(ol.extent.containsCoordinate(extent, [1, 1])).to.not.be(); + expect(ol.extent.containsCoordinate(extent, [1, 5])).to.not.be(); + expect(ol.extent.containsCoordinate(extent, [2, 1])).to.not.be(); + expect(ol.extent.containsCoordinate(extent, [2, 5])).to.not.be(); + expect(ol.extent.containsCoordinate(extent, [3, 1])).to.not.be(); + expect(ol.extent.containsCoordinate(extent, [3, 5])).to.not.be(); + expect(ol.extent.containsCoordinate(extent, [4, 1])).to.not.be(); + expect(ol.extent.containsCoordinate(extent, [4, 2])).to.not.be(); + expect(ol.extent.containsCoordinate(extent, [4, 3])).to.not.be(); + expect(ol.extent.containsCoordinate(extent, [4, 4])).to.not.be(); + expect(ol.extent.containsCoordinate(extent, [4, 5])).to.not.be(); }); }); }); + describe('getCenter', function() { + it('returns the expected center', function() { + var extent = [1, 3, 2, 4]; + var center = ol.extent.getCenter(extent); + expect(center[0]).to.eql(2); + expect(center[1]).to.eql(3); + }); + }); + + describe('getForView2DAndSize', function() { + + it('works for a unit square', function() { + var extent = ol.extent.getForView2DAndSize( + [0, 0], 1, 0, new ol.Size(1, 1)); + expect(extent[0]).to.be(-0.5); + expect(extent[1]).to.be(0.5); + expect(extent[2]).to.be(-0.5); + expect(extent[3]).to.be(0.5); + }); + + it('works for center', function() { + var extent = ol.extent.getForView2DAndSize( + [5, 10], 1, 0, new ol.Size(1, 1)); + expect(extent[0]).to.be(4.5); + expect(extent[1]).to.be(5.5); + expect(extent[2]).to.be(9.5); + expect(extent[3]).to.be(10.5); + }); + + it('works for rotation', function() { + var extent = ol.extent.getForView2DAndSize( + [0, 0], 1, Math.PI / 4, new ol.Size(1, 1)); + expect(extent[0]).to.roughlyEqual(-Math.sqrt(0.5), 1e-9); + expect(extent[1]).to.roughlyEqual(Math.sqrt(0.5), 1e-9); + expect(extent[2]).to.roughlyEqual(-Math.sqrt(0.5), 1e-9); + expect(extent[3]).to.roughlyEqual(Math.sqrt(0.5), 1e-9); + }); + + it('works for resolution', function() { + var extent = ol.extent.getForView2DAndSize( + [0, 0], 2, 0, new ol.Size(1, 1)); + expect(extent[0]).to.be(-1); + expect(extent[1]).to.be(1); + expect(extent[2]).to.be(-1); + expect(extent[3]).to.be(1); + }); + + it('works for size', function() { + var extent = ol.extent.getForView2DAndSize( + [0, 0], 1, 0, new ol.Size(10, 5)); + expect(extent[0]).to.be(-5); + expect(extent[1]).to.be(5); + expect(extent[2]).to.be(-2.5); + expect(extent[3]).to.be(2.5); + }); + + }); + + describe('getSize', function() { + it('returns the expected size', function() { + var extent = [0, 2, 1, 4]; + var size = ol.extent.getSize(extent); + expect(size.width).to.eql(2); + expect(size.height).to.eql(3); + }); + }); + + describe('intersect', function() { + + var extent1; + + beforeEach(function() { + extent1 = [50, 100, 50, 100]; + }); + + it('returns the expected value', function() { + expect(ol.extent.intersects(extent1, extent1)).to.be(true); + expect(ol.extent.intersects(extent1, [20, 80, 20, 80])).to.be(true); + expect(ol.extent.intersects(extent1, [20, 80, 50, 100])).to.be(true); + expect(ol.extent.intersects(extent1, [20, 80, 80, 120])).to.be(true); + expect(ol.extent.intersects(extent1, [50, 100, 20, 80])).to.be(true); + expect(ol.extent.intersects(extent1, [50, 100, 80, 120])).to.be(true); + expect(ol.extent.intersects(extent1, [80, 120, 20, 80])).to.be(true); + expect(ol.extent.intersects(extent1, [80, 120, 50, 100])).to.be(true); + expect(ol.extent.intersects(extent1, [80, 120, 80, 120])).to.be(true); + expect(ol.extent.intersects(extent1, [20, 120, 20, 120])).to.be(true); + expect(ol.extent.intersects(extent1, [70, 80, 70, 80])).to.be(true); + expect(ol.extent.intersects(extent1, [10, 30, 10, 30])).to.be(false); + expect(ol.extent.intersects(extent1, [30, 70, 10, 30])).to.be(false); + expect(ol.extent.intersects(extent1, [50, 100, 10, 30])).to.be(false); + expect(ol.extent.intersects(extent1, [80, 120, 10, 30])).to.be(false); + expect(ol.extent.intersects(extent1, [120, 140, 10, 30])).to.be(false); + expect(ol.extent.intersects(extent1, [10, 30, 30, 70])).to.be(false); + expect(ol.extent.intersects(extent1, [120, 140, 30, 70])).to.be(false); + expect(ol.extent.intersects(extent1, [10, 30, 50, 100])).to.be(false); + expect(ol.extent.intersects(extent1, [120, 140, 50, 100])).to.be(false); + expect(ol.extent.intersects(extent1, [10, 30, 80, 120])).to.be(false); + expect(ol.extent.intersects(extent1, [120, 140, 80, 120])).to.be(false); + expect(ol.extent.intersects(extent1, [10, 30, 120, 140])).to.be(false); + expect(ol.extent.intersects(extent1, [30, 70, 120, 140])).to.be(false); + expect(ol.extent.intersects(extent1, [50, 100, 120, 140])).to.be(false); + expect(ol.extent.intersects(extent1, [80, 120, 120, 140])).to.be(false); + expect(ol.extent.intersects(extent1, [120, 140, 120, 140])).to.be(false); + }); + }); + + describe('normalize', function() { + it('returns the expected coordinate', function() { + var extent = [0, 2, 1, 3]; + var coordinate; + + coordinate = ol.extent.normalize(extent, [1, 2]); + expect(coordinate[0]).to.eql(0.5); + expect(coordinate[1]).to.eql(0.5); + + coordinate = ol.extent.normalize(extent, [0, 3]); + expect(coordinate[0]).to.eql(0); + expect(coordinate[1]).to.eql(1); + + coordinate = ol.extent.normalize(extent, [2, 1]); + expect(coordinate[0]).to.eql(1); + expect(coordinate[1]).to.eql(0); + + coordinate = ol.extent.normalize(extent, [0, 0]); + expect(coordinate[0]).to.eql(0); + expect(coordinate[1]).to.eql(-0.5); + + coordinate = ol.extent.normalize(extent, [-1, 1]); + expect(coordinate[0]).to.eql(-0.5); + expect(coordinate[1]).to.eql(0); + }); + }); + + describe('scaleFromCenter', function() { + it('scales the extent from its center', function() { + var extent = [1, 3, 1, 3]; + ol.extent.scaleFromCenter(extent, 2); + expect(extent[0]).to.eql(0); + expect(extent[1]).to.eql(4); + expect(extent[2]).to.eql(0); + expect(extent[3]).to.eql(4); + }); + }); + + describe('toString', function() { + it('returns the expected string', function() { + var extent = [0, 2, 1, 3]; + expect(ol.extent.toString(extent)).to.eql('(0, 2, 1, 3)'); + }); + }); + describe('transform', function() { it('does transform', function() { var transformFn = ol.projection.getTransform('EPSG:4326', 'EPSG:3857'); - var sourceExtent = new ol.Extent(-15, -30, 45, 60); - var destinationExtent = sourceExtent.transform(transformFn); + var sourceExtent = [-15, 45, -30, 60]; + var destinationExtent = ol.extent.transform(sourceExtent, transformFn); expect(destinationExtent).not.to.be(undefined); expect(destinationExtent).not.to.be(null); // FIXME check values with third-party tool - expect(destinationExtent.minX).to.roughlyEqual(-1669792.3618991037, 1e-9); - expect(destinationExtent.minY).to.roughlyEqual(-3503549.843504376, 1e-8); - expect(destinationExtent.maxX).to.roughlyEqual(5009377.085697311, 1e-9); - expect(destinationExtent.maxY).to.roughlyEqual(8399737.889818361, 1e-9); + expect(destinationExtent[0]).to.roughlyEqual(-1669792.3618991037, 1e-9); + expect(destinationExtent[1]).to.roughlyEqual(5009377.085697311, 1e-9); + expect(destinationExtent[2]).to.roughlyEqual(-3503549.843504376, 1e-8); + expect(destinationExtent[3]).to.roughlyEqual(8399737.889818361, 1e-9); }); it('takes arbitrary function', function() { - var transformFn = function(input) { - return [-input[0], -input[1], -input[2], -input[3]]; + var transformFn = function(input, output, opt_dimension) { + var dimension = goog.isDef(opt_dimension) ? opt_dimension : 2; + if (!goog.isDef(output)) { + output = new Array(input.length); + } + var n = input.length; + var i; + for (i = 0; i < n; i += dimension) { + output[i] = -input[i]; + output[i + 1] = -input[i + 1]; + } + return output; }; - var sourceExtent = new ol.Extent(-15, -30, 45, 60); - var destinationExtent = sourceExtent.transform(transformFn); + var sourceExtent = [-15, 45, -30, 60]; + var destinationExtent = ol.extent.transform(sourceExtent, transformFn); expect(destinationExtent).not.to.be(undefined); expect(destinationExtent).not.to.be(null); - expect(destinationExtent.minX).to.be(-45); - expect(destinationExtent.minY).to.be(-60); - expect(destinationExtent.maxX).to.be(15); - expect(destinationExtent.maxY).to.be(30); - }); - - }); - - describe('getForView2DAndSize', function() { - - it('works for a unit square', function() { - var extent = ol.Extent.getForView2DAndSize( - [0, 0], 1, 0, new ol.Size(1, 1)); - expect(extent.minX).to.be(-0.5); - expect(extent.minY).to.be(-0.5); - expect(extent.maxX).to.be(0.5); - expect(extent.maxY).to.be(0.5); - }); - - it('works for center', function() { - var extent = ol.Extent.getForView2DAndSize( - [5, 10], 1, 0, new ol.Size(1, 1)); - expect(extent.minX).to.be(4.5); - expect(extent.minY).to.be(9.5); - expect(extent.maxX).to.be(5.5); - expect(extent.maxY).to.be(10.5); - }); - - it('works for rotation', function() { - var extent = ol.Extent.getForView2DAndSize( - [0, 0], 1, Math.PI / 4, new ol.Size(1, 1)); - expect(extent.minX).to.roughlyEqual(-Math.sqrt(0.5), 1e-9); - expect(extent.minY).to.roughlyEqual(-Math.sqrt(0.5), 1e-9); - expect(extent.maxX).to.roughlyEqual(Math.sqrt(0.5), 1e-9); - expect(extent.maxY).to.roughlyEqual(Math.sqrt(0.5), 1e-9); - }); - - it('works for resolution', function() { - var extent = ol.Extent.getForView2DAndSize( - [0, 0], 2, 0, new ol.Size(1, 1)); - expect(extent.minX).to.be(-1); - expect(extent.minY).to.be(-1); - expect(extent.maxX).to.be(1); - expect(extent.maxY).to.be(1); - }); - - it('works for size', function() { - var extent = ol.Extent.getForView2DAndSize( - [0, 0], 1, 0, new ol.Size(10, 5)); - expect(extent.minX).to.be(-5); - expect(extent.minY).to.be(-2.5); - expect(extent.maxX).to.be(5); - expect(extent.maxY).to.be(2.5); + expect(destinationExtent[0]).to.be(-45); + expect(destinationExtent[1]).to.be(15); + expect(destinationExtent[2]).to.be(-60); + expect(destinationExtent[3]).to.be(30); }); }); }); -goog.require('ol.Extent'); + goog.require('ol.Size'); +goog.require('ol.extent'); goog.require('ol.projection'); diff --git a/test/spec/ol/rectangle.test.js b/test/spec/ol/rectangle.test.js deleted file mode 100644 index 04bae55722..0000000000 --- a/test/spec/ol/rectangle.test.js +++ /dev/null @@ -1,125 +0,0 @@ -goog.provide('ol.test.Rectangle'); - -describe('ol.Rectangle', function() { - - describe('getCenter', function() { - it('returns the expected center', function() { - var rectangle = new ol.Rectangle(1, 2, 3, 4); - var center = rectangle.getCenter(); - expect(center[0]).to.eql(2); - expect(center[1]).to.eql(3); - }); - }); - - describe('intersect', function() { - - var rectangle1; - - beforeEach(function() { - rectangle1 = new ol.Rectangle(50, 50, 100, 100); - }); - - it('returns the expected value', function() { - expect(rectangle1).to.intersect(rectangle1); - expect(rectangle1).to.intersect(new ol.Rectangle(20, 20, 80, 80)); - expect(rectangle1).to.intersect(new ol.Rectangle(20, 50, 80, 100)); - expect(rectangle1).to.intersect(new ol.Rectangle(20, 80, 80, 120)); - expect(rectangle1).to.intersect(new ol.Rectangle(50, 20, 100, 80)); - expect(rectangle1).to.intersect(new ol.Rectangle(50, 80, 100, 120)); - expect(rectangle1).to.intersect(new ol.Rectangle(80, 20, 120, 80)); - expect(rectangle1).to.intersect(new ol.Rectangle(80, 50, 120, 100)); - expect(rectangle1).to.intersect(new ol.Rectangle(80, 80, 120, 120)); - expect(rectangle1).to.intersect(new ol.Rectangle(20, 20, 120, 120)); - expect(rectangle1).to.intersect(new ol.Rectangle(70, 70, 80, 80)); - expect(rectangle1).to.not.intersect( - new ol.Rectangle(10, 10, 30, 30)); - expect(rectangle1).to.not.intersect( - new ol.Rectangle(30, 10, 70, 30)); - expect(rectangle1).to.not.intersect( - new ol.Rectangle(50, 10, 100, 30)); - expect(rectangle1).to.not.intersect( - new ol.Rectangle(80, 10, 120, 30)); - expect(rectangle1).to.not.intersect( - new ol.Rectangle(120, 10, 140, 30)); - expect(rectangle1).to.not.intersect( - new ol.Rectangle(10, 30, 30, 70)); - expect(rectangle1).to.not.intersect( - new ol.Rectangle(120, 30, 140, 70)); - expect(rectangle1).to.not.intersect( - new ol.Rectangle(10, 50, 30, 100)); - expect(rectangle1).to.not.intersect( - new ol.Rectangle(120, 50, 140, 100)); - expect(rectangle1).to.not.intersect( - new ol.Rectangle(10, 80, 30, 120)); - expect(rectangle1).to.not.intersect( - new ol.Rectangle(120, 80, 140, 120)); - expect(rectangle1).to.not.intersect( - new ol.Rectangle(10, 120, 30, 140)); - expect(rectangle1).to.not.intersect( - new ol.Rectangle(30, 120, 70, 140)); - expect(rectangle1).to.not.intersect( - new ol.Rectangle(50, 120, 100, 140)); - expect(rectangle1).to.not.intersect( - new ol.Rectangle(80, 120, 120, 140)); - expect(rectangle1).to.not.intersect( - new ol.Rectangle(120, 120, 140, 140)); - }); - }); - - describe('getSize', function() { - it('returns the expected size', function() { - var rectangle = new ol.Rectangle(0, 1, 2, 4); - var size = rectangle.getSize(); - expect(size.width).to.eql(2); - expect(size.height).to.eql(3); - }); - }); - - describe('normalize', function() { - it('returns the expected coordinate', function() { - var rectangle = new ol.Rectangle(0, 1, 2, 3); - var coordinate; - - coordinate = rectangle.normalize([1, 2]); - expect(coordinate[0]).to.eql(0.5); - expect(coordinate[1]).to.eql(0.5); - - coordinate = rectangle.normalize([0, 3]); - expect(coordinate[0]).to.eql(0); - expect(coordinate[1]).to.eql(1); - - coordinate = rectangle.normalize([2, 1]); - expect(coordinate[0]).to.eql(1); - expect(coordinate[1]).to.eql(0); - - coordinate = rectangle.normalize([0, 0]); - expect(coordinate[0]).to.eql(0); - expect(coordinate[1]).to.eql(-0.5); - - coordinate = rectangle.normalize([-1, 1]); - expect(coordinate[0]).to.eql(-0.5); - expect(coordinate[1]).to.eql(0); - }); - }); - - describe('toString', function() { - it('returns the expected string', function() { - var rectangle = new ol.Rectangle(0, 1, 2, 3); - expect(rectangle.toString()).to.eql('(0, 1, 2, 3)'); - }); - }); - - describe('scaleFromCenter', function() { - it('scales the extent from its center', function() { - var rectangle = new ol.Rectangle(1, 1, 3, 3); - rectangle.scaleFromCenter(2); - expect(rectangle.minX).to.eql(0); - expect(rectangle.minY).to.eql(0); - expect(rectangle.maxX).to.eql(4); - expect(rectangle.maxY).to.eql(4); - }); - }); - -}); - -goog.require('ol.Rectangle');