RobWorkProject
Public Member Functions | Friends | Related Functions | List of all members
Quaternion< T > Class Template Reference

A Quaternion $ \mathbf{q}\in \mathbb{R}^4 $ a complex number used to describe rotations in 3-dimensional space. $ q_w+{\bf i}\ q_x+ {\bf j} q_y+ {\bf k}\ q_z $. More...

#include <Quaternion.hpp>

Inherits Rotation3DVector< T >.

Public Member Functions

 Quaternion (T qx, T qy, T qz, T qw)
 Creates a Quaternion. More...
 
 Quaternion (const Quaternion< T > &quat)
 Creates a Quaternion from another Quaternion. More...
 
 Quaternion (const Rotation3D< T > &rot)
 Extracts a Quaternion from Rotation matrix using setRotation(const Rotation3D<R>& rot) More...
 
 Quaternion (const EigenQuaternion &r)
 Creates a Quaternion from a Eigen quaternion. More...
 
void operator= (const EigenQuaternion &r)
 copy a boost quaternion to this Quaternion More...
 
getQx () const
 get method for the x component More...
 
getQy () const
 get method for the y component More...
 
getQz () const
 get method for the z component More...
 
getQw () const
 get method for the w component More...
 
getLength () const
 get length of quaternion $ \sqrt{q_x^2+q_y^2+q_z^2+q_w^2} $ More...
 
getLengthSquared () const
 get squared length of quaternion $ q_x^2+q_y^2+q_z^2+q_w^2 $ More...
 
void normalize ()
 normalizes this quaternion so that $ normalze(Q)=\frac{Q}{\sqrt{q_x^2+q_y^2+q_z^2+q_w^2}} $
 
const Rotation3D< T > toRotation3D () const
 Calculates the $ 3\times 3 $ Rotation matrix. More...
 
size_t size () const
 The dimension of the quaternion (i.e. 4). More...
 
operator() (size_t i) const
 Returns reference to Quaternion element. More...
 
T & operator() (size_t i)
 Returns reference to Quaternion element. More...
 
const Quaternion< T > slerp (const Quaternion< T > &v, const T t) const
 Calculates a slerp interpolation between this and v. More...
 
const Quaternion< T > operator* (T s) const
 Scalar multiplication.
 
const Quaternion< T > operator*= (T s)
 Scalar multiplication.
 
const Quaternion< T > operator*= (const Quaternion< T > &r)
 Multiply with operator.
 
const Quaternion< T > operator* (const Quaternion< T > &r)
 Multiply-from operator.
 
const Quaternion< T > operator+= (const Quaternion< T > &r)
 Add-to operator.
 
const Quaternion< T > operator-= (const Quaternion< T > &r)
 Subtract-from operator.
 
Quaternion< T > operator- () const
 Unary minus.
 
Quaternion< T > operator+ () const
 Unary minus.
 
bool operator== (const Quaternion< T > &r) const
 Comparison (equals) operator.
 
bool operator!= (const Quaternion< T > &r) const
 Comparison (not equals) operator.
 
template<class R >
void setRotation (const Rotation3D< R > &rot)
 Converts a Rotation3D to a Quaternion and saves the Quaternion in this. More...
 
Eigen::Quaternion< T > & e ()
 Convert to an Eigen Quaternion. More...
 
const Eigen::Quaternion< T > & e () const
 Convert to an Eigen Quaternion. More...
 
- Public Member Functions inherited from Rotation3DVector< T >
virtual ~Rotation3DVector ()
 Virtual destructor.
 

Friends

const Quaternion< T > operator* (T s, const Quaternion< T > &v)
 Scalar multiplication.
 
const Quaternion< T > operator+ (const Quaternion< T > &s, const Quaternion< T > &v)
 Addition of two quaternions.
 
const Quaternion< T > operator- (Quaternion< T > s, const Quaternion< T > &v)
 Scalar multiplication.
 

Related Functions

(Note that these are not member functions.)

template<class T >
std::ostream & operator<< (std::ostream &out, const Quaternion< T > &v)
 Streaming operator.
 
