diff --git a/src/ol/geom/flat/transformflatgeom.js b/src/ol/geom/flat/transformflatgeom.js index 15eec7df0a..29ca54a240 100644 --- a/src/ol/geom/flat/transformflatgeom.js +++ b/src/ol/geom/flat/transformflatgeom.js @@ -35,6 +35,39 @@ ol.geom.flat.transform.transform2D = function(flatCoordinates, offset, end, stri }; +/** + * @param {Array.} flatCoordinates Flat coordinates. + * @param {number} offset Offset. + * @param {number} end End. + * @param {number} stride Stride. + * @param {number} angle Angle. + * @param {Array.} anchor Rotation anchor point. + * @param {Array.=} opt_dest Destination. + * @return {Array.} Transformed coordinates. + */ +ol.geom.flat.transform.rotate = function(flatCoordinates, offset, end, stride, angle, anchor, opt_dest) { + var dest = opt_dest ? opt_dest : []; + var cos = Math.cos(angle); + var sin = Math.sin(angle); + var anchorX = anchor[0]; + var anchorY = anchor[1]; + var i = 0; + for (var j = offset; j < end; j += stride) { + var deltaX = flatCoordinates[j] - anchorX; + var deltaY = flatCoordinates[j + 1] - anchorY; + dest[i++] = anchorX + deltaX * cos - deltaY * sin; + dest[i++] = anchorY + deltaX * sin + deltaY * cos; + for (var k = j + 2; k < j + stride; ++k) { + dest[i++] = flatCoordinates[k]; + } + } + if (opt_dest && dest.length != i) { + dest.length = i; + } + return dest; +}; + + /** * @param {Array.} flatCoordinates Flat coordinates. * @param {number} offset Offset. diff --git a/src/ol/geom/geometry.js b/src/ol/geom/geometry.js index 2e7e98d09b..4d8e7ea8e5 100644 --- a/src/ol/geom/geometry.js +++ b/src/ol/geom/geometry.js @@ -169,6 +169,16 @@ ol.geom.Geometry.prototype.getExtent = function(opt_extent) { }; +/** + * Rotate the geometry around a given coordinate. This modifies the geometry + * coordinates in place. + * @param {number} angle Rotation angle in radians. + * @param {ol.Coordinate} anchor The rotation center. + * @api + */ +ol.geom.Geometry.prototype.rotate = goog.abstractMethod; + + /** * Create a simplified version of this geometry. For linestrings, this uses * the the {@link diff --git a/src/ol/geom/geometrycollection.js b/src/ol/geom/geometrycollection.js index 4dca505004..9cc250ac7f 100644 --- a/src/ol/geom/geometrycollection.js +++ b/src/ol/geom/geometrycollection.js @@ -231,6 +231,19 @@ ol.geom.GeometryCollection.prototype.isEmpty = function() { }; +/** + * @inheritDoc + * @api + */ +ol.geom.GeometryCollection.prototype.rotate = function(angle, anchor) { + var geometries = this.geometries_; + for (var i = 0, ii = geometries.length; i < ii; ++i) { + geometries[i].rotate(angle, anchor); + } + this.changed(); +}; + + /** * Set the geometries that make up this geometry collection. * @param {Array.} geometries Geometries. diff --git a/src/ol/geom/simplegeometry.js b/src/ol/geom/simplegeometry.js index 66f3fe6f9e..50281175bd 100644 --- a/src/ol/geom/simplegeometry.js +++ b/src/ol/geom/simplegeometry.js @@ -260,6 +260,22 @@ ol.geom.SimpleGeometry.prototype.applyTransform = function(transformFn) { }; +/** + * @inheritDoc + * @api + */ +ol.geom.SimpleGeometry.prototype.rotate = function(angle, anchor) { + var flatCoordinates = this.getFlatCoordinates(); + if (flatCoordinates) { + var stride = this.getStride(); + ol.geom.flat.transform.rotate( + flatCoordinates, 0, flatCoordinates.length, + stride, angle, anchor, flatCoordinates); + this.changed(); + } +}; + + /** * @inheritDoc * @api stable diff --git a/test/spec/ol/geom/flat/transformflatgeom.test.js b/test/spec/ol/geom/flat/transformflatgeom.test.js index c661116a88..6bb9b564c9 100644 --- a/test/spec/ol/geom/flat/transformflatgeom.test.js +++ b/test/spec/ol/geom/flat/transformflatgeom.test.js @@ -79,6 +79,51 @@ describe('ol.geom.flat.transform', function() { }); }); + describe('ol.geom.flat.transform.rotate', function() { + it('rotates the coordinates array', function() { + var multiPolygon = new ol.geom.MultiPolygon([ + [[[0, 0, 2], [0, 1, 2], [1, 1, 2], [1, 0, 2], [0, 0, 2]]], + [[[2, 2, 3], [2, 3, 3], [3, 3, 3], [3, 2, 3], [2, 2, 3]]]]); + var flatCoordinates = multiPolygon.getFlatCoordinates(); + var angle = Math.PI / 2; + var anchor = [0, 1]; + ol.geom.flat.transform.rotate(flatCoordinates, 0, + flatCoordinates.length, multiPolygon.getStride(), + angle, anchor, flatCoordinates); + expect(flatCoordinates[0]).to.roughlyEqual(1, 1e-9); + expect(flatCoordinates[1]).to.roughlyEqual(1, 1e-9); + expect(flatCoordinates[2]).to.roughlyEqual(2, 1e-9); + expect(flatCoordinates[3]).to.roughlyEqual(0, 1e-9); + expect(flatCoordinates[4]).to.roughlyEqual(1, 1e-9); + expect(flatCoordinates[5]).to.roughlyEqual(2, 1e-9); + expect(flatCoordinates[6]).to.roughlyEqual(Math.cos(angle), 1e-9); + expect(flatCoordinates[7]).to.roughlyEqual(2, 1e-9); + expect(flatCoordinates[8]).to.roughlyEqual(2, 1e-9); + expect(flatCoordinates[9]).to.roughlyEqual(1, 1e-9); + expect(flatCoordinates[10]).to.roughlyEqual(2, 1e-9); + expect(flatCoordinates[11]).to.roughlyEqual(2, 1e-9); + expect(flatCoordinates[12]).to.roughlyEqual(1, 1e-9); + expect(flatCoordinates[13]).to.roughlyEqual(1, 1e-9); + expect(flatCoordinates[14]).to.roughlyEqual(2, 1e-9); + expect(flatCoordinates[15]).to.roughlyEqual(-1, 1e-9); + expect(flatCoordinates[16]).to.roughlyEqual(3, 1e-9); + expect(flatCoordinates[17]).to.roughlyEqual(3, 1e-9); + expect(flatCoordinates[18]).to.roughlyEqual(-2, 1e-9); + expect(flatCoordinates[19]).to.roughlyEqual(3, 1e-9); + expect(flatCoordinates[20]).to.roughlyEqual(3, 1e-9); + expect(flatCoordinates[21]).to.roughlyEqual(-2, 1e-9); + expect(flatCoordinates[22]).to.roughlyEqual(4, 1e-9); + expect(flatCoordinates[23]).to.roughlyEqual(3, 1e-9); + expect(flatCoordinates[24]).to.roughlyEqual(-1, 1e-9); + expect(flatCoordinates[25]).to.roughlyEqual(4, 1e-9); + expect(flatCoordinates[26]).to.roughlyEqual(3, 1e-9); + expect(flatCoordinates[27]).to.roughlyEqual(-1, 1e-9); + expect(flatCoordinates[28]).to.roughlyEqual(3, 1e-9); + expect(flatCoordinates[29]).to.roughlyEqual(3, 1e-9); + }); + + }); + }); goog.require('ol.geom.MultiPolygon');