[comp.graphics] three dimensional rotation

11BDODD@GALLUA.BITNET (02/15/90)

  Is there any algortihm that relates to three dimensional rotation?
Hope you all know about it.


Thank-you,
Beverly Dodd

Beverly Dodd          Bitnet: 11bdodd@gallua.bitnet People/Link: B.DODD
Gallaudet University   Internet: 11bdodd@gallux.gallaudet.edu GEnie: B.DODD1
P.O. Box 489, 800 Florida Ave., N.E., Washington, DC 20002
"Gallaudet University is the only university for the deaf in the world."

xanthian@saturn.ADS.COM (Metafont Consultant Account) (02/15/90)

(This also got emailed; I'm just showing off.)

Hi, again, Beverly!

(Why do I get the feeling the net is doing your homework for you?)

You really need the books earlier recommended to you; the answers are
all in there.  But anyway.

Rotations are a subset of the general 3D linear transformation, which
includes combinations of translate, rotate, scale operations.  The
usual way to accomplish these is to represent each (x,y,z) coordinate
triple as a _homogeneous_coordinate_ V, which is a vector quadruple V =
[x,y,z,w] of real numbers.  For any point in world coordinates, you
use the (x,y,z) of the point, and set w = 1.0 to start.

The reason we use four numbers instead of three is that with three
numbers, you have to do a vector plus vector add to do a translation,
and a vector times matrix multiply to do a rotation or a scale.  With
four numbers, you can do them all with vector times matrix multiplies,
which makes the code much, much simpler, especially when you do a lot
of transformations on the same set of points.

Now a for a transformation, we use a _homogeneous_transformation_matrix_,
T, which is a 4 by four array of real numbers:

	[ T11 T12 T13 T14 ]   where the 11 is really a subscript like T
T =	[ T21 T22 T23 T24 ]                                            1,1
	[ T31 T32 T33 T34 ]
	[ T41 T42 T43 T44 ]   or like T[1,1].

Now if Tij = 1 for i=j, and Tij=0 for i=/=j, then you have the identity
transformation; multiply V * T and you get V back again.

To do translations in x, or y, or z, by dx, or dy, or dz, change the
identity matrix by setting T[4,1] = dx, or T[4,2] to dy, or T[4,3] to
dz.  Then when you multiply V * T, you get V' = [x+dx,y+dy,z+dz,1].
(Try it!)

To do scaling in x, or y, or z, by p or q or r, change the identity
matrix by setting T[1,1] = p, or T[2,2] = q, or T[3,3] = r.  Then when
you multiply V * T, you get V' = [p*x,q*y,r*z,1].  (Try it!).

Rotations are the hardest.  The easy way to remember rotations is that
the axis you rotate around is that axis whose values don't change.
So, if you rotate around the x axis, the the x values of all the
points in the object you are rotating stay the same, just the y and z
values change, and so on.  To do a rotation of f radians around the x
axis, change the identity matrix by setting T[2,2] = cos(f), T[2,3] =
sin(f), T[3,2] = -sin(f), and T[3,3] = cos(f) again.  Then when you
multiply V * T, you get V' = [x,y*(cos(f)+sin(f)),z*(cos(f)-sin(f)),1].
(Try it!)

Similarly, to rotate g radians around the y axis, change the identity
matrix by setting T[1,1] = cos(g), T[1,3] = -sin(g), T[3,1] = sin(g),
and T[3,3] = cos(g).  Then when you multiply V * T, you get V' =
[x*(cos(g)-sin(g)),y,z*(sin(g)+cos(g)),1].  (Try it!)

Similarly, to rotate h radians around the z axis, change the identity
matrix by setting T[1,1] = cos(h), T[1,2] = sin(h), T[2,1] = -sin(h),
and T[2,2] = cos(h).  Then when you multiply V * T, you get V' =
[x*(cos(h)+sin(h)),y*(cos(h)-sin(h)),z,1].  (Try it!)

Now these are all pretty simple motions.  The nice thing is, to do
several of them in a row, you just concatinate (multiply) the
transformation matrices in the same order as the motions you want to
do.  So, to do motion S, then T, then U, do V * S * T * U = V'.

If you have lots of points (Ps) you want to transform into Qs, rather
than doing them P*S = P', then P'*T = P'', then P''*U = Q, you can
matrix multiply S*T*U = R, then just multiply each P*R to get the
corresponding Q.  This is what makes the extra coordinate worth the
trouble.

However, there is a kicker.  In all the simple transformations, the
extra, w, coordinate stayed equal to one.  When other kinds of
transformations are done (non-linear ones having to do with
projections from 3D space onto the 2D viewing surface, for example),
the w coordinate may come out not equal to one.  If that is the case,
we must _normalize_ the coordinate by dividing each of x, y, z, and w
by w to get back to our world coordinates.

Now that you have rotation down cold, here is a nice extra trick:
smooth dynamic motion.

Suppose you have an object O of P points in space, and you want to
transform it to another transformation (motion) in space, smoothly
over time for N frames.  Just figure out the transformation matrix to
get it there, and take the identity matrix, and interpolate a current
transformation matrix, R, on a coordinate by coordinate basis for each
frame.  Say the object should be at motion I (the identity matrix, no
motion yet) at frame 0, and at the motion T ( the transformed matrix,
all motion done) at frame N.  Then a loop like:

	For k = 0 to N            -- loop through frames
         clear(frame[k])
         For i = 1 to 4           -- loop through matrix rows
          For j = 1 to 4          -- loop through matrix columns
           R[i,j] = k*I[i,j]/N + (N-k)*T[i,j]/N    -- interpolate to
                                  -- get current matix, R, entry
         For p = 1 to P           -- loop through points of object
          draw(O[p]*R)            -- O[p] is the p-th [x,y,z,w] of O
                                  -- there's work hidden here; O*R is
                                  -- a vector times matrix multiply
         output(frame[k])

will do a very nice dynamic motion between the start and end
transformations.

Kent.
--
Again, my opinions, not the account furnishers'.

xanthian@well.sf.ca.us (Kent Paul Dolan)
xanthian@ads.com - expiring soon; please use Well address for replies.
Kent, the (bionic) man from xanth, now available as a build-a-xanthian
kit at better toy stores near you.  Warning - some parts proven fragile.

Just another "overqualified" (read over 40 and previously well paid)
unemployed/unemployable computer graphics programming guru.

-> METAFONT, TeX, graphics programming done on spec -- (415) 964-4486 <-