Debian Codegraph @

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