File size: 2,271 Bytes
ec3121c
 
cc654e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ec3121c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc654e9
 
ec3121c
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
import type { Vector3 } from "./Vector3";

class Matrix3 {
    buffer: number[];

    // prettier-ignore
    constructor(n11: number = 1, n12: number = 0, n13: number = 0,
        n21: number = 0, n22: number = 1, n23: number = 0,
        n31: number = 0, n32: number = 0, n33: number = 1) {
        this.buffer = new Array(9);

        this.set(
            n11, n12, n13,
            n21, n22, n23,
            n31, n32, n33
        );
    }

    // prettier-ignore
    set(n11: number, n12: number, n13: number,
        n21: number, n22: number, n23: number,
        n31: number, n32: number, n33: number): Matrix3 {
        const e = this.buffer;

        e[0] = n11; e[1] = n12; e[2] = n13;
        e[3] = n21; e[4] = n22; e[5] = n23;
        e[6] = n31; e[7] = n32; e[8] = n33;

        return this;
    }

    multiply(m: Matrix3): Matrix3 {
        const a = this.buffer;
        const b = m.buffer;
        return new Matrix3(
            b[0] * a[0] + b[1] * a[3] + b[2] * a[6],
            b[0] * a[1] + b[1] * a[4] + b[2] * a[7],
            b[0] * a[2] + b[1] * a[5] + b[2] * a[8],
            b[3] * a[0] + b[4] * a[3] + b[5] * a[6],
            b[3] * a[1] + b[4] * a[4] + b[5] * a[7],
            b[3] * a[2] + b[4] * a[5] + b[5] * a[8],
            b[6] * a[0] + b[7] * a[3] + b[8] * a[6],
            b[6] * a[1] + b[7] * a[4] + b[8] * a[7],
            b[6] * a[2] + b[7] * a[5] + b[8] * a[8]
        );
    }

    clone(): Matrix3 {
        const e = this.buffer;
        // prettier-ignore
        return new Matrix3(
            e[0], e[1], e[2],
            e[3], e[4], e[5],
            e[6], e[7], e[8]
        );
    }

    static RotationFromEuler(m: Vector3): Matrix3 {
        const cx = Math.cos(m.x);
        const sx = Math.sin(m.x);
        const cy = Math.cos(m.y);
        const sy = Math.sin(m.y);
        const cz = Math.cos(m.z);
        const sz = Math.sin(m.z);

        const rotationMatrix = [
            cy * cz + sy * sx * sz,
            -cy * sz + sy * sx * cz,
            sy * cx,
            cx * sz,
            cx * cz,
            -sx,
            -sy * cz + cy * sx * sz,
            sy * sz + cy * sx * cz,
            cy * cx,
        ];

        return new Matrix3(...rotationMatrix);
    }
}

export { Matrix3 };