algebrix 0.1.0

Vectors, matrices, quaternions, and geometry for game engines; column vectors, optional SIMD.
Documentation
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
//! 3x3 column-major matrix: 3D rotation, scale, or combined linear transform.
//!
//! Multiply a Vec3 on the right: `matrix * point`. Use [`from_quat`](Mat3::from_quat),
//! [`from_axis_angle`](Mat3::from_axis_angle), or [`from_rotation_x/y/z`](Mat3::from_rotation_z) for rotation.
//!
//! # Example
//!
//! ```rust
//! use algebrix::{Mat3, Vec3};
//!
//! let rot = Mat3::from_rotation_z(std::f32::consts::FRAC_PI_2);
//! let x = Vec3::X;
//! let y = rot * x;
//! assert!((y - Vec3::Y).length() < 1e-5);
//!
//! let scale = Mat3::from_nonuniform_scale(Vec3::new(2.0, 3.0, 1.0));
//! let p = Vec3::new(1.0, 1.0, 0.0);
//! assert_eq!(scale * p, Vec3::new(2.0, 3.0, 0.0));
//! ```

use crate::Vec3;

/// 3x3 column-major matrix for 3D rotation, scale, or combined linear transform.
///
/// Multiply a Vec3 on the right: `matrix * point`.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Mat3 {
    pub x_axis: Vec3,
    pub y_axis: Vec3,
    pub z_axis: Vec3,
}

impl Mat3 {
    /// Zero matrix (all elements 0).
    pub const ZERO: Mat3 = Mat3 {
        x_axis: Vec3::ZERO,
        y_axis: Vec3::ZERO,
        z_axis: Vec3::ZERO,
    };

    /// Identity matrix (no rotation, no scale).
    pub const IDENTITY: Mat3 = Mat3 {
        x_axis: Vec3::X,
        y_axis: Vec3::Y,
        z_axis: Vec3::Z,
    };

    /// Build from three column vectors.
    pub const fn new(x_axis: Vec3, y_axis: Vec3, z_axis: Vec3) -> Self {
        Self {
            x_axis,
            y_axis,
            z_axis,
        }
    }

    /// Same as [`new`](Mat3::new); build from columns.
    pub const fn from_cols(x_axis: Vec3, y_axis: Vec3, z_axis: Vec3) -> Self {
        Self {
            x_axis,
            y_axis,
            z_axis,
        }
    }

    /// Diagonal matrix with (diagonal.x, diagonal.y, diagonal.z) on the diagonal.
    pub const fn from_diagonal(diagonal: Vec3) -> Self {
        Self {
            x_axis: Vec3::new(diagonal.x, 0.0, 0.0),
            y_axis: Vec3::new(0.0, diagonal.y, 0.0),
            z_axis: Vec3::new(0.0, 0.0, diagonal.z),
        }
    }

    /// Diagonal matrix with `value` on the diagonal (uniform scale).
    pub const fn from_diagonal_value(value: f32) -> Self {
        Self {
            x_axis: Vec3::new(value, 0.0, 0.0),
            y_axis: Vec3::new(0.0, value, 0.0),
            z_axis: Vec3::new(0.0, 0.0, value),
        }
    }

    /// Column at `index` (0, 1, or 2). Panics if `index > 2`.
    pub fn col(&self, index: usize) -> Vec3 {
        match index {
            0 => self.x_axis,
            1 => self.y_axis,
            2 => self.z_axis,
            _ => panic!("Column index out of bounds: {}", index),
        }
    }

    /// Transpose: swap rows and columns.
    #[inline]
    pub fn transpose(self) -> Self {
        Self {
            x_axis: Vec3::new(self.x_axis.x, self.y_axis.x, self.z_axis.x),
            y_axis: Vec3::new(self.x_axis.y, self.y_axis.y, self.z_axis.y),
            z_axis: Vec3::new(self.x_axis.z, self.y_axis.z, self.z_axis.z),
        }
    }

    /// Multiply matrix by a 3D vector.
    #[inline]
    pub fn mul_vec3(self, other: Vec3) -> Vec3 {
        Vec3::new(
            self.x_axis.x.mul_add(
                other.x,
                self.y_axis.x.mul_add(other.y, self.z_axis.x * other.z),
            ),
            self.x_axis.y.mul_add(
                other.x,
                self.y_axis.y.mul_add(other.y, self.z_axis.y * other.z),
            ),
            self.x_axis.z.mul_add(
                other.x,
                self.y_axis.z.mul_add(other.y, self.z_axis.z * other.z),
            ),
        )
    }

    /// Determinant (signed volume scale). Zero if the matrix is singular.
    #[inline]
    pub fn determinant(&self) -> f32 {
        self.x_axis.x * (self.y_axis.y * self.z_axis.z - self.y_axis.z * self.z_axis.y)
            - self.y_axis.x * (self.x_axis.y * self.z_axis.z - self.x_axis.z * self.z_axis.y)
            + self.z_axis.x * (self.x_axis.y * self.y_axis.z - self.x_axis.z * self.y_axis.y)
    }

