1use 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#[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#[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 pub const ZERO: Self = Self::from_cols(DVec4::ZERO, DVec4::ZERO, DVec4::ZERO, DVec4::ZERO);
56
57 pub const IDENTITY: Self = Self::from_cols(DVec4::X, DVec4::Y, DVec4::Z, DVec4::W);
59
60 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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[inline]
536 #[must_use]
537 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
538 pub fn from_scale(scale: DVec3) -> Self {
539 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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[must_use]
888 pub fn inverse_or_zero(&self) -> Self {
889 self.inverse_checked::<true>().0
890 }
891
892 #[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 #[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 #[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 #[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 #[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 #[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 #[inline]
990 #[must_use]
991 pub fn mul_mat4(&self, rhs: &Self) -> Self {
992 self.mul(rhs)
993 }
994
995 #[inline]
997 #[must_use]
998 pub fn add_mat4(&self, rhs: &Self) -> Self {
999 self.add(rhs)
1000 }
1001
1002 #[inline]
1004 #[must_use]
1005 pub fn sub_mat4(&self, rhs: &Self) -> Self {
1006 self.sub(rhs)
1007 }
1008
1009 #[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 #[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 #[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 #[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 #[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 #[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}