From 76779c175f15f8230ce7bbaccb0bc0460e568d66 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 12 Mar 2014 11:09:23 +0100 Subject: [PATCH 01/17] Use goog.math.lerp in ol.geom.flat.lineStringInterpolate --- src/ol/geom/flatgeom.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index c463be099f..25e281c1a8 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -288,9 +288,10 @@ ol.geom.flat.lineStringInterpolate = var t = (target - cumulativeLengths[-index - 2]) / (cumulativeLengths[-index - 1] - cumulativeLengths[-index - 2]); var o = offset + (-index - 2) * stride; - pointX = (1 - t) * flatCoordinates[o] + t * flatCoordinates[o + stride]; - pointY = (1 - t) * flatCoordinates[o + 1] + - t * flatCoordinates[o + stride + 1]; + pointX = goog.math.lerp( + flatCoordinates[o], flatCoordinates[o + stride], t); + pointY = goog.math.lerp( + flatCoordinates[o + 1], flatCoordinates[o + stride + 1], t); } else { pointX = flatCoordinates[offset + index * stride]; pointY = flatCoordinates[offset + index * stride + 1]; From b565092d624e9bc8aead5635ae665ebb49d701e1 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 12 Mar 2014 11:09:35 +0100 Subject: [PATCH 02/17] Use goog.math.lerp in ol.geom.flat.lineStringCoordinateAtM --- src/ol/geom/flatgeom.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index 25e281c1a8..5041bdf223 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -363,8 +363,8 @@ ol.geom.flat.lineStringCoordinateAtM = coordinate = []; var i; for (i = 0; i < stride - 1; ++i) { - coordinate.push((1 - t) * flatCoordinates[(lo - 1) * stride + i] + - t * flatCoordinates[lo * stride + i]); + coordinate.push(goog.math.lerp(flatCoordinates[(lo - 1) * stride + i], + flatCoordinates[lo * stride + i], t)); } coordinate.push(m); goog.asserts.assert(coordinate.length == stride); From 2b6845244cdbbe29983f51c89f24726c771adcbc Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 12 Mar 2014 11:17:39 +0100 Subject: [PATCH 03/17] Factor out ol.geom.flat.closest --- .../closestflatgeom.js} | 77 +++++++++++++++---- src/ol/geom/flatgeom.js | 43 ----------- src/ol/geom/linearring.js | 6 +- src/ol/geom/linestring.js | 6 +- src/ol/geom/multilinestring.js | 6 +- src/ol/geom/multipolygon.js | 6 +- src/ol/geom/polygon.js | 6 +- .../closestflatgeom.test.js} | 41 +++++----- 8 files changed, 96 insertions(+), 95 deletions(-) rename src/ol/geom/{closestgeom.js => flat/closestflatgeom.js} (76%) rename test/spec/ol/geom/{closestgeom.test.js => flat/closestflatgeom.test.js} (80%) diff --git a/src/ol/geom/closestgeom.js b/src/ol/geom/flat/closestflatgeom.js similarity index 76% rename from src/ol/geom/closestgeom.js rename to src/ol/geom/flat/closestflatgeom.js index 0084a0d6f6..12691ab88f 100644 --- a/src/ol/geom/closestgeom.js +++ b/src/ol/geom/flat/closestflatgeom.js @@ -1,11 +1,53 @@ -// FIXME find better names for these functions - -goog.provide('ol.geom.closest'); +goog.provide('ol.geom.flat.closest'); goog.require('goog.asserts'); +goog.require('goog.math'); goog.require('ol.geom.flat'); +/** + * Returns the point on the 2D line segment flatCoordinates[offset1] to + * flatCoordinates[offset2] that is closest to the point (x, y). Extra + * dimensions are linearly interpolated. + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset1 Offset 1. + * @param {number} offset2 Offset 2. + * @param {number} stride Stride. + * @param {number} x X. + * @param {number} y Y. + * @param {Array.} closestPoint Closest point. + */ +ol.geom.flat.closest.point = + function(flatCoordinates, offset1, offset2, stride, x, y, closestPoint) { + var x1 = flatCoordinates[offset1]; + var y1 = flatCoordinates[offset1 + 1]; + var dx = flatCoordinates[offset2] - x1; + var dy = flatCoordinates[offset2 + 1] - y1; + var i, offset; + if (dx === 0 && dy === 0) { + offset = offset1; + } else { + var t = ((x - x1) * dx + (y - y1) * dy) / (dx * dx + dy * dy); + if (t > 1) { + offset = offset2; + } else if (t > 0) { + for (i = 0; i < stride; ++i) { + closestPoint[i] = goog.math.lerp(flatCoordinates[offset1 + i], + flatCoordinates[offset2 + i], t); + } + closestPoint.length = stride; + return; + } else { + offset = offset1; + } + } + for (i = 0; i < stride; ++i) { + closestPoint[i] = flatCoordinates[offset + i]; + } + closestPoint.length = stride; +}; + + /** * Return the squared of the largest distance between any pair of consecutive * coordinates. @@ -16,7 +58,7 @@ goog.require('ol.geom.flat'); * @param {number} maxSquaredDelta Max squared delta. * @return {number} Max squared delta. */ -ol.geom.closest.getMaxSquaredDelta = +ol.geom.flat.closest.getMaxSquaredDelta = function(flatCoordinates, offset, end, stride, maxSquaredDelta) { var x1 = flatCoordinates[offset]; var y1 = flatCoordinates[offset + 1]; @@ -42,12 +84,12 @@ ol.geom.closest.getMaxSquaredDelta = * @param {number} maxSquaredDelta Max squared delta. * @return {number} Max squared delta. */ -ol.geom.closest.getsMaxSquaredDelta = +ol.geom.flat.closest.getsMaxSquaredDelta = function(flatCoordinates, offset, ends, stride, maxSquaredDelta) { var i, ii; for (i = 0, ii = ends.length; i < ii; ++i) { var end = ends[i]; - maxSquaredDelta = ol.geom.closest.getMaxSquaredDelta( + maxSquaredDelta = ol.geom.flat.closest.getMaxSquaredDelta( flatCoordinates, offset, end, stride, maxSquaredDelta); offset = end; } @@ -63,12 +105,12 @@ ol.geom.closest.getsMaxSquaredDelta = * @param {number} maxSquaredDelta Max squared delta. * @return {number} Max squared delta. */ -ol.geom.closest.getssMaxSquaredDelta = +ol.geom.flat.closest.getssMaxSquaredDelta = function(flatCoordinates, offset, endss, stride, maxSquaredDelta) { var i, ii; for (i = 0, ii = endss.length; i < ii; ++i) { var ends = endss[i]; - maxSquaredDelta = ol.geom.closest.getsMaxSquaredDelta( + maxSquaredDelta = ol.geom.flat.closest.getsMaxSquaredDelta( flatCoordinates, offset, ends, stride, maxSquaredDelta); offset = ends[ends.length - 1]; } @@ -90,8 +132,9 @@ ol.geom.closest.getssMaxSquaredDelta = * @param {Array.=} opt_tmpPoint Temporary point object. * @return {number} Minimum squared distance. */ -ol.geom.closest.getClosestPoint = function(flatCoordinates, offset, end, stride, - maxDelta, isRing, x, y, closestPoint, minSquaredDistance, opt_tmpPoint) { +ol.geom.flat.closest.getClosestPoint = function(flatCoordinates, offset, end, + stride, maxDelta, isRing, x, y, closestPoint, minSquaredDistance, + opt_tmpPoint) { if (offset == end) { return minSquaredDistance; } @@ -114,7 +157,7 @@ ol.geom.closest.getClosestPoint = function(flatCoordinates, offset, end, stride, var tmpPoint = goog.isDef(opt_tmpPoint) ? opt_tmpPoint : [NaN, NaN]; var index = offset + stride; while (index < end) { - ol.geom.flat.closestPoint( + ol.geom.flat.closest.point( flatCoordinates, index - stride, index, stride, x, y, tmpPoint); squaredDistance = ol.geom.flat.squaredDistance( x, y, tmpPoint[0], tmpPoint[1]); @@ -143,7 +186,7 @@ ol.geom.closest.getClosestPoint = function(flatCoordinates, offset, end, stride, } if (isRing) { // Check the closing segment. - ol.geom.flat.closestPoint( + ol.geom.flat.closest.point( flatCoordinates, end - stride, offset, stride, x, y, tmpPoint); squaredDistance = ol.geom.flat.squaredDistance( x, y, tmpPoint[0], tmpPoint[1]); @@ -173,14 +216,14 @@ ol.geom.closest.getClosestPoint = function(flatCoordinates, offset, end, stride, * @param {Array.=} opt_tmpPoint Temporary point object. * @return {number} Minimum squared distance. */ -ol.geom.closest.getsClosestPoint = function(flatCoordinates, offset, ends, +ol.geom.flat.closest.getsClosestPoint = function(flatCoordinates, offset, ends, stride, maxDelta, isRing, x, y, closestPoint, minSquaredDistance, opt_tmpPoint) { var tmpPoint = goog.isDef(opt_tmpPoint) ? opt_tmpPoint : [NaN, NaN]; var i, ii; for (i = 0, ii = ends.length; i < ii; ++i) { var end = ends[i]; - minSquaredDistance = ol.geom.closest.getClosestPoint( + minSquaredDistance = ol.geom.flat.closest.getClosestPoint( flatCoordinates, offset, end, stride, maxDelta, isRing, x, y, closestPoint, minSquaredDistance, tmpPoint); offset = end; @@ -203,14 +246,14 @@ ol.geom.closest.getsClosestPoint = function(flatCoordinates, offset, ends, * @param {Array.=} opt_tmpPoint Temporary point object. * @return {number} Minimum squared distance. */ -ol.geom.closest.getssClosestPoint = function(flatCoordinates, offset, endss, - stride, maxDelta, isRing, x, y, closestPoint, minSquaredDistance, +ol.geom.flat.closest.getssClosestPoint = function(flatCoordinates, offset, + endss, stride, maxDelta, isRing, x, y, closestPoint, minSquaredDistance, opt_tmpPoint) { var tmpPoint = goog.isDef(opt_tmpPoint) ? opt_tmpPoint : [NaN, NaN]; var i, ii; for (i = 0, ii = endss.length; i < ii; ++i) { var ends = endss[i]; - minSquaredDistance = ol.geom.closest.getsClosestPoint( + minSquaredDistance = ol.geom.flat.closest.getsClosestPoint( flatCoordinates, offset, ends, stride, maxDelta, isRing, x, y, closestPoint, minSquaredDistance, tmpPoint); offset = ends[ends.length - 1]; diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index 5041bdf223..9ba82972fd 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -7,49 +7,6 @@ goog.require('goog.vec.Mat4'); goog.require('ol.extent'); -/** - * Returns the point on the 2D line segment flatCoordinates[offset1] to - * flatCoordinates[offset2] that is closest to the point (x, y). Extra - * dimensions are linearly interpolated. - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset1 Offset 1. - * @param {number} offset2 Offset 2. - * @param {number} stride Stride. - * @param {number} x X. - * @param {number} y Y. - * @param {Array.} closestPoint Closest point. - */ -ol.geom.flat.closestPoint = - function(flatCoordinates, offset1, offset2, stride, x, y, closestPoint) { - var x1 = flatCoordinates[offset1]; - var y1 = flatCoordinates[offset1 + 1]; - var dx = flatCoordinates[offset2] - x1; - var dy = flatCoordinates[offset2 + 1] - y1; - var i, offset; - if (dx === 0 && dy === 0) { - offset = offset1; - } else { - var t = ((x - x1) * dx + (y - y1) * dy) / (dx * dx + dy * dy); - if (t > 1) { - offset = offset2; - } else if (t > 0) { - for (i = 0; i < stride; ++i) { - closestPoint[i] = goog.math.lerp(flatCoordinates[offset1 + i], - flatCoordinates[offset2 + i], t); - } - closestPoint.length = stride; - return; - } else { - offset = offset1; - } - } - for (i = 0; i < stride; ++i) { - closestPoint[i] = flatCoordinates[offset + i]; - } - closestPoint.length = stride; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. diff --git a/src/ol/geom/linearring.js b/src/ol/geom/linearring.js index 92d1ec19e1..71bde3380a 100644 --- a/src/ol/geom/linearring.js +++ b/src/ol/geom/linearring.js @@ -3,8 +3,8 @@ goog.provide('ol.geom.LinearRing'); goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); goog.require('ol.geom.SimpleGeometry'); -goog.require('ol.geom.closest'); goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.closest'); goog.require('ol.geom.simplify'); @@ -58,11 +58,11 @@ ol.geom.LinearRing.prototype.closestPointXY = return minSquaredDistance; } if (this.maxDeltaRevision_ != this.getRevision()) { - this.maxDelta_ = Math.sqrt(ol.geom.closest.getMaxSquaredDelta( + this.maxDelta_ = Math.sqrt(ol.geom.flat.closest.getMaxSquaredDelta( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride, 0)); this.maxDeltaRevision_ = this.getRevision(); } - return ol.geom.closest.getClosestPoint( + return ol.geom.flat.closest.getClosestPoint( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride, this.maxDelta_, true, x, y, closestPoint, minSquaredDistance); }; diff --git a/src/ol/geom/linestring.js b/src/ol/geom/linestring.js index 8ac6ec912e..ab3b55dfb0 100644 --- a/src/ol/geom/linestring.js +++ b/src/ol/geom/linestring.js @@ -5,8 +5,8 @@ goog.require('goog.asserts'); goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); goog.require('ol.geom.SimpleGeometry'); -goog.require('ol.geom.closest'); goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.closest'); goog.require('ol.geom.simplify'); @@ -86,11 +86,11 @@ ol.geom.LineString.prototype.closestPointXY = return minSquaredDistance; } if (this.maxDeltaRevision_ != this.getRevision()) { - this.maxDelta_ = Math.sqrt(ol.geom.closest.getMaxSquaredDelta( + this.maxDelta_ = Math.sqrt(ol.geom.flat.closest.getMaxSquaredDelta( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride, 0)); this.maxDeltaRevision_ = this.getRevision(); } - return ol.geom.closest.getClosestPoint( + return ol.geom.flat.closest.getClosestPoint( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride, this.maxDelta_, false, x, y, closestPoint, minSquaredDistance); }; diff --git a/src/ol/geom/multilinestring.js b/src/ol/geom/multilinestring.js index 816abef889..d859c34bae 100644 --- a/src/ol/geom/multilinestring.js +++ b/src/ol/geom/multilinestring.js @@ -6,8 +6,8 @@ goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); goog.require('ol.geom.LineString'); goog.require('ol.geom.SimpleGeometry'); -goog.require('ol.geom.closest'); goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.closest'); goog.require('ol.geom.simplify'); @@ -84,11 +84,11 @@ ol.geom.MultiLineString.prototype.closestPointXY = return minSquaredDistance; } if (this.maxDeltaRevision_ != this.getRevision()) { - this.maxDelta_ = Math.sqrt(ol.geom.closest.getsMaxSquaredDelta( + this.maxDelta_ = Math.sqrt(ol.geom.flat.closest.getsMaxSquaredDelta( this.flatCoordinates, 0, this.ends_, this.stride, 0)); this.maxDeltaRevision_ = this.getRevision(); } - return ol.geom.closest.getsClosestPoint( + return ol.geom.flat.closest.getsClosestPoint( this.flatCoordinates, 0, this.ends_, this.stride, this.maxDelta_, false, x, y, closestPoint, minSquaredDistance); }; diff --git a/src/ol/geom/multipolygon.js b/src/ol/geom/multipolygon.js index acd04669da..0e14f3798e 100644 --- a/src/ol/geom/multipolygon.js +++ b/src/ol/geom/multipolygon.js @@ -7,8 +7,8 @@ goog.require('ol.geom.GeometryType'); goog.require('ol.geom.MultiPoint'); goog.require('ol.geom.Polygon'); goog.require('ol.geom.SimpleGeometry'); -goog.require('ol.geom.closest'); goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.closest'); goog.require('ol.geom.simplify'); @@ -118,11 +118,11 @@ ol.geom.MultiPolygon.prototype.closestPointXY = return minSquaredDistance; } if (this.maxDeltaRevision_ != this.getRevision()) { - this.maxDelta_ = Math.sqrt(ol.geom.closest.getssMaxSquaredDelta( + this.maxDelta_ = Math.sqrt(ol.geom.flat.closest.getssMaxSquaredDelta( this.flatCoordinates, 0, this.endss_, this.stride, 0)); this.maxDeltaRevision_ = this.getRevision(); } - return ol.geom.closest.getssClosestPoint( + return ol.geom.flat.closest.getssClosestPoint( this.getOrientedFlatCoordinates(), 0, this.endss_, this.stride, this.maxDelta_, true, x, y, closestPoint, minSquaredDistance); }; diff --git a/src/ol/geom/polygon.js b/src/ol/geom/polygon.js index 1cd2d1fd4e..2fa6eebae5 100644 --- a/src/ol/geom/polygon.js +++ b/src/ol/geom/polygon.js @@ -7,8 +7,8 @@ goog.require('ol.geom.GeometryType'); goog.require('ol.geom.LinearRing'); goog.require('ol.geom.Point'); goog.require('ol.geom.SimpleGeometry'); -goog.require('ol.geom.closest'); goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.closest'); goog.require('ol.geom.simplify'); @@ -108,11 +108,11 @@ ol.geom.Polygon.prototype.closestPointXY = return minSquaredDistance; } if (this.maxDeltaRevision_ != this.getRevision()) { - this.maxDelta_ = Math.sqrt(ol.geom.closest.getsMaxSquaredDelta( + this.maxDelta_ = Math.sqrt(ol.geom.flat.closest.getsMaxSquaredDelta( this.flatCoordinates, 0, this.ends_, this.stride, 0)); this.maxDeltaRevision_ = this.getRevision(); } - return ol.geom.closest.getsClosestPoint( + return ol.geom.flat.closest.getsClosestPoint( this.flatCoordinates, 0, this.ends_, this.stride, this.maxDelta_, true, x, y, closestPoint, minSquaredDistance); }; diff --git a/test/spec/ol/geom/closestgeom.test.js b/test/spec/ol/geom/flat/closestflatgeom.test.js similarity index 80% rename from test/spec/ol/geom/closestgeom.test.js rename to test/spec/ol/geom/flat/closestflatgeom.test.js index 124b5afde6..d9b972b180 100644 --- a/test/spec/ol/geom/closestgeom.test.js +++ b/test/spec/ol/geom/flat/closestflatgeom.test.js @@ -1,41 +1,41 @@ -goog.provide('ol.test.geom.closest'); +goog.provide('ol.test.geom.flat.closest'); -describe('ol.geom.closest', function() { +describe('ol.geom.flat.closest', function() { describe('with simple data', function() { var flatCoordinates = [0, 0, 1, 0, 3, 0, 5, 0, 6, 0, 8, 0, 11, 0]; - describe('ol.geom.closest.getMaxSquaredDelta', function() { + describe('ol.geom.flat.closest.getMaxSquaredDelta', function() { it('returns the expected value in simple cases', function() { - expect(ol.geom.closest.getMaxSquaredDelta( + expect(ol.geom.flat.closest.getMaxSquaredDelta( flatCoordinates, 0, flatCoordinates.length, 2, 0)).to.be(9); }); }); - describe('ol.geom.closest.getClosestPoint', function() { + describe('ol.geom.flat.closest.getClosestPoint', function() { it('returns the expected value', function() { - var maxDelta = Math.sqrt(ol.geom.closest.getMaxSquaredDelta( + var maxDelta = Math.sqrt(ol.geom.flat.closest.getMaxSquaredDelta( flatCoordinates, 0, flatCoordinates.length, 2, 0)); expect(maxDelta).to.be(3); var closestPoint = [NaN, NaN]; - expect(ol.geom.closest.getClosestPoint( + expect(ol.geom.flat.closest.getClosestPoint( flatCoordinates, 0, flatCoordinates.length, 2, maxDelta, false, 0, 0, closestPoint, Infinity)).to.be(0); expect(closestPoint).to.eql([0, 0]); - expect(ol.geom.closest.getClosestPoint( + expect(ol.geom.flat.closest.getClosestPoint( flatCoordinates, 0, flatCoordinates.length, 2, maxDelta, false, 4, 1, closestPoint, Infinity)).to.be(1); expect(closestPoint).to.eql([4, 0]); - expect(ol.geom.closest.getClosestPoint( + expect(ol.geom.flat.closest.getClosestPoint( flatCoordinates, 0, flatCoordinates.length, 2, maxDelta, false, 5, 2, closestPoint, Infinity)).to.be(4); expect(closestPoint).to.eql([5, 0]); - expect(ol.geom.closest.getClosestPoint( + expect(ol.geom.flat.closest.getClosestPoint( flatCoordinates, 0, flatCoordinates.length, 2, maxDelta, false, 10, 100, closestPoint, Infinity)).to.be(10000); expect(closestPoint).to.eql([10, 0]); @@ -78,31 +78,31 @@ describe('ol.geom.closest', function() { describe('ol.geom.closet.maSquaredDelta', function() { it('returns the expected value', function() { - expect(ol.geom.closest.getMaxSquaredDelta( + expect(ol.geom.flat.closest.getMaxSquaredDelta( flatCoordinates, 0, flatCoordinates.length, 2, 0)). to.roughlyEqual(1389.1058, 1e-9); }); }); - describe('ol.geom.closest.getClosestPoint', function() { + describe('ol.geom.flat.closest.getClosestPoint', function() { it('returns the expected value', function() { - var maxDelta = Math.sqrt(ol.geom.closest.getMaxSquaredDelta( + var maxDelta = Math.sqrt(ol.geom.flat.closest.getMaxSquaredDelta( flatCoordinates, 0, flatCoordinates.length, 2, 0)); expect(maxDelta).to.roughlyEqual(Math.sqrt(1389.1058), 1e-9); var closestPoint = [NaN, NaN]; - expect(ol.geom.closest.getClosestPoint( + expect(ol.geom.flat.closest.getClosestPoint( flatCoordinates, 0, flatCoordinates.length, 2, maxDelta, false, 0, 0, closestPoint, Infinity)). to.roughlyEqual(110902.405, 1e-9); expect(closestPoint).to.eql([292.41, 159.37]); - expect(ol.geom.closest.getClosestPoint( + expect(ol.geom.flat.closest.getClosestPoint( flatCoordinates, 0, flatCoordinates.length, 2, maxDelta, false, 500, 500, closestPoint, Infinity)). to.roughlyEqual(106407.905, 1e-9); expect(closestPoint).to.eql([671.55, 222.55]); - expect(ol.geom.closest.getClosestPoint( + expect(ol.geom.flat.closest.getClosestPoint( flatCoordinates, 0, flatCoordinates.length, 2, maxDelta, false, 1000, 500, closestPoint, Infinity)). to.roughlyEqual(18229.4425, 1e-9); @@ -118,14 +118,14 @@ describe('ol.geom.closest', function() { var flatCoordinates = [0, 0, 10, -10, 2, 2, 30, -20]; var stride = 4; - describe('ol.geom.closest.getClosestPoint', function() { + describe('ol.geom.flat.closest.getClosestPoint', function() { it('interpolates M coordinates', function() { - var maxDelta = Math.sqrt(ol.geom.closest.getMaxSquaredDelta( + var maxDelta = Math.sqrt(ol.geom.flat.closest.getMaxSquaredDelta( flatCoordinates, 0, flatCoordinates.length, stride, 0)); expect(maxDelta).to.roughlyEqual(Math.sqrt(8), 1e-9); var closestPoint = [NaN, NaN]; - expect(ol.geom.closest.getClosestPoint( + expect(ol.geom.flat.closest.getClosestPoint( flatCoordinates, 0, flatCoordinates.length, stride, maxDelta, false, 1, 1, closestPoint, Infinity)). to.roughlyEqual(0, 1e-9); @@ -143,4 +143,5 @@ describe('ol.geom.closest', function() { }); -goog.require('ol.geom.closest'); +goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.closest'); From c8165a8168e67a2ac7684d510e8961816bfe579b Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 12 Mar 2014 11:21:55 +0100 Subject: [PATCH 04/17] Factor out ol.geom.flat.simplify --- .../simplifyflatgeom.js} | 46 +++++----- src/ol/geom/linearring.js | 4 +- src/ol/geom/linestring.js | 4 +- src/ol/geom/multilinestring.js | 4 +- src/ol/geom/multipolygon.js | 4 +- src/ol/geom/polygon.js | 4 +- src/ol/render/canvas/canvasreplay.js | 4 +- .../simplifyflatgeom.test.js} | 91 ++++++++++--------- 8 files changed, 81 insertions(+), 80 deletions(-) rename src/ol/geom/{simplifygeom.js => flat/simplifyflatgeom.js} (89%) rename test/spec/ol/geom/{simplifygeom.test.js => flat/simplifyflatgeom.test.js} (83%) diff --git a/src/ol/geom/simplifygeom.js b/src/ol/geom/flat/simplifyflatgeom.js similarity index 89% rename from src/ol/geom/simplifygeom.js rename to src/ol/geom/flat/simplifyflatgeom.js index 1e361ae3e5..fa6bce3485 100644 --- a/src/ol/geom/simplifygeom.js +++ b/src/ol/geom/flat/simplifyflatgeom.js @@ -24,7 +24,7 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. -goog.provide('ol.geom.simplify'); +goog.provide('ol.geom.flat.simplify'); goog.require('ol.geom.flat'); @@ -40,19 +40,19 @@ goog.require('ol.geom.flat'); * coordinates. * @return {Array.} Simplified line string. */ -ol.geom.simplify.lineString = function(flatCoordinates, offset, end, stride, - squaredTolerance, highQuality, opt_simplifiedFlatCoordinates) { +ol.geom.flat.simplify.lineString = function(flatCoordinates, offset, end, + stride, squaredTolerance, highQuality, opt_simplifiedFlatCoordinates) { var simplifiedFlatCoordinates = goog.isDef(opt_simplifiedFlatCoordinates) ? opt_simplifiedFlatCoordinates : []; if (!highQuality) { - end = ol.geom.simplify.radialDistance(flatCoordinates, offset, end, + end = ol.geom.flat.simplify.radialDistance(flatCoordinates, offset, end, stride, squaredTolerance, simplifiedFlatCoordinates, 0); flatCoordinates = simplifiedFlatCoordinates; offset = 0; stride = 2; } - simplifiedFlatCoordinates.length = ol.geom.simplify.douglasPeucker( + simplifiedFlatCoordinates.length = ol.geom.flat.simplify.douglasPeucker( flatCoordinates, offset, end, stride, squaredTolerance, simplifiedFlatCoordinates, 0); return simplifiedFlatCoordinates; @@ -70,7 +70,7 @@ ol.geom.simplify.lineString = function(flatCoordinates, offset, end, stride, * @param {number} simplifiedOffset Simplified offset. * @return {number} Simplified offset. */ -ol.geom.simplify.douglasPeucker = function(flatCoordinates, offset, end, +ol.geom.flat.simplify.douglasPeucker = function(flatCoordinates, offset, end, stride, squaredTolerance, simplifiedFlatCoordinates, simplifiedOffset) { var n = (end - offset) / stride; if (n < 3) { @@ -142,13 +142,13 @@ ol.geom.simplify.douglasPeucker = function(flatCoordinates, offset, end, * @param {Array.} simplifiedEnds Simplified ends. * @return {number} Simplified offset. */ -ol.geom.simplify.douglasPeuckers = function(flatCoordinates, offset, +ol.geom.flat.simplify.douglasPeuckers = function(flatCoordinates, offset, ends, stride, squaredTolerance, simplifiedFlatCoordinates, simplifiedOffset, simplifiedEnds) { var i, ii; for (i = 0, ii = ends.length; i < ii; ++i) { var end = ends[i]; - simplifiedOffset = ol.geom.simplify.douglasPeucker( + simplifiedOffset = ol.geom.flat.simplify.douglasPeucker( flatCoordinates, offset, end, stride, squaredTolerance, simplifiedFlatCoordinates, simplifiedOffset); simplifiedEnds.push(simplifiedOffset); @@ -170,14 +170,14 @@ ol.geom.simplify.douglasPeuckers = function(flatCoordinates, offset, * @param {Array.>} simplifiedEndss Simplified endss. * @return {number} Simplified offset. */ -ol.geom.simplify.douglasPeuckerss = function( +ol.geom.flat.simplify.douglasPeuckerss = function( flatCoordinates, offset, endss, stride, squaredTolerance, simplifiedFlatCoordinates, simplifiedOffset, simplifiedEndss) { var i, ii; for (i = 0, ii = endss.length; i < ii; ++i) { var ends = endss[i]; var simplifiedEnds = []; - simplifiedOffset = ol.geom.simplify.douglasPeuckers( + simplifiedOffset = ol.geom.flat.simplify.douglasPeuckers( flatCoordinates, offset, ends, stride, squaredTolerance, simplifiedFlatCoordinates, simplifiedOffset, simplifiedEnds); simplifiedEndss.push(simplifiedEnds); @@ -198,7 +198,7 @@ ol.geom.simplify.douglasPeuckerss = function( * @param {number} simplifiedOffset Simplified offset. * @return {number} Simplified offset. */ -ol.geom.simplify.radialDistance = function(flatCoordinates, offset, end, +ol.geom.flat.simplify.radialDistance = function(flatCoordinates, offset, end, stride, squaredTolerance, simplifiedFlatCoordinates, simplifiedOffset) { if (end <= offset + stride) { // zero or one point, no simplification possible, so copy and return @@ -241,7 +241,7 @@ ol.geom.simplify.radialDistance = function(flatCoordinates, offset, end, * @param {number} tolerance Squared tolerance. * @return {number} Rounded value. */ -ol.geom.simplify.snap = function(value, tolerance) { +ol.geom.flat.simplify.snap = function(value, tolerance) { return tolerance * Math.round(value / tolerance); }; @@ -265,15 +265,15 @@ ol.geom.simplify.snap = function(value, tolerance) { * @param {number} simplifiedOffset Simplified offset. * @return {number} Simplified offset. */ -ol.geom.simplify.quantize = function(flatCoordinates, offset, end, stride, +ol.geom.flat.simplify.quantize = function(flatCoordinates, offset, end, stride, tolerance, simplifiedFlatCoordinates, simplifiedOffset) { // do nothing if the line is empty if (offset == end) { return simplifiedOffset; } // snap the first coordinate (P1) - var x1 = ol.geom.simplify.snap(flatCoordinates[offset], tolerance); - var y1 = ol.geom.simplify.snap(flatCoordinates[offset + 1], tolerance); + var x1 = ol.geom.flat.simplify.snap(flatCoordinates[offset], tolerance); + var y1 = ol.geom.flat.simplify.snap(flatCoordinates[offset + 1], tolerance); offset += stride; // add the first coordinate to the output simplifiedFlatCoordinates[simplifiedOffset++] = x1; @@ -282,8 +282,8 @@ ol.geom.simplify.quantize = function(flatCoordinates, offset, end, stride, // coordinate (P2) var x2, y2; do { - x2 = ol.geom.simplify.snap(flatCoordinates[offset], tolerance); - y2 = ol.geom.simplify.snap(flatCoordinates[offset + 1], tolerance); + x2 = ol.geom.flat.simplify.snap(flatCoordinates[offset], tolerance); + y2 = ol.geom.flat.simplify.snap(flatCoordinates[offset + 1], tolerance); offset += stride; if (offset == end) { // all coordinates snap to the same value, the line collapses to a point @@ -298,8 +298,8 @@ ol.geom.simplify.quantize = function(flatCoordinates, offset, end, stride, while (offset < end) { var x3, y3; // snap the next coordinate (P3) - x3 = ol.geom.simplify.snap(flatCoordinates[offset], tolerance); - y3 = ol.geom.simplify.snap(flatCoordinates[offset + 1], tolerance); + x3 = ol.geom.flat.simplify.snap(flatCoordinates[offset], tolerance); + y3 = ol.geom.flat.simplify.snap(flatCoordinates[offset + 1], tolerance); offset += stride; // skip P3 if it is equal to P2 if (x3 == x2 && y3 == y2) { @@ -351,14 +351,14 @@ ol.geom.simplify.quantize = function(flatCoordinates, offset, end, stride, * @param {Array.} simplifiedEnds Simplified ends. * @return {number} Simplified offset. */ -ol.geom.simplify.quantizes = function( +ol.geom.flat.simplify.quantizes = function( flatCoordinates, offset, ends, stride, tolerance, simplifiedFlatCoordinates, simplifiedOffset, simplifiedEnds) { var i, ii; for (i = 0, ii = ends.length; i < ii; ++i) { var end = ends[i]; - simplifiedOffset = ol.geom.simplify.quantize( + simplifiedOffset = ol.geom.flat.simplify.quantize( flatCoordinates, offset, end, stride, tolerance, simplifiedFlatCoordinates, simplifiedOffset); @@ -381,7 +381,7 @@ ol.geom.simplify.quantizes = function( * @param {Array.>} simplifiedEndss Simplified endss. * @return {number} Simplified offset. */ -ol.geom.simplify.quantizess = function( +ol.geom.flat.simplify.quantizess = function( flatCoordinates, offset, endss, stride, tolerance, simplifiedFlatCoordinates, simplifiedOffset, simplifiedEndss) { @@ -389,7 +389,7 @@ ol.geom.simplify.quantizess = function( for (i = 0, ii = endss.length; i < ii; ++i) { var ends = endss[i]; var simplifiedEnds = []; - simplifiedOffset = ol.geom.simplify.quantizes( + simplifiedOffset = ol.geom.flat.simplify.quantizes( flatCoordinates, offset, ends, stride, tolerance, simplifiedFlatCoordinates, simplifiedOffset, simplifiedEnds); diff --git a/src/ol/geom/linearring.js b/src/ol/geom/linearring.js index 71bde3380a..19e7214b9e 100644 --- a/src/ol/geom/linearring.js +++ b/src/ol/geom/linearring.js @@ -5,7 +5,7 @@ goog.require('ol.geom.GeometryType'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.closest'); -goog.require('ol.geom.simplify'); +goog.require('ol.geom.flat.simplify'); @@ -94,7 +94,7 @@ ol.geom.LinearRing.prototype.getCoordinates = function() { ol.geom.LinearRing.prototype.getSimplifiedGeometryInternal = function(squaredTolerance) { var simplifiedFlatCoordinates = []; - simplifiedFlatCoordinates.length = ol.geom.simplify.douglasPeucker( + simplifiedFlatCoordinates.length = ol.geom.flat.simplify.douglasPeucker( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride, squaredTolerance, simplifiedFlatCoordinates, 0); var simplifiedLinearRing = new ol.geom.LinearRing(null); diff --git a/src/ol/geom/linestring.js b/src/ol/geom/linestring.js index ab3b55dfb0..15ebb69cc8 100644 --- a/src/ol/geom/linestring.js +++ b/src/ol/geom/linestring.js @@ -7,7 +7,7 @@ goog.require('ol.geom.GeometryType'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.closest'); -goog.require('ol.geom.simplify'); +goog.require('ol.geom.flat.simplify'); @@ -160,7 +160,7 @@ ol.geom.LineString.prototype.getFlatMidpoint = function() { ol.geom.LineString.prototype.getSimplifiedGeometryInternal = function(squaredTolerance) { var simplifiedFlatCoordinates = []; - simplifiedFlatCoordinates.length = ol.geom.simplify.douglasPeucker( + simplifiedFlatCoordinates.length = ol.geom.flat.simplify.douglasPeucker( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride, squaredTolerance, simplifiedFlatCoordinates, 0); var simplifiedLineString = new ol.geom.LineString(null); diff --git a/src/ol/geom/multilinestring.js b/src/ol/geom/multilinestring.js index d859c34bae..8581fde85b 100644 --- a/src/ol/geom/multilinestring.js +++ b/src/ol/geom/multilinestring.js @@ -8,7 +8,7 @@ goog.require('ol.geom.LineString'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.closest'); -goog.require('ol.geom.simplify'); +goog.require('ol.geom.flat.simplify'); @@ -214,7 +214,7 @@ ol.geom.MultiLineString.prototype.getSimplifiedGeometryInternal = function(squaredTolerance) { var simplifiedFlatCoordinates = []; var simplifiedEnds = []; - simplifiedFlatCoordinates.length = ol.geom.simplify.douglasPeuckers( + simplifiedFlatCoordinates.length = ol.geom.flat.simplify.douglasPeuckers( this.flatCoordinates, 0, this.ends_, this.stride, squaredTolerance, simplifiedFlatCoordinates, 0, simplifiedEnds); var simplifiedMultiLineString = new ol.geom.MultiLineString(null); diff --git a/src/ol/geom/multipolygon.js b/src/ol/geom/multipolygon.js index 0e14f3798e..9cc90a7a25 100644 --- a/src/ol/geom/multipolygon.js +++ b/src/ol/geom/multipolygon.js @@ -9,7 +9,7 @@ goog.require('ol.geom.Polygon'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.closest'); -goog.require('ol.geom.simplify'); +goog.require('ol.geom.flat.simplify'); @@ -219,7 +219,7 @@ ol.geom.MultiPolygon.prototype.getSimplifiedGeometryInternal = function(squaredTolerance) { var simplifiedFlatCoordinates = []; var simplifiedEndss = []; - simplifiedFlatCoordinates.length = ol.geom.simplify.quantizess( + simplifiedFlatCoordinates.length = ol.geom.flat.simplify.quantizess( this.flatCoordinates, 0, this.endss_, this.stride, Math.sqrt(squaredTolerance), simplifiedFlatCoordinates, 0, simplifiedEndss); diff --git a/src/ol/geom/polygon.js b/src/ol/geom/polygon.js index 2fa6eebae5..3507cc5152 100644 --- a/src/ol/geom/polygon.js +++ b/src/ol/geom/polygon.js @@ -9,7 +9,7 @@ goog.require('ol.geom.Point'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.closest'); -goog.require('ol.geom.simplify'); +goog.require('ol.geom.flat.simplify'); @@ -243,7 +243,7 @@ ol.geom.Polygon.prototype.getSimplifiedGeometryInternal = function(squaredTolerance) { var simplifiedFlatCoordinates = []; var simplifiedEnds = []; - simplifiedFlatCoordinates.length = ol.geom.simplify.quantizes( + simplifiedFlatCoordinates.length = ol.geom.flat.simplify.quantizes( this.flatCoordinates, 0, this.ends_, this.stride, Math.sqrt(squaredTolerance), simplifiedFlatCoordinates, 0, simplifiedEnds); diff --git a/src/ol/render/canvas/canvasreplay.js b/src/ol/render/canvas/canvasreplay.js index 5f89a901cb..594b7b3c21 100644 --- a/src/ol/render/canvas/canvasreplay.js +++ b/src/ol/render/canvas/canvasreplay.js @@ -17,7 +17,7 @@ goog.require('ol.color'); goog.require('ol.extent'); goog.require('ol.extent.Relationship'); goog.require('ol.geom.flat'); -goog.require('ol.geom.simplify'); +goog.require('ol.geom.flat.simplify'); goog.require('ol.render.IReplayGroup'); goog.require('ol.render.IVectorContext'); goog.require('ol.render.canvas'); @@ -1310,7 +1310,7 @@ ol.render.canvas.PolygonReplay.prototype.finish = function() { var coordinates = this.coordinates; var i, ii; for (i = 0, ii = coordinates.length; i < ii; ++i) { - coordinates[i] = ol.geom.simplify.snap(coordinates[i], tolerance); + coordinates[i] = ol.geom.flat.simplify.snap(coordinates[i], tolerance); } } }; diff --git a/test/spec/ol/geom/simplifygeom.test.js b/test/spec/ol/geom/flat/simplifyflatgeom.test.js similarity index 83% rename from test/spec/ol/geom/simplifygeom.test.js rename to test/spec/ol/geom/flat/simplifyflatgeom.test.js index a59f128304..c18029a5c4 100644 --- a/test/spec/ol/geom/simplifygeom.test.js +++ b/test/spec/ol/geom/flat/simplifyflatgeom.test.js @@ -1,7 +1,7 @@ goog.provide('ol.test.geom.simplify'); -describe('ol.geom.simplify', function() { +describe('ol.geom.flat.simplify', function() { var flatCoordinates = [ 224.55, 250.15, 226.91, 244.19, 233.31, 241.45, 234.98, 236.06, @@ -81,31 +81,31 @@ describe('ol.geom.simplify', function() { 866.36, 480.77 ]; - describe('ol.geom.simplify.lineString', function() { + describe('ol.geom.flat.simplify.lineString', function() { it('works with empty line strings', function() { - expect(ol.geom.simplify.lineString([], 0, 0, 2, 1, true)).to. + expect(ol.geom.flat.simplify.lineString([], 0, 0, 2, 1, true)).to. eql([]); - expect(ol.geom.simplify.lineString([], 0, 0, 2, 1, false)).to. + expect(ol.geom.flat.simplify.lineString([], 0, 0, 2, 1, false)).to. eql([]); }); it('works with a line string with a single point', function() { - expect(ol.geom.simplify.lineString([1, 2], 0, 2, 2, 1, true)).to. + expect(ol.geom.flat.simplify.lineString([1, 2], 0, 2, 2, 1, true)).to. eql([1, 2]); - expect(ol.geom.simplify.lineString([1, 2], 0, 2, 2, 1, false)).to. + expect(ol.geom.flat.simplify.lineString([1, 2], 0, 2, 2, 1, false)).to. eql([1, 2]); }); it('returns the expected result with low quality', function() { - var result = ol.geom.simplify.lineString( + var result = ol.geom.flat.simplify.lineString( flatCoordinates, 0, flatCoordinates.length, 2, 25, false); expect(result.length).to.be(simplifiedFlatCoordinates.length); expect(result).to.eql(simplifiedFlatCoordinates); }); it('returns the expected result with high quality', function() { - var result = ol.geom.simplify.lineString( + var result = ol.geom.flat.simplify.lineString( flatCoordinates, 0, flatCoordinates.length, 2, 25, true); expect(result.length).to.be(simplifiedHighQualityFlatCoordinates.length); expect(result).to.eql(simplifiedHighQualityFlatCoordinates); @@ -113,7 +113,7 @@ describe('ol.geom.simplify', function() { }); - describe('ol.geom.simplify.radialDistance', function() { + describe('ol.geom.flat.simplify.radialDistance', function() { var dest; beforeEach(function() { @@ -121,63 +121,63 @@ describe('ol.geom.simplify', function() { }); it('works with empty line strings', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( [], 0, 0, 2, 1, dest, 0)).to.be(0); expect(dest).to.eql([]); }); it('works with a line string with a single point', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( [1, 2], 0, 2, 2, 1, dest, 0)).to.be(2); expect(dest).to.eql([1, 2]); }); it('works with a line string with two points', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( [1, 2, 3, 4], 0, 4, 2, 1, dest, 0)).to.be(4); expect(dest).to.eql([1, 2, 3, 4]); }); it('works when the points are widely spaced', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( [0, 0, 1, 0, 2, 0, 3, 0], 0, 8, 2, 0.5, dest, 0)).to.be(8); expect(dest).to.eql([0, 0, 1, 0, 2, 0, 3, 0]); }); it('works when the spacing matches the tolerance', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( [0, 0, 1, 0, 2, 0, 3, 0], 0, 8, 2, 1, dest, 0)).to.be(6); expect(dest).to.eql([0, 0, 2, 0, 3, 0]); }); it('works when the points are closely spaced', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( [0, 0, 1, 0, 2, 0, 3, 0], 0, 8, 2, 1.5, dest, 0)).to.be(6); expect(dest).to.eql([0, 0, 2, 0, 3, 0]); }); it('works when the line oscillates with widely spaced points', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1], 0, 12, 2, 1, dest, 0)). to.be(12); expect(dest).to.eql([0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1]); }); it('works when the line oscillates with closely spaced points', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1], 0, 12, 2, 2, dest, 0)).to.be(4); expect(dest).to.eql([0, 0, 1, 1]); }); it('works when the line oscillates within the tolerance', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0], 0, 14, 2, 2, dest, 0)). to.be(2); expect(dest).to.eql([0, 0]); }); it('works with real data', function() { - expect(ol.geom.simplify.radialDistance( + expect(ol.geom.flat.simplify.radialDistance( flatCoordinates, 0, flatCoordinates.length, 2, 25, dest, 0)). to.be(simplifiedRadiallyFlatCoordinates.length); expect(dest).to.eql(simplifiedRadiallyFlatCoordinates); @@ -185,7 +185,7 @@ describe('ol.geom.simplify', function() { }); - describe('ol.geom.simplify.douglasPeucker', function() { + describe('ol.geom.flat.simplify.douglasPeucker', function() { var dest; beforeEach(function() { @@ -193,93 +193,93 @@ describe('ol.geom.simplify', function() { }); it('works with empty line strings', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [], 0, 0, 2, 1, dest, 0)).to.be(0); expect(dest).to.eql([]); }); it('works with a line string with a single point', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [1, 2], 0, 2, 2, 1, dest, 0)).to.be(2); expect(dest).to.eql([1, 2]); }); it('works with a line string with two points', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [1, 2, 3, 4], 0, 4, 2, 1, dest, 0)).to.be(4); expect(dest).to.eql([1, 2, 3, 4]); }); it('works when the points are widely spaced', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 0, 2, 0, 3, 0], 0, 8, 2, 0.5, dest, 0)).to.be(4); expect(dest).to.eql([0, 0, 3, 0]); }); it('works when the spacing matches the tolerance', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 0, 2, 0, 3, 0], 0, 8, 2, 1, dest, 0)).to.be(4); expect(dest).to.eql([0, 0, 3, 0]); }); it('works when the points are closely spaced', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 0, 2, 0, 3, 0], 0, 8, 2, 1.5, dest, 0)).to.be(4); expect(dest).to.eql([0, 0, 3, 0]); }); it('does not elimnate points outside the tolerance', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 1, 2, 0], 0, 6, 2, 0.5, dest, 0)).to.be(6); expect(dest).to.eql([0, 0, 1, 1, 2, 0]); }); it('does eliminate points within the tolerance', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 1, 2, 0], 0, 6, 2, 2, dest, 0)).to.be(4); expect(dest).to.eql([0, 0, 2, 0]); }); it('does not eliminate multiple points outside the tolerance', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 1, 1, -1, 2, 0], 0, 8, 2, 0.5, dest, 0)).to.be(8); expect(dest).to.eql([0, 0, 1, 1, 1, -1, 2, 0]); }); it('does eliminate multiple points within the tolerance', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 1, 1, -1, 2, 0], 0, 8, 2, 2, dest, 0)).to.be(4); expect(dest).to.eql([0, 0, 2, 0]); }); it('works when the line oscillates with widely spaced points', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1], 0, 12, 2, 1, dest, 0)).to.be(4); expect(dest).to.eql([0, 0, 1, 1]); }); it('works when the line oscillates with closely spaced points', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1], 0, 12, 2, 2, dest, 0)). to.be(4); expect(dest).to.eql([0, 0, 1, 1]); }); it('works when the line oscillates within the tolerance', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0], 0, 14, 2, 2, dest, 0)). to.be(4); expect(dest).to.eql([0, 0, 0, 0]); }); it('works on small triangles', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( [3, 0, 4, 1, 5, 2, 5, 0], 0, 8, 2, 1, dest, 0)).to.be(6); expect(dest).to.eql([3, 0, 5, 2, 5, 0]); }); it('is the same as high quality simplification', function() { - expect(ol.geom.simplify.douglasPeucker( + expect(ol.geom.flat.simplify.douglasPeucker( flatCoordinates, 0, flatCoordinates.length, 2, 25, dest, 0)). to.be(simplifiedHighQualityFlatCoordinates.length); expect(dest).to.eql(simplifiedHighQualityFlatCoordinates); @@ -287,32 +287,32 @@ describe('ol.geom.simplify', function() { }); - describe('ol.geom.simplify.quantize', function() { + describe('ol.geom.flat.simplify.quantize', function() { it('handles empty coordinates', function() { var simplifiedFlatCoordinates = []; - expect(ol.geom.simplify.quantize( + expect(ol.geom.flat.simplify.quantize( [], 0, 0, 2, 2, simplifiedFlatCoordinates, 0)).to.be(0); expect(simplifiedFlatCoordinates).to.be.empty(); }); it('expands points to a zero-length line', function() { var simplifiedFlatCoordinates = []; - expect(ol.geom.simplify.quantize( + expect(ol.geom.flat.simplify.quantize( [0, 0, 0, 0], 0, 4, 2, 2, simplifiedFlatCoordinates, 0)).to.be(4); expect(simplifiedFlatCoordinates).to.eql([0, 0, 0, 0]); }); it('snaps near-by points to the same value', function() { var simplifiedFlatCoordinates = []; - expect(ol.geom.simplify.quantize( + expect(ol.geom.flat.simplify.quantize( [0.1, 0, 0, 0.1], 0, 4, 2, 2, simplifiedFlatCoordinates, 0)).to.be(4); expect(simplifiedFlatCoordinates).to.eql([0, 0, 0, 0]); }); it('eliminates duplicate snapped points', function() { var simplifiedFlatCoordinates = []; - expect(ol.geom.simplify.quantize( + expect(ol.geom.flat.simplify.quantize( [0.1, 0, 2, 0, 2.1, 0, 2, 0.1, 1.9, 0, 2, -0.1], 0, 12, 2, 2, simplifiedFlatCoordinates, 0)).to.be(4); expect(simplifiedFlatCoordinates).to.eql([0, 0, 2, 0]); @@ -320,7 +320,7 @@ describe('ol.geom.simplify', function() { it('eliminates horizontal colinear points', function() { var simplifiedFlatCoordinates = []; - expect(ol.geom.simplify.quantize( + expect(ol.geom.flat.simplify.quantize( [0, 0, 2, 0, 4, 0, 6, 0], 0, 8, 2, 2, simplifiedFlatCoordinates, 0)).to.be(4); expect(simplifiedFlatCoordinates).to.eql([0, 0, 6, 0]); @@ -328,7 +328,7 @@ describe('ol.geom.simplify', function() { it('eliminates vertical colinear points', function() { var simplifiedFlatCoordinates = []; - expect(ol.geom.simplify.quantize( + expect(ol.geom.flat.simplify.quantize( [0, 0, 0, -2, 0, -4, 0, -6], 0, 8, 2, 2, simplifiedFlatCoordinates, 0)).to.be(4); expect(simplifiedFlatCoordinates).to.eql([0, 0, 0, -6]); @@ -336,7 +336,7 @@ describe('ol.geom.simplify', function() { it('eliminates diagonal colinear points', function() { var simplifiedFlatCoordinates = []; - expect(ol.geom.simplify.quantize( + expect(ol.geom.flat.simplify.quantize( [0, 0, 2, -2, 4, -4, 6, -6], 0, 8, 2, 2, simplifiedFlatCoordinates, 0)).to.be(4); expect(simplifiedFlatCoordinates).to.eql([0, 0, 6, -6]); @@ -344,7 +344,7 @@ describe('ol.geom.simplify', function() { it('handles switchbacks', function() { var simplifiedFlatCoordinates = []; - expect(ol.geom.simplify.quantize( + expect(ol.geom.flat.simplify.quantize( [0, 0, 2, 0, 0, 0, 4, 0], 0, 8, 2, 2, simplifiedFlatCoordinates, 0)).to.be(8); expect(simplifiedFlatCoordinates).to.eql([0, 0, 2, 0, 0, 0, 4, 0]); @@ -355,4 +355,5 @@ describe('ol.geom.simplify', function() { }); -goog.require('ol.geom.simplify'); +goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.simplify'); From 522fda16889fb8759dd7eb0182dfca7cfc29175a Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 12 Mar 2014 11:39:26 +0100 Subject: [PATCH 05/17] Factor out ol.geom.flat.interpolate --- src/ol/geom/flat/interpolateflatgeom.js | 190 ++++++++++++++++++ src/ol/geom/flatgeom.js | 185 ----------------- src/ol/geom/linestring.js | 3 +- src/ol/geom/multilinestring.js | 3 +- .../ol/geom/flat/interpolateflatgeom.test.js | 50 +++++ test/spec/ol/geom/flatgeom.test.js | 43 ---- 6 files changed, 244 insertions(+), 230 deletions(-) create mode 100644 src/ol/geom/flat/interpolateflatgeom.js create mode 100644 test/spec/ol/geom/flat/interpolateflatgeom.test.js diff --git a/src/ol/geom/flat/interpolateflatgeom.js b/src/ol/geom/flat/interpolateflatgeom.js new file mode 100644 index 0000000000..6a7a54031d --- /dev/null +++ b/src/ol/geom/flat/interpolateflatgeom.js @@ -0,0 +1,190 @@ +goog.provide('ol.geom.flat.interpolate'); + +goog.require('goog.array'); +goog.require('goog.asserts'); +goog.require('goog.math'); +goog.require('ol.geom.flat'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @param {number} fraction Fraction. + * @param {Array.=} opt_dest Destination. + * @return {Array.} Destination. + */ +ol.geom.flat.interpolate.lineString = + function(flatCoordinates, offset, end, stride, fraction, opt_dest) { + // FIXME interpolate extra dimensions + goog.asserts.assert(0 <= fraction && fraction <= 1); + var pointX = NaN; + var pointY = NaN; + var n = (end - offset) / stride; + if (n === 0) { + goog.asserts.fail(); + } else if (n == 1) { + pointX = flatCoordinates[offset]; + pointY = flatCoordinates[offset + 1]; + } else if (n == 2) { + pointX = (1 - fraction) * flatCoordinates[offset] + + fraction * flatCoordinates[offset + stride]; + pointY = (1 - fraction) * flatCoordinates[offset + 1] + + fraction * flatCoordinates[offset + stride + 1]; + } else { + var x1 = flatCoordinates[offset]; + var y1 = flatCoordinates[offset + 1]; + var length = 0; + var cumulativeLengths = [0]; + var i; + for (i = offset + stride; i < end; i += stride) { + var x2 = flatCoordinates[i]; + var y2 = flatCoordinates[i + 1]; + length += Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); + cumulativeLengths.push(length); + x1 = x2; + y1 = y2; + } + var target = fraction * length; + var index = goog.array.binarySearch(cumulativeLengths, target); + if (index < 0) { + var t = (target - cumulativeLengths[-index - 2]) / + (cumulativeLengths[-index - 1] - cumulativeLengths[-index - 2]); + var o = offset + (-index - 2) * stride; + pointX = goog.math.lerp( + flatCoordinates[o], flatCoordinates[o + stride], t); + pointY = goog.math.lerp( + flatCoordinates[o + 1], flatCoordinates[o + stride + 1], t); + } else { + pointX = flatCoordinates[offset + index * stride]; + pointY = flatCoordinates[offset + index * stride + 1]; + } + } + if (goog.isDefAndNotNull(opt_dest)) { + opt_dest.push(pointX, pointY); + return opt_dest; + } else { + return [pointX, pointY]; + } +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @param {number} m M. + * @param {boolean} extrapolate Extrapolate. + * @return {ol.Coordinate} Coordinate. + */ +ol.geom.flat.lineStringCoordinateAtM = + function(flatCoordinates, offset, end, stride, m, extrapolate) { + if (end == offset) { + return null; + } + var coordinate; + if (m < flatCoordinates[offset + stride - 1]) { + if (extrapolate) { + coordinate = flatCoordinates.slice(offset, offset + stride); + coordinate[stride - 1] = m; + return coordinate; + } else { + return null; + } + } else if (flatCoordinates[end - 1] < m) { + if (extrapolate) { + coordinate = flatCoordinates.slice(end - stride, end); + coordinate[stride - 1] = m; + return coordinate; + } else { + return null; + } + } + // FIXME use O(1) search + if (m == flatCoordinates[offset + stride - 1]) { + return flatCoordinates.slice(offset, offset + stride); + } + var lo = offset / stride; + var hi = end / stride; + while (lo < hi) { + var mid = (lo + hi) >> 1; + if (m < flatCoordinates[(mid + 1) * stride - 1]) { + hi = mid; + } else { + lo = mid + 1; + } + } + var m0 = flatCoordinates[lo * stride - 1]; + if (m == m0) { + return flatCoordinates.slice((lo - 1) * stride, (lo - 1) * stride + stride); + } + var m1 = flatCoordinates[(lo + 1) * stride - 1]; + goog.asserts.assert(m0 < m); + goog.asserts.assert(m <= m1); + var t = (m - m0) / (m1 - m0); + coordinate = []; + var i; + for (i = 0; i < stride - 1; ++i) { + coordinate.push(goog.math.lerp(flatCoordinates[(lo - 1) * stride + i], + flatCoordinates[lo * stride + i], t)); + } + coordinate.push(m); + goog.asserts.assert(coordinate.length == stride); + return coordinate; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} ends Ends. + * @param {number} stride Stride. + * @param {number} m M. + * @param {boolean} extrapolate Extrapolate. + * @param {boolean} interpolate Interpolate. + * @return {ol.Coordinate} Coordinate. + */ +ol.geom.flat.lineStringsCoordinateAtM = function( + flatCoordinates, offset, ends, stride, m, extrapolate, interpolate) { + if (interpolate) { + return ol.geom.flat.lineStringCoordinateAtM( + flatCoordinates, offset, ends[ends.length - 1], stride, m, extrapolate); + } + var coordinate; + if (m < flatCoordinates[stride - 1]) { + if (extrapolate) { + coordinate = flatCoordinates.slice(0, stride); + coordinate[stride - 1] = m; + return coordinate; + } else { + return null; + } + } + if (flatCoordinates[flatCoordinates.length - 1] < m) { + if (extrapolate) { + coordinate = flatCoordinates.slice(flatCoordinates.length - stride); + coordinate[stride - 1] = m; + return coordinate; + } else { + return null; + } + } + var i, ii; + for (i = 0, ii = ends.length; i < ii; ++i) { + var end = ends[i]; + if (offset == end) { + continue; + } + if (m < flatCoordinates[offset + stride - 1]) { + return null; + } else if (m <= flatCoordinates[end - 1]) { + return ol.geom.flat.lineStringCoordinateAtM( + flatCoordinates, offset, end, stride, m, false); + } + offset = end; + } + goog.asserts.fail(); + return null; +}; diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index 9ba82972fd..3acca91c64 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -2,7 +2,6 @@ goog.provide('ol.geom.flat'); goog.require('goog.array'); goog.require('goog.asserts'); -goog.require('goog.math'); goog.require('goog.vec.Mat4'); goog.require('ol.extent'); @@ -199,190 +198,6 @@ ol.geom.flat.inflateCoordinatesss = }; -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @param {number} fraction Fraction. - * @param {Array.=} opt_dest Destination. - * @return {Array.} Destination. - */ -ol.geom.flat.lineStringInterpolate = - function(flatCoordinates, offset, end, stride, fraction, opt_dest) { - // FIXME interpolate extra dimensions - goog.asserts.assert(0 <= fraction && fraction <= 1); - var pointX = NaN; - var pointY = NaN; - var n = (end - offset) / stride; - if (n === 0) { - goog.asserts.fail(); - } else if (n == 1) { - pointX = flatCoordinates[offset]; - pointY = flatCoordinates[offset + 1]; - } else if (n == 2) { - pointX = (1 - fraction) * flatCoordinates[offset] + - fraction * flatCoordinates[offset + stride]; - pointY = (1 - fraction) * flatCoordinates[offset + 1] + - fraction * flatCoordinates[offset + stride + 1]; - } else { - var x1 = flatCoordinates[offset]; - var y1 = flatCoordinates[offset + 1]; - var length = 0; - var cumulativeLengths = [0]; - var i; - for (i = offset + stride; i < end; i += stride) { - var x2 = flatCoordinates[i]; - var y2 = flatCoordinates[i + 1]; - length += Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); - cumulativeLengths.push(length); - x1 = x2; - y1 = y2; - } - var target = fraction * length; - var index = goog.array.binarySearch(cumulativeLengths, target); - if (index < 0) { - var t = (target - cumulativeLengths[-index - 2]) / - (cumulativeLengths[-index - 1] - cumulativeLengths[-index - 2]); - var o = offset + (-index - 2) * stride; - pointX = goog.math.lerp( - flatCoordinates[o], flatCoordinates[o + stride], t); - pointY = goog.math.lerp( - flatCoordinates[o + 1], flatCoordinates[o + stride + 1], t); - } else { - pointX = flatCoordinates[offset + index * stride]; - pointY = flatCoordinates[offset + index * stride + 1]; - } - } - if (goog.isDefAndNotNull(opt_dest)) { - opt_dest.push(pointX, pointY); - return opt_dest; - } else { - return [pointX, pointY]; - } -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @param {number} m M. - * @param {boolean} extrapolate Extrapolate. - * @return {ol.Coordinate} Coordinate. - */ -ol.geom.flat.lineStringCoordinateAtM = - function(flatCoordinates, offset, end, stride, m, extrapolate) { - if (end == offset) { - return null; - } - var coordinate; - if (m < flatCoordinates[offset + stride - 1]) { - if (extrapolate) { - coordinate = flatCoordinates.slice(offset, offset + stride); - coordinate[stride - 1] = m; - return coordinate; - } else { - return null; - } - } else if (flatCoordinates[end - 1] < m) { - if (extrapolate) { - coordinate = flatCoordinates.slice(end - stride, end); - coordinate[stride - 1] = m; - return coordinate; - } else { - return null; - } - } - // FIXME use O(1) search - if (m == flatCoordinates[offset + stride - 1]) { - return flatCoordinates.slice(offset, offset + stride); - } - var lo = offset / stride; - var hi = end / stride; - while (lo < hi) { - var mid = (lo + hi) >> 1; - if (m < flatCoordinates[(mid + 1) * stride - 1]) { - hi = mid; - } else { - lo = mid + 1; - } - } - var m0 = flatCoordinates[lo * stride - 1]; - if (m == m0) { - return flatCoordinates.slice((lo - 1) * stride, (lo - 1) * stride + stride); - } - var m1 = flatCoordinates[(lo + 1) * stride - 1]; - goog.asserts.assert(m0 < m); - goog.asserts.assert(m <= m1); - var t = (m - m0) / (m1 - m0); - coordinate = []; - var i; - for (i = 0; i < stride - 1; ++i) { - coordinate.push(goog.math.lerp(flatCoordinates[(lo - 1) * stride + i], - flatCoordinates[lo * stride + i], t)); - } - coordinate.push(m); - goog.asserts.assert(coordinate.length == stride); - return coordinate; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.} ends Ends. - * @param {number} stride Stride. - * @param {number} m M. - * @param {boolean} extrapolate Extrapolate. - * @param {boolean} interpolate Interpolate. - * @return {ol.Coordinate} Coordinate. - */ -ol.geom.flat.lineStringsCoordinateAtM = function( - flatCoordinates, offset, ends, stride, m, extrapolate, interpolate) { - if (interpolate) { - return ol.geom.flat.lineStringCoordinateAtM( - flatCoordinates, offset, ends[ends.length - 1], stride, m, extrapolate); - } - var coordinate; - if (m < flatCoordinates[stride - 1]) { - if (extrapolate) { - coordinate = flatCoordinates.slice(0, stride); - coordinate[stride - 1] = m; - return coordinate; - } else { - return null; - } - } - if (flatCoordinates[flatCoordinates.length - 1] < m) { - if (extrapolate) { - coordinate = flatCoordinates.slice(flatCoordinates.length - stride); - coordinate[stride - 1] = m; - return coordinate; - } else { - return null; - } - } - var i, ii; - for (i = 0, ii = ends.length; i < ii; ++i) { - var end = ends[i]; - if (offset == end) { - continue; - } - if (m < flatCoordinates[offset + stride - 1]) { - return null; - } else if (m <= flatCoordinates[end - 1]) { - return ol.geom.flat.lineStringCoordinateAtM( - flatCoordinates, offset, end, stride, m, false); - } - offset = end; - } - goog.asserts.fail(); - return null; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. diff --git a/src/ol/geom/linestring.js b/src/ol/geom/linestring.js index 15ebb69cc8..71197f755c 100644 --- a/src/ol/geom/linestring.js +++ b/src/ol/geom/linestring.js @@ -7,6 +7,7 @@ goog.require('ol.geom.GeometryType'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.closest'); +goog.require('ol.geom.flat.interpolate'); goog.require('ol.geom.flat.simplify'); @@ -145,7 +146,7 @@ ol.geom.LineString.prototype.getLength = function() { */ ol.geom.LineString.prototype.getFlatMidpoint = function() { if (this.flatMidpointRevision_ != this.getRevision()) { - this.flatMidpoint_ = ol.geom.flat.lineStringInterpolate( + this.flatMidpoint_ = ol.geom.flat.interpolate.lineString( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride, 0.5, this.flatMidpoint_); this.flatMidpointRevision_ = this.getRevision(); diff --git a/src/ol/geom/multilinestring.js b/src/ol/geom/multilinestring.js index 8581fde85b..781ccfb3fe 100644 --- a/src/ol/geom/multilinestring.js +++ b/src/ol/geom/multilinestring.js @@ -8,6 +8,7 @@ goog.require('ol.geom.LineString'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.closest'); +goog.require('ol.geom.flat.interpolate'); goog.require('ol.geom.flat.simplify'); @@ -198,7 +199,7 @@ ol.geom.MultiLineString.prototype.getFlatMidpoints = function() { var i, ii; for (i = 0, ii = ends.length; i < ii; ++i) { var end = ends[i]; - var midpoint = ol.geom.flat.lineStringInterpolate( + var midpoint = ol.geom.flat.interpolate.lineString( flatCoordinates, offset, end, stride, 0.5); goog.array.extend(midpoints, midpoint); offset = end; diff --git a/test/spec/ol/geom/flat/interpolateflatgeom.test.js b/test/spec/ol/geom/flat/interpolateflatgeom.test.js new file mode 100644 index 0000000000..3d05b52214 --- /dev/null +++ b/test/spec/ol/geom/flat/interpolateflatgeom.test.js @@ -0,0 +1,50 @@ +goog.provide('ol.test.geom.flat.interpolate'); + +describe('ol.geom.flat.interpolate', function() { + + describe('ol.geom.flat.interpolate.lineString', function() { + + it('returns the expected value for single points', function() { + var flatCoordinates = [0, 1]; + var point = + ol.geom.flat.interpolate.lineString(flatCoordinates, 0, 2, 2, 0.5); + expect(point).to.eql([0, 1]); + }); + + it('returns the expected value for simple line segments', function() { + var flatCoordinates = [0, 1, 2, 3]; + var point = + ol.geom.flat.interpolate.lineString(flatCoordinates, 0, 4, 2, 0.5); + expect(point).to.eql([1, 2]); + }); + + it('returns the expected value when the mid point is an existing ' + + 'coordinate', function() { + var flatCoordinates = [0, 1, 2, 3, 4, 5]; + var point = ol.geom.flat.interpolate.lineString( + flatCoordinates, 0, 6, 2, 0.5); + expect(point).to.eql([2, 3]); + }); + + it('returns the expected value when the midpoint falls halfway between ' + + 'two existing coordinates', function() { + var flatCoordinates = [0, 1, 2, 3, 4, 5, 6, 7]; + var point = ol.geom.flat.interpolate.lineString( + flatCoordinates, 0, 8, 2, 0.5); + expect(point).to.eql([3, 4]); + }); + + it('returns the expected value when the coordinates are not evenly spaced', + function() { + var flatCoordinates = [0, 1, 2, 3, 6, 7]; + var point = ol.geom.flat.interpolate.lineString( + flatCoordinates, 0, 6, 2, 0.5); + expect(point).to.eql([3, 4]); + }); + + }); + +}); + +goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.interpolate'); diff --git a/test/spec/ol/geom/flatgeom.test.js b/test/spec/ol/geom/flatgeom.test.js index dbbf55bf90..75328a9660 100644 --- a/test/spec/ol/geom/flatgeom.test.js +++ b/test/spec/ol/geom/flatgeom.test.js @@ -84,49 +84,6 @@ describe('ol.geom.flat', function() { }); }); - - describe('ol.geom.flat.lineStringInterpolate', function() { - - it('returns the expected value for single points', function() { - var flatCoordinates = [0, 1]; - var point = - ol.geom.flat.lineStringInterpolate(flatCoordinates, 0, 2, 2, 0.5); - expect(point).to.eql([0, 1]); - }); - - it('returns the expected value for simple line segments', function() { - var flatCoordinates = [0, 1, 2, 3]; - var point = - ol.geom.flat.lineStringInterpolate(flatCoordinates, 0, 4, 2, 0.5); - expect(point).to.eql([1, 2]); - }); - - it('returns the expected value when the mid point is an existing ' + - 'coordinate', function() { - var flatCoordinates = [0, 1, 2, 3, 4, 5]; - var point = - ol.geom.flat.lineStringInterpolate(flatCoordinates, 0, 6, 2, 0.5); - expect(point).to.eql([2, 3]); - }); - - it('returns the expected value when the midpoint falls halfway between ' + - 'two existing coordinates', function() { - var flatCoordinates = [0, 1, 2, 3, 4, 5, 6, 7]; - var point = - ol.geom.flat.lineStringInterpolate(flatCoordinates, 0, 8, 2, 0.5); - expect(point).to.eql([3, 4]); - }); - - it('returns the expected value when the coordinates are not evenly spaced', - function() { - var flatCoordinates = [0, 1, 2, 3, 6, 7]; - var point = - ol.geom.flat.lineStringInterpolate(flatCoordinates, 0, 6, 2, 0.5); - expect(point).to.eql([3, 4]); - }); - - }); - describe('ol.geom.flat.linearRingArea', function() { it('calcaultes the area of a triangle', function() { From 20ee1dc9925c0d88709e3390fb5be37e5efee007 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 12 Mar 2014 11:57:01 +0100 Subject: [PATCH 06/17] Factor out ol.geom.flat.length --- src/ol/geom/flat/lengthflatgeom.js | 45 ++++++++++++++++++++++++++++++ src/ol/geom/flatgeom.js | 41 --------------------------- src/ol/geom/linestring.js | 3 +- 3 files changed, 47 insertions(+), 42 deletions(-) create mode 100644 src/ol/geom/flat/lengthflatgeom.js diff --git a/src/ol/geom/flat/lengthflatgeom.js b/src/ol/geom/flat/lengthflatgeom.js new file mode 100644 index 0000000000..52d053c4d8 --- /dev/null +++ b/src/ol/geom/flat/lengthflatgeom.js @@ -0,0 +1,45 @@ +goog.provide('ol.geom.flat.length'); + +goog.require('ol.geom.flat'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @return {number} Length. + */ +ol.geom.flat.length.lineString = + function(flatCoordinates, offset, end, stride) { + var x1 = flatCoordinates[offset]; + var y1 = flatCoordinates[offset + 1]; + var length = 0; + var i; + for (i = offset + stride; i < end; i += stride) { + var x2 = flatCoordinates[i]; + var y2 = flatCoordinates[i + 1]; + length += Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); + x1 = x2; + y1 = y2; + } + return length; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @return {number} Perimeter. + */ +ol.geom.flat.length.linearRing = + function(flatCoordinates, offset, end, stride) { + var perimeter = + ol.geom.flat.length.lineString(flatCoordinates, offset, end, stride); + var dx = flatCoordinates[end - stride] - flatCoordinates[offset]; + var dy = flatCoordinates[end - stride + 1] - flatCoordinates[offset + 1]; + perimeter += Math.sqrt(dx * dx + dy * dy); + return perimeter; +}; diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index 3acca91c64..42df2388b7 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -198,29 +198,6 @@ ol.geom.flat.inflateCoordinatesss = }; -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @return {number} Length. - */ -ol.geom.flat.lineStringLength = function(flatCoordinates, offset, end, stride) { - var x1 = flatCoordinates[offset]; - var y1 = flatCoordinates[offset + 1]; - var length = 0; - var i; - for (i = offset + stride; i < end; i += stride) { - var x2 = flatCoordinates[i]; - var y2 = flatCoordinates[i + 1]; - length += Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); - x1 = x2; - y1 = y2; - } - return length; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. @@ -298,24 +275,6 @@ ol.geom.flat.linearRingIsClockwise = }; -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @return {number} Perimeter. - */ -ol.geom.flat.linearRingPerimeter = - function(flatCoordinates, offset, end, stride) { - var perimeter = - ol.geom.flat.lineStringLength(flatCoordinates, offset, end, stride); - var dx = flatCoordinates[end - stride] - flatCoordinates[offset]; - var dy = flatCoordinates[end - stride + 1] - flatCoordinates[offset + 1]; - perimeter += Math.sqrt(dx * dx + dy * dy); - return perimeter; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. diff --git a/src/ol/geom/linestring.js b/src/ol/geom/linestring.js index 71197f755c..91e621a6b2 100644 --- a/src/ol/geom/linestring.js +++ b/src/ol/geom/linestring.js @@ -8,6 +8,7 @@ goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.interpolate'); +goog.require('ol.geom.flat.length'); goog.require('ol.geom.flat.simplify'); @@ -136,7 +137,7 @@ ol.geom.LineString.prototype.getCoordinates = function() { * @todo stability experimental */ ol.geom.LineString.prototype.getLength = function() { - return ol.geom.flat.lineStringLength( + return ol.geom.flat.length.lineString( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride); }; From becd1318bd216a7f58d2301e6f87eabf8ae90325 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 12 Mar 2014 12:08:24 +0100 Subject: [PATCH 07/17] Factor out ol.geom.flat.area --- src/ol/geom/flat/areaflatgeom.js | 66 +++++++++++++++++++++ src/ol/geom/flatgeom.js | 61 ------------------- src/ol/geom/linearring.js | 3 +- src/ol/geom/multipolygon.js | 3 +- src/ol/geom/polygon.js | 3 +- test/spec/ol/geom/flat/areaflatgeom.test.js | 33 +++++++++++ test/spec/ol/geom/flatgeom.test.js | 23 ------- 7 files changed, 105 insertions(+), 87 deletions(-) create mode 100644 src/ol/geom/flat/areaflatgeom.js create mode 100644 test/spec/ol/geom/flat/areaflatgeom.test.js diff --git a/src/ol/geom/flat/areaflatgeom.js b/src/ol/geom/flat/areaflatgeom.js new file mode 100644 index 0000000000..54479f0fe1 --- /dev/null +++ b/src/ol/geom/flat/areaflatgeom.js @@ -0,0 +1,66 @@ +goog.provide('ol.geom.flat.area'); + +goog.require('ol.geom.flat'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @return {number} Area. + */ +ol.geom.flat.area.linearRing = function(flatCoordinates, offset, end, stride) { + var twiceArea = 0; + var x1 = flatCoordinates[end - stride]; + var y1 = flatCoordinates[end - stride + 1]; + for (; offset < end; offset += stride) { + var x2 = flatCoordinates[offset]; + var y2 = flatCoordinates[offset + 1]; + twiceArea += y1 * x2 - x1 * y2; + x1 = x2; + y1 = y2; + } + return twiceArea / 2; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} ends Ends. + * @param {number} stride Stride. + * @return {number} Area. + */ +ol.geom.flat.area.linearRings = + function(flatCoordinates, offset, ends, stride) { + var area = 0; + var i, ii; + for (i = 0, ii = ends.length; i < ii; ++i) { + var end = ends[i]; + area += ol.geom.flat.area.linearRing(flatCoordinates, offset, end, stride); + offset = end; + } + return area; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>} endss Endss. + * @param {number} stride Stride. + * @return {number} Area. + */ +ol.geom.flat.area.linearRingss = + function(flatCoordinates, offset, endss, stride) { + var area = 0; + var i, ii; + for (i = 0, ii = endss.length; i < ii; ++i) { + var ends = endss[i]; + area += + ol.geom.flat.area.linearRings(flatCoordinates, offset, ends, stride); + offset = ends[ends.length - 1]; + } + return area; +}; diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index 42df2388b7..4e69680230 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -198,28 +198,6 @@ ol.geom.flat.inflateCoordinatesss = }; -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @return {number} Area. - */ -ol.geom.flat.linearRingArea = function(flatCoordinates, offset, end, stride) { - var twiceArea = 0; - var x1 = flatCoordinates[end - stride]; - var y1 = flatCoordinates[end - stride + 1]; - for (; offset < end; offset += stride) { - var x2 = flatCoordinates[offset]; - var y2 = flatCoordinates[offset + 1]; - twiceArea += y1 * x2 - x1 * y2; - x1 = x2; - y1 = y2; - } - return twiceArea / 2; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. @@ -275,25 +253,6 @@ ol.geom.flat.linearRingIsClockwise = }; -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.} ends Ends. - * @param {number} stride Stride. - * @return {number} Area. - */ -ol.geom.flat.linearRingsArea = function(flatCoordinates, offset, ends, stride) { - var area = 0; - var i, ii; - for (i = 0, ii = ends.length; i < ii; ++i) { - var end = ends[i]; - area += ol.geom.flat.linearRingArea(flatCoordinates, offset, end, stride); - offset = end; - } - return area; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. @@ -434,26 +393,6 @@ ol.geom.flat.linearRingssAreOriented = }; -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>} endss Endss. - * @param {number} stride Stride. - * @return {number} Area. - */ -ol.geom.flat.linearRingssArea = - function(flatCoordinates, offset, endss, stride) { - var area = 0; - var i, ii; - for (i = 0, ii = endss.length; i < ii; ++i) { - var ends = endss[i]; - area += ol.geom.flat.linearRingsArea(flatCoordinates, offset, ends, stride); - offset = ends[ends.length - 1]; - } - return area; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. diff --git a/src/ol/geom/linearring.js b/src/ol/geom/linearring.js index 19e7214b9e..0b6158bbdb 100644 --- a/src/ol/geom/linearring.js +++ b/src/ol/geom/linearring.js @@ -4,6 +4,7 @@ goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.simplify'); @@ -73,7 +74,7 @@ ol.geom.LinearRing.prototype.closestPointXY = * @todo stability experimental */ ol.geom.LinearRing.prototype.getArea = function() { - return ol.geom.flat.linearRingArea( + return ol.geom.flat.area.linearRing( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride); }; diff --git a/src/ol/geom/multipolygon.js b/src/ol/geom/multipolygon.js index 9cc90a7a25..d37817582e 100644 --- a/src/ol/geom/multipolygon.js +++ b/src/ol/geom/multipolygon.js @@ -8,6 +8,7 @@ goog.require('ol.geom.MultiPoint'); goog.require('ol.geom.Polygon'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.simplify'); @@ -142,7 +143,7 @@ ol.geom.MultiPolygon.prototype.containsXY = function(x, y) { * @todo stability experimental */ ol.geom.MultiPolygon.prototype.getArea = function() { - return ol.geom.flat.linearRingssArea( + return ol.geom.flat.area.linearRingss( this.getOrientedFlatCoordinates(), 0, this.endss_, this.stride); }; diff --git a/src/ol/geom/polygon.js b/src/ol/geom/polygon.js index 3507cc5152..85c9e24cec 100644 --- a/src/ol/geom/polygon.js +++ b/src/ol/geom/polygon.js @@ -8,6 +8,7 @@ goog.require('ol.geom.LinearRing'); goog.require('ol.geom.Point'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.simplify'); @@ -132,7 +133,7 @@ ol.geom.Polygon.prototype.containsXY = function(x, y) { * @todo stability experimental */ ol.geom.Polygon.prototype.getArea = function() { - return ol.geom.flat.linearRingsArea( + return ol.geom.flat.area.linearRings( this.getOrientedFlatCoordinates(), 0, this.ends_, this.stride); }; diff --git a/test/spec/ol/geom/flat/areaflatgeom.test.js b/test/spec/ol/geom/flat/areaflatgeom.test.js new file mode 100644 index 0000000000..18a87e28d3 --- /dev/null +++ b/test/spec/ol/geom/flat/areaflatgeom.test.js @@ -0,0 +1,33 @@ +goog.provide('ol.test.geom.flat.area'); + +describe('ol.geom.flat.area', function() { + + describe('ol.geom.flat.area.linearRing', function() { + + it('calcaultes the area of a triangle', function() { + var area = ol.geom.flat.area.linearRing([0, 0, 0.5, 1, 1, 0], 0, 6, 2); + expect(area).to.be(0.5); + }); + + it('calculates the area of a unit square', function() { + var area = + ol.geom.flat.area.linearRing([0, 0, 0, 1, 1, 1, 1, 0], 0, 8, 2); + expect(area).to.be(1); + }); + + }); + + describe('ol.geom.flat.area.linearRings', function() { + + it('calculates the area with holes', function() { + var area = ol.geom.flat.area.linearRings( + [0, 0, 0, 3, 3, 3, 3, 0, 1, 1, 2, 1, 2, 2, 1, 2], 0, [8, 16], 2); + expect(area).to.be(8); + }); + + }); + +}); + +goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.area'); diff --git a/test/spec/ol/geom/flatgeom.test.js b/test/spec/ol/geom/flatgeom.test.js index 75328a9660..1ffa7ed5bc 100644 --- a/test/spec/ol/geom/flatgeom.test.js +++ b/test/spec/ol/geom/flatgeom.test.js @@ -83,19 +83,6 @@ describe('ol.geom.flat', function() { expect(coordinatess).to.eql([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]); }); - }); - describe('ol.geom.flat.linearRingArea', function() { - - it('calcaultes the area of a triangle', function() { - var area = ol.geom.flat.linearRingArea([0, 0, 0.5, 1, 1, 0], 0, 6, 2); - expect(area).to.be(0.5); - }); - - it('calculates the area of a unit square', function() { - var area = ol.geom.flat.linearRingArea([0, 0, 0, 1, 1, 1, 1, 0], 0, 8, 2); - expect(area).to.be(1); - }); - }); describe('ol.geom.flat.linearRingIsClockwise', function() { @@ -116,16 +103,6 @@ describe('ol.geom.flat', function() { }); - describe('ol.geom.flat.linearRingsArea', function() { - - it('calculates the area with holes', function() { - var area = ol.geom.flat.linearRingsArea( - [0, 0, 0, 3, 3, 3, 3, 0, 1, 1, 2, 1, 2, 2, 1, 2], 0, [8, 16], 2); - expect(area).to.be(8); - }); - - }); - describe('ol.geom.flat.reverseCoordinates', function() { describe('with a stride of 2', function() { From 1363ce3a5852cf9651663e23f52e466dcccb27b5 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 12 Mar 2014 12:27:12 +0100 Subject: [PATCH 08/17] Factor out ol.geom.flat.orient --- src/ol/geom/flat/orientflatgeom.js | 115 ++++++++++++++++++ src/ol/geom/flatgeom.js | 112 ----------------- src/ol/geom/multipolygon.js | 6 +- src/ol/geom/polygon.js | 8 +- test/spec/ol/geom/flat/orientflatgeom.test.js | 26 ++++ test/spec/ol/geom/flatgeom.test.js | 18 --- 6 files changed, 150 insertions(+), 135 deletions(-) create mode 100644 src/ol/geom/flat/orientflatgeom.js create mode 100644 test/spec/ol/geom/flat/orientflatgeom.test.js diff --git a/src/ol/geom/flat/orientflatgeom.js b/src/ol/geom/flat/orientflatgeom.js new file mode 100644 index 0000000000..06fc092e2c --- /dev/null +++ b/src/ol/geom/flat/orientflatgeom.js @@ -0,0 +1,115 @@ +goog.provide('ol.geom.flat.orient'); + +goog.require('ol.geom.flat'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @return {boolean} Is clockwise. + */ +ol.geom.flat.orient.linearRingIsClockwise = + function(flatCoordinates, offset, end, stride) { + // http://tinyurl.com/clockwise-method + // https://github.com/OSGeo/gdal/blob/trunk/gdal/ogr/ogrlinearring.cpp + var edge = 0; + var x1 = flatCoordinates[end - stride]; + var y1 = flatCoordinates[end - stride + 1]; + for (; offset < end; offset += stride) { + var x2 = flatCoordinates[offset]; + var y2 = flatCoordinates[offset + 1]; + edge += (x2 - x1) * (y2 + y1); + x1 = x2; + y1 = y2; + } + return edge > 0; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} ends Ends. + * @param {number} stride Stride. + * @return {boolean} `true` if all rings are correctly oriented, `false` + * otherwise. + */ +ol.geom.flat.orient.linearRingsAreOriented = + function(flatCoordinates, offset, ends, stride) { + var i, ii; + for (i = 0, ii = ends.length; i < ii; ++i) { + var end = ends[i]; + var isClockwise = ol.geom.flat.orient.linearRingIsClockwise( + flatCoordinates, offset, end, stride); + if (i === 0 ? !isClockwise : isClockwise) { + return false; + } + offset = end; + } + return true; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>} endss Endss. + * @param {number} stride Stride. + * @return {boolean} `true` if all rings are correctly oriented, `false` + * otherwise. + */ +ol.geom.flat.linearRingssAreOriented = + function(flatCoordinates, offset, endss, stride) { + var i, ii; + for (i = 0, ii = endss.length; i < ii; ++i) { + if (!ol.geom.flat.orient.linearRingsAreOriented( + flatCoordinates, offset, endss[i], stride)) { + return false; + } + } + return true; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} ends Ends. + * @param {number} stride Stride. + * @return {number} End. + */ +ol.geom.flat.orient.orientLinearRings = + function(flatCoordinates, offset, ends, stride) { + var i, ii; + for (i = 0, ii = ends.length; i < ii; ++i) { + var end = ends[i]; + var isClockwise = ol.geom.flat.orient.linearRingIsClockwise( + flatCoordinates, offset, end, stride); + var reverse = i === 0 ? !isClockwise : isClockwise; + if (reverse) { + ol.geom.flat.reverseCoordinates(flatCoordinates, offset, end, stride); + } + offset = end; + } + return offset; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>} endss Endss. + * @param {number} stride Stride. + * @return {number} End. + */ +ol.geom.flat.orient.orientLinearRingss = + function(flatCoordinates, offset, endss, stride) { + var i, ii; + for (i = 0, ii = endss.length; i < ii; ++i) { + offset = ol.geom.flat.orient.orientLinearRings( + flatCoordinates, offset, endss[i], stride); + } + return offset; +}; diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index 4e69680230..917f4b6b3d 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -228,31 +228,6 @@ ol.geom.flat.linearRingContainsXY = }; -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @return {boolean} Is clockwise. - */ -ol.geom.flat.linearRingIsClockwise = - function(flatCoordinates, offset, end, stride) { - // http://tinyurl.com/clockwise-method - // https://github.com/OSGeo/gdal/blob/trunk/gdal/ogr/ogrlinearring.cpp - var edge = 0; - var x1 = flatCoordinates[end - stride]; - var y1 = flatCoordinates[end - stride + 1]; - for (; offset < end; offset += stride) { - var x2 = flatCoordinates[offset]; - var y2 = flatCoordinates[offset + 1]; - edge += (x2 - x1) * (y2 + y1); - x1 = x2; - y1 = y2; - } - return edge > 0; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. @@ -348,51 +323,6 @@ ol.geom.flat.linearRingsGetInteriorPoint = function(flatCoordinates, offset, }; -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.} ends Ends. - * @param {number} stride Stride. - * @return {boolean} `true` if all rings are correctly oriented, `false` - * otherwise. - */ -ol.geom.flat.linearRingsAreOriented = - function(flatCoordinates, offset, ends, stride) { - var i, ii; - for (i = 0, ii = ends.length; i < ii; ++i) { - var end = ends[i]; - var isClockwise = ol.geom.flat.linearRingIsClockwise( - flatCoordinates, offset, end, stride); - if (i === 0 ? !isClockwise : isClockwise) { - return false; - } - offset = end; - } - return true; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>} endss Endss. - * @param {number} stride Stride. - * @return {boolean} `true` if all rings are correctly oriented, `false` - * otherwise. - */ -ol.geom.flat.linearRingssAreOriented = - function(flatCoordinates, offset, endss, stride) { - var i, ii; - for (i = 0, ii = endss.length; i < ii; ++i) { - if (!ol.geom.flat.linearRingsAreOriented( - flatCoordinates, offset, endss[i], stride)) { - return false; - } - } - return true; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. @@ -467,48 +397,6 @@ ol.geom.flat.linearRingssGetFlatCenters = }; -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.} ends Ends. - * @param {number} stride Stride. - * @return {number} End. - */ -ol.geom.flat.orientLinearRings = - function(flatCoordinates, offset, ends, stride) { - var i, ii; - for (i = 0, ii = ends.length; i < ii; ++i) { - var end = ends[i]; - var isClockwise = ol.geom.flat.linearRingIsClockwise( - flatCoordinates, offset, end, stride); - var reverse = i === 0 ? !isClockwise : isClockwise; - if (reverse) { - ol.geom.flat.reverseCoordinates(flatCoordinates, offset, end, stride); - } - offset = end; - } - return offset; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>} endss Endss. - * @param {number} stride Stride. - * @return {number} End. - */ -ol.geom.flat.orientLinearRingss = - function(flatCoordinates, offset, endss, stride) { - var i, ii; - for (i = 0, ii = endss.length; i < ii; ++i) { - offset = ol.geom.flat.orientLinearRings( - flatCoordinates, offset, endss[i], stride); - } - return offset; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. diff --git a/src/ol/geom/multipolygon.js b/src/ol/geom/multipolygon.js index d37817582e..7000aef599 100644 --- a/src/ol/geom/multipolygon.js +++ b/src/ol/geom/multipolygon.js @@ -10,6 +10,7 @@ goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.closest'); +goog.require('ol.geom.flat.orient'); goog.require('ol.geom.flat.simplify'); @@ -204,8 +205,9 @@ ol.geom.MultiPolygon.prototype.getOrientedFlatCoordinates = function() { this.orientedFlatCoordinates_ = flatCoordinates; } else { this.orientedFlatCoordinates_ = flatCoordinates.slice(); - this.orientedFlatCoordinates_.length = ol.geom.flat.orientLinearRingss( - this.orientedFlatCoordinates_, 0, this.endss_, this.stride); + this.orientedFlatCoordinates_.length = + ol.geom.flat.orient.orientLinearRingss( + this.orientedFlatCoordinates_, 0, this.endss_, this.stride); } this.orientedRevision_ = this.getRevision(); } diff --git a/src/ol/geom/polygon.js b/src/ol/geom/polygon.js index 85c9e24cec..5966f850bb 100644 --- a/src/ol/geom/polygon.js +++ b/src/ol/geom/polygon.js @@ -10,6 +10,7 @@ goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.closest'); +goog.require('ol.geom.flat.orient'); goog.require('ol.geom.flat.simplify'); @@ -223,13 +224,14 @@ ol.geom.Polygon.prototype.getLinearRings = function() { ol.geom.Polygon.prototype.getOrientedFlatCoordinates = function() { if (this.orientedRevision_ != this.getRevision()) { var flatCoordinates = this.flatCoordinates; - if (ol.geom.flat.linearRingsAreOriented( + if (ol.geom.flat.orient.linearRingsAreOriented( flatCoordinates, 0, this.ends_, this.stride)) { this.orientedFlatCoordinates_ = flatCoordinates; } else { this.orientedFlatCoordinates_ = flatCoordinates.slice(); - this.orientedFlatCoordinates_.length = ol.geom.flat.orientLinearRings( - this.orientedFlatCoordinates_, 0, this.ends_, this.stride); + this.orientedFlatCoordinates_.length = + ol.geom.flat.orient.orientLinearRings( + this.orientedFlatCoordinates_, 0, this.ends_, this.stride); } this.orientedRevision_ = this.getRevision(); } diff --git a/test/spec/ol/geom/flat/orientflatgeom.test.js b/test/spec/ol/geom/flat/orientflatgeom.test.js new file mode 100644 index 0000000000..010d25d33b --- /dev/null +++ b/test/spec/ol/geom/flat/orientflatgeom.test.js @@ -0,0 +1,26 @@ +goog.provide('ol.test.geom.flat.orient'); + +describe('ol.geom.flat.orient', function() { + + describe('ol.geom.flat.orient.linearRingIsClockwise', function() { + + it('identifies clockwise rings', function() { + var flatCoordinates = [0, 1, 1, 4, 4, 3, 3, 0]; + var isClockwise = ol.geom.flat.orient.linearRingIsClockwise( + flatCoordinates, 0, flatCoordinates.length, 2); + expect(isClockwise).to.be(true); + }); + + it('identifies anti-clockwise rings', function() { + var flatCoordinates = [2, 2, 3, 2, 3, 3, 2, 3]; + var isClockwise = ol.geom.flat.orient.linearRingIsClockwise( + flatCoordinates, 0, flatCoordinates.length, 2); + expect(isClockwise).to.be(false); + }); + + }); + +}); + +goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.orient'); diff --git a/test/spec/ol/geom/flatgeom.test.js b/test/spec/ol/geom/flatgeom.test.js index 1ffa7ed5bc..41f4a7f9cd 100644 --- a/test/spec/ol/geom/flatgeom.test.js +++ b/test/spec/ol/geom/flatgeom.test.js @@ -85,24 +85,6 @@ describe('ol.geom.flat', function() { }); - describe('ol.geom.flat.linearRingIsClockwise', function() { - - it('identifies clockwise rings', function() { - var flatCoordinates = [0, 1, 1, 4, 4, 3, 3, 0]; - var isClockwise = ol.geom.flat.linearRingIsClockwise( - flatCoordinates, 0, flatCoordinates.length, 2); - expect(isClockwise).to.be(true); - }); - - it('identifies anti-clockwise rings', function() { - var flatCoordinates = [2, 2, 3, 2, 3, 3, 2, 3]; - var isClockwise = ol.geom.flat.linearRingIsClockwise( - flatCoordinates, 0, flatCoordinates.length, 2); - expect(isClockwise).to.be(false); - }); - - }); - describe('ol.geom.flat.reverseCoordinates', function() { describe('with a stride of 2', function() { From d525b43d060ccc483fd28849909c32720fe4a589 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 12 Mar 2014 12:43:40 +0100 Subject: [PATCH 09/17] Factor out ol.geom.flat.contains and interiorpoint --- src/ol/geom/flat/containsflatgeom.js | 92 +++++++++++ src/ol/geom/flat/interiorpointflatgeom.js | 93 ++++++++++++ src/ol/geom/flatgeom.js | 176 ---------------------- src/ol/geom/multipolygon.js | 6 +- src/ol/geom/polygon.js | 6 +- 5 files changed, 193 insertions(+), 180 deletions(-) create mode 100644 src/ol/geom/flat/containsflatgeom.js create mode 100644 src/ol/geom/flat/interiorpointflatgeom.js diff --git a/src/ol/geom/flat/containsflatgeom.js b/src/ol/geom/flat/containsflatgeom.js new file mode 100644 index 0000000000..e0d43c4da4 --- /dev/null +++ b/src/ol/geom/flat/containsflatgeom.js @@ -0,0 +1,92 @@ +goog.provide('ol.geom.flat.contains'); + +goog.require('goog.asserts'); +goog.require('ol.geom.flat'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @param {number} x X. + * @param {number} y Y. + * @return {boolean} Contains (x, y). + */ +ol.geom.flat.contains.linearRingContainsXY = + function(flatCoordinates, offset, end, stride, x, y) { + // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html + var contains = false; + var x1 = flatCoordinates[end - stride]; + var y1 = flatCoordinates[end - stride + 1]; + for (; offset < end; offset += stride) { + var x2 = flatCoordinates[offset]; + var y2 = flatCoordinates[offset + 1]; + var intersect = ((y1 > y) != (y2 > y)) && + (x < (x2 - x1) * (y - y1) / (y2 - y1) + x1); + if (intersect) { + contains = !contains; + } + x1 = x2; + y1 = y2; + } + return contains; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} ends Ends. + * @param {number} stride Stride. + * @param {number} x X. + * @param {number} y Y. + * @return {boolean} Contains (x, y). + */ +ol.geom.flat.contains.linearRingsContainsXY = + function(flatCoordinates, offset, ends, stride, x, y) { + goog.asserts.assert(ends.length > 0); + if (ends.length === 0) { + return false; + } + if (!ol.geom.flat.contains.linearRingContainsXY( + flatCoordinates, offset, ends[0], stride, x, y)) { + return false; + } + var i, ii; + for (i = 1, ii = ends.length; i < ii; ++i) { + if (ol.geom.flat.contains.linearRingContainsXY( + flatCoordinates, ends[i - 1], ends[i], stride, x, y)) { + return false; + } + } + return true; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>} endss Endss. + * @param {number} stride Stride. + * @param {number} x X. + * @param {number} y Y. + * @return {boolean} Contains (x, y). + */ +ol.geom.flat.contains.linearRingssContainsXY = + function(flatCoordinates, offset, endss, stride, x, y) { + goog.asserts.assert(endss.length > 0); + if (endss.length === 0) { + return false; + } + var i, ii; + for (i = 0, ii = endss.length; i < ii; ++i) { + var ends = endss[i]; + if (ol.geom.flat.contains.linearRingsContainsXY( + flatCoordinates, offset, ends, stride, x, y)) { + return true; + } + offset = ends[ends.length - 1]; + } + return false; +}; diff --git a/src/ol/geom/flat/interiorpointflatgeom.js b/src/ol/geom/flat/interiorpointflatgeom.js new file mode 100644 index 0000000000..cf7144c6dd --- /dev/null +++ b/src/ol/geom/flat/interiorpointflatgeom.js @@ -0,0 +1,93 @@ +goog.provide('ol.geom.flat.interiorpoint'); + +goog.require('goog.asserts'); +goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.contains'); + + +/** + * Calculates a point that is likely to lie in the interior of the linear rings. + * Inspired by JTS's com.vividsolutions.jts.geom.Geometry#getInteriorPoint. + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} ends Ends. + * @param {number} stride Stride. + * @param {Array.} flatCenters Flat centers. + * @param {number} flatCentersOffset Flat center offset. + * @param {Array.=} opt_dest Destination. + * @return {Array.} Destination. + */ +ol.geom.flat.interiorpoint.linearRings = function(flatCoordinates, offset, + ends, stride, flatCenters, flatCentersOffset, opt_dest) { + var i, ii, x, x1, x2, y1, y2; + var y = flatCenters[flatCentersOffset + 1]; + /** @type {Array.} */ + var intersections = []; + // Calculate intersections with the horizontal line + var end = ends[0]; + x1 = flatCoordinates[end - stride]; + y1 = flatCoordinates[end - stride + 1]; + for (i = offset; i < end; i += stride) { + x2 = flatCoordinates[i]; + y2 = flatCoordinates[i + 1]; + if ((y <= y1 && y2 <= y) || (y1 <= y && y <= y2)) { + x = (y - y1) / (y2 - y1) * (x2 - x1) + x1; + intersections.push(x); + } + x1 = x2; + y1 = y2; + } + // Find the longest segment of the horizontal line that has its center point + // inside the linear ring. + var pointX = NaN; + var maxSegmentLength = -Infinity; + intersections.sort(); + x1 = intersections[0]; + for (i = 1, ii = intersections.length; i < ii; ++i) { + x2 = intersections[i]; + var segmentLength = Math.abs(x2 - x1); + if (segmentLength > maxSegmentLength) { + x = (x1 + x2) / 2; + if (ol.geom.flat.contains.linearRingsContainsXY( + flatCoordinates, offset, ends, stride, x, y)) { + pointX = x; + maxSegmentLength = segmentLength; + } + } + x1 = x2; + } + if (isNaN(pointX)) { + // There is no horizontal line that has its center point inside the linear + // ring. Use the center of the the linear ring's extent. + pointX = flatCenters[flatCentersOffset]; + } + if (goog.isDef(opt_dest)) { + opt_dest.push(pointX, y); + return opt_dest; + } else { + return [pointX, y]; + } +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>} endss Endss. + * @param {number} stride Stride. + * @param {Array.} flatCenters Flat centers. + * @return {Array.} Interior points. + */ +ol.geom.flat.interiorpoint.linearRingss = + function(flatCoordinates, offset, endss, stride, flatCenters) { + goog.asserts.assert(2 * endss.length == flatCenters.length); + var interiorPoints = []; + var i, ii; + for (i = 0, ii = endss.length; i < ii; ++i) { + var ends = endss[i]; + interiorPoints = ol.geom.flat.interiorpoint.linearRings(flatCoordinates, + offset, ends, stride, flatCenters, 2 * i, interiorPoints); + offset = ends[ends.length - 1]; + } + return interiorPoints; +}; diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index 917f4b6b3d..bdcdb53f03 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -198,182 +198,6 @@ ol.geom.flat.inflateCoordinatesss = }; -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @param {number} x X. - * @param {number} y Y. - * @return {boolean} Contains (x, y). - */ -ol.geom.flat.linearRingContainsXY = - function(flatCoordinates, offset, end, stride, x, y) { - // http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html - var contains = false; - var x1 = flatCoordinates[end - stride]; - var y1 = flatCoordinates[end - stride + 1]; - for (; offset < end; offset += stride) { - var x2 = flatCoordinates[offset]; - var y2 = flatCoordinates[offset + 1]; - var intersect = ((y1 > y) != (y2 > y)) && - (x < (x2 - x1) * (y - y1) / (y2 - y1) + x1); - if (intersect) { - contains = !contains; - } - x1 = x2; - y1 = y2; - } - return contains; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.} ends Ends. - * @param {number} stride Stride. - * @param {number} x X. - * @param {number} y Y. - * @return {boolean} Contains (x, y). - */ -ol.geom.flat.linearRingsContainsXY = - function(flatCoordinates, offset, ends, stride, x, y) { - goog.asserts.assert(ends.length > 0); - if (ends.length === 0) { - return false; - } - if (!ol.geom.flat.linearRingContainsXY( - flatCoordinates, offset, ends[0], stride, x, y)) { - return false; - } - var i, ii; - for (i = 1, ii = ends.length; i < ii; ++i) { - if (ol.geom.flat.linearRingContainsXY( - flatCoordinates, ends[i - 1], ends[i], stride, x, y)) { - return false; - } - } - return true; -}; - - -/** - * Calculates a point that is likely to lie in the interior of the linear rings. - * Inspired by JTS's com.vividsolutions.jts.geom.Geometry#getInteriorPoint. - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.} ends Ends. - * @param {number} stride Stride. - * @param {Array.} flatCenters Flat centers. - * @param {number} flatCentersOffset Flat center offset. - * @param {Array.=} opt_dest Destination. - * @return {Array.} Destination. - */ -ol.geom.flat.linearRingsGetInteriorPoint = function(flatCoordinates, offset, - ends, stride, flatCenters, flatCentersOffset, opt_dest) { - var i, ii, x, x1, x2, y1, y2; - var y = flatCenters[flatCentersOffset + 1]; - /** @type {Array.} */ - var intersections = []; - // Calculate intersections with the horizontal line - var end = ends[0]; - x1 = flatCoordinates[end - stride]; - y1 = flatCoordinates[end - stride + 1]; - for (i = offset; i < end; i += stride) { - x2 = flatCoordinates[i]; - y2 = flatCoordinates[i + 1]; - if ((y <= y1 && y2 <= y) || (y1 <= y && y <= y2)) { - x = (y - y1) / (y2 - y1) * (x2 - x1) + x1; - intersections.push(x); - } - x1 = x2; - y1 = y2; - } - // Find the longest segment of the horizontal line that has its center point - // inside the linear ring. - var pointX = NaN; - var maxSegmentLength = -Infinity; - intersections.sort(); - x1 = intersections[0]; - for (i = 1, ii = intersections.length; i < ii; ++i) { - x2 = intersections[i]; - var segmentLength = Math.abs(x2 - x1); - if (segmentLength > maxSegmentLength) { - x = (x1 + x2) / 2; - if (ol.geom.flat.linearRingsContainsXY( - flatCoordinates, offset, ends, stride, x, y)) { - pointX = x; - maxSegmentLength = segmentLength; - } - } - x1 = x2; - } - if (isNaN(pointX)) { - // There is no horizontal line that has its center point inside the linear - // ring. Use the center of the the linear ring's extent. - pointX = flatCenters[flatCentersOffset]; - } - if (goog.isDef(opt_dest)) { - opt_dest.push(pointX, y); - return opt_dest; - } else { - return [pointX, y]; - } -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>} endss Endss. - * @param {number} stride Stride. - * @param {number} x X. - * @param {number} y Y. - * @return {boolean} Contains (x, y). - */ -ol.geom.flat.linearRingssContainsXY = - function(flatCoordinates, offset, endss, stride, x, y) { - goog.asserts.assert(endss.length > 0); - if (endss.length === 0) { - return false; - } - var i, ii; - for (i = 0, ii = endss.length; i < ii; ++i) { - var ends = endss[i]; - if (ol.geom.flat.linearRingsContainsXY( - flatCoordinates, offset, ends, stride, x, y)) { - return true; - } - offset = ends[ends.length - 1]; - } - return false; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>} endss Endss. - * @param {number} stride Stride. - * @param {Array.} flatCenters Flat centers. - * @return {Array.} Interior points. - */ -ol.geom.flat.linearRingssGetInteriorPoints = - function(flatCoordinates, offset, endss, stride, flatCenters) { - goog.asserts.assert(2 * endss.length == flatCenters.length); - var interiorPoints = []; - var i, ii; - for (i = 0, ii = endss.length; i < ii; ++i) { - var ends = endss[i]; - interiorPoints = ol.geom.flat.linearRingsGetInteriorPoint(flatCoordinates, - offset, ends, stride, flatCenters, 2 * i, interiorPoints); - offset = ends[ends.length - 1]; - } - return interiorPoints; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. diff --git a/src/ol/geom/multipolygon.js b/src/ol/geom/multipolygon.js index 7000aef599..4d4343c033 100644 --- a/src/ol/geom/multipolygon.js +++ b/src/ol/geom/multipolygon.js @@ -10,6 +10,8 @@ goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.closest'); +goog.require('ol.geom.flat.contains'); +goog.require('ol.geom.flat.interiorpoint'); goog.require('ol.geom.flat.orient'); goog.require('ol.geom.flat.simplify'); @@ -134,7 +136,7 @@ ol.geom.MultiPolygon.prototype.closestPointXY = * @inheritDoc */ ol.geom.MultiPolygon.prototype.containsXY = function(x, y) { - return ol.geom.flat.linearRingssContainsXY( + return ol.geom.flat.contains.linearRingssContainsXY( this.getOrientedFlatCoordinates(), 0, this.endss_, this.stride, x, y); }; @@ -174,7 +176,7 @@ ol.geom.MultiPolygon.prototype.getFlatInteriorPoints = function() { if (this.flatInteriorPointsRevision_ != this.getRevision()) { var flatCenters = ol.geom.flat.linearRingssGetFlatCenters( this.flatCoordinates, 0, this.endss_, this.stride); - this.flatInteriorPoints_ = ol.geom.flat.linearRingssGetInteriorPoints( + this.flatInteriorPoints_ = ol.geom.flat.interiorpoint.linearRingss( this.getOrientedFlatCoordinates(), 0, this.endss_, this.stride, flatCenters); this.flatInteriorPointsRevision_ = this.getRevision(); diff --git a/src/ol/geom/polygon.js b/src/ol/geom/polygon.js index 5966f850bb..6c5b95b871 100644 --- a/src/ol/geom/polygon.js +++ b/src/ol/geom/polygon.js @@ -10,6 +10,8 @@ goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.closest'); +goog.require('ol.geom.flat.contains'); +goog.require('ol.geom.flat.interiorpoint'); goog.require('ol.geom.flat.orient'); goog.require('ol.geom.flat.simplify'); @@ -124,7 +126,7 @@ ol.geom.Polygon.prototype.closestPointXY = * @inheritDoc */ ol.geom.Polygon.prototype.containsXY = function(x, y) { - return ol.geom.flat.linearRingsContainsXY( + return ol.geom.flat.contains.linearRingsContainsXY( this.getOrientedFlatCoordinates(), 0, this.ends_, this.stride, x, y); }; @@ -163,7 +165,7 @@ ol.geom.Polygon.prototype.getEnds = function() { ol.geom.Polygon.prototype.getFlatInteriorPoint = function() { if (this.flatInteriorPointRevision_ != this.getRevision()) { var flatCenter = ol.extent.getCenter(this.getExtent()); - this.flatInteriorPoint_ = ol.geom.flat.linearRingsGetInteriorPoint( + this.flatInteriorPoint_ = ol.geom.flat.interiorpoint.linearRings( this.getOrientedFlatCoordinates(), 0, this.ends_, this.stride, flatCenter, 0); this.flatInteriorPointRevision_ = this.getRevision(); From 266c43dc782439890f42fe574f7517730f410375 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 12 Mar 2014 12:50:50 +0100 Subject: [PATCH 10/17] Factor out ol.geom.flat.deflate --- src/ol/geom/circle.js | 3 +- src/ol/geom/flat/deflateflatgeom.js | 91 +++++++++++++++++++ src/ol/geom/flatgeom.js | 88 ------------------ src/ol/geom/linearring.js | 3 +- src/ol/geom/linestring.js | 3 +- src/ol/geom/multilinestring.js | 3 +- src/ol/geom/multipoint.js | 3 +- src/ol/geom/multipolygon.js | 3 +- src/ol/geom/point.js | 3 +- src/ol/geom/polygon.js | 3 +- .../spec/ol/geom/flat/deflateflatgeom.test.js | 40 ++++++++ test/spec/ol/geom/flatgeom.test.js | 32 ------- 12 files changed, 147 insertions(+), 128 deletions(-) create mode 100644 src/ol/geom/flat/deflateflatgeom.js create mode 100644 test/spec/ol/geom/flat/deflateflatgeom.test.js diff --git a/src/ol/geom/circle.js b/src/ol/geom/circle.js index 94d4293deb..1df979d6b0 100644 --- a/src/ol/geom/circle.js +++ b/src/ol/geom/circle.js @@ -5,6 +5,7 @@ goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.deflate'); @@ -174,7 +175,7 @@ ol.geom.Circle.prototype.setCenterAndRadius = } /** @type {Array.} */ var flatCoordinates = this.flatCoordinates; - var offset = ol.geom.flat.deflateCoordinate( + var offset = ol.geom.flat.deflate.coordinate( flatCoordinates, 0, center, this.stride); flatCoordinates[offset++] = flatCoordinates[0] + radius; var i, ii; diff --git a/src/ol/geom/flat/deflateflatgeom.js b/src/ol/geom/flat/deflateflatgeom.js new file mode 100644 index 0000000000..e0aca18d91 --- /dev/null +++ b/src/ol/geom/flat/deflateflatgeom.js @@ -0,0 +1,91 @@ +goog.provide('ol.geom.flat.deflate'); + +goog.require('goog.asserts'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {ol.Coordinate} coordinate Coordinate. + * @param {number} stride Stride. + * @return {number} offset Offset. + */ +ol.geom.flat.deflate.coordinate = + function(flatCoordinates, offset, coordinate, stride) { + goog.asserts.assert(coordinate.length == stride); + var i, ii; + for (i = 0, ii = coordinate.length; i < ii; ++i) { + flatCoordinates[offset++] = coordinate[i]; + } + return offset; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} coordinates Coordinates. + * @param {number} stride Stride. + * @return {number} offset Offset. + */ +ol.geom.flat.deflate.coordinates = + function(flatCoordinates, offset, coordinates, stride) { + var i, ii; + for (i = 0, ii = coordinates.length; i < ii; ++i) { + var coordinate = coordinates[i]; + goog.asserts.assert(coordinate.length == stride); + var j; + for (j = 0; j < stride; ++j) { + flatCoordinates[offset++] = coordinate[j]; + } + } + return offset; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>} coordinatess Coordinatess. + * @param {number} stride Stride. + * @param {Array.=} opt_ends Ends. + * @return {Array.} Ends. + */ +ol.geom.flat.deflate.coordinatess = + function(flatCoordinates, offset, coordinatess, stride, opt_ends) { + var ends = goog.isDef(opt_ends) ? opt_ends : []; + var i = 0; + var j, jj; + for (j = 0, jj = coordinatess.length; j < jj; ++j) { + var end = ol.geom.flat.deflate.coordinates( + flatCoordinates, offset, coordinatess[j], stride); + ends[i++] = end; + offset = end; + } + ends.length = i; + return ends; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>>} coordinatesss Coordinatesss. + * @param {number} stride Stride. + * @param {Array.>=} opt_endss Endss. + * @return {Array.>} Endss. + */ +ol.geom.flat.deflate.coordinatesss = + function(flatCoordinates, offset, coordinatesss, stride, opt_endss) { + var endss = goog.isDef(opt_endss) ? opt_endss : []; + var i = 0; + var j, jj; + for (j = 0, jj = coordinatesss.length; j < jj; ++j) { + var ends = ol.geom.flat.deflate.coordinatess( + flatCoordinates, offset, coordinatesss[j], stride, endss[i]); + endss[i++] = ends; + offset = ends[ends.length - 1]; + } + endss.length = i; + return endss; +}; diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index bdcdb53f03..9524c90376 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -6,94 +6,6 @@ goog.require('goog.vec.Mat4'); goog.require('ol.extent'); -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {ol.Coordinate} coordinate Coordinate. - * @param {number} stride Stride. - * @return {number} offset Offset. - */ -ol.geom.flat.deflateCoordinate = - function(flatCoordinates, offset, coordinate, stride) { - goog.asserts.assert(coordinate.length == stride); - var i, ii; - for (i = 0, ii = coordinate.length; i < ii; ++i) { - flatCoordinates[offset++] = coordinate[i]; - } - return offset; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.} coordinates Coordinates. - * @param {number} stride Stride. - * @return {number} offset Offset. - */ -ol.geom.flat.deflateCoordinates = - function(flatCoordinates, offset, coordinates, stride) { - var i, ii; - for (i = 0, ii = coordinates.length; i < ii; ++i) { - var coordinate = coordinates[i]; - goog.asserts.assert(coordinate.length == stride); - var j; - for (j = 0; j < stride; ++j) { - flatCoordinates[offset++] = coordinate[j]; - } - } - return offset; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>} coordinatess Coordinatess. - * @param {number} stride Stride. - * @param {Array.=} opt_ends Ends. - * @return {Array.} Ends. - */ -ol.geom.flat.deflateCoordinatess = - function(flatCoordinates, offset, coordinatess, stride, opt_ends) { - var ends = goog.isDef(opt_ends) ? opt_ends : []; - var i = 0; - var j, jj; - for (j = 0, jj = coordinatess.length; j < jj; ++j) { - var end = ol.geom.flat.deflateCoordinates( - flatCoordinates, offset, coordinatess[j], stride); - ends[i++] = end; - offset = end; - } - ends.length = i; - return ends; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>>} coordinatesss Coordinatesss. - * @param {number} stride Stride. - * @param {Array.>=} opt_endss Endss. - * @return {Array.>} Endss. - */ -ol.geom.flat.deflateCoordinatesss = - function(flatCoordinates, offset, coordinatesss, stride, opt_endss) { - var endss = goog.isDef(opt_endss) ? opt_endss : []; - var i = 0; - var j, jj; - for (j = 0, jj = coordinatesss.length; j < jj; ++j) { - var ends = ol.geom.flat.deflateCoordinatess( - flatCoordinates, offset, coordinatesss[j], stride, endss[i]); - endss[i++] = ends; - offset = ends[ends.length - 1]; - } - endss.length = i; - return endss; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. diff --git a/src/ol/geom/linearring.js b/src/ol/geom/linearring.js index 0b6158bbdb..e6a101243f 100644 --- a/src/ol/geom/linearring.js +++ b/src/ol/geom/linearring.js @@ -6,6 +6,7 @@ goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.closest'); +goog.require('ol.geom.flat.deflate'); goog.require('ol.geom.flat.simplify'); @@ -127,7 +128,7 @@ ol.geom.LinearRing.prototype.setCoordinates = if (goog.isNull(this.flatCoordinates)) { this.flatCoordinates = []; } - this.flatCoordinates.length = ol.geom.flat.deflateCoordinates( + this.flatCoordinates.length = ol.geom.flat.deflate.coordinates( this.flatCoordinates, 0, coordinates, this.stride); this.dispatchChangeEvent(); } diff --git a/src/ol/geom/linestring.js b/src/ol/geom/linestring.js index 91e621a6b2..b04ec06657 100644 --- a/src/ol/geom/linestring.js +++ b/src/ol/geom/linestring.js @@ -7,6 +7,7 @@ goog.require('ol.geom.GeometryType'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.closest'); +goog.require('ol.geom.flat.deflate'); goog.require('ol.geom.flat.interpolate'); goog.require('ol.geom.flat.length'); goog.require('ol.geom.flat.simplify'); @@ -194,7 +195,7 @@ ol.geom.LineString.prototype.setCoordinates = if (goog.isNull(this.flatCoordinates)) { this.flatCoordinates = []; } - this.flatCoordinates.length = ol.geom.flat.deflateCoordinates( + this.flatCoordinates.length = ol.geom.flat.deflate.coordinates( this.flatCoordinates, 0, coordinates, this.stride); this.dispatchChangeEvent(); } diff --git a/src/ol/geom/multilinestring.js b/src/ol/geom/multilinestring.js index 781ccfb3fe..c35491c2ef 100644 --- a/src/ol/geom/multilinestring.js +++ b/src/ol/geom/multilinestring.js @@ -8,6 +8,7 @@ goog.require('ol.geom.LineString'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.closest'); +goog.require('ol.geom.flat.deflate'); goog.require('ol.geom.flat.interpolate'); goog.require('ol.geom.flat.simplify'); @@ -247,7 +248,7 @@ ol.geom.MultiLineString.prototype.setCoordinates = if (goog.isNull(this.flatCoordinates)) { this.flatCoordinates = []; } - var ends = ol.geom.flat.deflateCoordinatess( + var ends = ol.geom.flat.deflate.coordinatess( this.flatCoordinates, 0, coordinates, this.stride, this.ends_); this.flatCoordinates.length = ends.length === 0 ? 0 : ends[ends.length - 1]; this.dispatchChangeEvent(); diff --git a/src/ol/geom/multipoint.js b/src/ol/geom/multipoint.js index 931761f350..e100073843 100644 --- a/src/ol/geom/multipoint.js +++ b/src/ol/geom/multipoint.js @@ -7,6 +7,7 @@ goog.require('ol.geom.GeometryType'); goog.require('ol.geom.Point'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.deflate'); @@ -145,7 +146,7 @@ ol.geom.MultiPoint.prototype.setCoordinates = if (goog.isNull(this.flatCoordinates)) { this.flatCoordinates = []; } - this.flatCoordinates.length = ol.geom.flat.deflateCoordinates( + this.flatCoordinates.length = ol.geom.flat.deflate.coordinates( this.flatCoordinates, 0, coordinates, this.stride); this.dispatchChangeEvent(); } diff --git a/src/ol/geom/multipolygon.js b/src/ol/geom/multipolygon.js index 4d4343c033..8fb2790e1a 100644 --- a/src/ol/geom/multipolygon.js +++ b/src/ol/geom/multipolygon.js @@ -11,6 +11,7 @@ goog.require('ol.geom.flat'); goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.contains'); +goog.require('ol.geom.flat.deflate'); goog.require('ol.geom.flat.interiorpoint'); goog.require('ol.geom.flat.orient'); goog.require('ol.geom.flat.simplify'); @@ -317,7 +318,7 @@ ol.geom.MultiPolygon.prototype.setCoordinates = if (goog.isNull(this.flatCoordinates)) { this.flatCoordinates = []; } - var endss = ol.geom.flat.deflateCoordinatesss( + var endss = ol.geom.flat.deflate.coordinatesss( this.flatCoordinates, 0, coordinates, this.stride, this.endss_); var lastEnds = endss[endss.length - 1]; this.flatCoordinates.length = lastEnds.length === 0 ? diff --git a/src/ol/geom/point.js b/src/ol/geom/point.js index 3e32ef01cb..69b951dd1b 100644 --- a/src/ol/geom/point.js +++ b/src/ol/geom/point.js @@ -5,6 +5,7 @@ goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.deflate'); @@ -98,7 +99,7 @@ ol.geom.Point.prototype.setCoordinates = function(coordinates, opt_layout) { if (goog.isNull(this.flatCoordinates)) { this.flatCoordinates = []; } - this.flatCoordinates.length = ol.geom.flat.deflateCoordinate( + this.flatCoordinates.length = ol.geom.flat.deflate.coordinate( this.flatCoordinates, 0, coordinates, this.stride); this.dispatchChangeEvent(); } diff --git a/src/ol/geom/polygon.js b/src/ol/geom/polygon.js index 6c5b95b871..7b79f55182 100644 --- a/src/ol/geom/polygon.js +++ b/src/ol/geom/polygon.js @@ -11,6 +11,7 @@ goog.require('ol.geom.flat'); goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.contains'); +goog.require('ol.geom.flat.deflate'); goog.require('ol.geom.flat.interiorpoint'); goog.require('ol.geom.flat.orient'); goog.require('ol.geom.flat.simplify'); @@ -280,7 +281,7 @@ ol.geom.Polygon.prototype.setCoordinates = function(coordinates, opt_layout) { if (goog.isNull(this.flatCoordinates)) { this.flatCoordinates = []; } - var ends = ol.geom.flat.deflateCoordinatess( + var ends = ol.geom.flat.deflate.coordinatess( this.flatCoordinates, 0, coordinates, this.stride, this.ends_); this.flatCoordinates.length = ends.length === 0 ? 0 : ends[ends.length - 1]; this.dispatchChangeEvent(); diff --git a/test/spec/ol/geom/flat/deflateflatgeom.test.js b/test/spec/ol/geom/flat/deflateflatgeom.test.js new file mode 100644 index 0000000000..b29d7cf81c --- /dev/null +++ b/test/spec/ol/geom/flat/deflateflatgeom.test.js @@ -0,0 +1,40 @@ +goog.provide('ol.test.geom.flat.deflate'); + +describe('ol.geom.flat.deflate', function() { + + describe('ol.geom.flat.deflate.coordinates', function() { + + var flatCoordinates; + beforeEach(function() { + flatCoordinates = []; + }); + + it('flattens coordinates', function() { + var offset = ol.geom.flat.deflate.coordinates( + flatCoordinates, 0, [[1, 2], [3, 4]], 2); + expect(offset).to.be(4); + expect(flatCoordinates).to.eql([1, 2, 3, 4]); + }); + + }); + + describe('ol.geom.flat.deflate.coordinatess', function() { + + var flatCoordinates; + beforeEach(function() { + flatCoordinates = []; + }); + + it('flattens arrays of coordinates', function() { + var ends = ol.geom.flat.deflate.coordinatess(flatCoordinates, 0, + [[[1, 2], [3, 4]], [[5, 6], [7, 8]]], 2); + expect(ends).to.eql([4, 8]); + expect(flatCoordinates).to.eql([1, 2, 3, 4, 5, 6, 7, 8]); + }); + + }); + +}); + +goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.deflate'); diff --git a/test/spec/ol/geom/flatgeom.test.js b/test/spec/ol/geom/flatgeom.test.js index 41f4a7f9cd..2d1b102c98 100644 --- a/test/spec/ol/geom/flatgeom.test.js +++ b/test/spec/ol/geom/flatgeom.test.js @@ -3,38 +3,6 @@ goog.provide('ol.test.geom.flat'); describe('ol.geom.flat', function() { - describe('ol.geom.flat.deflateCoordinates', function() { - - var flatCoordinates; - beforeEach(function() { - flatCoordinates = []; - }); - - it('flattens coordinates', function() { - var offset = ol.geom.flat.deflateCoordinates( - flatCoordinates, 0, [[1, 2], [3, 4]], 2); - expect(offset).to.be(4); - expect(flatCoordinates).to.eql([1, 2, 3, 4]); - }); - - }); - - describe('ol.geom.flat.deflateCoordinatess', function() { - - var flatCoordinates; - beforeEach(function() { - flatCoordinates = []; - }); - - it('flattens arrays of coordinates', function() { - var ends = ol.geom.flat.deflateCoordinatess(flatCoordinates, 0, - [[[1, 2], [3, 4]], [[5, 6], [7, 8]]], 2); - expect(ends).to.eql([4, 8]); - expect(flatCoordinates).to.eql([1, 2, 3, 4, 5, 6, 7, 8]); - }); - - }); - describe('ol.geom.flat.flipXY', function() { it('can flip XY coordinates', function() { From 9ca996725e0a9a7e619964a09e521f275a555197 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 12 Mar 2014 12:55:30 +0100 Subject: [PATCH 11/17] Factor out ol.geom.flat.flip --- src/ol/geom/flat/flipflatgeom.js | 37 +++++++++++++++++++ src/ol/geom/flatgeom.js | 35 ------------------ test/spec/ol/geom/flat/flipflatgeom.test.js | 39 +++++++++++++++++++++ test/spec/ol/geom/flatgeom.test.js | 31 ---------------- 4 files changed, 76 insertions(+), 66 deletions(-) create mode 100644 src/ol/geom/flat/flipflatgeom.js create mode 100644 test/spec/ol/geom/flat/flipflatgeom.test.js diff --git a/src/ol/geom/flat/flipflatgeom.js b/src/ol/geom/flat/flipflatgeom.js new file mode 100644 index 0000000000..220f2115ac --- /dev/null +++ b/src/ol/geom/flat/flipflatgeom.js @@ -0,0 +1,37 @@ +goog.provide('ol.geom.flat.flip'); + +goog.require('goog.asserts'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @param {Array.=} opt_dest Destination. + * @param {number=} opt_destOffset Destination offset. + * @return {Array.} Flat coordinates. + */ +ol.geom.flat.flip.flipXY = + function(flatCoordinates, offset, end, stride, opt_dest, opt_destOffset) { + var dest, destOffset; + if (goog.isDef(opt_dest)) { + dest = opt_dest; + destOffset = goog.isDef(opt_destOffset) ? opt_destOffset : 0; + } else { + goog.asserts.assert(!goog.isDef(opt_destOffset)); + dest = []; + destOffset = 0; + } + var j, k; + for (j = offset; j < end; ) { + var x = flatCoordinates[j++]; + dest[destOffset++] = flatCoordinates[j++]; + dest[destOffset++] = x; + for (k = 2; k < stride; ++k) { + dest[destOffset++] = flatCoordinates[j++]; + } + } + dest.length = destOffset; + return dest; +}; diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index 9524c90376..4cd28319b3 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -1,45 +1,10 @@ goog.provide('ol.geom.flat'); goog.require('goog.array'); -goog.require('goog.asserts'); goog.require('goog.vec.Mat4'); goog.require('ol.extent'); -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @param {Array.=} opt_dest Destination. - * @param {number=} opt_destOffset Destination offset. - * @return {Array.} Flat coordinates. - */ -ol.geom.flat.flipXY = - function(flatCoordinates, offset, end, stride, opt_dest, opt_destOffset) { - var dest, destOffset; - if (goog.isDef(opt_dest)) { - dest = opt_dest; - destOffset = goog.isDef(opt_destOffset) ? opt_destOffset : 0; - } else { - goog.asserts.assert(!goog.isDef(opt_destOffset)); - dest = []; - destOffset = 0; - } - var j, k; - for (j = offset; j < end; ) { - var x = flatCoordinates[j++]; - dest[destOffset++] = flatCoordinates[j++]; - dest[destOffset++] = x; - for (k = 2; k < stride; ++k) { - dest[destOffset++] = flatCoordinates[j++]; - } - } - dest.length = destOffset; - return dest; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. diff --git a/test/spec/ol/geom/flat/flipflatgeom.test.js b/test/spec/ol/geom/flat/flipflatgeom.test.js new file mode 100644 index 0000000000..0a47e35c6e --- /dev/null +++ b/test/spec/ol/geom/flat/flipflatgeom.test.js @@ -0,0 +1,39 @@ +goog.provide('ol.test.geom.flat.flip'); + +describe('ol.geom.flat.flip', function() { + + describe('ol.geom.flat.flip.flipXY', function() { + + it('can flip XY coordinates', function() { + var flatCoordinates = ol.geom.flat.flip.flipXY([1, 2, 3, 4], 0, 4, 2); + expect(flatCoordinates).to.eql([2, 1, 4, 3]); + }); + + it('can flip XY coordinates while preserving other dimensions', function() { + var flatCoordinates = ol.geom.flat.flip.flipXY( + [1, 2, 3, 4, 5, 6, 7, 8], 0, 8, 4); + expect(flatCoordinates).to.eql([2, 1, 3, 4, 6, 5, 7, 8]); + }); + + it('can flip XY coordinates in place', function() { + var flatCoordinates = [1, 2, 3, 4]; + expect(ol.geom.flat.flip.flipXY( + flatCoordinates, 0, 4, 2, flatCoordinates)).to.be(flatCoordinates); + expect(flatCoordinates).to.eql([2, 1, 4, 3]); + }); + + it('can flip XY coordinates in place while preserving other dimensions', + function() { + var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9]; + expect(ol.geom.flat.flip.flipXY( + flatCoordinates, 0, 9, 3, flatCoordinates)). + to.be(flatCoordinates); + expect(flatCoordinates).to.eql([2, 1, 3, 5, 4, 6, 8, 7, 9]); + }); + + }); + +}); + +goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.flip'); diff --git a/test/spec/ol/geom/flatgeom.test.js b/test/spec/ol/geom/flatgeom.test.js index 2d1b102c98..927a56e04c 100644 --- a/test/spec/ol/geom/flatgeom.test.js +++ b/test/spec/ol/geom/flatgeom.test.js @@ -3,37 +3,6 @@ goog.provide('ol.test.geom.flat'); describe('ol.geom.flat', function() { - describe('ol.geom.flat.flipXY', function() { - - it('can flip XY coordinates', function() { - var flatCoordinates = ol.geom.flat.flipXY([1, 2, 3, 4], 0, 4, 2); - expect(flatCoordinates).to.eql([2, 1, 4, 3]); - }); - - it('can flip XY coordinates while preserving other dimensions', function() { - var flatCoordinates = ol.geom.flat.flipXY( - [1, 2, 3, 4, 5, 6, 7, 8], 0, 8, 4); - expect(flatCoordinates).to.eql([2, 1, 3, 4, 6, 5, 7, 8]); - }); - - it('can flip XY coordinates in place', function() { - var flatCoordinates = [1, 2, 3, 4]; - expect(ol.geom.flat.flipXY(flatCoordinates, 0, 4, 2, flatCoordinates)). - to.be(flatCoordinates); - expect(flatCoordinates).to.eql([2, 1, 4, 3]); - }); - - it('can flip XY coordinates in place while preserving other dimensions', - function() { - var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9]; - expect(ol.geom.flat.flipXY( - flatCoordinates, 0, 9, 3, flatCoordinates)). - to.be(flatCoordinates); - expect(flatCoordinates).to.eql([2, 1, 3, 5, 4, 6, 8, 7, 9]); - }); - - }); - describe('ol.geom.flat.inflateCoordinates', function() { it('inflates coordinates', function() { From 3541a01aab1b124d011b1a53b922be207b4c3c37 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 12 Mar 2014 12:59:40 +0100 Subject: [PATCH 12/17] Factor out ol.geom.flat.inflate --- src/ol/format/polylineformat.js | 4 +- src/ol/geom/flat/inflateflatgeom.js | 71 +++++++++++++++++++ src/ol/geom/flatgeom.js | 70 ------------------ src/ol/geom/linearring.js | 3 +- src/ol/geom/linestring.js | 3 +- src/ol/geom/multilinestring.js | 3 +- src/ol/geom/multipoint.js | 3 +- src/ol/geom/multipolygon.js | 3 +- src/ol/geom/polygon.js | 3 +- .../spec/ol/geom/flat/inflateflatgeom.test.js | 27 +++++++ test/spec/ol/geom/flatgeom.test.js | 19 ----- 11 files changed, 112 insertions(+), 97 deletions(-) create mode 100644 src/ol/geom/flat/inflateflatgeom.js create mode 100644 test/spec/ol/geom/flat/inflateflatgeom.test.js diff --git a/src/ol/format/polylineformat.js b/src/ol/format/polylineformat.js index a376e827ec..8cdbe55755 100644 --- a/src/ol/format/polylineformat.js +++ b/src/ol/format/polylineformat.js @@ -4,7 +4,7 @@ goog.require('goog.asserts'); goog.require('ol.Feature'); goog.require('ol.format.TextFeature'); goog.require('ol.geom.LineString'); -goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.inflate'); goog.require('ol.proj'); @@ -279,7 +279,7 @@ ol.format.Polyline.prototype.readFeaturesFromText = function(text) { */ ol.format.Polyline.prototype.readGeometryFromText = function(text) { var flatCoordinates = ol.format.Polyline.decodeFlatCoordinates(text, 2); - var coordinates = ol.geom.flat.inflateCoordinates( + var coordinates = ol.geom.flat.inflate.coordinates( flatCoordinates, 0, flatCoordinates.length, 2); return new ol.geom.LineString(coordinates); }; diff --git a/src/ol/geom/flat/inflateflatgeom.js b/src/ol/geom/flat/inflateflatgeom.js new file mode 100644 index 0000000000..53c4620c3f --- /dev/null +++ b/src/ol/geom/flat/inflateflatgeom.js @@ -0,0 +1,71 @@ +goog.provide('ol.geom.flat.inflate'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @param {Array.=} opt_coordinates Coordinates. + * @return {Array.} Coordinates. + */ +ol.geom.flat.inflate.coordinates = + function(flatCoordinates, offset, end, stride, opt_coordinates) { + var coordinates = goog.isDef(opt_coordinates) ? opt_coordinates : []; + var i = 0; + var j; + for (j = offset; j < end; j += stride) { + coordinates[i++] = flatCoordinates.slice(j, j + stride); + } + coordinates.length = i; + return coordinates; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.} ends Ends. + * @param {number} stride Stride. + * @param {Array.>=} opt_coordinatess Coordinatess. + * @return {Array.>} Coordinatess. + */ +ol.geom.flat.inflate.coordinatess = + function(flatCoordinates, offset, ends, stride, opt_coordinatess) { + var coordinatess = goog.isDef(opt_coordinatess) ? opt_coordinatess : []; + var i = 0; + var j, jj; + for (j = 0, jj = ends.length; j < jj; ++j) { + var end = ends[j]; + coordinatess[i++] = ol.geom.flat.inflate.coordinates( + flatCoordinates, offset, end, stride, coordinatess[i]); + offset = end; + } + coordinatess.length = i; + return coordinatess; +}; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>} endss Endss. + * @param {number} stride Stride. + * @param {Array.>>=} opt_coordinatesss + * Coordinatesss. + * @return {Array.>>} Coordinatesss. + */ +ol.geom.flat.inflate.coordinatesss = + function(flatCoordinates, offset, endss, stride, opt_coordinatesss) { + var coordinatesss = goog.isDef(opt_coordinatesss) ? opt_coordinatesss : []; + var i = 0; + var j, jj; + for (j = 0, jj = endss.length; j < jj; ++j) { + var ends = endss[j]; + coordinatesss[i++] = ol.geom.flat.inflate.coordinatess( + flatCoordinates, offset, ends, stride, coordinatesss[i]); + offset = ends[ends.length - 1]; + } + coordinatesss.length = i; + return coordinatesss; +}; diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index 4cd28319b3..d9d7fbe867 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -5,76 +5,6 @@ goog.require('goog.vec.Mat4'); goog.require('ol.extent'); -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - * @param {Array.=} opt_coordinates Coordinates. - * @return {Array.} Coordinates. - */ -ol.geom.flat.inflateCoordinates = - function(flatCoordinates, offset, end, stride, opt_coordinates) { - var coordinates = goog.isDef(opt_coordinates) ? opt_coordinates : []; - var i = 0; - var j; - for (j = offset; j < end; j += stride) { - coordinates[i++] = flatCoordinates.slice(j, j + stride); - } - coordinates.length = i; - return coordinates; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.} ends Ends. - * @param {number} stride Stride. - * @param {Array.>=} opt_coordinatess Coordinatess. - * @return {Array.>} Coordinatess. - */ -ol.geom.flat.inflateCoordinatess = - function(flatCoordinates, offset, ends, stride, opt_coordinatess) { - var coordinatess = goog.isDef(opt_coordinatess) ? opt_coordinatess : []; - var i = 0; - var j, jj; - for (j = 0, jj = ends.length; j < jj; ++j) { - var end = ends[j]; - coordinatess[i++] = ol.geom.flat.inflateCoordinates( - flatCoordinates, offset, end, stride, coordinatess[i]); - offset = end; - } - coordinatess.length = i; - return coordinatess; -}; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>} endss Endss. - * @param {number} stride Stride. - * @param {Array.>>=} opt_coordinatesss - * Coordinatesss. - * @return {Array.>>} Coordinatesss. - */ -ol.geom.flat.inflateCoordinatesss = - function(flatCoordinates, offset, endss, stride, opt_coordinatesss) { - var coordinatesss = goog.isDef(opt_coordinatesss) ? opt_coordinatesss : []; - var i = 0; - var j, jj; - for (j = 0, jj = endss.length; j < jj; ++j) { - var ends = endss[j]; - coordinatesss[i++] = ol.geom.flat.inflateCoordinatess( - flatCoordinates, offset, ends, stride, coordinatesss[i]); - offset = ends[ends.length - 1]; - } - coordinatesss.length = i; - return coordinatesss; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. diff --git a/src/ol/geom/linearring.js b/src/ol/geom/linearring.js index e6a101243f..034b98b520 100644 --- a/src/ol/geom/linearring.js +++ b/src/ol/geom/linearring.js @@ -7,6 +7,7 @@ goog.require('ol.geom.flat'); goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.deflate'); +goog.require('ol.geom.flat.inflate'); goog.require('ol.geom.flat.simplify'); @@ -85,7 +86,7 @@ ol.geom.LinearRing.prototype.getArea = function() { * @todo stability experimental */ ol.geom.LinearRing.prototype.getCoordinates = function() { - return ol.geom.flat.inflateCoordinates( + return ol.geom.flat.inflate.coordinates( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride); }; diff --git a/src/ol/geom/linestring.js b/src/ol/geom/linestring.js index b04ec06657..cc41ed8b38 100644 --- a/src/ol/geom/linestring.js +++ b/src/ol/geom/linestring.js @@ -8,6 +8,7 @@ goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.deflate'); +goog.require('ol.geom.flat.inflate'); goog.require('ol.geom.flat.interpolate'); goog.require('ol.geom.flat.length'); goog.require('ol.geom.flat.simplify'); @@ -128,7 +129,7 @@ ol.geom.LineString.prototype.getCoordinateAtM = function(m, opt_extrapolate) { * @todo stability experimental */ ol.geom.LineString.prototype.getCoordinates = function() { - return ol.geom.flat.inflateCoordinates( + return ol.geom.flat.inflate.coordinates( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride); }; diff --git a/src/ol/geom/multilinestring.js b/src/ol/geom/multilinestring.js index c35491c2ef..e67178979e 100644 --- a/src/ol/geom/multilinestring.js +++ b/src/ol/geom/multilinestring.js @@ -9,6 +9,7 @@ goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.deflate'); +goog.require('ol.geom.flat.inflate'); goog.require('ol.geom.flat.interpolate'); goog.require('ol.geom.flat.simplify'); @@ -136,7 +137,7 @@ ol.geom.MultiLineString.prototype.getCoordinateAtM = * @todo stability experimental */ ol.geom.MultiLineString.prototype.getCoordinates = function() { - return ol.geom.flat.inflateCoordinatess( + return ol.geom.flat.inflate.coordinatess( this.flatCoordinates, 0, this.ends_, this.stride); }; diff --git a/src/ol/geom/multipoint.js b/src/ol/geom/multipoint.js index e100073843..a9a0ffddf1 100644 --- a/src/ol/geom/multipoint.js +++ b/src/ol/geom/multipoint.js @@ -8,6 +8,7 @@ goog.require('ol.geom.Point'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.deflate'); +goog.require('ol.geom.flat.inflate'); @@ -81,7 +82,7 @@ ol.geom.MultiPoint.prototype.closestPointXY = * @todo stability experimental */ ol.geom.MultiPoint.prototype.getCoordinates = function() { - return ol.geom.flat.inflateCoordinates( + return ol.geom.flat.inflate.coordinates( this.flatCoordinates, 0, this.flatCoordinates.length, this.stride); }; diff --git a/src/ol/geom/multipolygon.js b/src/ol/geom/multipolygon.js index 8fb2790e1a..e2197e32c2 100644 --- a/src/ol/geom/multipolygon.js +++ b/src/ol/geom/multipolygon.js @@ -12,6 +12,7 @@ goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.contains'); goog.require('ol.geom.flat.deflate'); +goog.require('ol.geom.flat.inflate'); goog.require('ol.geom.flat.interiorpoint'); goog.require('ol.geom.flat.orient'); goog.require('ol.geom.flat.simplify'); @@ -157,7 +158,7 @@ ol.geom.MultiPolygon.prototype.getArea = function() { * @todo stability experimental */ ol.geom.MultiPolygon.prototype.getCoordinates = function() { - return ol.geom.flat.inflateCoordinatesss( + return ol.geom.flat.inflate.coordinatesss( this.flatCoordinates, 0, this.endss_, this.stride); }; diff --git a/src/ol/geom/polygon.js b/src/ol/geom/polygon.js index 7b79f55182..a8a633c4b0 100644 --- a/src/ol/geom/polygon.js +++ b/src/ol/geom/polygon.js @@ -12,6 +12,7 @@ goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.contains'); goog.require('ol.geom.flat.deflate'); +goog.require('ol.geom.flat.inflate'); goog.require('ol.geom.flat.interiorpoint'); goog.require('ol.geom.flat.orient'); goog.require('ol.geom.flat.simplify'); @@ -147,7 +148,7 @@ ol.geom.Polygon.prototype.getArea = function() { * @todo stability experimental */ ol.geom.Polygon.prototype.getCoordinates = function() { - return ol.geom.flat.inflateCoordinatess( + return ol.geom.flat.inflate.coordinatess( this.flatCoordinates, 0, this.ends_, this.stride); }; diff --git a/test/spec/ol/geom/flat/inflateflatgeom.test.js b/test/spec/ol/geom/flat/inflateflatgeom.test.js new file mode 100644 index 0000000000..fd6c4eb1e0 --- /dev/null +++ b/test/spec/ol/geom/flat/inflateflatgeom.test.js @@ -0,0 +1,27 @@ +goog.provide('ol.test.geom.flat.inflate'); + +describe('ol.geom.flat.inflate', function() { + + describe('ol.geom.flat.inflate.coordinates', function() { + + it('inflates coordinates', function() { + var coordinates = ol.geom.flat.inflate.coordinates([1, 2, 3, 4], 0, 4, 2); + expect(coordinates).to.eql([[1, 2], [3, 4]]); + }); + + }); + + describe('ol.geom.flat.inflate.coordinatess', function() { + + it('inflates arrays of coordinates', function() { + var coordinatess = ol.geom.flat.inflate.coordinatess( + [1, 2, 3, 4, 5, 6, 7, 8], 0, [4, 8], 2); + expect(coordinatess).to.eql([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]); + }); + + }); + +}); + +goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.inflate'); diff --git a/test/spec/ol/geom/flatgeom.test.js b/test/spec/ol/geom/flatgeom.test.js index 927a56e04c..709080987d 100644 --- a/test/spec/ol/geom/flatgeom.test.js +++ b/test/spec/ol/geom/flatgeom.test.js @@ -3,25 +3,6 @@ goog.provide('ol.test.geom.flat'); describe('ol.geom.flat', function() { - describe('ol.geom.flat.inflateCoordinates', function() { - - it('inflates coordinates', function() { - var coordinates = ol.geom.flat.inflateCoordinates([1, 2, 3, 4], 0, 4, 2); - expect(coordinates).to.eql([[1, 2], [3, 4]]); - }); - - }); - - describe('ol.geom.flat.inflateCoordinatess', function() { - - it('inflates arrays of coordinates', function() { - var coordinatess = ol.geom.flat.inflateCoordinatess( - [1, 2, 3, 4, 5, 6, 7, 8], 0, [4, 8], 2); - expect(coordinatess).to.eql([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]); - }); - - }); - describe('ol.geom.flat.reverseCoordinates', function() { describe('with a stride of 2', function() { From 65dcc38fad3dc0bbc86892f9a9d85e6fdecb10ff Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 12 Mar 2014 13:08:11 +0100 Subject: [PATCH 13/17] Factor out ol.geom.flat.reverse --- src/ol/geom/flat/orientflatgeom.js | 3 +- src/ol/geom/flat/reverseflatgeom.js | 22 ++++ src/ol/geom/flatgeom.js | 21 ---- test/spec/ol/geom/flat/reverseflatgeom.js | 131 ++++++++++++++++++++++ test/spec/ol/geom/flatgeom.test.js | 124 -------------------- 5 files changed, 155 insertions(+), 146 deletions(-) create mode 100644 src/ol/geom/flat/reverseflatgeom.js create mode 100644 test/spec/ol/geom/flat/reverseflatgeom.js diff --git a/src/ol/geom/flat/orientflatgeom.js b/src/ol/geom/flat/orientflatgeom.js index 06fc092e2c..75e5527824 100644 --- a/src/ol/geom/flat/orientflatgeom.js +++ b/src/ol/geom/flat/orientflatgeom.js @@ -1,6 +1,7 @@ goog.provide('ol.geom.flat.orient'); goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.reverse'); /** @@ -89,7 +90,7 @@ ol.geom.flat.orient.orientLinearRings = flatCoordinates, offset, end, stride); var reverse = i === 0 ? !isClockwise : isClockwise; if (reverse) { - ol.geom.flat.reverseCoordinates(flatCoordinates, offset, end, stride); + ol.geom.flat.reverse.coordinates(flatCoordinates, offset, end, stride); } offset = end; } diff --git a/src/ol/geom/flat/reverseflatgeom.js b/src/ol/geom/flat/reverseflatgeom.js new file mode 100644 index 0000000000..32ad1dbe01 --- /dev/null +++ b/src/ol/geom/flat/reverseflatgeom.js @@ -0,0 +1,22 @@ +goog.provide('ol.geom.flat.reverse'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + */ +ol.geom.flat.reverse.coordinates = + function(flatCoordinates, offset, end, stride) { + while (offset < end - stride) { + var i; + for (i = 0; i < stride; ++i) { + var tmp = flatCoordinates[offset + i]; + flatCoordinates[offset + i] = flatCoordinates[end - stride + i]; + flatCoordinates[end - stride + i] = tmp; + } + offset += stride; + end -= stride; + } +}; diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index d9d7fbe867..f54fe6b945 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -28,27 +28,6 @@ ol.geom.flat.linearRingssGetFlatCenters = }; -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {number} end End. - * @param {number} stride Stride. - */ -ol.geom.flat.reverseCoordinates = - function(flatCoordinates, offset, end, stride) { - while (offset < end - stride) { - var i; - for (i = 0; i < stride; ++i) { - var tmp = flatCoordinates[offset + i]; - flatCoordinates[offset + i] = flatCoordinates[end - stride + i]; - flatCoordinates[end - stride + i] = tmp; - } - offset += stride; - end -= stride; - } -}; - - /** * Returns the square of the closest distance between the point (x, y) and the * line segment (x1, y1) to (x2, y2). diff --git a/test/spec/ol/geom/flat/reverseflatgeom.js b/test/spec/ol/geom/flat/reverseflatgeom.js new file mode 100644 index 0000000000..83bd2309c1 --- /dev/null +++ b/test/spec/ol/geom/flat/reverseflatgeom.js @@ -0,0 +1,131 @@ +goog.provide('ol.test.geom.flat.reverse'); + +describe('ol.geom.flat.reverse', function() { + + describe('ol.geom.flat.reverse.coordinates', function() { + + describe('with a stride of 2', function() { + + it('can reverse empty flat coordinates', function() { + var flatCoordinates = []; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 2); + expect(flatCoordinates).to.be.empty(); + }); + + it('can reverse one flat coordinates', function() { + var flatCoordinates = [1, 2]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 2); + expect(flatCoordinates).to.eql([1, 2]); + }); + + it('can reverse two flat coordinates', function() { + var flatCoordinates = [1, 2, 3, 4]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 2); + expect(flatCoordinates).to.eql([3, 4, 1, 2]); + }); + + it('can reverse three flat coordinates', function() { + var flatCoordinates = [1, 2, 3, 4, 5, 6]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 2); + expect(flatCoordinates).to.eql([5, 6, 3, 4, 1, 2]); + }); + + it('can reverse four flat coordinates', function() { + var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 2); + expect(flatCoordinates).to.eql([7, 8, 5, 6, 3, 4, 1, 2]); + }); + + }); + + describe('with a stride of 3', function() { + + it('can reverse empty flat coordinates', function() { + var flatCoordinates = []; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 3); + expect(flatCoordinates).to.be.empty(); + }); + + it('can reverse one flat coordinates', function() { + var flatCoordinates = [1, 2, 3]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 3); + expect(flatCoordinates).to.eql([1, 2, 3]); + }); + + it('can reverse two flat coordinates', function() { + var flatCoordinates = [1, 2, 3, 4, 5, 6]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 3); + expect(flatCoordinates).to.eql([4, 5, 6, 1, 2, 3]); + }); + + it('can reverse three flat coordinates', function() { + var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 3); + expect(flatCoordinates).to.eql([7, 8, 9, 4, 5, 6, 1, 2, 3]); + }); + + it('can reverse four flat coordinates', function() { + var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 3); + expect(flatCoordinates).to.eql([10, 11, 12, 7, 8, 9, 4, 5, 6, 1, 2, 3]); + }); + + }); + + describe('with a stride of 4', function() { + + it('can reverse empty flat coordinates', function() { + var flatCoordinates = []; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 4); + expect(flatCoordinates).to.be.empty(); + }); + + it('can reverse one flat coordinates', function() { + var flatCoordinates = [1, 2, 3, 4]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 4); + expect(flatCoordinates).to.eql([1, 2, 3, 4]); + }); + + it('can reverse two flat coordinates', function() { + var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 4); + expect(flatCoordinates).to.eql([5, 6, 7, 8, 1, 2, 3, 4]); + }); + + it('can reverse three flat coordinates', function() { + var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 4); + expect(flatCoordinates).to.eql([9, 10, 11, 12, 5, 6, 7, 8, 1, 2, 3, 4]); + }); + + it('can reverse four flat coordinates', function() { + var flatCoordinates = + [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; + ol.geom.flat.reverse.coordinates( + flatCoordinates, 0, flatCoordinates.length, 4); + expect(flatCoordinates).to.eql( + [13, 14, 15, 16, 9, 10, 11, 12, 5, 6, 7, 8, 1, 2, 3, 4]); + }); + + }); + + }); + +}); + +goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.reverse'); diff --git a/test/spec/ol/geom/flatgeom.test.js b/test/spec/ol/geom/flatgeom.test.js index 709080987d..8f7de0e59b 100644 --- a/test/spec/ol/geom/flatgeom.test.js +++ b/test/spec/ol/geom/flatgeom.test.js @@ -2,130 +2,6 @@ goog.provide('ol.test.geom.flat'); describe('ol.geom.flat', function() { - - describe('ol.geom.flat.reverseCoordinates', function() { - - describe('with a stride of 2', function() { - - it('can reverse empty flat coordinates', function() { - var flatCoordinates = []; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 2); - expect(flatCoordinates).to.be.empty(); - }); - - it('can reverse one flat coordinates', function() { - var flatCoordinates = [1, 2]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 2); - expect(flatCoordinates).to.eql([1, 2]); - }); - - it('can reverse two flat coordinates', function() { - var flatCoordinates = [1, 2, 3, 4]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 2); - expect(flatCoordinates).to.eql([3, 4, 1, 2]); - }); - - it('can reverse three flat coordinates', function() { - var flatCoordinates = [1, 2, 3, 4, 5, 6]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 2); - expect(flatCoordinates).to.eql([5, 6, 3, 4, 1, 2]); - }); - - it('can reverse four flat coordinates', function() { - var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 2); - expect(flatCoordinates).to.eql([7, 8, 5, 6, 3, 4, 1, 2]); - }); - - }); - - describe('with a stride of 3', function() { - - it('can reverse empty flat coordinates', function() { - var flatCoordinates = []; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 3); - expect(flatCoordinates).to.be.empty(); - }); - - it('can reverse one flat coordinates', function() { - var flatCoordinates = [1, 2, 3]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 3); - expect(flatCoordinates).to.eql([1, 2, 3]); - }); - - it('can reverse two flat coordinates', function() { - var flatCoordinates = [1, 2, 3, 4, 5, 6]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 3); - expect(flatCoordinates).to.eql([4, 5, 6, 1, 2, 3]); - }); - - it('can reverse three flat coordinates', function() { - var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 3); - expect(flatCoordinates).to.eql([7, 8, 9, 4, 5, 6, 1, 2, 3]); - }); - - it('can reverse four flat coordinates', function() { - var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 3); - expect(flatCoordinates).to.eql([10, 11, 12, 7, 8, 9, 4, 5, 6, 1, 2, 3]); - }); - - }); - - describe('with a stride of 4', function() { - - it('can reverse empty flat coordinates', function() { - var flatCoordinates = []; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 4); - expect(flatCoordinates).to.be.empty(); - }); - - it('can reverse one flat coordinates', function() { - var flatCoordinates = [1, 2, 3, 4]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 4); - expect(flatCoordinates).to.eql([1, 2, 3, 4]); - }); - - it('can reverse two flat coordinates', function() { - var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 4); - expect(flatCoordinates).to.eql([5, 6, 7, 8, 1, 2, 3, 4]); - }); - - it('can reverse three flat coordinates', function() { - var flatCoordinates = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 4); - expect(flatCoordinates).to.eql([9, 10, 11, 12, 5, 6, 7, 8, 1, 2, 3, 4]); - }); - - it('can reverse four flat coordinates', function() { - var flatCoordinates = - [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; - ol.geom.flat.reverseCoordinates( - flatCoordinates, 0, flatCoordinates.length, 4); - expect(flatCoordinates).to.eql( - [13, 14, 15, 16, 9, 10, 11, 12, 5, 6, 7, 8, 1, 2, 3, 4]); - }); - - }); - - }); - }); From 6a00c1484147efdba636962a6566a29977e4cd25 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 12 Mar 2014 13:12:25 +0100 Subject: [PATCH 14/17] Move squaredDistance and squaredSegmentDistance into ol.math --- src/ol/geom/flat/closestflatgeom.js | 11 ++++--- src/ol/geom/flat/simplifyflatgeom.js | 5 ++-- src/ol/geom/flatgeom.js | 43 ---------------------------- src/ol/geom/multipoint.js | 3 +- src/ol/geom/point.js | 3 +- src/ol/math.js | 43 ++++++++++++++++++++++++++++ 6 files changed, 55 insertions(+), 53 deletions(-) diff --git a/src/ol/geom/flat/closestflatgeom.js b/src/ol/geom/flat/closestflatgeom.js index 12691ab88f..ec9b0eb993 100644 --- a/src/ol/geom/flat/closestflatgeom.js +++ b/src/ol/geom/flat/closestflatgeom.js @@ -3,6 +3,7 @@ goog.provide('ol.geom.flat.closest'); goog.require('goog.asserts'); goog.require('goog.math'); goog.require('ol.geom.flat'); +goog.require('ol.math'); /** @@ -65,7 +66,7 @@ ol.geom.flat.closest.getMaxSquaredDelta = for (offset += stride; offset < end; offset += stride) { var x2 = flatCoordinates[offset]; var y2 = flatCoordinates[offset + 1]; - var squaredDelta = ol.geom.flat.squaredDistance(x1, y1, x2, y2); + var squaredDelta = ol.math.squaredDistance(x1, y1, x2, y2); if (squaredDelta > maxSquaredDelta) { maxSquaredDelta = squaredDelta; } @@ -141,7 +142,7 @@ ol.geom.flat.closest.getClosestPoint = function(flatCoordinates, offset, end, var i, squaredDistance; if (maxDelta === 0) { // All points are identical, so just test the first point. - squaredDistance = ol.geom.flat.squaredDistance( + squaredDistance = ol.math.squaredDistance( x, y, flatCoordinates[offset], flatCoordinates[offset + 1]); if (squaredDistance < minSquaredDistance) { for (i = 0; i < stride; ++i) { @@ -159,8 +160,7 @@ ol.geom.flat.closest.getClosestPoint = function(flatCoordinates, offset, end, while (index < end) { ol.geom.flat.closest.point( flatCoordinates, index - stride, index, stride, x, y, tmpPoint); - squaredDistance = ol.geom.flat.squaredDistance( - x, y, tmpPoint[0], tmpPoint[1]); + squaredDistance = ol.math.squaredDistance(x, y, tmpPoint[0], tmpPoint[1]); if (squaredDistance < minSquaredDistance) { minSquaredDistance = squaredDistance; for (i = 0; i < stride; ++i) { @@ -188,8 +188,7 @@ ol.geom.flat.closest.getClosestPoint = function(flatCoordinates, offset, end, // Check the closing segment. ol.geom.flat.closest.point( flatCoordinates, end - stride, offset, stride, x, y, tmpPoint); - squaredDistance = ol.geom.flat.squaredDistance( - x, y, tmpPoint[0], tmpPoint[1]); + squaredDistance = ol.math.squaredDistance(x, y, tmpPoint[0], tmpPoint[1]); if (squaredDistance < minSquaredDistance) { minSquaredDistance = squaredDistance; for (i = 0; i < stride; ++i) { diff --git a/src/ol/geom/flat/simplifyflatgeom.js b/src/ol/geom/flat/simplifyflatgeom.js index fa6bce3485..5dbde27b28 100644 --- a/src/ol/geom/flat/simplifyflatgeom.js +++ b/src/ol/geom/flat/simplifyflatgeom.js @@ -27,6 +27,7 @@ goog.provide('ol.geom.flat.simplify'); goog.require('ol.geom.flat'); +goog.require('ol.math'); /** @@ -101,7 +102,7 @@ ol.geom.flat.simplify.douglasPeucker = function(flatCoordinates, offset, end, for (i = first + stride; i < last; i += stride) { var x = flatCoordinates[i]; var y = flatCoordinates[i + 1]; - var squaredDistance = ol.geom.flat.squaredSegmentDistance( + var squaredDistance = ol.math.squaredSegmentDistance( x, y, x1, y1, x2, y2); if (squaredDistance > maxSquaredDistance) { index = i; @@ -219,7 +220,7 @@ ol.geom.flat.simplify.radialDistance = function(flatCoordinates, offset, end, for (offset += stride; offset < end; offset += stride) { x2 = flatCoordinates[offset]; y2 = flatCoordinates[offset + 1]; - if (ol.geom.flat.squaredDistance(x1, y1, x2, y2) > squaredTolerance) { + if (ol.math.squaredDistance(x1, y1, x2, y2) > squaredTolerance) { // copy point at offset simplifiedFlatCoordinates[simplifiedOffset++] = x2; simplifiedFlatCoordinates[simplifiedOffset++] = y2; diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index f54fe6b945..4b08ed1b02 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -28,49 +28,6 @@ ol.geom.flat.linearRingssGetFlatCenters = }; -/** - * Returns the square of the closest distance between the point (x, y) and the - * line segment (x1, y1) to (x2, y2). - * @param {number} x X. - * @param {number} y Y. - * @param {number} x1 X1. - * @param {number} y1 Y1. - * @param {number} x2 X2. - * @param {number} y2 Y2. - * @return {number} Squared distance. - */ -ol.geom.flat.squaredSegmentDistance = function(x, y, x1, y1, x2, y2) { - var dx = x2 - x1; - var dy = y2 - y1; - if (dx !== 0 || dy !== 0) { - var t = ((x - x1) * dx + (y - y1) * dy) / (dx * dx + dy * dy); - if (t > 1) { - x1 = x2; - y1 = y2; - } else if (t > 0) { - x1 += dx * t; - y1 += dy * t; - } - } - return ol.geom.flat.squaredDistance(x, y, x1, y1); -}; - - -/** - * Returns the square of the distance between the points (x1, y1) and (x2, y2). - * @param {number} x1 X1. - * @param {number} y1 Y1. - * @param {number} x2 X2. - * @param {number} y2 Y2. - * @return {number} Squared distance. - */ -ol.geom.flat.squaredDistance = function(x1, y1, x2, y2) { - var dx = x2 - x1; - var dy = y2 - y1; - return dx * dx + dy * dy; -}; - - /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} stride Stride. diff --git a/src/ol/geom/multipoint.js b/src/ol/geom/multipoint.js index a9a0ffddf1..890a75458d 100644 --- a/src/ol/geom/multipoint.js +++ b/src/ol/geom/multipoint.js @@ -9,6 +9,7 @@ goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.deflate'); goog.require('ol.geom.flat.inflate'); +goog.require('ol.math'); @@ -63,7 +64,7 @@ ol.geom.MultiPoint.prototype.closestPointXY = var stride = this.stride; var i, ii, j; for (i = 0, ii = flatCoordinates.length; i < ii; i += stride) { - var squaredDistance = ol.geom.flat.squaredDistance( + var squaredDistance = ol.math.squaredDistance( x, y, flatCoordinates[i], flatCoordinates[i + 1]); if (squaredDistance < minSquaredDistance) { minSquaredDistance = squaredDistance; diff --git a/src/ol/geom/point.js b/src/ol/geom/point.js index 69b951dd1b..86b7617877 100644 --- a/src/ol/geom/point.js +++ b/src/ol/geom/point.js @@ -6,6 +6,7 @@ goog.require('ol.geom.GeometryType'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.deflate'); +goog.require('ol.math'); @@ -39,7 +40,7 @@ ol.geom.Point.prototype.clone = function() { ol.geom.Point.prototype.closestPointXY = function(x, y, closestPoint, minSquaredDistance) { var flatCoordinates = this.flatCoordinates; - var squaredDistance = ol.geom.flat.squaredDistance( + var squaredDistance = ol.math.squaredDistance( x, y, flatCoordinates[0], flatCoordinates[1]); if (squaredDistance < minSquaredDistance) { var stride = this.stride; diff --git a/src/ol/math.js b/src/ol/math.js index 14ffd2977a..70c46df4c6 100644 --- a/src/ol/math.js +++ b/src/ol/math.js @@ -59,6 +59,49 @@ ol.math.sinh = function(x) { }; +/** + * Returns the square of the closest distance between the point (x, y) and the + * line segment (x1, y1) to (x2, y2). + * @param {number} x X. + * @param {number} y Y. + * @param {number} x1 X1. + * @param {number} y1 Y1. + * @param {number} x2 X2. + * @param {number} y2 Y2. + * @return {number} Squared distance. + */ +ol.math.squaredSegmentDistance = function(x, y, x1, y1, x2, y2) { + var dx = x2 - x1; + var dy = y2 - y1; + if (dx !== 0 || dy !== 0) { + var t = ((x - x1) * dx + (y - y1) * dy) / (dx * dx + dy * dy); + if (t > 1) { + x1 = x2; + y1 = y2; + } else if (t > 0) { + x1 += dx * t; + y1 += dy * t; + } + } + return ol.math.squaredDistance(x, y, x1, y1); +}; + + +/** + * Returns the square of the distance between the points (x1, y1) and (x2, y2). + * @param {number} x1 X1. + * @param {number} y1 Y1. + * @param {number} x2 X2. + * @param {number} y2 Y2. + * @return {number} Squared distance. + */ +ol.math.squaredDistance = function(x1, y1, x2, y2) { + var dx = x2 - x1; + var dy = y2 - y1; + return dx * dx + dy * dy; +}; + + /** * @param {number} x X. * @return {number} Hyperbolic tangent of x. From ca44c6328c4c4aeb86384e7facadbb6ff43c7047 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 12 Mar 2014 13:18:29 +0100 Subject: [PATCH 15/17] Factor out ol.geom.flat.transform --- src/ol/geom/flat/transformflatgeom.js | 34 +++++++++++++++++++++++++ src/ol/geom/flatgeom.js | 33 ------------------------ src/ol/geom/simplegeometry.js | 3 ++- src/ol/render/canvas/canvasimmediate.js | 7 ++--- src/ol/render/canvas/canvasreplay.js | 5 ++-- 5 files changed, 43 insertions(+), 39 deletions(-) create mode 100644 src/ol/geom/flat/transformflatgeom.js diff --git a/src/ol/geom/flat/transformflatgeom.js b/src/ol/geom/flat/transformflatgeom.js new file mode 100644 index 0000000000..dc8c77e45e --- /dev/null +++ b/src/ol/geom/flat/transformflatgeom.js @@ -0,0 +1,34 @@ +goog.provide('ol.geom.flat.transform'); + +goog.require('goog.vec.Mat4'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} stride Stride. + * @param {goog.vec.Mat4.Number} transform Transform. + * @param {Array.=} opt_dest Destination. + * @return {Array.} Transformed coordinates. + */ +ol.geom.flat.transform.transform2D = + function(flatCoordinates, stride, transform, opt_dest) { + var m00 = goog.vec.Mat4.getElement(transform, 0, 0); + var m10 = goog.vec.Mat4.getElement(transform, 1, 0); + var m01 = goog.vec.Mat4.getElement(transform, 0, 1); + var m11 = goog.vec.Mat4.getElement(transform, 1, 1); + var m03 = goog.vec.Mat4.getElement(transform, 0, 3); + var m13 = goog.vec.Mat4.getElement(transform, 1, 3); + var dest = goog.isDef(opt_dest) ? opt_dest : []; + var i = 0; + var j, jj; + for (j = 0, jj = flatCoordinates.length; j < jj; j += stride) { + var x = flatCoordinates[j]; + var y = flatCoordinates[j + 1]; + dest[i++] = m00 * x + m01 * y + m03; + dest[i++] = m10 * x + m11 * y + m13; + } + if (goog.isDef(opt_dest) && dest.length != i) { + dest.length = i; + } + return dest; +}; diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index 4b08ed1b02..83bf883767 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -1,7 +1,5 @@ goog.provide('ol.geom.flat'); -goog.require('goog.array'); -goog.require('goog.vec.Mat4'); goog.require('ol.extent'); @@ -26,34 +24,3 @@ ol.geom.flat.linearRingssGetFlatCenters = } return flatCenters; }; - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} stride Stride. - * @param {goog.vec.Mat4.Number} transform Transform. - * @param {Array.=} opt_dest Destination. - * @return {Array.} Transformed coordinates. - */ -ol.geom.flat.transform2D = - function(flatCoordinates, stride, transform, opt_dest) { - var m00 = goog.vec.Mat4.getElement(transform, 0, 0); - var m10 = goog.vec.Mat4.getElement(transform, 1, 0); - var m01 = goog.vec.Mat4.getElement(transform, 0, 1); - var m11 = goog.vec.Mat4.getElement(transform, 1, 1); - var m03 = goog.vec.Mat4.getElement(transform, 0, 3); - var m13 = goog.vec.Mat4.getElement(transform, 1, 3); - var dest = goog.isDef(opt_dest) ? opt_dest : []; - var i = 0; - var j, jj; - for (j = 0, jj = flatCoordinates.length; j < jj; j += stride) { - var x = flatCoordinates[j]; - var y = flatCoordinates[j + 1]; - dest[i++] = m00 * x + m01 * y + m03; - dest[i++] = m10 * x + m11 * y + m13; - } - if (goog.isDef(opt_dest) && dest.length != i) { - dest.length = i; - } - return dest; -}; diff --git a/src/ol/geom/simplegeometry.js b/src/ol/geom/simplegeometry.js index 879a84f2e9..5ca2a609fc 100644 --- a/src/ol/geom/simplegeometry.js +++ b/src/ol/geom/simplegeometry.js @@ -6,6 +6,7 @@ goog.require('goog.object'); goog.require('ol.extent'); goog.require('ol.geom.Geometry'); goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.transform'); @@ -260,7 +261,7 @@ ol.geom.transformSimpleGeometry2D = return null; } else { var stride = simpleGeometry.getStride(); - return ol.geom.flat.transform2D( + return ol.geom.flat.transform.transform2D( flatCoordinates, stride, transform, opt_dest); } }; diff --git a/src/ol/render/canvas/canvasimmediate.js b/src/ol/render/canvas/canvasimmediate.js index ed1efeb6a7..84cf7ae0b6 100644 --- a/src/ol/render/canvas/canvasimmediate.js +++ b/src/ol/render/canvas/canvasimmediate.js @@ -1,6 +1,6 @@ // FIXME test, especially polygons with holes and multipolygons // FIXME need to handle large thick features (where pixel size matters) -// FIXME add offset and end to ol.geom.flat.transform2D? +// FIXME add offset and end to ol.geom.flat.transform.transform2D? goog.provide('ol.render.canvas.Immediate'); @@ -12,6 +12,7 @@ goog.require('ol.BrowserFeature'); goog.require('ol.color'); goog.require('ol.extent'); goog.require('ol.geom.flat'); +goog.require('ol.geom.flat.transform'); goog.require('ol.render.IVectorContext'); goog.require('ol.render.canvas'); goog.require('ol.vec.Mat4'); @@ -235,7 +236,7 @@ ol.render.canvas.Immediate.prototype.drawImages_ = } goog.asserts.assert(offset === 0); goog.asserts.assert(end == flatCoordinates.length); - var pixelCoordinates = ol.geom.flat.transform2D( + var pixelCoordinates = ol.geom.flat.transform.transform2D( flatCoordinates, 2, this.transform_, this.pixelCoordinates_); var context = this.context_; var localTransform = this.tmpLocalTransform_; @@ -301,7 +302,7 @@ ol.render.canvas.Immediate.prototype.drawText_ = this.setContextTextState_(this.textState_); goog.asserts.assert(offset === 0); goog.asserts.assert(end == flatCoordinates.length); - var pixelCoordinates = ol.geom.flat.transform2D( + var pixelCoordinates = ol.geom.flat.transform.transform2D( flatCoordinates, stride, this.transform_, this.pixelCoordinates_); var context = this.context_; for (; offset < end; offset += stride) { diff --git a/src/ol/render/canvas/canvasreplay.js b/src/ol/render/canvas/canvasreplay.js index 594b7b3c21..2e3320c761 100644 --- a/src/ol/render/canvas/canvasreplay.js +++ b/src/ol/render/canvas/canvasreplay.js @@ -18,6 +18,7 @@ goog.require('ol.extent'); goog.require('ol.extent.Relationship'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.simplify'); +goog.require('ol.geom.flat.transform'); goog.require('ol.render.IReplayGroup'); goog.require('ol.render.IVectorContext'); goog.require('ol.render.canvas'); @@ -217,7 +218,7 @@ ol.render.canvas.Replay.prototype.replay_ = function( if (ol.vec.Mat4.equals2D(transform, this.renderedTransform_)) { pixelCoordinates = this.pixelCoordinates_; } else { - pixelCoordinates = ol.geom.flat.transform2D( + pixelCoordinates = ol.geom.flat.transform.transform2D( this.coordinates, 2, transform, this.pixelCoordinates_); goog.vec.Mat4.setFromArray(this.renderedTransform_, transform); goog.asserts.assert(pixelCoordinates === this.pixelCoordinates_); @@ -1852,7 +1853,7 @@ ol.render.canvas.ReplayGroup.prototype.replay_ = function( var minY = maxExtent[1]; var maxX = maxExtent[2]; var maxY = maxExtent[3]; - var flatClipCoords = ol.geom.flat.transform2D( + var flatClipCoords = ol.geom.flat.transform.transform2D( [minX, minY, minX, maxY, maxX, maxY, maxX, minY], 2, transform); context.save(); context.beginPath(); From 48af6ecd1c31e0b07680e08443fb89da39b477f2 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 12 Mar 2014 13:22:19 +0100 Subject: [PATCH 16/17] Factor out ol.geom.flat.center --- src/ol/geom/flat/centerflatgeom.js | 26 ++++++++++++++++++++++++++ src/ol/geom/flatgeom.js | 25 ------------------------- src/ol/geom/multipolygon.js | 3 ++- 3 files changed, 28 insertions(+), 26 deletions(-) create mode 100644 src/ol/geom/flat/centerflatgeom.js diff --git a/src/ol/geom/flat/centerflatgeom.js b/src/ol/geom/flat/centerflatgeom.js new file mode 100644 index 0000000000..81f24369ff --- /dev/null +++ b/src/ol/geom/flat/centerflatgeom.js @@ -0,0 +1,26 @@ +goog.provide('ol.geom.flat.center'); + +goog.require('ol.extent'); + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {Array.>} endss Endss. + * @param {number} stride Stride. + * @return {Array.} Flat centers. + */ +ol.geom.flat.center.linearRingss = + function(flatCoordinates, offset, endss, stride) { + var flatCenters = []; + var i, ii; + var extent = ol.extent.createEmpty(); + for (i = 0, ii = endss.length; i < ii; ++i) { + var ends = endss[i]; + extent = ol.extent.createOrUpdateFromFlatCoordinates( + flatCoordinates, offset, ends[0], stride); + flatCenters.push((extent[0] + extent[2]) / 2, (extent[1] + extent[3]) / 2); + offset = ends[ends.length - 1]; + } + return flatCenters; +}; diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index 83bf883767..d94568448a 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -1,26 +1 @@ goog.provide('ol.geom.flat'); - -goog.require('ol.extent'); - - -/** - * @param {Array.} flatCoordinates Flat coordinates. - * @param {number} offset Offset. - * @param {Array.>} endss Endss. - * @param {number} stride Stride. - * @return {Array.} Flat centers. - */ -ol.geom.flat.linearRingssGetFlatCenters = - function(flatCoordinates, offset, endss, stride) { - var flatCenters = []; - var i, ii; - var extent = ol.extent.createEmpty(); - for (i = 0, ii = endss.length; i < ii; ++i) { - var ends = endss[i]; - extent = ol.extent.createOrUpdateFromFlatCoordinates( - flatCoordinates, offset, ends[0], stride); - flatCenters.push((extent[0] + extent[2]) / 2, (extent[1] + extent[3]) / 2); - offset = ends[ends.length - 1]; - } - return flatCenters; -}; diff --git a/src/ol/geom/multipolygon.js b/src/ol/geom/multipolygon.js index e2197e32c2..b1dd3bad80 100644 --- a/src/ol/geom/multipolygon.js +++ b/src/ol/geom/multipolygon.js @@ -9,6 +9,7 @@ goog.require('ol.geom.Polygon'); goog.require('ol.geom.SimpleGeometry'); goog.require('ol.geom.flat'); goog.require('ol.geom.flat.area'); +goog.require('ol.geom.flat.center'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.contains'); goog.require('ol.geom.flat.deflate'); @@ -176,7 +177,7 @@ ol.geom.MultiPolygon.prototype.getEndss = function() { */ ol.geom.MultiPolygon.prototype.getFlatInteriorPoints = function() { if (this.flatInteriorPointsRevision_ != this.getRevision()) { - var flatCenters = ol.geom.flat.linearRingssGetFlatCenters( + var flatCenters = ol.geom.flat.center.linearRingss( this.flatCoordinates, 0, this.endss_, this.stride); this.flatInteriorPoints_ = ol.geom.flat.interiorpoint.linearRingss( this.getOrientedFlatCoordinates(), 0, this.endss_, this.stride, From 78c8d976eb32aa8e7ae7e391b717d8f4a009b774 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 12 Mar 2014 13:25:12 +0100 Subject: [PATCH 17/17] Remove empty ol.geom.flat --- src/ol/geom/circle.js | 1 - src/ol/geom/flat/areaflatgeom.js | 2 -- src/ol/geom/flat/closestflatgeom.js | 1 - src/ol/geom/flat/containsflatgeom.js | 1 - src/ol/geom/flat/interiorpointflatgeom.js | 1 - src/ol/geom/flat/interpolateflatgeom.js | 1 - src/ol/geom/flat/lengthflatgeom.js | 2 -- src/ol/geom/flat/orientflatgeom.js | 1 - src/ol/geom/flat/simplifyflatgeom.js | 1 - src/ol/geom/flatgeom.js | 1 - src/ol/geom/linearring.js | 1 - src/ol/geom/linestring.js | 1 - src/ol/geom/multilinestring.js | 1 - src/ol/geom/multipoint.js | 1 - src/ol/geom/multipolygon.js | 1 - src/ol/geom/point.js | 1 - src/ol/geom/polygon.js | 1 - src/ol/geom/simplegeometry.js | 1 - src/ol/render/canvas/canvasimmediate.js | 1 - src/ol/render/canvas/canvasreplay.js | 1 - test/spec/ol/geom/flat/areaflatgeom.test.js | 1 - test/spec/ol/geom/flat/closestflatgeom.test.js | 1 - test/spec/ol/geom/flat/deflateflatgeom.test.js | 1 - test/spec/ol/geom/flat/flipflatgeom.test.js | 1 - test/spec/ol/geom/flat/inflateflatgeom.test.js | 1 - test/spec/ol/geom/flat/interpolateflatgeom.test.js | 1 - test/spec/ol/geom/flat/orientflatgeom.test.js | 1 - test/spec/ol/geom/flat/reverseflatgeom.js | 1 - test/spec/ol/geom/flat/simplifyflatgeom.test.js | 1 - test/spec/ol/geom/flatgeom.test.js | 8 -------- 30 files changed, 39 deletions(-) delete mode 100644 src/ol/geom/flatgeom.js delete mode 100644 test/spec/ol/geom/flatgeom.test.js diff --git a/src/ol/geom/circle.js b/src/ol/geom/circle.js index 1df979d6b0..e0c7916c06 100644 --- a/src/ol/geom/circle.js +++ b/src/ol/geom/circle.js @@ -4,7 +4,6 @@ goog.require('goog.asserts'); goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); goog.require('ol.geom.SimpleGeometry'); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.deflate'); diff --git a/src/ol/geom/flat/areaflatgeom.js b/src/ol/geom/flat/areaflatgeom.js index 54479f0fe1..eb0b1e4275 100644 --- a/src/ol/geom/flat/areaflatgeom.js +++ b/src/ol/geom/flat/areaflatgeom.js @@ -1,7 +1,5 @@ goog.provide('ol.geom.flat.area'); -goog.require('ol.geom.flat'); - /** * @param {Array.} flatCoordinates Flat coordinates. diff --git a/src/ol/geom/flat/closestflatgeom.js b/src/ol/geom/flat/closestflatgeom.js index ec9b0eb993..9170d2478c 100644 --- a/src/ol/geom/flat/closestflatgeom.js +++ b/src/ol/geom/flat/closestflatgeom.js @@ -2,7 +2,6 @@ goog.provide('ol.geom.flat.closest'); goog.require('goog.asserts'); goog.require('goog.math'); -goog.require('ol.geom.flat'); goog.require('ol.math'); diff --git a/src/ol/geom/flat/containsflatgeom.js b/src/ol/geom/flat/containsflatgeom.js index e0d43c4da4..5b5c93aacb 100644 --- a/src/ol/geom/flat/containsflatgeom.js +++ b/src/ol/geom/flat/containsflatgeom.js @@ -1,7 +1,6 @@ goog.provide('ol.geom.flat.contains'); goog.require('goog.asserts'); -goog.require('ol.geom.flat'); /** diff --git a/src/ol/geom/flat/interiorpointflatgeom.js b/src/ol/geom/flat/interiorpointflatgeom.js index cf7144c6dd..bc8d108222 100644 --- a/src/ol/geom/flat/interiorpointflatgeom.js +++ b/src/ol/geom/flat/interiorpointflatgeom.js @@ -1,7 +1,6 @@ goog.provide('ol.geom.flat.interiorpoint'); goog.require('goog.asserts'); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.contains'); diff --git a/src/ol/geom/flat/interpolateflatgeom.js b/src/ol/geom/flat/interpolateflatgeom.js index 6a7a54031d..dcfb16e2cb 100644 --- a/src/ol/geom/flat/interpolateflatgeom.js +++ b/src/ol/geom/flat/interpolateflatgeom.js @@ -3,7 +3,6 @@ goog.provide('ol.geom.flat.interpolate'); goog.require('goog.array'); goog.require('goog.asserts'); goog.require('goog.math'); -goog.require('ol.geom.flat'); /** diff --git a/src/ol/geom/flat/lengthflatgeom.js b/src/ol/geom/flat/lengthflatgeom.js index 52d053c4d8..ac1c685504 100644 --- a/src/ol/geom/flat/lengthflatgeom.js +++ b/src/ol/geom/flat/lengthflatgeom.js @@ -1,7 +1,5 @@ goog.provide('ol.geom.flat.length'); -goog.require('ol.geom.flat'); - /** * @param {Array.} flatCoordinates Flat coordinates. diff --git a/src/ol/geom/flat/orientflatgeom.js b/src/ol/geom/flat/orientflatgeom.js index 75e5527824..48c7dd5008 100644 --- a/src/ol/geom/flat/orientflatgeom.js +++ b/src/ol/geom/flat/orientflatgeom.js @@ -1,6 +1,5 @@ goog.provide('ol.geom.flat.orient'); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.reverse'); diff --git a/src/ol/geom/flat/simplifyflatgeom.js b/src/ol/geom/flat/simplifyflatgeom.js index 5dbde27b28..b5f2032040 100644 --- a/src/ol/geom/flat/simplifyflatgeom.js +++ b/src/ol/geom/flat/simplifyflatgeom.js @@ -26,7 +26,6 @@ goog.provide('ol.geom.flat.simplify'); -goog.require('ol.geom.flat'); goog.require('ol.math'); diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js deleted file mode 100644 index d94568448a..0000000000 --- a/src/ol/geom/flatgeom.js +++ /dev/null @@ -1 +0,0 @@ -goog.provide('ol.geom.flat'); diff --git a/src/ol/geom/linearring.js b/src/ol/geom/linearring.js index 034b98b520..389ccf561e 100644 --- a/src/ol/geom/linearring.js +++ b/src/ol/geom/linearring.js @@ -3,7 +3,6 @@ goog.provide('ol.geom.LinearRing'); goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); goog.require('ol.geom.SimpleGeometry'); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.deflate'); diff --git a/src/ol/geom/linestring.js b/src/ol/geom/linestring.js index cc41ed8b38..83bed93c14 100644 --- a/src/ol/geom/linestring.js +++ b/src/ol/geom/linestring.js @@ -5,7 +5,6 @@ goog.require('goog.asserts'); goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); goog.require('ol.geom.SimpleGeometry'); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.deflate'); goog.require('ol.geom.flat.inflate'); diff --git a/src/ol/geom/multilinestring.js b/src/ol/geom/multilinestring.js index e67178979e..a415be1ed2 100644 --- a/src/ol/geom/multilinestring.js +++ b/src/ol/geom/multilinestring.js @@ -6,7 +6,6 @@ goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); goog.require('ol.geom.LineString'); goog.require('ol.geom.SimpleGeometry'); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.deflate'); goog.require('ol.geom.flat.inflate'); diff --git a/src/ol/geom/multipoint.js b/src/ol/geom/multipoint.js index 890a75458d..54c7a8e170 100644 --- a/src/ol/geom/multipoint.js +++ b/src/ol/geom/multipoint.js @@ -6,7 +6,6 @@ goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); goog.require('ol.geom.Point'); goog.require('ol.geom.SimpleGeometry'); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.deflate'); goog.require('ol.geom.flat.inflate'); goog.require('ol.math'); diff --git a/src/ol/geom/multipolygon.js b/src/ol/geom/multipolygon.js index b1dd3bad80..bf694bc460 100644 --- a/src/ol/geom/multipolygon.js +++ b/src/ol/geom/multipolygon.js @@ -7,7 +7,6 @@ goog.require('ol.geom.GeometryType'); goog.require('ol.geom.MultiPoint'); goog.require('ol.geom.Polygon'); goog.require('ol.geom.SimpleGeometry'); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.center'); goog.require('ol.geom.flat.closest'); diff --git a/src/ol/geom/point.js b/src/ol/geom/point.js index 86b7617877..19275c48f4 100644 --- a/src/ol/geom/point.js +++ b/src/ol/geom/point.js @@ -4,7 +4,6 @@ goog.require('goog.asserts'); goog.require('ol.extent'); goog.require('ol.geom.GeometryType'); goog.require('ol.geom.SimpleGeometry'); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.deflate'); goog.require('ol.math'); diff --git a/src/ol/geom/polygon.js b/src/ol/geom/polygon.js index a8a633c4b0..1ab9f13818 100644 --- a/src/ol/geom/polygon.js +++ b/src/ol/geom/polygon.js @@ -7,7 +7,6 @@ goog.require('ol.geom.GeometryType'); goog.require('ol.geom.LinearRing'); goog.require('ol.geom.Point'); goog.require('ol.geom.SimpleGeometry'); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.area'); goog.require('ol.geom.flat.closest'); goog.require('ol.geom.flat.contains'); diff --git a/src/ol/geom/simplegeometry.js b/src/ol/geom/simplegeometry.js index 5ca2a609fc..e1841008d3 100644 --- a/src/ol/geom/simplegeometry.js +++ b/src/ol/geom/simplegeometry.js @@ -5,7 +5,6 @@ goog.require('goog.functions'); goog.require('goog.object'); goog.require('ol.extent'); goog.require('ol.geom.Geometry'); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.transform'); diff --git a/src/ol/render/canvas/canvasimmediate.js b/src/ol/render/canvas/canvasimmediate.js index 84cf7ae0b6..2abdb6e2e5 100644 --- a/src/ol/render/canvas/canvasimmediate.js +++ b/src/ol/render/canvas/canvasimmediate.js @@ -11,7 +11,6 @@ goog.require('goog.vec.Mat4'); goog.require('ol.BrowserFeature'); goog.require('ol.color'); goog.require('ol.extent'); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.transform'); goog.require('ol.render.IVectorContext'); goog.require('ol.render.canvas'); diff --git a/src/ol/render/canvas/canvasreplay.js b/src/ol/render/canvas/canvasreplay.js index 2e3320c761..47b9e4e2be 100644 --- a/src/ol/render/canvas/canvasreplay.js +++ b/src/ol/render/canvas/canvasreplay.js @@ -16,7 +16,6 @@ goog.require('ol.array'); goog.require('ol.color'); goog.require('ol.extent'); goog.require('ol.extent.Relationship'); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.simplify'); goog.require('ol.geom.flat.transform'); goog.require('ol.render.IReplayGroup'); diff --git a/test/spec/ol/geom/flat/areaflatgeom.test.js b/test/spec/ol/geom/flat/areaflatgeom.test.js index 18a87e28d3..afd0b8abdf 100644 --- a/test/spec/ol/geom/flat/areaflatgeom.test.js +++ b/test/spec/ol/geom/flat/areaflatgeom.test.js @@ -29,5 +29,4 @@ describe('ol.geom.flat.area', function() { }); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.area'); diff --git a/test/spec/ol/geom/flat/closestflatgeom.test.js b/test/spec/ol/geom/flat/closestflatgeom.test.js index d9b972b180..e583ed0c1f 100644 --- a/test/spec/ol/geom/flat/closestflatgeom.test.js +++ b/test/spec/ol/geom/flat/closestflatgeom.test.js @@ -143,5 +143,4 @@ describe('ol.geom.flat.closest', function() { }); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.closest'); diff --git a/test/spec/ol/geom/flat/deflateflatgeom.test.js b/test/spec/ol/geom/flat/deflateflatgeom.test.js index b29d7cf81c..75ff97e7dd 100644 --- a/test/spec/ol/geom/flat/deflateflatgeom.test.js +++ b/test/spec/ol/geom/flat/deflateflatgeom.test.js @@ -36,5 +36,4 @@ describe('ol.geom.flat.deflate', function() { }); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.deflate'); diff --git a/test/spec/ol/geom/flat/flipflatgeom.test.js b/test/spec/ol/geom/flat/flipflatgeom.test.js index 0a47e35c6e..ec5da63962 100644 --- a/test/spec/ol/geom/flat/flipflatgeom.test.js +++ b/test/spec/ol/geom/flat/flipflatgeom.test.js @@ -35,5 +35,4 @@ describe('ol.geom.flat.flip', function() { }); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.flip'); diff --git a/test/spec/ol/geom/flat/inflateflatgeom.test.js b/test/spec/ol/geom/flat/inflateflatgeom.test.js index fd6c4eb1e0..478aecd034 100644 --- a/test/spec/ol/geom/flat/inflateflatgeom.test.js +++ b/test/spec/ol/geom/flat/inflateflatgeom.test.js @@ -23,5 +23,4 @@ describe('ol.geom.flat.inflate', function() { }); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.inflate'); diff --git a/test/spec/ol/geom/flat/interpolateflatgeom.test.js b/test/spec/ol/geom/flat/interpolateflatgeom.test.js index 3d05b52214..13cdf9f0ca 100644 --- a/test/spec/ol/geom/flat/interpolateflatgeom.test.js +++ b/test/spec/ol/geom/flat/interpolateflatgeom.test.js @@ -46,5 +46,4 @@ describe('ol.geom.flat.interpolate', function() { }); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.interpolate'); diff --git a/test/spec/ol/geom/flat/orientflatgeom.test.js b/test/spec/ol/geom/flat/orientflatgeom.test.js index 010d25d33b..c8842eb5b2 100644 --- a/test/spec/ol/geom/flat/orientflatgeom.test.js +++ b/test/spec/ol/geom/flat/orientflatgeom.test.js @@ -22,5 +22,4 @@ describe('ol.geom.flat.orient', function() { }); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.orient'); diff --git a/test/spec/ol/geom/flat/reverseflatgeom.js b/test/spec/ol/geom/flat/reverseflatgeom.js index 83bd2309c1..a2c8fa0038 100644 --- a/test/spec/ol/geom/flat/reverseflatgeom.js +++ b/test/spec/ol/geom/flat/reverseflatgeom.js @@ -127,5 +127,4 @@ describe('ol.geom.flat.reverse', function() { }); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.reverse'); diff --git a/test/spec/ol/geom/flat/simplifyflatgeom.test.js b/test/spec/ol/geom/flat/simplifyflatgeom.test.js index c18029a5c4..abc4a12b7d 100644 --- a/test/spec/ol/geom/flat/simplifyflatgeom.test.js +++ b/test/spec/ol/geom/flat/simplifyflatgeom.test.js @@ -355,5 +355,4 @@ describe('ol.geom.flat.simplify', function() { }); -goog.require('ol.geom.flat'); goog.require('ol.geom.flat.simplify'); diff --git a/test/spec/ol/geom/flatgeom.test.js b/test/spec/ol/geom/flatgeom.test.js deleted file mode 100644 index 8f7de0e59b..0000000000 --- a/test/spec/ol/geom/flatgeom.test.js +++ /dev/null @@ -1,8 +0,0 @@ -goog.provide('ol.test.geom.flat'); - - -describe('ol.geom.flat', function() { -}); - - -goog.require('ol.geom.flat');