-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat4.spec.ts
91 lines (77 loc) · 4.44 KB
/
mat4.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { expect } from "chai";
import "mocha";
import { Mat4 } from "./mat4";
import { Vector3 } from "./vector3";
describe("Mat4", () => {
describe("init", () => {
it("Test matrix creation", () => {
const emptyMatrix = [
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1,
];
expect(Mat4.create().equalsArray(emptyMatrix)).to.be.true;
expect(Mat4.create().equals(Mat4.create())).to.be.true;
expect(Mat4.create().equals(null as any)).to.be.false;
expect(Mat4.create().equals({} as any)).to.be.false;
expect(Mat4.fromTranslation({x: 0, y: 0, z: 0}).equals(Mat4.create())).to.be.true;
expect(Mat4.fromScale({x: 1, y: 1, z: 1}).equals(Mat4.create())).to.be.true;
expect(Mat4.fromScale({x: 1, y: 1, z: 1})).to.deep.equal(Mat4.create());
expect(Mat4.fromTranslation({x: 0, y: 0, z: 0})).to.deep.equal(Mat4.create());
expect(Mat4.fromRotation(0, {x: Math.random() * 360, y: Math.random() * 360, z: Math.random() * 360})).to.deep.equal(Mat4.create());
expect(Mat4.fromXRotation(0).equals(Mat4.create())).to.be.true;
expect(Mat4.fromYRotation(0).equals(Mat4.create())).to.be.true;
expect(Mat4.fromZRotation(0).equals(Mat4.create())).to.be.true;
// // commented because error during comparison 0 and -0
// expect(Mat4.fromXRotation(0)).to.deep.equal(Mat4.create());
// expect(Mat4.fromYRotation(0)).to.deep.equal(Mat4.create());
// expect(Mat4.fromZRotation(0)).to.deep.equal(Mat4.create());
});
it("Test matrix translation", () => {
const matrix = Mat4.fromTranslation({x: 2, y: 3, z: 4});
const matrixB = Mat4.create().translate(2, 3, 4);
expect(matrix).to.deep.equal(matrixB);
const positionA = {x: 0, y: 0, z: 0};
const positionB = {x: 2, y: 3, z: 4};
const positionC = {x: 10, y: 10, z: 10};
const positionATransformed = matrix.getTransformedVector(positionA);
const positionBTransformed = matrix.getTransformedVector(positionB);
const positionCTransformed = matrix.getTransformedVector(positionC);
expect(positionATransformed).to.deep.equal({x: 2, y: 3, z: 4});
expect(positionBTransformed).to.deep.equal({x: 4, y: 6, z: 8});
expect(positionCTransformed).to.deep.equal({x: 12, y: 13, z: 14});
});
it("Test matrix rotation", () => {
const matrix = Mat4.fromRotation(Math.PI, {x: 0, y: 1, z: 0});
if(!matrix) {
throw new Error("Matrix is null");
}
const matrixB = Mat4.create().rotate(Math.PI, {x: 0, y: 1, z: 0});
expect(matrix).to.deep.equal(matrixB);
const positionA = {x: 2, y: 0, z: 0};
const positionB = {x: 0, y: 2, z: 0};
const positionC = {x: 0, y: 0, z: 2};
const positionATransformed = matrix.getTransformedVector(positionA);
const positionBTransformed = matrix.getTransformedVector(positionB);
const positionCTransformed = matrix.getTransformedVector(positionC);
expect(Vector3.equalsApproximately(positionATransformed, {x: -2, y: 0, z: 0})).to.be.true;
expect(Vector3.equalsApproximately(positionBTransformed, {x: 0, y: 2, z: 0})).to.be.true;
expect(Vector3.equalsApproximately(positionCTransformed, {x: 0, y: 0, z: -2})).to.be.true;
});
it("Test matrix scale", () => {
const matrix = Mat4.fromScale({x: 1, y: 2, z: 3});
const matrixB = Mat4.create().scale(1, 2, 3);
expect(matrix).to.deep.equal(matrixB);
const positionA = {x: 2, y: 0, z: 0};
const positionB = {x: 0, y: 2, z: 0};
const positionC = {x: 0, y: 0, z: 2};
const positionATransformed = matrix.getTransformedVector(positionA);
const positionBTransformed = matrix.getTransformedVector(positionB);
const positionCTransformed = matrix.getTransformedVector(positionC);
expect(Vector3.equalsApproximately(positionATransformed, {x: 2, y: 0, z: 0})).to.be.true;
expect(Vector3.equalsApproximately(positionBTransformed, {x: 0, y: 4, z: 0})).to.be.true;
expect(Vector3.equalsApproximately(positionCTransformed, {x: 0, y: 0, z: 6})).to.be.true;
});
});
});