Spatial rotations in three dimensions can be parametrized using both Euler angles and unit quaternions. This article explains how to convert between the two representations. Actually this simple use of "quaternions" was first presented by Euler some seventy years earlier than Hamilton to solve the problem of magic squares. For this reason the dynamics community commonly refers to quaternions in this application as "Euler parameters".
Definition
There are two representations of quaternions. This article uses the more popular Hamilton.
A quaternion has 4 real values: qw (the real part or the scalar part) and qx qy qz (the imaginary part).
Defining the norm of the quaternion as follows:
A unit quaternion satisfies:
We can associate a unit quaternion with a rotation around an axis by the following expression




where α is a simple rotation angle (the value in radians of the angle of rotation) and cos(βx), cos(βy) and cos(βz) are the "direction cosines" of the angles between the three coordinate axes and the axis of rotation. (Euler's Rotation Theorem).
Rotation matrices
The orthogonal matrix (post-multiplying a column vector) by the unit quaternion
is given by the inhomogeneous expression:

or equivalently, by the homogeneous expression:

If
is not a unit quaternion then the homogeneous form is still a scalar multiple of a rotation matrix, while the inhomogeneous form is in general no longer an orthogonal matrix. This is why in numerical work the homogeneous form is to be preferred if distortion is to be avoided.
Tait–Bryan angles
In this section we use the Tait Bryan angles (in terms of flight dynamics):
- Heading –
: rotation about the Z-axis
- Pitch –
: rotation about the new Y-axis
- Bank –
: rotation about the new X-axis
where the X-axis points forward, Y-axis to the right and Z-axis downward. In the conversion example above the rotation occurs in the order heading, pitch, bank.
The direction cosine matrix (from the rotated Body XYZ coordinates to the original Lab xyz coordinates corresponding to a post-multiply ZYX sequence with Tait-Bryan (ψ, θ, φ) is given by:[1]

(This is the formula in the penultimate, ZYX row for the Tait-Bryan angles in Euler angles.)
Angles (in ZYX sequence) to quaternion conversion
By combining the quaternion representations of the Euler rotations we get for the Body 3-2-1 sequence, where the airplane first does yaw (Body-Z) turn during taxiing onto the runway, then pitches (Body-Y) during take-off, and finally rolls (Body-X) in the air. The resulting orientation of Body 3-2-1 sequence (around the capitalized axis in the illustration of Tait–Bryan angles) is equivalent to that of lab 1-2-3 sequence (around the lower-cased axis), where the airplane is rolled first (lab-x axis), and then nosed up around the horizontal lab-y axis, and finally rotated around the vertical lab-z axis (lB = lab2Body):

Other rotation sequences use different conventions.[1]
Quaternion to angles (in ZYX sequence) conversion
A direct formula for the conversion from a quaternion to Euler angles in any of the 12 possible sequences exists.[2] For the rest of this section, the formula for the sequence Body 3-2-1 will be shown.
If the quaternion is properly normalized, the Euler angles can be obtained from the quaternions via the relations:

Note that the arctan functions implemented in computer languages only produce results between −π/2 and π/2, which is why atan2 is used to generate all the correct orientations. Moreover, typical implementations of arctan also might have some numerical disadvantages near zero and one.
Some implementations use the equivalent expression:[3]

Euler angles
For the most common definition of the Euler angles (intrinsic active rotation first by α around Z, then β around the new X, finally γ around the new Z) we find for the new coordinates X, Y, Z from the old coordinates x, y, z

(This is the formula in the last row, order ZXZ, in the table for proper Euler angles in Euler angles. The Siminovich formula
[4] uses the proper Euler angles in ZYZ order, for example.)
Singularities
One must be aware of singularities in the Euler angle parametrization when the pitch approaches ±90° (north/south pole). These cases must be handled specially. The common name for this situation is gimbal lock.
Code to handle the singularities is derived on this site: www.euclideanspace.com
Vector rotation
Let us define scalar
and vector
such that quaternion
.
Note that the canonical way to rotate a three-dimensional vector
by a quaternion
defining an Euler rotation is via the formula

where
is a quaternion containing the embedded vector
,
is a conjugate quaternion, and
is the rotated vector
. In computational implementations this requires two quaternion multiplications. An alternative approach is to apply the pair of relations


where
indicates a three-dimensional vector cross product. This involves fewer multiplications and is therefore computationally faster. Numerical tests indicate this latter approach may be up to 30% [5] faster than the original for vector rotation.
Proof
The general rule for quaternion multiplication involving scalar and vector parts is given by

Using this relation one finds for
that

and upon substitution for the triple product

where anti-commutivity of cross product and
has been applied. By next exploiting the property that
is a unit quaternion so that
, along with the standard vector identity

one obtains

which upon defining
can be written in terms of scalar and vector parts as

See also
References
- ^ a b NASA Mission Planning and Analysis Division (July 1977). "Euler Angles, Quaternions, and Transformation Matrices". NASA. Retrieved 24 May 2021.
- ^ Bernardes, Evandro; Viollet, Stéphane (10 November 2022). "Quaternion to Euler angles conversion: A direct, general and computationally efficient method". PLOS ONE. 17 (11) e0276302. Bibcode:2022PLoSO..1776302B. doi:10.1371/journal.pone.0276302. ISSN 1932-6203. PMC 9648712. PMID 36355707.
- ^ Blanco, Jose-Luis (2010). "A tutorial on se (3) transformation parameterizations and on-manifold optimization". University of Malaga, Tech. Rep. CiteSeerX 10.1.1.468.5407.
- ^ Siminovitch, David J. (1997). "Rotations in NMR: Part I. Euler-Rodriguez parameters and quaternions". Concep. Magn. Res. 9 (3): 117–193. doi:10.1002/(SICI)1099-0534(1997)9:3<149::AID-CMR3>3.0.CO;2-Z. eq [114]
- ^ Janota, A; Šimák, V; Nemec, D; Hrbček, J (2015). "Improving the Precision and Speed of Euler Angles Computation from Low-Cost Rotation Sensor Data". Sensors. 15 (3): 7016–7039. Bibcode:2015Senso..15.7016J. doi:10.3390/s150307016. PMC 4435132. PMID 25806874.
External links