master
ブランチ master
ShaderPlayground / deps / glm / glm / ext / quaternion_common.inl
3 KB、107行 生 Blame 履歴
1namespace glm
2{
3 template<typename T, qualifier Q>
4 GLM_FUNC_QUALIFIER qua<T, Q> mix(qua<T, Q> const& x, qua<T, Q> const& y, T a)
5 {
6 GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'mix' only accept floating-point inputs");
7
8 T const cosTheta = dot(x, y);
9
10 // Perform a linear interpolation when cosTheta is close to 1 to avoid side effect of sin(angle) becoming a zero denominator
11 if(cosTheta > static_cast<T>(1) - epsilon<T>())
12 {
13 // Linear interpolation
14 return qua<T, Q>(
15 mix(x.w, y.w, a),
16 mix(x.x, y.x, a),
17 mix(x.y, y.y, a),
18 mix(x.z, y.z, a));
19 }
20 else
21 {
22 // Essential Mathematics, page 467
23 T angle = acos(cosTheta);
24 return (sin((static_cast<T>(1) - a) * angle) * x + sin(a * angle) * y) / sin(angle);
25 }
26 }
27
28 template<typename T, qualifier Q>
29 GLM_FUNC_QUALIFIER qua<T, Q> lerp(qua<T, Q> const& x, qua<T, Q> const& y, T a)
30 {
31 GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'lerp' only accept floating-point inputs");
32
33 // Lerp is only defined in [0, 1]
34 assert(a >= static_cast<T>(0));
35 assert(a <= static_cast<T>(1));
36
37 return x * (static_cast<T>(1) - a) + (y * a);
38 }
39
40 template<typename T, qualifier Q>
41 GLM_FUNC_QUALIFIER qua<T, Q> slerp(qua<T, Q> const& x, qua<T, Q> const& y, T a)
42 {
43 GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'slerp' only accept floating-point inputs");
44
45 qua<T, Q> z = y;
46
47 T cosTheta = dot(x, y);
48
49 // If cosTheta < 0, the interpolation will take the long way around the sphere.
50 // To fix this, one quat must be negated.
51 if(cosTheta < static_cast<T>(0))
52 {
53 z = -y;
54 cosTheta = -cosTheta;
55 }
56
57 // Perform a linear interpolation when cosTheta is close to 1 to avoid side effect of sin(angle) becoming a zero denominator
58 if(cosTheta > static_cast<T>(1) - epsilon<T>())
59 {
60 // Linear interpolation
61 return qua<T, Q>(
62 mix(x.w, z.w, a),
63 mix(x.x, z.x, a),
64 mix(x.y, z.y, a),
65 mix(x.z, z.z, a));
66 }
67 else
68 {
69 // Essential Mathematics, page 467
70 T angle = acos(cosTheta);
71 return (sin((static_cast<T>(1) - a) * angle) * x + sin(a * angle) * z) / sin(angle);
72 }
73 }
74
75 template<typename T, qualifier Q>
76 GLM_FUNC_QUALIFIER qua<T, Q> conjugate(qua<T, Q> const& q)
77 {
78 return qua<T, Q>(q.w, -q.x, -q.y, -q.z);
79 }
80
81 template<typename T, qualifier Q>
82 GLM_FUNC_QUALIFIER qua<T, Q> inverse(qua<T, Q> const& q)
83 {
84 return conjugate(q) / dot(q, q);
85 }
86
87 template<typename T, qualifier Q>
88 GLM_FUNC_QUALIFIER vec<4, bool, Q> isnan(qua<T, Q> const& q)
89 {
90 GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'isnan' only accept floating-point inputs");
91
92 return vec<4, bool, Q>(isnan(q.x), isnan(q.y), isnan(q.z), isnan(q.w));
93 }
94
95 template<typename T, qualifier Q>
96 GLM_FUNC_QUALIFIER vec<4, bool, Q> isinf(qua<T, Q> const& q)
97 {
98 GLM_STATIC_ASSERT(std::numeric_limits<T>::is_iec559, "'isinf' only accept floating-point inputs");
99
100 return vec<4, bool, Q>(isinf(q.x), isinf(q.y), isinf(q.z), isinf(q.w));
101 }
102}//namespace glm
103
104#if GLM_CONFIG_SIMD == GLM_ENABLE
105# include "quaternion_common_simd.inl"
106#endif
107