    /// Trace: sum of diagonal elements (x_axis.x + y_axis.y + z_axis.z).
    #[inline(always)]
    pub fn trace(&self) -> f32 {
        self.x_axis.x + self.y_axis.y + self.z_axis.z
    }

    /// Inverse matrix. Returns `None` if determinant is zero (singular).
    #[inline]
    pub fn inverse(&self) -> Option<Self> {
        let det = self.determinant();
        if det.abs() < f32::EPSILON {
            return None;
        }
        let inv_det = det.recip();

        let c00 = self.y_axis.y * self.z_axis.z - self.y_axis.z * self.z_axis.y;
        let c01 = self.x_axis.z * self.z_axis.y - self.x_axis.y * self.z_axis.z;
        let c02 = self.x_axis.y * self.y_axis.z - self.x_axis.z * self.y_axis.y;

        let c10 = self.y_axis.z * self.z_axis.x - self.y_axis.x * self.z_axis.z;
        let c11 = self.x_axis.x * self.z_axis.z - self.x_axis.z * self.z_axis.x;
        let c12 = self.x_axis.z * self.y_axis.x - self.x_axis.x * self.y_axis.z;

        let c20 = self.y_axis.x * self.z_axis.y - self.y_axis.y * self.z_axis.x;
        let c21 = self.x_axis.y * self.z_axis.x - self.x_axis.x * self.z_axis.y;
        let c22 = self.x_axis.x * self.y_axis.y - self.x_axis.y * self.y_axis.x;

        Some(Self {
            x_axis: Vec3::new(c00 * inv_det, c01 * inv_det, c02 * inv_det),
            y_axis: Vec3::new(c10 * inv_det, c11 * inv_det, c12 * inv_det),
            z_axis: Vec3::new(c20 * inv_det, c21 * inv_det, c22 * inv_det),
        })
    }

    /// Uniform scale matrix (scale along all axes).
    #[inline]
    pub fn from_scale(scale: f32) -> Self {
        Self::from_diagonal(Vec3::splat(scale))
    }

    /// Non-uniform scale matrix (scale.x, scale.y, scale.z per axis).
    #[inline]
    pub fn from_nonuniform_scale(scale: Vec3) -> Self {
        Self::from_diagonal(scale)
    }

    /// Rotation around the X axis by `angle` radians (right-handed).
    #[inline]
    pub fn from_rotation_x(angle: f32) -> Self {
        let (sin, cos) = angle.sin_cos();
        Self {
            x_axis: Vec3::X,
            y_axis: Vec3::new(0.0, cos, sin),
            z_axis: Vec3::new(0.0, -sin, cos),
        }
    }

    /// Rotation around the Y axis by `angle` radians (right-handed).
    #[inline]
    pub fn from_rotation_y(angle: f32) -> Self {
        let (sin, cos) = angle.sin_cos();
        Self {
            x_axis: Vec3::new(cos, 0.0, -sin),
            y_axis: Vec3::Y,
            z_axis: Vec3::new(sin, 0.0, cos),
        }
    }

    /// Rotation around the Z axis by `angle` radians (right-handed).
    #[inline]
    pub fn from_rotation_z(angle: f32) -> Self {
        let (sin, cos) = angle.sin_cos();
        Self {
            x_axis: Vec3::new(cos, sin, 0.0),
            y_axis: Vec3::new(-sin, cos, 0.0),
            z_axis: Vec3::Z,
        }
    }

    /// Rotation around `axis` (normalized) by `angle` radians (right-handed).
    #[inline]
    pub fn from_axis_angle(axis: Vec3, angle: f32) -> Self {
        let axis = axis.normalize();
        let (sin, cos) = angle.sin_cos();
        let one_minus_cos = 1.0 - cos;

        let x = axis.x;
        let y = axis.y;
        let z = axis.z;

        Self {
            x_axis: Vec3::new(
                cos + x * x * one_minus_cos,
                x * y * one_minus_cos + z * sin,
                x * z * one_minus_cos - y * sin,
            ),
            y_axis: Vec3::new(
                y * x * one_minus_cos - z * sin,
                cos + y * y * one_minus_cos,
                y * z * one_minus_cos + x * sin,
            ),
            z_axis: Vec3::new(
                z * x * one_minus_cos + y * sin,
                z * y * one_minus_cos - x * sin,
                cos + z * z * one_minus_cos,
            ),
        }
    }

