CS-460/560, Week 12-B Spring, 1998 R. Eckert 3-D GEOMETRIC TRANSFORMATIONS We want to be able to move and manipulate objects in 3-D space. For example, we might want to translate an object, rotate an object about a given axis, or scale an object. These are examples of three dimensional geometric transformations. Just as we were able to implement such transformations in two dimensions by using homogeneous coordinates and matrices, we will be able to do the same in 3-D. TRANSLATIONS-- We wish to translate point P = (x,y,z) by a 3-D translation vector T = (tx, ty, tz) giving a new point P' = (x',y',z'). The component equations are: x' = x + tx y' = y + ty z' = z + tz We define a homogeneous representation of a vectors P and P' whose components in 3-D Cartesian space are (x,y,z) and (x',y',z') as follows: |x| |x'| P = |y| and P' = |y'| |z| |z'| |1| |1 | We can than represent the translation by the following matrix equation: P' = T * P where T is the following 4 X 4 homogeneous translation matrix: | 1 0 0 tx | T = | 0 1 0 ty | | 0 0 1 tz | | 0 0 0 1 | Notice the obvious extension from 2-D to 3-D. SCALING-- To perform a 3-D scaling with respect to the origin (0,0,0), we specify the three scaling factors sx, sy, sz. Point P=(x,y,z) will be converted to point P'=(x',y',z') by the following scaling transformation: P' = S * P where S is the following 4 X 4 scaling matrix: | sx 0 0 0 | S = | 0 sy 0 0 | | 0 0 sz 0 | | 0 0 0 1 | ROTATIONS-- To handle 3-D rotations, we need to specify the angle of rotation and the axis about which the rotation is to be performed. There is an infinite number of possible rotation axes. It turns out, however, that a rotation about any axis can be obtained by linear combinations of rotations about the x-axis, the y-axis, and the z-axis. We will first consider the rotation of a point P=(x,y,z) by angle theta about the z-axis, giving the rotated point P'=(x',y',z'). This kind of rotation will give exactly the same equations for x and y as in the 2-D case. And, of course, z will not change. x' = x*cos(theta) - y*sin(theta) y' = x*sin(theta) + y*cos(theta) z' = z This can be represented by the following homogeneous matrix equation: P' = Rz * P where Rz is the following 4 X 4 matrix for rotations about the z-axis: | cos(theta) -sin(theta) 0 0 | Rz = | sin(theta) cos(theta) 0 0 | | 0 0 1 0 | | 0 0 0 1 | To get the matrix Rx for rotations about the x axis, we make a symmetry argument. y z | | | | | | ------x ------y / / / / z x about z about x From the diagram, we see that rotations about the x-axis are equivalent to rotations about z-axis if we make the following replacements: x --> y y --> z z --> x Making these replacements in the component equations above, we get: y' = y*cos(theta) - z*sin(theta) z' = y*sin(theta) + z*cos(theta) x' = x These can be represented by the following homogeneous matrix equation: P' = Rx * P where Rx is the following 4 X 4 matrix for rotations about the x-axis: | 1 0 0 0 | Rx = | 0 cos(theta) -sin(theta) 0 | | 0 sin(theta) cos(theta) 0 | | 0 0 0 1 | A similar symmetry argument can be made to get the matrix Ry for rotations about the y-axis. y x | | | | | | ------x ------z / / / / z y about z about y From the diagram, we see that rotations about the y-axis are equivalent to rotations about z-axis if we make the following replacements: x --> z y --> x z --> y Making these replacements in the component equations above, we get: z' = z*cos(theta) - x*sin(theta) x' = z*sin(theta) + x*cos(theta) y' = y These can be represented by the following homogeneous matrix equation: P' = Ry * P where Ry is the following 4 X 4 matrix for rotations about the x-axis: | cos(theta) 0 sin(theta) 0 | Ry = | 0 1 0 0 | | -sin(theta) 0 cos(theta) 0 | | 0 0 0 1 | For all three of these fundamental rotations, the positive sense is defined as counter clockwise as we look down the rotation axis toward the origin. COMPOSITE 3-D GEOMETRIC TRANSFORMATIONS-- Just as in 2-D, a series of consecutive transformations represented by the homogeneous transformation matrices T1, T2, ..., Tn is equivalent to a single transformation represented by the composite transformation matrix T, where T is given by the matrix product: T = Tn*...*T2*T1 Because of the simple way in which transformations in 3-D are extensions of transformations in 2-D, it is very straightforward to build a library of 3-D transformation functions from the 2-D transformation package you designed earlier. This might contain some of the following functions that will enable you to set up and transform points and polygons (see notes on 2-D transformations). (In the following, a, b, and c are arrays containing the 12 non-trivial matrix elements of a 4 X 4 homogeneous transformation matrix; vi and vo are 3-D point structures; inpoly and outpoly are polygon structures. These latter structures were described in the previous set of notes.) void settranslate3d(a, tx, ty, tz); // set up translation matrix from // components (tx,ty,tz) of translation void setscale3d(a, sx, sy, sz); // set up scaling matrix from // scaling factors (sx,sy,sz) void setrotatex3d(a, theta); // set up x-axis rotation matrix // from rotation angle theta void setrotatey3d(a, theta); // set up y-axis rotation matrix void setrotatez3d(a, theta); // set up z-axis rotation matrix void combine3d(c, a, b); // C = A * B (matrix product) void xformcoord3d(c, vi, *vo); // vo = C * vi void xformpoly3d(inpoly, outpoly, float c); // transform polygon ROTATION ABOUT AN ARBITRARY AXIS-- To determine the composite transformation matrix that will rotate a point by angle alpha about an axis of rotation which is the line segment whose endpoints are P1=(x1,y1,z1) and P2=(x2,y2,z2), we can convert this problem into a rotation about the x-axis. We need to do the following: 1. Translate so that endpoint P1 is at the origin: T1 = T(-x1,-y1,-z1). 2. Compute the spherical coordinates of the other endpoint: rho = sqrt((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2) phi = arccos((z2-z1)/rho) theta = arctan((y2-y1)/(x2-x1)) 3. Rotate about the z-axis by -theta so that the line lies in the x-z plane: T2 = Rz(-theta). 4. Rotate about the y-axis by (90-phi) to make the line coincide with the x-axis: T3 = Ry(90-phi). 5. Rotate about the x-axis by the given angle alpha: T4 = Rx(alpha). 6. Rotate back to undo step 4: T5 = Ry(phi-90). 7. Rotate back to undo step 3: T6 = Rz(theta). 8. Translate back to undo step 1: T7 = T(x1,y1,z1) The composite transformation then will be: T = T7*T6*T5*T4*T3*T2*T1 3-D COORDINATE SYSTEM TRANSFORMATIONS-- Just as for 2-D transformations, there is a symmetrical relationship between 3-D geometric transformations (moving the object) and 3-D coordinate system transformations (moving the coordinate system). For translations, the relationship is: Tcoord(x,y,z) = Tgeom(-x,-y,-z) For each of the 3 principal-axis rotations, the relationship is: Rcoord(theta) = Rgeom(-theta) We'll use these relationships when we derive the equations for the 3-D viewing transformation.