Debian Codegraph @

debian/missing-sources/glam-0.20.5/src/core/scalar/quaternion.rs

raw symbols SCIP
1 use crate::core::{
2 storage::{XYZ, XYZW},
3 traits::{quaternion::Quaternion, scalar::*, vector::*},
4 };
5
6 impl<T: FloatEx> Quaternion<T> for XYZW<T> {
7 // fallback
8 type SIMDVector3 = XYZ<T>;
9
10 #[inline(always)]
11 fn conjugate(self) -> Self {
12 Self::new(-self.x, -self.y, -self.z, self.w)
13 }
14
15 #[inline]
16 fn lerp(self, end: Self, s: T) -> Self {
17 glam_assert!(FloatVector4::is_normalized(self));
18 glam_assert!(FloatVector4::is_normalized(end));
19
20 let start = self;
21 let end = end;
22 let dot = start.dot(end);
23 let bias = if dot >= T::ZERO { T::ONE } else { T::NEG_ONE };
24 let interpolated = start.add(end.mul_scalar(bias).sub(start).mul_scalar(s));
25 interpolated.normalize()
26 }
27
28 #[inline]
29 fn slerp(self, mut end: Self, s: T) -> Self {
30 // http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/
31
32 glam_assert!(FloatVector4::is_normalized(self));
33 glam_assert!(FloatVector4::is_normalized(end));
34
35 let mut dot = self.dot(end);
36
37 // Note that a rotation can be represented by two quaternions: `q` and
38 // `-q`. The slerp path between `q` and `end` will be different from the
39 // path between `-q` and `end`. One path will take the long way around and
40 // one will take the short way. In order to correct for this, the `dot`
41 // product between `self` and `end` should be positive. If the `dot`
42 // product is negative, slerp between `self` and `-end`.
43 if dot < T::ZERO {
44 end = end.mul_scalar(T::NEG_ONE);
45 dot = -dot;
46 }
47
48 if dot > T::from_f32(0.9995) {
49 // assumes lerp returns a normalized quaternion
50 self.lerp(end, s)
51 } else {
52 // assumes scalar_acos clamps the input to [-1.0, 1.0]
53 let theta = dot.acos_approx();
54 let scale1 = (theta * (T::ONE - s)).sin();
55 let scale2 = (theta * s).sin();
56 let theta_sin = theta.sin();
57
58 self.mul_scalar(scale1)
59 .add(end.mul_scalar(scale2))
60 .mul_scalar(theta_sin.recip())
61 }
62 }
63
64 #[inline]
65 fn mul_vector3(self, other: XYZ<T>) -> XYZ<T> {
66 glam_assert!(FloatVector4::is_normalized(self));
67 let w = self.w;
68 let b = XYZ {
69 x: self.x,
70 y: self.y,
71 z: self.z,
72 };
73 let b2 = b.dot(b);
74 other
75 .mul_scalar(w * w - b2)
76 .add(b.mul_scalar(other.dot(b) * T::TWO))
77 .add(b.cross(other).mul_scalar(w * T::TWO))
78 }
79
80 #[inline]
81 fn mul_quaternion(self, other: Self) -> Self {
82 glam_assert!(FloatVector4::is_normalized(self));
83 glam_assert!(FloatVector4::is_normalized(other));
84 let (x0, y0, z0, w0) = self.into_tuple();
85 let (x1, y1, z1, w1) = other.into_tuple();
86 Self::new(
87 w0 * x1 + x0 * w1 + y0 * z1 - z0 * y1,
88 w0 * y1 - x0 * z1 + y0 * w1 + z0 * x1,
89 w0 * z1 + x0 * y1 - y0 * x1 + z0 * w1,
90 w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1,
91 )
92 }
93
94 #[inline(always)]
95 fn mul_float4_as_vector3(self, other: XYZ<T>) -> XYZ<T> {
96 self.mul_vector3(other)
97 }
98 }