Quaternion.to_angles
Quaternion.to_angles(seq: str = 'ZYX') → ndarray : Changed in version 0.4.1.
code
Return the Euler angles from the Quaternion.
This is a new implementation based on the excellent work by Evandro
Bernardes and Stéphane Viollet [[BV22](/u/fab8b0a9fec670cf#id4)].
**Derivation**
Given a versor (unit quaternion) \(\mathbf{q} = \begin{bmatrix}q_w & q_x & q_y & q_z \end{bmatrix}^T), return the rotation angles
that represent the same orientation for a given sequence of 3 rotations
(e.g., 'ZYX', 'XYZ', etc.)
code
When rotation axes are all different, the sequence is called
**Tait-Bryan angles**.
When the first and third rotation axes are the same, the sequence is
called **Proper Euler angles**.
If the rotations occur around a fixed global coordinate system, the
angles are called **extrinsic**.
If the rotations occur sequentially around its local coordinate system,
the angles are called **intrinsic**.
We start with the equivalence between a versor (unit quaternion) and
the axis-angle representation of a rotation.
where
is a **unit vector** describing the axis of rotation about which a
point rotates by an angle .
Note
Remember that a **versor** is the unit quaternion commonly used to
represent rotations, and it has a norm equal to one:
Likewise, a **unit vector** is a vector, whose norm is also equal
to one:
We can decompose this relation as follows:
where, for :
\begin{split}\begin{eqnarray}
s_1 \equiv \, & \sin\frac{\theta_1}{2}, & \; &
c_1 \equiv \, & \cos\frac{\theta_1}{2} \\
s_2 \equiv \, & \sin\frac{\theta_2}{2}, & \; &
c_2 \equiv \, & \cos\frac{\theta_2}{2} \\
s_3 \equiv \, & \sin\frac{\theta_3}{2}, & \; &
c_3 \equiv \, & \cos\frac{\theta_3}{2}
\end{eqnarray}\end{split}
and and are orthonormal unit
vectors such that . A third
orthonormal unit vector can be obtained from their cross product:
where .
The values of and are in the range .
The authors chose a solution in which , and .
A factorization with these properties leads to:
with:
\begin{split}\begin{eqnarray}
s_+ \equiv \, & \sin(\theta_+) = & \sin(\frac{\theta_3+\theta_1}{2}) = & c_1s_3 + s_1c_3 \\
s_- \equiv \, & \sin(\theta_-) = & \sin(\frac{\theta_3-\theta_1}{2}) = & c_1s_3 - s_1c_3 \\
c_+ \equiv \, & \cos(\theta_+) = & \cos(\frac{\theta_3+\theta_1}{2}) = & c_1c_3 - s_1s_3 \\
c_- \equiv \, & \cos(\theta_-) = & \cos(\frac{\theta_3-\theta_1}{2}) = & c_1c_3 + s_1s_3
\end{eqnarray}\end{split}
using the helper variables:
\begin{split}\begin{eqnarray}
\theta_+ & = & \frac{\theta_3 + \theta_1}{2} \\ \\
\theta_- & = & \frac{\theta_3 - \theta_1}{2}
\end{eqnarray}\end{split}
With it we can obtain the following relation between the quaternion and
the orthogonal components:
\begin{split}\begin{bmatrix}
a \\ b \\ c \\ d
\end{bmatrix} & = &
\begin{bmatrix}
c_2c_+ \\ c_2s_+ \\ s_2c_- \\ s_2s_-
\end{bmatrix}\end{split}
The array is a
permutation of the elements of the vector part of the quaternion.
\begin{split}\begin{bmatrix}
b \\ c \\ d
\end{bmatrix} & = & \,
\begin{bmatrix}
\mathbf{v} & \mathbf{v}' & \mathbf{v} \times \mathbf{v}'
\end{bmatrix}^T
\begin{bmatrix}
q_x \\ q_y \\ q_z
\end{bmatrix}\end{split}
From it, we simplify the expression for and
:
\begin{split}\begin{eqnarray}
\theta_+ & = & \arctan2(b, a) \\ \\
\theta_- & = & \arctan2(-d, c) \\
\end{eqnarray}\end{split}
To calculate , we see that:
\begin{split}\begin{eqnarray}
c_2 & = & \cos\frac{\theta_2}{2} = \sqrt{a^2 + b^2} \\ \\
s_2 & = & \sin\frac{\theta_2}{2} = \sqrt{c^2 + d^2}
\end{eqnarray}\end{split}
Using inverse trigonometrics, we find the solution for :
This yields the solution for the other two angles:
\begin{split}\begin{eqnarray}
\theta_1 & = & \theta_+ - \theta_- \\
\theta_3 & = & \theta_+ + \theta_-
\end{eqnarray}\end{split}
However, there are two **Singularities** with this approach:
- When : we get , and
is undefined.
- When : we get , and
is undefined.
In both situations, a degree of freedom is lost, and we cannot uniquely
determine angles and . This is known
as a [Gimbal Lock](https://en.wikipedia.org/wiki/Gimbal_lock).
In such cases, we set the undefined angle to zero, or set it to its
last known value.
In this implementation, the angle that is set to zero is the last
one of the sequence `seq`, so that the remaining rotation is absorbed
by the first angle. The original article instead sets
to zero. Both choices give the same orientation.
**Tait-Bryan angles.** For sequences whose three axes are all different,
the second factor of the decomposition is rotated by
around the middle axis, and the third angle changes sign.
Writing and \(\theta_3' =\varepsilon\theta_3), the proper-case formulas can be reused. After computing them, the implementation applies
code
where is the parity of the permutation
of the axes, given by the Levi-Civita symbol of their indices.
**Algorithm Summary.** The steps of the conversion are:
1. Compute the parity , and combine the components
of the quaternion into . For proper sequences,
this is a simple permutation of
(Eq. 20-22). For Tait-Bryan sequences, the components are combined
as , ,
, and ,
where are the indices of the axes.
2. Compute the middle angle as
(Eq. 33-34). This needs no normalization of the quaternion.
3. Compute and
. Then
and
(Eq. 32).
4. Apply the Tait-Bryan correction if needed, and wrap the angles to
the interval .
Since no intermediate rotation matrix is built, the method only needs
a few trigonometric functions and permutations of the components.
Parameters:
: **seq** (*str, default: ‘ZYX’*) – Sequence of three rotation axes. Upper-case letters (e.g.,
`'ZYX'`) describe **intrinsic** rotations, and lower-case letters
(e.g., `'zyx'`) describe **extrinsic** rotations. Both cannot be
mixed, and consecutive axes must be different.
Returns:
: **angles** – Euler angles of quaternion, in radians, in the order given by
`seq`.
Return type:
: numpy.ndarray
Raises:
: **ValueError** – If `seq` has not three axes, mixes intrinsic and extrinsic
axes, contains invalid axes, or has equal consecutive axes.
Examples
Rotation of around the X-axis, given as the quaternion
:
```defaultimport numpy as np q = Quaternion([np.sqrt(2)/2, np.sqrt(2)/2, 0.0, 0.0]) q.to_angles() array([0. , 0. , 1.57079633])
q.to_angles(seq='ZYZ') array([-1.57079633, 1.57079633, 1.57079633])
q.to_angles(seq='xyz') array([1.57079633, 0. , 0. ])
code
Rotation of around the Z-axis:
```default
>>> q = Quaternion([np.cos(np.pi/8), 0.0, 0.0, np.sin(np.pi/8)])
>>> q.to_angles(seq='ZYX')
array([0.78539816, 0. , 0. ])