Conversion between quaternions and Euler angles

Last updated

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".

Contents

Definition

There are two representations of quaternions. This article uses the more popular Hamilton.

A quaternion has 4 scalar values: qw (the real part) and qx qy qz (the imaginary part).

Defining the norm of the quaternion as follows:

A unit quaternion satisfies:

We can associate a 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).

Intuition

To better understand how "direction cosines" work with quaternions:

If the axis of rotation is the x-axis:

If the axis of rotation is the y-axis:

If the axis of rotation is the z-axis:

If the axis of rotation is a vector located 45° (π/4 radians) between the x and y axes:

Therefore, the x and y axes "share" influence over the new axis of rotation.

Tait–Bryan angles

Tait-Bryan angles. z-y'-x'' sequence (intrinsic rotations; N coincides with y'). The angle rotation sequence is ps, th, ph. Note that in this case ps > 90deg and th is a negative angle. Taitbrianzyx.svg
Tait–Bryan angles. z-y′-x″ sequence (intrinsic rotations; N coincides with y’). The angle rotation sequence is ψ, θ, φ. Note that in this case ψ > 90° and θ is a negative angle.

Similarly for Euler angles, we use the Tait Bryan angles (in terms of flight dynamics):

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 (post-multiplying a column vector) corresponding to a clockwise/left-handed (looking along positive axis to origin) rotation 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.

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 (ψ, θ, φ) is given by: [1]

Euler angles for Body 3-1-3 Sequence - The xyz (original fixed Lab) system is shown in blue, the XYZ (rotated final Body) system is shown in red. The line of nodes, labelled N and shown in green, is the intermediate Body X-axis around which the second rotation occurs. Eulerangles.svg
Euler angles for Body 3-1-3 Sequence – The xyz (original fixed Lab) system is shown in blue, the XYZ (rotated final Body) system is shown in red. The line of nodes, labelled N and shown in green, is the intermediate Body X-axis around which the second rotation occurs.

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):

Other rotation sequences use different conventions. [1]

Source code

Below code in C++ illustrates above conversion:

structQuaternion{doublew,x,y,z;};QuaternionToQuaternion(doubleroll,doublepitch,doubleyaw)// roll (x), pitch (y), yaw (z), angles are in radians{// Abbreviations for the various angular functionsdoublecr=cos(roll*0.5);doublesr=sin(roll*0.5);doublecp=cos(pitch*0.5);doublesp=sin(pitch*0.5);doublecy=cos(yaw*0.5);doublesy=sin(yaw*0.5);Quaternionq;q.w=cr*cp*cy+sr*sp*sy;q.x=sr*cp*cy-cr*sp*sy;q.y=cr*sp*cy+sr*cp*sy;q.z=cr*cp*sy-sr*sp*cy;returnq;}

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. [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]

Source code

The following C++ program illustrates conversion above:

#define _USE_MATH_DEFINES#include<cmath>structQuaternion{doublew,x,y,z;};structEulerAngles{doubleroll,pitch,yaw;};// this implementation assumes normalized quaternion// converts to Euler angles in 3-2-1 sequenceEulerAnglesToEulerAngles(Quaternionq){EulerAnglesangles;// roll (x-axis rotation)doublesinr_cosp=2*(q.w*q.x+q.y*q.z);doublecosr_cosp=1-2*(q.x*q.x+q.y*q.y);angles.roll=std::atan2(sinr_cosp,cosr_cosp);// pitch (y-axis rotation)doublesinp=std::sqrt(1+2*(q.w*q.y-q.x*q.z));doublecosp=std::sqrt(1-2*(q.w*q.y-q.x*q.z));angles.pitch=2*std::atan2(sinp,cosp)-M_PI/2;// yaw (z-axis rotation)doublesiny_cosp=2*(q.w*q.z+q.x*q.y);doublecosy_cosp=1-2*(q.y*q.y+q.z*q.z);angles.yaw=std::atan2(siny_cosp,cosy_cosp);returnangles;}

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% [4] 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

Related Research Articles

<span class="mw-page-title-main">Pauli matrices</span> Matrices important in quantum mechanics and the study of spin

In mathematical physics and mathematics, the Pauli matrices are a set of three 2 × 2 complex matrices that are Hermitian, involutory and unitary. Usually indicated by the Greek letter sigma, they are occasionally denoted by tau when used in connection with isospin symmetries.

<span class="mw-page-title-main">Potential flow</span> Velocity field as the gradient of a scalar function

In fluid dynamics, potential flow is the ideal flow pattern of an inviscid fluid. Potential flows are described and determined by mathematical methods.

<span class="mw-page-title-main">Unit vector</span> Vector of length one

In mathematics, a unit vector in a normed vector space is a vector of length 1. A unit vector is often denoted by a lowercase letter with a circumflex, or "hat", as in .

In mechanics and geometry, the 3D rotation group, often denoted SO(3), is the group of all rotations about the origin of three-dimensional Euclidean space under the operation of composition.

Unit quaternions, known as versors, provide a convenient mathematical notation for representing spatial orientations and rotations of elements in three dimensional space. Specifically, they encode information about an axis-angle rotation about an arbitrary axis. Rotation and orientation quaternions have applications in computer graphics, computer vision, robotics, navigation, molecular dynamics, flight dynamics, orbital mechanics of satellites, and crystallographic texture analysis.

