# Quaternion¶

template <class Real>
class Quaternion

Quaternion representation with basic operations.

Template Parameters
• Real: The precision to be used. Must be a scalar real number type (e.g. float, double).

Public Functions

Quaternion(const Quaternion &q)

copy constructor

Quaternion &operator=(const Quaternion &q)

assignment operator

template <class T>
operator Quaternion<T>() const

conversion to different precision

operator float4() const

conversion to float4 (real part will be stored first, followed by the vector part)

operator double4() const

conversion to double4 (real part will be stored first, followed by the vector part)

Real realPart() const

Return
the real part of the quaternion

Real3 vectorPart() const

Return
the vector part of the quaternion

Quaternion<Real> conjugate() const

Return
the conjugate of the quaternion

Real norm() const

Return
the norm of the quaternion

Quaternion &normalize()

Normalize the current quaternion. Must be non zero.

Quaternion normalized() const

Return
A normalized copy of this Quaternion

Quaternion &operator+=(const Quaternion &q)

Add a quaternion to the current one.

Quaternion &operator-=(const Quaternion &q)

Subtract a quaternion to the current one.

Quaternion &operator*=(Real a)

Scale the current quaternion.

Quaternion &operator*=(const Quaternion &q)

Multiply the current quaternion with another with Quaternion multiplication and store the result in this object.

Real3 rotate(Real3 v) const

Return
The input vector rotated by the current quaternion

Real3 inverseRotate(Real3 v) const

Return
The input vector rotated by the current quaternion inverse

Quaternion timeDerivative(Real3 omega) const

Return
The time derivative of the given angular velocity, useful for time integration of rigid objects

Public Members

Real w

real part

Real x

vector part, x

Real y

vector part, y

Real z

vector part, z

Public Static Functions

static Quaternion createFromComponents(Real w, Real x, Real y, Real z)

Create a Quaternion from components.

static Quaternion createFromComponents(Real w, Real3 v)

Create a Quaternion from real part and vector part.

static Quaternion createFromComponents(Real4 v)

Create a Quaternion from components.

static Quaternion pureVector(Real3 v)

Create a pure vector Quaternion.

static Quaternion createFromRotation(Real angle, Real3 axis)

Create a Quaternion that represents the rotation around an axis with a given angle.

Parameters
• angle: The angle (in radians) of the rotation
• axis: The axis of rotation, must be non zero (or nan will be returned)

static Quaternion createFromVectors(Real3 from, Real3 to)

Create a Quaternion that represents the “shortest” rotation between two vectors.

The vectors must be non zero.

Parameters
• from: The origin vector
• to: The vector obtained by applying the rotation to from

Friends

Quaternion operator+(Quaternion q1, const Quaternion &q2)

Return
The sum of 2 quaternions

Quaternion operator-(Quaternion q1, const Quaternion &q2)

Return
The difference of 2 quaternions

Quaternion operator*(Real a, Quaternion q)

Return
The scalar multiplication of a quaternion

Quaternion operator*(Quaternion q, Real a)

Return
The scalar multiplication of a quaternion

Quaternion operator*(const Quaternion &q1, const Quaternion &q2)

Return
The quaternion product of 2 quaternions