1use crate::{Mat2, Mat3, Mat3A, Vec2, Vec3A};
4use core::ops::{Deref, DerefMut, Mul, MulAssign};
5
6#[cfg(all(feature = "zerocopy", not(feature = "core-simd")))]
7use zerocopy_derive::*;
8
9#[derive(Copy, Clone)]
11#[cfg_attr(
12 all(feature = "bytemuck", not(feature = "scalar-math")),
13 derive(bytemuck::AnyBitPattern)
14)]
15#[cfg_attr(
16 all(feature = "bytemuck", feature = "scalar-math"),
17 derive(bytemuck::Pod, bytemuck::Zeroable)
18)]
19#[cfg_attr(
20 all(feature = "zerocopy", not(feature = "core-simd")),
21 derive(FromBytes, Immutable, KnownLayout)
22)]
23#[repr(C)]
24pub struct Affine2 {
25 pub matrix2: Mat2,
26 pub translation: Vec2,
27}
28
29impl Affine2 {
30 pub const ZERO: Self = Self {
35 matrix2: Mat2::ZERO,
36 translation: Vec2::ZERO,
37 };
38
39 pub const IDENTITY: Self = Self {
43 matrix2: Mat2::IDENTITY,
44 translation: Vec2::ZERO,
45 };
46
47 pub const NAN: Self = Self {
49 matrix2: Mat2::NAN,
50 translation: Vec2::NAN,
51 };
52
53 #[inline(always)]
55 #[must_use]
56 pub const fn from_cols(x_axis: Vec2, y_axis: Vec2, z_axis: Vec2) -> Self {
57 Self {
58 matrix2: Mat2::from_cols(x_axis, y_axis),
59 translation: z_axis,
60 }
61 }
62
63 #[inline]
65 #[must_use]
66 pub fn from_cols_array(m: &[f32; 6]) -> Self {
67 Self {
68 matrix2: Mat2::from_cols_array(&[m[0], m[1], m[2], m[3]]),
69 translation: Vec2::from_array([m[4], m[5]]),
70 }
71 }
72
73 #[inline]
75 #[must_use]
76 pub fn to_cols_array(&self) -> [f32; 6] {
77 let x = &self.matrix2.x_axis;
78 let y = &self.matrix2.y_axis;
79 let z = &self.translation;
80 [x.x, x.y, y.x, y.y, z.x, z.y]
81 }
82
83 #[inline]
88 #[must_use]
89 pub fn from_cols_array_2d(m: &[[f32; 2]; 3]) -> Self {
90 Self {
91 matrix2: Mat2::from_cols(m[0].into(), m[1].into()),
92 translation: m[2].into(),
93 }
94 }
95
96 #[inline]
100 #[must_use]
101 pub fn to_cols_array_2d(&self) -> [[f32; 2]; 3] {
102 [
103 self.matrix2.x_axis.into(),
104 self.matrix2.y_axis.into(),
105 self.translation.into(),
106 ]
107 }
108
109 #[inline]
115 #[must_use]
116 pub fn from_cols_slice(slice: &[f32]) -> Self {
117 Self {
118 matrix2: Mat2::from_cols_slice(&slice[0..4]),
119 translation: Vec2::from_slice(&slice[4..6]),
120 }
121 }
122
123 #[inline]
129 pub fn write_cols_to_slice(&self, slice: &mut [f32]) {
130 self.matrix2.write_cols_to_slice(&mut slice[0..4]);
131 self.translation.write_to_slice(&mut slice[4..6]);
132 }
133
134 #[inline]
137 #[must_use]
138 pub fn from_scale(scale: Vec2) -> Self {
139 Self {
140 matrix2: Mat2::from_diagonal(scale),
141 translation: Vec2::ZERO,
142 }
143 }
144
145 #[inline]
147 #[must_use]
148 pub fn from_angle(angle: f32) -> Self {
149 Self {
150 matrix2: Mat2::from_angle(angle),
151 translation: Vec2::ZERO,
152 }
153 }
154
155 #[inline]
157 #[must_use]
158 pub fn from_translation(translation: Vec2) -> Self {
159 Self {
160 matrix2: Mat2::IDENTITY,
161 translation,
162 }
163 }
164
165 #[inline]
167 #[must_use]
168 pub fn from_mat2(matrix2: Mat2) -> Self {
169 Self {
170 matrix2,
171 translation: Vec2::ZERO,
172 }
173 }
174
175 #[inline]
181 #[must_use]
182 pub fn from_mat2_translation(matrix2: Mat2, translation: Vec2) -> Self {
183 Self {
184 matrix2,
185 translation,
186 }
187 }
188
189 #[inline]
195 #[must_use]
196 pub fn from_scale_angle_translation(scale: Vec2, angle: f32, translation: Vec2) -> Self {
197 let rotation = Mat2::from_angle(angle);
198 Self {
199 matrix2: Mat2::from_cols(rotation.x_axis * scale.x, rotation.y_axis * scale.y),
200 translation,
201 }
202 }
203
204 #[inline]
209 #[must_use]
210 pub fn from_angle_translation(angle: f32, translation: Vec2) -> Self {
211 Self {
212 matrix2: Mat2::from_angle(angle),
213 translation,
214 }
215 }
216
217 #[inline]
219 #[must_use]
220 pub fn from_mat3(m: Mat3) -> Self {
221 use crate::swizzles::Vec3Swizzles;
222 Self {
223 matrix2: Mat2::from_cols(m.x_axis.xy(), m.y_axis.xy()),
224 translation: m.z_axis.xy(),
225 }
226 }
227
228 #[inline]
230 #[must_use]
231 pub fn from_mat3a(m: Mat3A) -> Self {
232 use crate::swizzles::Vec3Swizzles;
233 Self {
234 matrix2: Mat2::from_cols(m.x_axis.xy(), m.y_axis.xy()),
235 translation: m.z_axis.xy(),
236 }
237 }
238
239 #[inline]
249 #[must_use]
250 pub fn to_scale_angle_translation(&self) -> (Vec2, f32, Vec2) {
251 use crate::f32::math;
252 let det = self.matrix2.determinant();
253 glam_assert!(det != 0.0);
254
255 let scale = Vec2::new(
256 self.matrix2.x_axis.length() * math::signum(det),
257 self.matrix2.y_axis.length(),
258 );
259
260 glam_assert!(scale.cmpne(Vec2::ZERO).all());
261
262 let angle = math::atan2(-self.matrix2.y_axis.x, self.matrix2.y_axis.y);
263
264 (scale, angle, self.translation)
265 }
266
267 #[inline]
269 #[must_use]
270 pub fn transform_point2(&self, rhs: Vec2) -> Vec2 {
271 self.matrix2 * rhs + self.translation
272 }
273
274 #[inline]
279 pub fn transform_vector2(&self, rhs: Vec2) -> Vec2 {
280 self.matrix2 * rhs
281 }
282
283 #[inline]
288 #[must_use]
289 pub fn is_finite(&self) -> bool {
290 self.matrix2.is_finite() && self.translation.is_finite()
291 }
292
293 #[inline]
295 #[must_use]
296 pub fn is_nan(&self) -> bool {
297 self.matrix2.is_nan() || self.translation.is_nan()
298 }
299
300 #[inline]
310 #[must_use]
311 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
312 self.matrix2.abs_diff_eq(rhs.matrix2, max_abs_diff)
313 && self.translation.abs_diff_eq(rhs.translation, max_abs_diff)
314 }
315
316 #[inline]
320 #[must_use]
321 pub fn inverse(&self) -> Self {
322 let matrix2 = self.matrix2.inverse();
323 let translation = -(matrix2 * self.translation);
325
326 Self {
327 matrix2,
328 translation,
329 }
330 }
331
332 #[cfg(feature = "f64")]
334 #[inline]
335 #[must_use]
336 pub fn as_daffine2(&self) -> crate::DAffine2 {
337 crate::DAffine2::from_mat2_translation(self.matrix2.as_dmat2(), self.translation.as_dvec2())
338 }
339}
340
341impl Default for Affine2 {
342 #[inline(always)]
343 fn default() -> Self {
344 Self::IDENTITY
345 }
346}
347
348impl Deref for Affine2 {
349 type Target = crate::deref::Cols3<Vec2>;
350 #[inline(always)]
351 fn deref(&self) -> &Self::Target {
352 unsafe { &*(self as *const Self as *const Self::Target) }
353 }
354}
355
356impl DerefMut for Affine2 {
357 #[inline(always)]
358 fn deref_mut(&mut self) -> &mut Self::Target {
359 unsafe { &mut *(self as *mut Self as *mut Self::Target) }
360 }
361}
362
363impl PartialEq for Affine2 {
364 #[inline]
365 fn eq(&self, rhs: &Self) -> bool {
366 self.matrix2.eq(&rhs.matrix2) && self.translation.eq(&rhs.translation)
367 }
368}
369
370impl core::fmt::Debug for Affine2 {
371 fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
372 fmt.debug_struct(stringify!(Affine2))
373 .field("matrix2", &self.matrix2)
374 .field("translation", &self.translation)
375 .finish()
376 }
377}
378
379impl core::fmt::Display for Affine2 {
380 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
381 if let Some(p) = f.precision() {
382 write!(
383 f,
384 "[{:.*}, {:.*}, {:.*}]",
385 p, self.matrix2.x_axis, p, self.matrix2.y_axis, p, self.translation
386 )
387 } else {
388 write!(
389 f,
390 "[{}, {}, {}]",
391 self.matrix2.x_axis, self.matrix2.y_axis, self.translation
392 )
393 }
394 }
395}
396
397impl<'a> core::iter::Product<&'a Self> for Affine2 {
398 fn product<I>(iter: I) -> Self
399 where
400 I: Iterator<Item = &'a Self>,
401 {
402 iter.fold(Self::IDENTITY, |a, &b| a * b)
403 }
404}
405
406impl Mul for Affine2 {
407 type Output = Self;
408
409 #[inline]
410 fn mul(self, rhs: Self) -> Self {
411 Self {
412 matrix2: self.matrix2 * rhs.matrix2,
413 translation: self.matrix2 * rhs.translation + self.translation,
414 }
415 }
416}
417
418impl Mul<&Self> for Affine2 {
419 type Output = Self;
420 #[inline]
421 fn mul(self, rhs: &Self) -> Self {
422 self.mul(*rhs)
423 }
424}
425
426impl Mul<&Affine2> for &Affine2 {
427 type Output = Affine2;
428 #[inline]
429 fn mul(self, rhs: &Affine2) -> Affine2 {
430 (*self).mul(*rhs)
431 }
432}
433
434impl Mul<Affine2> for &Affine2 {
435 type Output = Affine2;
436 #[inline]
437 fn mul(self, rhs: Affine2) -> Affine2 {
438 (*self).mul(rhs)
439 }
440}
441
442impl MulAssign for Affine2 {
443 #[inline]
444 fn mul_assign(&mut self, rhs: Self) {
445 *self = self.mul(rhs);
446 }
447}
448
449impl MulAssign<&Self> for Affine2 {
450 #[inline]
451 fn mul_assign(&mut self, rhs: &Self) {
452 self.mul_assign(*rhs);
453 }
454}
455
456impl From<Affine2> for Mat3 {
457 #[inline]
458 fn from(m: Affine2) -> Self {
459 Self::from_cols(
460 m.matrix2.x_axis.extend(0.0),
461 m.matrix2.y_axis.extend(0.0),
462 m.translation.extend(1.0),
463 )
464 }
465}
466
467impl Mul<Mat3> for Affine2 {
468 type Output = Mat3;
469
470 #[inline]
471 fn mul(self, rhs: Mat3) -> Self::Output {
472 Mat3::from(self) * rhs
473 }
474}
475
476impl Mul<&Mat3> for Affine2 {
477 type Output = Mat3;
478 #[inline]
479 fn mul(self, rhs: &Mat3) -> Mat3 {
480 self.mul(*rhs)
481 }
482}
483
484impl Mul<&Mat3> for &Affine2 {
485 type Output = Mat3;
486 #[inline]
487 fn mul(self, rhs: &Mat3) -> Mat3 {
488 (*self).mul(*rhs)
489 }
490}
491
492impl Mul<Mat3> for &Affine2 {
493 type Output = Mat3;
494 #[inline]
495 fn mul(self, rhs: Mat3) -> Mat3 {
496 (*self).mul(rhs)
497 }
498}
499
500impl Mul<Affine2> for Mat3 {
501 type Output = Self;
502
503 #[inline]
504 fn mul(self, rhs: Affine2) -> Self {
505 self * Self::from(rhs)
506 }
507}
508
509impl Mul<&Affine2> for Mat3 {
510 type Output = Self;
511 #[inline]
512 fn mul(self, rhs: &Affine2) -> Self {
513 self.mul(*rhs)
514 }
515}
516
517impl Mul<&Affine2> for &Mat3 {
518 type Output = Mat3;
519 #[inline]
520 fn mul(self, rhs: &Affine2) -> Mat3 {
521 (*self).mul(*rhs)
522 }
523}
524
525impl Mul<Affine2> for &Mat3 {
526 type Output = Mat3;
527 #[inline]
528 fn mul(self, rhs: Affine2) -> Mat3 {
529 (*self).mul(rhs)
530 }
531}
532
533impl MulAssign<Affine2> for Mat3 {
534 #[inline]
535 fn mul_assign(&mut self, rhs: Affine2) {
536 *self = self.mul(rhs);
537 }
538}
539
540impl MulAssign<&Affine2> for Mat3 {
541 #[inline]
542 fn mul_assign(&mut self, rhs: &Affine2) {
543 self.mul_assign(*rhs);
544 }
545}
546
547impl Mul<Mat3A> for Affine2 {
548 type Output = Mat3A;
549
550 #[inline]
551 fn mul(self, rhs: Mat3A) -> Self::Output {
552 Mat3A::from(self) * rhs
553 }
554}
555
556impl Mul<&Mat3A> for Affine2 {
557 type Output = Mat3A;
558 #[inline]
559 fn mul(self, rhs: &Mat3A) -> Mat3A {
560 self.mul(*rhs)
561 }
562}
563
564impl Mul<&Mat3A> for &Affine2 {
565 type Output = Mat3A;
566 #[inline]
567 fn mul(self, rhs: &Mat3A) -> Mat3A {
568 (*self).mul(*rhs)
569 }
570}
571
572impl Mul<Mat3A> for &Affine2 {
573 type Output = Mat3A;
574 #[inline]
575 fn mul(self, rhs: Mat3A) -> Mat3A {
576 (*self).mul(rhs)
577 }
578}
579
580impl Mul<Affine2> for Mat3A {
581 type Output = Self;
582
583 #[inline]
584 fn mul(self, rhs: Affine2) -> Self {
585 self * Self::from(rhs)
586 }
587}
588
589impl Mul<&Affine2> for Mat3A {
590 type Output = Self;
591 #[inline]
592 fn mul(self, rhs: &Affine2) -> Self {
593 self.mul(*rhs)
594 }
595}
596
597impl Mul<&Affine2> for &Mat3A {
598 type Output = Mat3A;
599 #[inline]
600 fn mul(self, rhs: &Affine2) -> Mat3A {
601 (*self).mul(*rhs)
602 }
603}
604
605impl Mul<Affine2> for &Mat3A {
606 type Output = Mat3A;
607 #[inline]
608 fn mul(self, rhs: Affine2) -> Mat3A {
609 (*self).mul(rhs)
610 }
611}
612
613impl MulAssign<Affine2> for Mat3A {
614 #[inline]
615 fn mul_assign(&mut self, rhs: Affine2) {
616 *self = self.mul(rhs);
617 }
618}
619
620impl MulAssign<&Affine2> for Mat3A {
621 #[inline]
622 fn mul_assign(&mut self, rhs: &Affine2) {
623 self.mul_assign(*rhs);
624 }
625}
626
627impl From<Affine2> for Mat3A {
628 #[inline]
629 fn from(m: Affine2) -> Self {
630 Self::from_cols(
631 Vec3A::from((m.matrix2.x_axis, 0.0)),
632 Vec3A::from((m.matrix2.y_axis, 0.0)),
633 Vec3A::from((m.translation, 1.0)),
634 )
635 }
636}