Debian Codegraph @

debian/missing-sources/glam-0.20.5/src/affine2.rs

raw symbols SCIP
1 use crate::core::storage::Columns3;
2 use crate::{DMat2, DMat3, DVec2, Mat2, Mat3, Mat3A, Vec2, Vec3A};
3 use core::ops::{Add, Deref, DerefMut, Mul, Sub};
4
5 #[cfg(not(feature = "std"))]
6 use num_traits::Float;
7
8 macro_rules! define_affine2_struct {
9 ($affine2:ident, $matrix:ident, $column:ident) => {
10 /// A 2D affine transform, which can represent translation, rotation, scaling and shear.
11 #[derive(Copy, Clone)]
12 #[repr(C)]
13 pub struct $affine2 {
14 pub matrix2: $matrix,
15 pub translation: $column,
16 }
17 };
18 }
19
20 macro_rules! impl_affine2_methods {
21 ($t:ty, $mat2:ident, $mat3:ident, $vec2:ident, $affine2:ident, $matrix:ident, $column:ident) => {
22 impl $affine2 {
23 /// The degenerate zero transform.
24 ///
25 /// This transforms any finite vector and point to zero.
26 /// The zero transform is non-invertible.
27 pub const ZERO: Self = Self {
28 matrix2: $matrix::ZERO,
29 translation: $column::ZERO,
30 };
31
32 /// The identity transform.
33 ///
34 /// Multiplying a vector with this returns the same vector.
35 pub const IDENTITY: Self = Self {
36 matrix2: $matrix::IDENTITY,
37 translation: $column::ZERO,
38 };
39
40 /// All NAN:s.
41 pub const NAN: Self = Self {
42 matrix2: $matrix::NAN,
43 translation: $column::NAN,
44 };
45
46 /// Creates an affine transform from three column vectors.
47 #[inline(always)]
48 pub fn from_cols(x_axis: $column, y_axis: $column, z_axis: $column) -> Self {
49 Self {
50 matrix2: $matrix::from_cols(x_axis, y_axis),
51 translation: z_axis,
52 }
53 }
54
55 /// Creates an affine transform from a `[S; 6]` array stored in column major order.
56 /// If your data is stored in row major you will need to `transpose` the returned
57 /// matrix.
58 #[inline(always)]
59 pub fn from_cols_array(m: &[$t; 6]) -> Self {
60 Self {
61 matrix2: $matrix::from_cols_slice(&m[0..4]),
62 translation: $column::from_slice(&m[4..6]),
63 }
64 }
65
66 /// Creates a `[S; 6]` array storing data in column major order.
67 /// If you require data in row major order `transpose` the matrix first.
68 #[inline(always)]
69 pub fn to_cols_array(&self) -> [$t; 6] {
70 let x = &self.matrix2.x_axis;
71 let y = &self.matrix2.y_axis;
72 let z = &self.translation;
73 [x.x, x.y, y.x, y.y, z.x, z.y]
74 }
75
76 /// Creates an affine transform from a `[[S; 2]; 3]` 2D array stored in column major order.
77 /// If your data is in row major order you will need to `transpose` the returned
78 /// matrix.
79 #[inline(always)]
80 pub fn from_cols_array_2d(m: &[[$t; 2]; 3]) -> Self {
81 Self {
82 matrix2: $matrix::from_cols(m[0].into(), m[1].into()),
83 translation: m[2].into(),
84 }
85 }
86
87 /// Creates a `[[S; 2]; 3]` 2D array storing data in column major order.
88 /// If you require data in row major order `transpose` the matrix first.
89 #[inline(always)]
90 pub fn to_cols_array_2d(&self) -> [[$t; 2]; 3] {
91 [
92 self.matrix2.x_axis.into(),
93 self.matrix2.y_axis.into(),
94 self.translation.into(),
95 ]
96 }
97
98 /// Creates an affine transform from the first 6 values in `slice`.
99 ///
100 /// # Panics
101 ///
102 /// Panics if `slice` is less than 6 elements long.
103 #[inline(always)]
104 pub fn from_cols_slice(slice: &[$t]) -> Self {
105 Self {
106 matrix2: $matrix::from_cols_slice(&slice[0..4]),
107 translation: $column::from_slice(&slice[4..6]),
108 }
109 }
110
111 /// Writes the columns of `self` to the first 12 elements in `slice`.
112 ///
113 /// # Panics
114 ///
115 /// Panics if `slice` is less than 12 elements long.
116 #[inline(always)]
117 pub fn write_cols_to_slice(self, slice: &mut [$t]) {
118 self.matrix2.write_cols_to_slice(&mut slice[0..4]);
119 self.translation.write_to_slice(&mut slice[4..6]);
120 }
121
122 /// Creates an affine transform that changes scale.
123 /// Note that if any scale is zero the transform will be non-invertible.
124 #[inline(always)]
125 pub fn from_scale(scale: $vec2) -> Self {
126 Self {
127 matrix2: $matrix::from_diagonal(scale),
128 translation: $column::ZERO,
129 }
130 }
131
132 /// Creates an affine transform from the given rotation `angle`.
133 #[inline(always)]
134 pub fn from_angle(angle: $t) -> Self {
135 Self {
136 matrix2: $matrix::from_angle(angle),
137 translation: $column::ZERO,
138 }
139 }
140
141 /// Creates an affine transformation from the given 2D `translation`.
142 #[inline(always)]
143 pub fn from_translation(translation: $vec2) -> Self {
144 Self {
145 matrix2: $matrix::IDENTITY,
146 translation,
147 }
148 }
149
150 /// Creates an affine transform from a 2x2 matrix (expressing scale, shear and
151 /// rotation)
152 #[inline(always)]
153 pub fn from_mat2(matrix2: $mat2) -> Self {
154 Self {
155 matrix2,
156 translation: $column::ZERO,
157 }
158 }
159
160 /// Creates an affine transform from a 2x2 matrix (expressing scale, shear and rotation)
161 /// and a translation vector.
162 ///
163 /// Equivalent to `Affine2::from_translation(translation) * Affine2::from_mat2(mat2)`
164 #[inline(always)]
165 pub fn from_mat2_translation(matrix2: $mat2, translation: $vec2) -> Self {
166 Self {
167 matrix2,
168 translation,
169 }
170 }
171
172 /// Creates an affine transform from the given 2D `scale`, rotation `angle` (in
173 /// radians) and `translation`.
174 ///
175 /// Equivalent to `Affine2::from_translation(translation) *
176 /// Affine2::from_angle(angle) * Affine2::from_scale(scale)`
177 #[inline]
178 pub fn from_scale_angle_translation(
179 scale: $vec2,
180 angle: $t,
181 translation: $vec2,
182 ) -> Self {
183 let rotation = $matrix::from_angle(angle);
184 Self {
185 matrix2: $matrix::from_cols(
186 rotation.x_axis * scale.x,
187 rotation.y_axis * scale.y,
188 ),
189 translation,
190 }
191 }
192
193 /// Creates an affine transform from the given 2D rotation `angle` (in radians) and
194 /// `translation`.
195 ///
196 /// Equivalent to `Affine2::from_translation(translation) * Affine2::from_angle(angle)`
197 #[inline(always)]
198 pub fn from_angle_translation(angle: $t, translation: $vec2) -> Self {
199 Self {
200 matrix2: $matrix::from_angle(angle),
201 translation,
202 }
203 }
204
205 /// The given `Mat3` must be an affine transform,
206 #[inline]
207 pub fn from_mat3(m: $mat3) -> Self {
208 Self {
209 matrix2: $matrix::from_cols($vec2(m.x_axis.0.into()), $vec2(m.y_axis.0.into())),
210 translation: $vec2(m.z_axis.0.into()),
211 }
212 }
213
214 /// Transforms the given 2D point, applying shear, scale, rotation and translation.
215 #[inline(always)]
216 pub fn transform_point2(&self, other: $vec2) -> $vec2 {
217 self.matrix2 * other + self.translation
218 }
219
220 /// Transforms the given 2D vector, applying shear, scale and rotation (but NOT
221 /// translation).
222 ///
223 /// To also apply translation, use [`Self::transform_point2`] instead.
224 #[inline(always)]
225 pub fn transform_vector2(&self, other: $vec2) -> $vec2 {
226 self.matrix2 * other
227 }
228
229 /// Returns `true` if, and only if, all elements are finite.
230 ///
231 /// If any element is either `NaN`, positive or negative infinity, this will return
232 /// `false`.
233 #[inline]
234 pub fn is_finite(&self) -> bool {
235 self.matrix2.is_finite() && self.translation.is_finite()
236 }
237
238 /// Returns `true` if any elements are `NaN`.
239 #[inline]
240 pub fn is_nan(&self) -> bool {
241 self.matrix2.is_nan() || self.translation.is_nan()
242 }
243
244 /// Returns true if the absolute difference of all elements between `self` and `other`
245 /// is less than or equal to `max_abs_diff`.
246 ///
247 /// This can be used to compare if two 3x4 matrices contain similar elements. It works
248 /// best when comparing with a known value. The `max_abs_diff` that should be used used
249 /// depends on the values being compared against.
250 ///
251 /// For more see
252 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
253 #[inline]
254 pub fn abs_diff_eq(&self, other: Self, max_abs_diff: $t) -> bool {
255 self.matrix2.abs_diff_eq(&other.matrix2, max_abs_diff)
256 && self
257 .translation
258 .abs_diff_eq(other.translation, max_abs_diff)
259 }
260
261 /// Return the inverse of this transform.
262 ///
263 /// Note that if the transform is not invertible the result will be invalid.
264 #[must_use]
265 #[inline]
266 pub fn inverse(&self) -> Self {
267 let matrix2 = self.matrix2.inverse();
268 // transform negative translation by the 2x2 inverse:
269 let translation = -(matrix2 * self.translation);
270
271 Self {
272 matrix2,
273 translation,
274 }
275 }
276 }
277 };
278 }
279
280 macro_rules! impl_affine2_traits {
281 ($t:ty, $mat2:ident, $mat3:ident, $vec2:ident, $affine2:ident, $matrix:ident, $column:ident, $deref:ident) => {
282 impl Default for $affine2 {
283 #[inline(always)]
284 fn default() -> Self {
285 Self::IDENTITY
286 }
287 }
288
289 impl Deref for $affine2 {
290 type Target = $deref;
291 #[inline(always)]
292 fn deref(&self) -> &Self::Target {
293 unsafe { &*(self as *const Self as *const Self::Target) }
294 }
295 }
296
297 impl DerefMut for $affine2 {
298 #[inline(always)]
299 fn deref_mut(&mut self) -> &mut Self::Target {
300 unsafe { &mut *(self as *mut Self as *mut Self::Target) }
301 }
302 }
303
304 impl PartialEq for $affine2 {
305 #[inline]
306 fn eq(&self, other: &Self) -> bool {
307 self.matrix2.eq(&other.matrix2) && self.translation.eq(&other.translation)
308 }
309 }
310
311 #[cfg(not(target_arch = "spirv"))]
312 impl core::fmt::Debug for $affine2 {
313 fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
314 fmt.debug_struct(stringify!($affine2))
315 .field("matrix2", &self.matrix2)
316 .field("translation", &self.translation)
317 .finish()
318 }
319 }
320
321 #[cfg(not(target_arch = "spirv"))]
322 impl core::fmt::Display for $affine2 {
323 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
324 write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
325 }
326 }
327
328 impl From<$affine2> for $mat3 {
329 #[inline]
330 fn from(m: $affine2) -> $mat3 {
331 Self::from_cols(
332 m.matrix2.x_axis.extend(0.0),
333 m.matrix2.y_axis.extend(0.0),
334 m.translation.extend(1.0),
335 )
336 }
337 }
338
339 impl Mul for $affine2 {
340 type Output = $affine2;
341
342 #[inline(always)]
343 fn mul(self, other: $affine2) -> Self::Output {
344 Self {
345 matrix2: self.matrix2 * other.matrix2,
346 translation: self.matrix2 * other.translation + self.translation,
347 }
348 }
349 }
350
351 impl Mul<$affine2> for $t {
352 type Output = $affine2;
353 #[inline(always)]
354 fn mul(self, other: $affine2) -> Self::Output {
355 $affine2 {
356 matrix2: self * other.matrix2,
357 translation: self * other.translation,
358 }
359 }
360 }
361
362 impl Mul<$t> for $affine2 {
363 type Output = Self;
364 #[inline(always)]
365 fn mul(self, other: $t) -> Self::Output {
366 Self {
367 matrix2: self.matrix2 * other,
368 translation: self.translation * other,
369 }
370 }
371 }
372
373 impl Add<$affine2> for $affine2 {
374 type Output = Self;
375 #[inline(always)]
376 fn add(self, other: Self) -> Self::Output {
377 Self {
378 matrix2: self.matrix2 + other.matrix2,
379 translation: self.translation + other.translation,
380 }
381 }
382 }
383
384 impl Sub<$affine2> for $affine2 {
385 type Output = Self;
386 #[inline(always)]
387 fn sub(self, other: Self) -> Self::Output {
388 Self {
389 matrix2: self.matrix2 - other.matrix2,
390 translation: self.translation - other.translation,
391 }
392 }
393 }
394
395 impl Mul<$mat3> for $affine2 {
396 type Output = $mat3;
397
398 #[inline(always)]
399 fn mul(self, other: $mat3) -> Self::Output {
400 $mat3::from(self) * other
401 }
402 }
403
404 impl Mul<$affine2> for $mat3 {
405 type Output = $mat3;
406
407 #[inline(always)]
408 fn mul(self, other: $affine2) -> Self::Output {
409 self * $mat3::from(other)
410 }
411 }
412
413 impl<'a> core::iter::Product<&'a Self> for $affine2 {
414 fn product<I>(iter: I) -> Self
415 where
416 I: Iterator<Item = &'a Self>,
417 {
418 iter.fold(Self::IDENTITY, |a, &b| a * b)
419 }
420 }
421 };
422 }
423
424 type TransformF32 = Mat2;
425 type TranslateF32 = Vec2;
426 type DerefTargetF32 = Columns3<crate::Vec2>;
427
428 define_affine2_struct!(Affine2, TransformF32, TranslateF32);
429 impl_affine2_methods!(f32, Mat2, Mat3, Vec2, Affine2, TransformF32, TranslateF32);
430 impl_affine2_traits!(
431 f32,
432 Mat2,
433 Mat3,
434 Vec2,
435 Affine2,
436 TransformF32,
437 TranslateF32,
438 DerefTargetF32
439 );
440
441 impl From<Affine2> for Mat3A {
442 #[inline]
443 fn from(m: Affine2) -> Mat3A {
444 Self::from_cols(
445 Vec3A::from((m.matrix2.x_axis, 0.0)),
446 Vec3A::from((m.matrix2.y_axis, 0.0)),
447 Vec3A::from((m.translation, 1.0)),
448 )
449 }
450 }
451
452 impl Mul<Mat3A> for Affine2 {
453 type Output = Mat3A;
454
455 #[inline(always)]
456 fn mul(self, other: Mat3A) -> Self::Output {
457 Mat3A::from(self) * other
458 }
459 }
460
461 impl Mul<Affine2> for Mat3A {
462 type Output = Mat3A;
463
464 #[inline(always)]
465 fn mul(self, other: Affine2) -> Self::Output {
466 self * Mat3A::from(other)
467 }
468 }
469
470 type TransformF64 = DMat2;
471 type TranslateF64 = DVec2;
472 type DerefTargetF64 = Columns3<DVec2>;
473
474 define_affine2_struct!(DAffine2, TransformF64, TranslateF64);
475 impl_affine2_methods!(
476 f64,
477 DMat2,
478 DMat3,
479 DVec2,
480 DAffine2,
481 TransformF64,
482 TranslateF64
483 );
484 impl_affine2_traits!(
485 f64,
486 DMat2,
487 DMat3,
488 DVec2,
489 DAffine2,
490 TransformF64,
491 TranslateF64,
492 DerefTargetF64
493 );
494
495 #[cfg(all(
496 not(feature = "cuda"),
497 any(feature = "scalar-math", target_arch = "spirv")
498 ))]
499 mod const_test_affine2 {
500 const_assert_eq!(
501 core::mem::align_of::<super::Vec2>(),
502 core::mem::align_of::<super::Affine2>()
503 );
504 const_assert_eq!(24, core::mem::size_of::<super::Affine2>());
505 }
506
507 #[cfg(not(any(feature = "scalar-math", target_arch = "spirv")))]
508 mod const_test_affine2 {
509 const_assert_eq!(16, core::mem::align_of::<super::Affine2>());
510 const_assert_eq!(32, core::mem::size_of::<super::Affine2>());
511 }
512
513 mod const_test_daffine2 {
514 const_assert_eq!(
515 core::mem::align_of::<super::DVec2>(),
516 core::mem::align_of::<super::DAffine2>()
517 );
518 const_assert_eq!(48, core::mem::size_of::<super::DAffine2>());
519 }