Debian Codegraph @

debian/missing-sources/glam-0.19.0/src/core/traits/quaternion.rs

raw symbols SCIP
1 use crate::core::{
2 storage::XYZ,
3 traits::{
4 scalar::{FloatEx, NumEx},
5 vector::*,
6 },
7 };
8
9 pub trait Quaternion<T: FloatEx>: FloatVector4<T> {
10 type SIMDVector3;
11
12 #[inline]
13 fn from_axis_angle(axis: XYZ<T>, angle: T) -> Self {
14 glam_assert!(FloatVector3::is_normalized(axis));
15 let (s, c) = (angle * T::HALF).sin_cos();
16 let v = axis.mul_scalar(s);
17 Self::new(v.x, v.y, v.z, c)
18 }
19
20 #[inline]
21 fn from_rotation_x(angle: T) -> Self {
22 let (s, c) = (angle * T::HALF).sin_cos();
23 Self::new(s, T::ZERO, T::ZERO, c)
24 }
25
26 #[inline]
27 fn from_rotation_y(angle: T) -> Self {
28 let (s, c) = (angle * T::HALF).sin_cos();
29 Self::new(T::ZERO, s, T::ZERO, c)
30 }
31
32 #[inline]
33 fn from_rotation_z(angle: T) -> Self {
34 let (s, c) = (angle * T::HALF).sin_cos();
35 Self::new(T::ZERO, T::ZERO, s, c)
36 }
37
38 /// From the columns of a 3x3 rotation matrix.
39 #[inline]
40 fn from_rotation_axes(x_axis: XYZ<T>, y_axis: XYZ<T>, z_axis: XYZ<T>) -> Self {
41 // Based on https://github.com/microsoft/DirectXMath `XM$quaternionRotationMatrix`
42 // TODO: sse2 version
43 let (m00, m01, m02) = x_axis.into_tuple();
44 let (m10, m11, m12) = y_axis.into_tuple();
45 let (m20, m21, m22) = z_axis.into_tuple();
46 if m22 <= T::ZERO {
47 // x^2 + y^2 >= z^2 + w^2
48 let dif10 = m11 - m00;
49 let omm22 = T::ONE - m22;
50 if dif10 <= T::ZERO {
51 // x^2 >= y^2
52 let four_xsq = omm22 - dif10;
53 let inv4x = T::HALF / four_xsq.sqrt();
54 Self::new(
55 four_xsq * inv4x,
56 (m01 + m10) * inv4x,
57 (m02 + m20) * inv4x,
58 (m12 - m21) * inv4x,
59 )
60 } else {
61 // y^2 >= x^2
62 let four_ysq = omm22 + dif10;
63 let inv4y = T::HALF / four_ysq.sqrt();
64 Self::new(
65 (m01 + m10) * inv4y,
66 four_ysq * inv4y,
67 (m12 + m21) * inv4y,
68 (m20 - m02) * inv4y,
69 )
70 }
71 } else {
72 // z^2 + w^2 >= x^2 + y^2
73 let sum10 = m11 + m00;
74 let opm22 = T::ONE + m22;
75 if sum10 <= T::ZERO {
76 // z^2 >= w^2
77 let four_zsq = opm22 - sum10;
78 let inv4z = T::HALF / four_zsq.sqrt();
79 Self::new(
80 (m02 + m20) * inv4z,
81 (m12 + m21) * inv4z,
82 four_zsq * inv4z,
83 (m01 - m10) * inv4z,
84 )
85 } else {
86 // w^2 >= z^2
87 let four_wsq = opm22 + sum10;
88 let inv4w = T::HALF / four_wsq.sqrt();
89 Self::new(
90 (m12 - m21) * inv4w,
91 (m20 - m02) * inv4w,
92 (m01 - m10) * inv4w,
93 four_wsq * inv4w,
94 )
95 }
96 }
97 }
98
99 fn to_axis_angle(self) -> (XYZ<T>, T) {
100 // const EPSILON: f32 = 1.0e-8;
101 // const EPSILON_SQUARED: f32 = EPSILON * EPSILON;
102 let (x, y, z, w) = Vector4::into_tuple(self);
103 let angle = w.acos_approx() * T::TWO;
104 let scale_sq = NumEx::max(T::ONE - w * w, T::ZERO);
105 // TODO: constants for epslions?
106 if scale_sq >= T::from_f32(1.0e-8 * 1.0e-8) {
107 (XYZ { x, y, z }.mul_scalar(scale_sq.sqrt().recip()), angle)
108 } else {
109 (Vector3Const::X, angle)
110 }
111 }
112
113 #[inline]
114 fn is_near_identity(self) -> bool {
115 // Based on https://github.com/nfrechette/rtm `rtm::quat_near_identity`
116 let threshold_angle = T::from_f64(0.002_847_144_6);
117 // Because of floating point precision, we cannot represent very small rotations.
118 // The closest f32 to 1.0 that is not 1.0 itself yields:
119 // 0.99999994.acos() * 2.0 = 0.000690533954 rad
120 //
121 // An error threshold of 1.e-6 is used by default.
122 // (1.0 - 1.e-6).acos() * 2.0 = 0.00284714461 rad
123 // (1.0 - 1.e-7).acos() * 2.0 = 0.00097656250 rad
124 //
125 // We don't really care about the angle value itself, only if it's close to 0.
126 // This will happen whenever quat.w is close to 1.0.
127 // If the quat.w is close to -1.0, the angle will be near 2*PI which is close to
128 // a negative 0 rotation. By forcing quat.w to be positive, we'll end up with
129 // the shortest path.
130 let positive_w_angle = self.as_ref_xyzw().w.abs().acos_approx() * T::TWO;
131 positive_w_angle < threshold_angle
132 }
133
134 fn conjugate(self) -> Self;
135 fn lerp(self, end: Self, s: T) -> Self;
136 fn slerp(self, end: Self, s: T) -> Self;
137 fn mul_quaternion(self, other: Self) -> Self;
138 fn mul_vector3(self, other: XYZ<T>) -> XYZ<T>;
139 fn mul_float4_as_vector3(self, other: Self::SIMDVector3) -> Self::SIMDVector3;
140 }