## Extracting scale (both sign and magnitude) from generic matrix

### Extracting scale (both sign and magnitude) from generic matrix

I need to extract translation, rotation, and non-uniform scale values
from a given transformation matrix (generated by 3DStudio Max).
Translation and rotation are easy.  The magnitude of the scale factors
has also been easy to find.  The problem is, the matrix may also have
one or more mirrored axes encoded inside of it.  How can I determine
which of the three axes have been mirrored (i.e. how to find BOTH the
sign and the magnitude of the scale factor).

I searched through all the posts here that I can find on the topic
without real success. There are quite a number of posts on the
subject, but none seems to address how to get the sign of the scale
factors.

I know that calculating the determinate of the matrix tell whether the
resulting coordinate system is left-handed or right-handed (i.e. given
that I am starting with a right-handed system, I can determine if one
or three of the axes have been flipped).  Several people have
suggested, in regards to algorithms such as Shoemake's decomp_affine
(), that when the det(M) < 0 the scale factors should all be
multiplied by -1.  The problem is, not ALL of the factors are
negative.  I need to know which ones are flipped.

I have tried the following approaches to extracting both rotation and
scale from the matrices of mirrored and non-uniformly scaled nodes:

1. Using decomp_affine () inside of Max.
This works great for extracting the magnitude of the scale in the
AffineParts.k vector, but the sign of the scale factors are encoded in
the quaternion part, AffineParts.q, not in the k vector. In other
words, k is always positive for all three components, even if one or
three of the axis have been mirrored.  Is this to be expected?
According to Shoemake's algorithm will the k factors always be
positive?  Does it make sense that the scale signs are encoded in the
quaternion?

Does anyone know of a way to extract the axis flip information from a
given quaternion q?  (I know the mirroring info is in the quaternion,
because if I reconstruct a matrix from q and compare it against the
original matrix, the axis flips are reconstructed also).

2. Convert the matrix to Heading, Pitch, Roll, then work backwards.
After finding the HPR, calculate a corresponding matrix from those
values. Take the inverse of that matrix and multiply it times the
original matrix. I'm working under the assumption that this will
"null" out the rotation portion of the original matrix and leave only
the scale factors.  Well, it does indeed leave the magnitude of the
scale factors, but once again the sign is being lost.

A similar procedure starting from decomp_affine and using q to
generate an inverse rotation matrix, yields the same
results--magnitude, but not sign.

3.  I tried to use a Singular Value Decomposition algorithm provided
in the Gandalf open source project, but the diagonal vector was not
returning meaningful results.  (I may be using the Gandalf code wrong.
Haven't spent much time on it).  Should SVD extract both the sign and
magnitude of the scale factors?

Does anyone know what I am missing? Is this simply a fact of life,
that sign gets so mixed up inside the matrix that it can never be
extracted? Has anyone had success solving this problem? Can a
quaternion with both rotation and reflection in it be decomposed into
rotation and reflection parts?

Any help would be greatly appreciated.

### Extracting scale (both sign and magnitude) from generic matrix

>I need to extract translation, rotation, and non-uniform scale values
>from a given transformation matrix (generated by 3DStudio Max).
>Translation and rotation are easy.  The magnitude of the scale factors
>has also been easy to find.  The problem is, the matrix may also have
>one or more mirrored axes encoded inside of it.  How can I determine
>which of the three axes have been mirrored (i.e. how to find BOTH the
>sign and the magnitude of the scale factor).

Consider the matrix

[1  0  0]
[0 -1  0]
[0  0 -1]

This is a 180 degree rotation around the x axis. An alternative way to
look at it is as mirroring of both the y and z axes. Now consider

[-1 0 0]
[ 0 1 0]
[ 0 0 1]

which is the negative of the first matrix. This is a mirroring of the
x axis, or a 180 degree rotation around the x axis combined with three
mirrorings (an inversion).

Conclusion? You're asking for the impossible.

Shoemake and Duff's GI '92 paper, with Shoemake's code in Graphics
Gems IV, is as good as you can hope to do. If you can find a copy of
the proceedings, the paper explains the issues. The GGIV article is a
much abbreviated introduction to the code.

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 ?