======================================================================
=                  Quaternions and spatial rotation                  =
======================================================================

                            Introduction
======================================================================
Unit quaternions, also known as versors, provide a convenient
mathematical notation for representing orientations and rotations of
objects in three dimensions. Compared to Euler angles they are simpler
to compose and avoid the problem of gimbal lock. Compared to rotation
matrices they are more compact, more numerically stable, and more
efficient. Quaternions have applications in computer graphics,
computer vision, robotics, navigation, molecular dynamics, flight
dynamics, orbital mechanics of satellites and  crystallographic
texture analysis.

When used to represent rotation, unit quaternions are also called
rotation quaternions as they represent the 3D rotation group. When
used to represent an orientation (rotation relative to a reference
coordinate system), they are called orientation quaternions or
attitude quaternions. The equation for spatial rotations can be
summarized for  radians about a unit axis (X,Y,Z) as the Quaternion
(C, X*S, Y*S, Z*S) where C = \cos(\theta/2) and S=\sin(\theta/2).


Quaternions
=============
The complex numbers can be defined by introducing an abstract symbol
which satisfies the usual rules of algebra and additionally the rule .
This is sufficient to reproduce all of the rules of complex number
arithmetic: for example:
: (a+b\mathbf{i})(c+d\mathbf{i}) = ac + ad\mathbf{i} + b\mathbf{i}c +
b\mathbf{i}d\mathbf{i} = ac + ad\mathbf{i} + bc\mathbf{i} +
bd\mathbf{i}^2 = (ac - bd) + (bc + ad) \mathbf{i}.

In the same way the quaternions can be defined by introducing abstract
symbols , ,  which satisfy the rules  and the usual algebraic rules
'except' the commutative law of multiplication (a familiar example of
such a noncommutative multiplication is matrix multiplication). From
this all of the rules of quaternion arithmetic follow, such as the
rules on multiplication of quaternion basis elements. Using these
rules, one can show that:
: (a + b\mathbf{i} + c\mathbf{j} + d\mathbf{k}) (e + f\mathbf{i} +
g\mathbf{j} + h\mathbf{k}) =
(ae - bf - cg - dh) + (af + be + ch - dg) \mathbf{i} + (ag - bh + ce
+ df) \mathbf{j} + (ah + bg - cf + de) \mathbf{k}.

The imaginary part b\mathbf{i} + c\mathbf{j} + d\mathbf{k} of a
quaternion behaves like a vector \vec{v} = (b,c,d) in three dimension
vector space, and the real part  behaves like a scalar in . When
quaternions are used in geometry, it is more convenient to define them
as a scalar plus a vector:

: a + b\mathbf{i} + c\mathbf{j} + d\mathbf{k} = a + \vec{v}.

Those who have studied vectors at school might find it strange to add
a 'number' to a 'vector', as they are objects of very different
natures, or to 'multiply' two vectors together, as this operation is
usually undefined. However, if one remembers that it is a mere
notation for the real and imaginary parts of a quaternion, it becomes
more legitimate. In other words, the correct reasoning is the addition
of two quaternions, one with zero vector/imaginary part, and another
one with zero scalar/real part:
: a + \vec{v} = (a, \vec{0}) + (0, \vec{v}).

We can express quaternion multiplication in the modern language of
vector cross and dot products (which were actually inspired by the
quaternions in the first place ). When multiplying the
vector/imaginary parts, in place of the rules  we have the quaternion
multiplication rule:
: \vec{v} \vec{w} = \vec{v} \times \vec{w} - \vec{v} \cdot \vec{w},
where:
* \vec{v} \vec{w} is the resulting quaternion,
* \vec{v} \times \vec{w} is vector cross product (a vector),
* \vec{v} \cdot \vec{w} is vector scalar product (a scalar).

Quaternion multiplication is noncommutative (because of the cross
product, which anti-commutes), while scalar-scalar and scalar-vector
multiplications commute. From these rules it follows immediately that
(see details):

: (s + \vec{v}) (t + \vec{w}) = (s t - \vec{v} \cdot \vec{w}) + (s
\vec{w} + t \vec{v} + \vec{v} \times \vec{w}).

The (left and right) multiplicative inverse or reciprocal of a nonzero
quaternion is given by the conjugate-to-norm ratio (see details):

: (s + \vec{v})^{-1} = \frac{(s + \vec{v})^*}{\lVert s + \vec{v}
\rVert^2} = \frac{s - \vec{v}}{s^2 + \lVert \vec{v} \rVert^2},

as can be verified by direct calculation.


Proof of the quaternion rotation identity
===========================================
Let \vec{u} be a unit vector (the rotation axis) and let q = \cos
\frac{\alpha}{2} + \vec{u} \sin \frac{\alpha}{2}. Our goal is to show
that
:\vec{v'} = q \vec{v} q^{-1} = \left( \cos \frac{\alpha}{2} + \vec{u}
\sin \frac{\alpha}{2} \right) \, \vec{v} \, \left( \cos
\frac{\alpha}{2} - \vec{u} \sin \frac{\alpha}{2} \right)
yields the vector \vec{v} rotated by an angle \alpha around the axis
\vec{u}. Expanding out, we have

