2015-05-12 22:40:32 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2017-11-02 13:14:11 +00:00
|
|
|
*
|
|
|
|
* @emails oncall+react_native
|
2015-05-12 22:40:32 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var MatrixMath = require('MatrixMath');
|
|
|
|
|
|
|
|
function degreesToRadians(degrees) {
|
|
|
|
return degrees * Math.PI / 180;
|
|
|
|
}
|
|
|
|
|
2015-12-22 19:40:16 +00:00
|
|
|
function convertZeroes(degrees) {
|
|
|
|
return degrees.map(value => value === -0 ? 0 : value);
|
|
|
|
}
|
|
|
|
|
2015-05-12 22:40:32 +00:00
|
|
|
describe('MatrixMath', () => {
|
|
|
|
|
|
|
|
it('decomposes a 4x4 matrix to produce accurate Z-axis angles', () => {
|
|
|
|
expect(MatrixMath.decomposeMatrix([
|
|
|
|
1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1
|
|
|
|
]).rotationDegrees).toEqual([0, 0, 0]);
|
|
|
|
|
|
|
|
[30, 45, 60, 75, 90, 100, 115, 120, 133, 167].forEach(angle => {
|
|
|
|
var mat = MatrixMath.createRotateZ(degreesToRadians(angle));
|
2015-12-22 19:40:16 +00:00
|
|
|
expect(convertZeroes(MatrixMath.decomposeMatrix(mat).rotationDegrees))
|
2015-05-12 22:40:32 +00:00
|
|
|
.toEqual([0, 0, angle]);
|
|
|
|
|
|
|
|
mat = MatrixMath.createRotateZ(degreesToRadians(-angle));
|
2015-12-22 19:40:16 +00:00
|
|
|
expect(convertZeroes(MatrixMath.decomposeMatrix(mat).rotationDegrees))
|
2015-05-12 22:40:32 +00:00
|
|
|
.toEqual([0, 0, -angle]);
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(MatrixMath.decomposeMatrix(
|
|
|
|
MatrixMath.createRotateZ(degreesToRadians(180))
|
|
|
|
).rotationDegrees).toEqual([0, 0, 180]);
|
|
|
|
|
|
|
|
// all values are between 0 and 180;
|
|
|
|
// change of sign and direction in the third and fourth quadrant
|
|
|
|
expect(MatrixMath.decomposeMatrix(
|
|
|
|
MatrixMath.createRotateZ(degreesToRadians(222))
|
|
|
|
).rotationDegrees).toEqual([0, 0, -138]);
|
|
|
|
|
|
|
|
expect(MatrixMath.decomposeMatrix(
|
|
|
|
MatrixMath.createRotateZ(degreesToRadians(270))
|
|
|
|
).rotationDegrees).toEqual([0, 0, -90]);
|
|
|
|
|
|
|
|
// 360 is expressed as 0
|
|
|
|
expect(MatrixMath.decomposeMatrix(
|
|
|
|
MatrixMath.createRotateZ(degreesToRadians(360))
|
2015-12-22 19:40:16 +00:00
|
|
|
).rotationDegrees).toEqual([0, 0, -0]);
|
2015-05-12 22:40:32 +00:00
|
|
|
|
|
|
|
expect(MatrixMath.decomposeMatrix(
|
|
|
|
MatrixMath.createRotateZ(degreesToRadians(33.33333333))
|
|
|
|
).rotationDegrees).toEqual([0, 0, 33.333]);
|
|
|
|
|
|
|
|
expect(MatrixMath.decomposeMatrix(
|
|
|
|
MatrixMath.createRotateZ(degreesToRadians(86.75309))
|
|
|
|
).rotationDegrees).toEqual([0, 0, 86.753]);
|
|
|
|
|
|
|
|
expect(MatrixMath.decomposeMatrix(
|
|
|
|
MatrixMath.createRotateZ(degreesToRadians(42.00000000001))
|
|
|
|
).rotationDegrees).toEqual([0, 0, 42]);
|
|
|
|
|
|
|
|
expect(MatrixMath.decomposeMatrix(
|
|
|
|
MatrixMath.createRotateZ(degreesToRadians(42.99999999999))
|
|
|
|
).rotationDegrees).toEqual([0, 0, 43]);
|
|
|
|
|
|
|
|
expect(MatrixMath.decomposeMatrix(
|
|
|
|
MatrixMath.createRotateZ(degreesToRadians(42.49999999999))
|
|
|
|
).rotationDegrees).toEqual([0, 0, 42.5]);
|
|
|
|
|
|
|
|
expect(MatrixMath.decomposeMatrix(
|
|
|
|
MatrixMath.createRotateZ(degreesToRadians(42.55555555555))
|
|
|
|
).rotationDegrees).toEqual([0, 0, 42.556]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('decomposes a 4x4 matrix to produce accurate Y-axis angles', () => {
|
|
|
|
var mat;
|
|
|
|
[30, 45, 60, 75, 90, 100, 110, 120, 133, 167].forEach(angle => {
|
|
|
|
mat = MatrixMath.createIdentityMatrix();
|
|
|
|
MatrixMath.reuseRotateYCommand(mat, degreesToRadians(angle));
|
2015-12-22 19:40:16 +00:00
|
|
|
expect(convertZeroes(MatrixMath.decomposeMatrix(mat).rotationDegrees))
|
2015-05-12 22:40:32 +00:00
|
|
|
.toEqual([0, angle, 0]);
|
|
|
|
|
|
|
|
mat = MatrixMath.createIdentityMatrix();
|
|
|
|
MatrixMath.reuseRotateYCommand(mat, degreesToRadians(-angle));
|
2015-12-22 19:40:16 +00:00
|
|
|
expect(convertZeroes(MatrixMath.decomposeMatrix(mat).rotationDegrees))
|
2015-05-12 22:40:32 +00:00
|
|
|
.toEqual([0, -angle, 0]);
|
|
|
|
});
|
|
|
|
|
|
|
|
// all values are between 0 and 180;
|
|
|
|
// change of sign and direction in the third and fourth quadrant
|
|
|
|
mat = MatrixMath.createIdentityMatrix();
|
|
|
|
MatrixMath.reuseRotateYCommand(mat, degreesToRadians(222));
|
|
|
|
expect(MatrixMath.decomposeMatrix(mat).rotationDegrees)
|
|
|
|
.toEqual([0, -138, 0]);
|
|
|
|
|
|
|
|
mat = MatrixMath.createIdentityMatrix();
|
|
|
|
MatrixMath.reuseRotateYCommand(mat, degreesToRadians(270));
|
|
|
|
expect(MatrixMath.decomposeMatrix(mat).rotationDegrees)
|
|
|
|
.toEqual([0, -90, 0]);
|
|
|
|
|
|
|
|
mat = MatrixMath.createIdentityMatrix();
|
|
|
|
MatrixMath.reuseRotateYCommand(mat, degreesToRadians(360));
|
|
|
|
expect(MatrixMath.decomposeMatrix(mat).rotationDegrees)
|
|
|
|
.toEqual([0, 0, 0]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('decomposes a 4x4 matrix to produce accurate X-axis angles', () => {
|
|
|
|
var mat;
|
|
|
|
[30, 45, 60, 75, 90, 100, 110, 120, 133, 167].forEach(angle => {
|
|
|
|
mat = MatrixMath.createIdentityMatrix();
|
|
|
|
MatrixMath.reuseRotateXCommand(mat, degreesToRadians(angle));
|
2015-12-22 19:40:16 +00:00
|
|
|
expect(convertZeroes(MatrixMath.decomposeMatrix(mat).rotationDegrees))
|
2015-05-12 22:40:32 +00:00
|
|
|
.toEqual([angle, 0, 0]);
|
|
|
|
});
|
|
|
|
|
|
|
|
// all values are between 0 and 180;
|
|
|
|
// change of sign and direction in the third and fourth quadrant
|
|
|
|
mat = MatrixMath.createIdentityMatrix();
|
|
|
|
MatrixMath.reuseRotateXCommand(mat, degreesToRadians(222));
|
|
|
|
expect(MatrixMath.decomposeMatrix(mat).rotationDegrees)
|
|
|
|
.toEqual([-138, 0, 0]);
|
|
|
|
|
|
|
|
mat = MatrixMath.createIdentityMatrix();
|
|
|
|
MatrixMath.reuseRotateXCommand(mat, degreesToRadians(270));
|
|
|
|
expect(MatrixMath.decomposeMatrix(mat).rotationDegrees)
|
|
|
|
.toEqual([-90, 0, 0]);
|
|
|
|
|
|
|
|
mat = MatrixMath.createIdentityMatrix();
|
|
|
|
MatrixMath.reuseRotateXCommand(mat, degreesToRadians(360));
|
|
|
|
expect(MatrixMath.decomposeMatrix(mat).rotationDegrees)
|
|
|
|
.toEqual([0, 0, 0]);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|