I need to retrieve scale/rotation/move… from a given 4x4 transform matrix (I use a computer vision librairy (ARToolkit Plus) which only gives me a 4x4 matrix as a result).
My code currently looks like this:
So I need is an algorithm/librairy which could transform back this matrix into classic components such as rotation, move, scale…
I think this has been discussed recently.
There is no way to reconstruct the sequence of glTranslate/glRotate/glScale but you can get final position, scale and angles from the matrix.
Position would be actually last column of the matrix.
x-scale would be length of vector created from first row of the matrix (first 3 columns of this row).
y-scale would be in second row and z-scale would be in third row.
The angles are actually dependent on what you choose as your description of objects orientation. Let’s say it’s like an airplane - heading, pitch and roll.
First row of the matrix is the x vector (right), second row is y (up) and third row is z (forward).
So heading is easy - look at forward vector from top - arctan(forward.x/forward.z) - of course take care of divide by zero and take signs of .x and .z into account.
Now rotate the up vector by -heading. Look at it from left side (or right) - pitch=arctan(up.z/up.y)
Look at it from front/back - roll=arctan(up.x/up.y)
I believe the “Graphics Gems” books, volumes 2 and 3, had algorithms for decomposing transformation matrices. You might google for that.
After ripping out the translation of column 3, you could use singular value decomposition on the remaining 3x3 matrix.
This is the tool to deal with all the ugly cases that the literature usually doesn’t cover (the ones I’ve seen make some quite restrictive assumptions about the matrix). For each matrix M, you get matrices U,S,V where S is a scaling matrix and U and V are isometries (rotations and maybe reflections) and U.S.transpose(V)=M.
You find an SVD implementation in LAPACK (http://www.netlib.org/lapack/)