Dedicated function for inverting a transform without modifying the source
This commit is contained in:
+31
-21
@@ -203,30 +203,40 @@ export function compose(transform, dx1, dy1, sx, sy, angle, dx2, dy2) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Invert the given transform.
|
* Invert the given transform.
|
||||||
* @param {!Transform} transform Transform.
|
* @param {!Transform} source The source transform to invert.
|
||||||
* @return {!Transform} Inverse of the transform.
|
* @return {!Transform} The inverted (source) transform.
|
||||||
*/
|
*/
|
||||||
export function invert(transform) {
|
export function invert(source) {
|
||||||
const det = determinant(transform);
|
return makeInverse(source, source);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* Returns the determinant of the given matrix.
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
multiply,
|
multiply,
|
||||||
compose,
|
compose,
|
||||||
invert,
|
invert,
|
||||||
|
makeInverse,
|
||||||
apply
|
apply
|
||||||
} from '../../../src/ol/transform.js';
|
} from '../../../src/ol/transform.js';
|
||||||
|
|
||||||
@@ -110,13 +111,39 @@ describe('ol.transform', function() {
|
|||||||
|
|
||||||
describe('invert()', function() {
|
describe('invert()', function() {
|
||||||
it('inverts a transform', function() {
|
it('inverts a transform', function() {
|
||||||
let transform = [1, 0, 1, 0, 1, 0];
|
const transform = [1, 1, 1, 2, 2, 0];
|
||||||
expect(function() {
|
|
||||||
invert(transform);
|
|
||||||
}).to.throwException();
|
|
||||||
transform = [1, 1, 1, 2, 2, 0];
|
|
||||||
expect(invert(transform)).to.eql([2, -1, -1, 1, -4, 2]);
|
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);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user