## Arbitrary matrix transform from scale & rotate?

### Arbitrary matrix transform from scale & rotate?

Hello,

I need to represent an arbitrary 2x2 matrix transform:

|A B|
|C D|

as the product of some series of scaling operations:

|H 0|
|0 V|

and rotation operations:

| cos sin|
|-sin cos|

I believe that it can be done by concatenating a rotation, a scale,
and then another rotation, but I'm not sure.

Does anyone have this info handy?

Thanks,
Scott Howlett

--

### Arbitrary matrix transform from scale & rotate?

> > I believe that it can be done by concatenating a rotation, a scale,
> > and then another rotation, but I'm not sure.

> You only need a single scale and rotate.

> scale x rotate

Not likely : a general 2x2 matrix has 4 four free parameters. A rotation
has 1 and a scale has 2...

More precisely, let
(a b)
(c d)
(h 0)
(0 v)
the scaling, and t the angle of the rotation
scale*rotation = ( h cos(t) h sin(t))
(-v sin(t) v cos(t))
if this equals the matrix, then (in particular)
c=-vsin(t)
d=vcos(t)
so d*d-c*c=v*v, if d=1 and c=2, v*v=-3,
(and v can't be complex, because c=vcos(t) is real...)

Regards
Francois

### Arbitrary matrix transform from scale & rotate?

Sorry, I misunderstood the original post. The problem is to decompose
an arbitrary matrix in terms of rotations and scales.

This is impossible, as the matrix might also contain non affine
transformations.

You can only do this if you can be certain that the original
matrix is made up of rotations and scales, which might not
be the case.

--

<\___/>
/ O O \
\_____/  FTB.

Those who do not understand Unix are condemned to
reinvent it, poorly.              - Henry Spencer

### Arbitrary matrix transform from scale & rotate?

> Hello,

> I need to represent an arbitrary 2x2 matrix transform:

> |A B|
> |C D|

> as the product of some series of scaling operations:

> |H 0|
> |0 V|

> and rotation operations:

> | cos sin|
> |-sin cos|

> I believe that it can be done by concatenating a rotation, a scale,
> and then another rotation, but I'm not sure.

> Does anyone have this info handy?

This is definitely possible, and here is a basic way to show how to do it
(warning: this is certainly not optimal! I suppose this kind of
transformation can be found in books on matrix).

Let M be your matrix, if it is symetric (B=C), then it is diagonalisable
in an orthogonal basis, that is, there is one rotation R, and a scaling S
such that
M = R* S R (where R* is the inverse of R), let t be the angle of R, and
Rot(t) denote the rotation with angle t,

M = Rot(-t) S Rot(t)
for any symetric M (there should be some efficient algorithms to find t
and S, I don't have any book handy here...)

Now, suppose M is not symetric, and let u be some angle, let's see
whether Rot(-u) M can be symetric:

Rot(-u) M = | a cos(u) - c sin(u)  b cos(u) - d sin(u) |
| a sin(u) + c cos(u)  b sin(u) - d cos(u) |

it is symetric iff:
a sin(u) + c cos(u) = b cos(u) - d sint(u)

(a+d)sin(u) = (b-c)cos(u)
that is if u = arctg( (b-c)/(a+d)) if a+b!=0
(when you implement this; as you ultimately need sin(u) and cos(u), you
won't need to explicitly calculate the arctg())
and u = PI/2 else.

Now as Rot(-u) M is symetric, there exists some scaling S and t such as

Rot(-u) M = Rot(-t) S Rot(t)
so

M = R(u-t) S Rot(t)

Regards
Francois

### Arbitrary matrix transform from scale & rotate?

> Hello,

> I need to represent an arbitrary 2x2 matrix transform:

> |A B|
> |C D|

> as the product of some series of scaling operations:

> |H 0|
> |0 V|

> and rotation operations:

> | cos sin|
> |-sin cos|

> I believe that it can be done by concatenating a rotation, a scale,
> and then another rotation, but I'm not sure.

> Does anyone have this info handy?

You only need a single scale and rotate.

scale x rotate

--

<\___/>
/ O O \
\_____/  FTB.

Those who do not understand Unix are condemned to
reinvent it, poorly.              - Henry Spencer

### Arbitrary matrix transform from scale & rotate?

Quote:> This is impossible, as the matrix might also contain non affine
> transformations.

A 2x2 matrix will always preserve parallel lines, so I'm not
sure what you mean here.

You could get a shear, for example, by rotating 45 degrees
and then scaling vertically. You'll get a vertically
stretched diamond. But that's still an affine transform.

I'm doing image manipulation. I have code which rotates
and resizes images, and I want to be able to produce
arbitrary matrix transforms from some combination of
these operations.

My rotation, though, is actually implemented as a set
of three concatenated shears, so a better question for
me to be asking would be "what is the minimum number
of shears & scales (each dimension counts separately)
I need to do to produce an arbitrary 2x2 matrix
transform?"

- Scott

--

### Arbitrary matrix transform from scale & rotate?

: Sorry, I misunderstood the original post. The problem is to decompose
: an arbitrary matrix in terms of rotations and scales.

: This is impossible, as the matrix might also contain non affine
: transformations.

: You can only do this if you can be certain that the original
: matrix is made up of rotations and scales, which might not
: be the case.

I think it is probably possible to represent any 2x2 matrix
in terms of a rotate, scale, and shear operation:

shear:

1 0
v 1

and/or likewise with other 0 replaced with some value instead.

### Arbitrary matrix transform from scale & rotate?

< snip >

Quote:>    "what is the minimum number
> of shears & scales (each dimension counts separately)
> I need to do to produce an arbitrary 2x2 matrix
> transform?"

The answer is 3 shears and 1 (1D) scale, and the scale can be included
in one of the shears. If you want subpixel translation, it can also be
included in a shear (with almost no hit in speed).

I think you can deduce it yourself (at least with a hint).

[x2]   [m3 s3 t3]   [ 1  0  0]   [m1 s1 t1]   [x1]
[y2] = [ 0  1  0] * [s2 m2 t2] * [ 0  1  0] * [y1]
[ 1]   [ 0  0  1]   [ 0  0  1]   [ 0  0  1]   [ 1]
or
[x2]   [ 1  0  0] * [m2 s2 t2] * [ 1  0  0] * [y1]
[y2] = [s3 m3 t3]   [ 0  1  0]   [s1 m1 t1]   [x1]
[ 1]   [ 0  0  1]   [ 0  0  1]   [ 0  0  1]   [ 1]

mi - magnify, two of these should be 1.
si - shear.
ti - translate, one of these should be 0.

I don't know which form you shold use, which mi's should be 1, and which
ti should be 0, to make it optimal (that is, to give the intermediate
images a good size). Figure out that yourself. If you don't want the
translation part, just use the upper left 2x2 matrixes.

Well how is the parameters calculated?
Just multiply the matrixes, and identify with your wanted matrix. The
equations you get shouldn't be that difficult to solve, (a little tip:
m1*m2*m3=det(original matrix), and two of the mi's is 1).

Hope this will help.

MrBasic

I using DirectX to render objects modeled in 3DS.
But some objects are mirrors, that means that they have been scaled with negative values.
I need to find such cases. I have the 3D transform matrix (4x3), i know how to get the translation,
but i don't known how to get the scale, all that i need is the sign of the scale, that can be non uniform (x<>y<>z)
Any ideias ?