# How to extract rotation matrix and scale vector from a 3D affine transformation?

For the affine transformation: $$\begin{bmatrix} a & b & c\\ e & f & g\\ i & j & k\\ \end{bmatrix}$$

how do I extract the rotation and scale parts?

According to this answer, the scale along each axis can be extracted by taking the length of the respective matrix column, but what about the sign of the scale? This is especially interesting for me because the matrix might be used to flip around an axis using a scale of -1.

• You must have $i=j=0$ for this to represent an affine transformation. – amd Nov 8 '18 at 18:38
• The OP is a little confused, I believe. As you can see from comments on my answer, the software returns a translation and a 3x3 matrix (which OP says represents an affine transformation, but you and I know represents the somewhat more restrictive linear transformation). I, too, thought that OP was representing affine transformations using homogeneous coords, but that appears not to be what's going on. – John Hughes Nov 8 '18 at 20:22

One reason that you're not finding answers is that there isn't actually an answer in general. Some affine transformations (even without a translation, like this one): $$\pmatrix{1 & 1 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1}$$ cannot be written as a product of any scale and any rotation. So there's no solution there.

On the other hand, a matrix like $$\pmatrix{-1 & 0 & 0 \\ 0 & -1 & 0 \\ 0 & 0 & 1}$$ can be written as a product of a scale and a rotation in two different ways:

1. rotate $$180$$ degrees in the $$xy$$-plane, scale $$= (1, 1, 1)$$

2. rotate $$0$$ degrees, scale $$= (-1, -1, 1)$$

In fact, if you have

rotation by $$A$$ degrees in the $$xy$$ plane; scale by $$(p, q, 1)$$,

that's always the same as "rotate by $$A + 180$$ degrees, scale by $$(-p, -q, 1)$$" so the answer is never unique. Even the identity transformation is both "no rotation, scale by $$(1,1, 1)$$" and "rotate 180 degrees, scale by $$(-1, -1, 1)$$."

I know that's not the answer you wanted, but it's the truth, and it might help you stop looking for an answer that doesn't exist. (Or it might help you to reformulate your question to one that does have an answer.)

• I am using code that returns a transformation in two parts. A 3D translation vector, and 3x3 affine matrix that "describes scaling and rotation". I would like to create a rotation Quaternion and a scale vector from this matrix. they do not have to be unique, they just need to have the same effect as the matrix. I hope this explains my problem. – RandomName Nov 8 '18 at 19:29
• It explains your problem, but it doesn't mean that your problem has an answer. (In my answer, I was assuming you were working with homogeneous coordinates in 2D, but my remarks all work for 3D as well, with small modification.) Are you sure that your matrix transformations are all merely "affine"? Or are they guaranteed to be similarities, in which case such a factorization is indeed possible? – John Hughes Nov 8 '18 at 20:18
• I think it is fair to assume that there is no shear, since the documentation mentions only rotation and scale. – RandomName Nov 8 '18 at 22:02
• I don't think that's "safe to assume" at all without a lot more context, but that's just me. My 40 years with graphics software have made me cynical. If you include that condition --- that it's known that the matrix can be written as a product of a single rotation and a single scale --- to the problem statement as an addendum, then I'll give you a complete answer. Note that saying "it's a product of only rotations and scales" doesn't eliminate shear, a result that (amazingly) goes back to Gauss, and shows that every shear can be written as a product of rotations and scales, very simply. – John Hughes Nov 9 '18 at 12:24

A pure rotation matrix will have determinant 1. Find the determinant of the given matrix. That will be the "scale". Dividing the matrix by its determinant gives the pure rotation matrix.

• You might look at the first example in my answer to see why what you've said is not correct. – John Hughes Nov 8 '18 at 18:50
• Also: Divide $\pmatrix{2 & 0 \\ 0 & \frac{1}{2}}$ by its determinant (which is $1$), and you don't get a rotation. – John Hughes Nov 9 '18 at 16:43