To be more concrete, I'm working on an iOS app, and have a CATransform3D
struct (basically a 4x4 transform array).
Is it possible to deduce all the different "operations" this matrix implies? Things like how much rotation, scale, etc. it implies?
Computer Graphics Stack Exchange is a question and answer site for computer graphics researchers and programmers. It only takes a minute to sign up.
Sign up to join this communityYou can decompose the matrix $\mathbf{M} = \mathbf{TRS}$ into basic transformations: translation, scaling, and rotation. Given this matrix:
$$\mathbf{M} = \begin{bmatrix} a_{00} & a_{01} & a_{02} & a_{03}\\ a_{10} & a_{11} & a_{12} & a_{13}\\ a_{20} & a_{21} & a_{22} & a_{23}\\ 0 & 0 & 0 & 1 \end{bmatrix}$$
You can decompose the translation by inspection using the last column $\mathbf{t} = (a_{03}, a_{13}, a_{23})$.
For scaling, we know that the first three columns of the matrix corresponds to the bases (axes). We can get the scale by the length/norm of these vectors, i.e. how much the bases were scaled. So the scale is $\mathbf{s} = (s_0, s_1, s_2)$ where:
$$\begin{matrix} s_0 = \left \|(a_{00}, a_{10}, a_{20}) \right \|\\ s_1 = \left \|(a_{01}, a_{11}, a_{21}) \right \|\\ s_2 = \left \|(a_{02}, a_{12}, a_{22}) \right \|\\ \end{matrix}$$
Now you have the scale, you can get rid of it using the $3\times 3$ sub-matrix that corresponds to $\mathbf{RS}$ by multiplying the matrix with the inverse of the scale $\mathbf{S}^{-1}$ to get $\mathbf{R}$
$$\begin{align} \mathbf{(RS)S}^{-1} &= \begin{bmatrix} a_{00} & a_{01} & a_{02}\\ a_{10} & a_{11} & a_{12}\\ a_{20} & a_{21} & a_{22}\\ \end{bmatrix} \begin{bmatrix} s_0 & 0 & 0\\ 0 & s_1 & 0\\ 0 & 0 & s_2 \end{bmatrix}^{-1} \\ &= \begin{bmatrix} a_{00} & a_{01} & a_{02}\\ a_{10} & a_{11} & a_{12}\\ a_{20} & a_{21} & a_{22}\\ \end{bmatrix} \begin{bmatrix} 1/s_0 & 0 & 0\\ 0 & 1/s_1 & 0\\ 0 & 0 & 1/s_2 \end{bmatrix} \end{align}$$
Thus ($\mathbf{(RS)S}^{-1} = \mathbf{RI} = \mathbf{R}$):
$$\mathbf{R} = \begin{bmatrix} a_{00}/s_0 & a_{01}/s_1 & a_{02}/s_2\\ a_{10}/s_0 & a_{11}/s_1 & a_{12}/s_2\\ a_{20}/s_0 & a_{21}/s_1 & a_{22}/s_2\\ \end{bmatrix}$$
This is the final rotation matrix. You can further decompose it using many ways. It is quit lengthy but you can search for decomposing a rotation matrix.
This method only gives an equivalent values in the form of translation, scaling and rotation (the original matrix maybe the result of other types of transformations). It may has problems with floating point precision with the rotation angles if you further used the decomposed angles, rounding errors may accumulate in the computations. You should not use it unless you did not construct the matrix yourself.
If you are the one who constructed the matrix and wanted the decomposition in order to be able to edit and display the translation, scale and rotation individually and independently, probabbly the cleanest why is to store the components of $\mathbf{t}$, $\mathbf{s}$ and $\mathbf{r}$ in a transform class individually as vectors (maybe quaternion for the rotation). Only when you need the transform matrix, construct a $\mathbf{TRS}$ matrix from these components (You can cache the matrix until some component is changed).