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