diff --git a/modules/core/docs/api-reference/matrix4.md b/modules/core/docs/api-reference/matrix4.md index 9d040df4..34f60d64 100644 --- a/modules/core/docs/api-reference/matrix4.md +++ b/modules/core/docs/api-reference/matrix4.md @@ -326,6 +326,13 @@ Transforms ### transformByMatrix2(vector : Number[4]) : Number[4] +### getRotation([result : Number[16]]) : Number[16] + +Returns rotation matrix (4 * 4). + +### getRotationMatrix3([result : Number[9]]) : Number[9] + +Returns rotation matrix (3 * 3). ## Remarks diff --git a/modules/core/src/classes/matrix4.js b/modules/core/src/classes/matrix4.js index b6b433ac..5cb3f57d 100644 --- a/modules/core/src/classes/matrix4.js +++ b/modules/core/src/classes/matrix4.js @@ -365,6 +365,25 @@ export default class Matrix4 extends Matrix { return result; } + getRotationMatrix3(result = [-0, -0, -0, -0, -0, -0, -0, -0, -0], scaleResult = null) { + const scale = this.getScale(scaleResult || [-0, -0, -0]); + + const inverseScale0 = 1 / scale[0]; + const inverseScale1 = 1 / scale[1]; + const inverseScale2 = 1 / scale[2]; + + result[0] = this[0] * inverseScale0; + result[1] = this[1] * inverseScale1; + result[2] = this[2] * inverseScale2; + result[3] = this[4] * inverseScale0; + result[4] = this[5] * inverseScale1; + result[5] = this[6] * inverseScale2; + result[6] = this[8] * inverseScale0; + result[7] = this[9] * inverseScale1; + result[8] = this[10] * inverseScale2; + return result; + } + // Modifiers transpose() { diff --git a/modules/core/test/classes/matrix4.bench.js b/modules/core/test/classes/matrix4.bench.js index c042277f..7860add2 100644 --- a/modules/core/test/classes/matrix4.bench.js +++ b/modules/core/test/classes/matrix4.bench.js @@ -127,7 +127,8 @@ export default function matrix4Bench(suite, addReferenceBenchmarks) { .add('Matrix4#determinant()', () => matrix4.determinant()) .add('Matrix4#getScale()', () => matrix4.getScale()) .add('Matrix4#getTranslation()', () => matrix4.getTranslation()) - .add('Matrix4#getRotation()', () => matrix4.getRotation()); + .add('Matrix4#getRotation()', () => matrix4.getRotation()) + .add('Matrix4#getRotationMatrix3()', () => matrix4.getRotationMatrix3()); suite .group('@math.gl/core: Matrix4 operations') diff --git a/modules/core/test/classes/matrix4.spec.js b/modules/core/test/classes/matrix4.spec.js index 3b0dc17e..4342d113 100644 --- a/modules/core/test/classes/matrix4.spec.js +++ b/modules/core/test/classes/matrix4.spec.js @@ -202,6 +202,26 @@ test('Matrix4#getRotation', t => { t.end(); }); +test('Matrix4#getRotationMatrix3', t => { + const INPUT = INDICES_MATRIX; + const RESULT = [ + 0.2672612419124244, + 0.19069251784911848, + 0.17263060129453078, + 1.3363062095621219, + 0.5720775535473555, + 0.4028047363539052, + 2.4053511772118195, + 0.9534625892455924, + 0.6329788714132796 + ]; + + const m = new Matrix4(INPUT).getRotationMatrix3([...INDICES_MATRIX.slice(0, 9)]); + tapeEquals(t, m, RESULT, 'getRotationMatrix3 gave the right result'); + + t.end(); +}); + test('Matrix4#getTranslation', t => { const INPUT = INDICES_MATRIX; const RESULT = [13, 14, 15];