Skip to content

Commit

Permalink
Merge bb4d5c3 into 7b51193
Browse files Browse the repository at this point in the history
  • Loading branch information
ibgreen committed Jun 13, 2019
2 parents 7b51193 + bb4d5c3 commit 6614abc
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 124 deletions.
4 changes: 2 additions & 2 deletions modules/core/src/classes/matrix3.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,10 @@ export default class Matrix3 extends Matrix {
result = vec2.transformMat3(result || [-0, -0], vector, this);
break;
case 3:
vec3.transformMat3(result || [-0, -0, -0], vector, this);
result = vec3.transformMat3(result || [-0, -0, -0], vector, this);
break;
case 4:
vec4_transformMat3(result || [-0, -0, -0, -0], vector, this);
result = vec4_transformMat3(result || [-0, -0, -0, -0], vector, this);
break;
default:
throw new Error('Illegal vector');
Expand Down
24 changes: 10 additions & 14 deletions modules/core/src/classes/matrix4.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,26 @@ const INDICES = Object.freeze({
const constants = {};

export default class Matrix4 extends Matrix {
get ELEMENTS() {
return 16;
static get IDENTITY() {
constants.IDENTITY = constants.IDENTITY || Object.freeze(new Matrix4(IDENTITY));
return constants.IDENTITY;
}

get RANK() {
return 4;
static get ZERO() {
constants.ZERO = constants.ZERO || Object.freeze(new Matrix4(ZERO));
return constants.ZERO;
}

get INDICES() {
return INDICES;
}

get IDENTITY() {
constants.IDENTITY = constants.IDENTITY || Object.freeze(new Matrix4(IDENTITY));
return constants.IDENTITY;
get ELEMENTS() {
return 16;
}

get ZERO() {
constants.ZERO = constants.ZERO || Object.freeze(new Matrix4(ZERO));
return constants.ZERO;
get RANK() {
return 4;
}

constructor(array) {
Expand Down Expand Up @@ -457,10 +457,6 @@ export default class Matrix4 extends Matrix {
return this.identity().translate([x, y, z]);
}

makeRotationFromQuaternion(q) {
return this.fromQuaternion(q);
}

// DEPRECATED in 3.0

transformPoint(vector, result) {
Expand Down
20 changes: 10 additions & 10 deletions modules/core/src/classes/quaternion.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,17 +302,17 @@ export default class Quaternion extends MathArray {
return this.multiplyLeft(a, b);
}

// DEPRECATED

fromValues(x, y, z, w) {
return this.set(x, y, z, w);
}

squaredLength() {
return this.lengthSquared();
}

multiply(a, b) {
return this.multiplyRight(a, b);
}

// DEPRECATED

// fromValues(x, y, z, w) {
// return this.set(x, y, z, w);
// }

// squaredLength() {
// return this.lengthSquared();
// }
}
52 changes: 52 additions & 0 deletions modules/core/test/classes/euler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ function extendToMatrix4(arr) {

test('Euler#import', t => {
t.equals(typeof Euler, 'function');
t.ok(Euler.ZYX >= 0);
t.ok(Euler.YXZ > 0);
t.ok(Euler.XZY > 0);
t.ok(Euler.ZXY > 0);
t.ok(Euler.YZX > 0);
t.ok(Euler.XYZ > 0);

t.ok(Euler.RollPitchYaw >= 0);
t.ok(Euler.DefaultOrder >= 0);
t.ok(Euler.RotationOrders);

t.equals(Euler.rotationOrder(Euler.ZYX), 'ZYX');

t.end();
});

Expand All @@ -38,6 +51,45 @@ test('Euler#construct and Array.isArray check', t => {
t.end();
});

test('Euler#coverage', t => {
let result = new Euler().fromRollPitchYaw(0, 0, 0);
t.ok(result);
result = new Euler().fromRotationMatrix(Matrix4.IDENTITY);
t.ok(result);

const euler = new Euler();

euler.x = euler.y;
euler.y = euler.z;
euler.z = euler.x;

euler.beta = euler.alpha;
euler.gamma = euler.beta;
euler.alpha = euler.gamma;

t.ok(euler.alpha >= 0);
t.ok(euler.beta >= 0);
t.ok(euler.gamma >= 0);

euler.phi = euler.theta;
euler.theta = euler.psi;
euler.psi = euler.phi;

euler.order = Euler.XYZ;
euler.order = euler.order;

euler.copy([0, 0, 0, 1]);

euler.to([0, 0, 0, 0]);
euler.toArray4([0, 0, 0, 0]);
euler.toVector3([0, 0, 0]);

// result = euler.getQuaternion();
// t.ok(result);

t.end();
});

test('Euler#toQuaternion', t => {
const eulers = [
new Euler(
Expand Down
57 changes: 56 additions & 1 deletion modules/core/test/classes/matrix3.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ const TRANSPOSED_INDICES_MATRIX = [1, 4, 7, 2, 5, 8, 3, 6, 9];

test('Matrix3#types', t => {
t.equals(typeof Matrix3, 'function');
t.ok(Matrix3.IDENTITY);
t.ok(Matrix3.ZERO);
t.end();
});

test('Matrix3#construct and Array.isArray check', t => {
t.ok(Array.isArray(new Matrix3()));
const m = new Matrix3();
t.ok(Array.isArray(m));
t.ok(m.INDICES);
t.end();
});

Expand Down Expand Up @@ -285,3 +289,54 @@ test('Matrix3#translate', t => {
tapeEquals(t, result, RESULT, 'translate gave the right result');
t.end();
});

test('Matrix3#transform', t => {
const matrix = new Matrix3().scale([2, 2, 2]);

const TEST_CASES = [
{
method: 'transform',
input: [2, 2, 0],
expected: [4, 4, 0]
},
{
method: 'transform',
input: [2, 2],
expected: [4, 4]
},
// DEPRECATED
{
method: 'transformVector',
input: [2, 2],
expected: [4, 4]
},
{
method: 'transformVector',
input: [2, 2, 0],
expected: [4, 4, 0]
},
{
method: 'transformVector2',
input: [2, 2],
expected: [4, 4]
},
{
method: 'transformVector3',
input: [2, 2, 0],
expected: [4, 4, 0]
}
];

for (const testCase of TEST_CASES) {
const p4 = matrix[testCase.method](testCase.input);
tapeEquals(t, p4, testCase.expected, 'transform gave the right result');
}

t.throws(() => matrix.transform([NaN, 0, 0, 0]));
t.throws(() => matrix.transform([0]));
t.throws(() => matrix.transform([0, 0, 0, 0, 0]));
t.throws(() => matrix.transformAsVector([0, 0, 0, 0, 0]));
t.throws(() => matrix.transformAsPoint([0, 0, 0, 0, 0]));

t.end();
});
101 changes: 83 additions & 18 deletions modules/core/test/classes/matrix4.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,20 @@ const TRANSPOSED_INDICES_MATRIX = [1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15, 4, 8

test('Matrix4#types', t => {
t.equals(typeof Matrix4, 'function');
t.ok(Matrix4.IDENTITY);
t.ok(Matrix4.ZERO);
t.end();
});

test('Matrix4#construct and Array.isArray check', t => {
const m = new Matrix4();
t.ok(Array.isArray(m));
t.ok(m.INDICES);
t.ok(m.IDENTITY);
t.ok(m.ZERO);
t.end();
});

test('Matrix4#fromQuaternion', t => {
tapeEquals(t, new Matrix4().fromQuaternion([0, 0, 0, 1]), IDENTITY_MATRIX);
t.end();
});

Expand Down Expand Up @@ -265,6 +270,35 @@ test('Matrix4#perspective#with infinite far plane, 45deg fovy, and realistic asp
[1.81066, 0, 0, 0, 0, 2.414213, 0, 0, 0, 0, -1, -1, 0, 0, -0.2, 0],
'should calculate correct matrix'
);
t.throws(() => new Matrix4().perspective({fovy: 10, aspect: 1, near: 0, far: 1}));
t.end();
});

test('Matrix4#orthographic#', t => {
const fovy = Math.PI * 0.5;
const result = new Matrix4().orthographic({fovy, aspect: 1, near: 0, far: 1});
t.ok(result);
t.throws(() => new Matrix4().orthographic({fovy: 10, aspect: 1, near: 0, far: 1}));
t.end();
});

test('Matrix4#frustum', t => {
const result = new Matrix4().frustum({left: -1, right: 1, bottom: -1, top: 1, near: -1, far: 1});
t.ok(result);
t.end();
});

test('Matrix4#ortho', t => {
const result = new Matrix4().ortho({left: -1, right: 1, bottom: -1, top: 1, near: -1, far: 1});
t.ok(result);
t.end();
});

test('Matrix4#lookat', t => {
let result = new Matrix4().lookAt({eye: [1, 1, 1], center: [0, 0, 0], up: [0, 1, 0]});
t.ok(result);
result = new Matrix4().lookAt([1, 1, 1], [0, 0, 0], [0, 1, 0]);
t.ok(result);
t.end();
});

Expand Down Expand Up @@ -310,6 +344,24 @@ test('Matrix4.scale', t => {
t.end();
});

test('Matrix4.multiplyRight', t => {
const RESULT = [2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2];

const m = new Matrix4().multiplyRight(RESULT);

tapeEquals(t, m, RESULT, 'multiplyRight gave the right result');
t.end();
});

test('Matrix4.multiplyLeft', t => {
const RESULT = [2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2];

const m = new Matrix4().multiplyLeft(RESULT);

tapeEquals(t, m, RESULT, 'multiplyLeft gave the right result');
t.end();
});

test('Matrix4.translate', t => {
const RESULT = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 2, 3, 1];

Expand Down Expand Up @@ -468,19 +520,30 @@ test('Matrix4.rotateAxis', t => {
t.end();
});

// test('Matrix4.rotateXYZ', t => {
// t.equals(typeof Matrix4.prototype.rotateXYZ, 'function');
// const m = new Matrix4();
// const result = m.rotateXYZ([1, 2, 3]);
// const reference = new Matrix4([
// 0.411982245665683, -0.8337376517741568, -0.36763046292489926, 0,
// -0.05872664492762098, -0.42691762127620736, 0.9023815854833308, 0,
// -0.9092974268256817, -0.35017548837401463, -0.2248450953661529, 0,
// 0, 0, 0, 1
// ]).transpose();
// t.assert(result.equals(reference), 'rotateXYZ generated expected matrix');
// t.end();
// });
test('Matrix4.rotateXYZ', t => {
const m = new Matrix4();
const result = m.rotateXYZ([1, 2, 3]);
const reference = [
0.411982245665683,
-0.6812427202564033,
0.6051272472413688,
0,
0.05872664492762098,
-0.642872836134547,
-0.7637183366502791,
0,
0.9092974268256817,
0.35017548837401463,
-0.2248450953661529,
0,
0,
0,
0,
1
];
tapeEquals(t, result, reference, 'rotateXYZ generated expected matrix');
t.end();
});

test('Matrix4#transform', t => {
const matrix = new Matrix4().translate([1, 2, 3]).scale([2, 2, 2]);
Expand Down Expand Up @@ -560,9 +623,11 @@ test('Matrix4#transform', t => {
tapeEquals(t, p4, testCase.expected, 'transform gave the right result');
}

t.throws(() => t.transform([NaN, 0, 0, 0]));
t.throws(() => t.transform([0, 0, 0]));
t.throws(() => t.transform([0, 0, 0, 0, 0]));
t.throws(() => matrix.transform([NaN, 0, 0, 0]));
t.throws(() => matrix.transform([0]));
t.throws(() => matrix.transform([0, 0, 0, 0, 0]));
t.throws(() => matrix.transformAsVector([0, 0, 0, 0, 0]));
t.throws(() => matrix.transformAsPoint([0, 0, 0, 0, 0]));

t.end();
});
8 changes: 8 additions & 0 deletions modules/core/test/classes/pose.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ test('Pose#constructor', t => {
pose2 = new Pose(flattenProps);

t.ok(pose1.equals(pose2), 'reconstructed from flatten props');
t.end();
});

test('Pose#equals', t => {
const pose1 = new Pose({});

t.notOk(pose1.equals(undefined), 'not equal to undefined');
t.notOk(pose1.exactEquals(undefined), 'not exactEquals to undefined');

t.end();
});
Expand Down

0 comments on commit 6614abc

Please sign in to comment.