:\begin{align}
\vec{v'} &= \vec{v} \cos^2 \frac{\alpha}{2} + (\vec{u}\vec{v} -
\vec{v}\vec{u}) \sin \frac{\alpha}{2} \cos \frac{\alpha}{2} -
\vec{u}\vec{v}\vec{u} \sin^2 \frac{\alpha}{2} \\[6pt]
&= \vec{v} \cos^2 \frac{\alpha}{2} + 2 (\vec{u} \times \vec{v})
\sin \frac{\alpha}{2} \cos \frac{\alpha}{2} - (\vec{v} (\vec{u} \cdot
\vec{u}) - 2 \vec{u} (\vec{u} \cdot \vec{v})) \sin^2 \frac{\alpha}{2}
\\[6pt]
&= \vec{v} (\cos^2 \frac{\alpha}{2} - \sin^2 \frac{\alpha}{2}) +
(\vec{u} \times \vec{v}) (2 \sin \frac{\alpha}{2} \cos
\frac{\alpha}{2}) + \vec{u} (\vec{u} \cdot \vec{v}) (2 \sin^2
\frac{\alpha}{2}) \\[6pt]
&= \vec{v} \cos \alpha + (\vec{u} \times \vec{v}) \sin \alpha +
\vec{u} (\vec{u} \cdot \vec{v}) (1 - \cos \alpha) \\[6pt]
&= (\vec{v} - \vec{u} (\vec{u} \cdot \vec{v})) \cos \alpha +
(\vec{u} \times \vec{v}) \sin \alpha + \vec{u} (\vec{u} \cdot \vec{v})
\\[6pt]
&= \vec{v}_\bot \cos \alpha + (\vec{u} \times \vec{v}) \sin \alpha
+ \vec{v}_{\|}
\end{align}

where \vec{v}_{\bot} and \vec{v}_{\|} are the components of
perpendicular and parallel to  respectively. This is the formula of a
rotation by  around the  axis.


                   Using quaternion as rotations
======================================================================
A visualization of a rotation represented by an Euler axis and angle.
In 3-dimensional space, according to Euler's rotation theorem, any
rotation or sequence of rotations of a rigid body or coordinate system
about a fixed point is equivalent to a single rotation by a given
angle  about a fixed axis (called the 'Euler axis') that runs through
the fixed point. The Euler axis is typically represented by a unit
vector . Therefore, any rotation in three dimensions can be
represented as a combination of a vector   and a scalar . Quaternions
give a simple way to encode this axis-angle representation in four
numbers, and can be used to apply the corresponding rotation to a
position vector, representing a point relative to the origin in R3.

A Euclidean vector such as  or  can be rewritten as  or , where , ,
are unit vectors representing the three Cartesian axes. A rotation
through an angle of  around the axis defined by a unit vector
: \vec{u} = (u_x, u_y, u_z) = u_x\mathbf{i} + u_y\mathbf{j} +
u_z\mathbf{k}
can be represented by a quaternion. This can be done using an
extension of Euler's formula:

:  \mathbf{q} = e^{\frac{\theta}{2}{(u_x\mathbf{i} + u_y\mathbf{j} +
u_z\mathbf{k})}} = \cos \frac{\theta}{2} + (u_x\mathbf{i} +
u_y\mathbf{j} + u_z\mathbf{k}) \sin \frac{\theta}{2}

It can be shown that the desired rotation can be applied to an
ordinary vector \mathbf{p} = (p_x, p_y, p_z) = p_x\mathbf{i} +
p_y\mathbf{j} + p_z\mathbf{k} in 3-dimensional space, considered as a
quaternion with a real coordinate equal to zero, by evaluating the
conjugation of  by :
: \mathbf{p'} = \mathbf{q} \mathbf{p} \mathbf{q}^{-1}
using the Hamilton product, where  is the new position vector of the
point after the rotation. In a programmatic implementation, this is
achieved by constructing a quaternion whose vector part is  and real
part equals zero and then performing the quaternion multiplication.
The vector part of the resulting quaternion is the desired vector .

Mathematically, this operation carries the 'set' of all "pure"
quaternions  (those with real part equal to zero)�which constitute a
3-dimensional space among the quaternions�into itself, by the desired
rotation about the axis 'u', by the angle θ. (Each real quaternion is
carried into itself by this operation. But for the purpose of
rotations in 3-dimensional space, we ignore the real quaternions.)

The rotation is clockwise if our line of sight points in the same
direction as .

In this instance,  is a unit quaternion and
:  \mathbf{q}^{-1} = e^{-\frac{\theta}{2}{(u_x\mathbf{i} +
u_y\mathbf{j} + u_z\mathbf{k})}} = \cos \frac{\theta}{2} -
(u_x\mathbf{i} + u_y\mathbf{j} + u_z\mathbf{k}) \sin \frac{\theta}{2}