Add new ol.geom.Geometry#rotate function

This commit is contained in:
Frederic Junod
2016-03-07 15:27:55 +01:00
parent 6c74e90c5d
commit d5564a04db
5 changed files with 117 additions and 0 deletions

View File

@@ -35,6 +35,39 @@ ol.geom.flat.transform.transform2D = function(flatCoordinates, offset, end, stri
};
/**
* @param {Array.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.
* @param {number} end End.
* @param {number} stride Stride.
* @param {number} angle Angle.
* @param {Array.<number>} anchor Rotation anchor point.
* @param {Array.<number>=} opt_dest Destination.
* @return {Array.<number>} 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.<number>} flatCoordinates Flat coordinates.
* @param {number} offset Offset.

View File

@@ -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

View File

@@ -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.<ol.geom.Geometry>} geometries Geometries.

View File

@@ -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

View File

@@ -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');