Post · 6 min read· pp. 135–139

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 [θ1θ2θ3]T\begin{bmatrix}\theta_1 &\theta_2 &\theta_3\end{bmatrix}^T 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.

q=[qwqv]=[cos⁡(θ2)sin⁡(θ2)v]=[cos⁡(θ2)sin⁡(θ2)vxsin⁡(θ2)vysin⁡(θ2)vz]=[qwqxqyqz]\begin{split}\mathbf{q} = \begin{bmatrix} q_w \\ \mathbf{q}_v \end{bmatrix} = \begin{bmatrix} \cos\big(\frac{\theta}{2}\big) \\ \sin\big(\frac{\theta}{2}\big) \mathbf{v} \end{bmatrix} = \begin{bmatrix} \cos\big(\frac{\theta}{2}\big) \\ \sin\big(\frac{\theta}{2}\big) v_x \\ \sin\big(\frac{\theta}{2}\big) v_y \\ \sin\big(\frac{\theta}{2}\big) v_z \end{bmatrix} = \begin{bmatrix} q_w \\ q_x \\ q_y \\ q_z \end{bmatrix}\end{split}
where v=[vxvyvz]T\mathbf{v}=\begin{bmatrix}v_x & v_y & v_z\end{bmatrix}^T is a **unit vector** describing the axis of rotation about which a point rotates by an angle θ\theta. Note Remember that a **versor** is the unit quaternion commonly used to represent rotations, and it has a norm equal to one:
∥q∥=1\|\mathbf{q}\| = 1
Likewise, a **unit vector** is a vector, whose norm is also equal to one:
∥v∥=1\|\mathbf{v}\| = 1
We can decompose this relation as follows:
q=[c3s3v]⊗[c2s2v′]⊗[c1s1v]\begin{split}\mathbf{q} = \begin{bmatrix} c_3 \\ s_3\mathbf{v} \end{bmatrix} \otimes \begin{bmatrix} c_2 \\ s_2\mathbf{v}' \end{bmatrix} \otimes \begin{bmatrix} c_1 \\ s_1\mathbf{v} \end{bmatrix}\end{split}
where, for 0≤θ2≤π0 \leq \theta_2 \leq \pi:
\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 v\mathbf{v} and v′\mathbf{v}' are orthonormal unit vectors such that v⋅v′=0\mathbf{v} \cdot \mathbf{v}' = 0. A third orthonormal unit vector can be obtained from their cross product:
v′′=εv×v′\mathbf{v}'' = \varepsilon \mathbf{v} \times \mathbf{v}'
where ε=±1\varepsilon = \pm 1. The values of s2s_2 and c2c_2 are in the range [−1,1][-1, 1]. The authors chose a solution in which s2>0s_2>0, and c2>0c_2>0. A factorization with these properties leads to:
q=[qwqv⋅vqv⋅v′εqv⋅v′′]=[c2c+c2s+v+s2c−v′+s2s−εv′′]\begin{split}\mathbf{q} = \begin{bmatrix} q_w \\ \mathbf{q}_v \cdot \mathbf{v} \\ \mathbf{q}_v \cdot \mathbf{v}' \\ \varepsilon \mathbf{q}_v \cdot \mathbf{v}'' \end{bmatrix} = \begin{bmatrix} c_2c_+ \\ c_2s_+\mathbf{v} + s_2c_-\mathbf{v}' + s_2s_-\varepsilon\mathbf{v}'' \end{bmatrix}\end{split}
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 [bcd]T\begin{bmatrix} b & c & d\end{bmatrix}^T 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 θ+\theta_+ and θ−\theta_-:
\begin{split}\begin{eqnarray} \theta_+ & = & \arctan2(b, a) \\ \\ \theta_- & = & \arctan2(-d, c) \\ \end{eqnarray}\end{split}
To calculate θ2\theta_2, 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 θ2\theta_2:
θ2=2arctan⁡2(c2+d2,a2+b2)\boxed{ \theta_2 = 2 \arctan2\big(\sqrt{c^2 + d^2}, \sqrt{a^2 + b^2}\big) }
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 θ2=0\theta_2=0 : we get s2=c=0s_2=c=0, and θ−\theta_- is undefined. - When θ2=π\theta_2=\pi : we get c2=b=0c_2=b=0, and θ+\theta_+ is undefined. In both situations, a degree of freedom is lost, and we cannot uniquely determine angles θ1\theta_1 and θ3\theta_3. 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 θ1\theta_1 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 π/2\pi/2 around the middle axis, and the third angle changes sign. Writing θ2′=θ2+π/2\theta_2' = \theta_2 + \pi/2 and \(\theta_3' =

\varepsilon\theta_3), the proper-case formulas can be reused. After computing them, the implementation applies

code
θ2←θ2−π2,θ3←ε θ3\theta_2 \leftarrow \theta_2 - \frac{\pi}{2}, \qquad \theta_3 \leftarrow \varepsilon\,\theta_3
where ε=±1\varepsilon = \pm 1 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 ε\varepsilon, and combine the components of the quaternion into (a,b,c,d)(a, b, c, d). For proper sequences, this is a simple permutation of (qw,qx,qy,qz)(q_w, q_x, q_y, q_z) (Eq. 20-22). For Tait-Bryan sequences, the components are combined as a=qw−qja = q_w - q_j, b=qi+εqkb = q_i + \varepsilon q_k, c=qj+qwc = q_j + q_w, and d=εqk−qid = \varepsilon q_k - q_i, where (i,j,k)(i, j, k) are the indices of the axes. 2. Compute the middle angle as θ2=2arctan⁡2(c2+d2,a2+b2)\theta_2 = 2\arctan2\big(\sqrt{c^2+d^2}, \sqrt{a^2+b^2}\big) (Eq. 33-34). This needs no normalization of the quaternion. 3. Compute θ+=arctan⁡2(b,a)\theta_+ = \arctan2(b, a) and θ−=arctan⁡2(d,c)\theta_- = \arctan2(d, c). Then θ1=θ+−θ−\theta_1 = \theta_+ - \theta_- and θ3=θ++θ−\theta_3 = \theta_+ + \theta_- (Eq. 32). 4. Apply the Tait-Bryan correction if needed, and wrap the angles to the interval [−π,π][-\pi, \pi]. 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 π/2\pi/2 around the X-axis, given as the quaternion [cos⁡(π/4)sin⁡(π/4)00]\begin{bmatrix}\cos(\pi/4) & \sin(\pi/4) & 0 & 0\end{bmatrix}: ```default

import 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 π/4\pi/4 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.        ])