diff --git a/src/ol/transform.js b/src/ol/transform.js index cbb19c3c39..1694a4c95e 100644 --- a/src/ol/transform.js +++ b/src/ol/transform.js @@ -203,30 +203,40 @@ export function compose(transform, dx1, dy1, sx, sy, angle, dx2, dy2) { /** * Invert the given transform. - * @param {!Transform} transform Transform. - * @return {!Transform} Inverse of the transform. + * @param {!Transform} source The source transform to invert. + * @return {!Transform} The inverted (source) transform. */ -export function invert(transform) { - const det = determinant(transform); - assert(det !== 0, 32); // Transformation matrix cannot be inverted - - const a = transform[0]; - const b = transform[1]; - const c = transform[2]; - const d = transform[3]; - const e = transform[4]; - const f = transform[5]; - - transform[0] = d / det; - transform[1] = -b / det; - transform[2] = -c / det; - transform[3] = a / det; - transform[4] = (c * f - d * e) / det; - transform[5] = -(a * f - b * e) / det; - - return transform; +export function invert(source) { + return makeInverse(source, source); } +/** + * Invert the given transform. + * @param {!Transform} source The source transform to invert. + * @param {!Transform} target Transform to be set as the inverse of + * the source transform. + * @return {!Transform} The inverted (target) transform. + */ +export function makeInverse(source, target) { + const det = determinant(source); + assert(det !== 0, 32); // Transformation matrix cannot be inverted + + const a = source[0]; + const b = source[1]; + const c = source[2]; + const d = source[3]; + const e = source[4]; + const f = source[5]; + + target[0] = d / det; + target[1] = -b / det; + target[2] = -c / det; + target[3] = a / det; + target[4] = (c * f - d * e) / det; + target[5] = -(a * f - b * e) / det; + + return target; +} /** * Returns the determinant of the given matrix. diff --git a/test/spec/ol/transform.test.js b/test/spec/ol/transform.test.js index 13b5d91846..4cc8427fa2 100644 --- a/test/spec/ol/transform.test.js +++ b/test/spec/ol/transform.test.js @@ -9,6 +9,7 @@ import { multiply, compose, invert, + makeInverse, apply } from '../../../src/ol/transform.js'; @@ -110,13 +111,39 @@ describe('ol.transform', function() { describe('invert()', function() { it('inverts a transform', function() { - let transform = [1, 0, 1, 0, 1, 0]; - expect(function() { - invert(transform); - }).to.throwException(); - transform = [1, 1, 1, 2, 2, 0]; + const transform = [1, 1, 1, 2, 2, 0]; expect(invert(transform)).to.eql([2, -1, -1, 1, -4, 2]); - expect(transform).to.eql([2, -1, -1, 1, -4, 2]); + }); + + it('throws if the transform cannot be inverted', function() { + const indeterminant = [1, 0, 1, 0, 1, 0]; + expect(function() { + invert(indeterminant); + }).to.throwException(); + }); + + it('modifies the source', function() { + const source = [1, 1, 1, 2, 2, 0]; + const inverted = invert(source); + expect(inverted).to.eql([2, -1, -1, 1, -4, 2]); + expect(source).to.be(inverted); + }); + }); + + describe('makeInverse()', function() { + it('makes the target the inverse of the source', function() { + const source = [1, 1, 1, 2, 2, 0]; + const target = [1, 0, 0, 1, 0, 0]; + makeInverse(source, target); + expect(source).to.eql([1, 1, 1, 2, 2, 0]); + expect(target).to.eql([2, -1, -1, 1, -4, 2]); + }); + + it('returns the target', function() { + const source = [1, 1, 1, 2, 2, 0]; + const target = [1, 0, 0, 1, 0, 0]; + const inverted = makeInverse(source, target); + expect(target).to.be(inverted); }); });