UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
Utils::Quaternion< T > Class Template Reference

Implement some operationn on quaternion. More...

#include <Quaternion.hxx>

Public Types

using real_type = T
 

Public Member Functions

 Quaternion ()
 
 Quaternion (real_type A, real_type B, real_type C, real_type D)
 
void setup (real_type A, real_type B, real_type C, real_type D)
 
void print (ostream_type &os) const
 
real_type operator[] (int i) const
 
void conj ()
 
void invert ()
 
real_type norm () const
 
void rotate (real_type const v[3], real_type w[3]) const
 
real_type to_axis (real_type axis[3]) const
 
void to_matrix (real_type mat[3][3]) const
 

Detailed Description

template<typename T>
class Utils::Quaternion< T >

Implement some operationn on quaternion.

Quaternion Class

Represents a quaternion, which is a mathematical entity that extends complex numbers. A quaternion is defined as a quadruplet \( (A, B, C, D) \) of real numbers, often represented as:

\[ Q = A + B \mathbf{i} + C \mathbf{j} + D \mathbf{k} \]

Quaternions are widely used in 3D computer graphics and robotics for representing rotations and orientations due to their efficiency and ability to avoid gimbal lock.

Reference:

  • James Foley, Andries van Dam, Steven Feiner, John Hughes, Computer Graphics: Principles and Practice, Second Edition, Addison Wesley, 1990.

Member Typedef Documentation

◆ real_type

template<typename T>
using Utils::Quaternion< T >::real_type = T

Type definition for the real number type of the quaternion.

Constructor & Destructor Documentation

◆ Quaternion() [1/2]

template<typename T>
Utils::Quaternion< T >::Quaternion ( )
inline

Default constructor that initializes a null quaternion.

◆ Quaternion() [2/2]

template<typename T>
Utils::Quaternion< T >::Quaternion ( real_type A,
real_type B,
real_type C,
real_type D )
inline

Constructs a quaternion with specified components.

Parameters
AThe scalar part of the quaternion.
BThe coefficient for \( \mathbf{i} \).
CThe coefficient for \( \mathbf{j} \).
DThe coefficient for \( \mathbf{k} \).

Member Function Documentation

◆ conj()

template<typename T>
void Utils::Quaternion< T >::conj ( )
inline

Conjugates the quaternion:

\[ Q = A + B \mathbf{i} + C \mathbf{j} + D \mathbf{k} \]

The conjugate of \( Q \) is given by:

\[ \overline{Q} = A - B \mathbf{i} - C \mathbf{j} - D \mathbf{k} \]

◆ invert()

template<typename T>
void Utils::Quaternion< T >::invert ( )
inline

Inverts the quaternion:

\[ Q = A + B \mathbf{i} + C \mathbf{j} + D \mathbf{k} \]

The inverse of \( Q \) is calculated as:

\[ Q^{-1} = \dfrac{ A - B \mathbf{i} - C \mathbf{j} - D \mathbf{k}} { A^2 + B^2 + C^2 + D^2 } \]

◆ norm()

template<typename T>
real_type Utils::Quaternion< T >::norm ( ) const
inline

Computes the norm of the quaternion.

The norm of \( Q \) is defined as:

\[ \text{norm}(Q) = \sqrt{A^2 + B^2 + C^2 + D^2} \]

Returns
The computed norm of the quaternion.

◆ operator[]()

template<typename T>
real_type Utils::Quaternion< T >::operator[] ( int i) const
inline

Accesses the \(i\)-th component of the quaternion.

Parameters
iIndex of the quaternion component (0 for A, 1 for B, etc.).
Returns
The value of the specified component.

◆ print()

template<typename T>
void Utils::Quaternion< T >::print ( ostream_type & os) const
inline

Prints the quaternion to the specified output stream.

Parameters
osThe output stream where the quaternion will be printed.

◆ rotate()

template<typename T>
void Utils::Quaternion< T >::rotate ( real_type const v[3],
real_type w[3] ) const
inline

Applies a quaternion rotation to a 3D vector.

If \( Q \) is a unit quaternion representing a rotation of ANGLE radians about the vector AXIS, the result \( W \) of the rotation on the vector \( V \) can be computed as:

\( W = Q * V * \overline{Q} \)

Parameters
vThe input vector to be rotated.
wThe output vector that will hold the result.

◆ setup()

template<typename T>
void Utils::Quaternion< T >::setup ( real_type A,
real_type B,
real_type C,
real_type D )
inline

Sets the components of the quaternion to the specified values.

Parameters
AThe scalar part of the quaternion.
BThe coefficient for \( \mathbf{i} \).
CThe coefficient for \( \mathbf{j} \).
DThe coefficient for \( \mathbf{k} \).

◆ to_axis()

template<typename T>
real_type Utils::Quaternion< T >::to_axis ( real_type axis[3]) const
inline

Converts a rotation from quaternion to axis format in 3D.

A rotation quaternion Q has the form:

\[ Q = A + B \mathbf{i} + C \mathbf{j} + D \mathbf{k} \]

where A, B, C and D are real numbers, and \( \mathbf{i} \), \( \mathbf{j} \), and \( \mathbf{k} \) are to be regarded as symbolic constant basis vectors, similar to the role of the "\f$ \mathbf{i} \f$" in the representation of imaginary numbers.

A is the cosine of half of the angle of rotation. (B,C,D) is a vector pointing in the direction of the axis of rotation. Rotation multiplication and inversion can be carried out using this format and the usual rules for quaternion multiplication and inversion.

Parameters
axisAn array that will be populated with the axis of rotation.
Returns
The angle of rotation in radians.

◆ to_matrix()

template<typename T>
void Utils::Quaternion< T >::to_matrix ( real_type mat[3][3]) const
inline

Converts the quaternion to a 3x3 rotation matrix.

Parameters
matA 3x3 array that will hold the resulting rotation matrix.

The documentation for this class was generated from the following file: