1 | /**************************************************************************/ |
2 | /* quaternion.cpp */ |
3 | /**************************************************************************/ |
4 | /* This file is part of: */ |
5 | /* GODOT ENGINE */ |
6 | /* https://godotengine.org */ |
7 | /**************************************************************************/ |
8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
10 | /* */ |
11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
12 | /* a copy of this software and associated documentation files (the */ |
13 | /* "Software"), to deal in the Software without restriction, including */ |
14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
17 | /* the following conditions: */ |
18 | /* */ |
19 | /* The above copyright notice and this permission notice shall be */ |
20 | /* included in all copies or substantial portions of the Software. */ |
21 | /* */ |
22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
29 | /**************************************************************************/ |
30 | |
31 | #include "quaternion.h" |
32 | |
33 | #include "core/math/basis.h" |
34 | #include "core/string/ustring.h" |
35 | |
36 | real_t Quaternion::angle_to(const Quaternion &p_to) const { |
37 | real_t d = dot(p_to); |
38 | // acos does clamping. |
39 | return Math::acos(d * d * 2 - 1); |
40 | } |
41 | |
42 | Vector3 Quaternion::get_euler(EulerOrder p_order) const { |
43 | #ifdef MATH_CHECKS |
44 | ERR_FAIL_COND_V_MSG(!is_normalized(), Vector3(0, 0, 0), "The quaternion must be normalized." ); |
45 | #endif |
46 | return Basis(*this).get_euler(p_order); |
47 | } |
48 | |
49 | void Quaternion::operator*=(const Quaternion &p_q) { |
50 | real_t xx = w * p_q.x + x * p_q.w + y * p_q.z - z * p_q.y; |
51 | real_t yy = w * p_q.y + y * p_q.w + z * p_q.x - x * p_q.z; |
52 | real_t zz = w * p_q.z + z * p_q.w + x * p_q.y - y * p_q.x; |
53 | w = w * p_q.w - x * p_q.x - y * p_q.y - z * p_q.z; |
54 | x = xx; |
55 | y = yy; |
56 | z = zz; |
57 | } |
58 | |
59 | Quaternion Quaternion::operator*(const Quaternion &p_q) const { |
60 | Quaternion r = *this; |
61 | r *= p_q; |
62 | return r; |
63 | } |
64 | |
65 | bool Quaternion::is_equal_approx(const Quaternion &p_quaternion) const { |
66 | return Math::is_equal_approx(x, p_quaternion.x) && Math::is_equal_approx(y, p_quaternion.y) && Math::is_equal_approx(z, p_quaternion.z) && Math::is_equal_approx(w, p_quaternion.w); |
67 | } |
68 | |
69 | bool Quaternion::is_finite() const { |
70 | return Math::is_finite(x) && Math::is_finite(y) && Math::is_finite(z) && Math::is_finite(w); |
71 | } |
72 | |
73 | real_t Quaternion::length() const { |
74 | return Math::sqrt(length_squared()); |
75 | } |
76 | |
77 | void Quaternion::normalize() { |
78 | *this /= length(); |
79 | } |
80 | |
81 | Quaternion Quaternion::normalized() const { |
82 | return *this / length(); |
83 | } |
84 | |
85 | bool Quaternion::is_normalized() const { |
86 | return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); //use less epsilon |
87 | } |
88 | |
89 | Quaternion Quaternion::inverse() const { |
90 | #ifdef MATH_CHECKS |
91 | ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The quaternion must be normalized." ); |
92 | #endif |
93 | return Quaternion(-x, -y, -z, w); |
94 | } |
95 | |
96 | Quaternion Quaternion::log() const { |
97 | Quaternion src = *this; |
98 | Vector3 src_v = src.get_axis() * src.get_angle(); |
99 | return Quaternion(src_v.x, src_v.y, src_v.z, 0); |
100 | } |
101 | |
102 | Quaternion Quaternion::exp() const { |
103 | Quaternion src = *this; |
104 | Vector3 src_v = Vector3(src.x, src.y, src.z); |
105 | real_t theta = src_v.length(); |
106 | src_v = src_v.normalized(); |
107 | if (theta < CMP_EPSILON || !src_v.is_normalized()) { |
108 | return Quaternion(0, 0, 0, 1); |
109 | } |
110 | return Quaternion(src_v, theta); |
111 | } |
112 | |
113 | Quaternion Quaternion::slerp(const Quaternion &p_to, const real_t &p_weight) const { |
114 | #ifdef MATH_CHECKS |
115 | ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized." ); |
116 | ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quaternion(), "The end quaternion must be normalized." ); |
117 | #endif |
118 | Quaternion to1; |
119 | real_t omega, cosom, sinom, scale0, scale1; |
120 | |
121 | // calc cosine |
122 | cosom = dot(p_to); |
123 | |
124 | // adjust signs (if necessary) |
125 | if (cosom < 0.0f) { |
126 | cosom = -cosom; |
127 | to1 = -p_to; |
128 | } else { |
129 | to1 = p_to; |
130 | } |
131 | |
132 | // calculate coefficients |
133 | |
134 | if ((1.0f - cosom) > (real_t)CMP_EPSILON) { |
135 | // standard case (slerp) |
136 | omega = Math::acos(cosom); |
137 | sinom = Math::sin(omega); |
138 | scale0 = Math::sin((1.0 - p_weight) * omega) / sinom; |
139 | scale1 = Math::sin(p_weight * omega) / sinom; |
140 | } else { |
141 | // "from" and "to" quaternions are very close |
142 | // ... so we can do a linear interpolation |
143 | scale0 = 1.0f - p_weight; |
144 | scale1 = p_weight; |
145 | } |
146 | // calculate final values |
147 | return Quaternion( |
148 | scale0 * x + scale1 * to1.x, |
149 | scale0 * y + scale1 * to1.y, |
150 | scale0 * z + scale1 * to1.z, |
151 | scale0 * w + scale1 * to1.w); |
152 | } |
153 | |
154 | Quaternion Quaternion::slerpni(const Quaternion &p_to, const real_t &p_weight) const { |
155 | #ifdef MATH_CHECKS |
156 | ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized." ); |
157 | ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quaternion(), "The end quaternion must be normalized." ); |
158 | #endif |
159 | const Quaternion &from = *this; |
160 | |
161 | real_t dot = from.dot(p_to); |
162 | |
163 | if (Math::absf(dot) > 0.9999f) { |
164 | return from; |
165 | } |
166 | |
167 | real_t theta = Math::acos(dot), |
168 | sinT = 1.0f / Math::sin(theta), |
169 | newFactor = Math::sin(p_weight * theta) * sinT, |
170 | invFactor = Math::sin((1.0f - p_weight) * theta) * sinT; |
171 | |
172 | return Quaternion(invFactor * from.x + newFactor * p_to.x, |
173 | invFactor * from.y + newFactor * p_to.y, |
174 | invFactor * from.z + newFactor * p_to.z, |
175 | invFactor * from.w + newFactor * p_to.w); |
176 | } |
177 | |
178 | Quaternion Quaternion::spherical_cubic_interpolate(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, const real_t &p_weight) const { |
179 | #ifdef MATH_CHECKS |
180 | ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized." ); |
181 | ERR_FAIL_COND_V_MSG(!p_b.is_normalized(), Quaternion(), "The end quaternion must be normalized." ); |
182 | #endif |
183 | Quaternion from_q = *this; |
184 | Quaternion pre_q = p_pre_a; |
185 | Quaternion to_q = p_b; |
186 | Quaternion post_q = p_post_b; |
187 | |
188 | // Align flip phases. |
189 | from_q = Basis(from_q).get_rotation_quaternion(); |
190 | pre_q = Basis(pre_q).get_rotation_quaternion(); |
191 | to_q = Basis(to_q).get_rotation_quaternion(); |
192 | post_q = Basis(post_q).get_rotation_quaternion(); |
193 | |
194 | // Flip quaternions to shortest path if necessary. |
195 | bool flip1 = signbit(from_q.dot(pre_q)); |
196 | pre_q = flip1 ? -pre_q : pre_q; |
197 | bool flip2 = signbit(from_q.dot(to_q)); |
198 | to_q = flip2 ? -to_q : to_q; |
199 | bool flip3 = flip2 ? to_q.dot(post_q) <= 0 : signbit(to_q.dot(post_q)); |
200 | post_q = flip3 ? -post_q : post_q; |
201 | |
202 | // Calc by Expmap in from_q space. |
203 | Quaternion ln_from = Quaternion(0, 0, 0, 0); |
204 | Quaternion ln_to = (from_q.inverse() * to_q).log(); |
205 | Quaternion ln_pre = (from_q.inverse() * pre_q).log(); |
206 | Quaternion ln_post = (from_q.inverse() * post_q).log(); |
207 | Quaternion ln = Quaternion(0, 0, 0, 0); |
208 | ln.x = Math::cubic_interpolate(ln_from.x, ln_to.x, ln_pre.x, ln_post.x, p_weight); |
209 | ln.y = Math::cubic_interpolate(ln_from.y, ln_to.y, ln_pre.y, ln_post.y, p_weight); |
210 | ln.z = Math::cubic_interpolate(ln_from.z, ln_to.z, ln_pre.z, ln_post.z, p_weight); |
211 | Quaternion q1 = from_q * ln.exp(); |
212 | |
213 | // Calc by Expmap in to_q space. |
214 | ln_from = (to_q.inverse() * from_q).log(); |
215 | ln_to = Quaternion(0, 0, 0, 0); |
216 | ln_pre = (to_q.inverse() * pre_q).log(); |
217 | ln_post = (to_q.inverse() * post_q).log(); |
218 | ln = Quaternion(0, 0, 0, 0); |
219 | ln.x = Math::cubic_interpolate(ln_from.x, ln_to.x, ln_pre.x, ln_post.x, p_weight); |
220 | ln.y = Math::cubic_interpolate(ln_from.y, ln_to.y, ln_pre.y, ln_post.y, p_weight); |
221 | ln.z = Math::cubic_interpolate(ln_from.z, ln_to.z, ln_pre.z, ln_post.z, p_weight); |
222 | Quaternion q2 = to_q * ln.exp(); |
223 | |
224 | // To cancel error made by Expmap ambiguity, do blending. |
225 | return q1.slerp(q2, p_weight); |
226 | } |
227 | |
228 | Quaternion Quaternion::spherical_cubic_interpolate_in_time(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, const real_t &p_weight, |
229 | const real_t &p_b_t, const real_t &p_pre_a_t, const real_t &p_post_b_t) const { |
230 | #ifdef MATH_CHECKS |
231 | ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion must be normalized." ); |
232 | ERR_FAIL_COND_V_MSG(!p_b.is_normalized(), Quaternion(), "The end quaternion must be normalized." ); |
233 | #endif |
234 | Quaternion from_q = *this; |
235 | Quaternion pre_q = p_pre_a; |
236 | Quaternion to_q = p_b; |
237 | Quaternion post_q = p_post_b; |
238 | |
239 | // Align flip phases. |
240 | from_q = Basis(from_q).get_rotation_quaternion(); |
241 | pre_q = Basis(pre_q).get_rotation_quaternion(); |
242 | to_q = Basis(to_q).get_rotation_quaternion(); |
243 | post_q = Basis(post_q).get_rotation_quaternion(); |
244 | |
245 | // Flip quaternions to shortest path if necessary. |
246 | bool flip1 = signbit(from_q.dot(pre_q)); |
247 | pre_q = flip1 ? -pre_q : pre_q; |
248 | bool flip2 = signbit(from_q.dot(to_q)); |
249 | to_q = flip2 ? -to_q : to_q; |
250 | bool flip3 = flip2 ? to_q.dot(post_q) <= 0 : signbit(to_q.dot(post_q)); |
251 | post_q = flip3 ? -post_q : post_q; |
252 | |
253 | // Calc by Expmap in from_q space. |
254 | Quaternion ln_from = Quaternion(0, 0, 0, 0); |
255 | Quaternion ln_to = (from_q.inverse() * to_q).log(); |
256 | Quaternion ln_pre = (from_q.inverse() * pre_q).log(); |
257 | Quaternion ln_post = (from_q.inverse() * post_q).log(); |
258 | Quaternion ln = Quaternion(0, 0, 0, 0); |
259 | ln.x = Math::cubic_interpolate_in_time(ln_from.x, ln_to.x, ln_pre.x, ln_post.x, p_weight, p_b_t, p_pre_a_t, p_post_b_t); |
260 | ln.y = Math::cubic_interpolate_in_time(ln_from.y, ln_to.y, ln_pre.y, ln_post.y, p_weight, p_b_t, p_pre_a_t, p_post_b_t); |
261 | ln.z = Math::cubic_interpolate_in_time(ln_from.z, ln_to.z, ln_pre.z, ln_post.z, p_weight, p_b_t, p_pre_a_t, p_post_b_t); |
262 | Quaternion q1 = from_q * ln.exp(); |
263 | |
264 | // Calc by Expmap in to_q space. |
265 | ln_from = (to_q.inverse() * from_q).log(); |
266 | ln_to = Quaternion(0, 0, 0, 0); |
267 | ln_pre = (to_q.inverse() * pre_q).log(); |
268 | ln_post = (to_q.inverse() * post_q).log(); |
269 | ln = Quaternion(0, 0, 0, 0); |
270 | ln.x = Math::cubic_interpolate_in_time(ln_from.x, ln_to.x, ln_pre.x, ln_post.x, p_weight, p_b_t, p_pre_a_t, p_post_b_t); |
271 | ln.y = Math::cubic_interpolate_in_time(ln_from.y, ln_to.y, ln_pre.y, ln_post.y, p_weight, p_b_t, p_pre_a_t, p_post_b_t); |
272 | ln.z = Math::cubic_interpolate_in_time(ln_from.z, ln_to.z, ln_pre.z, ln_post.z, p_weight, p_b_t, p_pre_a_t, p_post_b_t); |
273 | Quaternion q2 = to_q * ln.exp(); |
274 | |
275 | // To cancel error made by Expmap ambiguity, do blending. |
276 | return q1.slerp(q2, p_weight); |
277 | } |
278 | |
279 | Quaternion::operator String() const { |
280 | return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ", " + String::num_real(w, false) + ")" ; |
281 | } |
282 | |
283 | Vector3 Quaternion::get_axis() const { |
284 | if (Math::abs(w) > 1 - CMP_EPSILON) { |
285 | return Vector3(x, y, z); |
286 | } |
287 | real_t r = ((real_t)1) / Math::sqrt(1 - w * w); |
288 | return Vector3(x * r, y * r, z * r); |
289 | } |
290 | |
291 | real_t Quaternion::get_angle() const { |
292 | return 2 * Math::acos(w); |
293 | } |
294 | |
295 | Quaternion::Quaternion(const Vector3 &p_axis, real_t p_angle) { |
296 | #ifdef MATH_CHECKS |
297 | ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 must be normalized." ); |
298 | #endif |
299 | real_t d = p_axis.length(); |
300 | if (d == 0) { |
301 | x = 0; |
302 | y = 0; |
303 | z = 0; |
304 | w = 0; |
305 | } else { |
306 | real_t sin_angle = Math::sin(p_angle * 0.5f); |
307 | real_t cos_angle = Math::cos(p_angle * 0.5f); |
308 | real_t s = sin_angle / d; |
309 | x = p_axis.x * s; |
310 | y = p_axis.y * s; |
311 | z = p_axis.z * s; |
312 | w = cos_angle; |
313 | } |
314 | } |
315 | |
316 | // Euler constructor expects a vector containing the Euler angles in the format |
317 | // (ax, ay, az), where ax is the angle of rotation around x axis, |
318 | // and similar for other axes. |
319 | // This implementation uses YXZ convention (Z is the first rotation). |
320 | Quaternion Quaternion::from_euler(const Vector3 &p_euler) { |
321 | real_t half_a1 = p_euler.y * 0.5f; |
322 | real_t half_a2 = p_euler.x * 0.5f; |
323 | real_t half_a3 = p_euler.z * 0.5f; |
324 | |
325 | // R = Y(a1).X(a2).Z(a3) convention for Euler angles. |
326 | // Conversion to quaternion as listed in https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19770024290.pdf (page A-6) |
327 | // a3 is the angle of the first rotation, following the notation in this reference. |
328 | |
329 | real_t cos_a1 = Math::cos(half_a1); |
330 | real_t sin_a1 = Math::sin(half_a1); |
331 | real_t cos_a2 = Math::cos(half_a2); |
332 | real_t sin_a2 = Math::sin(half_a2); |
333 | real_t cos_a3 = Math::cos(half_a3); |
334 | real_t sin_a3 = Math::sin(half_a3); |
335 | |
336 | return Quaternion( |
337 | sin_a1 * cos_a2 * sin_a3 + cos_a1 * sin_a2 * cos_a3, |
338 | sin_a1 * cos_a2 * cos_a3 - cos_a1 * sin_a2 * sin_a3, |
339 | -sin_a1 * sin_a2 * cos_a3 + cos_a1 * cos_a2 * sin_a3, |
340 | sin_a1 * sin_a2 * sin_a3 + cos_a1 * cos_a2 * cos_a3); |
341 | } |
342 | |