Efficient implementation of a quaternion.
Quaternions are a superior way to deal with rotations and orientations. This quaternion consists of 4 statically allocated elements, where the first element represents the real part and the three other elements represent the three imaginary parts. The naming convention of the elements is as following:
\f[\left(\begin{array}{*{4}{c}} r & i & j & k \\ \end{array}\right)\f]
These elements can be accessed directly with the subscript operator. The numbering of the quaternion elements is
\f[\left(\begin{array}{*{4}{c}} 0 & 1 & 2 & 3 \\ \end{array}\right)\f]
Note: The Quaternion class can only be instantiated for non-cv-qualified floating point types! Therefore the only possible Quaternion instantiations are
The attempt to create a quaternion with an integral data type results in a compile time error.
#include <Types.h>
Public Types | |
using | ElementType = Type |
Type of the quaternion elements. More... | |
Public Member Functions | |
template<typename Other > | |
Quaternion< Type > & | operator= (const Quaternion< Other > &rhs) |
Assignment operator for different Quaternion instances. More... | |
Constructors | |
Quaternion ()=default | |
Quaternion (Type r, Type i, Type j, Type k) | |
Constructor for a direct initialization of all quaternion elements. More... | |
template<typename Axis > | |
Quaternion (Vector3< Axis > axis, Type angle) | |
Constructor for a quaternion depending on a rotation axis and angle. More... | |
Quaternion (Type xangle, Type yangle, Type zangle) | |
Constructor for a quaternion rotated by the Euler angles xangle, yangle and zangle. More... | |
template<typename Other > | |
Quaternion (const Vector3< Other > &euler) | |
Constructor for a quaternion rotated by the Euler angles euler. More... | |
Quaternion (const Quaternion &q)=default | |
template<typename Other > | |
Quaternion (const Quaternion< Other > &q) | |
Conversion constructor from different Quaternion instances. More... | |
Operators | |
Quaternion & | operator= (const Quaternion &rhs)=default |
template<typename Other > | |
Quaternion & | operator= (const Quaternion< Other > &rhs) |
Type | operator[] (size_t index) const |
Subscript operator for the direct access to the quaternion elements. More... | |
Utility functions | |
Quaternion & | set (Type r, Type i, Type j, Type k) |
Setting the value of the quaternion elements. More... | |
void | reset () |
Reset to the default initial values. More... | |
Quaternion & | invert () |
Inversion of the quaternion ( \( \hat{q} = \hat{q}^{-1} \)). More... | |
const Quaternion | getInverse () const |
Calculation of the inverse quaternion ( \( \hat{q} = \hat{p}^{-1} \)). More... | |
const Matrix3< Type > | toRotationMatrix () const |
Conversion to a rotation matrix. More... | |
Type | getAngle () const |
Calculation of the inherent rotation angle. More... | |
const Vector3< Type > | getAxis () const |
Calculation of the inherent rotation axis. More... | |
void | rotateX (Type angle) |
Rotating the quaternion around the global x-axis by angle degrees (radian measure). More... | |
void | rotateY (Type angle) |
Rotating the quaternion around the global y-axis by angle degrees (radian measure). More... | |
void | rotateZ (Type angle) |
Rotating the quaternion around the global z-axis by angle degrees (radian measure). More... | |
void | swap (Quaternion &q) |
Swapping the contents of two quaternions. More... | |
const Vector3< Type > | getEulerAnglesXYZ () const |
Returns the euler angles in xyz order. More... | |
Type * | data () |
const Type * | data () const |
Math functions | |
The return type of the math functions depends on the involved data types of the quaternions, matrices and vectors (for further detail see the MathTrait class description). | |
template<typename Other > | |
const Vector3< typename MathTrait< Type, Other >::MultType > | rotate (const Vector3< Other > &v) const |
Rotation of a vector v ( \( \vec{rot} = \hat{q} \cdot \vec{v} \cdot \hat{q}^{-1} \)). More... | |
template<typename Other > | |
const Matrix3< typename MathTrait< Type, Other >::MultType > | rotate (const Matrix3< Other > &m) const |
Rotation of a matrix. More... | |
template<typename Other > | |
const Matrix3< typename MathTrait< Type, Other >::MultType > | diagRotate (const Matrix3< Other > &m) const |
Rotation of a diagonal matrix. More... | |
template<typename Other > | |
MathTrait< Type, Other >::HighType | calcAngle (const Vector3< Other > &axis) const |
Returns the angle in radian measure between the quaternion and a given axis. More... | |
Private Attributes | |
Member variables | |
Type | v_ [4] = {Type(1), Type(0), Type(0), Type(0)} |
The four statically allocated quaternion elements. More... | |
using walberla::math::Quaternion< Type >::ElementType = Type |
Type of the quaternion elements.
|
inlineexplicitdefault |
|
inlineexplicit |
Constructor for a direct initialization of all quaternion elements.
r | The initial value for the real part. |
i | The initial value for the first imaginary part. |
j | The initial value for the second imaginary part. |
k | The initial value for the third imaginary part. |
The initial values for the quaternion have to be chosen such that the length of the quaternion is 1.
|
inlineexplicit |
Constructor for a quaternion depending on a rotation axis and angle.
axis | The rotation axis. |
angle | The rotation angle (radian measure). |
This constructor creates a quaternion from the rotation axis axis and the rotation angle angle. axis may be an arbitrary, non-zero vector of any length. However, it is allowed to use the zero vector (0,0,0) in combination with an angle of 0. This combination results in a default quaternion
\f[ \left(\begin{array}{c} 1 \\ 0 \\ 0 \\ 0 \end{array}\right) \f]
|
inlineexplicit |
Constructor for a quaternion rotated by the Euler angles xangle, yangle and zangle.
xangle | Rotation around the x-axis (radian measure). |
yangle | Rotation around the y-axis (radian measure). |
zangle | Rotation around the z-axis (radian measure). |
This constructor creates a quaternion rotated by the Euler angles xangle, yangle and zangle (all in radian measure). The rotations are applied in the order x, y, and z.
|
inlineexplicit |
Constructor for a quaternion rotated by the Euler angles euler.
euler | 3-dimensional vector of the three rotation angles (radian measure). |
This constructor creates a quaternion rotated by the Euler angles euler (all components in radian measure). The rotations are applied in the order x, y, and z.
|
inlinedefault |
|
inline |
Conversion constructor from different Quaternion instances.
q | Quaternion to be copied. |
|
inline |
Returns the angle in radian measure between the quaternion and a given axis.
axis | The given axis. |
|
inline |
|
inline |
|
inline |
Rotation of a diagonal matrix.
m | The diagonal matrix to be rotated. |
The DiagRotate function is a special case of the rotate function. The matrix is assumed to be a diagonal matrix, which reduces the number of floating point operations of the rotation. The function is selected for matrices of different data type (in case Type and Other are supported by the MathTrait class). The function returns a matrix of the higher-order data type of the two involved data types.
|
inline |
Calculation of the inherent rotation angle.
|
inline |
Calculation of the inherent rotation axis.
If the angle is (close to) zero, the return value is set to (1,0,0)
|
inline |
Returns the euler angles in xyz order.
|
inline |
Calculation of the inverse quaternion ( \( \hat{q} = \hat{p}^{-1} \)).
|
inline |
Inversion of the quaternion ( \( \hat{q} = \hat{q}^{-1} \)).
|
inlinedefault |
|
inline |
|
inline |
Assignment operator for different Quaternion instances.
rhs | Quaternion to be copied. |
|
inline |
Subscript operator for the direct access to the quaternion elements.
index | Access index. The index has to be in the range \([0..3]\). |
When compiled in Debug mode, this operator performs an index check.
|
inline |
Reset to the default initial values.
This function resets the quaternion to the default initial values. The real part of the quaternion is reset to 1, whereas the imaginary parts are reset to 0:
\f[\left(\begin{array}{*{4}{c}} 1 & 0 & 0 & 0 \\ \end{array}\right)\f]
|
inline |
Rotation of a matrix.
m | The matrix to be rotated. |
The function is selected for matrices of different data type (in case Type and Other are supported by the MathTrait class). The function returns a matrix of the higher-order data type of the two involved data types.
|
inline |
Rotation of a vector v ( \( \vec{rot} = \hat{q} \cdot \vec{v} \cdot \hat{q}^{-1} \)).
v | The vector to be rotated. |
The function is selected for vectors of different data type (in case Type and Other are supported by the MathTrait class). The function returns a vector of the higher-order data type of the two involved data types.
|
inline |
Rotating the quaternion around the global x-axis by angle degrees (radian measure).
angle | The rotation angle (radian measure). |
|
inline |
Rotating the quaternion around the global y-axis by angle degrees (radian measure).
angle | The rotation angle (radian measure). |
|
inline |
Rotating the quaternion around the global z-axis by angle degrees (radian measure).
angle | The rotation angle (radian measure). |
|
inline |
Setting the value of the quaternion elements.
r | The value for the real part. |
i | The value for the first imaginary part. |
j | The value for the second imaginary part. |
k | The value for the third imaginary part. |
|
inline |
Swapping the contents of two quaternions.
q | The quaternion to be swapped. |
no-throw | guarantee. |
|
inline |
Conversion to a rotation matrix.
|
private |
The four statically allocated quaternion elements.
Access to the quaternion values is gained via the subscript operator. The order of the elements is
\[\left(\begin{array}{*{4}{c}} 0 & 1 & 2 & 3 \\ \end{array}\right)\]