1use crate::{f64::math, swizzles::*, DMat3, DVec2, Mat2};
4use core::fmt;
5use core::iter::{Product, Sum};
6use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
7
8#[cfg(feature = "zerocopy-08")]
9use zerocopy_derive_08::*;
10
11#[inline(always)]
13#[must_use]
14pub const fn dmat2(x_axis: DVec2, y_axis: DVec2) -> DMat2 {
15 DMat2::from_cols(x_axis, y_axis)
16}
17
18#[derive(Clone, Copy)]
20#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
21#[cfg_attr(
22 feature = "zerocopy-08",
23 derive(FromBytes, Immutable, IntoBytes, KnownLayout)
24)]
25#[cfg_attr(feature = "cuda", repr(align(16)))]
26#[repr(C)]
27pub struct DMat2 {
28 pub x_axis: DVec2,
29 pub y_axis: DVec2,
30}
31
32impl DMat2 {
33 pub const ZERO: Self = Self::from_cols(DVec2::ZERO, DVec2::ZERO);
35
36 pub const IDENTITY: Self = Self::from_cols(DVec2::X, DVec2::Y);
38
39 pub const NAN: Self = Self::from_cols(DVec2::NAN, DVec2::NAN);
41
42 #[allow(clippy::too_many_arguments)]
43 #[inline(always)]
44 #[must_use]
45 const fn new(m00: f64, m01: f64, m10: f64, m11: f64) -> Self {
46 Self {
47 x_axis: DVec2::new(m00, m01),
48 y_axis: DVec2::new(m10, m11),
49 }
50 }
51
52 #[inline(always)]
56 #[must_use]
57 pub const fn from_cols(x_axis: DVec2, y_axis: DVec2) -> Self {
58 Self { x_axis, y_axis }
59 }
60
61 #[inline(always)]
67 #[must_use]
68 pub const fn from_rows(row0: DVec2, row1: DVec2) -> Self {
69 let [m00, m01] = row0.to_array();
70 let [m10, m11] = row1.to_array();
71 Self::new(m00, m10, m01, m11)
72 }
73
74 #[inline]
78 #[must_use]
79 pub const fn from_cols_array(m: &[f64; 4]) -> Self {
80 Self::new(m[0], m[1], m[2], m[3])
81 }
82
83 #[inline]
87 #[must_use]
88 pub const fn to_cols_array(&self) -> [f64; 4] {
89 [self.x_axis.x, self.x_axis.y, self.y_axis.x, self.y_axis.y]
90 }
91
92 #[inline]
96 #[must_use]
97 pub const fn from_cols_array_2d(m: &[[f64; 2]; 2]) -> Self {
98 Self::from_cols(DVec2::from_array(m[0]), DVec2::from_array(m[1]))
99 }
100
101 #[inline]
105 #[must_use]
106 pub const fn to_cols_array_2d(&self) -> [[f64; 2]; 2] {
107 [self.x_axis.to_array(), self.y_axis.to_array()]
108 }
109
110 #[inline]
116 #[must_use]
117 pub const fn from_rows_array(m: &[f64; 4]) -> Self {
118 Self::new(m[0], m[2], m[1], m[3])
119 }
120
121 #[inline]
127 #[must_use]
128 pub const fn to_rows_array(&self) -> [f64; 4] {
129 let m = self.to_cols_array();
130 [m[0], m[2], m[1], m[3]]
131 }
132
133 #[doc(alias = "scale")]
135 #[inline]
136 #[must_use]
137 pub const fn from_diagonal(diagonal: DVec2) -> Self {
138 Self::new(diagonal.x, 0.0, 0.0, diagonal.y)
139 }
140
141 #[inline]
144 #[must_use]
145 pub fn from_scale_angle(scale: DVec2, angle: f64) -> Self {
146 let (sin, cos) = math::sin_cos(angle);
147 Self::new(cos * scale.x, sin * scale.x, -sin * scale.y, cos * scale.y)
148 }
149
150 #[inline]
152 #[must_use]
153 pub fn from_angle(angle: f64) -> Self {
154 let (sin, cos) = math::sin_cos(angle);
155 Self::new(cos, sin, -sin, cos)
156 }
157
158 #[inline]
160 #[must_use]
161 pub fn from_mat3(m: DMat3) -> Self {
162 Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
163 }
164
165 #[inline]
172 #[must_use]
173 #[track_caller]
174 pub fn from_mat3_minor(m: DMat3, i: usize, j: usize) -> Self {
175 match (i, j) {
176 (0, 0) => Self::from_cols(m.y_axis.yz(), m.z_axis.yz()),
177 (0, 1) => Self::from_cols(m.y_axis.xz(), m.z_axis.xz()),
178 (0, 2) => Self::from_cols(m.y_axis.xy(), m.z_axis.xy()),
179 (1, 0) => Self::from_cols(m.x_axis.yz(), m.z_axis.yz()),
180 (1, 1) => Self::from_cols(m.x_axis.xz(), m.z_axis.xz()),
181 (1, 2) => Self::from_cols(m.x_axis.xy(), m.z_axis.xy()),
182 (2, 0) => Self::from_cols(m.x_axis.yz(), m.y_axis.yz()),
183 (2, 1) => Self::from_cols(m.x_axis.xz(), m.y_axis.xz()),
184 (2, 2) => Self::from_cols(m.x_axis.xy(), m.y_axis.xy()),
185 _ => panic!("index out of bounds"),
186 }
187 }
188
189 #[inline]
197 #[must_use]
198 #[track_caller]
199 pub const fn from_cols_slice(slice: &[f64]) -> Self {
200 Self::new(slice[0], slice[1], slice[2], slice[3])
201 }
202
203 #[inline]
209 #[track_caller]
210 pub fn write_cols_to_slice(&self, slice: &mut [f64]) {
211 slice[0] = self.x_axis.x;
212 slice[1] = self.x_axis.y;
213 slice[2] = self.y_axis.x;
214 slice[3] = self.y_axis.y;
215 }
216
217 #[inline]
228 #[must_use]
229 #[track_caller]
230 pub const fn from_rows_slice(slice: &[f64]) -> Self {
231 Self::new(slice[0], slice[2], slice[1], slice[3])
232 }
233
234 #[inline]
240 #[must_use]
241 #[track_caller]
242 pub fn col(&self, index: usize) -> DVec2 {
243 match index {
244 0 => self.x_axis,
245 1 => self.y_axis,
246 _ => panic!("index out of bounds"),
247 }
248 }
249
250 #[inline]
256 #[track_caller]
257 pub fn col_mut(&mut self, index: usize) -> &mut DVec2 {
258 match index {
259 0 => &mut self.x_axis,
260 1 => &mut self.y_axis,
261 _ => panic!("index out of bounds"),
262 }
263 }
264
265 #[inline]
273 #[must_use]
274 #[track_caller]
275 pub fn row(&self, index: usize) -> DVec2 {
276 match index {
277 0 => DVec2::new(self.x_axis.x, self.y_axis.x),
278 1 => DVec2::new(self.x_axis.y, self.y_axis.y),
279 _ => panic!("index out of bounds"),
280 }
281 }
282
283 #[inline]
293 #[track_caller]
294 pub fn set_row(&mut self, index: usize, row: DVec2) {
295 match index {
296 0 => {
297 self.x_axis.x = row.x;
298 self.y_axis.x = row.y;
299 }
300 1 => {
301 self.x_axis.y = row.x;
302 self.y_axis.y = row.y;
303 }
304 _ => panic!("index out of bounds"),
305 }
306 }
307
308 #[inline]
311 #[must_use]
312 pub fn is_finite(&self) -> bool {
313 self.x_axis.is_finite() && self.y_axis.is_finite()
314 }
315
316 #[inline]
318 #[must_use]
319 pub fn is_nan(&self) -> bool {
320 self.x_axis.is_nan() || self.y_axis.is_nan()
321 }
322
323 #[inline]
325 #[must_use]
326 pub fn transpose(&self) -> Self {
327 Self {
328 x_axis: DVec2::new(self.x_axis.x, self.y_axis.x),
329 y_axis: DVec2::new(self.x_axis.y, self.y_axis.y),
330 }
331 }
332
333 #[inline]
335 #[must_use]
336 pub fn diagonal(&self) -> DVec2 {
337 DVec2::new(self.x_axis.x, self.y_axis.y)
338 }
339
340 #[inline]
342 #[must_use]
343 pub fn determinant(&self) -> f64 {
344 self.x_axis.x * self.y_axis.y - self.x_axis.y * self.y_axis.x
345 }
346
347 #[inline(always)]
359 #[must_use]
360 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
361 fn inverse_checked<const CHECKED: bool>(&self) -> (Self, bool) {
362 let inv_det = 1.0 / self.determinant();
363 let m = Self::new(
364 self.y_axis.y * inv_det,
365 self.x_axis.y * -inv_det,
366 self.y_axis.x * -inv_det,
367 self.x_axis.x * inv_det,
368 );
369 if CHECKED {
370 if !m.is_finite() {
371 return (Self::ZERO, false);
372 }
373 } else {
374 glam_assert!(m.is_finite());
375 }
376 (m, true)
377 }
378
379 #[inline]
391 #[must_use]
392 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
393 pub fn inverse(&self) -> Self {
394 self.inverse_checked::<false>().0
395 }
396
397 #[inline]
400 #[must_use]
401 pub fn try_inverse(&self) -> Option<Self> {
402 let (m, is_valid) = self.inverse_checked::<true>();
403 if is_valid {
404 Some(m)
405 } else {
406 None
407 }
408 }
409
410 #[inline]
413 #[must_use]
414 pub fn inverse_or_zero(&self) -> Self {
415 self.inverse_checked::<true>().0
416 }
417
418 #[inline]
420 #[must_use]
421 pub fn mul_vec2(&self, rhs: DVec2) -> DVec2 {
422 #[allow(clippy::suspicious_operation_groupings)]
423 DVec2::new(
424 (self.x_axis.x * rhs.x) + (self.y_axis.x * rhs.y),
425 (self.x_axis.y * rhs.x) + (self.y_axis.y * rhs.y),
426 )
427 }
428
429 #[inline]
431 #[must_use]
432 pub fn mul_transpose_vec2(&self, rhs: DVec2) -> DVec2 {
433 DVec2::new(self.x_axis.dot(rhs), self.y_axis.dot(rhs))
434 }
435
436 #[inline]
438 #[must_use]
439 pub fn mul_mat2(&self, rhs: &Self) -> Self {
440 self.mul(rhs)
441 }
442
443 #[inline]
445 #[must_use]
446 pub fn add_mat2(&self, rhs: &Self) -> Self {
447 self.add(rhs)
448 }
449
450 #[inline]
452 #[must_use]
453 pub fn sub_mat2(&self, rhs: &Self) -> Self {
454 self.sub(rhs)
455 }
456
457 #[inline]
459 #[must_use]
460 pub fn mul_scalar(&self, rhs: f64) -> Self {
461 Self::from_cols(self.x_axis.mul(rhs), self.y_axis.mul(rhs))
462 }
463
464 #[inline]
468 #[must_use]
469 pub fn mul_diagonal_scale(&self, scale: DVec2) -> Self {
470 Self::from_cols(self.x_axis * scale.x, self.y_axis * scale.y)
471 }
472
473 #[inline]
475 #[must_use]
476 pub fn div_scalar(&self, rhs: f64) -> Self {
477 let rhs = DVec2::splat(rhs);
478 Self::from_cols(self.x_axis.div(rhs), self.y_axis.div(rhs))
479 }
480
481 #[inline]
483 #[must_use]
484 pub fn recip(&self) -> Self {
485 Self::from_cols(self.x_axis.recip(), self.y_axis.recip())
486 }
487
488 #[inline]
498 #[must_use]
499 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f64) -> bool {
500 self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
501 && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
502 }
503
504 #[inline]
506 #[must_use]
507 pub fn abs(&self) -> Self {
508 Self::from_cols(self.x_axis.abs(), self.y_axis.abs())
509 }
510
511 #[cfg(feature = "f64")]
512 #[inline]
513 #[must_use]
514 pub fn as_mat2(&self) -> Mat2 {
515 Mat2::from_cols(self.x_axis.as_vec2(), self.y_axis.as_vec2())
516 }
517}
518
519impl Default for DMat2 {
520 #[inline]
521 fn default() -> Self {
522 Self::IDENTITY
523 }
524}
525
526impl Add for DMat2 {
527 type Output = Self;
528 #[inline]
529 fn add(self, rhs: Self) -> Self {
530 Self::from_cols(self.x_axis.add(rhs.x_axis), self.y_axis.add(rhs.y_axis))
531 }
532}
533
534impl Add<&Self> for DMat2 {
535 type Output = Self;
536 #[inline]
537 fn add(self, rhs: &Self) -> Self {
538 self.add(*rhs)
539 }
540}
541
542impl Add<&DMat2> for &DMat2 {
543 type Output = DMat2;
544 #[inline]
545 fn add(self, rhs: &DMat2) -> DMat2 {
546 (*self).add(*rhs)
547 }
548}
549
550impl Add<DMat2> for &DMat2 {
551 type Output = DMat2;
552 #[inline]
553 fn add(self, rhs: DMat2) -> DMat2 {
554 (*self).add(rhs)
555 }
556}
557
558impl AddAssign for DMat2 {
559 #[inline]
560 fn add_assign(&mut self, rhs: Self) {
561 *self = self.add(rhs);
562 }
563}
564
565impl AddAssign<&Self> for DMat2 {
566 #[inline]
567 fn add_assign(&mut self, rhs: &Self) {
568 self.add_assign(*rhs);
569 }
570}
571
572impl Sub for DMat2 {
573 type Output = Self;
574 #[inline]
575 fn sub(self, rhs: Self) -> Self {
576 Self::from_cols(self.x_axis.sub(rhs.x_axis), self.y_axis.sub(rhs.y_axis))
577 }
578}
579
580impl Sub<&Self> for DMat2 {
581 type Output = Self;
582 #[inline]
583 fn sub(self, rhs: &Self) -> Self {
584 self.sub(*rhs)
585 }
586}
587
588impl Sub<&DMat2> for &DMat2 {
589 type Output = DMat2;
590 #[inline]
591 fn sub(self, rhs: &DMat2) -> DMat2 {
592 (*self).sub(*rhs)
593 }
594}
595
596impl Sub<DMat2> for &DMat2 {
597 type Output = DMat2;
598 #[inline]
599 fn sub(self, rhs: DMat2) -> DMat2 {
600 (*self).sub(rhs)
601 }
602}
603
604impl SubAssign for DMat2 {
605 #[inline]
606 fn sub_assign(&mut self, rhs: Self) {
607 *self = self.sub(rhs);
608 }
609}
610
611impl SubAssign<&Self> for DMat2 {
612 #[inline]
613 fn sub_assign(&mut self, rhs: &Self) {
614 self.sub_assign(*rhs);
615 }
616}
617
618impl Neg for DMat2 {
619 type Output = Self;
620 #[inline]
621 fn neg(self) -> Self::Output {
622 Self::from_cols(self.x_axis.neg(), self.y_axis.neg())
623 }
624}
625
626impl Neg for &DMat2 {
627 type Output = DMat2;
628 #[inline]
629 fn neg(self) -> DMat2 {
630 (*self).neg()
631 }
632}
633
634impl Mul for DMat2 {
635 type Output = Self;
636 #[inline]
637 fn mul(self, rhs: Self) -> Self {
638 Self::from_cols(self.mul(rhs.x_axis), self.mul(rhs.y_axis))
639 }
640}
641
642impl Mul<&Self> for DMat2 {
643 type Output = Self;
644 #[inline]
645 fn mul(self, rhs: &Self) -> Self {
646 self.mul(*rhs)
647 }
648}
649
650impl Mul<&DMat2> for &DMat2 {
651 type Output = DMat2;
652 #[inline]
653 fn mul(self, rhs: &DMat2) -> DMat2 {
654 (*self).mul(*rhs)
655 }
656}
657
658impl Mul<DMat2> for &DMat2 {
659 type Output = DMat2;
660 #[inline]
661 fn mul(self, rhs: DMat2) -> DMat2 {
662 (*self).mul(rhs)
663 }
664}
665
666impl MulAssign for DMat2 {
667 #[inline]
668 fn mul_assign(&mut self, rhs: Self) {
669 *self = self.mul(rhs);
670 }
671}
672
673impl MulAssign<&Self> for DMat2 {
674 #[inline]
675 fn mul_assign(&mut self, rhs: &Self) {
676 self.mul_assign(*rhs);
677 }
678}
679
680impl Mul<DVec2> for DMat2 {
681 type Output = DVec2;
682 #[inline]
683 fn mul(self, rhs: DVec2) -> Self::Output {
684 self.mul_vec2(rhs)
685 }
686}
687
688impl Mul<&DVec2> for DMat2 {
689 type Output = DVec2;
690 #[inline]
691 fn mul(self, rhs: &DVec2) -> DVec2 {
692 self.mul(*rhs)
693 }
694}
695
696impl Mul<&DVec2> for &DMat2 {
697 type Output = DVec2;
698 #[inline]
699 fn mul(self, rhs: &DVec2) -> DVec2 {
700 (*self).mul(*rhs)
701 }
702}
703
704impl Mul<DVec2> for &DMat2 {
705 type Output = DVec2;
706 #[inline]
707 fn mul(self, rhs: DVec2) -> DVec2 {
708 (*self).mul(rhs)
709 }
710}
711
712impl Mul<DMat2> for f64 {
713 type Output = DMat2;
714 #[inline]
715 fn mul(self, rhs: DMat2) -> Self::Output {
716 rhs.mul_scalar(self)
717 }
718}
719
720impl Mul<&DMat2> for f64 {
721 type Output = DMat2;
722 #[inline]
723 fn mul(self, rhs: &DMat2) -> DMat2 {
724 self.mul(*rhs)
725 }
726}
727
728impl Mul<&DMat2> for &f64 {
729 type Output = DMat2;
730 #[inline]
731 fn mul(self, rhs: &DMat2) -> DMat2 {
732 (*self).mul(*rhs)
733 }
734}
735
736impl Mul<DMat2> for &f64 {
737 type Output = DMat2;
738 #[inline]
739 fn mul(self, rhs: DMat2) -> DMat2 {
740 (*self).mul(rhs)
741 }
742}
743
744impl Mul<f64> for DMat2 {
745 type Output = Self;
746 #[inline]
747 fn mul(self, rhs: f64) -> Self {
748 self.mul_scalar(rhs)
749 }
750}
751
752impl Mul<&f64> for DMat2 {
753 type Output = Self;
754 #[inline]
755 fn mul(self, rhs: &f64) -> Self {
756 self.mul(*rhs)
757 }
758}
759
760impl Mul<&f64> for &DMat2 {
761 type Output = DMat2;
762 #[inline]
763 fn mul(self, rhs: &f64) -> DMat2 {
764 (*self).mul(*rhs)
765 }
766}
767
768impl Mul<f64> for &DMat2 {
769 type Output = DMat2;
770 #[inline]
771 fn mul(self, rhs: f64) -> DMat2 {
772 (*self).mul(rhs)
773 }
774}
775
776impl MulAssign<f64> for DMat2 {
777 #[inline]
778 fn mul_assign(&mut self, rhs: f64) {
779 *self = self.mul(rhs);
780 }
781}
782
783impl MulAssign<&f64> for DMat2 {
784 #[inline]
785 fn mul_assign(&mut self, rhs: &f64) {
786 self.mul_assign(*rhs);
787 }
788}
789
790impl Div<DMat2> for f64 {
791 type Output = DMat2;
792 #[inline]
793 fn div(self, rhs: DMat2) -> Self::Output {
794 DMat2::from_cols(self.div(rhs.x_axis), self.div(rhs.y_axis))
795 }
796}
797
798impl Div<&DMat2> for f64 {
799 type Output = DMat2;
800 #[inline]
801 fn div(self, rhs: &DMat2) -> DMat2 {
802 self.div(*rhs)
803 }
804}
805
806impl Div<&DMat2> for &f64 {
807 type Output = DMat2;
808 #[inline]
809 fn div(self, rhs: &DMat2) -> DMat2 {
810 (*self).div(*rhs)
811 }
812}
813
814impl Div<DMat2> for &f64 {
815 type Output = DMat2;
816 #[inline]
817 fn div(self, rhs: DMat2) -> DMat2 {
818 (*self).div(rhs)
819 }
820}
821
822impl Div<f64> for DMat2 {
823 type Output = Self;
824 #[inline]
825 fn div(self, rhs: f64) -> Self {
826 self.div_scalar(rhs)
827 }
828}
829
830impl Div<&f64> for DMat2 {
831 type Output = Self;
832 #[inline]
833 fn div(self, rhs: &f64) -> Self {
834 self.div(*rhs)
835 }
836}
837
838impl Div<&f64> for &DMat2 {
839 type Output = DMat2;
840 #[inline]
841 fn div(self, rhs: &f64) -> DMat2 {
842 (*self).div(*rhs)
843 }
844}
845
846impl Div<f64> for &DMat2 {
847 type Output = DMat2;
848 #[inline]
849 fn div(self, rhs: f64) -> DMat2 {
850 (*self).div(rhs)
851 }
852}
853
854impl DivAssign<f64> for DMat2 {
855 #[inline]
856 fn div_assign(&mut self, rhs: f64) {
857 *self = self.div(rhs);
858 }
859}
860
861impl DivAssign<&f64> for DMat2 {
862 #[inline]
863 fn div_assign(&mut self, rhs: &f64) {
864 self.div_assign(*rhs);
865 }
866}
867
868impl Sum<Self> for DMat2 {
869 fn sum<I>(iter: I) -> Self
870 where
871 I: Iterator<Item = Self>,
872 {
873 iter.fold(Self::ZERO, Self::add)
874 }
875}
876
877impl<'a> Sum<&'a Self> for DMat2 {
878 fn sum<I>(iter: I) -> Self
879 where
880 I: Iterator<Item = &'a Self>,
881 {
882 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
883 }
884}
885
886impl Product for DMat2 {
887 fn product<I>(iter: I) -> Self
888 where
889 I: Iterator<Item = Self>,
890 {
891 iter.fold(Self::IDENTITY, Self::mul)
892 }
893}
894
895impl<'a> Product<&'a Self> for DMat2 {
896 fn product<I>(iter: I) -> Self
897 where
898 I: Iterator<Item = &'a Self>,
899 {
900 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
901 }
902}
903
904impl PartialEq for DMat2 {
905 #[inline]
906 fn eq(&self, rhs: &Self) -> bool {
907 self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis)
908 }
909}
910
911impl AsRef<[f64; 4]> for DMat2 {
912 #[inline]
913 fn as_ref(&self) -> &[f64; 4] {
914 unsafe { &*(self as *const Self as *const [f64; 4]) }
915 }
916}
917
918impl AsMut<[f64; 4]> for DMat2 {
919 #[inline]
920 fn as_mut(&mut self) -> &mut [f64; 4] {
921 unsafe { &mut *(self as *mut Self as *mut [f64; 4]) }
922 }
923}
924
925impl fmt::Debug for DMat2 {
926 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
927 fmt.debug_struct(stringify!(DMat2))
928 .field("x_axis", &self.x_axis)
929 .field("y_axis", &self.y_axis)
930 .finish()
931 }
932}
933
934impl fmt::Display for DMat2 {
935 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
936 if let Some(p) = f.precision() {
937 write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
938 } else {
939 write!(f, "[{}, {}]", self.x_axis, self.y_axis)
940 }
941 }
942}