template<>
void write (const rw::math::Quaternion< double > &sobject, rw::common::OutputArchive &oarchive, const std::string &id)
 
template<>
void write (const rw::math::Quaternion< float > &sobject, rw::common::OutputArchive &oarchive, const std::string &id)
 
template<>
void read (rw::math::Quaternion< double > &sobject, rw::common::InputArchive &iarchive, const std::string &id)
 
template<>
void read (rw::math::Quaternion< float > &sobject, rw::common::InputArchive &iarchive, const std::string &id)
 

Additional Inherited Members

- Protected Member Functions inherited from Rotation3DVector< T >
 Rotation3DVector (const Rotation3DVector &)
 Copy Constructor. More...
 
Rotation3DVectoroperator= (const Rotation3DVector &)
 Assignment operator is protected to force subclasses to implement it by themself.
 
 Rotation3DVector ()
 Default Constructor.
 

Detailed Description

template<class T = double>
class rw::math::Quaternion< T >

A Quaternion $ \mathbf{q}\in \mathbb{R}^4 $ a complex number used to describe rotations in 3-dimensional space. $ q_w+{\bf i}\ q_x+ {\bf j} q_y+ {\bf k}\ q_z $.

Quaternions can be added and multiplied in a similar way as usual algebraic numbers. Though there are differences. Quaternion multiplication is not commutative which means $Q\cdot P \neq P\cdot Q $

Constructor & Destructor Documentation

◆ Quaternion() [1/4]

Quaternion ( qx,
qy,
qz,
qw 
)
inline

Creates a Quaternion.

Parameters
qx[in] $ q_x $
qy[in] $ q_y $
qz[in] $ q_z $
qw[in] $ q_w $

◆ Quaternion() [2/4]

Quaternion ( const Quaternion< T > &  quat)
inline

Creates a Quaternion from another Quaternion.

Parameters
quat[in] Quaternion

◆ Quaternion() [3/4]

Quaternion ( const Rotation3D< T > &  rot)
inline

Extracts a Quaternion from Rotation matrix using setRotation(const Rotation3D<R>& rot)

Parameters
rot[in] A 3x3 rotation matrix $ \mathbf{rot} $

◆ Quaternion() [4/4]

Quaternion ( const EigenQuaternion< T > &  r)
inline

Creates a Quaternion from a Eigen quaternion.

Parameters
r[in] a boost quaternion

Member Function Documentation

◆ e() [1/2]

Eigen::Quaternion<T>& e ( )
inline

Convert to an Eigen Quaternion.

Returns
Eigen Quaternion representation.

◆ e() [2/2]

const Eigen::Quaternion<T>& e ( ) const
inline

Convert to an Eigen Quaternion.

Returns
Eigen Quaternion representation.

◆ getLength()

T getLength ( ) const
inline

get length of quaternion $ \sqrt{q_x^2+q_y^2+q_z^2+q_w^2} $

Returns
the length og this quaternion

◆ getLengthSquared()

T getLengthSquared ( ) const
inline

get squared length of quaternion $ q_x^2+q_y^2+q_z^2+q_w^2 $

Returns
the length og this quaternion

◆ getQw()

T getQw ( ) const
inline

get method for the w component

Returns
the w component of the quaternion

◆ getQx()

T getQx ( ) const
inline

get method for the x component

Returns
the x component of the quaternion

◆ getQy()

T getQy ( ) const
inline

get method for the y component

Returns
the y component of the quaternion

◆ getQz()

T getQz ( ) const
inline

get method for the z component

Returns
the z component of the quaternion

◆ operator()() [1/2]

T operator() ( size_t  i) const
inline

Returns reference to Quaternion element.

Parameters
i[in] index in the quaternion $i\in \{0,1,2,3\} $
Returns
const reference to element

◆ operator()() [2/2]

T& operator() ( size_t  i)
inline

Returns reference to Quaternion element.

Parameters
i[in] index in the quaternion $i\in \{0,1,2,3\} $
Returns
reference to element

◆ operator=()

void operator= ( const EigenQuaternion< T > &  r)
inline

copy a boost quaternion to this Quaternion

Parameters
r[in] - boost quaternion

◆ setRotation()

