glam/f32/sse2/
mat3a.rs

1// Generated from mat.rs.tera template. Edit the template, not the generated file.
2
3use crate::{
4    euler::{FromEuler, ToEuler},
5    f32::math,
6    swizzles::*,
7    DMat3, EulerRot, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec3A,
8};
9use core::fmt;
10use core::iter::{Product, Sum};
11use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
12
13#[cfg(target_arch = "x86")]
14use core::arch::x86::*;
15#[cfg(target_arch = "x86_64")]
16use core::arch::x86_64::*;
17
18/// Creates a 3x3 matrix from three column vectors.
19#[inline(always)]
20#[must_use]
21pub const fn mat3a(x_axis: Vec3A, y_axis: Vec3A, z_axis: Vec3A) -> Mat3A {
22    Mat3A::from_cols(x_axis, y_axis, z_axis)
23}
24
25/// A 3x3 column major matrix.
26///
27/// This 3x3 matrix type features convenience methods for creating and using linear and
28/// affine transformations. If you are primarily dealing with 2D affine transformations the
29/// [`Affine2`](crate::Affine2) type is much faster and more space efficient than
30/// using a 3x3 matrix.
31///
32/// Linear transformations including 3D rotation and scale can be created using methods
33/// such as [`Self::from_diagonal()`], [`Self::from_quat()`], [`Self::from_axis_angle()`],
34/// [`Self::from_rotation_x()`], [`Self::from_rotation_y()`], or
35/// [`Self::from_rotation_z()`].
36///
37/// The resulting matrices can be use to transform 3D vectors using regular vector
38/// multiplication.
39///
40/// Affine transformations including 2D translation, rotation and scale can be created
41/// using methods such as [`Self::from_translation()`], [`Self::from_angle()`],
42/// [`Self::from_scale()`] and [`Self::from_scale_angle_translation()`].
43///
44/// The [`Self::transform_point2()`] and [`Self::transform_vector2()`] convenience methods
45/// are provided for performing affine transforms on 2D vectors and points. These multiply
46/// 2D inputs as 3D vectors with an implicit `z` value of `1` for points and `0` for
47/// vectors respectively. These methods assume that `Self` contains a valid affine
48/// transform.
49#[derive(Clone, Copy)]
50#[repr(C)]
51pub struct Mat3A {
52    pub x_axis: Vec3A,
53    pub y_axis: Vec3A,
54    pub z_axis: Vec3A,
55}
56
57impl Mat3A {
58    /// A 3x3 matrix with all elements set to `0.0`.
59    pub const ZERO: Self = Self::from_cols(Vec3A::ZERO, Vec3A::ZERO, Vec3A::ZERO);
60
61    /// A 3x3 identity matrix, where all diagonal elements are `1`, and all off-diagonal elements are `0`.
62    pub const IDENTITY: Self = Self::from_cols(Vec3A::X, Vec3A::Y, Vec3A::Z);
63
64    /// All NAN:s.
65    pub const NAN: Self = Self::from_cols(Vec3A::NAN, Vec3A::NAN, Vec3A::NAN);
66
67    #[allow(clippy::too_many_arguments)]
68    #[inline(always)]
69    #[must_use]
70    const fn new(
71        m00: f32,
72        m01: f32,
73        m02: f32,
74        m10: f32,
75        m11: f32,
76        m12: f32,
77        m20: f32,
78        m21: f32,
79        m22: f32,
80    ) -> Self {
81        Self {
82            x_axis: Vec3A::new(m00, m01, m02),
83            y_axis: Vec3A::new(m10, m11, m12),
84            z_axis: Vec3A::new(m20, m21, m22),
85        }
86    }
87
88    /// Creates a 3x3 matrix from three column vectors.
89    #[inline(always)]
90    #[must_use]
91    pub const fn from_cols(x_axis: Vec3A, y_axis: Vec3A, z_axis: Vec3A) -> Self {
92        Self {
93            x_axis,
94            y_axis,
95            z_axis,
96        }
97    }
98
99    /// Creates a 3x3 matrix from a `[f32; 9]` array stored in column major order.
100    /// If your data is stored in row major you will need to `transpose` the returned
101    /// matrix.
102    #[inline]
103    #[must_use]
104    pub const fn from_cols_array(m: &[f32; 9]) -> Self {
105        Self::new(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8])
106    }
107
108    /// Creates a `[f32; 9]` array storing data in column major order.
109    /// If you require data in row major order `transpose` the matrix first.
110    #[inline]
111    #[must_use]
112    pub const fn to_cols_array(&self) -> [f32; 9] {
113        let [x_axis_x, x_axis_y, x_axis_z] = self.x_axis.to_array();
114        let [y_axis_x, y_axis_y, y_axis_z] = self.y_axis.to_array();
115        let [z_axis_x, z_axis_y, z_axis_z] = self.z_axis.to_array();
116
117        [
118            x_axis_x, x_axis_y, x_axis_z, y_axis_x, y_axis_y, y_axis_z, z_axis_x, z_axis_y,
119            z_axis_z,
120        ]
121    }
122
123    /// Creates a 3x3 matrix from a `[[f32; 3]; 3]` 3D array stored in column major order.
124    /// If your data is in row major order you will need to `transpose` the returned
125    /// matrix.
126    #[inline]
127    #[must_use]
128    pub const fn from_cols_array_2d(m: &[[f32; 3]; 3]) -> Self {
129        Self::from_cols(
130            Vec3A::from_array(m[0]),
131            Vec3A::from_array(m[1]),
132            Vec3A::from_array(m[2]),
133        )
134    }
135
136    /// Creates a `[[f32; 3]; 3]` 3D array storing data in column major order.
137    /// If you require data in row major order `transpose` the matrix first.
138    #[inline]
139    #[must_use]
140    pub const fn to_cols_array_2d(&self) -> [[f32; 3]; 3] {
141        [
142            self.x_axis.to_array(),
143            self.y_axis.to_array(),
144            self.z_axis.to_array(),
145        ]
146    }
147
148    /// Creates a 3x3 matrix with its diagonal set to `diagonal` and all other entries set to 0.
149    #[doc(alias = "scale")]
150    #[inline]
151    #[must_use]
152    pub const fn from_diagonal(diagonal: Vec3) -> Self {
153        Self::new(
154            diagonal.x, 0.0, 0.0, 0.0, diagonal.y, 0.0, 0.0, 0.0, diagonal.z,
155        )
156    }
157
158    /// Creates a 3x3 matrix from a 4x4 matrix, discarding the 4th row and column.
159    #[inline]
160    #[must_use]
161    pub fn from_mat4(m: Mat4) -> Self {
162        Self::from_cols(
163            Vec3A::from_vec4(m.x_axis),
164            Vec3A::from_vec4(m.y_axis),
165            Vec3A::from_vec4(m.z_axis),
166        )
167    }
168
169    /// Creates a 3x3 matrix from the minor of the given 4x4 matrix, discarding the `i`th column
170    /// and `j`th row.
171    ///
172    /// # Panics
173    ///
174    /// Panics if `i` or `j` is greater than 3.
175    #[inline]
176    #[must_use]
177    pub fn from_mat4_minor(m: Mat4, i: usize, j: usize) -> Self {
178        match (i, j) {
179            (0, 0) => Self::from_cols(
180                Vec3A::from_vec4(m.y_axis.yzww()),
181                Vec3A::from_vec4(m.z_axis.yzww()),
182                Vec3A::from_vec4(m.w_axis.yzww()),
183            ),
184            (0, 1) => Self::from_cols(
185                Vec3A::from_vec4(m.y_axis.xzww()),
186                Vec3A::from_vec4(m.z_axis.xzww()),
187                Vec3A::from_vec4(m.w_axis.xzww()),
188            ),
189            (0, 2) => Self::from_cols(
190                Vec3A::from_vec4(m.y_axis.xyww()),
191                Vec3A::from_vec4(m.z_axis.xyww()),
192                Vec3A::from_vec4(m.w_axis.xyww()),
193            ),
194            (0, 3) => Self::from_cols(
195                Vec3A::from_vec4(m.y_axis.xyzw()),
196                Vec3A::from_vec4(m.z_axis.xyzw()),
197                Vec3A::from_vec4(m.w_axis.xyzw()),
198            ),
199            (1, 0) => Self::from_cols(
200                Vec3A::from_vec4(m.x_axis.yzww()),
201                Vec3A::from_vec4(m.z_axis.yzww()),
202                Vec3A::from_vec4(m.w_axis.yzww()),
203            ),
204            (1, 1) => Self::from_cols(
205                Vec3A::from_vec4(m.x_axis.xzww()),
206                Vec3A::from_vec4(m.z_axis.xzww()),
207                Vec3A::from_vec4(m.w_axis.xzww()),
208            ),
209            (1, 2) => Self::from_cols(
210                Vec3A::from_vec4(m.x_axis.xyww()),
211                Vec3A::from_vec4(m.z_axis.xyww()),
212                Vec3A::from_vec4(m.w_axis.xyww()),
213            ),
214            (1, 3) => Self::from_cols(
215                Vec3A::from_vec4(m.x_axis.xyzw()),
216                Vec3A::from_vec4(m.z_axis.xyzw()),
217                Vec3A::from_vec4(m.w_axis.xyzw()),
218            ),
219            (2, 0) => Self::from_cols(
220                Vec3A::from_vec4(m.x_axis.yzww()),
221                Vec3A::from_vec4(m.y_axis.yzww()),
222                Vec3A::from_vec4(m.w_axis.yzww()),
223            ),
224            (2, 1) => Self::from_cols(
225                Vec3A::from_vec4(m.x_axis.xzww()),
226                Vec3A::from_vec4(m.y_axis.xzww()),
227                Vec3A::from_vec4(m.w_axis.xzww()),
228            ),
229            (2, 2) => Self::from_cols(
230                Vec3A::from_vec4(m.x_axis.xyww()),
231                Vec3A::from_vec4(m.y_axis.xyww()),
232                Vec3A::from_vec4(m.w_axis.xyww()),
233            ),
234            (2, 3) => Self::from_cols(
235                Vec3A::from_vec4(m.x_axis.xyzw()),
236                Vec3A::from_vec4(m.y_axis.xyzw()),
237                Vec3A::from_vec4(m.w_axis.xyzw()),
238            ),
239            (3, 0) => Self::from_cols(
240                Vec3A::from_vec4(m.x_axis.yzww()),
241                Vec3A::from_vec4(m.y_axis.yzww()),
242                Vec3A::from_vec4(m.z_axis.yzww()),
243            ),
244            (3, 1) => Self::from_cols(
245                Vec3A::from_vec4(m.x_axis.xzww()),
246                Vec3A::from_vec4(m.y_axis.xzww()),
247                Vec3A::from_vec4(m.z_axis.xzww()),
248            ),
249            (3, 2) => Self::from_cols(
250                Vec3A::from_vec4(m.x_axis.xyww()),
251                Vec3A::from_vec4(m.y_axis.xyww()),
252                Vec3A::from_vec4(m.z_axis.xyww()),
253            ),
254            (3, 3) => Self::from_cols(
255                Vec3A::from_vec4(m.x_axis.xyzw()),
256                Vec3A::from_vec4(m.y_axis.xyzw()),
257                Vec3A::from_vec4(m.z_axis.xyzw()),
258            ),
259            _ => panic!("index out of bounds"),
260        }
261    }
262
263    /// Creates a 3D rotation matrix from the given quaternion.
264    ///
265    /// # Panics
266    ///
267    /// Will panic if `rotation` is not normalized when `glam_assert` is enabled.
268    #[inline]
269    #[must_use]
270    pub fn from_quat(rotation: Quat) -> Self {
271        glam_assert!(rotation.is_normalized());
272
273        let x2 = rotation.x + rotation.x;
274        let y2 = rotation.y + rotation.y;
275        let z2 = rotation.z + rotation.z;
276        let xx = rotation.x * x2;
277        let xy = rotation.x * y2;
278        let xz = rotation.x * z2;
279        let yy = rotation.y * y2;
280        let yz = rotation.y * z2;
281        let zz = rotation.z * z2;
282        let wx = rotation.w * x2;
283        let wy = rotation.w * y2;
284        let wz = rotation.w * z2;
285
286        Self::from_cols(
287            Vec3A::new(1.0 - (yy + zz), xy + wz, xz - wy),
288            Vec3A::new(xy - wz, 1.0 - (xx + zz), yz + wx),
289            Vec3A::new(xz + wy, yz - wx, 1.0 - (xx + yy)),
290        )
291    }
292
293    /// Creates a 3D rotation matrix from a normalized rotation `axis` and `angle` (in
294    /// radians).
295    ///
296    /// # Panics
297    ///
298    /// Will panic if `axis` is not normalized when `glam_assert` is enabled.
299    #[inline]
300    #[must_use]
301    pub fn from_axis_angle(axis: Vec3, angle: f32) -> Self {
302        glam_assert!(axis.is_normalized());
303
304        let (sin, cos) = math::sin_cos(angle);
305        let (xsin, ysin, zsin) = axis.mul(sin).into();
306        let (x, y, z) = axis.into();
307        let (x2, y2, z2) = axis.mul(axis).into();
308        let omc = 1.0 - cos;
309        let xyomc = x * y * omc;
310        let xzomc = x * z * omc;
311        let yzomc = y * z * omc;
312        Self::from_cols(
313            Vec3A::new(x2 * omc + cos, xyomc + zsin, xzomc - ysin),
314            Vec3A::new(xyomc - zsin, y2 * omc + cos, yzomc + xsin),
315            Vec3A::new(xzomc + ysin, yzomc - xsin, z2 * omc + cos),
316        )
317    }
318
319    /// Creates a 3D rotation matrix from the given euler rotation sequence and the angles (in
320    /// radians).
321    #[inline]
322    #[must_use]
323    pub fn from_euler(order: EulerRot, a: f32, b: f32, c: f32) -> Self {
324        Self::from_euler_angles(order, a, b, c)
325    }
326
327    /// Extract Euler angles with the given Euler rotation order.
328    ///
329    /// Note if the input matrix contains scales, shears, or other non-rotation transformations then
330    /// the resulting Euler angles will be ill-defined.
331    ///
332    /// # Panics
333    ///
334    /// Will panic if any input matrix column is not normalized when `glam_assert` is enabled.
335    #[inline]
336    #[must_use]
337    pub fn to_euler(&self, order: EulerRot) -> (f32, f32, f32) {
338        glam_assert!(
339            self.x_axis.is_normalized()
340                && self.y_axis.is_normalized()
341                && self.z_axis.is_normalized()
342        );
343        self.to_euler_angles(order)
344    }
345
346    /// Creates a 3D rotation matrix from `angle` (in radians) around the x axis.
347    #[inline]
348    #[must_use]
349    pub fn from_rotation_x(angle: f32) -> Self {
350        let (sina, cosa) = math::sin_cos(angle);
351        Self::from_cols(
352            Vec3A::X,
353            Vec3A::new(0.0, cosa, sina),
354            Vec3A::new(0.0, -sina, cosa),
355        )
356    }
357
358    /// Creates a 3D rotation matrix from `angle` (in radians) around the y axis.
359    #[inline]
360    #[must_use]
361    pub fn from_rotation_y(angle: f32) -> Self {
362        let (sina, cosa) = math::sin_cos(angle);
363        Self::from_cols(
364            Vec3A::new(cosa, 0.0, -sina),
365            Vec3A::Y,
366            Vec3A::new(sina, 0.0, cosa),
367        )
368    }
369
370    /// Creates a 3D rotation matrix from `angle` (in radians) around the z axis.
371    #[inline]
372    #[must_use]
373    pub fn from_rotation_z(angle: f32) -> Self {
374        let (sina, cosa) = math::sin_cos(angle);
375        Self::from_cols(
376            Vec3A::new(cosa, sina, 0.0),
377            Vec3A::new(-sina, cosa, 0.0),
378            Vec3A::Z,
379        )
380    }
381
382    /// Creates an affine transformation matrix from the given 2D `translation`.
383    ///
384    /// The resulting matrix can be used to transform 2D points and vectors. See
385    /// [`Self::transform_point2()`] and [`Self::transform_vector2()`].
386    #[inline]
387    #[must_use]
388    pub fn from_translation(translation: Vec2) -> Self {
389        Self::from_cols(
390            Vec3A::X,
391            Vec3A::Y,
392            Vec3A::new(translation.x, translation.y, 1.0),
393        )
394    }
395
396    /// Creates an affine transformation matrix from the given 2D rotation `angle` (in
397    /// radians).
398    ///
399    /// The resulting matrix can be used to transform 2D points and vectors. See
400    /// [`Self::transform_point2()`] and [`Self::transform_vector2()`].
401    #[inline]
402    #[must_use]
403    pub fn from_angle(angle: f32) -> Self {
404        let (sin, cos) = math::sin_cos(angle);
405        Self::from_cols(
406            Vec3A::new(cos, sin, 0.0),
407            Vec3A::new(-sin, cos, 0.0),
408            Vec3A::Z,
409        )
410    }
411
412    /// Creates an affine transformation matrix from the given 2D `scale`, rotation `angle` (in
413    /// radians) and `translation`.
414    ///
415    /// The resulting matrix can be used to transform 2D points and vectors. See
416    /// [`Self::transform_point2()`] and [`Self::transform_vector2()`].
417    #[inline]
418    #[must_use]
419    pub fn from_scale_angle_translation(scale: Vec2, angle: f32, translation: Vec2) -> Self {
420        let (sin, cos) = math::sin_cos(angle);
421        Self::from_cols(
422            Vec3A::new(cos * scale.x, sin * scale.x, 0.0),
423            Vec3A::new(-sin * scale.y, cos * scale.y, 0.0),
424            Vec3A::new(translation.x, translation.y, 1.0),
425        )
426    }
427
428    /// Creates an affine transformation matrix from the given non-uniform 2D `scale`.
429    ///
430    /// The resulting matrix can be used to transform 2D points and vectors. See
431    /// [`Self::transform_point2()`] and [`Self::transform_vector2()`].
432    ///
433    /// # Panics
434    ///
435    /// Will panic if all elements of `scale` are zero when `glam_assert` is enabled.
436    #[inline]
437    #[must_use]
438    pub fn from_scale(scale: Vec2) -> Self {
439        // Do not panic as long as any component is non-zero
440        glam_assert!(scale.cmpne(Vec2::ZERO).any());
441
442        Self::from_cols(
443            Vec3A::new(scale.x, 0.0, 0.0),
444            Vec3A::new(0.0, scale.y, 0.0),
445            Vec3A::Z,
446        )
447    }
448
449    /// Creates an affine transformation matrix from the given 2x2 matrix.
450    ///
451    /// The resulting matrix can be used to transform 2D points and vectors. See
452    /// [`Self::transform_point2()`] and [`Self::transform_vector2()`].
453    #[inline]
454    pub fn from_mat2(m: Mat2) -> Self {
455        Self::from_cols((m.x_axis, 0.0).into(), (m.y_axis, 0.0).into(), Vec3A::Z)
456    }
457
458    /// Creates a 3x3 matrix from the first 9 values in `slice`.
459    ///
460    /// # Panics
461    ///
462    /// Panics if `slice` is less than 9 elements long.
463    #[inline]
464    #[must_use]
465    pub const fn from_cols_slice(slice: &[f32]) -> Self {
466        Self::new(
467            slice[0], slice[1], slice[2], slice[3], slice[4], slice[5], slice[6], slice[7],
468            slice[8],
469        )
470    }
471
472    /// Writes the columns of `self` to the first 9 elements in `slice`.
473    ///
474    /// # Panics
475    ///
476    /// Panics if `slice` is less than 9 elements long.
477    #[inline]
478    pub fn write_cols_to_slice(self, slice: &mut [f32]) {
479        slice[0] = self.x_axis.x;
480        slice[1] = self.x_axis.y;
481        slice[2] = self.x_axis.z;
482        slice[3] = self.y_axis.x;
483        slice[4] = self.y_axis.y;
484        slice[5] = self.y_axis.z;
485        slice[6] = self.z_axis.x;
486        slice[7] = self.z_axis.y;
487        slice[8] = self.z_axis.z;
488    }
489
490    /// Returns the matrix column for the given `index`.
491    ///
492    /// # Panics
493    ///
494    /// Panics if `index` is greater than 2.
495    #[inline]
496    #[must_use]
497    pub fn col(&self, index: usize) -> Vec3A {
498        match index {
499            0 => self.x_axis,
500            1 => self.y_axis,
501            2 => self.z_axis,
502            _ => panic!("index out of bounds"),
503        }
504    }
505
506    /// Returns a mutable reference to the matrix column for the given `index`.
507    ///
508    /// # Panics
509    ///
510    /// Panics if `index` is greater than 2.
511    #[inline]
512    pub fn col_mut(&mut self, index: usize) -> &mut Vec3A {
513        match index {
514            0 => &mut self.x_axis,
515            1 => &mut self.y_axis,
516            2 => &mut self.z_axis,
517            _ => panic!("index out of bounds"),
518        }
519    }
520
521    /// Returns the matrix row for the given `index`.
522    ///
523    /// # Panics
524    ///
525    /// Panics if `index` is greater than 2.
526    #[inline]
527    #[must_use]
528    pub fn row(&self, index: usize) -> Vec3A {
529        match index {
530            0 => Vec3A::new(self.x_axis.x, self.y_axis.x, self.z_axis.x),
531            1 => Vec3A::new(self.x_axis.y, self.y_axis.y, self.z_axis.y),
532            2 => Vec3A::new(self.x_axis.z, self.y_axis.z, self.z_axis.z),
533            _ => panic!("index out of bounds"),
534        }
535    }
536
537    /// Returns `true` if, and only if, all elements are finite.
538    /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
539    #[inline]
540    #[must_use]
541    pub fn is_finite(&self) -> bool {
542        self.x_axis.is_finite() && self.y_axis.is_finite() && self.z_axis.is_finite()
543    }
544
545    /// Returns `true` if any elements are `NaN`.
546    #[inline]
547    #[must_use]
548    pub fn is_nan(&self) -> bool {
549        self.x_axis.is_nan() || self.y_axis.is_nan() || self.z_axis.is_nan()
550    }
551
552    /// Returns the transpose of `self`.
553    #[inline]
554    #[must_use]
555    pub fn transpose(&self) -> Self {
556        unsafe {
557            let tmp0 = _mm_shuffle_ps(self.x_axis.0, self.y_axis.0, 0b01_00_01_00);
558            let tmp1 = _mm_shuffle_ps(self.x_axis.0, self.y_axis.0, 0b11_10_11_10);
559
560            Self {
561                x_axis: Vec3A(_mm_shuffle_ps(tmp0, self.z_axis.0, 0b00_00_10_00)),
562                y_axis: Vec3A(_mm_shuffle_ps(tmp0, self.z_axis.0, 0b01_01_11_01)),
563                z_axis: Vec3A(_mm_shuffle_ps(tmp1, self.z_axis.0, 0b10_10_10_00)),
564            }
565        }
566    }
567
568    /// Returns the determinant of `self`.
569    #[inline]
570    #[must_use]
571    pub fn determinant(&self) -> f32 {
572        self.z_axis.dot(self.x_axis.cross(self.y_axis))
573    }
574
575    /// Returns the inverse of `self`.
576    ///
577    /// If the matrix is not invertible the returned matrix will be invalid.
578    ///
579    /// # Panics
580    ///
581    /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled.
582    #[inline]
583    #[must_use]
584    pub fn inverse(&self) -> Self {
585        let tmp0 = self.y_axis.cross(self.z_axis);
586        let tmp1 = self.z_axis.cross(self.x_axis);
587        let tmp2 = self.x_axis.cross(self.y_axis);
588        let det = self.z_axis.dot(tmp2);
589        glam_assert!(det != 0.0);
590        let inv_det = Vec3A::splat(det.recip());
591        Self::from_cols(tmp0.mul(inv_det), tmp1.mul(inv_det), tmp2.mul(inv_det)).transpose()
592    }
593
594    /// Transforms the given 2D vector as a point.
595    ///
596    /// This is the equivalent of multiplying `rhs` as a 3D vector where `z` is `1`.
597    ///
598    /// This method assumes that `self` contains a valid affine transform.
599    ///
600    /// # Panics
601    ///
602    /// Will panic if the 2nd row of `self` is not `(0, 0, 1)` when `glam_assert` is enabled.
603    #[inline]
604    #[must_use]
605    pub fn transform_point2(&self, rhs: Vec2) -> Vec2 {
606        glam_assert!(self.row(2).abs_diff_eq(Vec3A::Z, 1e-6));
607        Mat2::from_cols(self.x_axis.xy(), self.y_axis.xy()) * rhs + self.z_axis.xy()
608    }
609
610    /// Rotates the given 2D vector.
611    ///
612    /// This is the equivalent of multiplying `rhs` as a 3D vector where `z` is `0`.
613    ///
614    /// This method assumes that `self` contains a valid affine transform.
615    ///
616    /// # Panics
617    ///
618    /// Will panic if the 2nd row of `self` is not `(0, 0, 1)` when `glam_assert` is enabled.
619    #[inline]
620    #[must_use]
621    pub fn transform_vector2(&self, rhs: Vec2) -> Vec2 {
622        glam_assert!(self.row(2).abs_diff_eq(Vec3A::Z, 1e-6));
623        Mat2::from_cols(self.x_axis.xy(), self.y_axis.xy()) * rhs
624    }
625
626    /// Creates a left-handed view matrix using a facing direction and an up direction.
627    ///
628    /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
629    ///
630    /// # Panics
631    ///
632    /// Will panic if `dir` or `up` are not normalized when `glam_assert` is enabled.
633    #[inline]
634    #[must_use]
635    pub fn look_to_lh(dir: Vec3, up: Vec3) -> Self {
636        Self::look_to_rh(-dir, up)
637    }
638
639    /// Creates a right-handed view matrix using a facing direction and an up direction.
640    ///
641    /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
642    ///
643    /// # Panics
644    ///
645    /// Will panic if `dir` or `up` are not normalized when `glam_assert` is enabled.
646    #[inline]
647    #[must_use]
648    pub fn look_to_rh(dir: Vec3, up: Vec3) -> Self {
649        glam_assert!(dir.is_normalized());
650        glam_assert!(up.is_normalized());
651        let f = dir;
652        let s = f.cross(up).normalize();
653        let u = s.cross(f);
654
655        Self::from_cols(
656            Vec3A::new(s.x, u.x, -f.x),
657            Vec3A::new(s.y, u.y, -f.y),
658            Vec3A::new(s.z, u.z, -f.z),
659        )
660    }
661
662    /// Creates a left-handed view matrix using a camera position, a focal point and an up
663    /// direction.
664    ///
665    /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
666    ///
667    /// # Panics
668    ///
669    /// Will panic if `up` is not normalized when `glam_assert` is enabled.
670    #[inline]
671    #[must_use]
672    pub fn look_at_lh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
673        Self::look_to_lh(center.sub(eye).normalize(), up)
674    }
675
676    /// Creates a right-handed view matrix using a camera position, a focal point and an up
677    /// direction.
678    ///
679    /// For a view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
680    ///
681    /// # Panics
682    ///
683    /// Will panic if `up` is not normalized when `glam_assert` is enabled.
684    #[inline]
685    pub fn look_at_rh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
686        Self::look_to_rh(center.sub(eye).normalize(), up)
687    }
688
689    /// Transforms a 3D vector.
690    #[inline]
691    #[must_use]
692    pub fn mul_vec3(&self, rhs: Vec3) -> Vec3 {
693        self.mul_vec3a(rhs.into()).into()
694    }
695
696    /// Transforms a [`Vec3A`].
697    #[inline]
698    #[must_use]
699    pub fn mul_vec3a(&self, rhs: Vec3A) -> Vec3A {
700        let mut res = self.x_axis.mul(rhs.xxx());
701        res = res.add(self.y_axis.mul(rhs.yyy()));
702        res = res.add(self.z_axis.mul(rhs.zzz()));
703        res
704    }
705
706    /// Multiplies two 3x3 matrices.
707    #[inline]
708    #[must_use]
709    pub fn mul_mat3(&self, rhs: &Self) -> Self {
710        Self::from_cols(
711            self.mul(rhs.x_axis),
712            self.mul(rhs.y_axis),
713            self.mul(rhs.z_axis),
714        )
715    }
716
717    /// Adds two 3x3 matrices.
718    #[inline]
719    #[must_use]
720    pub fn add_mat3(&self, rhs: &Self) -> Self {
721        Self::from_cols(
722            self.x_axis.add(rhs.x_axis),
723            self.y_axis.add(rhs.y_axis),
724            self.z_axis.add(rhs.z_axis),
725        )
726    }
727
728    /// Subtracts two 3x3 matrices.
729    #[inline]
730    #[must_use]
731    pub fn sub_mat3(&self, rhs: &Self) -> Self {
732        Self::from_cols(
733            self.x_axis.sub(rhs.x_axis),
734            self.y_axis.sub(rhs.y_axis),
735            self.z_axis.sub(rhs.z_axis),
736        )
737    }
738
739    /// Multiplies a 3x3 matrix by a scalar.
740    #[inline]
741    #[must_use]
742    pub fn mul_scalar(&self, rhs: f32) -> Self {
743        Self::from_cols(
744            self.x_axis.mul(rhs),
745            self.y_axis.mul(rhs),
746            self.z_axis.mul(rhs),
747        )
748    }
749
750    /// Divides a 3x3 matrix by a scalar.
751    #[inline]
752    #[must_use]
753    pub fn div_scalar(&self, rhs: f32) -> Self {
754        let rhs = Vec3A::splat(rhs);
755        Self::from_cols(
756            self.x_axis.div(rhs),
757            self.y_axis.div(rhs),
758            self.z_axis.div(rhs),
759        )
760    }
761
762    /// Returns true if the absolute difference of all elements between `self` and `rhs`
763    /// is less than or equal to `max_abs_diff`.
764    ///
765    /// This can be used to compare if two matrices contain similar elements. It works best
766    /// when comparing with a known value. The `max_abs_diff` that should be used used
767    /// depends on the values being compared against.
768    ///
769    /// For more see
770    /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
771    #[inline]
772    #[must_use]
773    pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
774        self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
775            && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
776            && self.z_axis.abs_diff_eq(rhs.z_axis, max_abs_diff)
777    }
778
779    /// Takes the absolute value of each element in `self`
780    #[inline]
781    #[must_use]
782    pub fn abs(&self) -> Self {
783        Self::from_cols(self.x_axis.abs(), self.y_axis.abs(), self.z_axis.abs())
784    }
785
786    #[inline]
787    pub fn as_dmat3(&self) -> DMat3 {
788        DMat3::from_cols(
789            self.x_axis.as_dvec3(),
790            self.y_axis.as_dvec3(),
791            self.z_axis.as_dvec3(),
792        )
793    }
794}
795
796impl Default for Mat3A {
797    #[inline]
798    fn default() -> Self {
799        Self::IDENTITY
800    }
801}
802
803impl Add<Mat3A> for Mat3A {
804    type Output = Self;
805    #[inline]
806    fn add(self, rhs: Self) -> Self::Output {
807        self.add_mat3(&rhs)
808    }
809}
810
811impl AddAssign<Mat3A> for Mat3A {
812    #[inline]
813    fn add_assign(&mut self, rhs: Self) {
814        *self = self.add_mat3(&rhs);
815    }
816}
817
818impl Sub<Mat3A> for Mat3A {
819    type Output = Self;
820    #[inline]
821    fn sub(self, rhs: Self) -> Self::Output {
822        self.sub_mat3(&rhs)
823    }
824}
825
826impl SubAssign<Mat3A> for Mat3A {
827    #[inline]
828    fn sub_assign(&mut self, rhs: Self) {
829        *self = self.sub_mat3(&rhs);
830    }
831}
832
833impl Neg for Mat3A {
834    type Output = Self;
835    #[inline]
836    fn neg(self) -> Self::Output {
837        Self::from_cols(self.x_axis.neg(), self.y_axis.neg(), self.z_axis.neg())
838    }
839}
840
841impl Mul<Mat3A> for Mat3A {
842    type Output = Self;
843    #[inline]
844    fn mul(self, rhs: Self) -> Self::Output {
845        self.mul_mat3(&rhs)
846    }
847}
848
849impl MulAssign<Mat3A> for Mat3A {
850    #[inline]
851    fn mul_assign(&mut self, rhs: Self) {
852        *self = self.mul_mat3(&rhs);
853    }
854}
855
856impl Mul<Vec3A> for Mat3A {
857    type Output = Vec3A;
858    #[inline]
859    fn mul(self, rhs: Vec3A) -> Self::Output {
860        self.mul_vec3a(rhs)
861    }
862}
863
864impl Mul<Mat3A> for f32 {
865    type Output = Mat3A;
866    #[inline]
867    fn mul(self, rhs: Mat3A) -> Self::Output {
868        rhs.mul_scalar(self)
869    }
870}
871
872impl Mul<f32> for Mat3A {
873    type Output = Self;
874    #[inline]
875    fn mul(self, rhs: f32) -> Self::Output {
876        self.mul_scalar(rhs)
877    }
878}
879
880impl MulAssign<f32> for Mat3A {
881    #[inline]
882    fn mul_assign(&mut self, rhs: f32) {
883        *self = self.mul_scalar(rhs);
884    }
885}
886
887impl Div<Mat3A> for f32 {
888    type Output = Mat3A;
889    #[inline]
890    fn div(self, rhs: Mat3A) -> Self::Output {
891        rhs.div_scalar(self)
892    }
893}
894
895impl Div<f32> for Mat3A {
896    type Output = Self;
897    #[inline]
898    fn div(self, rhs: f32) -> Self::Output {
899        self.div_scalar(rhs)
900    }
901}
902
903impl DivAssign<f32> for Mat3A {
904    #[inline]
905    fn div_assign(&mut self, rhs: f32) {
906        *self = self.div_scalar(rhs);
907    }
908}
909
910impl Mul<Vec3> for Mat3A {
911    type Output = Vec3;
912    #[inline]
913    fn mul(self, rhs: Vec3) -> Vec3 {
914        self.mul_vec3a(rhs.into()).into()
915    }
916}
917
918impl From<Mat3> for Mat3A {
919    #[inline]
920    fn from(m: Mat3) -> Self {
921        Self {
922            x_axis: m.x_axis.into(),
923            y_axis: m.y_axis.into(),
924            z_axis: m.z_axis.into(),
925        }
926    }
927}
928
929impl Sum<Self> for Mat3A {
930    fn sum<I>(iter: I) -> Self
931    where
932        I: Iterator<Item = Self>,
933    {
934        iter.fold(Self::ZERO, Self::add)
935    }
936}
937
938impl<'a> Sum<&'a Self> for Mat3A {
939    fn sum<I>(iter: I) -> Self
940    where
941        I: Iterator<Item = &'a Self>,
942    {
943        iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
944    }
945}
946
947impl Product for Mat3A {
948    fn product<I>(iter: I) -> Self
949    where
950        I: Iterator<Item = Self>,
951    {
952        iter.fold(Self::IDENTITY, Self::mul)
953    }
954}
955
956impl<'a> Product<&'a Self> for Mat3A {
957    fn product<I>(iter: I) -> Self
958    where
959        I: Iterator<Item = &'a Self>,
960    {
961        iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
962    }
963}
964
965impl PartialEq for Mat3A {
966    #[inline]
967    fn eq(&self, rhs: &Self) -> bool {
968        self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis) && self.z_axis.eq(&rhs.z_axis)
969    }
970}
971
972impl fmt::Debug for Mat3A {
973    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
974        fmt.debug_struct(stringify!(Mat3A))
975            .field("x_axis", &self.x_axis)
976            .field("y_axis", &self.y_axis)
977            .field("z_axis", &self.z_axis)
978            .finish()
979    }
980}
981
982impl fmt::Display for Mat3A {
983    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
984        if let Some(p) = f.precision() {
985            write!(
986                f,
987                "[{:.*}, {:.*}, {:.*}]",
988                p, self.x_axis, p, self.y_axis, p, self.z_axis
989            )
990        } else {
991            write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
992        }
993    }
994}