Skip to main content

three_rs/math/
matrix3.rs

1//! Port of `three.js/src/math/Matrix3.js` (rung 2 subset).
2//!
3//! Column-major like `Matrix4`: `elements[0..3]` is the first column.
4
5use super::{Matrix4, Vector3};
6
7/// Port of three.js' `Matrix3`: a 3x3 matrix.
8#[derive(Clone, Copy, Debug, PartialEq)]
9pub struct Matrix3 {
10    /// A column-major list of matrix values.
11    pub elements: [f64; 9],
12}
13
14impl Default for Matrix3 {
15    fn default() -> Self {
16        Self::identity()
17    }
18}
19
20impl Matrix3 {
21    /// The identity matrix.
22    pub const fn identity() -> Self {
23        Self {
24            elements: [1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0],
25        }
26    }
27
28    /// `Matrix3.set( n11, n12, n13, n21, ... )` — row-order arguments into a
29    /// column-major store, exactly as three.js writes them.
30    #[allow(clippy::too_many_arguments)]
31    pub fn set(
32        &mut self,
33        n11: f64,
34        n12: f64,
35        n13: f64,
36        n21: f64,
37        n22: f64,
38        n23: f64,
39        n31: f64,
40        n32: f64,
41        n33: f64,
42    ) -> &mut Self {
43        let te = &mut self.elements;
44        te[0] = n11;
45        te[1] = n21;
46        te[2] = n31;
47        te[3] = n12;
48        te[4] = n22;
49        te[5] = n32;
50        te[6] = n13;
51        te[7] = n23;
52        te[8] = n33;
53        self
54    }
55
56    /// `Matrix3.setFromMatrix4()`.
57    pub fn set_from_matrix4(&mut self, m: &Matrix4) -> &mut Self {
58        let me = &m.elements;
59        self.set(
60            me[0], me[4], me[8], //
61            me[1], me[5], me[9], //
62            me[2], me[6], me[10],
63        )
64    }
65
66    /// `Matrix3.invert()`.
67    pub fn invert(&mut self) -> &mut Self {
68        let te = &mut self.elements;
69
70        let (n11, n21, n31) = (te[0], te[1], te[2]);
71        let (n12, n22, n32) = (te[3], te[4], te[5]);
72        let (n13, n23, n33) = (te[6], te[7], te[8]);
73
74        let t11 = n33 * n22 - n32 * n23;
75        let t12 = n32 * n13 - n33 * n12;
76        let t13 = n23 * n12 - n22 * n13;
77
78        let det = n11 * t11 + n21 * t12 + n31 * t13;
79
80        if det == 0.0 {
81            *te = [0.0; 9];
82            return self;
83        }
84
85        let det_inv = 1.0 / det;
86
87        te[0] = t11 * det_inv;
88        te[1] = (n31 * n23 - n33 * n21) * det_inv;
89        te[2] = (n32 * n21 - n31 * n22) * det_inv;
90
91        te[3] = t12 * det_inv;
92        te[4] = (n33 * n11 - n31 * n13) * det_inv;
93        te[5] = (n31 * n12 - n32 * n11) * det_inv;
94
95        te[6] = t13 * det_inv;
96        te[7] = (n21 * n13 - n23 * n11) * det_inv;
97        te[8] = (n22 * n11 - n21 * n12) * det_inv;
98
99        self
100    }
101
102    /// `Matrix3.transpose()`.
103    pub fn transpose(&mut self) -> &mut Self {
104        let m = &mut self.elements;
105        m.swap(1, 3);
106        m.swap(2, 6);
107        m.swap(5, 7);
108        self
109    }
110
111    /// `Matrix3.getNormalMatrix( matrix4 )`.
112    pub fn get_normal_matrix(&mut self, m: &Matrix4) -> &mut Self {
113        self.set_from_matrix4(m).invert().transpose()
114    }
115
116    /// `Matrix3.identity()`.
117    pub fn set_identity(&mut self) -> &mut Self {
118        self.set(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
119    }
120
121    /// `Matrix3.copy()`.
122    pub fn copy(&mut self, m: &Self) -> &mut Self {
123        self.elements = m.elements;
124        self
125    }
126
127    /// `Matrix3.extractBasis()`.
128    pub fn extract_basis(&self, x_axis: &mut Vector3, y_axis: &mut Vector3, z_axis: &mut Vector3) {
129        x_axis.set_from_matrix3_column(self, 0);
130        y_axis.set_from_matrix3_column(self, 1);
131        z_axis.set_from_matrix3_column(self, 2);
132    }
133
134    /// `Matrix3.multiply()`.
135    pub fn multiply(&mut self, m: &Self) -> &mut Self {
136        let a = *self;
137        self.multiply_matrices(&a, m)
138    }
139
140    /// `Matrix3.premultiply()`.
141    pub fn premultiply(&mut self, m: &Self) -> &mut Self {
142        let b = *self;
143        self.multiply_matrices(m, &b)
144    }
145
146    /// `Matrix3.multiplyMatrices()`.
147    pub fn multiply_matrices(&mut self, a: &Self, b: &Self) -> &mut Self {
148        let ae = &a.elements;
149        let be = &b.elements;
150
151        let (a11, a12, a13) = (ae[0], ae[3], ae[6]);
152        let (a21, a22, a23) = (ae[1], ae[4], ae[7]);
153        let (a31, a32, a33) = (ae[2], ae[5], ae[8]);
154
155        let (b11, b12, b13) = (be[0], be[3], be[6]);
156        let (b21, b22, b23) = (be[1], be[4], be[7]);
157        let (b31, b32, b33) = (be[2], be[5], be[8]);
158
159        let te = &mut self.elements;
160
161        te[0] = a11 * b11 + a12 * b21 + a13 * b31;
162        te[3] = a11 * b12 + a12 * b22 + a13 * b32;
163        te[6] = a11 * b13 + a12 * b23 + a13 * b33;
164
165        te[1] = a21 * b11 + a22 * b21 + a23 * b31;
166        te[4] = a21 * b12 + a22 * b22 + a23 * b32;
167        te[7] = a21 * b13 + a22 * b23 + a23 * b33;
168
169        te[2] = a31 * b11 + a32 * b21 + a33 * b31;
170        te[5] = a31 * b12 + a32 * b22 + a33 * b32;
171        te[8] = a31 * b13 + a32 * b23 + a33 * b33;
172
173        self
174    }
175
176    /// `Matrix3.multiplyScalar()`.
177    pub fn multiply_scalar(&mut self, s: f64) -> &mut Self {
178        for e in self.elements.iter_mut() {
179            *e *= s;
180        }
181        self
182    }
183
184    /// `Matrix3.determinant()`.
185    pub fn determinant(&self) -> f64 {
186        let te = &self.elements;
187        let (a, b, c) = (te[0], te[1], te[2]);
188        let (d, e, f) = (te[3], te[4], te[5]);
189        let (g, h, i) = (te[6], te[7], te[8]);
190
191        a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g
192    }
193
194    /// `Matrix3.transposeIntoArray()`.
195    pub fn transpose_into_array(&self) -> [f64; 9] {
196        let m = &self.elements;
197        [m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8]]
198    }
199
200    /// `Matrix3.setUvTransform()`.
201    #[allow(clippy::too_many_arguments)]
202    pub fn set_uv_transform(
203        &mut self,
204        tx: f64,
205        ty: f64,
206        sx: f64,
207        sy: f64,
208        rotation: f64,
209        cx: f64,
210        cy: f64,
211    ) -> &mut Self {
212        let c = rotation.cos();
213        let s = rotation.sin();
214
215        self.set(
216            sx * c,
217            sx * s,
218            -sx * (c * cx + s * cy) + cx + tx,
219            -sy * s,
220            sy * c,
221            -sy * (-s * cx + c * cy) + cy + ty,
222            0.0,
223            0.0,
224            1.0,
225        )
226    }
227
228    /// `Matrix3.makeTranslation()`.
229    pub fn make_translation(&mut self, x: f64, y: f64) -> &mut Self {
230        self.set(1.0, 0.0, x, 0.0, 1.0, y, 0.0, 0.0, 1.0)
231    }
232
233    /// `Matrix3.makeRotation()` — counter-clockwise.
234    pub fn make_rotation(&mut self, theta: f64) -> &mut Self {
235        let c = theta.cos();
236        let s = theta.sin();
237
238        self.set(c, -s, 0.0, s, c, 0.0, 0.0, 0.0, 1.0)
239    }
240
241    /// `Matrix3.makeScale()`.
242    pub fn make_scale(&mut self, x: f64, y: f64) -> &mut Self {
243        self.set(x, 0.0, 0.0, 0.0, y, 0.0, 0.0, 0.0, 1.0)
244    }
245
246    /// `Matrix3.equals()`.
247    pub fn equals(&self, m: &Self) -> bool {
248        self.elements == m.elements
249    }
250
251    /// `Matrix3.fromArray()`.
252    pub fn from_array(&mut self, array: &[f64], offset: usize) -> &mut Self {
253        self.elements.copy_from_slice(&array[offset..offset + 9]);
254        self
255    }
256
257    /// `Matrix3.toArray()`.
258    pub fn to_array(&self) -> [f64; 9] {
259        self.elements
260    }
261
262    /// The nine elements narrowed to `f32`, laid out as three `vec4` columns —
263    /// the std140/WGSL uniform layout of a `mat3x3<f32>`.
264    #[allow(clippy::wrong_self_convention)] // a 72-byte matrix is cheaper borrowed
265    pub(crate) fn to_padded_f32_array(&self) -> [f32; 12] {
266        let e = &self.elements;
267        [
268            e[0] as f32,
269            e[1] as f32,
270            e[2] as f32,
271            0.0, //
272            e[3] as f32,
273            e[4] as f32,
274            e[5] as f32,
275            0.0, //
276            e[6] as f32,
277            e[7] as f32,
278            e[8] as f32,
279            0.0,
280        ]
281    }
282}