void setRotation ( const Rotation3D< R > &  rot)
inline

Converts a Rotation3D to a Quaternion and saves the Quaternion in this.

Parameters
rot[in] A 3x3 rotation matrix $ \mathbf{R} $

$ \begin{array}{c} q_x\\ q_y\\ q_z\\ q_w \end{array} = \left[ \begin{array}{c} \\ \\ \end{array} \right] $

The conversion method is proposed by Henrik Gordon Petersen. The switching between different cases occur well before numerical instabilities, hence the solution should be more robust, than many of the methods proposed elsewhere.

◆ size()

size_t size ( ) const
inline

The dimension of the quaternion (i.e. 4).

This method is provided to help support generic algorithms using size() and operator[].

◆ slerp()

const Quaternion<T> slerp ( const Quaternion< T > &  v,
const T  t 
) const
inline

Calculates a slerp interpolation between this and v.

The slerp interpolation ensures a constant velocity across the interpolation. For $t=0$ the result is this and for $t=1$ it is v.

Note
Algorithm and implementation is thanks to euclideanspace.com

◆ toRotation3D()

const Rotation3D<T> toRotation3D ( ) const
inlinevirtual

Calculates the $ 3\times 3 $ Rotation matrix.

Returns
A 3x3 rotation matrix $ \mathbf{rot} $ $ \mathbf{rot} = \left[ \begin{array}{ccc} 1-2(q_y^2-q_z^2) & 2(q_x\ q_y+q_z\ q_w)& 2(q_x\ q_z-q_y\ q_w) \\ 2(q_x\ q_y-q_z\ q_w) & 1-2(q_x^2-q_z^2) & 2(q_y\ q_z+q_x\ q_w)\\ 2(q_x\ q_z+q_y\ q_w) & 2(q_y\ q_z-q_x\ q_z) & 1-2(q_x^2-q_y^2) \end{array} \right] $

Implements Rotation3DVector< T >.

Friends And Related Function Documentation

◆ read() [1/2]

void read ( rw::math::Quaternion< double > &  sobject,
rw::common::InputArchive iarchive,
const std::string &  id 
)
related

Enable read-serialization of class T by overloading this method. Data is read from iarchive and filled into sobject.

Parameters
sobject[out] the object in which the data should be streamed into
iarchive[in] the InputArchive from which to read data.
id[in] The id of the serialized sobject.
Note
the id can be empty in which case the overloaded method should provide a default identifier. E.g. the Vector3D class defined "Vector3D" as its default id.

◆ read() [2/2]

void read ( rw::math::Quaternion< float > &  sobject,
rw::common::InputArchive iarchive,
const std::string &  id 
)
related

Enable read-serialization of class T by overloading this method. Data is read from iarchive and filled into sobject.

Parameters
sobject[out] the object in which the data should be streamed into
iarchive[in] the InputArchive from which to read data.
id[in] The id of the serialized sobject.
Note
the id can be empty in which case the overloaded method should provide a default identifier. E.g. the Vector3D class defined "Vector3D" as its default id.

◆ write() [1/2]

void write ( const rw::math::Quaternion< double > &  sobject,
rw::common::OutputArchive oarchive,
const std::string &  id 
)
related

Enable write-serialization of class T by overloading this method. Data is written to oarchive from the sobject.

Parameters
sobject[in] the object from which the data should be streamed.
oarchive[out] the OutputArchive in which data should be written.
id[in] The id of the serialized sobject.
Note
the id can be empty in which case the overloaded method should provide a default identifier. E.g. the Vector3D class defined "Vector3D" as its default id.

◆ write() [2/2]

void write ( const rw::math::Quaternion< float > &  sobject,
rw::common::OutputArchive oarchive,
const std::string &  id 
)
related

Enable write-serialization of class T by overloading this method. Data is written to oarchive from the sobject.

Parameters
sobject[in] the object from which the data should be streamed.
oarchive[out] the OutputArchive in which data should be written.
id[in] The id of the serialized sobject.
Note
the id can be empty in which case the overloaded method should provide a default identifier. E.g. the Vector3D class defined "Vector3D" as its default id.

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