Skip to main content

glam/f64/
dmat4.rs

1// Generated from mat.rs.tera template. Edit the template, not the generated file.
2
3use crate::{
4    DMat3, DQuat, DVec3, DVec4, EulerRot, Mat4,
5    euler::{FromEuler, ToEuler},
6    f64::math,
7    swizzles::*,
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-08")]
14use zerocopy_derive_08::*;
15
16/// Creates a 4x4 matrix from four column vectors.
17#[inline(always)]
18#[must_use]
19pub const fn dmat4(x_axis: DVec4, y_axis: DVec4, z_axis: DVec4, w_axis: DVec4) -> DMat4 {
20    DMat4::from_cols(x_axis, y_axis, z_axis, w_axis)
21}
22
23/// A 4x4 column major matrix.
24///
25/// If you are primarily dealing with 3D affine transformations
26/// considering using [`DAffine3`](crate::DAffine3) which is faster than a 4x4 matrix
27/// for some affine operations.
28///
29/// Affine transformations including 3D translation, rotation and scale can be created
30/// using methods such as [`Self::from_translation()`], [`Self::from_quat()`],
31/// [`Self::from_scale()`] and [`Self::from_scale_rotation_translation()`].
32///
33/// The [`Self::transform_point3()`] and [`Self::transform_vector3()`] convenience methods
34/// are provided for performing affine transformations on 3D vectors and points. These
35/// multiply 3D inputs as 4D vectors with an implicit `w` value of `1` for points and `0`
36/// for vectors respectively. These methods assume that `Self` contains a valid affine
37/// transform.
38#[derive(Clone, Copy)]
39#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
40#[cfg_attr(
41    feature = "zerocopy-08",
42    derive(FromBytes, Immutable, IntoBytes, KnownLayout)
43)]
44#[cfg_attr(feature = "cuda", repr(align(16)))]
45#[repr(C)]
46pub struct DMat4 {
47    pub x_axis: DVec4,
48    pub y_axis: DVec4,
49    pub z_axis: DVec4,
50    pub w_axis: DVec4,
51}
52
53impl DMat4 {
54    /// A 4x4 matrix with all elements set to `0.0`.
55    pub const ZERO: Self = Self::from_cols(DVec4::ZERO, DVec4::ZERO, DVec4::ZERO, DVec4::ZERO);
56
57    /// A 4x4 identity matrix, where all diagonal elements are `1`, and all off-diagonal elements are `0`.
58    pub const IDENTITY: Self = Self::from_cols(DVec4::X, DVec4::Y, DVec4::Z, DVec4::W);
59
60    /// All NAN:s.
61    pub const NAN: Self = Self::from_cols(DVec4::NAN, DVec4::NAN, DVec4::NAN, DVec4::NAN);
62
63    #[allow(clippy::too_many_arguments)]
64    #[inline(always)]
65    #[must_use]
66    const fn new(
67        m00: f64,
68        m01: f64,
69        m02: f64,
70        m03: f64,
71        m10: f64,
72        m11: f64,
73        m12: f64,
74        m13: f64,
75        m20: f64,
76        m21: f64,
77        m22: f64,
78        m23: f64,
79        m30: f64,
80        m31: f64,
81        m32: f64,
82        m33: f64,
83    ) -> Self {
84        Self {
85            x_axis: DVec4::new(m00, m01, m02, m03),
86            y_axis: DVec4::new(m10, m11, m12, m13),
87            z_axis: DVec4::new(m20, m21, m22, m23),
88            w_axis: DVec4::new(m30, m31, m32, m33),
89        }
90    }
91
92    /// Creates a 4x4 matrix from four column vectors.
93    ///
94    /// See also [`Self::from_rows`] when the data is in row major order.
95    #[inline(always)]
96    #[must_use]
97    pub const fn from_cols(x_axis: DVec4, y_axis: DVec4, z_axis: DVec4, w_axis: DVec4) -> Self {
98        Self {
99            x_axis,
100            y_axis,
101            z_axis,
102            w_axis,
103        }
104    }
105
106    /// Creates a 4x4 matrix from four row vectors.
107    ///
108    /// Matrices are stored in column major order, so the given rows are permuted into
109    /// the matrix layout. Use [`Self::from_cols`] instead when the data is already in
110    /// column major order.
111    #[inline(always)]
112    #[must_use]
113    pub const fn from_rows(row0: DVec4, row1: DVec4, row2: DVec4, row3: DVec4) -> Self {
114        let [m00, m01, m02, m03] = row0.to_array();
115        let [m10, m11, m12, m13] = row1.to_array();
116        let [m20, m21, m22, m23] = row2.to_array();
117        let [m30, m31, m32, m33] = row3.to_array();
118        Self::new(
119            m00, m10, m20, m30, m01, m11, m21, m31, m02, m12, m22, m32, m03, m13, m23, m33,
120        )
121    }
122
123    /// Creates a 4x4 matrix from a `[f64; 16]` array stored in column major order.
124    ///
125    /// If the data is in row major order use [`Self::from_rows_array`] instead.
126    #[inline]
127    #[must_use]
128    pub const fn from_cols_array(m: &[f64; 16]) -> Self {
129        Self::new(
130            m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8], m[9], m[10], m[11], m[12], m[13],
131            m[14], m[15],
132        )
133    }
134
135    /// Creates a `[f64; 16]` array storing data in column major order.
136    ///
137    /// If you require the data in row major order use [`Self::to_rows_array`] instead.
138    #[inline]
139    #[must_use]
140    pub const fn to_cols_array(&self) -> [f64; 16] {
141        [
142            self.x_axis.x,
143            self.x_axis.y,
144            self.x_axis.z,
145            self.x_axis.w,
146            self.y_axis.x,
147            self.y_axis.y,
148            self.y_axis.z,
149            self.y_axis.w,
150            self.z_axis.x,
151            self.z_axis.y,
152            self.z_axis.z,
153            self.z_axis.w,
154            self.w_axis.x,
155            self.w_axis.y,
156            self.w_axis.z,
157            self.w_axis.w,
158        ]
159    }
160
161    /// Creates a 4x4 matrix from a `[[f64; 4]; 4]` 4D array stored in column major order.
162    ///
163    /// If the data is in row major order `transpose` the returned matrix.
164    #[inline]
165    #[must_use]
166    pub const fn from_cols_array_2d(m: &[[f64; 4]; 4]) -> Self {
167        Self::from_cols(
168            DVec4::from_array(m[0]),
169            DVec4::from_array(m[1]),
170            DVec4::from_array(m[2]),
171            DVec4::from_array(m[3]),
172        )
173    }
174
175    /// Creates a `[[f64; 4]; 4]` 4D array storing data in column major order.
176    ///
177    /// If you require row major order `transpose` the matrix first.
178    #[inline]
179    #[must_use]
180    pub const fn to_cols_array_2d(&self) -> [[f64; 4]; 4] {
181        [
182            self.x_axis.to_array(),
183            self.y_axis.to_array(),
184            self.z_axis.to_array(),
185            self.w_axis.to_array(),
186        ]
187    }
188
189    /// Creates a 4x4 matrix from a `[f64; 16]` array stored in row major order.
190    ///
191    /// Matrices are stored in column major order, so the array is permuted into the
192    /// matrix layout. Use [`Self::from_cols_array`] instead when the data is already in
193    /// column major order.
194    #[inline]
195    #[must_use]
196    pub const fn from_rows_array(m: &[f64; 16]) -> Self {
197        Self::new(
198            m[0], m[4], m[8], m[12], m[1], m[5], m[9], m[13], m[2], m[6], m[10], m[14], m[3], m[7],
199            m[11], m[15],
200        )
201    }
202
203    /// Creates a `[f64; 16]` array storing data in row major order.
204    ///
205    /// Matrices are stored in column major order, so the array is permuted out of the
206    /// column major storage. Use [`Self::to_cols_array`] instead when you want data in
207    /// column major order.
208    #[inline]
209    #[must_use]
210    pub const fn to_rows_array(&self) -> [f64; 16] {
211        let m = self.to_cols_array();
212        [
213            m[0], m[4], m[8], m[12], m[1], m[5], m[9], m[13], m[2], m[6], m[10], m[14], m[3], m[7],
214            m[11], m[15],
215        ]
216    }
217
218    /// Creates a 4x4 matrix with its diagonal set to `diagonal` and all other entries set to 0.
219    #[doc(alias = "scale")]
220    #[inline]
221    #[must_use]
222    pub const fn from_diagonal(diagonal: DVec4) -> Self {
223        Self::new(
224            diagonal.x, 0.0, 0.0, 0.0, 0.0, diagonal.y, 0.0, 0.0, 0.0, 0.0, diagonal.z, 0.0, 0.0,
225            0.0, 0.0, diagonal.w,
226        )
227    }
228
229    #[inline]
230    #[must_use]
231    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
232    fn quat_to_axes(rotation: DQuat) -> (DVec4, DVec4, DVec4) {
233        glam_assert!(rotation.is_normalized());
234
235        let (x, y, z, w) = rotation.into();
236        let x2 = x + x;
237        let y2 = y + y;
238        let z2 = z + z;
239        let xx = x * x2;
240        let xy = x * y2;
241        let xz = x * z2;
242        let yy = y * y2;
243        let yz = y * z2;
244        let zz = z * z2;
245        let wx = w * x2;
246        let wy = w * y2;
247        let wz = w * z2;
248
249        let x_axis = DVec4::new(1.0 - (yy + zz), xy + wz, xz - wy, 0.0);
250        let y_axis = DVec4::new(xy - wz, 1.0 - (xx + zz), yz + wx, 0.0);
251        let z_axis = DVec4::new(xz + wy, yz - wx, 1.0 - (xx + yy), 0.0);
252        (x_axis, y_axis, z_axis)
253    }
254
255    /// Creates an affine transformation matrix from the given 3D `scale`, `rotation` and
256    /// `translation`.
257    ///
258    /// The resulting matrix can be used to transform 3D points and vectors. See
259    /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
260    ///
261    /// # Panics
262    ///
263    /// Will panic if `rotation` is not normalized when `glam_assert` is enabled.
264    #[inline]
265    #[must_use]
266    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
267    pub fn from_scale_rotation_translation(
268        scale: DVec3,
269        rotation: DQuat,
270        translation: DVec3,
271    ) -> Self {
272        let (x_axis, y_axis, z_axis) = Self::quat_to_axes(rotation);
273        Self::from_cols(
274            x_axis.mul(scale.x),
275            y_axis.mul(scale.y),
276            z_axis.mul(scale.z),
277            DVec4::from((translation, 1.0)),
278        )
279    }
280
281    /// Creates an affine transformation matrix from the given 3D `translation`.
282    ///
283    /// The resulting matrix can be used to transform 3D points and vectors. See
284    /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
285    ///
286    /// # Panics
287    ///
288    /// Will panic if `rotation` is not normalized when `glam_assert` is enabled.
289    #[inline]
290    #[must_use]
291    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
292    pub fn from_rotation_translation(rotation: DQuat, translation: DVec3) -> Self {
293        let (x_axis, y_axis, z_axis) = Self::quat_to_axes(rotation);
294        Self::from_cols(x_axis, y_axis, z_axis, DVec4::from((translation, 1.0)))
295    }
296
297    /// Extracts `scale`, `rotation` and `translation` from `self`. The input matrix is
298    /// expected to be a 3D affine transformation matrix otherwise the output will be invalid.
299    ///
300    /// # Panics
301    /// Will panic if `self` is not a valid affine transformation matrix, if the determinant of the
302    /// 3x3 linear part (the rotation and scale part of the transform) is zero, when `glam_assert`
303    /// is enabled.
304    #[inline]
305    #[must_use]
306    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
307    pub fn to_scale_rotation_translation(&self) -> (DVec3, DQuat, DVec3) {
308        glam_assert!(self.row(3).abs_diff_eq(DVec4::W, 1e-6));
309
310        let r = DMat3::from_mat4(*self);
311
312        let det = r.determinant();
313
314        glam_assert!(det != 0.0);
315
316        let scale = DVec3::new(
317            r.x_axis.length() * det.signum(),
318            r.y_axis.length(),
319            r.z_axis.length(),
320        );
321
322        glam_assert!(scale.cmpne(DVec3::ZERO).all());
323
324        let inv_scale = scale.recip();
325
326        let rotation = DQuat::from_rotation_axes(
327            r.x_axis.mul(inv_scale.x),
328            r.y_axis.mul(inv_scale.y),
329            r.z_axis.mul(inv_scale.z),
330        );
331
332        let translation = self.w_axis.xyz();
333
334        (scale, rotation, translation)
335    }
336
337    /// Creates an affine transformation matrix from the given `rotation` quaternion.
338    ///
339    /// The resulting matrix can be used to transform 3D points and vectors. See
340    /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
341    ///
342    /// # Panics
343    ///
344    /// Will panic if `rotation` is not normalized when `glam_assert` is enabled.
345    #[inline]
346    #[must_use]
347    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
348    pub fn from_quat(rotation: DQuat) -> Self {
349        let (x_axis, y_axis, z_axis) = Self::quat_to_axes(rotation);
350        Self::from_cols(x_axis, y_axis, z_axis, DVec4::W)
351    }
352
353    /// Creates an affine transformation matrix from the given 3x3 linear transformation
354    /// matrix.
355    ///
356    /// The resulting matrix can be used to transform 3D points and vectors. See
357    /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
358    #[inline]
359    #[must_use]
360    pub fn from_mat3(m: DMat3) -> Self {
361        Self::from_cols(
362            DVec4::from((m.x_axis, 0.0)),
363            DVec4::from((m.y_axis, 0.0)),
364            DVec4::from((m.z_axis, 0.0)),
365            DVec4::W,
366        )
367    }
368
369    /// Creates an affine transformation matrics from a 3x3 matrix (expressing scale, shear and
370    /// rotation) and a translation vector.
371    ///
372    /// Equivalent to `DMat4::from_translation(translation) * DMat4::from_mat3(mat3)`
373    #[inline]
374    #[must_use]
375    pub fn from_mat3_translation(mat3: DMat3, translation: DVec3) -> Self {
376        Self::from_cols(
377            DVec4::from((mat3.x_axis, 0.0)),
378            DVec4::from((mat3.y_axis, 0.0)),
379            DVec4::from((mat3.z_axis, 0.0)),
380            DVec4::from((translation, 1.0)),
381        )
382    }
383
384    /// Creates an affine transformation matrix from the given 3D `translation`.
385    ///
386    /// The resulting matrix can be used to transform 3D points and vectors. See
387    /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
388    #[inline]
389    #[must_use]
390    pub fn from_translation(translation: DVec3) -> Self {
391        Self::from_cols(
392            DVec4::X,
393            DVec4::Y,
394            DVec4::Z,
395            DVec4::new(translation.x, translation.y, translation.z, 1.0),
396        )
397    }
398
399    /// Creates an affine transformation matrix containing a 3D rotation around a normalized
400    /// rotation `axis` of `angle` (in radians).
401    ///
402    /// The resulting matrix can be used to transform 3D points and vectors. See
403    /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
404    ///
405    /// # Panics
406    ///
407    /// Will panic if `axis` is not normalized when `glam_assert` is enabled.
408    #[inline]
409    #[must_use]
410    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
411    pub fn from_axis_angle(axis: DVec3, angle: f64) -> Self {
412        glam_assert!(axis.is_normalized());
413
414        let (sin, cos) = math::sin_cos(angle);
415        let axis_sin = axis.mul(sin);
416        let axis_sq = axis.mul(axis);
417        let omc = 1.0 - cos;
418        let xyomc = axis.x * axis.y * omc;
419        let xzomc = axis.x * axis.z * omc;
420        let yzomc = axis.y * axis.z * omc;
421        Self::from_cols(
422            DVec4::new(
423                axis_sq.x * omc + cos,
424                xyomc + axis_sin.z,
425                xzomc - axis_sin.y,
426                0.0,
427            ),
428            DVec4::new(
429                xyomc - axis_sin.z,
430                axis_sq.y * omc + cos,
431                yzomc + axis_sin.x,
432                0.0,
433            ),
434            DVec4::new(
435                xzomc + axis_sin.y,
436                yzomc - axis_sin.x,
437                axis_sq.z * omc + cos,
438                0.0,
439            ),
440            DVec4::W,
441        )
442    }
443
444    /// Creates a affine transformation matrix containing a rotation from the given euler
445    /// rotation sequence and angles (in radians).
446    ///
447    /// The resulting matrix can be used to transform 3D points and vectors. See
448    /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
449    #[inline]
450    #[must_use]
451    pub fn from_euler(order: EulerRot, a: f64, b: f64, c: f64) -> Self {
452        Self::from_euler_angles(order, a, b, c)
453    }
454
455    /// Extract Euler angles with the given Euler rotation order.
456    ///
457    /// Note if the upper 3x3 matrix contain scales, shears, or other non-rotation transformations
458    /// then the resulting Euler angles will be ill-defined.
459    ///
460    /// # Panics
461    ///
462    /// Will panic if any column of the upper 3x3 rotation matrix is not normalized when
463    /// `glam_assert` is enabled.
464    #[inline]
465    #[must_use]
466    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
467    pub fn to_euler(&self, order: EulerRot) -> (f64, f64, f64) {
468        glam_assert!(
469            self.x_axis.xyz().is_normalized()
470                && self.y_axis.xyz().is_normalized()
471                && self.z_axis.xyz().is_normalized()
472        );
473        DMat3::from_mat4(*self).to_euler_angles(order)
474    }
475
476    /// Creates an affine transformation matrix containing a 3D rotation around the x axis of
477    /// `angle` (in radians).
478    ///
479    /// The resulting matrix can be used to transform 3D points and vectors. See
480    /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
481    #[inline]
482    #[must_use]
483    pub fn from_rotation_x(angle: f64) -> Self {
484        let (sina, cosa) = math::sin_cos(angle);
485        Self::from_cols(
486            DVec4::X,
487            DVec4::new(0.0, cosa, sina, 0.0),
488            DVec4::new(0.0, -sina, cosa, 0.0),
489            DVec4::W,
490        )
491    }
492
493    /// Creates an affine transformation matrix containing a 3D rotation around the y axis of
494    /// `angle` (in radians).
495    ///
496    /// The resulting matrix can be used to transform 3D points and vectors. See
497    /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
498    #[inline]
499    #[must_use]
500    pub fn from_rotation_y(angle: f64) -> Self {
501        let (sina, cosa) = math::sin_cos(angle);
502        Self::from_cols(
503            DVec4::new(cosa, 0.0, -sina, 0.0),
504            DVec4::Y,
505            DVec4::new(sina, 0.0, cosa, 0.0),
506            DVec4::W,
507        )
508    }
509
510    /// Creates an affine transformation matrix containing a 3D rotation around the z axis of
511    /// `angle` (in radians).
512    ///
513    /// The resulting matrix can be used to transform 3D points and vectors. See
514    /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
515    #[inline]
516    #[must_use]
517    pub fn from_rotation_z(angle: f64) -> Self {
518        let (sina, cosa) = math::sin_cos(angle);
519        Self::from_cols(
520            DVec4::new(cosa, sina, 0.0, 0.0),
521            DVec4::new(-sina, cosa, 0.0, 0.0),
522            DVec4::Z,
523            DVec4::W,
524        )
525    }
526
527    /// Creates an affine transformation matrix containing the given 3D non-uniform `scale`.
528    ///
529    /// The resulting matrix can be used to transform 3D points and vectors. See
530    /// [`Self::transform_point3()`] and [`Self::transform_vector3()`].
531    ///
532    /// # Panics
533    ///
534    /// Will panic if all elements of `scale` are zero when `glam_assert` is enabled.
535    #[inline]
536    #[must_use]
537    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
538    pub fn from_scale(scale: DVec3) -> Self {
539        // Do not panic as long as any component is non-zero
540        glam_assert!(scale.cmpne(DVec3::ZERO).any());
541
542        Self::from_cols(
543            DVec4::new(scale.x, 0.0, 0.0, 0.0),
544            DVec4::new(0.0, scale.y, 0.0, 0.0),
545            DVec4::new(0.0, 0.0, scale.z, 0.0),
546            DVec4::W,
547        )
548    }
549
550    /// Creates a 4x4 matrix from the first 16 values in `slice`.
551    ///
552    /// See also [`Self::from_rows_slice`] when the slice is in row major order.
553    ///
554    /// # Panics
555    ///
556    /// Panics if `slice` is less than 16 elements long.
557    #[inline]
558    #[must_use]
559    #[track_caller]
560    pub const fn from_cols_slice(slice: &[f64]) -> Self {
561        Self::new(
562            slice[0], slice[1], slice[2], slice[3], slice[4], slice[5], slice[6], slice[7],
563            slice[8], slice[9], slice[10], slice[11], slice[12], slice[13], slice[14], slice[15],
564        )
565    }
566
567    /// Writes the columns of `self` to the first 16 elements in `slice`.
568    ///
569    /// # Panics
570    ///
571    /// Panics if `slice` is less than 16 elements long.
572    #[inline]
573    #[track_caller]
574    pub fn write_cols_to_slice(&self, slice: &mut [f64]) {
575        slice[0] = self.x_axis.x;
576        slice[1] = self.x_axis.y;
577        slice[2] = self.x_axis.z;
578        slice[3] = self.x_axis.w;
579        slice[4] = self.y_axis.x;
580        slice[5] = self.y_axis.y;
581        slice[6] = self.y_axis.z;
582        slice[7] = self.y_axis.w;
583        slice[8] = self.z_axis.x;
584        slice[9] = self.z_axis.y;
585        slice[10] = self.z_axis.z;
586        slice[11] = self.z_axis.w;
587        slice[12] = self.w_axis.x;
588        slice[13] = self.w_axis.y;
589        slice[14] = self.w_axis.z;
590        slice[15] = self.w_axis.w;
591    }
592
593    /// Creates a 4x4 matrix from the first 16 values in `slice`, stored in row
594    /// major order.
595    ///
596    /// Matrices are stored in column major order, so the slice is permuted into the
597    /// matrix layout. Use [`Self::from_cols_slice`] instead when the slice is already in
598    /// column major order.
599    ///
600    /// # Panics
601    ///
602    /// Panics if `slice` is less than 16 elements long.
603    #[inline]
604    #[must_use]
605    #[track_caller]
606    pub const fn from_rows_slice(slice: &[f64]) -> Self {
607        Self::new(
608            slice[0], slice[4], slice[8], slice[12], slice[1], slice[5], slice[9], slice[13],
609            slice[2], slice[6], slice[10], slice[14], slice[3], slice[7], slice[11], slice[15],
610        )
611    }
612
613    /// Returns the matrix column for the given `index`.
614    ///
615    /// # Panics
616    ///
617    /// Panics if `index` is greater than 3.
618    #[inline]
619    #[must_use]
620    #[track_caller]
621    pub fn col(&self, index: usize) -> DVec4 {
622        match index {
623            0 => self.x_axis,
624            1 => self.y_axis,
625            2 => self.z_axis,
626            3 => self.w_axis,
627            _ => panic!("index out of bounds"),
628        }
629    }
630
631    /// Returns a mutable reference to the matrix column for the given `index`.
632    ///
633    /// # Panics
634    ///
635    /// Panics if `index` is greater than 3.
636    #[inline]
637    #[track_caller]
638    pub fn col_mut(&mut self, index: usize) -> &mut DVec4 {
639        match index {
640            0 => &mut self.x_axis,
641            1 => &mut self.y_axis,
642            2 => &mut self.z_axis,
643            3 => &mut self.w_axis,
644            _ => panic!("index out of bounds"),
645        }
646    }
647
648    /// Returns the matrix row for the given `index`.
649    ///
650    /// See also [`Self::set_row`] when you need to change the row.
651    ///
652    /// # Panics
653    ///
654    /// Panics if `index` is greater than 3.
655    #[inline]
656    #[must_use]
657    #[track_caller]
658    pub fn row(&self, index: usize) -> DVec4 {
659        match index {
660            0 => DVec4::new(self.x_axis.x, self.y_axis.x, self.z_axis.x, self.w_axis.x),
661            1 => DVec4::new(self.x_axis.y, self.y_axis.y, self.z_axis.y, self.w_axis.y),
662            2 => DVec4::new(self.x_axis.z, self.y_axis.z, self.z_axis.z, self.w_axis.z),
663            3 => DVec4::new(self.x_axis.w, self.y_axis.w, self.z_axis.w, self.w_axis.w),
664            _ => panic!("index out of bounds"),
665        }
666    }
667
668    /// Sets the matrix row for the given `index`.
669    ///
670    /// Matrices are stored in column major order, so the row is spread across all
671    /// 4 columns and writing it touches every column. Use [`Self::col_mut`]
672    /// instead when you can work with columns. See also [`Self::row`].
673    ///
674    /// # Panics
675    ///
676    /// Panics if `index` is greater than 3.
677    #[inline]
678    #[track_caller]
679    pub fn set_row(&mut self, index: usize, row: DVec4) {
680        match index {
681            0 => {
682                self.x_axis.x = row.x;
683                self.y_axis.x = row.y;
684                self.z_axis.x = row.z;
685                self.w_axis.x = row.w;
686            }
687            1 => {
688                self.x_axis.y = row.x;
689                self.y_axis.y = row.y;
690                self.z_axis.y = row.z;
691                self.w_axis.y = row.w;
692            }
693            2 => {
694                self.x_axis.z = row.x;
695                self.y_axis.z = row.y;
696                self.z_axis.z = row.z;
697                self.w_axis.z = row.w;
698            }
699            3 => {
700                self.x_axis.w = row.x;
701                self.y_axis.w = row.y;
702                self.z_axis.w = row.z;
703                self.w_axis.w = row.w;
704            }
705            _ => panic!("index out of bounds"),
706        }
707    }
708
709    /// Returns `true` if, and only if, all elements are finite.
710    /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
711    #[inline]
712    #[must_use]
713    pub fn is_finite(&self) -> bool {
714        self.x_axis.is_finite()
715            && self.y_axis.is_finite()
716            && self.z_axis.is_finite()
717            && self.w_axis.is_finite()
718    }
719
720    /// Returns `true` if any elements are `NaN`.
721    #[inline]
722    #[must_use]
723    pub fn is_nan(&self) -> bool {
724        self.x_axis.is_nan() || self.y_axis.is_nan() || self.z_axis.is_nan() || self.w_axis.is_nan()
725    }
726
727    /// Returns the transpose of `self`.
728    #[inline]
729    #[must_use]
730    pub fn transpose(&self) -> Self {
731        Self {
732            x_axis: DVec4::new(self.x_axis.x, self.y_axis.x, self.z_axis.x, self.w_axis.x),
733            y_axis: DVec4::new(self.x_axis.y, self.y_axis.y, self.z_axis.y, self.w_axis.y),
734            z_axis: DVec4::new(self.x_axis.z, self.y_axis.z, self.z_axis.z, self.w_axis.z),
735            w_axis: DVec4::new(self.x_axis.w, self.y_axis.w, self.z_axis.w, self.w_axis.w),
736        }
737    }
738
739    /// Returns the diagonal of `self`.
740    #[inline]
741    #[must_use]
742    pub fn diagonal(&self) -> DVec4 {
743        DVec4::new(self.x_axis.x, self.y_axis.y, self.z_axis.z, self.w_axis.w)
744    }
745
746    /// Returns the determinant of `self`.
747    #[must_use]
748    pub fn determinant(&self) -> f64 {
749        let (m00, m01, m02, m03) = self.x_axis.into();
750        let (m10, m11, m12, m13) = self.y_axis.into();
751        let (m20, m21, m22, m23) = self.z_axis.into();
752        let (m30, m31, m32, m33) = self.w_axis.into();
753
754        let a2323 = m22 * m33 - m23 * m32;
755        let a1323 = m21 * m33 - m23 * m31;
756        let a1223 = m21 * m32 - m22 * m31;
757        let a0323 = m20 * m33 - m23 * m30;
758        let a0223 = m20 * m32 - m22 * m30;
759        let a0123 = m20 * m31 - m21 * m30;
760
761        m00 * (m11 * a2323 - m12 * a1323 + m13 * a1223)
762            - m01 * (m10 * a2323 - m12 * a0323 + m13 * a0223)
763            + m02 * (m10 * a1323 - m11 * a0323 + m13 * a0123)
764            - m03 * (m10 * a1223 - m11 * a0223 + m12 * a0123)
765    }
766
767    /// If `CHECKED` is true then if the determinant is zero this function will return a tuple
768    /// containing a zero matrix and false. If the determinant is non zero a tuple containing the
769    /// inverted matrix and true is returned.
770    ///
771    /// If `CHECKED` is false then the determinant is not checked and if it is zero the resulting
772    /// inverted matrix will be invalid. Will panic if the resulting inverted matrix is not finite
773    /// when `glam_assert` is enabled.
774    ///
775    /// A tuple containing the inverted matrix and a bool is used instead of an option here as
776    /// regular Rust enums put the discriminant first which can result in a lot of padding if the
777    /// matrix is aligned.
778    #[inline(always)]
779    #[must_use]
780    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
781    fn inverse_checked<const CHECKED: bool>(&self) -> (Self, bool) {
782        let (m00, m01, m02, m03) = self.x_axis.into();
783        let (m10, m11, m12, m13) = self.y_axis.into();
784        let (m20, m21, m22, m23) = self.z_axis.into();
785        let (m30, m31, m32, m33) = self.w_axis.into();
786
787        let coef00 = m22 * m33 - m32 * m23;
788        let coef02 = m12 * m33 - m32 * m13;
789        let coef03 = m12 * m23 - m22 * m13;
790
791        let coef04 = m21 * m33 - m31 * m23;
792        let coef06 = m11 * m33 - m31 * m13;
793        let coef07 = m11 * m23 - m21 * m13;
794
795        let coef08 = m21 * m32 - m31 * m22;
796        let coef10 = m11 * m32 - m31 * m12;
797        let coef11 = m11 * m22 - m21 * m12;
798
799        let coef12 = m20 * m33 - m30 * m23;
800        let coef14 = m10 * m33 - m30 * m13;
801        let coef15 = m10 * m23 - m20 * m13;
802
803        let coef16 = m20 * m32 - m30 * m22;
804        let coef18 = m10 * m32 - m30 * m12;
805        let coef19 = m10 * m22 - m20 * m12;
806
807        let coef20 = m20 * m31 - m30 * m21;
808        let coef22 = m10 * m31 - m30 * m11;
809        let coef23 = m10 * m21 - m20 * m11;
810
811        let fac0 = DVec4::new(coef00, coef00, coef02, coef03);
812        let fac1 = DVec4::new(coef04, coef04, coef06, coef07);
813        let fac2 = DVec4::new(coef08, coef08, coef10, coef11);
814        let fac3 = DVec4::new(coef12, coef12, coef14, coef15);
815        let fac4 = DVec4::new(coef16, coef16, coef18, coef19);
816        let fac5 = DVec4::new(coef20, coef20, coef22, coef23);
817
818        let vec0 = DVec4::new(m10, m00, m00, m00);
819        let vec1 = DVec4::new(m11, m01, m01, m01);
820        let vec2 = DVec4::new(m12, m02, m02, m02);
821        let vec3 = DVec4::new(m13, m03, m03, m03);
822
823        let inv0 = vec1.mul(fac0).sub(vec2.mul(fac1)).add(vec3.mul(fac2));
824        let inv1 = vec0.mul(fac0).sub(vec2.mul(fac3)).add(vec3.mul(fac4));
825        let inv2 = vec0.mul(fac1).sub(vec1.mul(fac3)).add(vec3.mul(fac5));
826        let inv3 = vec0.mul(fac2).sub(vec1.mul(fac4)).add(vec2.mul(fac5));
827
828        let sign_a = DVec4::new(1.0, -1.0, 1.0, -1.0);
829        let sign_b = DVec4::new(-1.0, 1.0, -1.0, 1.0);
830
831        let inverse = Self::from_cols(
832            inv0.mul(sign_a),
833            inv1.mul(sign_b),
834            inv2.mul(sign_a),
835            inv3.mul(sign_b),
836        );
837
838        let col0 = DVec4::new(
839            inverse.x_axis.x,
840            inverse.y_axis.x,
841            inverse.z_axis.x,
842            inverse.w_axis.x,
843        );
844
845        let dot0 = self.x_axis.mul(col0);
846        let dot1 = dot0.x + dot0.y + dot0.z + dot0.w;
847        let m = inverse.mul(1.0 / dot1);
848
849        if CHECKED {
850            if !m.is_finite() {
851                return (Self::ZERO, false);
852            }
853        } else {
854            glam_assert!(m.is_finite());
855        }
856
857        (m, true)
858    }
859
860    /// Returns the inverse of `self`.
861    ///
862    /// If the matrix is not invertible the returned matrix will be invalid. The
863    /// returned matrix will also be invalid if the inverse is not finite, which can
864    /// happen when `self` contains very large or very small values. Use
865    /// [`Self::try_inverse`] or [`Self::inverse_or_zero`] to detect these cases.
866    ///
867    /// # Panics
868    ///
869    /// Will panic if the resulting inverted matrix is not finite when `glam_assert`
870    /// is enabled.
871    #[must_use]
872    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
873    pub fn inverse(&self) -> Self {
874        self.inverse_checked::<false>().0
875    }
876
877    /// Returns the inverse of `self` or `None` if the matrix is not invertible, or if
878    /// the inverse is not finite.
879    #[must_use]
880    pub fn try_inverse(&self) -> Option<Self> {
881        let (m, is_valid) = self.inverse_checked::<true>();
882        if is_valid { Some(m) } else { None }
883    }
884
885    /// Returns the inverse of `self` or `DMat4::ZERO` if the matrix is not
886    /// invertible, or if the inverse is not finite.
887    #[must_use]
888    pub fn inverse_or_zero(&self) -> Self {
889        self.inverse_checked::<true>().0
890    }
891
892    /// Transforms the given 3D vector as a point, applying perspective correction.
893    ///
894    /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is `1.0`.
895    /// The perspective divide is performed meaning the resulting 3D vector is divided by `w`.
896    ///
897    /// This method assumes that `self` contains a projective transform.
898    #[inline]
899    #[must_use]
900    pub fn project_point3(&self, rhs: DVec3) -> DVec3 {
901        let mut res = self.x_axis.mul(rhs.x);
902        res = self.y_axis.mul(rhs.y).add(res);
903        res = self.z_axis.mul(rhs.z).add(res);
904        res = self.w_axis.add(res);
905        res = res.div(res.w);
906        res.xyz()
907    }
908
909    /// Transforms the given 3D vector as a point.
910    ///
911    /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is
912    /// `1.0`.
913    ///
914    /// This method assumes that `self` contains a valid affine transform. It does not perform
915    /// a perspective divide, if `self` contains a perspective transform, or if you are unsure,
916    /// the [`Self::project_point3()`] method should be used instead.
917    ///
918    /// # Panics
919    ///
920    /// Will panic if the 3rd row of `self` is not `(0, 0, 0, 1)` when `glam_assert` is enabled.
921    #[inline]
922    #[must_use]
923    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
924    pub fn transform_point3(&self, rhs: DVec3) -> DVec3 {
925        glam_assert!(self.row(3).abs_diff_eq(DVec4::W, 1e-6));
926        let mut res = self.x_axis.mul(rhs.x);
927        res = self.y_axis.mul(rhs.y).add(res);
928        res = self.z_axis.mul(rhs.z).add(res);
929        res = self.w_axis.add(res);
930        res.xyz()
931    }
932
933    /// Transforms the given 3D vector as a direction.
934    ///
935    /// This is the equivalent of multiplying the 3D vector as a 4D vector where `w` is
936    /// `0.0`.
937    ///
938    /// This method assumes that `self` contains a valid affine transform.
939    ///
940    /// # Panics
941    ///
942    /// Will panic if the 3rd row of `self` is not `(0, 0, 0, 1)` when `glam_assert` is enabled.
943    #[inline]
944    #[must_use]
945    #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
946    pub fn transform_vector3(&self, rhs: DVec3) -> DVec3 {
947        glam_assert!(self.row(3).abs_diff_eq(DVec4::W, 1e-6));
948        let mut res = self.x_axis.mul(rhs.x);
949        res = self.y_axis.mul(rhs.y).add(res);
950        res = self.z_axis.mul(rhs.z).add(res);
951        res.xyz()
952    }
953
954    /// Transforms a 4D vector.
955    #[inline]
956    #[must_use]
957    pub fn mul_vec4(&self, rhs: DVec4) -> DVec4 {
958        let mut res = self.x_axis.mul(rhs.x);
959        res = res.add(self.y_axis.mul(rhs.y));
960        res = res.add(self.z_axis.mul(rhs.z));
961        res = res.add(self.w_axis.mul(rhs.w));
962        res
963    }
964
965    /// Transforms a 4D vector by the transpose of `self`.
966    #[inline]
967    #[must_use]
968    pub fn mul_transpose_vec4(&self, rhs: DVec4) -> DVec4 {
969        DVec4::new(
970            self.x_axis.dot(rhs),
971            self.y_axis.dot(rhs),
972            self.z_axis.dot(rhs),
973            self.w_axis.dot(rhs),
974        )
975    }
976
977    /// Multiplies two 4x4 matrices.
978    #[inline]
979    #[must_use]
980    pub fn mul_mat4(&self, rhs: &Self) -> Self {
981        self.mul(rhs)
982    }
983
984    /// Adds two 4x4 matrices.
985    #[inline]
986    #[must_use]
987    pub fn add_mat4(&self, rhs: &Self) -> Self {
988        self.add(rhs)
989    }
990
991    /// Subtracts two 4x4 matrices.
992    #[inline]
993    #[must_use]
994    pub fn sub_mat4(&self, rhs: &Self) -> Self {
995        self.sub(rhs)
996    }
997
998    /// Multiplies a 4x4 matrix by a scalar.
999    #[inline]
1000    #[must_use]
1001    pub fn mul_scalar(&self, rhs: f64) -> Self {
1002        Self::from_cols(
1003            self.x_axis.mul(rhs),
1004            self.y_axis.mul(rhs),
1005            self.z_axis.mul(rhs),
1006            self.w_axis.mul(rhs),
1007        )
1008    }
1009
1010    /// Multiply `self` by a scaling vector `scale`.
1011    /// This is faster than creating a whole diagonal scaling matrix and then multiplying that.
1012    /// This operation is commutative.
1013    #[inline]
1014    #[must_use]
1015    pub fn mul_diagonal_scale(&self, scale: DVec4) -> Self {
1016        Self::from_cols(
1017            self.x_axis * scale.x,
1018            self.y_axis * scale.y,
1019            self.z_axis * scale.z,
1020            self.w_axis * scale.w,
1021        )
1022    }
1023
1024    /// Divides a 4x4 matrix by a scalar.
1025    #[inline]
1026    #[must_use]
1027    pub fn div_scalar(&self, rhs: f64) -> Self {
1028        let rhs = DVec4::splat(rhs);
1029        Self::from_cols(
1030            self.x_axis.div(rhs),
1031            self.y_axis.div(rhs),
1032            self.z_axis.div(rhs),
1033            self.w_axis.div(rhs),
1034        )
1035    }
1036
1037    /// Returns a matrix containing the reciprocal `1.0/n` of each element of `self`.
1038    #[inline]
1039    #[must_use]
1040    pub fn recip(&self) -> Self {
1041        Self::from_cols(
1042            self.x_axis.recip(),
1043            self.y_axis.recip(),
1044            self.z_axis.recip(),
1045            self.w_axis.recip(),
1046        )
1047    }
1048
1049    /// Returns true if the absolute difference of all elements between `self` and `rhs`
1050    /// is less than or equal to `max_abs_diff`.
1051    ///
1052    /// This can be used to compare if two matrices contain similar elements. It works best
1053    /// when comparing with a known value. The `max_abs_diff` that should be used used
1054    /// depends on the values being compared against.
1055    ///
1056    /// For more see
1057    /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
1058    #[inline]
1059    #[must_use]
1060    pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f64) -> bool {
1061        self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
1062            && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
1063            && self.z_axis.abs_diff_eq(rhs.z_axis, max_abs_diff)
1064            && self.w_axis.abs_diff_eq(rhs.w_axis, max_abs_diff)
1065    }
1066
1067    /// Takes the absolute value of each element in `self`
1068    #[inline]
1069    #[must_use]
1070    pub fn abs(&self) -> Self {
1071        Self::from_cols(
1072            self.x_axis.abs(),
1073            self.y_axis.abs(),
1074            self.z_axis.abs(),
1075            self.w_axis.abs(),
1076        )
1077    }
1078
1079    #[cfg(feature = "f64")]
1080    #[inline]
1081    #[must_use]
1082    pub fn as_mat4(&self) -> Mat4 {
1083        Mat4::from_cols(
1084            self.x_axis.as_vec4(),
1085            self.y_axis.as_vec4(),
1086            self.z_axis.as_vec4(),
1087            self.w_axis.as_vec4(),
1088        )
1089    }
1090}
1091
1092impl Default for DMat4 {
1093    #[inline]
1094    fn default() -> Self {
1095        Self::IDENTITY
1096    }
1097}
1098
1099impl Add for DMat4 {
1100    type Output = Self;
1101    #[inline]
1102    fn add(self, rhs: Self) -> Self {
1103        Self::from_cols(
1104            self.x_axis.add(rhs.x_axis),
1105            self.y_axis.add(rhs.y_axis),
1106            self.z_axis.add(rhs.z_axis),
1107            self.w_axis.add(rhs.w_axis),
1108        )
1109    }
1110}
1111
1112impl Add<&Self> for DMat4 {
1113    type Output = Self;
1114    #[inline]
1115    fn add(self, rhs: &Self) -> Self {
1116        self.add(*rhs)
1117    }
1118}
1119
1120impl Add<&DMat4> for &DMat4 {
1121    type Output = DMat4;
1122    #[inline]
1123    fn add(self, rhs: &DMat4) -> DMat4 {
1124        (*self).add(*rhs)
1125    }
1126}
1127
1128impl Add<DMat4> for &DMat4 {
1129    type Output = DMat4;
1130    #[inline]
1131    fn add(self, rhs: DMat4) -> DMat4 {
1132        (*self).add(rhs)
1133    }
1134}
1135
1136impl AddAssign for DMat4 {
1137    #[inline]
1138    fn add_assign(&mut self, rhs: Self) {
1139        *self = self.add(rhs);
1140    }
1141}
1142
1143impl AddAssign<&Self> for DMat4 {
1144    #[inline]
1145    fn add_assign(&mut self, rhs: &Self) {
1146        self.add_assign(*rhs);
1147    }
1148}
1149
1150impl Sub for DMat4 {
1151    type Output = Self;
1152    #[inline]
1153    fn sub(self, rhs: Self) -> Self {
1154        Self::from_cols(
1155            self.x_axis.sub(rhs.x_axis),
1156            self.y_axis.sub(rhs.y_axis),
1157            self.z_axis.sub(rhs.z_axis),
1158            self.w_axis.sub(rhs.w_axis),
1159        )
1160    }
1161}
1162
1163impl Sub<&Self> for DMat4 {
1164    type Output = Self;
1165    #[inline]
1166    fn sub(self, rhs: &Self) -> Self {
1167        self.sub(*rhs)
1168    }
1169}
1170
1171impl Sub<&DMat4> for &DMat4 {
1172    type Output = DMat4;
1173    #[inline]
1174    fn sub(self, rhs: &DMat4) -> DMat4 {
1175        (*self).sub(*rhs)
1176    }
1177}
1178
1179impl Sub<DMat4> for &DMat4 {
1180    type Output = DMat4;
1181    #[inline]
1182    fn sub(self, rhs: DMat4) -> DMat4 {
1183        (*self).sub(rhs)
1184    }
1185}
1186
1187impl SubAssign for DMat4 {
1188    #[inline]
1189    fn sub_assign(&mut self, rhs: Self) {
1190        *self = self.sub(rhs);
1191    }
1192}
1193
1194impl SubAssign<&Self> for DMat4 {
1195    #[inline]
1196    fn sub_assign(&mut self, rhs: &Self) {
1197        self.sub_assign(*rhs);
1198    }
1199}
1200
1201impl Neg for DMat4 {
1202    type Output = Self;
1203    #[inline]
1204    fn neg(self) -> Self::Output {
1205        Self::from_cols(
1206            self.x_axis.neg(),
1207            self.y_axis.neg(),
1208            self.z_axis.neg(),
1209            self.w_axis.neg(),
1210        )
1211    }
1212}
1213
1214impl Neg for &DMat4 {
1215    type Output = DMat4;
1216    #[inline]
1217    fn neg(self) -> DMat4 {
1218        (*self).neg()
1219    }
1220}
1221
1222impl Mul for DMat4 {
1223    type Output = Self;
1224    #[inline]
1225    fn mul(self, rhs: Self) -> Self {
1226        Self::from_cols(
1227            self.mul(rhs.x_axis),
1228            self.mul(rhs.y_axis),
1229            self.mul(rhs.z_axis),
1230            self.mul(rhs.w_axis),
1231        )
1232    }
1233}
1234
1235impl Mul<&Self> for DMat4 {
1236    type Output = Self;
1237    #[inline]
1238    fn mul(self, rhs: &Self) -> Self {
1239        self.mul(*rhs)
1240    }
1241}
1242
1243impl Mul<&DMat4> for &DMat4 {
1244    type Output = DMat4;
1245    #[inline]
1246    fn mul(self, rhs: &DMat4) -> DMat4 {
1247        (*self).mul(*rhs)
1248    }
1249}
1250
1251impl Mul<DMat4> for &DMat4 {
1252    type Output = DMat4;
1253    #[inline]
1254    fn mul(self, rhs: DMat4) -> DMat4 {
1255        (*self).mul(rhs)
1256    }
1257}
1258
1259impl MulAssign for DMat4 {
1260    #[inline]
1261    fn mul_assign(&mut self, rhs: Self) {
1262        *self = self.mul(rhs);
1263    }
1264}
1265
1266impl MulAssign<&Self> for DMat4 {
1267    #[inline]
1268    fn mul_assign(&mut self, rhs: &Self) {
1269        self.mul_assign(*rhs);
1270    }
1271}
1272
1273impl Mul<DVec4> for DMat4 {
1274    type Output = DVec4;
1275    #[inline]
1276    fn mul(self, rhs: DVec4) -> Self::Output {
1277        self.mul_vec4(rhs)
1278    }
1279}
1280
1281impl Mul<&DVec4> for DMat4 {
1282    type Output = DVec4;
1283    #[inline]
1284    fn mul(self, rhs: &DVec4) -> DVec4 {
1285        self.mul(*rhs)
1286    }
1287}
1288
1289impl Mul<&DVec4> for &DMat4 {
1290    type Output = DVec4;
1291    #[inline]
1292    fn mul(self, rhs: &DVec4) -> DVec4 {
1293        (*self).mul(*rhs)
1294    }
1295}
1296
1297impl Mul<DVec4> for &DMat4 {
1298    type Output = DVec4;
1299    #[inline]
1300    fn mul(self, rhs: DVec4) -> DVec4 {
1301        (*self).mul(rhs)
1302    }
1303}
1304
1305impl Mul<DMat4> for f64 {
1306    type Output = DMat4;
1307    #[inline]
1308    fn mul(self, rhs: DMat4) -> Self::Output {
1309        rhs.mul_scalar(self)
1310    }
1311}
1312
1313impl Mul<&DMat4> for f64 {
1314    type Output = DMat4;
1315    #[inline]
1316    fn mul(self, rhs: &DMat4) -> DMat4 {
1317        self.mul(*rhs)
1318    }
1319}
1320
1321impl Mul<&DMat4> for &f64 {
1322    type Output = DMat4;
1323    #[inline]
1324    fn mul(self, rhs: &DMat4) -> DMat4 {
1325        (*self).mul(*rhs)
1326    }
1327}
1328
1329impl Mul<DMat4> for &f64 {
1330    type Output = DMat4;
1331    #[inline]
1332    fn mul(self, rhs: DMat4) -> DMat4 {
1333        (*self).mul(rhs)
1334    }
1335}
1336
1337impl Mul<f64> for DMat4 {
1338    type Output = Self;
1339    #[inline]
1340    fn mul(self, rhs: f64) -> Self {
1341        self.mul_scalar(rhs)
1342    }
1343}
1344
1345impl Mul<&f64> for DMat4 {
1346    type Output = Self;
1347    #[inline]
1348    fn mul(self, rhs: &f64) -> Self {
1349        self.mul(*rhs)
1350    }
1351}
1352
1353impl Mul<&f64> for &DMat4 {
1354    type Output = DMat4;
1355    #[inline]
1356    fn mul(self, rhs: &f64) -> DMat4 {
1357        (*self).mul(*rhs)
1358    }
1359}
1360
1361impl Mul<f64> for &DMat4 {
1362    type Output = DMat4;
1363    #[inline]
1364    fn mul(self, rhs: f64) -> DMat4 {
1365        (*self).mul(rhs)
1366    }
1367}
1368
1369impl MulAssign<f64> for DMat4 {
1370    #[inline]
1371    fn mul_assign(&mut self, rhs: f64) {
1372        *self = self.mul(rhs);
1373    }
1374}
1375
1376impl MulAssign<&f64> for DMat4 {
1377    #[inline]
1378    fn mul_assign(&mut self, rhs: &f64) {
1379        self.mul_assign(*rhs);
1380    }
1381}
1382
1383impl Div<DMat4> for f64 {
1384    type Output = DMat4;
1385    #[inline]
1386    fn div(self, rhs: DMat4) -> Self::Output {
1387        DMat4::from_cols(
1388            self.div(rhs.x_axis),
1389            self.div(rhs.y_axis),
1390            self.div(rhs.z_axis),
1391            self.div(rhs.w_axis),
1392        )
1393    }
1394}
1395
1396impl Div<&DMat4> for f64 {
1397    type Output = DMat4;
1398    #[inline]
1399    fn div(self, rhs: &DMat4) -> DMat4 {
1400        self.div(*rhs)
1401    }
1402}
1403
1404impl Div<&DMat4> for &f64 {
1405    type Output = DMat4;
1406    #[inline]
1407    fn div(self, rhs: &DMat4) -> DMat4 {
1408        (*self).div(*rhs)
1409    }
1410}
1411
1412impl Div<DMat4> for &f64 {
1413    type Output = DMat4;
1414    #[inline]
1415    fn div(self, rhs: DMat4) -> DMat4 {
1416        (*self).div(rhs)
1417    }
1418}
1419
1420impl Div<f64> for DMat4 {
1421    type Output = Self;
1422    #[inline]
1423    fn div(self, rhs: f64) -> Self {
1424        self.div_scalar(rhs)
1425    }
1426}
1427
1428impl Div<&f64> for DMat4 {
1429    type Output = Self;
1430    #[inline]
1431    fn div(self, rhs: &f64) -> Self {
1432        self.div(*rhs)
1433    }
1434}
1435
1436impl Div<&f64> for &DMat4 {
1437    type Output = DMat4;
1438    #[inline]
1439    fn div(self, rhs: &f64) -> DMat4 {
1440        (*self).div(*rhs)
1441    }
1442}
1443
1444impl Div<f64> for &DMat4 {
1445    type Output = DMat4;
1446    #[inline]
1447    fn div(self, rhs: f64) -> DMat4 {
1448        (*self).div(rhs)
1449    }
1450}
1451
1452impl DivAssign<f64> for DMat4 {
1453    #[inline]
1454    fn div_assign(&mut self, rhs: f64) {
1455        *self = self.div(rhs);
1456    }
1457}
1458
1459impl DivAssign<&f64> for DMat4 {
1460    #[inline]
1461    fn div_assign(&mut self, rhs: &f64) {
1462        self.div_assign(*rhs);
1463    }
1464}
1465
1466impl Sum<Self> for DMat4 {
1467    fn sum<I>(iter: I) -> Self
1468    where
1469        I: Iterator<Item = Self>,
1470    {
1471        iter.fold(Self::ZERO, Self::add)
1472    }
1473}
1474
1475impl<'a> Sum<&'a Self> for DMat4 {
1476    fn sum<I>(iter: I) -> Self
1477    where
1478        I: Iterator<Item = &'a Self>,
1479    {
1480        iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1481    }
1482}
1483
1484impl Product for DMat4 {
1485    fn product<I>(iter: I) -> Self
1486    where
1487        I: Iterator<Item = Self>,
1488    {
1489        iter.fold(Self::IDENTITY, Self::mul)
1490    }
1491}
1492
1493impl<'a> Product<&'a Self> for DMat4 {
1494    fn product<I>(iter: I) -> Self
1495    where
1496        I: Iterator<Item = &'a Self>,
1497    {
1498        iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
1499    }
1500}
1501
1502impl PartialEq for DMat4 {
1503    #[inline]
1504    fn eq(&self, rhs: &Self) -> bool {
1505        self.x_axis.eq(&rhs.x_axis)
1506            && self.y_axis.eq(&rhs.y_axis)
1507            && self.z_axis.eq(&rhs.z_axis)
1508            && self.w_axis.eq(&rhs.w_axis)
1509    }
1510}
1511
1512impl AsRef<[f64; 16]> for DMat4 {
1513    #[inline]
1514    fn as_ref(&self) -> &[f64; 16] {
1515        unsafe { &*(self as *const Self as *const [f64; 16]) }
1516    }
1517}
1518
1519impl AsMut<[f64; 16]> for DMat4 {
1520    #[inline]
1521    fn as_mut(&mut self) -> &mut [f64; 16] {
1522        unsafe { &mut *(self as *mut Self as *mut [f64; 16]) }
1523    }
1524}
1525
1526impl fmt::Debug for DMat4 {
1527    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1528        fmt.debug_struct(stringify!(DMat4))
1529            .field("x_axis", &self.x_axis)
1530            .field("y_axis", &self.y_axis)
1531            .field("z_axis", &self.z_axis)
1532            .field("w_axis", &self.w_axis)
1533            .finish()
1534    }
1535}
1536
1537impl fmt::Display for DMat4 {
1538    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1539        if let Some(p) = f.precision() {
1540            write!(
1541                f,
1542                "[{:.*}, {:.*}, {:.*}, {:.*}]",
1543                p, self.x_axis, p, self.y_axis, p, self.z_axis, p, self.w_axis
1544            )
1545        } else {
1546            write!(
1547                f,
1548                "[{}, {}, {}, {}]",
1549                self.x_axis, self.y_axis, self.z_axis, self.w_axis
1550            )
1551        }
1552    }
1553}