Conversion between quaternions and Euler angles
   HOME

TheInfoList



OR:

Spatial rotations in three dimensions can be parametrized using both
Euler angles The Euler angles are three angles introduced by Leonhard Euler to describe the Orientation (geometry), orientation of a rigid body with respect to a fixed coordinate system.Novi Commentarii academiae scientiarum Petropolitanae 20, 1776, pp. 189� ...
and unit quaternions. This article explains how to convert between the two representations. Actually this simple use of "quaternions" was first presented by
Euler Leonhard Euler ( ; ; ; 15 April 170718 September 1783) was a Swiss polymath who was active as a mathematician, physicist, astronomer, logician, geographer, and engineer. He founded the studies of graph theory and topology and made influential ...
some seventy years earlier than
Hamilton Hamilton may refer to: * Alexander Hamilton (1755/1757–1804), first U.S. Secretary of the Treasury and one of the Founding Fathers of the United States * ''Hamilton'' (musical), a 2015 Broadway musical by Lin-Manuel Miranda ** ''Hamilton'' (al ...
to solve the problem of
magic square In mathematics, especially History of mathematics, historical and recreational mathematics, a square array of numbers, usually positive integers, is called a magic square if the sums of the numbers in each row, each column, and both main diago ...
s. 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: (the real part or the scalar part) and (the imaginary part). Defining the norm of the quaternion as follows: \lVert q \rVert = \sqrt A ''unit quaternion'' satisfies: \lVert q \rVert = 1 We can associate a
quaternion In mathematics, the quaternion number system extends the complex numbers. Quaternions were first described by the Irish mathematician William Rowan Hamilton in 1843 and applied to mechanics in three-dimensional space. The algebra of quater ...
with a rotation around an axis by the following expression :\mathbf_w = \cos(\alpha/2) :\mathbf_x = \sin(\alpha/2)\cos(\beta_x) :\mathbf_y = \sin(\alpha/2)\cos(\beta_y) :\mathbf_z = \sin(\alpha/2)\cos(\beta_z) where α is a simple rotation angle (the value in radians of the
angle of rotation The angular displacement (symbol θ, , or φ) – also called angle of rotation, rotational displacement, or rotary displacement – of a physical body is the angle (in units of radians, degrees, turns, etc.) through which the body rotates (r ...
) 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).


Intuition

To better understand how " direction cosines" work with quaternions: :\begin \mathbf_w = \cos(\text/2)\\ \mathbf_x = \sin(\text/2)\cos(\text)\\ \mathbf_y = \sin(\text/2)\cos(\text)\\ \mathbf_z = \sin(\text/2)\cos(\text)\end If the
axis of rotation Rotation or rotational/rotary motion is the circular movement of an object around a central line, known as an ''axis of rotation''. A plane figure can rotate in either a clockwise or counterclockwise sense around a perpendicular axis intersect ...
is the ''x''-axis: :\begin \mathbf_w = \cos(\alpha/2)\\ \mathbf_x = \sin(\alpha/2)\cdot1\\ \mathbf_y = \sin(\alpha/2)\cdot0\\ \mathbf_z = \sin(\alpha/2)\cdot0\end If the
axis of rotation Rotation or rotational/rotary motion is the circular movement of an object around a central line, known as an ''axis of rotation''. A plane figure can rotate in either a clockwise or counterclockwise sense around a perpendicular axis intersect ...
is the ''y''-axis: :\begin \mathbf_w = \cos(\alpha/2)\\ \mathbf_x = \sin(\alpha/2)\cdot0\\ \mathbf_y = \sin(\alpha/2)\cdot1\\ \mathbf_z = \sin(\alpha/2)\cdot0\end If the
axis of rotation Rotation or rotational/rotary motion is the circular movement of an object around a central line, known as an ''axis of rotation''. A plane figure can rotate in either a clockwise or counterclockwise sense around a perpendicular axis intersect ...
is the ''z''-axis: :\begin \mathbf_w = \cos(\alpha/2)\\ \mathbf_x = \sin(\alpha/2)\cdot0\\ \mathbf_y = \sin(\alpha/2)\cdot0\\ \mathbf_z = \sin(\alpha/2)\cdot1\end If the
axis of rotation Rotation or rotational/rotary motion is the circular movement of an object around a central line, known as an ''axis of rotation''. A plane figure can rotate in either a clockwise or counterclockwise sense around a perpendicular axis intersect ...
is a
vector Vector most often refers to: * Euclidean vector, a quantity with a magnitude and a direction * Disease vector, an agent that carries and transmits an infectious pathogen into another living organism Vector may also refer to: Mathematics a ...
located 45° ( radians) between the ''x'' and ''y'' axes: :\begin \mathbf_w = \cos(\alpha/2)\\ \mathbf_x = \sin(\alpha/2)\cdot0.707 \ldots\\ \mathbf_y = \sin(\alpha/2)\cdot0.707 \ldots\\ \mathbf_z = \sin(\alpha/2)\cdot0\end Therefore, the ''x'' and ''y'' axes "share" influence over the new
axis of rotation Rotation or rotational/rotary motion is the circular movement of an object around a central line, known as an ''axis of rotation''. A plane figure can rotate in either a clockwise or counterclockwise sense around a perpendicular axis intersect ...
.


Tait–Bryan angles

Similarly for Euler angles, we use the Tait Bryan angles (in terms of
flight dynamics Flight dynamics in aviation and spacecraft, is the study of the performance, stability, and control of vehicles flight, flying through the air or in outer space. It is concerned with how forces acting on the vehicle determine its velocity and at ...
): * Heading – \psi: rotation about the Z-axis * Pitch – \theta: rotation about the new Y-axis * Bank – \phi: 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.


Rotation matrices

The
orthogonal matrix In linear algebra, an orthogonal matrix, or orthonormal matrix, is a real square matrix whose columns and rows are orthonormal vectors. One way to express this is Q^\mathrm Q = Q Q^\mathrm = I, where is the transpose of and is the identi ...
(post-multiplying a column vector) corresponding to a clockwise/
left-handed In human biology, handedness is an individual's preferential use of one hand, known as the dominant hand, due to and causing it to be stronger, faster or more dextrous. The other hand, comparatively often the weaker, less dextrous or simply l ...
(looking along positive axis to origin) rotation by the unit
quaternion In mathematics, the quaternion number system extends the complex numbers. Quaternions were first described by the Irish mathematician William Rowan Hamilton in 1843 and applied to mechanics in three-dimensional space. The algebra of quater ...
q=q_w+iq_x+jq_y+kq_z is given by the inhomogeneous expression: :R = \begin 1- 2(q_y^2 + q_z^2) & 2(q_x q_y - q_w q_z) & 2(q_w q_y + q_x q_z) \\ 2(q_x q_y + q_w q_z) & 1 - 2(q_x^2 + q_z^2) & 2(q_y q_z - q_w q_x) \\ 2(q_x q_z - q_w q_y) & 2( q_w q_x + q_y q_z) & 1 - 2(q_x^2 + q_y^2) \end or equivalently, by the
homogeneous Homogeneity and heterogeneity are concepts relating to the uniformity of a substance, process or image. A homogeneous feature is uniform in composition or character (i.e., color, shape, size, weight, height, distribution, texture, language, i ...
expression: :R = \begin q_w^2 + q_x^2 - q_y^2 - q_z^2 & 2(q_x q_y - q_w q_z) & 2(q_w q_y + q_x q_z) \\ 2(q_x q_y + q_w q_z) & q_w^2 - q_x^2 + q_y^2 - q_z^2 & 2(q_y q_z - q_w q_x) \\ 2(q_x q_z - q_w q_y) & 2( q_w q_x + q_y q_z) & q_w^2 - q_x^2 - q_y^2 + q_z^2 \end If q_w+iq_x+jq_y+kq_z 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. The direction cosine matrix (from the rotated Body XYZ coordinates to the original Lab xyz coordinates for a clockwise/lefthand rotation) corresponding to a post-multiply Body 3-2-1 sequence with
Euler angles The Euler angles are three angles introduced by Leonhard Euler to describe the Orientation (geometry), orientation of a rigid body with respect to a fixed coordinate system.Novi Commentarii academiae scientiarum Petropolitanae 20, 1776, pp. 189� ...
(ψ, θ, φ) is given by: : \begin \begin x \\ y \\ z \\ \end & = R_z(\psi) R_y(\theta) R_x(\phi) \begin X \\ Y \\ Z \\ \end \\ & = \begin \cos\psi & -\sin\psi & 0 \\ \sin\psi & \cos\psi & 0 \\ 0 & 0 & 1 \\ \end \begin \cos\theta & 0 & \sin\theta \\ 0 & 1 & 0 \\ -\sin\theta & 0 & \cos\theta \\ \end \begin 1 & 0 & 0 \\ 0 & \cos\phi & -\sin\phi \\ 0 & \sin\phi & \cos\phi \\ \end \begin X \\ Y \\ Z \\ \end \\ & = \begin \cos\theta \cos\psi & -\cos\phi \sin\psi + \sin\phi \sin\theta \cos\psi & \sin\phi \sin\psi + \cos\phi \sin\theta \cos\psi \\ \cos\theta \sin\psi & \cos\phi \cos\psi + \sin\phi \sin\theta \sin\psi & -\sin\phi \cos\psi + \cos\phi \sin\theta \sin\psi \\ -\sin\theta & \sin\phi \cos\theta & \cos\phi \cos\theta \\ \end \begin X \\ Y \\ Z \\ \end \\ \end


Euler angles (in 3-2-1 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): : \begin \mathbf & = \begin \cos (\psi /2) \\ 0 \\ 0 \\ \sin (\psi /2) \\ \end \begin \cos (\theta /2) \\ 0 \\ \sin (\theta /2) \\ 0 \\ \end \begin \cos (\phi /2) \\ \sin (\phi /2) \\ 0 \\ 0 \\ \end \\ & = \begin \cos (\phi /2) \cos (\theta /2) \cos (\psi /2) + \sin (\phi /2) \sin (\theta /2) \sin (\psi /2) \\ \sin (\phi /2) \cos (\theta /2) \cos (\psi /2) - \cos (\phi /2) \sin (\theta /2) \sin (\psi /2) \\ \cos (\phi /2) \sin (\theta /2) \cos (\psi /2) + \sin (\phi /2) \cos (\theta /2) \sin (\psi /2) \\ \cos (\phi /2) \cos (\theta /2) \sin (\psi /2) - \sin (\phi /2) \sin (\theta /2) \cos (\psi /2) \\ \end \\ \end Other rotation sequences use different conventions.


Source code

Below code in C++ illustrates above conversion: struct Quaternion ; // This is not in game format, it is in mathematical format. Quaternion ToQuaternion(double roll, double pitch, double yaw) // roll (x), pitch (y), yaw (z), angles are in radians


Quaternion to Euler angles (in 3-2-1 sequence) conversion

A direct formula for the conversion from a quaternion to Euler angles in any of the 12 possible sequences exists. 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: :\begin \phi \\ \theta \\ \psi \end = \begin \mbox \left(2(q_w q_x + q_y q_z),1 - 2(q_x^2 + q_y^2)\right) \\ - \pi/2 + 2 \, \mbox \left(\sqrt, \sqrt\right) \\ \mbox \left(2(q_w q_z + q_x q_y),1 - 2(q_y^2 + q_z^2)\right) \end Note that the
arctan In mathematics, the inverse trigonometric functions (occasionally also called ''antitrigonometric'', ''cyclometric'', or ''arcus'' functions) are the inverse functions of the trigonometric functions, under suitably restricted domains. Specific ...
functions implemented in computer languages only produce results between −π/2 and π/2, which is why
atan2 In computing and mathematics, the function (mathematics), function atan2 is the 2-Argument of a function, argument arctangent. By definition, \theta = \operatorname(y, x) is the angle measure (in radians, with -\pi 0, \\ mu \arctan\left(\fr ...
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: : \theta = \mbox (2(q_w q_y - q_x q_z))


Source code

The following C++ program illustrates conversion above: #define _USE_MATH_DEFINES #include struct Quaternion ; struct EulerAngles ; // this implementation assumes normalized quaternion // converts to Euler angles in 3-2-1 sequence EulerAngles ToEulerAngles(Quaternion q)


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 Gimbal lock is the loss of one degree of freedom (mechanics), degree of freedom in a multi-dimensional mechanism at certain alignments of the axes. In a three-dimensional three-gimbal mechanism, gimbal lock occurs when the axes of two of the gi ...
. Code to handle the singularities is derived on this site
www.euclideanspace.com


Vector rotation

Let us define scalar q_w and vector \vec such that quaternion \mathbf = (q_w,\vec). Note that the canonical way to rotate a three-dimensional vector \vec by a quaternion q defining an Euler rotation is via the formula :\mathbf^ = \mathbf^\ast where \mathbf = (0,\vec) is a quaternion containing the embedded vector \vec, \mathbf^\ast=(q_w,-\vec) is a conjugate quaternion, and \mathbf^ = (0,\vec^) is the rotated vector \vec^. In computational implementations this requires two quaternion multiplications. An alternative approach is to apply the pair of relations :\vec = 2\vec \times \vec :\vec^ = \vec + q_w \vec + \vec \times \vec where \times 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% faster than the original for vector rotation.


Proof

The general rule for quaternion multiplication involving scalar and vector parts is given by : \begin \mathbf & = (p_w,\vec)(q_w,\vec) \\ & = (p_w q_w - \vec \cdot \vec, p_w \vec + q_w \vec + \vec \times \vec) \\ \end Using this relation one finds for \mathbf = (0,\vec) that : \begin \mathbf & = (0,\vec)(q_w,-\vec) \\ & = (\vec \cdot \vec, q_w \vec - \vec \times \vec) \\ \end and upon substitution for the triple product : \begin \mathbf & = (q_w,\vec)(\vec \cdot \vec, q_w \vec - \vec \times \vec) \\ & = (0, q_w^2 \vec + q_w \vec \times \vec + (\vec \cdot \vec) \vec + q_w \vec \times \vec + \vec\times(\vec\times\vec )) \\ \end where anti-commutivity of cross product and \vec\cdot \vec \times \vec = 0 has been applied. By next exploiting the property that \mathbf is a
unit quaternion In mathematics, a versor is a quaternion of norm one, also known as a unit quaternion. Each versor has the form :u = \exp(a\mathbf) = \cos a + \mathbf \sin a, \quad \mathbf^2 = -1, \quad a \in ,\pi where the r2 = −1 condition means that r is ...
so that q_w^2 = 1 - \vec\cdot\vec, along with the standard vector identity : \vec\times(\vec\times\vec) = (\vec\cdot\vec)\vec - (\vec\cdot\vec)\vec one obtains : \begin \mathbf^\prime & = \mathbf = (0, \vec + 2 q_w \vec \times \vec + 2\vec\times (\vec\times\vec )) \\ \end which upon defining \vec = 2\vec \times \vec can be written in terms of scalar and vector parts as : (0, \vec^) = (0, \vec + q_w \vec + \vec \times \vec ).


See also

*
Rotation operator (vector space) Rotation or rotational/rotary motion is the circular movement of an object around a central line, known as an ''axis of rotation''. A plane figure can rotate in either a clockwise or counterclockwise sense around a perpendicular axis interse ...
*
Quaternions and spatial rotation unit vector, Unit quaternions, known as versor, ''versors'', provide a convenient mathematics, mathematical notation for representing spatial Orientation (geometry), orientations and rotations of elements in three dimensional space. Specifically, th ...
*
Euler Angles The Euler angles are three angles introduced by Leonhard Euler to describe the Orientation (geometry), orientation of a rigid body with respect to a fixed coordinate system.Novi Commentarii academiae scientiarum Petropolitanae 20, 1776, pp. 189� ...
*
Rotation matrix In linear algebra, a rotation matrix is a transformation matrix that is used to perform a rotation (mathematics), rotation in Euclidean space. For example, using the convention below, the matrix :R = \begin \cos \theta & -\sin \theta \\ \sin \t ...
*
Rotation formalisms in three dimensions In geometry, there exist various rotation formalisms to express a rotation in three dimensions as a mathematical transformation. In physics, this concept is applied to classical mechanics where rotational (or angular) kinematics is the science o ...


References

{{Reflist


External links


Q60. How do I convert Euler rotation angles to a quaternion?
and related questions at The Matrix and Quaternions FAQ Rotation in three dimensions Euclidean symmetries 3D computer graphics Quaternions