2.2 The Quaternion class

The Quaternion class deals with quaternions. Unit quaternions are used to represent rotations. It is composed of two elements: a scalar s (Real s_) and a vector v (ColumnVector v_) representing a quaternion (see[1]).

q  =  w +  xi+ yj + zk                                    (2.16)
   =  (s,v)                                               (2.17)

An object of this class can be initialize with no parameter (s = 1 and v = 0), from an other unit quaternion, from an angle of rotation around a unit vector, from a rotation matrix, from a quaternion object or from the four components of a quaternion. The constructors does not guarantee that quaternions will be unit.

constructors

Syntax
Quaternion();  
Quaternion(const Quaternion & q);  
Quaternion(const Real angle_in_rad, const ColumnVector & axis);  
Quaternion(const Real s, const Real v1, const Real v2, const Real v3);  
Quaternion(const Matrix & R);  
Quaternion & operator=(const Quaternion & q);

Description

Quaternion object constructors, copy constructor and equal operator.

Return Value

None

operators

Syntax
Quaternion   operator+(const Quaternion & q)const;  
Quaternion   operator-(const Quaternion & q)const;  
Quaternion   operator*(const Quaternion & q)const;  
Quaternion   operator*(const ColumnVector & vec)const;  
Quaternion   operator*(constReal c)const;  
Quaternion   operator/(const Quaternion & q)const;  
Quaternion   operator/(constReal c)const;

Description

The operators +, -, * and for quaternion are implemented. The operators * and will generate unit quaternions only if the quaternions involve are unity.

Return Value

Quaternion

conjugate and inverse

Syntax
Quaternion   conjugate()const;  
Quaternion   i()const;

Description

Compute the conjugate of the quaternion (or the inverse if it’s a unit quaternion). The conjugate is defined as

 *
q   =  w - xi - yj - zk                                   (2.18)
    =  (s,- v)                                            (2.19)

Return Value

Quaternion

exponential and logarithm

Syntax
Quaternion   exp()const;  
Quaternion   Log()const;  
Quaternion   power(const Real t)const;

Description

A unit quaternion can be represented by q = cos(θ) + usin(θ). Euler’s identity for complex numbers generalizes to quaternions exp() = cos(θ) + usin(θ), where exp(x) is replace by exp() and uu is replace by -1. With this identity we obtain the exponential of the quaternion q = (0,θv), where q is not necessary a unit quaternion. It is then possible to define the logarithm and the power of a unit quaternion [2].

Log (q)  =   Log(cos(θ)+ u sin(θ)) = Log(exp(uθ)) = uθ      (2.20)
     t
    q   =   cos(tθ)+ u sin(tθ)                              (2.21)
Log(q) is not necessary a unit quaternion even if q is a unit quaternion.

Return Value

Quaternion for exp, Log

dot_product

Syntax
Real dot_prod(const Quaternion & q)const;

Description

Compute the dot product of quaternions.

Return Value

Real

quaternion time derivative

Syntax
Quaternion   dot(const ColumnVector & w, const short sign)const;  
ReturnMatrix E(const short sign)const;

Description

The quaternion time derivative is obtain from the quaternion propagation law [2].

        1
˙s  =   ---vTw                                             (2.22)
        2
˙v  =   1E (s,v )w                                           (2.23)
       2
where
E = ηI - S (ϵ)  in base frame                             (2.24)
E = ηI + S (ϵ)  in body frame

The choice of reference system (base or body) for w is assign by sign. A value of 1 is for base frame while -1 is for body frame.

Return Value

Quaternion for dot

Matrix for E

unit and norm

Syntax
Quaternion & unit();  
Real norm()const;

Description

unit() makes the quaternion a unit quaternion, norm() computes and returns the norm of the quaternion. norm_sqr() computes and returns the square norm of the quaternion.

Return Value

Quaternion for unit()

Real for norm() and norm_sqr()

s and v

Syntax
Real s()const;  
void set_s(const Real s);  
ReturnMatrix v()const;  
void set_v(const ColumnVector & v);

Description

The functions s() and v() returns one of the components of a quaternion (s or v), while set_s() and set_v() can assign a value to one of the components.

Return Value

None for set_s() and set_v()

Real for s()

Matrix for v()

Rotation matrices

Syntax
ReturnMatrix R() const;  
ReturnMatrix T() const;

Description

Returns a rotation matrix from the quaternion (R() returns a 3 × 3 matrix and T() returns a 4 × 4 matrix).

Return Value

Matrix

Omega, ω

Syntax
ReturnMatrix Omega(const Quaternion & q, const Quaternion & q_dot);

Description

Omega is not a member function of the class Quaternion. The function returned the angular velocity obtain from a quaternion and it’s time derivative. Like the member function dot, it use the quaternions propagation law [2].

Return Value

ColumnVector

Slerp

Syntax
Quaternion Slerp(const Quaternion & q0, const Quaternion & q1,  
                 const Real t);

Description

Slerp stands for Spherical Linear Interpolation. Slerp is not a member function of the class Quaternion. The quaternions q0 and q1 needs to be unit quaternions. It returns a unit quaternion. As the parameter t uniformly varies between 0 and 1, the values q(t) are required to uniformly vary along the circular arc from q0 to q1.

It is customary to choose the sign G on q1 so that q0 Gq1 0 (the angle between q0 and Gq1 is acute). This choice avoids extra spinning caused by the interpolated rotations [2]. For unit quaternions Slerp is defined as

    {       -1  t
q =     q0(-q10 q1) t  if q0 ⋅q1 ≥ 0                          (2.25)
      q0(q0 (- q1))    otherwise

Return Value

Quaternion

Slerp_prime

Syntax
Quaternion Slerp_prime(const Quaternion & q0, const Quaternion & q1,  
                       const Real t);

Description

Slerp_prime represent the Slerp derivative. Slerp_prime is not a member function of the class Quaternion. The quaternions q0 and q1 needs to be unit quaternions. It does not necessary returns a unit quaternion.

It is customary to choose the sign G on q1 so that q0 Gq1 0 (the angle between q0 and Gq1 is acute). This choice avoids extra spinning caused by the interpolated rotations [2]. For unit quaternions Slerp is defined as

    {
        Slerp(q0,q1,t)Log(q-1q1)    if q0 ⋅q1 ≥ 0
q =   Slerp(q ,q ,t)Log (q-01(- q ))  otherwise              (2.26)
             0  1        0    1

Return Value

Quaternion

Squad

Syntax
Quaternion Squad(const Quaternion & p, const Quaternion & a,  
                 const Quaternion & b, const Quaternion & r,  
                 const Real t);

Description

Squad stands for Spherical Cubic Interpolation. Squad is not a member function of the class Quaternion. The quaternions p, a, b and r needs to be unit quaternions. It returns a unit quaternion.

Squad uses an iterative of three slerps. Suppose four quaternions, p, a, b and r as the ordered vertices of quadrilateral. Interpolate c along p to q using slerp and d along a to b also using slerp. Now interpolate q along c to d [2]. Squad is defined as

q = Slerp(Slerp(p,r,t),Slerp(a,b,t),2t(1- t));              (2.27)

Return Value

Quaternion

Squad_prime

Syntax
Quaternion Squad_prime(const Quaternion & p, const Quaternion & a,  
                       const Quaternion & b, const Quaternion & q,  
                       const Real t);

Description

Squad_prime represent the Squad derivative. Squad_prime is not a member function of the class Quaternion.

Return Value

Quaternion