Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getRotation return a Matrix3 object #95

Merged
merged 2 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated: For the best performance these should all have a result passed in...


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