djcmm476
djcmm476

Reputation: 1813

Decompose 3x3 Matrix

I have a 3x3 matrix I'm using to track movement in 2D. I need to extract from that the translation, rotation and scale matrices. Can anyone suggest how I would do this? I've had no luck searching online so far (possibly I'm using the wrong terms).

Upvotes: 2

Views: 2195

Answers (2)

B. Young
B. Young

Reputation: 191

For a column major matrix, given:

  • X vector of < Xx, Xy >;
  • Y vector of < Yx, Yy > and
  • Translation/position of < Tx, Ty >,

    The corresponding column major transformation matrix is:

enter image description here

The translation is:

enter image description here

For the Scaling, the scale factors for the X and Y vectors are:

enter image description here

enter image description here

The scaling transformation is then:

enter image description here

For Rotation, the normalized vectors are:

X Vector: < Xx/Sx, Xy/Sx >

Y Vector: < Yx/Sy, Yy/Sy >

And the rotation transformation "R" is:

enter image description here

RECOMPOSITION:

Given translation "T", rotation "R", and scaling "S" transformation in column major order, the original matrix/transform can be recreated with:

            T * R * S

Assumption, the original transformation has X and Y vectors that are:

  • Not parallel;
  • Not a length of zero;
  • Not mirrored; and
  • Not skewed/sheared.

The rotation can be further decomposed into an angle as described by 3Dave (which is row major, and needs to be transposed to be column major).

Upvotes: 1

3Dave
3Dave

Reputation: 29051

This is just off the top of my head so there may be an error in here, but:

Assuming your matrix is row-major (just transpose everything if you're using column major):

|  cos(t)   -sin(t)   0 |
|  sin(t)    cos(t)   0 |
|        tx        ty 1 |

The translation vector will be the last row in the matrix [tx ty 1]. Extracting scale and rotation in a composed matrix is a bit trickier.

Looking at a 3x3 rotation matrix,

| cos(t)  -sin(t) 0 |
| sin(t) cos(t) 0 |
| 0       0      1 |

And a scale matrix

| vx  0   0 |
| 0   vy  0 |
| 0   0   1 |

The combined rotation & scale matrix might look like (ct = cos(t), st = sin(t))

|  vx*ct  -vx*st   0 |
|  vy*st  vy*ct   0 |
|      0      0   1 |

For uniform scaling, vx=vy.

|  v*ct  -v*st   0 |
|  v*st  v*ct   0 |
|      0      0   1 |

Remembering the trig identity

ct^2 + st^2 = 1

We can see that the

(v*ct)^2 + (v*st)^2 = v^2

or

v^2*ct^2 + v^2*st^2 = v^2

... all the terms of which are in the composite (scale,rotation,translation or SRT for short) matrix,

So,

v = sqrt((v*ct)^2 + (v*st)^2)

or

v = sqrt(M[0,0]^2 + M[0,1]^2);

Theta, then is just

t = acos(vct/v)

or

t = acos(M[0,0]/v)

or, this might work much easier but I haven't tried it:

theta = atan2(vst,vct), 
scale = sqrt(vst^2+vct^2)

where ^ is an exponent, not an XOR.

... and you can work out the rest.

It would be wiser to keep your scale, rotation and translation values around and use those values both to build the matrix and for whatever other tasks you require. Relying on the matrix as the only container for that information will eventually lead to compound floating-point errors and other drama.

Some notes:

Theta is an angle. It's a fun, easy-to-draw Greek symbol, and all us engineers love greek characters. It's mostly Stockholm syndrome since some of them look like 5's, and some of them are impossible to draw unless you're Greek.

This works great for 2D, where there is only one possible rotation axis. If you're working in three (or higher! It's a thing now!) dimensions, the concept is the same but the implementation becomes much, much messier.

st, ct, vst, vct, etc. are all contained in the composite matrix you have available. (Composite == concatenated == combined. The terminology in use depends on who you're reading.) The non-intuitive part is extracting the elements that you need from the matrix. This requires understanding what is in the matrix, where, and why.

This is a solvable problem. Normally, I'm a sit-down-and-start-coding kind of guy, and in 25 years of professional development, I'd have to say that it's worked out pretty well so far. But, there are times where a whiteboard or a notebook and a mechanical pencil are better friends than your keyboard.

Upvotes: 3

Related Questions