Debian Codegraph @

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