1use crate::{f32::math, swizzles::*, DMat3, EulerRot, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec3A};
4#[cfg(not(target_arch = "spirv"))]
5use core::fmt;
6use core::iter::{Product, Sum};
7use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
8
9#[cfg(target_arch = "x86")]
10use core::arch::x86::*;
11#[cfg(target_arch = "x86_64")]
12use core::arch::x86_64::*;
13
14#[inline(always)]
16#[must_use]
17pub const fn mat3a(x_axis: Vec3A, y_axis: Vec3A, z_axis: Vec3A) -> Mat3A {
18 Mat3A::from_cols(x_axis, y_axis, z_axis)
19}
20
21#[derive(Clone, Copy)]
46#[repr(C)]
47pub struct Mat3A {
48 pub x_axis: Vec3A,
49 pub y_axis: Vec3A,
50 pub z_axis: Vec3A,
51}
52
53impl Mat3A {
54 pub const ZERO: Self = Self::from_cols(Vec3A::ZERO, Vec3A::ZERO, Vec3A::ZERO);
56
57 pub const IDENTITY: Self = Self::from_cols(Vec3A::X, Vec3A::Y, Vec3A::Z);
59
60 pub const NAN: Self = Self::from_cols(Vec3A::NAN, Vec3A::NAN, Vec3A::NAN);
62
63 #[allow(clippy::too_many_arguments)]
64 #[inline(always)]
65 #[must_use]
66 const fn new(
67 m00: f32,
68 m01: f32,
69 m02: f32,
70 m10: f32,
71 m11: f32,
72 m12: f32,
73 m20: f32,
74 m21: f32,
75 m22: f32,
76 ) -> Self {
77 Self {
78 x_axis: Vec3A::new(m00, m01, m02),
79 y_axis: Vec3A::new(m10, m11, m12),
80 z_axis: Vec3A::new(m20, m21, m22),
81 }
82 }
83
84 #[inline(always)]
86 #[must_use]
87 pub const fn from_cols(x_axis: Vec3A, y_axis: Vec3A, z_axis: Vec3A) -> Self {
88 Self {
89 x_axis,
90 y_axis,
91 z_axis,
92 }
93 }
94
95 #[inline]
99 #[must_use]
100 pub const fn from_cols_array(m: &[f32; 9]) -> Self {
101 Self::new(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8])
102 }
103
104 #[inline]
107 #[must_use]
108 pub const fn to_cols_array(&self) -> [f32; 9] {
109 let [x_axis_x, x_axis_y, x_axis_z] = self.x_axis.to_array();
110 let [y_axis_x, y_axis_y, y_axis_z] = self.y_axis.to_array();
111 let [z_axis_x, z_axis_y, z_axis_z] = self.z_axis.to_array();
112
113 [
114 x_axis_x, x_axis_y, x_axis_z, y_axis_x, y_axis_y, y_axis_z, z_axis_x, z_axis_y,
115 z_axis_z,
116 ]
117 }
118
119 #[inline]
123 #[must_use]
124 pub const fn from_cols_array_2d(m: &[[f32; 3]; 3]) -> Self {
125 Self::from_cols(
126 Vec3A::from_array(m[0]),
127 Vec3A::from_array(m[1]),
128 Vec3A::from_array(m[2]),
129 )
130 }
131
132 #[inline]
135 #[must_use]
136 pub const fn to_cols_array_2d(&self) -> [[f32; 3]; 3] {
137 [
138 self.x_axis.to_array(),
139 self.y_axis.to_array(),
140 self.z_axis.to_array(),
141 ]
142 }
143
144 #[doc(alias = "scale")]
146 #[inline]
147 #[must_use]
148 pub const fn from_diagonal(diagonal: Vec3) -> Self {
149 Self::new(
150 diagonal.x, 0.0, 0.0, 0.0, diagonal.y, 0.0, 0.0, 0.0, diagonal.z,
151 )
152 }
153
154 #[inline]
156 #[must_use]
157 pub fn from_mat4(m: Mat4) -> Self {
158 Self::from_cols(m.x_axis.into(), m.y_axis.into(), m.z_axis.into())
159 }
160
161 #[inline]
167 #[must_use]
168 pub fn from_quat(rotation: Quat) -> Self {
169 glam_assert!(rotation.is_normalized());
170
171 let x2 = rotation.x + rotation.x;
172 let y2 = rotation.y + rotation.y;
173 let z2 = rotation.z + rotation.z;
174 let xx = rotation.x * x2;
175 let xy = rotation.x * y2;
176 let xz = rotation.x * z2;
177 let yy = rotation.y * y2;
178 let yz = rotation.y * z2;
179 let zz = rotation.z * z2;
180 let wx = rotation.w * x2;
181 let wy = rotation.w * y2;
182 let wz = rotation.w * z2;
183
184 Self::from_cols(
185 Vec3A::new(1.0 - (yy + zz), xy + wz, xz - wy),
186 Vec3A::new(xy - wz, 1.0 - (xx + zz), yz + wx),
187 Vec3A::new(xz + wy, yz - wx, 1.0 - (xx + yy)),
188 )
189 }
190
191 #[inline]
198 #[must_use]
199 pub fn from_axis_angle(axis: Vec3, angle: f32) -> Self {
200 glam_assert!(axis.is_normalized());
201
202 let (sin, cos) = math::sin_cos(angle);
203 let (xsin, ysin, zsin) = axis.mul(sin).into();
204 let (x, y, z) = axis.into();
205 let (x2, y2, z2) = axis.mul(axis).into();
206 let omc = 1.0 - cos;
207 let xyomc = x * y * omc;
208 let xzomc = x * z * omc;
209 let yzomc = y * z * omc;
210 Self::from_cols(
211 Vec3A::new(x2 * omc + cos, xyomc + zsin, xzomc - ysin),
212 Vec3A::new(xyomc - zsin, y2 * omc + cos, yzomc + xsin),
213 Vec3A::new(xzomc + ysin, yzomc - xsin, z2 * omc + cos),
214 )
215 }
216
217 #[inline]
220 #[must_use]
221 pub fn from_euler(order: EulerRot, a: f32, b: f32, c: f32) -> Self {
222 let quat = Quat::from_euler(order, a, b, c);
223 Self::from_quat(quat)
224 }
225
226 #[inline]
228 #[must_use]
229 pub fn from_rotation_x(angle: f32) -> Self {
230 let (sina, cosa) = math::sin_cos(angle);
231 Self::from_cols(
232 Vec3A::X,
233 Vec3A::new(0.0, cosa, sina),
234 Vec3A::new(0.0, -sina, cosa),
235 )
236 }
237
238 #[inline]
240 #[must_use]
241 pub fn from_rotation_y(angle: f32) -> Self {
242 let (sina, cosa) = math::sin_cos(angle);
243 Self::from_cols(
244 Vec3A::new(cosa, 0.0, -sina),
245 Vec3A::Y,
246 Vec3A::new(sina, 0.0, cosa),
247 )
248 }
249
250 #[inline]
252 #[must_use]
253 pub fn from_rotation_z(angle: f32) -> Self {
254 let (sina, cosa) = math::sin_cos(angle);
255 Self::from_cols(
256 Vec3A::new(cosa, sina, 0.0),
257 Vec3A::new(-sina, cosa, 0.0),
258 Vec3A::Z,
259 )
260 }
261
262 #[inline]
267 #[must_use]
268 pub fn from_translation(translation: Vec2) -> Self {
269 Self::from_cols(
270 Vec3A::X,
271 Vec3A::Y,
272 Vec3A::new(translation.x, translation.y, 1.0),
273 )
274 }
275
276 #[inline]
282 #[must_use]
283 pub fn from_angle(angle: f32) -> Self {
284 let (sin, cos) = math::sin_cos(angle);
285 Self::from_cols(
286 Vec3A::new(cos, sin, 0.0),
287 Vec3A::new(-sin, cos, 0.0),
288 Vec3A::Z,
289 )
290 }
291
292 #[inline]
298 #[must_use]
299 pub fn from_scale_angle_translation(scale: Vec2, angle: f32, translation: Vec2) -> Self {
300 let (sin, cos) = math::sin_cos(angle);
301 Self::from_cols(
302 Vec3A::new(cos * scale.x, sin * scale.x, 0.0),
303 Vec3A::new(-sin * scale.y, cos * scale.y, 0.0),
304 Vec3A::new(translation.x, translation.y, 1.0),
305 )
306 }
307
308 #[inline]
317 #[must_use]
318 pub fn from_scale(scale: Vec2) -> Self {
319 glam_assert!(scale.cmpne(Vec2::ZERO).any());
321
322 Self::from_cols(
323 Vec3A::new(scale.x, 0.0, 0.0),
324 Vec3A::new(0.0, scale.y, 0.0),
325 Vec3A::Z,
326 )
327 }
328
329 #[inline]
334 pub fn from_mat2(m: Mat2) -> Self {
335 Self::from_cols((m.x_axis, 0.0).into(), (m.y_axis, 0.0).into(), Vec3A::Z)
336 }
337
338 #[inline]
344 #[must_use]
345 pub const fn from_cols_slice(slice: &[f32]) -> Self {
346 Self::new(
347 slice[0], slice[1], slice[2], slice[3], slice[4], slice[5], slice[6], slice[7],
348 slice[8],
349 )
350 }
351
352 #[inline]
358 pub fn write_cols_to_slice(self, slice: &mut [f32]) {
359 slice[0] = self.x_axis.x;
360 slice[1] = self.x_axis.y;
361 slice[2] = self.x_axis.z;
362 slice[3] = self.y_axis.x;
363 slice[4] = self.y_axis.y;
364 slice[5] = self.y_axis.z;
365 slice[6] = self.z_axis.x;
366 slice[7] = self.z_axis.y;
367 slice[8] = self.z_axis.z;
368 }
369
370 #[inline]
376 #[must_use]
377 pub fn col(&self, index: usize) -> Vec3A {
378 match index {
379 0 => self.x_axis,
380 1 => self.y_axis,
381 2 => self.z_axis,
382 _ => panic!("index out of bounds"),
383 }
384 }
385
386 #[inline]
392 pub fn col_mut(&mut self, index: usize) -> &mut Vec3A {
393 match index {
394 0 => &mut self.x_axis,
395 1 => &mut self.y_axis,
396 2 => &mut self.z_axis,
397 _ => panic!("index out of bounds"),
398 }
399 }
400
401 #[inline]
407 #[must_use]
408 pub fn row(&self, index: usize) -> Vec3A {
409 match index {
410 0 => Vec3A::new(self.x_axis.x, self.y_axis.x, self.z_axis.x),
411 1 => Vec3A::new(self.x_axis.y, self.y_axis.y, self.z_axis.y),
412 2 => Vec3A::new(self.x_axis.z, self.y_axis.z, self.z_axis.z),
413 _ => panic!("index out of bounds"),
414 }
415 }
416
417 #[inline]
420 #[must_use]
421 pub fn is_finite(&self) -> bool {
422 self.x_axis.is_finite() && self.y_axis.is_finite() && self.z_axis.is_finite()
423 }
424
425 #[inline]
427 #[must_use]
428 pub fn is_nan(&self) -> bool {
429 self.x_axis.is_nan() || self.y_axis.is_nan() || self.z_axis.is_nan()
430 }
431
432 #[inline]
434 #[must_use]
435 pub fn transpose(&self) -> Self {
436 unsafe {
437 let tmp0 = _mm_shuffle_ps(self.x_axis.0, self.y_axis.0, 0b01_00_01_00);
438 let tmp1 = _mm_shuffle_ps(self.x_axis.0, self.y_axis.0, 0b11_10_11_10);
439
440 Self {
441 x_axis: Vec3A(_mm_shuffle_ps(tmp0, self.z_axis.0, 0b00_00_10_00)),
442 y_axis: Vec3A(_mm_shuffle_ps(tmp0, self.z_axis.0, 0b01_01_11_01)),
443 z_axis: Vec3A(_mm_shuffle_ps(tmp1, self.z_axis.0, 0b10_10_10_00)),
444 }
445 }
446 }
447
448 #[inline]
450 #[must_use]
451 pub fn determinant(&self) -> f32 {
452 self.z_axis.dot(self.x_axis.cross(self.y_axis))
453 }
454
455 #[inline]
463 #[must_use]
464 pub fn inverse(&self) -> Self {
465 let tmp0 = self.y_axis.cross(self.z_axis);
466 let tmp1 = self.z_axis.cross(self.x_axis);
467 let tmp2 = self.x_axis.cross(self.y_axis);
468 let det = self.z_axis.dot(tmp2);
469 glam_assert!(det != 0.0);
470 let inv_det = Vec3A::splat(det.recip());
471 Self::from_cols(tmp0.mul(inv_det), tmp1.mul(inv_det), tmp2.mul(inv_det)).transpose()
472 }
473
474 #[inline]
484 #[must_use]
485 pub fn transform_point2(&self, rhs: Vec2) -> Vec2 {
486 glam_assert!(self.row(2).abs_diff_eq(Vec3A::Z, 1e-6));
487 Mat2::from_cols(self.x_axis.xy(), self.y_axis.xy()) * rhs + self.z_axis.xy()
488 }
489
490 #[inline]
500 #[must_use]
501 pub fn transform_vector2(&self, rhs: Vec2) -> Vec2 {
502 glam_assert!(self.row(2).abs_diff_eq(Vec3A::Z, 1e-6));
503 Mat2::from_cols(self.x_axis.xy(), self.y_axis.xy()) * rhs
504 }
505
506 #[inline]
508 #[must_use]
509 pub fn mul_vec3(&self, rhs: Vec3) -> Vec3 {
510 self.mul_vec3a(rhs.into()).into()
511 }
512
513 #[inline]
515 #[must_use]
516 pub fn mul_vec3a(&self, rhs: Vec3A) -> Vec3A {
517 let mut res = self.x_axis.mul(rhs.xxx());
518 res = res.add(self.y_axis.mul(rhs.yyy()));
519 res = res.add(self.z_axis.mul(rhs.zzz()));
520 res
521 }
522
523 #[inline]
525 #[must_use]
526 pub fn mul_mat3(&self, rhs: &Self) -> Self {
527 Self::from_cols(
528 self.mul(rhs.x_axis),
529 self.mul(rhs.y_axis),
530 self.mul(rhs.z_axis),
531 )
532 }
533
534 #[inline]
536 #[must_use]
537 pub fn add_mat3(&self, rhs: &Self) -> Self {
538 Self::from_cols(
539 self.x_axis.add(rhs.x_axis),
540 self.y_axis.add(rhs.y_axis),
541 self.z_axis.add(rhs.z_axis),
542 )
543 }
544
545 #[inline]
547 #[must_use]
548 pub fn sub_mat3(&self, rhs: &Self) -> Self {
549 Self::from_cols(
550 self.x_axis.sub(rhs.x_axis),
551 self.y_axis.sub(rhs.y_axis),
552 self.z_axis.sub(rhs.z_axis),
553 )
554 }
555
556 #[inline]
558 #[must_use]
559 pub fn mul_scalar(&self, rhs: f32) -> Self {
560 Self::from_cols(
561 self.x_axis.mul(rhs),
562 self.y_axis.mul(rhs),
563 self.z_axis.mul(rhs),
564 )
565 }
566
567 #[inline]
577 #[must_use]
578 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
579 self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
580 && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
581 && self.z_axis.abs_diff_eq(rhs.z_axis, max_abs_diff)
582 }
583
584 #[inline]
585 pub fn as_dmat3(&self) -> DMat3 {
586 DMat3::from_cols(
587 self.x_axis.as_dvec3(),
588 self.y_axis.as_dvec3(),
589 self.z_axis.as_dvec3(),
590 )
591 }
592}
593
594impl Default for Mat3A {
595 #[inline]
596 fn default() -> Self {
597 Self::IDENTITY
598 }
599}
600
601impl Add<Mat3A> for Mat3A {
602 type Output = Self;
603 #[inline]
604 fn add(self, rhs: Self) -> Self::Output {
605 self.add_mat3(&rhs)
606 }
607}
608
609impl AddAssign<Mat3A> for Mat3A {
610 #[inline]
611 fn add_assign(&mut self, rhs: Self) {
612 *self = self.add_mat3(&rhs);
613 }
614}
615
616impl Sub<Mat3A> for Mat3A {
617 type Output = Self;
618 #[inline]
619 fn sub(self, rhs: Self) -> Self::Output {
620 self.sub_mat3(&rhs)
621 }
622}
623
624impl SubAssign<Mat3A> for Mat3A {
625 #[inline]
626 fn sub_assign(&mut self, rhs: Self) {
627 *self = self.sub_mat3(&rhs);
628 }
629}
630
631impl Neg for Mat3A {
632 type Output = Self;
633 #[inline]
634 fn neg(self) -> Self::Output {
635 Self::from_cols(self.x_axis.neg(), self.y_axis.neg(), self.z_axis.neg())
636 }
637}
638
639impl Mul<Mat3A> for Mat3A {
640 type Output = Self;
641 #[inline]
642 fn mul(self, rhs: Self) -> Self::Output {
643 self.mul_mat3(&rhs)
644 }
645}
646
647impl MulAssign<Mat3A> for Mat3A {
648 #[inline]
649 fn mul_assign(&mut self, rhs: Self) {
650 *self = self.mul_mat3(&rhs);
651 }
652}
653
654impl Mul<Vec3A> for Mat3A {
655 type Output = Vec3A;
656 #[inline]
657 fn mul(self, rhs: Vec3A) -> Self::Output {
658 self.mul_vec3a(rhs)
659 }
660}
661
662impl Mul<Mat3A> for f32 {
663 type Output = Mat3A;
664 #[inline]
665 fn mul(self, rhs: Mat3A) -> Self::Output {
666 rhs.mul_scalar(self)
667 }
668}
669
670impl Mul<f32> for Mat3A {
671 type Output = Self;
672 #[inline]
673 fn mul(self, rhs: f32) -> Self::Output {
674 self.mul_scalar(rhs)
675 }
676}
677
678impl MulAssign<f32> for Mat3A {
679 #[inline]
680 fn mul_assign(&mut self, rhs: f32) {
681 *self = self.mul_scalar(rhs);
682 }
683}
684
685impl Mul<Vec3> for Mat3A {
686 type Output = Vec3;
687 #[inline]
688 fn mul(self, rhs: Vec3) -> Vec3 {
689 self.mul_vec3a(rhs.into()).into()
690 }
691}
692
693impl From<Mat3> for Mat3A {
694 #[inline]
695 fn from(m: Mat3) -> Self {
696 Self {
697 x_axis: m.x_axis.into(),
698 y_axis: m.y_axis.into(),
699 z_axis: m.z_axis.into(),
700 }
701 }
702}
703
704impl Sum<Self> for Mat3A {
705 fn sum<I>(iter: I) -> Self
706 where
707 I: Iterator<Item = Self>,
708 {
709 iter.fold(Self::ZERO, Self::add)
710 }
711}
712
713impl<'a> Sum<&'a Self> for Mat3A {
714 fn sum<I>(iter: I) -> Self
715 where
716 I: Iterator<Item = &'a Self>,
717 {
718 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
719 }
720}
721
722impl Product for Mat3A {
723 fn product<I>(iter: I) -> Self
724 where
725 I: Iterator<Item = Self>,
726 {
727 iter.fold(Self::IDENTITY, Self::mul)
728 }
729}
730
731impl<'a> Product<&'a Self> for Mat3A {
732 fn product<I>(iter: I) -> Self
733 where
734 I: Iterator<Item = &'a Self>,
735 {
736 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
737 }
738}
739
740impl PartialEq for Mat3A {
741 #[inline]
742 fn eq(&self, rhs: &Self) -> bool {
743 self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis) && self.z_axis.eq(&rhs.z_axis)
744 }
745}
746
747#[cfg(not(target_arch = "spirv"))]
748impl fmt::Debug for Mat3A {
749 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
750 fmt.debug_struct(stringify!(Mat3A))
751 .field("x_axis", &self.x_axis)
752 .field("y_axis", &self.y_axis)
753 .field("z_axis", &self.z_axis)
754 .finish()
755 }
756}
757
758#[cfg(not(target_arch = "spirv"))]
759impl fmt::Display for Mat3A {
760 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
761 write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
762 }
763}