Debian Codegraph @

debian/missing-sources/glam-0.23.0/src/f32/affine3a.rs

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