16 #ifndef MATHFU_QUATERNION_H_
17 #define MATHFU_QUATERNION_H_
20 #if !defined(_USE_MATH_DEFINES)
21 #define _USE_MATH_DEFINES // For M_PI.
22 #endif // !defined(_USE_MATH_DEFINES)
66 inline Quaternion(
const T& s1,
const T& s2,
const T& s3,
const T& s4) {
88 inline const T&
scalar()
const {
return s_; }
141 axis.Normalized() *
static_cast<T
>(sin(0.5f * angle)));
162 T scale = (1 / length);
185 *axis = s_ > 0 ? v_ : -v_;
186 *angle = 2 * atan2(axis->Normalize(), s_ > 0 ? s_ : -s_);
195 T cos2 = m[0] * m[0] + m[1] * m[1];
199 m[2] < 0 ?
static_cast<T
>(0.5 * M_PI) : static_cast<T>(-0.5 * M_PI),
200 -std::atan2(m[3], m[4]));
203 std::atan2(-m[2], std::sqrt(cos2)),
204 std::atan2(m[1], m[0]));
212 const T x2 = v_[0] * v_[0], y2 = v_[1] * v_[1], z2 = v_[2] * v_[2];
213 const T sx = s_ * v_[0], sy = s_ * v_[1], sz = s_ * v_[2];
214 const T xz = v_[0] * v_[2], yz = v_[1] * v_[2], xy = v_[0] * v_[1];
215 return Matrix<T, 3>(1 - 2 * (y2 + z2), 2 * (xy + sz), 2 * (xz - sy),
216 2 * (xy - sz), 1 - 2 * (x2 + z2), 2 * (sx + yz),
217 2 * (sy + xz), 2 * (yz - sx), 1 - 2 * (x2 + y2));
224 const T x2 = v_[0] * v_[0], y2 = v_[1] * v_[1], z2 = v_[2] * v_[2];
225 const T sx = s_ * v_[0], sy = s_ * v_[1], sz = s_ * v_[2];
226 const T xz = v_[0] * v_[2], yz = v_[1] * v_[2], xy = v_[0] * v_[1];
227 return Matrix<T, 4>(1 - 2 * (y2 + z2), 2 * (xy + sz), 2 * (xz - sy), 0.0f,
228 2 * (xy - sz), 1 - 2 * (x2 + z2), 2 * (sx + yz), 0.0f,
229 2 * (sy + xz), 2 * (yz - sx), 1 - 2 * (x2 + y2), 0.0f,
230 0.0f, 0.0f, 0.0f, 1.0f);
239 const T halfAngle =
static_cast<T
>(0.5) * angle;
243 localAxis.Normalized() *
static_cast<T
>(sin(halfAngle)));
252 const Vector<T, 3> halfAngles(static_cast<T>(0.5) * angles[0],
253 static_cast<T>(0.5) * angles[1],
254 static_cast<T>(0.5) * angles[2]);
255 const T sinx = std::sin(halfAngles[0]);
256 const T cosx = std::cos(halfAngles[0]);
257 const T siny = std::sin(halfAngles[1]);
258 const T cosy = std::cos(halfAngles[1]);
259 const T sinz = std::sin(halfAngles[2]);
260 const T cosz = std::cos(halfAngles[2]);
261 return Quaternion<T>(cosx * cosy * cosz + sinx * siny * sinz,
262 sinx * cosy * cosz - cosx * siny * sinz,
263 cosx * siny * cosz + sinx * cosy * sinz,
264 cosx * cosy * sinz - sinx * siny * cosz);
272 const T trace = m(0, 0) + m(1, 1) + m(2, 2);
274 const T s = sqrt(trace + 1) * 2;
275 const T oneOverS = 1 / s;
276 return Quaternion<T>(
static_cast<T
>(0.25) * s, (m[5] - m[7]) * oneOverS,
277 (m[6] - m[2]) * oneOverS, (m[1] - m[3]) * oneOverS);
278 }
else if (m[0] > m[4] && m[0] > m[8]) {
279 const T s = sqrt(m[0] - m[4] - m[8] + 1) * 2;
280 const T oneOverS = 1 / s;
281 return Quaternion<T>((m[5] - m[7]) * oneOverS, static_cast<T>(0.25) * s,
282 (m[3] + m[1]) * oneOverS, (m[6] + m[2]) * oneOverS);
283 }
else if (m[4] > m[8]) {
284 const T s = sqrt(m[4] - m[0] - m[8] + 1) * 2;
285 const T oneOverS = 1 / s;
286 return Quaternion<T>((m[6] - m[2]) * oneOverS, (m[3] + m[1]) * oneOverS,
287 static_cast<T
>(0.25) * s, (m[5] + m[7]) * oneOverS);
289 const T s = sqrt(m[8] - m[0] - m[4] + 1) * 2;
290 const T oneOverS = 1 / s;
291 return Quaternion<T>((m[1] - m[3]) * oneOverS, (m[6] + m[2]) * oneOverS,
292 (m[5] + m[7]) * oneOverS, static_cast<T>(0.25) * s);
302 const T trace = m(0, 0) + m(1, 1) + m(2, 2);
304 const T s = sqrt(trace + 1) * 2;
305 const T oneOverS = 1 / s;
306 return Quaternion<T>(
static_cast<T
>(0.25) * s, (m[6] - m[9]) * oneOverS,
307 (m[8] - m[2]) * oneOverS, (m[1] - m[4]) * oneOverS);
308 }
else if (m[0] > m[5] && m[0] > m[10]) {
309 const T s = sqrt(m[0] - m[5] - m[10] + 1) * 2;
310 const T oneOverS = 1 / s;
311 return Quaternion<T>((m[6] - m[9]) * oneOverS, static_cast<T>(0.25) * s,
312 (m[4] + m[1]) * oneOverS, (m[8] + m[2]) * oneOverS);
313 }
else if (m[5] > m[10]) {
314 const T s = sqrt(m[5] - m[0] - m[10] + 1) * 2;
315 const T oneOverS = 1 / s;
316 return Quaternion<T>((m[8] - m[2]) * oneOverS, (m[4] + m[1]) * oneOverS,
317 static_cast<T
>(0.25) * s, (m[6] + m[9]) * oneOverS);
319 const T s = sqrt(m[10] - m[0] - m[5] + 1) * 2;
320 const T oneOverS = 1 / s;
321 return Quaternion<T>((m[1] - m[4]) * oneOverS, (m[8] + m[2]) * oneOverS,
322 (m[6] + m[9]) * oneOverS, static_cast<T>(0.25) * s);
348 q1.v_ * (1 - s1) + q2.v_ * s1);
349 return q1 * ((q1.
Inverse() * q2) * s1);
358 if (i == 0)
return s_;
382 Vector<T, 3>(static_cast<T>(1), static_cast<T>(0), static_cast<T>(0)),
387 if (axis.LengthSquared() <
static_cast<T
>(0.05)) {
389 Vector<T, 3>(static_cast<T>(0), static_cast<T>(1), static_cast<T>(0)),
413 if (dot_product >= static_cast<T>(0.99999847691)) {
418 if (dot_product <= static_cast<T>(-0.99999847691)) {
425 return Quaternion<T>(
static_cast<T
>(1.0) + dot_product, cross_product)
507 if (dot_product >= static_cast<T>(0.99999847691)) {
512 if (dot_product <= static_cast<T>(-0.99999847691)) {
519 return Quaternion<T>(
static_cast<T
>(1.0) + dot_product, cross_product)
533 template <
typename T>
556 #endif // MATHFU_QUATERNION_H_
Definition: vector_3.h:24
Vector of d elements with type T.
Definition: vector.h:63
static Quaternion< T > RotateFromTo(const Vector< T, 3 > &v1, const Vector< T, 3 > &v2)
Returns the a Quaternion that rotates from start to end.
Definition: quaternion.h:497
#define MATHFU_DEFINE_CLASS_SIMD_AWARE_NEW_DELETE
Macro which defines the new and delete for MathFu classes.
Definition: utilities.h:600
Quaternion(const T &s1, const Vector< T, 3 > &v1)
Construct a quaternion from a scalar and 3-dimensional Vector.
Definition: quaternion.h:75
T & operator[](const int i)
Access an element of the quaternion.
Definition: quaternion.h:357
static Quaternion< T > FromEulerAngles(const Vector< T, 3 > &angles)
Create a quaternion from 3 euler angles.
Definition: quaternion.h:251
Matrix stores a set of "rows" by "columns" elements of type T and provides functions that operate on ...
Definition: matrix.h:147
static Quaternion< T > RotateFromToWithAxis(const Vector< T, 3 > &v1, const Vector< T, 3 > &v2, const Vector< T, 3 > &preferred_axis)
Returns the a Quaternion that rotates from start to end.
Definition: quaternion.h:402
Quaternion< T > operator*(const T &s1) const
Multiply this Quaternion by a scalar.
Definition: quaternion.h:135
Quaternion(const T &s1, const T &s2, const T &s3, const T &s4)
Construct a Quaternion using scalar values to initialize each element.
Definition: quaternion.h:66
void ToAngleAxis(T *angle, Vector< T, 3 > *axis) const
Convert this Quaternion to an Angle and axis.
Definition: quaternion.h:184
Quaternion(const Quaternion< T > &q)
Construct a Quaternion from a copy.
Definition: quaternion.h:54
Quaternion< T > operator*(const T &s, const Quaternion< T > &q)
Multiply a Quaternion by a scalar.
Definition: quaternion.h:550
static Quaternion< T > Slerp(const Quaternion< T > &q1, const Quaternion< T > &q2, const T &s1)
Calculate the spherical linear interpolation between two Quaternions.
Definition: quaternion.h:344
const T & operator[](const int i) const
Access an element of the quaternion.
Definition: quaternion.h:366
Quaternion< T > operator*(const Quaternion< T > &q) const
Multiply this Quaternion with another Quaternion.
Definition: quaternion.h:124
Vector< T, 3 > & vector()
Return the vector component of the quaternion.
Definition: quaternion.h:98
static Quaternion< T > FromAngleAxis(const T &angle, const Vector< T, 3 > &axis)
Create a Quaternion from an angle and axis.
Definition: quaternion.h:238
static T DotProduct(const Vector< T, d > &v1, const Vector< T, d > &v2)
Calculate the dot product of two vectors.
Definition: vector.h:426
const Vector< T, 3 > & vector() const
Return the vector component of the quaternion.
Definition: quaternion.h:103
void set_scalar(const T &s)
Set the scalar component of the quaternion.
Definition: quaternion.h:93
static Quaternion< T > identity
Contains a quaternion doing the identity transform.
Definition: quaternion.h:524
Quaternion< T > Inverse() const
Calculate the inverse Quaternion.
Definition: quaternion.h:116
Matrix class and functions.
T Normalize()
Normalize this quaterion (in-place).
Definition: quaternion.h:160
Quaternion< T > Normalized() const
Calculate the normalized version of this quaternion.
Definition: quaternion.h:171
const T & scalar() const
Return the scalar component of the quaternion.
Definition: quaternion.h:88
Vector class and functions.
Matrix< T, 4 > ToMatrix4() const
Convert to a 4x4 Matrix.
Definition: quaternion.h:223
Stores a Quaternion of type T and provides a set of utility operations on each Quaternion.
Definition: quaternion.h:47
static Quaternion< T > FromMatrix(const Matrix< T, 3 > &m)
Create a quaternion from a rotation Matrix.
Definition: quaternion.h:271
Quaternion()
Construct an uninitialized Quaternion.
Definition: quaternion.h:50
Vector< T, 3 > operator*(const Vector< T, 3 > &v1) const
Multiply a Vector by this Quaternion.
Definition: quaternion.h:151
void set_vector(const Vector< T, 3 > &v)
Set the vector component of the quaternion.
Definition: quaternion.h:108
Matrix< T, 3 > ToMatrix() const
Convert to a 3x3 Matrix.
Definition: quaternion.h:211
Vector< T, 3 > ToEulerAngles() const
Convert this Quaternion to 3 Euler Angles.
Definition: quaternion.h:193
T & scalar()
Return the scalar component of the quaternion.
Definition: quaternion.h:83
static Vector< T, 3 > CrossProduct(const Vector< T, 3 > &v1, const Vector< T, 3 > &v2)
Calculate the cross product of two vectors.
Definition: vector.h:446
static Quaternion< T > FromMatrix(const Matrix< T, 4 > &m)
Create a quaternion from the upper-left 3x3 roation Matrix of a 4x4 Matrix.
Definition: quaternion.h:301
static Vector< T, 3 > PerpendicularVector(const Vector< T, 3 > &v)
Returns a vector that is perpendicular to the supplied vector.
Definition: quaternion.h:376
static T DotProduct(const Quaternion< T > &q1, const Quaternion< T > &q2)
Calculate the dot product of two Quaternions.
Definition: quaternion.h:331