[comp.graphics] Rotational matrices

vlg@fctunl.rccn.pt (Vicente Luis Gracias) (08/08/90)

       Does anybody know how I can determine the description of
rotations given a rotational matrix? That is, for example, given a
matrix M that is ONLY composed by rotations on the X, Y or Z axis, how
can I determine each of the rotations (e.g. M = (RotZ a)(RotX b)...)?
There is more than one solution for the equation, but for my case any
one of the solutions is enough. I have tried "extracting" a rotation
on X by an angle that satisfies some conditions, then on Y and then on
Z, cyclically until I get an identity matrix. But this does not always
work and sometimes it loops infinitely. Also the composition of
rotational matrices is not commutative,so RotX.RotY.RotX <> RotX.RotX.RotY.

                                                     Thanks,
                                                            Vicente

platt@ndla.UUCP (Daniel E. Platt) (08/10/90)

In article <VLG.90Aug8124538@hal.fctunl.rccn.pt>, vlg@fctunl.rccn.pt (Vicente Luis Gracias) writes:
> 
>        Does anybody know how I can determine the description of
> rotations given a rotational matrix? That is, for example, given a
> matrix M that is ONLY composed by rotations on the X, Y or Z axis, how
> can I determine each of the rotations (e.g. M = (RotZ a)(RotX b)...)?

Hi,

This problem may be handled by noting that, given an axis 'n' (unit vector)
that a vector may be broken up into two parts:

	R = n(n.R) + (R - n(n.R))

where n.R is the length of the projection of R onto the axis n, and the
other term is the vector part of R perpendicular to n.  This may be
seen by noting that

	n.(R - n(n.R)) = n.R - (n.n)(n.R) = n.R - n.R = 0.

Since n != 0 and R != 0, the vectors n and R-n(n.R) must be perpendicular.
A third vector may be constructed to complete the basis:

	n X (R - n(n.R)) = n X R

where X = vector cross product.  Now, a rotation of an angle A about
n would be a superposition of these three vectors

	n(n.R), R - n(n.R), n X R.

Since the part of the vector along n is preserved, and the parts perpendicular
to the axis essentially move in a circular composition of these two other
bases, we have

	R' = n(n.R) + (R - n(n.R)) cos A + n X R sin A.

The next twist is to introduce "dyadic" notation.  An outer product of
a vector U with another vector V is written UV.  The dot product (inner product)
of this thing with another vector W is (UV).W and is interpreted as U(V.W), and
thus is a vector.  Further, W.(UV) != (UV).W.  A unit dyadic is one that always
produces the same vector when an inner product is taken.  For example:

	R = x(x.R) + y(y.R) + z(z.R)

where x, y, z are unit vectors in the x, y, and z directions respectively.
Then

	R = (xx + yy + zz) . R,

and I = xx + yy + zz is the unit matrix.  The relation between a dyadic
and a matrix is

    xx =    (1  0  0)   xy =   (0  1  0) ...
            (0  0  0)          (0  0  0)
            (0  0  0)          (0  0  0)

etc.

Cross products may be written as an inner product of a vector
and a matrix:

	n X R = (n X I).R

The cross product of a vector with I is a matrix:

    (n X I) = (  0   -n_z    n_y)
			  ( n_z    0    -n_x)
              (-n_y   n_x     0 )

It's possible to construct cross products between any vector and any
matrix:

	u X V = (u X I) . V

where u X I is easily computed.

Now, back to the rotated vector:

	R' = (nn + (I - nn) cos A + (n X I) sin A).R

It follows that the rotation matrix (dyadic) can be written:

	R' = U.R

where

	U = nn + (I - nn) cos A + (n X I) sin A.

A vector 'n' corresponding to the axis of rotation may be obtained by
noting that

	U.n = nn.n + (I - nn).n + (nXI).n
		= n(n.n) + (n - n(n.n)) + nXn
		= n + 0 + 0 = n.

In other words, n is an eigenvector of U with eigenvalue 1 (there's only
two unit vectors that satisfy this relationship -- proving that is another
problem).  The algebra for the above problem is fairly straight forward.
Once you have the axis of rotation, you can get spherical angles (theta, phi)
to specify it:

	n = x sin(theta) cos(phi) + y sin(theta) sin (phi) + z cos(theta).

Also, the angle A may be uniquely determined given some axis n by looking
at the trace of U and nXU.

	Tr(U) = Tr(nn) + Tr(I - nn) cos A + Tr(nXI) sin A
		  = 1 + (3 - 1) cos A + 0 sin A = 1 + 2 cos A.

	n X U = n X (nn) + n X (I - nn) cos A + n X (n X I) sin A
		  = 0 +  n X I cos A + (nn - I) sin A.

	Tr(n X U) = Tr(n X I) cos A + Tr(nn - I) sin A
			  = 0 cos A + (1 - 3) sin A.

Thus:

	cos A = (Tr(U) - 1) / 2,
	sin A = - Tr(n X U) / 2.


I think you can do it this way.  I've had good luck using the above technique.
Hope it helps.

Sincerely,
Dan Platt