Skip to content

Commit

Permalink
Merge 58b12cc into bba0e71
Browse files Browse the repository at this point in the history
  • Loading branch information
Xintong Xia committed Aug 26, 2019
2 parents bba0e71 + 58b12cc commit 8d1786b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
7 changes: 7 additions & 0 deletions modules/core/docs/api-reference/matrix4.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions modules/core/src/classes/matrix4.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion modules/core/test/classes/matrix4.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
20 changes: 20 additions & 0 deletions modules/core/test/classes/matrix4.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 8d1786b

Please sign in to comment.