glam/f64/
dmat3.rs

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