From 9ca996725e0a9a7e619964a09e521f275a555197 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Wed, 12 Mar 2014 12:55:30 +0100 Subject: [PATCH] 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() {