Mathter
A configurable 3D math library for game developers.
QuaternionFunction.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "QuaternionImpl.hpp"
4 
5 namespace mathter {
6 
8 template <class T, bool Packed>
10  return Length(q.vec);
11 }
12 
14 template <class T, bool Packed>
16  return Quaternion<T, Packed>{ q.vec * Vector<T, 4, Packed>{ T(1), T(-1), T(-1), T(-1) } };
17 }
18 
20 template <class T, bool Packed>
22  auto a = q.ScalarPart();
23  auto v = q.VectorPart();
24  T mag = Length(v);
25  T es = exp(a);
26 
27  Quaternion<T, Packed> ret = { std::cos(mag), v * (std::sin(mag) / mag) };
28  ret *= es;
29 
30  return ret;
31 }
32 
34 template <class T, bool Packed>
36  auto magq = Length(q);
37  auto vn = Normalize(q.VectorPart());
38 
39  Quaternion ret = { std::log(magq), vn * std::acos(q.s / magq) };
40  return ret;
41 }
42 
44 template <class T, bool Packed>
46  return Exp(a * Log(q));
47 }
48 
52 template <class T, bool Packed>
54  return LengthSquared(q.vec);
55 }
56 
59 template <class T, bool Packed>
61  return Abs(q);
62 }
63 
65 template <class T, bool Packed>
67  return Quaternion<T, Packed>{ q.vec.Normalized() };
68 }
69 
71 template <class T, bool Packed>
73  return Conjugate(q);
74 }
75 
77 template <class T, bool Packed>
79  return q.vec.IsNormalized();
80 }
81 
82 
83 
84 
85 } // namespace mathter
Matrix< T, Dim, Dim, Order, Layout, Packed > Inverse(const Matrix< T, Dim, Dim, Order, Layout, Packed > &m)
Returns the inverse of the matrix.
Definition: MatrixFunction.hpp:46
Quaternion< T, Packed > Log(const Quaternion< T, Packed > &q)
Natural quaternion logarithm, base e.
Definition: QuaternionFunction.hpp:35
Quaternion< T, Packed > Pow(const Quaternion< T, Packed > &q, T a)
Raises q to the power of a .
Definition: QuaternionFunction.hpp:45
Allows you to do quaternion math and represent rotation in a compact way.
Definition: Definitions.hpp:69
Quaternion< T, Packed > Normalize(const Quaternion< T, Packed > &q)
Returns the unit quaternion of the same direction. Does not change this object.
Definition: QuaternionFunction.hpp:66
T Abs(const Quaternion< T, Packed > &q)
The euclidean length of the vector of the 4 elements of the quaternion.
Definition: QuaternionFunction.hpp:9
Quaternion< T, Packed > Exp(const Quaternion< T, Packed > &q)
Natural quaternion exponentiation, base e.
Definition: QuaternionFunction.hpp:21
const T ScalarPart() const
Returns the scalar part (w) of (w + xi + yj + zk).
Definition: QuaternionImpl.hpp:144
Definition: Approx.hpp:11
const Vector< T, 3, Packed > VectorPart() const
Returns the vector part (x, y, z) of (w + xi + yj + zk).
Definition: QuaternionImpl.hpp:148
T LengthSquared(const Quaternion< T, Packed > &q)
Returns the square of the absolute value.
Definition: QuaternionFunction.hpp:53
Quaternion< T, Packed > Conjugate(const Quaternion< T, Packed > &q)
Negates the imaginary values of the quaternion.
Definition: QuaternionFunction.hpp:15
Vector< T, 4, Packed > vec
Definition: QuaternionImpl.hpp:41
T Length(const Quaternion< T, Packed > &q)
Returns the absolute value of the quaternion.
Definition: QuaternionFunction.hpp:60
T s
Definition: QuaternionImpl.hpp:36
bool IsNormalized(const Quaternion< T, Packed > &q)
Check if the quaternion is a unit quaternion, with some tolerance for floats.
Definition: QuaternionFunction.hpp:78