1use crate::{Affine3A, Mat3, Mat4, Quat, Vec3};
4use core::ops::{Deref, DerefMut, Mul, MulAssign};
5
6#[cfg(all(feature = "zerocopy-08", not(feature = "core-simd")))]
7use zerocopy_derive_08::*;
8
9#[derive(Copy, Clone)]
11#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
12#[cfg_attr(
13 all(feature = "zerocopy-08", not(feature = "core-simd")),
14 derive(FromBytes, Immutable, IntoBytes, KnownLayout)
15)]
16#[repr(C)]
17pub struct Affine3 {
18 pub matrix3: Mat3,
19 pub translation: Vec3,
20}
21
22impl Affine3 {
23 pub const ZERO: Self = Self {
28 matrix3: Mat3::ZERO,
29 translation: Vec3::ZERO,
30 };
31
32 pub const IDENTITY: Self = Self {
36 matrix3: Mat3::IDENTITY,
37 translation: Vec3::ZERO,
38 };
39
40 pub const NAN: Self = Self {
42 matrix3: Mat3::NAN,
43 translation: Vec3::NAN,
44 };
45
46 #[inline(always)]
48 #[must_use]
49 pub const fn from_cols(x_axis: Vec3, y_axis: Vec3, z_axis: Vec3, w_axis: Vec3) -> Self {
50 Self {
51 matrix3: Mat3::from_cols(x_axis, y_axis, z_axis),
52 translation: w_axis,
53 }
54 }
55
56 #[inline]
58 #[must_use]
59 pub fn from_cols_array(m: &[f32; 12]) -> Self {
60 Self {
61 matrix3: Mat3::from_cols_array(&[m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]]),
62 translation: Vec3::from_array([m[9], m[10], m[11]]),
63 }
64 }
65
66 #[inline]
68 #[must_use]
69 pub fn to_cols_array(&self) -> [f32; 12] {
70 let x = &self.matrix3.x_axis;
71 let y = &self.matrix3.y_axis;
72 let z = &self.matrix3.z_axis;
73 let w = &self.translation;
74 [x.x, x.y, x.z, y.x, y.y, y.z, z.x, z.y, z.z, w.x, w.y, w.z]
75 }
76
77 #[inline]
82 #[must_use]
83 pub fn from_cols_array_2d(m: &[[f32; 3]; 4]) -> Self {
84 Self {
85 matrix3: Mat3::from_cols(m[0].into(), m[1].into(), m[2].into()),
86 translation: m[3].into(),
87 }
88 }
89
90 #[inline]
94 #[must_use]
95 pub fn to_cols_array_2d(&self) -> [[f32; 3]; 4] {
96 [
97 self.matrix3.x_axis.into(),
98 self.matrix3.y_axis.into(),
99 self.matrix3.z_axis.into(),
100 self.translation.into(),
101 ]
102 }
103
104 #[inline]
110 #[must_use]
111 pub fn from_cols_slice(slice: &[f32]) -> Self {
112 Self {
113 matrix3: Mat3::from_cols_slice(&slice[0..9]),
114 translation: Vec3::from_slice(&slice[9..12]),
115 }
116 }
117
118 #[inline]
124 pub fn write_cols_to_slice(&self, slice: &mut [f32]) {
125 self.matrix3.write_cols_to_slice(&mut slice[0..9]);
126 self.translation.write_to_slice(&mut slice[9..12]);
127 }
128
129 #[inline]
132 #[must_use]
133 pub fn from_scale(scale: Vec3) -> Self {
134 Self {
135 matrix3: Mat3::from_diagonal(scale),
136 translation: Vec3::ZERO,
137 }
138 }
139 #[inline]
145 #[must_use]
146 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
147 pub fn from_quat(rotation: Quat) -> Self {
148 Self {
149 matrix3: Mat3::from_quat(rotation),
150 translation: Vec3::ZERO,
151 }
152 }
153
154 #[inline]
161 #[must_use]
162 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
163 pub fn from_axis_angle(axis: Vec3, angle: f32) -> Self {
164 Self {
165 matrix3: Mat3::from_axis_angle(axis, angle),
166 translation: Vec3::ZERO,
167 }
168 }
169
170 #[inline]
173 #[must_use]
174 pub fn from_rotation_x(angle: f32) -> Self {
175 Self {
176 matrix3: Mat3::from_rotation_x(angle),
177 translation: Vec3::ZERO,
178 }
179 }
180
181 #[inline]
184 #[must_use]
185 pub fn from_rotation_y(angle: f32) -> Self {
186 Self {
187 matrix3: Mat3::from_rotation_y(angle),
188 translation: Vec3::ZERO,
189 }
190 }
191
192 #[inline]
195 #[must_use]
196 pub fn from_rotation_z(angle: f32) -> Self {
197 Self {
198 matrix3: Mat3::from_rotation_z(angle),
199 translation: Vec3::ZERO,
200 }
201 }
202
203 #[inline]
205 #[must_use]
206 pub fn from_translation(translation: Vec3) -> Self {
207 #[allow(clippy::useless_conversion)]
208 Self {
209 matrix3: Mat3::IDENTITY,
210 translation: translation.into(),
211 }
212 }
213
214 #[inline]
217 #[must_use]
218 pub fn from_mat3(mat3: Mat3) -> Self {
219 #[allow(clippy::useless_conversion)]
220 Self {
221 matrix3: mat3.into(),
222 translation: Vec3::ZERO,
223 }
224 }
225
226 #[inline]
231 #[must_use]
232 pub fn from_mat3_translation(mat3: Mat3, translation: Vec3) -> Self {
233 #[allow(clippy::useless_conversion)]
234 Self {
235 matrix3: mat3.into(),
236 translation: translation.into(),
237 }
238 }
239
240 #[inline]
250 #[must_use]
251 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
252 pub fn from_scale_rotation_translation(scale: Vec3, rotation: Quat, translation: Vec3) -> Self {
253 let rotation = Mat3::from_quat(rotation);
254 #[allow(clippy::useless_conversion)]
255 Self {
256 matrix3: Mat3::from_cols(
257 rotation.x_axis * scale.x,
258 rotation.y_axis * scale.y,
259 rotation.z_axis * scale.z,
260 ),
261 translation: translation.into(),
262 }
263 }
264
265 #[inline]
273 #[must_use]
274 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
275 pub fn from_rotation_translation(rotation: Quat, translation: Vec3) -> Self {
276 #[allow(clippy::useless_conversion)]
277 Self {
278 matrix3: Mat3::from_quat(rotation),
279 translation: translation.into(),
280 }
281 }
282
283 #[inline]
286 #[must_use]
287 pub fn from_mat4(m: Mat4) -> Self {
288 Self {
289 matrix3: Mat3::from_cols(
290 Vec3::from_vec4(m.x_axis),
291 Vec3::from_vec4(m.y_axis),
292 Vec3::from_vec4(m.z_axis),
293 ),
294 translation: Vec3::from_vec4(m.w_axis),
295 }
296 }
297
298 #[inline]
308 #[must_use]
309 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
310 pub fn to_scale_rotation_translation(&self) -> (Vec3, Quat, Vec3) {
311 let det = self.matrix3.determinant();
312 glam_assert!(det != 0.0);
313
314 let scale = Vec3::new(
315 self.matrix3.x_axis.length() * det.signum(),
316 self.matrix3.y_axis.length(),
317 self.matrix3.z_axis.length(),
318 );
319
320 glam_assert!(scale.cmpne(Vec3::ZERO).all());
321
322 let inv_scale = scale.recip();
323
324 #[allow(clippy::useless_conversion)]
325 let rotation = Quat::from_mat3(&Mat3::from_cols(
326 (self.matrix3.x_axis * inv_scale.x).into(),
327 (self.matrix3.y_axis * inv_scale.y).into(),
328 (self.matrix3.z_axis * inv_scale.z).into(),
329 ));
330
331 #[allow(clippy::useless_conversion)]
332 (scale, rotation, self.translation.into())
333 }
334
335 #[inline]
337 pub fn transform_point3(&self, rhs: Vec3) -> Vec3 {
338 #[allow(clippy::useless_conversion)]
339 ((self.matrix3.x_axis * rhs.x)
340 + (self.matrix3.y_axis * rhs.y)
341 + (self.matrix3.z_axis * rhs.z)
342 + self.translation)
343 .into()
344 }
345
346 #[inline]
351 #[must_use]
352 pub fn transform_vector3(&self, rhs: Vec3) -> Vec3 {
353 #[allow(clippy::useless_conversion)]
354 ((self.matrix3.x_axis * rhs.x)
355 + (self.matrix3.y_axis * rhs.y)
356 + (self.matrix3.z_axis * rhs.z))
357 .into()
358 }
359
360 #[inline]
365 #[must_use]
366 pub fn is_finite(&self) -> bool {
367 self.matrix3.is_finite() && self.translation.is_finite()
368 }
369
370 #[inline]
372 #[must_use]
373 pub fn is_nan(&self) -> bool {
374 self.matrix3.is_nan() || self.translation.is_nan()
375 }
376
377 #[inline]
387 #[must_use]
388 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
389 self.matrix3.abs_diff_eq(rhs.matrix3, max_abs_diff)
390 && self.translation.abs_diff_eq(rhs.translation, max_abs_diff)
391 }
392
393 #[inline]
401 #[must_use]
402 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
403 pub fn inverse(&self) -> Self {
404 let matrix3 = self.matrix3.inverse();
405 let translation = -(matrix3 * self.translation);
407
408 Self {
409 matrix3,
410 translation,
411 }
412 }
413
414 #[cfg(feature = "f64")]
416 #[inline]
417 #[must_use]
418 pub fn as_daffine3(&self) -> crate::DAffine3 {
419 crate::DAffine3::from_mat3_translation(self.matrix3.as_dmat3(), self.translation.as_dvec3())
420 }
421}
422
423impl Default for Affine3 {
424 #[inline(always)]
425 fn default() -> Self {
426 Self::IDENTITY
427 }
428}
429
430impl Deref for Affine3 {
431 type Target = crate::deref::Cols4<Vec3>;
432 #[inline(always)]
433 fn deref(&self) -> &Self::Target {
434 unsafe { &*(self as *const Self as *const Self::Target) }
435 }
436}
437
438impl DerefMut for Affine3 {
439 #[inline(always)]
440 fn deref_mut(&mut self) -> &mut Self::Target {
441 unsafe { &mut *(self as *mut Self as *mut Self::Target) }
442 }
443}
444
445impl PartialEq for Affine3 {
446 #[inline]
447 fn eq(&self, rhs: &Self) -> bool {
448 self.matrix3.eq(&rhs.matrix3) && self.translation.eq(&rhs.translation)
449 }
450}
451
452impl core::fmt::Debug for Affine3 {
453 fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
454 fmt.debug_struct(stringify!(Affine3))
455 .field("matrix3", &self.matrix3)
456 .field("translation", &self.translation)
457 .finish()
458 }
459}
460
461impl core::fmt::Display for Affine3 {
462 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
463 if let Some(p) = f.precision() {
464 write!(
465 f,
466 "[{:.*}, {:.*}, {:.*}, {:.*}]",
467 p,
468 self.matrix3.x_axis,
469 p,
470 self.matrix3.y_axis,
471 p,
472 self.matrix3.z_axis,
473 p,
474 self.translation
475 )
476 } else {
477 write!(
478 f,
479 "[{}, {}, {}, {}]",
480 self.matrix3.x_axis, self.matrix3.y_axis, self.matrix3.z_axis, self.translation
481 )
482 }
483 }
484}
485
486impl<'a> core::iter::Product<&'a Self> for Affine3 {
487 fn product<I>(iter: I) -> Self
488 where
489 I: Iterator<Item = &'a Self>,
490 {
491 iter.fold(Self::IDENTITY, |a, &b| a * b)
492 }
493}
494
495impl Mul for Affine3 {
496 type Output = Self;
497
498 #[inline]
499 fn mul(self, rhs: Self) -> Self {
500 Self {
501 matrix3: self.matrix3 * rhs.matrix3,
502 translation: self.matrix3 * rhs.translation + self.translation,
503 }
504 }
505}
506
507impl Mul<&Self> for Affine3 {
508 type Output = Self;
509 #[inline]
510 fn mul(self, rhs: &Self) -> Self {
511 self.mul(*rhs)
512 }
513}
514
515impl Mul<&Affine3> for &Affine3 {
516 type Output = Affine3;
517 #[inline]
518 fn mul(self, rhs: &Affine3) -> Affine3 {
519 (*self).mul(*rhs)
520 }
521}
522
523impl Mul<Affine3> for &Affine3 {
524 type Output = Affine3;
525 #[inline]
526 fn mul(self, rhs: Affine3) -> Affine3 {
527 (*self).mul(rhs)
528 }
529}
530
531impl MulAssign for Affine3 {
532 #[inline]
533 fn mul_assign(&mut self, rhs: Self) {
534 *self = self.mul(rhs);
535 }
536}
537
538impl MulAssign<&Self> for Affine3 {
539 #[inline]
540 fn mul_assign(&mut self, rhs: &Self) {
541 self.mul_assign(*rhs);
542 }
543}
544
545impl Mul<Mat4> for Affine3 {
546 type Output = Mat4;
547
548 #[inline]
549 fn mul(self, rhs: Mat4) -> Self::Output {
550 Mat4::from(self) * rhs
551 }
552}
553
554impl Mul<&Mat4> for Affine3 {
555 type Output = Mat4;
556 #[inline]
557 fn mul(self, rhs: &Mat4) -> Mat4 {
558 self.mul(*rhs)
559 }
560}
561
562impl Mul<&Mat4> for &Affine3 {
563 type Output = Mat4;
564 #[inline]
565 fn mul(self, rhs: &Mat4) -> Mat4 {
566 (*self).mul(*rhs)
567 }
568}
569
570impl Mul<Mat4> for &Affine3 {
571 type Output = Mat4;
572 #[inline]
573 fn mul(self, rhs: Mat4) -> Mat4 {
574 (*self).mul(rhs)
575 }
576}
577
578impl Mul<Affine3> for Mat4 {
579 type Output = Self;
580
581 #[inline]
582 fn mul(self, rhs: Affine3) -> Self {
583 self * Self::from(rhs)
584 }
585}
586
587impl Mul<&Affine3> for Mat4 {
588 type Output = Self;
589 #[inline]
590 fn mul(self, rhs: &Affine3) -> Self {
591 self.mul(*rhs)
592 }
593}
594
595impl Mul<&Affine3> for &Mat4 {
596 type Output = Mat4;
597 #[inline]
598 fn mul(self, rhs: &Affine3) -> Mat4 {
599 (*self).mul(*rhs)
600 }
601}
602
603impl Mul<Affine3> for &Mat4 {
604 type Output = Mat4;
605 #[inline]
606 fn mul(self, rhs: Affine3) -> Mat4 {
607 (*self).mul(rhs)
608 }
609}
610
611impl MulAssign<Affine3> for Mat4 {
612 #[inline]
613 fn mul_assign(&mut self, rhs: Affine3) {
614 *self = self.mul(rhs);
615 }
616}
617
618impl MulAssign<&Affine3> for Mat4 {
619 #[inline]
620 fn mul_assign(&mut self, rhs: &Affine3) {
621 self.mul_assign(*rhs);
622 }
623}
624
625impl From<Affine3> for Mat4 {
626 #[inline]
627 fn from(m: Affine3) -> Self {
628 Self::from_cols(
629 m.matrix3.x_axis.extend(0.0),
630 m.matrix3.y_axis.extend(0.0),
631 m.matrix3.z_axis.extend(0.0),
632 m.translation.extend(1.0),
633 )
634 }
635}
636
637impl From<Affine3A> for Affine3 {
638 #[inline]
639 fn from(a: Affine3A) -> Self {
640 Self::from_cols(
641 a.matrix3.x_axis.into(),
642 a.matrix3.y_axis.into(),
643 a.matrix3.z_axis.into(),
644 a.translation.into(),
645 )
646 }
647}