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