RavEngine
Loading...
Searching...
No Matches
quaternion.h
1//----------------------------------------------------------------------------//
2// //
3// ozz-animation is hosted at http://github.com/guillaumeblanc/ozz-animation //
4// and distributed under the MIT License (MIT). //
5// //
6// Copyright (c) Guillaume Blanc //
7// //
8// Permission is hereby granted, free of charge, to any person obtaining a //
9// copy of this software and associated documentation files (the "Software"), //
10// to deal in the Software without restriction, including without limitation //
11// the rights to use, copy, modify, merge, publish, distribute, sublicense, //
12// and/or sell copies of the Software, and to permit persons to whom the //
13// Software is furnished to do so, subject to the following conditions: //
14// //
15// The above copyright notice and this permission notice shall be included in //
16// all copies or substantial portions of the Software. //
17// //
18// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
19// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //
20// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL //
21// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
22// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING //
23// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER //
24// DEALINGS IN THE SOFTWARE. //
25// //
26//----------------------------------------------------------------------------//
27
28#ifndef OZZ_OZZ_BASE_MATHS_QUATERNION_H_
29#define OZZ_OZZ_BASE_MATHS_QUATERNION_H_
30
31#include <cassert>
32
33#include "ozz/base/maths/math_constant.h"
34#include "ozz/base/maths/math_ex.h"
35#include "ozz/base/maths/vec_float.h"
36#include "ozz/base/platform.h"
37
38namespace ozz {
39namespace math {
40
41struct OZZ_BASE_DLL Quaternion {
42 float x, y, z, w;
43
44 // Constructs an uninitialized quaternion.
45 OZZ_INLINE Quaternion() {}
46
47 // Constructs a quaternion from 4 floating point values.
48 OZZ_INLINE Quaternion(float _x, float _y, float _z, float _w)
49 : x(_x), y(_y), z(_z), w(_w) {}
50
51 // Returns a normalized quaternion initialized from an axis angle
52 // representation.
53 // Assumes the axis part (x, y, z) of _axis_angle is normalized.
54 // _angle.x is the angle in radian.
55 static OZZ_INLINE Quaternion FromAxisAngle(const Float3& _axis, float _angle);
56
57 // Returns a normalized quaternion initialized from an axis and angle cosine
58 // representation.
59 // Assumes the axis part (x, y, z) of _axis_angle is normalized.
60 // _angle.x is the angle cosine in radian, it must be within [-1,1] range.
61 static OZZ_INLINE Quaternion FromAxisCosAngle(const Float3& _axis,
62 float _cos);
63
64 // Returns a normalized quaternion initialized from an Euler representation.
65 // Euler angles are ordered Heading, Elevation and Bank, or Yaw, Pitch and
66 // Roll.
67 static OZZ_INLINE Quaternion FromEuler(float _yaw, float _pitch,
68 float _roll);
69
70 // Returns the quaternion that will rotate vector _from into vector _to,
71 // around their plan perpendicular axis.The input vectors don't need to be
72 // normalized, they can be null as well.
73 static OZZ_INLINE Quaternion FromVectors(const Float3& _from,
74 const Float3& _to);
75
76 // Returns the quaternion that will rotate vector _from into vector _to,
77 // around their plan perpendicular axis. The input vectors must be normalized.
78 static OZZ_INLINE Quaternion FromUnitVectors(const Float3& _from,
79 const Float3& _to);
80
81 // Returns the identity quaternion.
82 static OZZ_INLINE Quaternion identity() {
83 return Quaternion(0.f, 0.f, 0.f, 1.f);
84 }
85};
86
87// Returns true if each element of a is equal to each element of _b.
88OZZ_INLINE bool operator==(const Quaternion& _a, const Quaternion& _b) {
89 return _a.x == _b.x && _a.y == _b.y && _a.z == _b.z && _a.w == _b.w;
90}
91
92// Returns true if one element of a differs from one element of _b.
93OZZ_INLINE bool operator!=(const Quaternion& _a, const Quaternion& _b) {
94 return _a.x != _b.x || _a.y != _b.y || _a.z != _b.z || _a.w != _b.w;
95}
96
97// Returns the conjugate of _q. This is the same as the inverse if _q is
98// normalized. Otherwise the magnitude of the inverse is 1.f/|_q|.
99OZZ_INLINE Quaternion Conjugate(const Quaternion& _q) {
100 return Quaternion(-_q.x, -_q.y, -_q.z, _q.w);
101}
102
103// Returns the addition of _a and _b.
104OZZ_INLINE Quaternion operator+(const Quaternion& _a, const Quaternion& _b) {
105 return Quaternion(_a.x + _b.x, _a.y + _b.y, _a.z + _b.z, _a.w + _b.w);
106}
107
108// Returns the multiplication of _q and a scalar _f.
109OZZ_INLINE Quaternion operator*(const Quaternion& _q, float _f) {
110 return Quaternion(_q.x * _f, _q.y * _f, _q.z * _f, _q.w * _f);
111}
112
113// Returns the multiplication of _a and _b. If both _a and _b are normalized,
114// then the result is normalized.
115OZZ_INLINE Quaternion operator*(const Quaternion& _a, const Quaternion& _b) {
116 return Quaternion(_a.w * _b.x + _a.x * _b.w + _a.y * _b.z - _a.z * _b.y,
117 _a.w * _b.y + _a.y * _b.w + _a.z * _b.x - _a.x * _b.z,
118 _a.w * _b.z + _a.z * _b.w + _a.x * _b.y - _a.y * _b.x,
119 _a.w * _b.w - _a.x * _b.x - _a.y * _b.y - _a.z * _b.z);
120}
121
122// Returns the negate of _q. This represent the same rotation as q.
123OZZ_INLINE Quaternion operator-(const Quaternion& _q) {
124 return Quaternion(-_q.x, -_q.y, -_q.z, -_q.w);
125}
126
127// Returns true if the angle between _a and _b is less than _tolerance.
128OZZ_INLINE bool Compare(const math::Quaternion& _a, const math::Quaternion& _b,
129 float _cos_half_tolerance) {
130 // Computes w component of a-1 * b.
131 const float cos_half_angle =
132 _a.x * _b.x + _a.y * _b.y + _a.z * _b.z + _a.w * _b.w;
133 return std::abs(cos_half_angle) >= _cos_half_tolerance;
134}
135
136// Returns true if _q is a normalized quaternion.
137OZZ_INLINE bool IsNormalized(const Quaternion& _q) {
138 const float sq_len = _q.x * _q.x + _q.y * _q.y + _q.z * _q.z + _q.w * _q.w;
139 return std::abs(sq_len - 1.f) < kNormalizationToleranceSq;
140}
141
142// Returns the normalized quaternion _q.
143OZZ_INLINE Quaternion Normalize(const Quaternion& _q) {
144 const float sq_len = _q.x * _q.x + _q.y * _q.y + _q.z * _q.z + _q.w * _q.w;
145 assert(sq_len != 0.f && "_q is not normalizable");
146 const float inv_len = 1.f / std::sqrt(sq_len);
147 return Quaternion(_q.x * inv_len, _q.y * inv_len, _q.z * inv_len,
148 _q.w * inv_len);
149}
150
151// Returns the normalized quaternion _q if the norm of _q is not 0.
152// Otherwise returns _safer.
153OZZ_INLINE Quaternion NormalizeSafe(const Quaternion& _q,
154 const Quaternion& _safer) {
155 assert(IsNormalized(_safer) && "_safer is not normalized");
156 const float sq_len = _q.x * _q.x + _q.y * _q.y + _q.z * _q.z + _q.w * _q.w;
157 if (sq_len == 0) {
158 return _safer;
159 }
160 const float inv_len = 1.f / std::sqrt(sq_len);
161 return Quaternion(_q.x * inv_len, _q.y * inv_len, _q.z * inv_len,
162 _q.w * inv_len);
163}
164
165OZZ_INLINE Quaternion Quaternion::FromAxisAngle(const Float3& _axis,
166 float _angle) {
167 assert(IsNormalized(_axis) && "axis is not normalized.");
168 const float half_angle = _angle * .5f;
169 const float half_sin = std::sin(half_angle);
170 const float half_cos = std::cos(half_angle);
171 return Quaternion(_axis.x * half_sin, _axis.y * half_sin, _axis.z * half_sin,
172 half_cos);
173}
174
175OZZ_INLINE Quaternion Quaternion::FromAxisCosAngle(const Float3& _axis,
176 float _cos) {
177 assert(IsNormalized(_axis) && "axis is not normalized.");
178 assert(_cos >= -1.f && _cos <= 1.f && "cos is not in [-1,1] range.");
179
180 const float half_cos2 = (1.f + _cos) * 0.5f;
181 const float half_sin = std::sqrt(1.f - half_cos2);
182 return Quaternion(_axis.x * half_sin, _axis.y * half_sin, _axis.z * half_sin,
183 std::sqrt(half_cos2));
184}
185
186// Returns to an axis angle representation of quaternion _q.
187// Assumes quaternion _q is normalized.
188OZZ_INLINE Float4 ToAxisAngle(const Quaternion& _q) {
189 assert(IsNormalized(_q));
190 const float clamped_w = Clamp(-1.f, _q.w, 1.f);
191 const float angle = 2.f * std::acos(clamped_w);
192 const float s = std::sqrt(1.f - clamped_w * clamped_w);
193
194 // Assuming quaternion normalized then s always positive.
195 if (s < .001f) { // Tests to avoid divide by zero.
196 // If s close to zero then direction of axis is not important.
197 return Float4(1.f, 0.f, 0.f, angle);
198 } else {
199 // Normalize axis
200 const float inv_s = 1.f / s;
201 return Float4(_q.x * inv_s, _q.y * inv_s, _q.z * inv_s, angle);
202 }
203}
204
205OZZ_INLINE Quaternion Quaternion::FromEuler(float _yaw, float _pitch,
206 float _roll) {
207 const float half_yaw = _yaw * .5f;
208 const float c1 = std::cos(half_yaw);
209 const float s1 = std::sin(half_yaw);
210 const float half_pitch = _pitch * .5f;
211 const float c2 = std::cos(half_pitch);
212 const float s2 = std::sin(half_pitch);
213 const float half_roll = _roll * .5f;
214 const float c3 = std::cos(half_roll);
215 const float s3 = std::sin(half_roll);
216 const float c1c2 = c1 * c2;
217 const float s1s2 = s1 * s2;
218 return Quaternion(c1c2 * s3 + s1s2 * c3, s1 * c2 * c3 + c1 * s2 * s3,
219 c1 * s2 * c3 - s1 * c2 * s3, c1c2 * c3 - s1s2 * s3);
220}
221
222// Returns to an Euler representation of quaternion _q.
223// Quaternion _q does not require to be normalized.
224OZZ_INLINE Float3 ToEuler(const Quaternion& _q) {
225 const float sqw = _q.w * _q.w;
226 const float sqx = _q.x * _q.x;
227 const float sqy = _q.y * _q.y;
228 const float sqz = _q.z * _q.z;
229 // If normalized is one, otherwise is correction factor.
230 const float unit = sqx + sqy + sqz + sqw;
231 const float test = _q.x * _q.y + _q.z * _q.w;
232 Float3 euler;
233 if (test > .499f * unit) { // Singularity at north pole
234 euler.x = 2.f * std::atan2(_q.x, _q.w);
235 euler.y = ozz::math::kPi_2;
236 euler.z = 0;
237 } else if (test < -.499f * unit) { // Singularity at south pole
238 euler.x = -2 * std::atan2(_q.x, _q.w);
239 euler.y = -kPi_2;
240 euler.z = 0;
241 } else {
242 euler.x = std::atan2(2.f * _q.y * _q.w - 2.f * _q.x * _q.z,
243 sqx - sqy - sqz + sqw);
244 euler.y = std::asin(2.f * test / unit);
245 euler.z = std::atan2(2.f * _q.x * _q.w - 2.f * _q.y * _q.z,
246 -sqx + sqy - sqz + sqw);
247 }
248 return euler;
249}
250
251OZZ_INLINE Quaternion Quaternion::FromVectors(const Float3& _from,
252 const Float3& _to) {
253 // http://lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final
254
255 const float norm_from_norm_to = std::sqrt(LengthSqr(_from) * LengthSqr(_to));
256 if (norm_from_norm_to < 1.e-5f) {
257 return Quaternion::identity();
258 }
259 const float real_part = norm_from_norm_to + Dot(_from, _to);
260 Quaternion quat;
261 if (real_part < 1.e-6f * norm_from_norm_to) {
262 // If _from and _to are exactly opposite, rotate 180 degrees around an
263 // arbitrary orthogonal axis. Axis normalization can happen later, when we
264 // normalize the quaternion.
265 quat = std::abs(_from.x) > std::abs(_from.z)
266 ? Quaternion(-_from.y, _from.x, 0.f, 0.f)
267 : Quaternion(0.f, -_from.z, _from.y, 0.f);
268 } else {
269 const Float3 cross = Cross(_from, _to);
270 quat = Quaternion(cross.x, cross.y, cross.z, real_part);
271 }
272 return Normalize(quat);
273}
274
275OZZ_INLINE Quaternion Quaternion::FromUnitVectors(const Float3& _from,
276 const Float3& _to) {
277 assert(IsNormalized(_from) && IsNormalized(_to) &&
278 "Input vectors must be normalized.");
279
280 // http://lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final
281 const float real_part = 1.f + Dot(_from, _to);
282 if (real_part < 1.e-6f) {
283 // If _from and _to are exactly opposite, rotate 180 degrees around an
284 // arbitrary orthogonal axis.
285 // Normalisation isn't needed, as from is already.
286 return std::abs(_from.x) > std::abs(_from.z)
287 ? Quaternion(-_from.y, _from.x, 0.f, 0.f)
288 : Quaternion(0.f, -_from.z, _from.y, 0.f);
289 } else {
290 const Float3 cross = Cross(_from, _to);
291 return Normalize(Quaternion(cross.x, cross.y, cross.z, real_part));
292 }
293}
294
295// Returns the dot product of _a and _b.
296OZZ_INLINE float Dot(const Quaternion& _a, const Quaternion& _b) {
297 return _a.x * _b.x + _a.y * _b.y + _a.z * _b.z + _a.w * _b.w;
298}
299
300// Returns the linear interpolation of quaternion _a and _b with coefficient
301// _f.
302OZZ_INLINE Quaternion Lerp(const Quaternion& _a, const Quaternion& _b,
303 float _f) {
304 return Quaternion((_b.x - _a.x) * _f + _a.x, (_b.y - _a.y) * _f + _a.y,
305 (_b.z - _a.z) * _f + _a.z, (_b.w - _a.w) * _f + _a.w);
306}
307
308// Returns the linear interpolation of quaternion _a and _b with coefficient
309// _f. _a and _n must be from the same hemisphere (aka dot(_a, _b) >= 0).
310OZZ_INLINE Quaternion NLerp(const Quaternion& _a, const Quaternion& _b,
311 float _f) {
312 const Float4 lerp((_b.x - _a.x) * _f + _a.x, (_b.y - _a.y) * _f + _a.y,
313 (_b.z - _a.z) * _f + _a.z, (_b.w - _a.w) * _f + _a.w);
314 const float sq_len =
315 lerp.x * lerp.x + lerp.y * lerp.y + lerp.z * lerp.z + lerp.w * lerp.w;
316 const float inv_len = 1.f / std::sqrt(sq_len);
317 return Quaternion(lerp.x * inv_len, lerp.y * inv_len, lerp.z * inv_len,
318 lerp.w * inv_len);
319}
320
321// Returns the spherical interpolation of quaternion _a and _b with
322// coefficient _f.
323OZZ_INLINE Quaternion SLerp(const Quaternion& _a, const Quaternion& _b,
324 float _f) {
325 assert(IsNormalized(_a));
326 assert(IsNormalized(_b));
327 // Calculate angle between them.
328 float cos_half_theta = _a.x * _b.x + _a.y * _b.y + _a.z * _b.z + _a.w * _b.w;
329
330 // If _a=_b or _a=-_b then theta = 0 and we can return _a.
331 if (std::abs(cos_half_theta) >= .999f) {
332 return _a;
333 }
334
335 // Calculate temporary values.
336 const float half_theta = std::acos(cos_half_theta);
337 const float sin_half_theta = std::sqrt(1.f - cos_half_theta * cos_half_theta);
338
339 // If theta = pi then result is not fully defined, we could rotate around
340 // any axis normal to _a or _b.
341 if (sin_half_theta < .001f) {
342 return Quaternion((_a.x + _b.x) * .5f, (_a.y + _b.y) * .5f,
343 (_a.z + _b.z) * .5f, (_a.w + _b.w) * .5f);
344 }
345
346 const float ratio_a = std::sin((1.f - _f) * half_theta) / sin_half_theta;
347 const float ratio_b = std::sin(_f * half_theta) / sin_half_theta;
348
349 // Calculate Quaternion.
350 return Quaternion(
351 ratio_a * _a.x + ratio_b * _b.x, ratio_a * _a.y + ratio_b * _b.y,
352 ratio_a * _a.z + ratio_b * _b.z, ratio_a * _a.w + ratio_b * _b.w);
353}
354
355// Computes the transformation of a Quaternion and a vector _v.
356// This is equivalent to carrying out the quaternion multiplications:
357// _q.conjugate() * (*this) * _q
358OZZ_INLINE Float3 TransformVector(const Quaternion& _q, const Float3& _v) {
359 // http://www.neil.dantam.name/note/dantam-quaternion.pdf
360 // _v + 2.f * cross(_q.xyz, cross(_q.xyz, _v) + _q.w * _v);
361 const Float3 a(_q.y * _v.z - _q.z * _v.y + _v.x * _q.w,
362 _q.z * _v.x - _q.x * _v.z + _v.y * _q.w,
363 _q.x * _v.y - _q.y * _v.x + _v.z * _q.w);
364 const Float3 b(_q.y * a.z - _q.z * a.y, _q.z * a.x - _q.x * a.z,
365 _q.x * a.y - _q.y * a.x);
366 return Float3(_v.x + b.x + b.x, _v.y + b.y + b.y, _v.z + b.z + b.z);
367}
368} // namespace math
369} // namespace ozz
370#endif // OZZ_OZZ_BASE_MATHS_QUATERNION_H_
GLM_FUNC_QUALIFIER vec< 3, T, Q > cross(vec< 3, T, Q > const &x, vec< 3, T, Q > const &y)
Definition func_geometric.inl:175
GLM_FUNC_DECL qua< T, Q > lerp(qua< T, Q > const &x, qua< T, Q > const &y, T a)
Definition quaternion_common.inl:29
qua< float, defaultp > quat
Quaternion of single-precision floating-point numbers.
Definition quaternion_float.hpp:35
GLM_FUNC_DECL T angle(qua< T, Q > const &x)
Definition quaternion_trigonometric.inl:6
GLM_FUNC_DECL GLM_CONSTEXPR genType euler()
Definition constants.inl:108
Definition vec_float.h:67
Definition quaternion.h:41