Conversion between quaternions and Euler angles: Difference between revisions

Content deleted Content added
Vector rotation: Remove irrelevent mention of ijk
Link suggestions feature: 3 links added.
 
(16 intermediate revisions by 9 users not shown)
Line 1:
{{Short description|Mathematical strategy}}
{{cleanup rewrite|date=January 2024}}
[[Rotation formalisms in three dimensions|Spatial rotations in three dimensions]] can be [[Coordinate system|parametrized]] using both [[Euler angles]] and [[Quaternions and spatial rotation|unit quaternions]]. This article explains how to convert between the two representations. Actually this simple use of "quaternions" was first presented by [[Leonhard Euler|Euler]] some seventy years earlier than [[William Rowan Hamilton|Hamilton]] to solve the problem of [[magic square]]s. For this reason the dynamics community commonly refers to quaternions in this application as "Euler parameters".
 
==Definition==
There are [[Quaternions_and_spatial_rotation#Alternative_conventions|two representations]] of quaternions. This article uses the more popular Hamilton.
There are two representations of quaternions. Hamilton (where w is the first component) and [[JPL]] (where w is the last component).<ref>W. G. Breckenridge, "Quaternions proposed standard conventions," NASA Jet Propulsion Laboratory, Technical Report, Oct. 1979.</ref> This article uses Hamilton for some formulas. A unit [[quaternion]] can be described as:
 
:<math>\mathbf{q} = \begin{bmatrix} q_w & q_x & q_y & q_z \end{bmatrix}^T</math>
A quaternion has 4 real values: {{mvar|q<sub>w</sub>}} (the real part or the scalar part) and {{mvar|q<sub>x</sub> q<sub>y</sub> q<sub>z</sub>}} (the imaginary part).
:<math>|\mathbf{q}|^2 = q_w^2 + q_x^2 + q_y^2 + q_z^2 = 1</math>
 
Defining the [[Quaternion#Conjugation,_the_norm,_and_reciprocal|norm of the quaternion]] as follows:
:<math display=block>|\mathbf{lVert q}|^2 \rVert = \sqrt{\,q_w^2 + q_x^2 + q_y^2 + q_z^2 = 1~}</math>
 
A ''unit quaternion'' satisfies:
<math display=block>\lVert q \rVert = 1</math>
 
We can associate a [[quaternion]] with a rotation around an axis by the following expression
Line 13 ⟶ 20:
:<math>\mathbf{q}_y = \sin(\alpha/2)\cos(\beta_y)</math>
:<math>\mathbf{q}_z = \sin(\alpha/2)\cos(\beta_z)</math>
where α is a simple rotation angle (the value in radians of the [[angle of rotation]]) and cos(β<sub>''x''</sub>), cos(β<sub>''y''</sub>) and cos(β<sub>''z''</sub>) are the "[[direction cosine]]s" of the angles between the three coordinate axes and the axis of rotation. ([[Euler's rotation theorem|Euler's Rotation Theorem]]).
 
==Intuition==
Line 63 ⟶ 70:
 
== Rotation matrices ==
The [[orthogonal matrix]] (post-multiplying a [[Row and column vectors|column vector]]) corresponding to a clockwise/[[Right-hand rule|left-handed]] (looking along positive axis to origin) rotation by the unit [[quaternion]] <math>q=q_w+iq_x+jq_y+kq_z</math> is given by the [[Quaternions and spatial rotation#Quaternion-derived rotation matrix|inhomogeneous expression]]:
 
:<math>R = \begin{bmatrix}
Line 166 ⟶ 173:
};
 
// This is not in game format, it is in mathematical format.
Quaternion ToQuaternion(double roll, double pitch, double yaw) // roll (x), pitch (Yy), yaw (z), angles are in radians
{
// Abbreviations for the various angular functions
Line 191 ⟶ 199:
A direct formula for the conversion from a quaternion to Euler angles in any of the 12 possible sequences exists.<ref>{{cite journal |last1=Bernardes |first1=Evandro |last2=Viollet |first2=Stéphane |title=Quaternion to Euler angles conversion: A direct, general and computationally efficient method |journal=PLOS ONE |date=10 November 2022 |volume=17 |issue=11 |pages=e0276302 |doi=10.1371/journal.pone.0276302 |pmid=36355707 |pmc=9648712 |bibcode=2022PLoSO..1776302B |language=en |issn=1932-6203|doi-access=free }}</ref> 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:
 
:<math>\begin{bmatrix}
\phi \\ \theta \\ \psi
\end{bmatrix} =
\begin{bmatrix}
\mbox{arctan} \left(\dfrac {2(q_w q_x + q_y q_z)} {1 - 2(q_x^2 + q_y^2)}\right)\\
- \pi/2 + 2 \, \mbox{arctan} \sqrt{\dfrac {1 + 2(q_w q_y - q_x q_z)} {1 - 2(q_w q_y - q_x q_z)}} \\
\mbox{arctan} \left(\dfrac {2(q_w q_z + q_x q_y)} {1 - 2(q_y^2 + q_z^2)}\right)
\end{bmatrix} </math>
 
However the [[arctan]] functions implemented in computer languages only produce results between −π/2 and [[right angle|π/2]], to generate all the orientations one needs to replace the arctan functions in computer code by [[atan2]]:
 
:<math>\begin{bmatrix}
Line 212 ⟶ 209:
\end{bmatrix} </math>
 
HoweverNote that the [[arctan]] functions implemented in computer languages only produce results between −π/2 and [[right angle|π/2]], which is why [[atan2]] is used to generate all the correct orientations. oneMoreover, needstypical toimplementations replace theof arctan functionsalso inmight computerhave codesome bynumerical disadvantages near zero and one. [[atan2]]:
Moreover, typical implementations of arctan also might have some numerical disadvantages near zero and one. Some implementations use the equivalent expression:<ref>{{cite journal|last1=Blanco|first1=Jose-Luis|title=A tutorial on se (3) transformation parameterizations and on-manifold optimization|journal=University of Malaga, Tech. Rep|date=2010|citeseerx=10.1.1.468.5407}}</ref>
 
Moreover, typical implementations of arctan also might have some numerical disadvantages near zero and one. Some implementations use the equivalent expression:<ref>{{cite journal|last1=Blanco|first1=Jose-Luis|title=A tutorial on se (3) transformation parameterizations and on-manifold optimization|journal=University of Malaga, Tech. Rep|date=2010|citeseerx=10.1.1.468.5407}}</ref>
:<math> \theta = \mbox{arcsin} (2(q_w q_y - q_x q_z)) </math>
 
Line 268 ⟶ 267:
:<math>\vec{t} = 2\vec{q} \times \vec{v}</math>
:<math>\vec{v}^{\,\prime} = \vec{v} + q_w \vec{t} + \vec{q} \times \vec{t}</math>
where <math>\times</math> 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% <ref>{{cite journal |pmc=4435132|year=2015|last1=Janota|first1=A|title=Improving the Precision and Speed of Euler Angles Computation from Low-Cost Rotation Sensor Data|journal=Sensors|volume=15|issue=3|pages=7016–7039|last2=Šimák|first2=V|last3=Nemec|first3=D|last4=Hrbček|first4=J|doi=10.3390/s150307016|pmid=25806874|bibcode=2015Senso..15.7016J |doi-access=free}}</ref> faster than the original for vector rotation.
 
=== Proof ===