Debian Codegraph @

debian/missing-sources/glam-0.17.3/src/quat.rs

raw symbols SCIP
1 use crate::core::traits::{
2 quaternion::Quaternion,
3 vector::{FloatVector4, MaskVector4, Vector, Vector4, Vector4Const},
4 };
5 use crate::euler::{EulerFromQuaternion, EulerRot, EulerToQuaternion};
6 use crate::{DMat3, DMat4, DVec3, DVec4};
7 use crate::{Mat3, Mat4, Vec3, Vec3A, Vec4};
8
9 #[cfg(not(feature = "std"))]
10 use num_traits::Float;
11
12 #[cfg(all(
13 target_arch = "x86",
14 target_feature = "sse2",
15 not(feature = "scalar-math")
16 ))]
17 use core::arch::x86::*;
18 #[cfg(all(
19 target_arch = "x86_64",
20 target_feature = "sse2",
21 not(feature = "scalar-math")
22 ))]
23 use core::arch::x86_64::*;
24
25 #[cfg(not(target_arch = "spirv"))]
26 use core::fmt;
27 use core::ops::{Add, Deref, Div, Mul, MulAssign, Neg, Sub};
28
29 #[cfg(feature = "std")]
30 use std::iter::{Product, Sum};
31
32 macro_rules! impl_quat_methods {
33 ($t:ident, $quat:ident, $vec3:ident, $vec4:ident, $mat3:ident, $mat4:ident, $inner:ident) => {
34 /// The identity quaternion. Corresponds to no rotation.
35 pub const IDENTITY: Self = Self($inner::W);
36
37 /// Creates a new rotation quaternion.
38 ///
39 /// This should generally not be called manually unless you know what you are doing.
40 /// Use one of the other constructors instead such as `identity` or `from_axis_angle`.
41 ///
42 /// `from_xyzw` is mostly used by unit tests and `serde` deserialization.
43 ///
44 /// # Preconditions
45 ///
46 /// This function does not check if the input is normalized, it is up to the user to
47 /// provide normalized input or to normalized the resulting quaternion.
48 #[inline(always)]
49 pub fn from_xyzw(x: $t, y: $t, z: $t, w: $t) -> Self {
50 Self(Vector4::new(x, y, z, w))
51 }
52
53 /// Creates a rotation quaternion from an array.
54 ///
55 /// # Preconditions
56 ///
57 /// This function does not check if the input is normalized, it is up to the user to
58 /// provide normalized input or to normalized the resulting quaternion.
59 #[inline(always)]
60 pub fn from_array(a: [$t; 4]) -> Self {
61 let q = Vector4::from_array(a);
62 Self(q)
63 }
64
65 /// Creates a new rotation quaternion from a 4D vector.
66 ///
67 /// # Preconditions
68 ///
69 /// This function does not check if the input is normalized, it is up to the user to
70 /// provide normalized input or to normalized the resulting quaternion.
71 #[inline(always)]
72 pub fn from_vec4(v: $vec4) -> Self {
73 Self(v.0)
74 }
75
76 /// Creates a rotation quaternion from a slice.
77 ///
78 /// # Preconditions
79 ///
80 /// This function does not check if the input is normalized, it is up to the user to
81 /// provide normalized input or to normalized the resulting quaternion.
82 ///
83 /// # Panics
84 ///
85 /// Panics if `slice` length is less than 4.
86 #[inline(always)]
87 pub fn from_slice(slice: &[$t]) -> Self {
88 #[allow(clippy::let_and_return)]
89 let q = Vector4::from_slice_unaligned(slice);
90 Self(q)
91 }
92
93 /// Writes the quaternion to an unaligned slice.
94 ///
95 /// # Panics
96 ///
97 /// Panics if `slice` length is less than 4.
98 #[inline(always)]
99 pub fn write_to_slice(self, slice: &mut [$t]) {
100 Vector4::write_to_slice_unaligned(self.0, slice)
101 }
102
103 /// Create a quaternion for a normalized rotation `axis` and `angle` (in radians).
104 /// The axis must be normalized (unit-length).
105 ///
106 /// # Panics
107 ///
108 /// Will panic if `axis` is not normalized when `glam_assert` is enabled.
109 #[inline(always)]
110 pub fn from_axis_angle(axis: $vec3, angle: $t) -> Self {
111 Self($inner::from_axis_angle(axis.0, angle))
112 }
113
114 /// Create a quaternion that rotates `v.length()` radians around `v.normalize()`.
115 ///
116 /// `from_scaled_axis(Vec3::ZERO)` results in the identity quaternion.
117 #[inline(always)]
118 pub fn from_scaled_axis(v: $vec3) -> Self {
119 // Self($inner::from_scaled_axis(v.0))
120 let length = v.length();
121 if length == 0.0 {
122 Self::IDENTITY
123 } else {
124 Self::from_axis_angle(v / length, length)
125 }
126 }
127
128 /// Creates a quaternion from the `angle` (in radians) around the x axis.
129 #[inline(always)]
130 pub fn from_rotation_x(angle: $t) -> Self {
131 Self($inner::from_rotation_x(angle))
132 }
133
134 /// Creates a quaternion from the `angle` (in radians) around the y axis.
135 #[inline(always)]
136 pub fn from_rotation_y(angle: $t) -> Self {
137 Self($inner::from_rotation_y(angle))
138 }
139
140 /// Creates a quaternion from the `angle` (in radians) around the z axis.
141 #[inline(always)]
142 pub fn from_rotation_z(angle: $t) -> Self {
143 Self($inner::from_rotation_z(angle))
144 }
145
146 #[inline(always)]
147 /// Creates a quaternion from the given euler rotation sequence and the angles (in radians).
148 pub fn from_euler(euler: EulerRot, a: $t, b: $t, c: $t) -> Self {
149 euler.new_quat(a, b, c)
150 }
151
152 /// Creates a quaternion from a 3x3 rotation matrix.
153 #[inline]
154 pub fn from_mat3(mat: &$mat3) -> Self {
155 Self(Quaternion::from_rotation_axes(
156 mat.x_axis.0,
157 mat.y_axis.0,
158 mat.z_axis.0,
159 ))
160 }
161
162 /// Creates a quaternion from a 3x3 rotation matrix inside a homogeneous 4x4 matrix.
163 #[inline]
164 pub fn from_mat4(mat: &$mat4) -> Self {
165 Self(Quaternion::from_rotation_axes(
166 mat.x_axis.0.into(),
167 mat.y_axis.0.into(),
168 mat.z_axis.0.into(),
169 ))
170 }
171
172 /// Gets the minimal rotation for transforming `from` to `to`. The rotation is in the
173 /// plane spanned by the two vectors. Will rotate at most 180 degrees.
174 ///
175 /// The input vectors must be normalized (unit-length).
176 ///
177 /// `from_rotation_arc(from, to) * from ≈ to`.
178 ///
179 /// For near-singular cases (from≈to and from≈-to) the current implementation
180 /// is only accurate to about 0.001 (for `f32`).
181 ///
182 /// # Panics
183 ///
184 /// Will panic if `from` or `to` are not normalized when `glam_assert` is enabled.
185 #[cfg(feature = "std")]
186 pub fn from_rotation_arc(from: $vec3, to: $vec3) -> Self {
187 glam_assert!(from.is_normalized());
188 glam_assert!(to.is_normalized());
189
190 let one_minus_eps = 1.0 - 2.0 * core::$t::EPSILON;
191 let dot = from.dot(to);
192 if dot > one_minus_eps {
193 // 0° singulary: from ≈ to
194 Self::IDENTITY
195 } else if dot < -one_minus_eps {
196 // 180° singulary: from ≈ -to
197 let pi = core::$t::consts::PI; // half a turn = 𝛕/2 = 180°
198 Self::from_axis_angle(from.any_orthonormal_vector(), pi)
199 } else {
200 let c = from.cross(to);
201 Self::from_xyzw(c.x, c.y, c.z, 1.0 + dot).normalize()
202 }
203 }
204
205 /// Gets the minimal rotation for transforming `from` to either `to` or `-to`. This means
206 /// that the resulting quaternion will rotate `from` so that it is colinear with `to`.
207 ///
208 /// The rotation is in the plane spanned by the two vectors. Will rotate at most 90
209 /// degrees.
210 ///
211 /// The input vectors must be normalized (unit-length).
212 ///
213 /// `to.dot(from_rotation_arc_colinear(from, to) * from).abs() ≈ 1`.
214 ///
215 /// # Panics
216 ///
217 /// Will panic if `from` or `to` are not normalized when `glam_assert` is enabled.
218 #[cfg(feature = "std")]
219 pub fn from_rotation_arc_colinear(from: $vec3, to: $vec3) -> Self {
220 if from.dot(to) < 0.0 {
221 Self::from_rotation_arc(from, -to)
222 } else {
223 Self::from_rotation_arc(from, to)
224 }
225 }
226
227 /// Returns the rotation axis and angle (in radians) of `self`.
228 #[inline(always)]
229 pub fn to_axis_angle(self) -> ($vec3, $t) {
230 let (axis, angle) = self.0.to_axis_angle();
231 ($vec3(axis), angle)
232 }
233
234 /// Returns the rotation axis scaled by the rotation in radians.
235 #[inline(always)]
236 pub fn to_scaled_axis(self) -> $vec3 {
237 let (axis, angle) = self.0.to_axis_angle();
238 $vec3(axis) * angle
239 }
240
241 /// Returns the rotation angles for the given euler rotation sequence.
242 #[inline(always)]
243 pub fn to_euler(self, euler: EulerRot) -> ($t, $t, $t) {
244 euler.convert_quat(self)
245 }
246
247 /// Returns the quaternion conjugate of `self`. For a unit quaternion the
248 /// conjugate is also the inverse.
249 #[must_use]
250 #[inline(always)]
251 pub fn conjugate(self) -> Self {
252 Self(self.0.conjugate())
253 }
254
255 /// Returns the inverse of a normalized quaternion.
256 ///
257 /// Typically quaternion inverse returns the conjugate of a normalized quaternion.
258 /// Because `self` is assumed to already be unit length this method *does not* normalize
259 /// before returning the conjugate.
260 ///
261 /// # Panics
262 ///
263 /// Will panic if `self` is not normalized when `glam_assert` is enabled.
264 #[must_use]
265 #[inline(always)]
266 pub fn inverse(self) -> Self {
267 glam_assert!(self.is_normalized());
268 self.conjugate()
269 }
270
271 /// Computes the dot product of `self` and `other`. The dot product is
272 /// equal to the the cosine of the angle between two quaternion rotations.
273 #[inline(always)]
274 pub fn dot(self, other: Self) -> $t {
275 Vector4::dot(self.0, other.0)
276 }
277
278 /// Computes the length of `self`.
279 #[cfg_attr(docsrs, doc(alias = "magnitude"))]
280 #[inline(always)]
281 pub fn length(self) -> $t {
282 FloatVector4::length(self.0)
283 }
284
285 /// Computes the squared length of `self`.
286 ///
287 /// This is generally faster than `length()` as it avoids a square
288 /// root operation.
289 #[cfg_attr(docsrs, doc(alias = "magnitude2"))]
290 #[inline(always)]
291 pub fn length_squared(self) -> $t {
292 FloatVector4::length_squared(self.0)
293 }
294
295 /// Computes `1.0 / length()`.
296 ///
297 /// For valid results, `self` must _not_ be of length zero.
298 #[inline(always)]
299 pub fn length_recip(self) -> $t {
300 FloatVector4::length_recip(self.0)
301 }
302
303 /// Returns `self` normalized to length 1.0.
304 ///
305 /// For valid results, `self` must _not_ be of length zero.
306 ///
307 /// Panics
308 ///
309 /// Will panic if `self` is zero length when `glam_assert` is enabled.
310 #[must_use]
311 #[inline(always)]
312 pub fn normalize(self) -> Self {
313 Self(FloatVector4::normalize(self.0))
314 }
315
316 /// Returns `true` if, and only if, all elements are finite.
317 /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
318 #[inline(always)]
319 pub fn is_finite(self) -> bool {
320 FloatVector4::is_finite(self.0)
321 }
322
323 #[inline(always)]
324 pub fn is_nan(self) -> bool {
325 FloatVector4::is_nan(self.0)
326 }
327
328 /// Returns whether `self` of length `1.0` or not.
329 ///
330 /// Uses a precision threshold of `1e-6`.
331 #[inline(always)]
332 pub fn is_normalized(self) -> bool {
333 FloatVector4::is_normalized(self.0)
334 }
335
336 #[inline(always)]
337 pub fn is_near_identity(self) -> bool {
338 self.0.is_near_identity()
339 }
340
341 /// Returns the angle (in radians) for the minimal rotation
342 /// for transforming this quaternion into another.
343 ///
344 /// Both quaternions must be normalized.
345 ///
346 /// # Panics
347 ///
348 /// Will panic if `self` or `other` are not normalized when `glam_assert` is enabled.
349 pub fn angle_between(self, other: Self) -> $t {
350 glam_assert!(self.is_normalized() && other.is_normalized());
351 use crate::core::traits::scalar::FloatEx;
352 self.dot(other).abs().acos_approx() * 2.0
353 }
354
355 /// Returns true if the absolute difference of all elements between `self` and `other`
356 /// is less than or equal to `max_abs_diff`.
357 ///
358 /// This can be used to compare if two quaternions contain similar elements. It works
359 /// best when comparing with a known value. The `max_abs_diff` that should be used used
360 /// depends on the values being compared against.
361 ///
362 /// For more see
363 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
364 #[inline(always)]
365 pub fn abs_diff_eq(self, other: Self, max_abs_diff: $t) -> bool {
366 FloatVector4::abs_diff_eq(self.0, other.0, max_abs_diff)
367 }
368
369 /// Performs a linear interpolation between `self` and `other` based on
370 /// the value `s`.
371 ///
372 /// When `s` is `0.0`, the result will be equal to `self`. When `s`
373 /// is `1.0`, the result will be equal to `other`.
374 ///
375 /// # Panics
376 ///
377 /// Will panic if `self` or `end` are not normalized when `glam_assert` is enabled.
378 #[inline(always)]
379 #[cfg_attr(docsrs, doc(alias = "mix"))]
380 pub fn lerp(self, end: Self, s: $t) -> Self {
381 Self(self.0.lerp(end.0, s))
382 }
383
384 /// Performs a spherical linear interpolation between `self` and `end`
385 /// based on the value `s`.
386 ///
387 /// When `s` is `0.0`, the result will be equal to `self`. When `s`
388 /// is `1.0`, the result will be equal to `end`.
389 ///
390 /// Note that a rotation can be represented by two quaternions: `q` and
391 /// `-q`. The slerp path between `q` and `end` will be different from the
392 /// path between `-q` and `end`. One path will take the long way around and
393 /// one will take the short way. In order to correct for this, the `dot`
394 /// product between `self` and `end` should be positive. If the `dot`
395 /// product is negative, slerp between `-self` and `end`.
396 ///
397 /// # Panics
398 ///
399 /// Will panic if `self` or `end` are not normalized when `glam_assert` is enabled.
400 #[inline(always)]
401 pub fn slerp(self, end: Self, s: $t) -> Self {
402 Self(self.0.slerp(end.0, s))
403 }
404
405 /// Multiplies a quaternion and a 3D vector, returning the rotated vector.
406 ///
407 /// # Panics
408 ///
409 /// Will panic if `self` is not normalized when `glam_assert` is enabled.
410 #[inline(always)]
411 pub fn mul_vec3(self, other: $vec3) -> $vec3 {
412 $vec3(self.0.mul_vector3(other.0))
413 }
414
415 /// Multiplies two quaternions. If they each represent a rotation, the result will
416 /// represent the combined rotation.
417 ///
418 /// Note that due to floating point rounding the result may not be perfectly normalized.
419 ///
420 /// # Panics
421 ///
422 /// Will panic if `self` or `other` are not normalized when `glam_assert` is enabled.
423 #[inline(always)]
424 pub fn mul_quat(self, other: Self) -> Self {
425 Self(self.0.mul_quaternion(other.0))
426 }
427 };
428 }
429
430 macro_rules! impl_quat_traits {
431 ($t:ty, $new:ident, $quat:ident, $vec3:ident, $vec4:ident, $inner:ident) => {
432 /// Creates a quaternion from `x`, `y`, `z` and `w` values.
433 ///
434 /// This should generally not be called manually unless you know what you are doing. Use
435 /// one of the other constructors instead such as `identity` or `from_axis_angle`.
436 #[inline]
437 pub fn $new(x: $t, y: $t, z: $t, w: $t) -> $quat {
438 $quat::from_xyzw(x, y, z, w)
439 }
440
441 #[cfg(not(target_arch = "spirv"))]
442 impl fmt::Debug for $quat {
443 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
444 fmt.debug_tuple(stringify!($quat))
445 .field(&self.x)
446 .field(&self.y)
447 .field(&self.z)
448 .field(&self.w)
449 .finish()
450 }
451 }
452
453 #[cfg(not(target_arch = "spirv"))]
454 impl fmt::Display for $quat {
455 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
456 write!(fmt, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
457 }
458 }
459
460 impl Add<$quat> for $quat {
461 type Output = Self;
462 /// Adds two quaternions.
463 ///
464 /// The sum is not guaranteed to be normalized.
465 ///
466 /// Note that addition is not the same as combining the rotations represented by the
467 /// two quaternions! That corresponds to multiplication.
468 #[inline]
469 fn add(self, other: Self) -> Self {
470 Self(self.0.add(other.0))
471 }
472 }
473
474 impl Sub<$quat> for $quat {
475 type Output = Self;
476 /// Subtracts the other quaternion from self.
477 ///
478 /// The difference is not guaranteed to be normalized.
479 #[inline]
480 fn sub(self, other: Self) -> Self {
481 Self(self.0.sub(other.0))
482 }
483 }
484
485 impl Mul<$t> for $quat {
486 type Output = Self;
487 /// Multiplies a quaternion by a scalar value.
488 ///
489 /// The product is not guaranteed to be normalized.
490 #[inline]
491 fn mul(self, other: $t) -> Self {
492 Self(self.0.scale(other))
493 }
494 }
495
496 impl Div<$t> for $quat {
497 type Output = Self;
498 /// Divides a quaternion by a scalar value.
499 /// The quotient is not guaranteed to be normalized.
500 #[inline]
501 fn div(self, other: $t) -> Self {
502 Self(self.0.scale(other.recip()))
503 }
504 }
505
506 impl Mul<$quat> for $quat {
507 type Output = Self;
508 /// Multiplies two quaternions. If they each represent a rotation, the result will
509 /// represent the combined rotation.
510 ///
511 /// Note that due to floating point rounding the result may not be perfectly
512 /// normalized.
513 ///
514 /// # Panics
515 ///
516 /// Will panic if `self` or `other` are not normalized when `glam_assert` is enabled.
517 #[inline]
518 fn mul(self, other: Self) -> Self {
519 Self(self.0.mul_quaternion(other.0))
520 }
521 }
522
523 impl MulAssign<$quat> for $quat {
524 /// Multiplies two quaternions. If they each represent a rotation, the result will
525 /// represent the combined rotation.
526 ///
527 /// Note that due to floating point rounding the result may not be perfectly
528 /// normalized.
529 ///
530 /// # Panics
531 ///
532 /// Will panic if `self` or `other` are not normalized when `glam_assert` is enabled.
533 #[inline]
534 fn mul_assign(&mut self, other: Self) {
535 self.0 = self.0.mul_quaternion(other.0);
536 }
537 }
538
539 impl Mul<$vec3> for $quat {
540 type Output = $vec3;
541 /// Multiplies a quaternion and a 3D vector, returning the rotated vector.
542 ///
543 /// # Panics
544 ///
545 /// Will panic if `self` is not normalized when `glam_assert` is enabled.
546 #[inline]
547 fn mul(self, other: $vec3) -> Self::Output {
548 $vec3(self.0.mul_vector3(other.0))
549 }
550 }
551
552 impl Neg for $quat {
553 type Output = Self;
554 #[inline]
555 fn neg(self) -> Self {
556 Self(self.0.scale(-1.0))
557 }
558 }
559
560 impl Default for $quat {
561 #[inline]
562 fn default() -> Self {
563 Self::IDENTITY
564 }
565 }
566
567 impl PartialEq for $quat {
568 #[inline]
569 fn eq(&self, other: &Self) -> bool {
570 MaskVector4::all(self.0.cmpeq(other.0))
571 }
572 }
573
574 #[cfg(not(target_arch = "spriv"))]
575 impl AsRef<[$t; 4]> for $quat {
576 #[inline(always)]
577 fn as_ref(&self) -> &[$t; 4] {
578 unsafe { &*(self as *const Self as *const [$t; 4]) }
579 }
580 }
581
582 #[cfg(not(target_arch = "spriv"))]
583 impl AsMut<[$t; 4]> for $quat {
584 #[inline(always)]
585 fn as_mut(&mut self) -> &mut [$t; 4] {
586 unsafe { &mut *(self as *mut Self as *mut [$t; 4]) }
587 }
588 }
589
590 impl From<$quat> for $vec4 {
591 #[inline(always)]
592 fn from(q: $quat) -> Self {
593 $vec4(q.0)
594 }
595 }
596
597 impl From<$quat> for ($t, $t, $t, $t) {
598 #[inline(always)]
599 fn from(q: $quat) -> Self {
600 Vector4::into_tuple(q.0)
601 }
602 }
603
604 impl From<$quat> for [$t; 4] {
605 #[inline(always)]
606 fn from(q: $quat) -> Self {
607 Vector4::into_array(q.0)
608 }
609 }
610
611 impl From<$quat> for $inner {
612 // TODO: write test
613 #[inline(always)]
614 fn from(q: $quat) -> Self {
615 q.0
616 }
617 }
618
619 impl Deref for $quat {
620 type Target = crate::XYZW<$t>;
621 #[inline(always)]
622 fn deref(&self) -> &Self::Target {
623 self.0.as_ref_xyzw()
624 }
625 }
626
627 #[cfg(feature = "std")]
628 impl<'a> Sum<&'a Self> for $quat {
629 fn sum<I>(iter: I) -> Self
630 where
631 I: Iterator<Item = &'a Self>,
632 {
633 use crate::core::traits::vector::VectorConst;
634 iter.fold(Self($inner::ZERO), |a, &b| Self::add(a, b))
635 }
636 }
637
638 #[cfg(feature = "std")]
639 impl<'a> Product<&'a Self> for $quat {
640 fn product<I>(iter: I) -> Self
641 where
642 I: Iterator<Item = &'a Self>,
643 {
644 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
645 }
646 }
647 };
648 }
649
650 #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))]
651 type InnerF32 = __m128;
652
653 #[cfg(any(not(target_feature = "sse2"), feature = "scalar-math"))]
654 type InnerF32 = crate::XYZW<f32>;
655
656 /// A quaternion representing an orientation.
657 ///
658 /// This quaternion is intended to be of unit length but may denormalize due to
659 /// floating point "error creep" which can occur when successive quaternion
660 /// operations are applied.
661 ///
662 /// This type is 16 byte aligned.
663 #[derive(Clone, Copy)]
664 #[cfg_attr(
665 not(any(feature = "scalar-math", target_arch = "spriv")),
666 repr(align(16))
667 )]
668 #[cfg_attr(any(feature = "scalar-math", target_arch = "spriv"), repr(transparent))]
669 pub struct Quat(pub(crate) InnerF32);
670
671 impl Quat {
672 impl_quat_methods!(f32, Quat, Vec3, Vec4, Mat3, Mat4, InnerF32);
673
674 /// Multiplies a quaternion and a 3D vector, returning the rotated vector.
675 #[inline(always)]
676 pub fn mul_vec3a(self, other: Vec3A) -> Vec3A {
677 #[allow(clippy::useless_conversion)]
678 Vec3A(self.0.mul_float4_as_vector3(other.0.into()).into())
679 }
680
681 #[inline(always)]
682 pub fn as_f64(self) -> DQuat {
683 DQuat::from_xyzw(self.x as f64, self.y as f64, self.z as f64, self.w as f64)
684 }
685
686 /// Creates a quaternion from a 3x3 rotation matrix inside a 3D affine transform.
687 #[inline]
688 pub fn from_affine3(mat: &crate::Affine3A) -> Self {
689 Self(Quaternion::from_rotation_axes(
690 mat.x_axis.0.into(),
691 mat.y_axis.0.into(),
692 mat.z_axis.0.into(),
693 ))
694 }
695 }
696 impl_quat_traits!(f32, quat, Quat, Vec3, Vec4, InnerF32);
697
698 impl Mul<Vec3A> for Quat {
699 type Output = Vec3A;
700 #[inline(always)]
701 fn mul(self, other: Vec3A) -> Self::Output {
702 self.mul_vec3a(other)
703 }
704 }
705
706 type InnerF64 = crate::XYZW<f64>;
707
708 /// A quaternion representing an orientation.
709 ///
710 /// This quaternion is intended to be of unit length but may denormalize due to
711 /// floating point "error creep" which can occur when successive quaternion
712 /// operations are applied.
713 #[derive(Clone, Copy)]
714 #[repr(transparent)]
715 pub struct DQuat(pub(crate) InnerF64);
716
717 impl DQuat {
718 impl_quat_methods!(f64, DQuat, DVec3, DVec4, DMat3, DMat4, InnerF64);
719
720 #[inline(always)]
721 pub fn as_f32(self) -> Quat {
722 Quat::from_xyzw(self.x as f32, self.y as f32, self.z as f32, self.w as f32)
723 }
724
725 /// Creates a quaternion from a 3x3 rotation matrix inside a 3D affine transform.
726 #[inline]
727 pub fn from_affine3(mat: &crate::DAffine3) -> Self {
728 Self(Quaternion::from_rotation_axes(
729 mat.x_axis.0,
730 mat.y_axis.0,
731 mat.z_axis.0,
732 ))
733 }
734 }
735 impl_quat_traits!(f64, dquat, DQuat, DVec3, DVec4, InnerF64);
736
737 #[cfg(any(feature = "scalar-math", target_arch = "spriv"))]
738 mod const_test_quat {
739 const_assert_eq!(
740 core::mem::align_of::<f32>(),
741 core::mem::align_of::<super::Quat>()
742 );
743 const_assert_eq!(16, core::mem::size_of::<super::Quat>());
744 }
745
746 #[cfg(not(any(feature = "scalar-math", target_arch = "spriv")))]
747 mod const_test_quat {
748 const_assert_eq!(16, core::mem::align_of::<super::Quat>());
749 const_assert_eq!(16, core::mem::size_of::<super::Quat>());
750 }
751
752 mod const_test_dquat {
753 const_assert_eq!(
754 core::mem::align_of::<f64>(),
755 core::mem::align_of::<super::DQuat>()
756 );
757 const_assert_eq!(32, core::mem::size_of::<super::DQuat>());
758 }