Debian Codegraph @

core/math/quaternion.cpp

raw symbols SCIP
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 " + operator String() + " must be normalized.");
45 #endif
46 return Basis(*this).get_euler(p_order);
47 }
48
49 bool Quaternion::is_equal_approx(const Quaternion &p_quaternion) const {
50 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);
51 }
52
53 bool Quaternion::is_same(const Quaternion &p_quaternion) const {
54 return Math::is_same(x, p_quaternion.x) && Math::is_same(y, p_quaternion.y) && Math::is_same(z, p_quaternion.z) && Math::is_same(w, p_quaternion.w);
55 }
56
57 bool Quaternion::is_finite() const {
58 return Math::is_finite(x) && Math::is_finite(y) && Math::is_finite(z) && Math::is_finite(w);
59 }
60
61 real_t Quaternion::length() const {
62 return Math::sqrt(length_squared());
63 }
64
65 void Quaternion::normalize() {
66 *this /= length();
67 }
68
69 Quaternion Quaternion::normalized() const {
70 return *this / length();
71 }
72
73 bool Quaternion::is_normalized() const {
74 return Math::is_equal_approx(length_squared(), 1, (real_t)UNIT_EPSILON); //use less epsilon
75 }
76
77 Quaternion Quaternion::inverse() const {
78 #ifdef MATH_CHECKS
79 ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The quaternion " + operator String() + " must be normalized.");
80 #endif
81 return Quaternion(-x, -y, -z, w);
82 }
83
84 Quaternion Quaternion::log() const {
85 Quaternion src = *this;
86 Vector3 src_v = src.get_axis() * src.get_angle();
87 return Quaternion(src_v.x, src_v.y, src_v.z, 0);
88 }
89
90 Quaternion Quaternion::exp() const {
91 Quaternion src = *this;
92 Vector3 src_v = Vector3(src.x, src.y, src.z);
93 real_t theta = src_v.length();
94 src_v = src_v.normalized();
95 if (theta < CMP_EPSILON || !src_v.is_normalized()) {
96 return Quaternion(0, 0, 0, 1);
97 }
98 return Quaternion(src_v, theta);
99 }
100
101 Quaternion Quaternion::slerp(const Quaternion &p_to, real_t p_weight) const {
102 #ifdef MATH_CHECKS
103 ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion " + operator String() + " must be normalized.");
104 ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quaternion(), "The end quaternion " + p_to.operator String() + " must be normalized.");
105 #endif
106 Quaternion to1;
107 real_t omega, cosom, sinom, scale0, scale1;
108
109 // calc cosine
110 cosom = dot(p_to);
111
112 // adjust signs (if necessary)
113 if (cosom < 0.0f) {
114 cosom = -cosom;
115 to1 = -p_to;
116 } else {
117 to1 = p_to;
118 }
119
120 // calculate coefficients
121
122 if ((1.0f - cosom) > (real_t)CMP_EPSILON) {
123 // standard case (slerp)
124 omega = Math::acos(cosom);
125 sinom = Math::sin(omega);
126 scale0 = Math::sin((1.0 - p_weight) * omega) / sinom;
127 scale1 = Math::sin(p_weight * omega) / sinom;
128 } else {
129 // "from" and "to" quaternions are very close
130 // ... so we can do a linear interpolation
131 scale0 = 1.0f - p_weight;
132 scale1 = p_weight;
133 }
134 // calculate final values
135 return Quaternion(
136 scale0 * x + scale1 * to1.x,
137 scale0 * y + scale1 * to1.y,
138 scale0 * z + scale1 * to1.z,
139 scale0 * w + scale1 * to1.w);
140 }
141
142 Quaternion Quaternion::slerpni(const Quaternion &p_to, real_t p_weight) const {
143 #ifdef MATH_CHECKS
144 ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion " + operator String() + " must be normalized.");
145 ERR_FAIL_COND_V_MSG(!p_to.is_normalized(), Quaternion(), "The end quaternion " + p_to.operator String() + " must be normalized.");
146 #endif
147 const Quaternion &from = *this;
148
149 real_t dot = from.dot(p_to);
150
151 if (Math::abs(dot) > 0.9999f) {
152 return from;
153 }
154
155 real_t theta = Math::acos(dot),
156 sinT = 1.0f / Math::sin(theta),
157 newFactor = Math::sin(p_weight * theta) * sinT,
158 invFactor = Math::sin((1.0f - p_weight) * theta) * sinT;
159
160 return Quaternion(invFactor * from.x + newFactor * p_to.x,
161 invFactor * from.y + newFactor * p_to.y,
162 invFactor * from.z + newFactor * p_to.z,
163 invFactor * from.w + newFactor * p_to.w);
164 }
165
166 Quaternion Quaternion::spherical_cubic_interpolate(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, real_t p_weight) const {
167 #ifdef MATH_CHECKS
168 ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion " + operator String() + " must be normalized.");
169 ERR_FAIL_COND_V_MSG(!p_b.is_normalized(), Quaternion(), "The end quaternion " + p_b.operator String() + " must be normalized.");
170 #endif
171 Quaternion from_q = *this;
172 Quaternion pre_q = p_pre_a;
173 Quaternion to_q = p_b;
174 Quaternion post_q = p_post_b;
175
176 // Align flip phases.
177 from_q = Basis(from_q).get_rotation_quaternion();
178 pre_q = Basis(pre_q).get_rotation_quaternion();
179 to_q = Basis(to_q).get_rotation_quaternion();
180 post_q = Basis(post_q).get_rotation_quaternion();
181
182 // Flip quaternions to shortest path if necessary.
183 bool flip1 = std::signbit(from_q.dot(pre_q));
184 pre_q = flip1 ? -pre_q : pre_q;
185 bool flip2 = std::signbit(from_q.dot(to_q));
186 to_q = flip2 ? -to_q : to_q;
187 bool flip3 = flip2 ? to_q.dot(post_q) <= 0 : std::signbit(to_q.dot(post_q));
188 post_q = flip3 ? -post_q : post_q;
189
190 // Calc by Expmap in from_q space.
191 Quaternion ln_from = Quaternion(0, 0, 0, 0);
192 Quaternion ln_to = (from_q.inverse() * to_q).log();
193 Quaternion ln_pre = (from_q.inverse() * pre_q).log();
194 Quaternion ln_post = (from_q.inverse() * post_q).log();
195 Quaternion ln = Quaternion(0, 0, 0, 0);
196 ln.x = Math::cubic_interpolate(ln_from.x, ln_to.x, ln_pre.x, ln_post.x, p_weight);
197 ln.y = Math::cubic_interpolate(ln_from.y, ln_to.y, ln_pre.y, ln_post.y, p_weight);
198 ln.z = Math::cubic_interpolate(ln_from.z, ln_to.z, ln_pre.z, ln_post.z, p_weight);
199 Quaternion q1 = from_q * ln.exp();
200
201 // Calc by Expmap in to_q space.
202 ln_from = (to_q.inverse() * from_q).log();
203 ln_to = Quaternion(0, 0, 0, 0);
204 ln_pre = (to_q.inverse() * pre_q).log();
205 ln_post = (to_q.inverse() * post_q).log();
206 ln = Quaternion(0, 0, 0, 0);
207 ln.x = Math::cubic_interpolate(ln_from.x, ln_to.x, ln_pre.x, ln_post.x, p_weight);
208 ln.y = Math::cubic_interpolate(ln_from.y, ln_to.y, ln_pre.y, ln_post.y, p_weight);
209 ln.z = Math::cubic_interpolate(ln_from.z, ln_to.z, ln_pre.z, ln_post.z, p_weight);
210 Quaternion q2 = to_q * ln.exp();
211
212 // To cancel error made by Expmap ambiguity, do blending.
213 return q1.slerp(q2, p_weight);
214 }
215
216 Quaternion Quaternion::spherical_cubic_interpolate_in_time(const Quaternion &p_b, const Quaternion &p_pre_a, const Quaternion &p_post_b, real_t p_weight,
217 real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const {
218 #ifdef MATH_CHECKS
219 ERR_FAIL_COND_V_MSG(!is_normalized(), Quaternion(), "The start quaternion " + operator String() + " must be normalized.");
220 ERR_FAIL_COND_V_MSG(!p_b.is_normalized(), Quaternion(), "The end quaternion " + p_b.operator String() + " must be normalized.");
221 #endif
222 Quaternion from_q = *this;
223 Quaternion pre_q = p_pre_a;
224 Quaternion to_q = p_b;
225 Quaternion post_q = p_post_b;
226
227 // Align flip phases.
228 from_q = Basis(from_q).get_rotation_quaternion();
229 pre_q = Basis(pre_q).get_rotation_quaternion();
230 to_q = Basis(to_q).get_rotation_quaternion();
231 post_q = Basis(post_q).get_rotation_quaternion();
232
233 // Flip quaternions to shortest path if necessary.
234 bool flip1 = std::signbit(from_q.dot(pre_q));
235 pre_q = flip1 ? -pre_q : pre_q;
236 bool flip2 = std::signbit(from_q.dot(to_q));
237 to_q = flip2 ? -to_q : to_q;
238 bool flip3 = flip2 ? to_q.dot(post_q) <= 0 : std::signbit(to_q.dot(post_q));
239 post_q = flip3 ? -post_q : post_q;
240
241 // Calc by Expmap in from_q space.
242 Quaternion ln_from = Quaternion(0, 0, 0, 0);
243 Quaternion ln_to = (from_q.inverse() * to_q).log();
244 Quaternion ln_pre = (from_q.inverse() * pre_q).log();
245 Quaternion ln_post = (from_q.inverse() * post_q).log();
246 Quaternion ln = Quaternion(0, 0, 0, 0);
247 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);
248 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);
249 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);
250 Quaternion q1 = from_q * ln.exp();
251
252 // Calc by Expmap in to_q space.
253 ln_from = (to_q.inverse() * from_q).log();
254 ln_to = Quaternion(0, 0, 0, 0);
255 ln_pre = (to_q.inverse() * pre_q).log();
256 ln_post = (to_q.inverse() * post_q).log();
257 ln = Quaternion(0, 0, 0, 0);
258 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);
259 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);
260 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);
261 Quaternion q2 = to_q * ln.exp();
262
263 // To cancel error made by Expmap ambiguity, do blending.
264 return q1.slerp(q2, p_weight);
265 }
266
267 Quaternion::operator String() const {
268 return "(" + String::num_real(x, false) + ", " + String::num_real(y, false) + ", " + String::num_real(z, false) + ", " + String::num_real(w, false) + ")";
269 }
270
271 Vector3 Quaternion::get_axis() const {
272 if (Math::abs(w) > 1 - CMP_EPSILON) {
273 return Vector3(x, y, z);
274 }
275 real_t r = ((real_t)1) / Math::sqrt(1 - w * w);
276 return Vector3(x * r, y * r, z * r);
277 }
278
279 real_t Quaternion::get_angle() const {
280 return 2 * Math::acos(w);
281 }
282
283 Quaternion::Quaternion(const Vector3 &p_axis, real_t p_angle) {
284 #ifdef MATH_CHECKS
285 ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 " + p_axis.operator String() + " must be normalized.");
286 #endif
287 real_t d = p_axis.length();
288 if (d == 0) {
289 x = 0;
290 y = 0;
291 z = 0;
292 w = 0;
293 } else {
294 real_t sin_angle = Math::sin(p_angle * 0.5f);
295 real_t cos_angle = Math::cos(p_angle * 0.5f);
296 real_t s = sin_angle / d;
297 x = p_axis.x * s;
298 y = p_axis.y * s;
299 z = p_axis.z * s;
300 w = cos_angle;
301 }
302 }
303
304 // Euler constructor expects a vector containing the Euler angles in the format
305 // (ax, ay, az), where ax is the angle of rotation around x axis,
306 // and similar for other axes.
307 // This implementation uses YXZ convention (Z is the first rotation).
308 Quaternion Quaternion::from_euler(const Vector3 &p_euler) {
309 real_t half_a1 = p_euler.y * 0.5f;
310 real_t half_a2 = p_euler.x * 0.5f;
311 real_t half_a3 = p_euler.z * 0.5f;
312
313 // R = Y(a1).X(a2).Z(a3) convention for Euler angles.
314 // Conversion to quaternion as listed in https://ntrs.nasa.gov/archive/nasa/casi.ntrs.nasa.gov/19770024290.pdf (page A-6)
315 // a3 is the angle of the first rotation, following the notation in this reference.
316
317 real_t cos_a1 = Math::cos(half_a1);
318 real_t sin_a1 = Math::sin(half_a1);
319 real_t cos_a2 = Math::cos(half_a2);
320 real_t sin_a2 = Math::sin(half_a2);
321 real_t cos_a3 = Math::cos(half_a3);
322 real_t sin_a3 = Math::sin(half_a3);
323
324 return Quaternion(
325 sin_a1 * cos_a2 * sin_a3 + cos_a1 * sin_a2 * cos_a3,
326 sin_a1 * cos_a2 * cos_a3 - cos_a1 * sin_a2 * sin_a3,
327 -sin_a1 * sin_a2 * cos_a3 + cos_a1 * cos_a2 * sin_a3,
328 sin_a1 * sin_a2 * sin_a3 + cos_a1 * cos_a2 * cos_a3);
329 }