## Pinned Update #1

The Darc Library (C++) is now updated as of March, 2012. Click ▷here too browse the entire solution.

## Thursday, July 7, 2011

### Matrix to Euler Angles

Every orientation in 3D space can be described using the product $$T$$ of three rotation matrices, usually around the main axes $$R_X$$, $$R_Y$$ and $$R_Z$$. Given such a matrix $$T$$, it is often necessary to decompose it into three seperate angles - the Euler Angles. These can be used as input arguments for a sequence of basic rotation operations, so that the following equation holds:

\begin{align} T = R_i(\alpha_0) R_j(\alpha_1) R_k(\alpha_2) \end{align}

The choice of $$i, j, k$$ determines the rotation convention. One common possibility would be Yaw-Pitch-Roll (XYZ, depending on the local coordinate system) but the following implementation works for arbitrary conventions.

Mathematics

The decomposition of $$T$$ depends highly on the definition (internal implementation) of $$R_i$$ so make sure you know what your matrix class is doing. The standard definition of rotation matrices around X, Y and Z looks like this:

 \begin{align} R_X=\begin{pmatrix} 1 & 0 & 0 \\ 0 & A & -B \\ 0 & B & A \end{pmatrix} \end{align} \begin{align} R_Y=\begin{pmatrix} C & 0 & D \\ 0 & 1 & 0 \\ -D & 0 & C \end{pmatrix} \end{align} \begin{align} R_Z=\begin{pmatrix} E & -F & 0 \\ F & E & 0 \\ 0 & 0 & 1 \end{pmatrix} \end{align}

Here $$A, C, E$$ denote the cosine and $$B, D, F$$ the sine of the unknown Euler Angles. Let's take a look at $$T$$ for the convention YXY.

\begin{align} T=R_{YXY}= \begin{pmatrix} CC-DDA & DB & CD+DAC \\ DB & A & -CB \\ -CD-DAC & CB & -DD+CCA \end{pmatrix} \end{align}

Apparently the element (indexing starting with 0) at $$T(1,1)$$ is the cosine of the Euler Angle $$\alpha_1$$ for the middle rotation matrix $$R_X$$. This is true for all of the 12 possible conventions. However the location of this pivot element is not always the same. The coordinates are determined by the first and third rotation component by using the simple conversion $$X \rightarrow 0$$, $$Y \rightarrow 1$$ and $$Z \rightarrow 2$$. The next step is to compute $$\alpha_0$$ and $$\alpha_2$$ using the elements horizontal and vertical of the pivot. Knowing $$A$$ one can easily compute $$\alpha_1$$ and therefore $$B$$. You can now use the horizontal neighbors of the pivot element to get $$\alpha_2$$ and the vertical neighbors to get $$\alpha_0$$. Since the applied convention uses the same axis for the first and third rotation there are no $$E, F$$ components in this matrix.

Gimbal Lock

Now what happens if $$B$$ is close to zero in the above case? This means a so called Gimbal Lock has occured because the divisions in the row and column of the pivot element are undefined. This can happen if the orientation of $$T$$ is so that one angle becomes ambiguous which means it can be set to any value, e.g., $$\alpha_0 = 0$$. Since one Euler Angle is now zero the corresponding rotation matrix yields $$R_i=I$$ and the new reduced transformation matrix looks like this:

\begin{align} T=R_{XY}= \begin{pmatrix} C & 0 & D \\ DB & A & -CB \\ -DA & B & CA \end{pmatrix} \end{align}

Interpreting the axes as coordinates again a new pivot element is found at $$T(0,1)$$, which is always zero. Once again it's horizontal neighbors can be used to determine the remaining Euler Angle $$\alpha_2$$. Keep in mind that the order of cosine and sine elements may vary depending on the convention applied. As previously mentioned there are 12 possibilities.

:: CODE BLOCK ::
 $$R_X$$ - pivot $$R_Y$$ pivot $$R_Z$$ pivot Y-X-Y X-Y-X X-Z-X Y-X-Z X-Y-Z X-Z-Y Z-X-Y Z-Y-X Y-Z-X Z-X-Z Z-Y-Z Y-Z-Y
Listing: A - "Conventions"

If you want to construct an Euler Angle decomposition for a special convention just follow these instructions to extract the necessary equations. I recommend a symbolic calculation, e.g., in MatLab to make this task easier.