<span class="mw-page-title-main">Rotation (mathematics)</span> Motion of a certain space that preserves at least one point

Rotation in mathematics is a concept originating in geometry. Any rotation is a motion of a certain space that preserves at least one point. It can describe, for example, the motion of a rigid body around a fixed point. Rotation can have a sign (as in the sign of an angle): a clockwise rotation is a negative magnitude so a counterclockwise turn has a positive magnitude. A rotation is different from other types of motions: translations, which have no fixed points, and (hyperplane) reflections, each of them having an entire (n − 1)-dimensional flat of fixed points in a n-dimensional space.

<span class="mw-page-title-main">Euler angles</span> Description of the orientation of a rigid body

The Euler angles are three angles introduced by Leonhard Euler to describe the orientation of a rigid body with respect to a fixed coordinate system.

An infinitesimal rotation matrix or differential rotation matrix is a matrix representing an infinitely small rotation.

<span class="mw-page-title-main">Bloch sphere</span> Geometrical representation of the pure state space of a two-level quantum mechanical system

In quantum mechanics and computing, the Bloch sphere is a geometrical representation of the pure state space of a two-level quantum mechanical system (qubit), named after the physicist Felix Bloch.

In linear algebra, a rotation matrix is a transformation matrix that is used to perform a rotation in Euclidean space. For example, using the convention below, the matrix

In mathematics, the Cayley transform, named after Arthur Cayley, is any of a cluster of related things. As originally described by Cayley (1846), the Cayley transform is a mapping between skew-symmetric matrices and special orthogonal matrices. The transform is a homography used in real analysis, complex analysis, and quaternionic analysis. In the theory of Hilbert spaces, the Cayley transform is a mapping between linear operators.

In rotordynamics, the rigid rotor is a mechanical model of rotating systems. An arbitrary rigid rotor is a 3-dimensional rigid object, such as a top. To orient such an object in space requires three angles, known as Euler angles. A special rigid rotor is the linear rotor requiring only two angles to describe, for example of a diatomic molecule. More general molecules are 3-dimensional, such as water, ammonia, or methane.

Screw theory is the algebraic calculation of pairs of vectors, such as angular and linear velocity, or forces and moments, that arise in the kinematics and dynamics of rigid bodies.

In the theory of three-dimensional rotation, Rodrigues' rotation formula, named after Olinde Rodrigues, is an efficient algorithm for rotating a vector in space, given an axis and angle of rotation. By extension, this can be used to transform all three basis vectors to compute a rotation matrix in SO(3), the group of all rotation matrices, from an axis–angle representation. In terms of Lie theory, the Rodrigues' formula provides an algorithm to compute the exponential map from the Lie algebra so(3) to its Lie group SO(3).

In geometry, various formalisms exist to express a rotation in three dimensions as a mathematical transformation. In physics, this concept is applied to classical mechanics where rotational kinematics is the science of quantitative description of a purely rotational motion. The orientation of an object at a given instant is described with the same tools, as it is defined as an imaginary rotation from a reference placement in space, rather than an actually observed rotation from a previous placement in space.

<span class="mw-page-title-main">Dual quaternion</span> Eight-dimensional algebra over the real numbers

In mathematics, the dual quaternions are an 8-dimensional real algebra isomorphic to the tensor product of the quaternions and the dual numbers. Thus, they may be constructed in the same way as the quaternions, except using dual numbers instead of real numbers as coefficients. A dual quaternion can be represented in the form A + εB, where A and B are ordinary quaternions and ε is the dual unit, which satisfies ε2 = 0 and commutes with every element of the algebra. Unlike quaternions, the dual quaternions do not form a division algebra.

<span class="mw-page-title-main">Gravitational lensing formalism</span>

In general relativity, a point mass deflects a light ray with impact parameter by an angle approximately equal to

In fluid dynamics, the Oseen equations describe the flow of a viscous and incompressible fluid at small Reynolds numbers, as formulated by Carl Wilhelm Oseen in 1910. Oseen flow is an improved description of these flows, as compared to Stokes flow, with the (partial) inclusion of convective acceleration.

In pure and applied mathematics, quantum mechanics and computer graphics, a tensor operator generalizes the notion of operators which are scalars and vectors. A special class of these are spherical tensor operators which apply the notion of the spherical basis and spherical harmonics. The spherical basis closely relates to the description of angular momentum in quantum mechanics and spherical harmonic functions. The coordinate-free generalization of a tensor operator is known as a representation operator.

In physics and engineering, Davenport chained rotations are three chained intrinsic rotations about body-fixed specific axes. Euler rotations and Tait–Bryan rotations are particular cases of the Davenport general rotation decomposition. The angles of rotation are called Davenport angles because the general problem of decomposing a rotation in a sequence of three was studied first by Paul B. Davenport.

References

  1. 1 2 NASA Mission Planning and Analysis Division (July 1977). "Euler Angles, Quaternions, and Transformation Matrices". NASA . Retrieved 24 May 2021.
  2. 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.
  3. 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 .
  4. 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.