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]); - }); - - }); - - }); - });