From b3473c3cba8797854e84d2e2cd3b2a9688d34387 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Mon, 2 Dec 2013 09:07:55 +0100 Subject: [PATCH] Add ol.geom.flat.flipXY --- src/ol/geom/flatgeom.js | 34 ++++++++++++++++++++++++++++++ test/spec/ol/geom/flatgeom.test.js | 31 +++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/ol/geom/flatgeom.js b/src/ol/geom/flatgeom.js index 9cfab1b0a3..4add38c2d8 100644 --- a/src/ol/geom/flatgeom.js +++ b/src/ol/geom/flatgeom.js @@ -74,6 +74,40 @@ ol.geom.flat.deflateCoordinatesss = }; +/** + * @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/flatgeom.test.js b/test/spec/ol/geom/flatgeom.test.js index 25728c75f4..03ede2b5db 100644 --- a/test/spec/ol/geom/flatgeom.test.js +++ b/test/spec/ol/geom/flatgeom.test.js @@ -44,6 +44,37 @@ 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.inflateCoordinatess', function() { it('inflates arrays of coordinates', function() {