34
$\begingroup$

I have this problem from my Graphics course. Given this transformation matrix:

$$\begin{pmatrix} -2 &-1& 2\\ -2 &1& -1\\ 0 &0& 1\\ \end{pmatrix}$$

I need to extract translation, rotation and scale matrices. I've also have the answer (which is $TRS$): $$T=\begin{pmatrix} 1&0&2\\ 0&1&-1\\ 0&0&1\end{pmatrix}\\ R=\begin{pmatrix} 1/\sqrt2 & -1/\sqrt2 &0 \\ 1/\sqrt2 & 1/\sqrt2 &0 \\ 0&0&1 \end{pmatrix}\\ S=\begin{pmatrix} -2/\sqrt2 & 0 & 0 \\ 0 & \sqrt2 & 0 \\ 0& 0& 1 \end{pmatrix} % 1 0 2 1/sqrt(2) -1/sqrt(2) 0 -2/sqrt(2) 0 0 %T = 0 1 -1 R = /1/sqrt(2) 1/sqrt(2) 0 S = 0 sqrt(2) 0 % 0 0 1 0 0 1 0 0 1 $$

I just have no idea (except for the Translation matrix) how I would get to this solution.

$\endgroup$
  • $\begingroup$ The principles described here don't apply to the given matrix. This is where I start losing hope of understanding this. $\endgroup$ – metavers Nov 14 '12 at 18:10
  • 1
    $\begingroup$ The upper left element of $S$ should be $-2\sqrt{2}$, not $-2/\sqrt{2}$. $\endgroup$ – celtschk Jun 11 '13 at 20:19
38
$\begingroup$

I am a person from the future, and I had the same problem. For future reference, here's the algorithm for 4x4. You can solve your 3x3 problem by padding out your problem to the larger dimensions.

Start with a transformation matrix:$$ \begin{bmatrix} a & b & c & d\\ e & f & g & h\\ i & j & k & l\\ 0 & 0 & 0 & 1 \end{bmatrix} $$

  1. Extract Translation
    This is basically the last column of the matrix:$$ \vec{t} = <d,h,l> $$While you're at it, zero them in the matrix.

  2. Extract Scale
    For this, take the length of the first three column vectors:$$ s_x = \|<a, e, i>\|\\ s_y = \|<b, f, j>\|\\ s_z = \|<c, g, k>\|\\ \vec{s} = <s_x,s_y,s_z> $$

  3. Extract Rotation
    Divide the first three column vectors by the scaling factors you just found. Your matrix should now look like this (remember we zeroed the translation):$$ \begin{bmatrix} a/s_x & b/s_y & c/s_z & 0\\ e/s_x & f/s_y & g/s_z & 0\\ i/s_x & j/s_y & k/s_z & 0\\ 0 & 0 & 0 & 1 \end{bmatrix} $$This is the rotation matrix. There are methods to convert it to quaternions, and from there to axis-angle, if you want either of those instead.

resource

$\endgroup$
  • 2
    $\begingroup$ It is not precisely true. Matrix obtained in third step has still 6 degrees of freedom, and it is composition of shear and rotation. $\endgroup$ – peper0 Oct 6 '15 at 18:19
  • $\begingroup$ The OP did not specify shear was an option. The rotation matrix may produce a degenerate quaternion, but this is easy to detect during the conversion, and you should be doing that anyway. $\endgroup$ – imallett Oct 6 '15 at 19:00
  • 2
    $\begingroup$ This works when scale is positive, however when odd number of scale components were negative, this won't get correct result. Would it help to multiply scale vector by sign of matrix diagonals? $\endgroup$ – Ondrej Petrzilka Mar 24 '16 at 22:15
  • $\begingroup$ But how do we find reflections. sx, sy, sz values can be negative too. $\endgroup$ – nbsrujan Apr 21 '16 at 9:47
11
$\begingroup$

It appears you are working with Affine Transformation Matrices, which is also the case in the other answer you referenced, which is standard for working with 2D computer graphics. The only difference between the matrices here and those in the other answer is that yours use the square form, rather than a rectangular augmented form.

So, using the labels from the other answer, you would have

$$ \left[ \begin{array}{ccc} a & b & t_x\\ c & d & t_y\\ 0 & 0 & 1\end{array}\right]=\left[\begin{array}{ccc} s_{x}\cos\psi & -s_{x}\sin\psi & t_x\\ s_{y}\sin\psi & s_{y}\cos\psi & t_y\\ 0 & 0 & 1\end{array}\right] $$

The matrices you seek then take the form:

$$ T=\begin{pmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{pmatrix}\\ R=\begin{pmatrix} \cos{\psi} & -\sin{\psi} &0 \\ \sin{\psi} & \cos{\psi} &0 \\ 0 & 0 & 1 \end{pmatrix}\\ S=\begin{pmatrix} s_x & 0 & 0 \\ 0 & s_y & 0 \\ 0 & 0 & 1 \end{pmatrix} $$

If you need help with extracting those values, the other answer has explicit formulae.

$\endgroup$

protected by T. Bongers Dec 13 '18 at 18:00

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.