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 transpose_mul_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    /// Transforms a 4D vector by the transpose of `self`.
978    #[inline]
979    #[must_use]
980    #[deprecated(
981        since = "0.34.2",
982        note = "Use `transpose_mul_vec4` instead, whose word order matches its order of operations and is consistent with `transpose().mul_vec4`."
983    )]
984    pub fn mul_transpose_vec4(&self, rhs: DVec4) -> DVec4 {
985        self.transpose_mul_vec4(rhs)
986    }
987
988    /// Multiplies two 4x4 matrices.
989    #[inline]
990    #[must_use]
991    pub fn mul_mat4(&self, rhs: &Self) -> Self {
992        self.mul(rhs)
993    }
994
995    /// Adds two 4x4 matrices.
996    #[inline]
997    #[must_use]
998    pub fn add_mat4(&self, rhs: &Self) -> Self {
999        self.add(rhs)
1000    }
1001
1002    /// Subtracts two 4x4 matrices.
1003    #[inline]
1004    #[must_use]
1005    pub fn sub_mat4(&self, rhs: &Self) -> Self {
1006        self.sub(rhs)
1007    }
1008
1009    /// Multiplies a 4x4 matrix by a scalar.
1010    #[inline]
1011    #[must_use]
1012    pub fn mul_scalar(&self, rhs: f64) -> Self {
1013        Self::from_cols(
1014            self.x_axis.mul(rhs),
1015            self.y_axis.mul(rhs),
1016            self.z_axis.mul(rhs),
1017            self.w_axis.mul(rhs),
1018        )
1019    }
1020
1021    /// Multiply `self` by a scaling vector `scale`.
1022    /// This is faster than creating a whole diagonal scaling matrix and then multiplying that.
1023    /// This operation is commutative.
1024    #[inline]
1025    #[must_use]
1026    pub fn mul_diagonal_scale(&self, scale: DVec4) -> Self {
1027        Self::from_cols(
1028            self.x_axis * scale.x,
1029            self.y_axis * scale.y,
1030            self.z_axis * scale.z,
1031            self.w_axis * scale.w,
1032        )
1033    }
1034
1035    /// Divides a 4x4 matrix by a scalar.
1036    #[inline]
1037    #[must_use]
1038    pub fn div_scalar(&self, rhs: f64) -> Self {
1039        let rhs = DVec4::splat(rhs);
1040        Self::from_cols(
1041            self.x_axis.div(rhs),
1042            self.y_axis.div(rhs),
1043            self.z_axis.div(rhs),
1044            self.w_axis.div(rhs),
1045        )
1046    }
1047
1048    /// Returns a matrix containing the reciprocal `1.0/n` of each element of `self`.
1049    #[inline]
1050    #[must_use]
1051    pub fn recip(&self) -> Self {
1052        Self::from_cols(
1053            self.x_axis.recip(),
1054            self.y_axis.recip(),
1055            self.z_axis.recip(),
1056            self.w_axis.recip(),
1057        )
1058    }
1059
1060    /// Returns true if the absolute difference of all elements between `self` and `rhs`
1061    /// is less than or equal to `max_abs_diff`.
1062    ///
1063    /// This can be used to compare if two matrices contain similar elements. It works best
1064    /// when comparing with a known value. The `max_abs_diff` that should be used used
1065    /// depends on the values being compared against.
1066    ///
1067    /// For more see
1068    /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
1069    #[inline]
1070    #[must_use]
1071    pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f64) -> bool {
1072        self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
1073            && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
1074            && self.z_axis.abs_diff_eq(rhs.z_axis, max_abs_diff)
1075            && self.w_axis.abs_diff_eq(rhs.w_axis, max_abs_diff)
1076    }
1077
1078    /// Takes the absolute value of each element in `self`
1079    #[inline]
1080    #[must_use]
1081    pub fn abs(&self) -> Self {
1082        Self::from_cols(
1083            self.x_axis.abs(),
1084            self.y_axis.abs(),
1085            self.z_axis.abs(),
1086            self.w_axis.abs(),
1087        )
1088    }
1089
1090    #[cfg(feature = "f64")]
1091    #[inline]
1092    #[must_use]
1093    pub fn as_mat4(&self) -> Mat4 {
1094        Mat4::from_cols(
1095            self.x_axis.as_vec4(),
1096            self.y_axis.as_vec4(),
1097            self.z_axis.as_vec4(),
1098            self.w_axis.as_vec4(),
1099        )
1100    }
1101}
1102
1103impl Default for DMat4 {
1104    #[inline]
1105    fn default() -> Self {
1106        Self::IDENTITY
1107    }
1108}
1109
1110impl Add for DMat4 {
1111    type Output = Self;
1112    #[inline]
1113    fn add(self, rhs: Self) -> Self {
1114        Self::from_cols(
1115            self.x_axis.add(rhs.x_axis),
1116            self.y_axis.add(rhs.y_axis),
1117            self.z_axis.add(rhs.z_axis),
1118            self.w_axis.add(rhs.w_axis),
1119        )
1120    }
1121}
1122
1123impl Add<&Self> for DMat4 {
1124    type Output = Self;
1125    #[inline]
1126    fn add(self, rhs: &Self) -> Self {
1127        self.add(*rhs)
1128    }
1129}
1130
1131impl Add<&DMat4> for &DMat4 {
1132    type Output = DMat4;
1133    #[inline]
1134    fn add(self, rhs: &DMat4) -> DMat4 {
1135        (*self).add(*rhs)
1136    }
1137}
1138
1139impl Add<DMat4> for &DMat4 {
1140    type Output = DMat4;
1141    #[inline]
1142    fn add(self, rhs: DMat4) -> DMat4 {
1143        (*self).add(rhs)
1144    }
1145}
1146
1147impl AddAssign for DMat4 {
1148    #[inline]
1149    fn add_assign(&mut self, rhs: Self) {
1150        *self = self.add(rhs);
1151    }
1152}
1153
1154impl AddAssign<&Self> for DMat4 {
1155    #[inline]
1156    fn add_assign(&mut self, rhs: &Self) {
1157        self.add_assign(*rhs);
1158    }
1159}
1160
1161impl Sub for DMat4 {
1162    type Output = Self;
1163    #[inline]
1164    fn sub(self, rhs: Self) -> Self {
1165        Self::from_cols(
1166            self.x_axis.sub(rhs.x_axis),
1167            self.y_axis.sub(rhs.y_axis),
1168            self.z_axis.sub(rhs.z_axis),
1169            self.w_axis.sub(rhs.w_axis),
1170        )
1171    }
1172}
1173
1174impl Sub<&Self> for DMat4 {
1175    type Output = Self;
1176    #[inline]
1177    fn sub(self, rhs: &Self) -> Self {
1178        self.sub(*rhs)
1179    }
1180}
1181
1182impl Sub<&DMat4> for &DMat4 {
1183    type Output = DMat4;
1184    #[inline]
1185    fn sub(self, rhs: &DMat4) -> DMat4 {
1186        (*self).sub(*rhs)
1187    }
1188}
1189
1190impl Sub<DMat4> for &DMat4 {
1191    type Output = DMat4;
1192    #[inline]
1193    fn sub(self, rhs: DMat4) -> DMat4 {
1194        (*self).sub(rhs)
1195    }
1196}
1197
1198impl SubAssign for DMat4 {
1199    #[inline]
1200    fn sub_assign(&mut self, rhs: Self) {
1201        *self = self.sub(rhs);
1202    }
1203}
1204
1205impl SubAssign<&Self> for DMat4 {
1206    #[inline]
1207    fn sub_assign(&mut self, rhs: &Self) {
1208        self.sub_assign(*rhs);
1209    }
1210}
1211
1212impl Neg for DMat4 {
1213    type Output = Self;
1214    #[inline]
1215    fn neg(self) -> Self::Output {
1216        Self::from_cols(
1217            self.x_axis.neg(),
1218            self.y_axis.neg(),
1219            self.z_axis.neg(),
1220            self.w_axis.neg(),
1221        )
1222    }
1223}
1224
1225impl Neg for &DMat4 {
1226    type Output = DMat4;
1227    #[inline]
1228    fn neg(self) -> DMat4 {
1229        (*self).neg()
1230    }
1231}
1232
1233impl Mul for DMat4 {
1234    type Output = Self;
1235    #[inline]
1236    fn mul(self, rhs: Self) -> Self {
1237        Self::from_cols(
1238            self.mul(rhs.x_axis),
1239            self.mul(rhs.y_axis),
1240            self.mul(rhs.z_axis),
1241            self.mul(rhs.w_axis),
1242        )
1243    }
1244}
1245
1246impl Mul<&Self> for DMat4 {
1247    type Output = Self;
1248    #[inline]
1249    fn mul(self, rhs: &Self) -> Self {
1250        self.mul(*rhs)
1251    }
1252}
1253
1254impl Mul<&DMat4> for &DMat4 {
1255    type Output = DMat4;
1256    #[inline]
1257    fn mul(self, rhs: &DMat4) -> DMat4 {
1258        (*self).mul(*rhs)
1259    }
1260}
1261
1262impl Mul<DMat4> for &DMat4 {
1263    type Output = DMat4;
1264    #[inline]
1265    fn mul(self, rhs: DMat4) -> DMat4 {
1266        (*self).mul(rhs)
1267    }
1268}
1269
1270impl MulAssign for DMat4 {
1271    #[inline]
1272    fn mul_assign(&mut self, rhs: Self) {
1273        *self = self.mul(rhs);
1274    }
1275}
1276
1277impl MulAssign<&Self> for DMat4 {
1278    #[inline]
1279    fn mul_assign(&mut self, rhs: &Self) {
1280        self.mul_assign(*rhs);
1281    }
1282}
1283
1284impl Mul<DVec4> for DMat4 {
1285    type Output = DVec4;
1286    #[inline]
1287    fn mul(self, rhs: DVec4) -> Self::Output {
1288        self.mul_vec4(rhs)
1289    }
1290}
1291
1292impl Mul<&DVec4> for DMat4 {
1293    type Output = DVec4;
1294    #[inline]
1295    fn mul(self, rhs: &DVec4) -> DVec4 {
1296        self.mul(*rhs)
1297    }
1298}
1299
1300impl Mul<&DVec4> for &DMat4 {
1301    type Output = DVec4;
1302    #[inline]
1303    fn mul(self, rhs: &DVec4) -> DVec4 {
1304        (*self).mul(*rhs)
1305    }
1306}
1307
1308impl Mul<DVec4> for &DMat4 {
1309    type Output = DVec4;
1310    #[inline]
1311    fn mul(self, rhs: DVec4) -> DVec4 {
1312        (*self).mul(rhs)
1313    }
1314}
1315
1316impl Mul<DMat4> for f64 {
1317    type Output = DMat4;
1318    #[inline]
1319    fn mul(self, rhs: DMat4) -> Self::Output {
1320        rhs.mul_scalar(self)
1321    }
1322}
1323
1324impl Mul<&DMat4> for f64 {
1325    type Output = DMat4;
1326    #[inline]
1327    fn mul(self, rhs: &DMat4) -> DMat4 {
1328        self.mul(*rhs)
1329    }
1330}
1331
1332impl Mul<&DMat4> for &f64 {
1333    type Output = DMat4;
1334    #[inline]
1335    fn mul(self, rhs: &DMat4) -> DMat4 {
1336        (*self).mul(*rhs)
1337    }
1338}
1339
1340impl Mul<DMat4> for &f64 {
1341    type Output = DMat4;
1342    #[inline]
1343    fn mul(self, rhs: DMat4) -> DMat4 {
1344        (*self).mul(rhs)
1345    }
1346}
1347
1348impl Mul<f64> for DMat4 {
1349    type Output = Self;
1350    #[inline]
1351    fn mul(self, rhs: f64) -> Self {
1352        self.mul_scalar(rhs)
1353    }
1354}
1355
1356impl Mul<&f64> for DMat4 {
1357    type Output = Self;
1358    #[inline]
1359    fn mul(self, rhs: &f64) -> Self {
1360        self.mul(*rhs)
1361    }
1362}
1363
1364impl Mul<&f64> for &DMat4 {
1365    type Output = DMat4;
1366    #[inline]
1367    fn mul(self, rhs: &f64) -> DMat4 {
1368        (*self).mul(*rhs)
1369    }
1370}
1371
1372impl Mul<f64> for &DMat4 {
1373    type Output = DMat4;
1374    #[inline]
1375    fn mul(self, rhs: f64) -> DMat4 {
1376        (*self).mul(rhs)
1377    }
1378}
1379
1380impl MulAssign<f64> for DMat4 {
1381    #[inline]
1382    fn mul_assign(&mut self, rhs: f64) {
1383        *self = self.mul(rhs);
1384    }
1385}
1386
1387impl MulAssign<&f64> for DMat4 {
1388    #[inline]
1389    fn mul_assign(&mut self, rhs: &f64) {
1390        self.mul_assign(*rhs);
1391    }
1392}
1393
1394impl Div<DMat4> for f64 {
1395    type Output = DMat4;
1396    #[inline]
1397    fn div(self, rhs: DMat4) -> Self::Output {
1398        DMat4::from_cols(
1399            self.div(rhs.x_axis),
1400            self.div(rhs.y_axis),
1401            self.div(rhs.z_axis),
1402            self.div(rhs.w_axis),
1403        )
1404    }
1405}
1406
1407impl Div<&DMat4> for f64 {
1408    type Output = DMat4;
1409    #[inline]
1410    fn div(self, rhs: &DMat4) -> DMat4 {
1411        self.div(*rhs)
1412    }
1413}
1414
1415impl Div<&DMat4> for &f64 {
1416    type Output = DMat4;
1417    #[inline]
1418    fn div(self, rhs: &DMat4) -> DMat4 {
1419        (*self).div(*rhs)
1420    }
1421}
1422
1423impl Div<DMat4> for &f64 {
1424    type Output = DMat4;
1425    #[inline]
1426    fn div(self, rhs: DMat4) -> DMat4 {
1427        (*self).div(rhs)
1428    }
1429}
1430
1431impl Div<f64> for DMat4 {
1432    type Output = Self;
1433    #[inline]
1434    fn div(self, rhs: f64) -> Self {
1435        self.div_scalar(rhs)
1436    }
1437}
1438
1439impl Div<&f64> for DMat4 {
1440    type Output = Self;
1441    #[inline]
1442    fn div(self, rhs: &f64) -> Self {
1443        self.div(*rhs)
1444    }
1445}
1446
1447impl Div<&f64> for &DMat4 {
1448    type Output = DMat4;
1449    #[inline]
1450    fn div(self, rhs: &f64) -> DMat4 {
1451        (*self).div(*rhs)
1452    }
1453}
1454
1455impl Div<f64> for &DMat4 {
1456    type Output = DMat4;
1457    #[inline]
1458    fn div(self, rhs: f64) -> DMat4 {
1459        (*self).div(rhs)
1460    }
1461}
1462
1463impl DivAssign<f64> for DMat4 {
1464    #[inline]
1465    fn div_assign(&mut self, rhs: f64) {
1466        *self = self.div(rhs);
1467    }
1468}
1469
1470impl DivAssign<&f64> for DMat4 {
1471    #[inline]
1472    fn div_assign(&mut self, rhs: &f64) {
1473        self.div_assign(*rhs);
1474    }
1475}
1476
1477impl Sum<Self> for DMat4 {
1478    fn sum<I>(iter: I) -> Self
1479    where
1480        I: Iterator<Item = Self>,
1481    {
1482        iter.fold(Self::ZERO, Self::add)
1483    }
1484}
1485
1486impl<'a> Sum<&'a Self> for DMat4 {
1487    fn sum<I>(iter: I) -> Self
1488    where
1489        I: Iterator<Item = &'a Self>,
1490    {
1491        iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1492    }
1493}
1494
1495impl Product for DMat4 {
1496    fn product<I>(iter: I) -> Self
1497    where
1498        I: Iterator<Item = Self>,
1499    {
1500        iter.fold(Self::IDENTITY, Self::mul)
1501    }
1502}
1503
1504impl<'a> Product<&'a Self> for DMat4 {
1505    fn product<I>(iter: I) -> Self
1506    where
1507        I: Iterator<Item = &'a Self>,
1508    {
1509        iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
1510    }
1511}
1512
1513impl PartialEq for DMat4 {
1514    #[inline]
1515    fn eq(&self, rhs: &Self) -> bool {
1516        self.x_axis.eq(&rhs.x_axis)
1517            && self.y_axis.eq(&rhs.y_axis)
1518            && self.z_axis.eq(&rhs.z_axis)
1519            && self.w_axis.eq(&rhs.w_axis)
1520    }
1521}
1522
1523impl AsRef<[f64; 16]> for DMat4 {
1524    #[inline]
1525    fn as_ref(&self) -> &[f64; 16] {
1526        unsafe { &*(self as *const Self as *const [f64; 16]) }
1527    }
1528}
1529
1530impl AsMut<[f64; 16]> for DMat4 {
1531    #[inline]
1532    fn as_mut(&mut self) -> &mut [f64; 16] {
1533        unsafe { &mut *(self as *mut Self as *mut [f64; 16]) }
1534    }
1535}
1536
1537impl fmt::Debug for DMat4 {
1538    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1539        fmt.debug_struct(stringify!(DMat4))
1540            .field("x_axis", &self.x_axis)
1541            .field("y_axis", &self.y_axis)
1542            .field("z_axis", &self.z_axis)
1543            .field("w_axis", &self.w_axis)
1544            .finish()
1545    }
1546}
1547
1548impl fmt::Display for DMat4 {
1549    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1550        if let Some(p) = f.precision() {
1551            write!(
1552                f,
1553                "[{:.*}, {:.*}, {:.*}, {:.*}]",
1554                p, self.x_axis, p, self.y_axis, p, self.z_axis, p, self.w_axis
1555            )
1556        } else {
1557            write!(
1558                f,
1559                "[{}, {}, {}, {}]",
1560                self.x_axis, self.y_axis, self.z_axis, self.w_axis
1561            )
1562        }
1563    }
1564}