    /// Rotation matrix from a unit quaternion.
    #[inline]
    pub fn from_quat(quat: crate::Quat) -> Self {
        let x = quat.x;
        let y = quat.y;
        let z = quat.z;
        let w = quat.w;

        let x2 = x + x;
        let y2 = y + y;
        let z2 = z + z;
        let xx = x * x2;
        let xy = x * y2;
        let xz = x * z2;
        let yy = y * y2;
        let yz = y * z2;
        let zz = z * z2;
        let wx = w * x2;
        let wy = w * y2;
        let wz = w * z2;

        Self {
            x_axis: Vec3::new(1.0 - yy - zz, xy + wz, xz - wy),
            y_axis: Vec3::new(xy - wz, 1.0 - xx - zz, yz + wx),
            z_axis: Vec3::new(xz + wy, yz - wx, 1.0 - xx - yy),
        }
    }

    /// Build from a 9-element array in column-major order.
    #[inline]
    pub fn from_cols_array(m: &[f32; 9]) -> Self {
        Self {
            x_axis: Vec3::new(m[0], m[1], m[2]),
            y_axis: Vec3::new(m[3], m[4], m[5]),
            z_axis: Vec3::new(m[6], m[7], m[8]),
        }
    }

    /// Copy into a 9-element array [col0, col1, col2] in column-major order.
    #[inline]
    pub fn to_cols_array(self) -> [f32; 9] {
        [
            self.x_axis.x, self.x_axis.y, self.x_axis.z,
            self.y_axis.x, self.y_axis.y, self.y_axis.z,
            self.z_axis.x, self.z_axis.y, self.z_axis.z,
        ]
    }
}

impl std::ops::Mul for Mat3 {
    type Output = Self;
    #[inline]
    fn mul(self, other: Self) -> Self {
        Self {
            x_axis: self.mul_vec3(other.x_axis),
            y_axis: self.mul_vec3(other.y_axis),
            z_axis: self.mul_vec3(other.z_axis),
        }
    }
}

impl std::ops::Mul<Vec3> for Mat3 {
    type Output = Vec3;
    #[inline]
    fn mul(self, other: Vec3) -> Vec3 {
        self.mul_vec3(other)
    }
}

impl std::ops::Mul<f32> for Mat3 {
    type Output = Self;
    #[inline]
    fn mul(self, scalar: f32) -> Self {
        Self {
            x_axis: self.x_axis * scalar,
            y_axis: self.y_axis * scalar,
            z_axis: self.z_axis * scalar,
        }
    }
}

impl std::ops::Mul<Mat3> for f32 {
    type Output = Mat3;
    #[inline]
    fn mul(self, matrix: Mat3) -> Mat3 {
        Mat3 {
            x_axis: matrix.x_axis * self,
            y_axis: matrix.y_axis * self,
            z_axis: matrix.z_axis * self,
        }
    }
}

impl std::ops::Div<f32> for Mat3 {
    type Output = Self;
    #[inline]
    fn div(self, scalar: f32) -> Self {
        let inv = 1.0 / scalar;
        Self {
            x_axis: self.x_axis * inv,
            y_axis: self.y_axis * inv,
            z_axis: self.z_axis * inv,
        }
    }
}

impl std::ops::Add for Mat3 {
    type Output = Self;
    #[inline]
    fn add(self, other: Self) -> Self {
        Self {
            x_axis: self.x_axis + other.x_axis,
            y_axis: self.y_axis + other.y_axis,
            z_axis: self.z_axis + other.z_axis,
        }
    }
}

impl std::ops::Sub for Mat3 {
    type Output = Self;
    #[inline]
    fn sub(self, other: Self) -> Self {
        Self {
            x_axis: self.x_axis - other.x_axis,
            y_axis: self.y_axis - other.y_axis,
            z_axis: self.z_axis - other.z_axis,
        }
    }
}

impl std::ops::AddAssign for Mat3 {
    #[inline]
    fn add_assign(&mut self, other: Self) {
        self.x_axis += other.x_axis;
        self.y_axis += other.y_axis;
        self.z_axis += other.z_axis;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_mat3_identity() {
        let m = Mat3::IDENTITY;
        let v = Vec3::new(1.0, 2.0, 3.0);
        assert_eq!(m * v, v);
    }

    #[test]
    fn test_mat3_mul_vec3() {
        let m = Mat3::IDENTITY;
        let v = Vec3::new(1.0, 2.0, 3.0);
        assert_eq!(m.mul_vec3(v), v);
    }

    #[test]
    fn test_mat3_transpose() {
        let m = Mat3::new(
            Vec3::new(1.0, 2.0, 3.0),
            Vec3::new(4.0, 5.0, 6.0),
            Vec3::new(7.0, 8.0, 9.0),
        );
        let transposed = m.transpose();
        assert_eq!(transposed.x_axis.x, m.x_axis.x);
        assert_eq!(transposed.x_axis.y, m.y_axis.x);
        assert_eq!(transposed.y_axis.x, m.x_axis.y);
    }
}