diff --git a/src/ol/geom/geometry.js b/src/ol/geom/geometry.js index 59a6045ff5..e6a75e5c94 100644 --- a/src/ol/geom/geometry.js +++ b/src/ol/geom/geometry.js @@ -486,3 +486,24 @@ ol.geom.inflateCoordinatesss = coordinatesss.length = i; return coordinatesss; }; + + +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + */ +ol.geom.reverseFlatCoordinates = + 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/test/spec/ol/geom/geom.test.js b/test/spec/ol/geom/geom.test.js index 881dad4489..6908756e66 100644 --- a/test/spec/ol/geom/geom.test.js +++ b/test/spec/ol/geom/geom.test.js @@ -54,4 +54,127 @@ describe('ol.geom', function() { }); + describe('ol.geom.reverseFlatCoordinates', function() { + + describe('with a stride of 2', function() { + + it('can reverse empty flat coordinates', function() { + var flatCoordinates = []; + ol.geom.reverseFlatCoordinates( + flatCoordinates, 0, flatCoordinates.length, 2); + expect(flatCoordinates).to.be.empty(); + }); + + it('can reverse one flat coordinates', function() { + var flatCoordinates = [1, 2]; + ol.geom.reverseFlatCoordinates( + 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.reverseFlatCoordinates( + 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.reverseFlatCoordinates( + 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.reverseFlatCoordinates( + 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.reverseFlatCoordinates( + flatCoordinates, 0, flatCoordinates.length, 3); + expect(flatCoordinates).to.be.empty(); + }); + + it('can reverse one flat coordinates', function() { + var flatCoordinates = [1, 2, 3]; + ol.geom.reverseFlatCoordinates( + 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.reverseFlatCoordinates( + 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.reverseFlatCoordinates( + 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.reverseFlatCoordinates( + 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.reverseFlatCoordinates( + 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.reverseFlatCoordinates( + 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.reverseFlatCoordinates( + 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.reverseFlatCoordinates( + 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.reverseFlatCoordinates( + 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]); + }); + + }); + + }); + });