Transformations and Matrices
From Math Images
(New page: {{Image Description |ImageName=Transformations and Matrices |Image=Bunnyhead.jpg |ImageIntro=placeholder |ImageDescElem=placeholder |ImageDesc=placeholder |other=stacks |AuthorName=Steve C...) |
(page content written by Steve Cunningham) |
||
Line 4: | Line 4: | ||
|ImageIntro=placeholder | |ImageIntro=placeholder | ||
|ImageDescElem=placeholder | |ImageDescElem=placeholder | ||
- | |ImageDesc= | + | |ImageDesc= |
+ | ==Linear Transformations Are Matrices== | ||
+ | A linear transformation on 2D (or 3D) space is a function f from 2D (or 3D) space to itself that has the property that<br /> | ||
+ | <math> f(aA + bB) = af(A) + bf(B). </math> <br /> | ||
+ | Since points in 2D or 3D space can be written as <math> P = xi + yj </math> or <math> P = xi + yj +zk </math> with i, j, and k the coordinate vectors, then we see that <math> f(P) = xf(i) + yf(j) </math> or <math> f(P) = xf(i) + yf(j) + zf(k) </math> <br /> | ||
+ | This tells us that the linear transformation is completely determined by what it does to the coordinate vectors. <br /><br /> | ||
+ | |||
+ | Let’s see an example of this: if the transformation has the following action on the coordinates:<br /> | ||
+ | <math> \begin{align}f(1,0,0) = f(i) = (2,-2,1) \\ | ||
+ | f(0,1,0) = f(j) = (-1,3,2) \\ | ||
+ | f(0,0,1) = f(k) = (4,3,-2) \end{align}</math><br /> | ||
+ | then for any point we have:<br /> | ||
+ | <math>\begin{align}f(x,y,z) = (2x,-2x,x)+(-y,3y,2y)+(4z,3z,-2z) \\ = (2x-y+4z, -2x+3y+3z, x+2y-2z) \\ | ||
+ | = \begin{bmatrix} | ||
+ | 2x - y + 4z \\ | ||
+ | -2x + 3y + 3z \\ | ||
+ | x + 2y - 2z | ||
+ | \end{bmatrix} = \begin{bmatrix} | ||
+ | 2 & -1 & 4 \\ | ||
+ | -2 & 3 & 3 \\ | ||
+ | 1 & 2 & -2 | ||
+ | \end{bmatrix} | ||
+ | \begin{bmatrix} | ||
+ | x \\ | ||
+ | y \\ | ||
+ | z | ||
+ | \end{bmatrix} \end{align} | ||
+ | </math><br /><br /> | ||
+ | |||
+ | From this example, we see that the linear transformation is exactly determined by the matrix whose first column is <math>f(i)</math>, whose second column is <math>f(j)</math>, and whose third column is <math>f(k)</math>, and that applying the function f is exactly the same as multiplying by the matrix. So the linear transformation <b>is</b> the matrix multiplication, and we can use the concepts of linear transformation and matrix multiplication interchangeably.<br /> | ||
+ | |||
+ | ==Transformation Composition Is Matrix Multiplication== | ||
+ | Transformations are usually not used by themselves, especially in graphics, so you need to have a way to compose transformations, as in <math>g(f(P))</math>. But if G is the matrix for the transformation g, and F is the matrix for the transformation f, then the matrix product G*F is the matrix for the composed functions gf.<br /> | ||
+ | |||
+ | ==Basic Transformations For Graphics== | ||
+ | Computer graphics works by representing objects in terms of simple primitives (link to the graphics primitives page) that are manipulated with transformations that preserve some primitives’ essential properties. These properties may include angles, lengths, or basic shapes. Some of these transformations can work on primitives with vertices in standard 2D or 3D space, but some need to have vertices in homogeneous coordinates. The general graphics approach is to do everything in homogeneous coordinates, but we’ll talk about the primitives in terms of both kinds when we can. <br /><br /> | ||
+ | |||
+ | The most fundamental kinds of transformations for graphics are rotation, scaling, and translation. There are also a few cases when you might want to use shear transformations, so we’ll talk about these as well. !!PICTURE HERE!! <br /><br /> | ||
+ | |||
+ | ===Rotation=== | ||
+ | A 2D rotation transformation rotates everything in 2D space around the origin by a given angle. In order to see what it does, let’s take a look at what a rotation by a positive angle does to the coordinate axes. Now (x,y) is the result when you apply the transformation to (1,0), which means that<br /> | ||
+ | <math> \begin{align} x = cos(\theta ) \\ | ||
+ | y = sin(\theta ) \end{align} </math><br /> | ||
+ | But (x’,y’) is the result when you apply the transformation to (0,1), or <br /> | ||
+ | <math> \begin{align} x' = cos(\theta +\frac{p}{2}) = cos(\theta )cos(\frac{p}{2}) - sin(\theta )sin(\frac{p}{2}) = -sin(\theta ) \\ | ||
+ | y' = sin(\theta +\frac{p}{2}) = sin(\theta )cos(\frac{p}{2}) + cos(\theta )sin(\frac{p}{2}) = cos(\theta ) \end{align} </math> <br /> | ||
+ | |||
+ | Then as we saw above, the rotation transformation must have the image of (1,0) as the first column and the image of (0,1) as the second column, or<br /> | ||
+ | <math> rotate(\theta ) = \begin{bmatrix} | ||
+ | cos(\theta ) & -sin(\theta ) \\ | ||
+ | sin(\theta ) & cos(\theta ) | ||
+ | \end{bmatrix} </math><br /> | ||
+ | or, as XKCD (http://xkcd.com/) sees it (notice that the rotation is by -90° and <math>sin(-90\,^{\circ}) = -sin(90\,^{\circ})</math>, !!PICTURE HERE!! <br /><br /> | ||
+ | |||
+ | The situation for 3D rotations is different because a rotation in 3D space must leave a fixed line through the origin. In fact we really only handle the special cases where the fixed line is one of the coordinate axes. Let’s start with the easiest one.<br /><br /> | ||
+ | |||
+ | A rotation around the Z-axis is a 2D rotation as above with the third dimension fixed. So the matrix for this rotation is pretty clearly <br /> | ||
+ | <math> \begin{bmatrix} | ||
+ | cos(\theta ) & -sin(\theta ) & 0 \\ | ||
+ | sin(\theta ) & cos(\theta ) & 0 \\ | ||
+ | 0 & 0 & 1 \end{bmatrix} </math><br /><br /> | ||
+ | |||
+ | A rotation around the X-axis is pretty similar. If we look down the X-axis, we see the following 2D coordinates: !!PICTURE HERE!! with <math> Y \times Z = X</math> !!CHECK WORD!!, the axis of rotation. This looks like an exact analogue of the XY-plane, and so we can see that the rotation matrix must leave X fixed and operate only on Y and Z as <br /> | ||
+ | <math> \begin{bmatrix} | ||
+ | 1 & 0 & 0 \\ | ||
+ | 1 & cos(\theta ) & -sin(\theta ) \\ | ||
+ | 0 & sin(\theta ) & cos(\theta ) \end{bmatrix} </math><br /><br /> | ||
+ | |||
+ | For rotations around the Y-axis, the view down the Y-axis looks different from the one down the Z-axis; it is !!PICTURE HERE!! <br /> | ||
+ | Here a positive-angle is from the X-axis towards the Z-axis, but <math> X \times Z = -Z \times X = -Y </math>, !!CHECK WORD!! so the rotation axis dimension is pointing in the opposite direction from the Y-axis. Thus a the angle for the rotation is the negative of the angle we would see in the axes above, and since cos is an even function but sin is odd, we have the rotation matrix <br /> | ||
+ | <math> \begin{bmatrix} | ||
+ | cos(-\theta ) & 0 & -sin(-\theta ) \\ | ||
+ | 0 & 1 & 0 \\ | ||
+ | sin(-\theta ) & 0 & cos(-\theta ) \end{bmatrix} = | ||
+ | \begin{bmatrix} | ||
+ | cos(\theta ) & 0 & sin(\theta ) \\ | ||
+ | 0 & 1 & 0 \\ | ||
+ | sin(-\theta ) & 0 & cos(\theta ) \end{bmatrix} </math><br /> | ||
+ | around the Y-axis.<br /><br /> | ||
+ | |||
+ | When you want to get a rotation around a different line than a coordinate axis, the usual approach is to find two rotations that, when composed, take a coordinate line into the fixed line you want. You can then apply these two rotations, apply the rotation you want around the coordinate line, and apply the inverses of the two rotations (in inverse order) to construct the general rotation. The sequence goes like this:<br /> | ||
+ | apply a rotation <math> R_1 </math> around the Z-axis to move your fixed line into the YZ-plane<br /> | ||
+ | apply a rotation <math> R_2 </math> around the X-axis to move that line to the Y-axis<br /> | ||
+ | apply the rotation by your desired angle around the Y-axis<br /> | ||
+ | apply the inverse <math> R_2^{-1} </math> to move your rotation line back into the YZ-plane<br /> | ||
+ | apply the inverse <math> R_1^{-1} </math> to move your rotation line back to the original line.<br /> | ||
+ | Whew! This can all be put into a function – or you can simply keep everything in terms of rotations around X, Y, and Z.<br /><br /> | ||
+ | |||
+ | If we are working in homogeneous coordinates, we see that all of the rotation operations take place in standard 3D space and so the fourth coordinate is not changed. Thus the general pattern for all the rotations in homogeneous coordinates is <br /> | ||
+ | <math> \begin{bmatrix} | ||
+ | * & * & * & 0 \\ [0.3em] | ||
+ | * & * & * & 0 \\ [0.3em] | ||
+ | * & * & * & 0 \\ | ||
+ | 0 & 0 & 0 & 1 \end{bmatrix} </math> where the * terms are the terms from the 3D rotations above. | ||
+ | |||
+ | ===Scaling=== | ||
+ | Scaling is the action of multiplying each coordinate of a point by a constant amount. As an example, let <math>f(x,y,z) = (2x,3y,4z)</math>. Then<br /> | ||
+ | <math>f((x,y,z)+(a,b,c)) = f(x+a,y+b,z+c) = (2(x+a),3(y+b), 4(z+c)) = (2x,3y,4z)+(2a,3b,4c) </math><br /> | ||
+ | So this is a linear transformation. If we look at what this transformation does to each of the coordinate vectors, we have<br /> | ||
+ | <math> \begin{align} f(1,0,0) = (2,0,0) \\ | ||
+ | f(0,1,0) = (0,3,0) \\ | ||
+ | f(0,0,1) = (0,0,4) \end{align} </math><br /> | ||
+ | So the matrix for this transformation is <math> \begin{bmatrix} 2 & 0 & 0 \\ 0 & 3 & 0 \\ 0 & 0 & 4 \end{bmatrix} </math> and, in general, a scaling matrix looks like <math> \begin{bmatrix} \sigma_x & 0 & 0 \\ 0 & \sigma_y & 0 \\ 0 & 0 & \sigma_z \end{bmatrix} </math> where the <math> \sigma_x , \sigma_y ,\text{ and }\sigma_z </math> are the scaling factors for x, y, and z respectively. <br /> | ||
+ | In case of only 2D transformations, scaling simply scales down to two dimensions and we simply have<br /> | ||
+ | <math> \begin{bmatrix} \sigma_x & 0 \\ 0 & \sigma_y \end{bmatrix} </math><br /><br /> | ||
+ | |||
+ | In case we are working with homogeneous coordinates, a scaling transformation only acts on the three primary components and leaves the homogeneous component alone, so we simply have the matrix <math> \begin{bmatrix} \sigma_x & 0 & 0 & 0 \\ 0 & \sigma_y & 0 & 0 \\ 0 & 0 & \sigma_z & 0 \\ 0 & 0 & 0 & 1 \end{bmatrix} </math> for the scaling transformation. | ||
+ | |||
+ | ===Translation=== | ||
+ | Notice that a translation function cannot be a linear transformation on normal space because it does not take the origin to the origin. These are examples of affine transformations, transformations that are composed of a linear transformation, such as a rotation, scaling, or shear, and a translation. In order to write a translation matrix, we need to use homogeneous coordinates. <br /><br /> | ||
+ | |||
+ | If we want to add <math> T_x </math> to the X-coordinate and <math> T_y </math> to the Y-coordinate of every point in 2D space, we see that <br /> | ||
+ | <math> \begin{bmatrix} 1 & 0 & T_x \\ 0 & 1 & T_y \\ 0 & 0 & 1 \end{bmatrix} \times | ||
+ | \begin{bmatrix} x \\ y \\ 1 \end{bmatrix} = | ||
+ | \begin{bmatrix} x+T_x \\ y+T_y \\ 1 \end{bmatrix} </math> so that the matrx | ||
+ | <math> \begin{bmatrix} 1 & 0 & T_x \\ 0 & 1 & T_y \\ 0 & 0 & 1 \end{bmatrix} </math> gives a 2D translation. <br /><br /> | ||
+ | |||
+ | The 3D case is basically the same, and by the same argument we see that the 3D translation is given by <br /> | ||
+ | <math> \begin{bmatrix} 1 & 0 & 0 & T_x \\ 0 & 1 & 0 & T_y \\ 0 & 0 & 1 & T_z \\ 0 & 0 & 0 & 1 \end{bmatrix} </math><br /><br /> | ||
+ | |||
+ | hese are linear transformations in the space one degree higher than the geometry you are working with. In fact, the main reason for including homogeneous coordinates is the math for graphics is to be able to handle translations (and thus all basic transformations) as linear transformations represented by matrices.<br /> | ||
+ | |||
+ | ===Shear=== | ||
+ | The shear transformation is not widely used in computer graphics, but can be used for things like the oblique view in engineering drawings. The concept of a shear is to add a multiple of one coordinate to another coordinate of each point, or, for example,<br /> | ||
+ | <math>shear(x,y,z) = (x+3y,y,z)</math><br /> | ||
+ | The matrix for this shear transformation looks like <math> \begin{bmatrix} 1 & 3 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}</math>.<br /><br /> | ||
+ | |||
+ | In general, the matrix for a shear transformation will look like the identity matrix with one non-zero element A off the diagonal. If A is in row i, column j, then the matrix will add A times the jth coordinate of the vector to the ith coordinate. <br /><br /> | ||
+ | |||
+ | For the oblique view of engineering drawings, we look at the shear matrices that add a certain amount of the z-coordinate to each of the x- and y-coordinates. The matrices are<br /> | ||
+ | <math> \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ A & 0 & 1 \end{bmatrix} \times | ||
+ | \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & B & 1 \end{bmatrix} = | ||
+ | \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ A & B & 1 \end{bmatrix} </math> <br /> | ||
+ | that take <math> (x,y,z) \times (x+Az,y+Bz,z)</math>. !!CHECK WORD!! The values of A and B are adjusted to give precisely the view that you want, and the z-term of the result is usually dropped to give the needed 2D view of the 3D object. An example is the classical <i>cabinet view</i> shown below: !!PICTURE HERE!! <br /><br /> | ||
+ | |||
+ | To experiment with these transformations, we have two interactive applets. The first one lets you apply the 2D transformations to a 2D figure. | ||
+ | ( use the 2D transformation applet around here ) <br /><br /> | ||
+ | |||
+ | The second applet lets you apply the 3D transformations to a 3D figure. | ||
+ | ( use the 3D transformation applet around here. It would be better if the object were more complex. ) <br /> | ||
+ | |||
+ | ===Matrix Inverses=== | ||
+ | In general, getting the inverse of a matrix can be difficult, but for the basic graphics transformations the inverses are easy because we can simply undo the geometric action of the original transformation.<br /> <br /> | ||
+ | |||
+ | The inverse of the scaling matrix <math> \begin{bmatrix} \sigma_x & 0 & 0 \\ 0 & \sigma_y & 0 \\ 0 & 0 & \sigma_z \end{bmatrix} </math> is clearly | ||
+ | <math> \begin{bmatrix} \frac{1}{\sigma_x } & 0 & 0 \\ 0 & \frac{1}{\sigma_y } & 0 \\ 0 & 0 & \frac{1}{\sigma_z } \end{bmatrix} </math><br /><br /> | ||
+ | |||
+ | The inverse of a rotation transformation by angle <math> \theta </math> is clearly the rotation around the same line by the angle <math>-\theta </math>.<br /><br /> | ||
+ | |||
+ | The inverse of the translation matrix <math> \begin{bmatrix} 1 & 0 & 0 & T_x \\ 0 & 1 & 0 & T_y \\ 0 & 0 & 1 & T_z \\ 0 & 0 & 0 & 1 \end{bmatrix} </math> is clearly <math> \begin{bmatrix} 1 & 0 & 0 & -T_x \\ 0 & 1 & 0 & -T_y \\ 0 & 0 & 1 & -T_z \\ 0 & 0 & 0 & 1 \end{bmatrix} </math><br /><br /> | ||
+ | |||
+ | The inverse of the simple shear transformation is also straightforward. Since a simple shear adds a multiple of one vector component to another component, the inverse only needs to subtract that multiple. So we have<br /><br /> <math> \begin{bmatrix} 1 & A \\ 0 & 1 \end{bmatrix}^{-1} = \begin{bmatrix} 1 & -A \\ 0 & 1 \end{bmatrix} </math> and the 3D case is a simple extension of this. <br /><br /> | ||
+ | |||
+ | So we have a major observation: If any transformation is the product of basic graphics transformations, it is easy to find the inverse of its matrix (and hence its inverse transformation) as the product of the inverses of the components in reverse order. Or: <br /> | ||
+ | <math>(A \times B \times C)^{-1} = C^{-1} \times B^{-1} \times A^{-1} </math> <br /> | ||
+ | |||
+ | ==Transformations and Graphics Environments== | ||
+ | Attention – this concept needs a bit of programming background; it involves stacks.<br /><br /> | ||
+ | |||
+ | When you are defining the geometry for a graphics image, you will sometimes want to model your scene as a hierarchy of simpler objects. You might have a desk, for example, that is made up of several parts (legs, drawers, shelves); the drawers may have handles or other parts; you may want to put several things on top of the desk; and so on. It’s common to define general models for each simple part, and then to put the pieces together in a common space, called the “world space.”<br /><br /> | ||
+ | |||
+ | Each simple part will be defined in its own “model space,” and then you can apply transformations that move all the parts into the right place in the more complex part. In turn, that whole more complex part may be moved into another position, and so on – you can build up quite complex models this way. One common technique for this kind of hierarchical modeling is to build a “scene graph” that shows how everything is assembled and the transformations that are used in the assembly.<br /><br /> | ||
+ | |||
+ | As an example, consider the simple picture of a bunny head, basically made up of several spheres. Each sphere is scaled (making it an ellipsoid of the right size), rotated into the right orientation, and then translated into the proper place. The tree next to the picture shows how this is organized. !!PICTURES HERE!!<br /><br /> | ||
+ | |||
+ | In order to make this work, you have to apply each set of transformations to its own sphere and then “forget” those transformations so you can apply the transformations for the next piece. You could, of course, use inverses to undo the transformations, but that’s slow and invites roundoff errors from many multiplications.<br /><br /> | ||
+ | |||
+ | instead, it is common to maintain a “transformation stack” that holds the history of every place you want to get back to – all the transformations you have saved. This is a stack of 4x4 matrices that implement the transformations. You also have an active transformation to which you apply any new transformations by matrix multiplication.<br /><br /> | ||
+ | |||
+ | To save a transformation to get back to later, you push a copy of the current active transformation (as a 4x4 matrix) onto the stack. Later, when you have applied whatever new transformations you need and want to get back to the last saved transformation, you pop the top matrix off the stack and make it the current active transformation. Presto – all the transformations you had used since the corresponding push operation are gone.<br /><br /> | ||
+ | |||
+ | So let’s get back to the rabbit. We want to create the rabbit head, and we have whatever active transformation was in place when we wanted to draw the head. Then we have<br /><br /> | ||
+ | *push | ||
+ | **scale | ||
+ | **sphere for main part | ||
+ | *pop | ||
+ | **translate | ||
+ | **scale | ||
+ | **sphere for left eye | ||
+ | *pop | ||
+ | **Translate | ||
+ | **Scale | ||
+ | **sphere for right eye | ||
+ | *pop | ||
+ | **Translate | ||
+ | **Rotate | ||
+ | **Scale | ||
+ | **sphere for left ear | ||
+ | *pop | ||
+ | **Translate | ||
+ | **Rotate | ||
+ | **Scale | ||
+ | **sphere for right ear | ||
+ | *pop | ||
+ | <br /><br /> | ||
|other=stacks | |other=stacks | ||
|AuthorName=Steve Cunningham | |AuthorName=Steve Cunningham |
Revision as of 14:37, 23 June 2011
Transformations and Matrices |
---|
Transformations and Matrices
- placeholder
Contents |
Basic Description
placeholderA More Mathematical Explanation
- Note: understanding of this explanation requires: *stacks
Linear Transformations Are Matrices
A linear transformation on 2D (or 3D) space is a function f f [...]Linear Transformations Are Matrices
A linear transformation on 2D (or 3D) space is a function f from 2D (or 3D) space to itself that has the property that
Since points in 2D or 3D space can be written as or with i, j, and k the coordinate vectors, then we see that or
This tells us that the linear transformation is completely determined by what it does to the coordinate vectors.
Let’s see an example of this: if the transformation has the following action on the coordinates:
then for any point we have:
From this example, we see that the linear transformation is exactly determined by the matrix whose first column is , whose second column is , and whose third column is , and that applying the function f is exactly the same as multiplying by the matrix. So the linear transformation is the matrix multiplication, and we can use the concepts of linear transformation and matrix multiplication interchangeably.
Transformation Composition Is Matrix Multiplication
Transformations are usually not used by themselves, especially in graphics, so you need to have a way to compose transformations, as in . But if G is the matrix for the transformation g, and F is the matrix for the transformation f, then the matrix product G*F is the matrix for the composed functions gf.
Basic Transformations For Graphics
Computer graphics works by representing objects in terms of simple primitives (link to the graphics primitives page) that are manipulated with transformations that preserve some primitives’ essential properties. These properties may include angles, lengths, or basic shapes. Some of these transformations can work on primitives with vertices in standard 2D or 3D space, but some need to have vertices in homogeneous coordinates. The general graphics approach is to do everything in homogeneous coordinates, but we’ll talk about the primitives in terms of both kinds when we can.
The most fundamental kinds of transformations for graphics are rotation, scaling, and translation. There are also a few cases when you might want to use shear transformations, so we’ll talk about these as well. !!PICTURE HERE!!
Rotation
A 2D rotation transformation rotates everything in 2D space around the origin by a given angle. In order to see what it does, let’s take a look at what a rotation by a positive angle does to the coordinate axes. Now (x,y) is the result when you apply the transformation to (1,0), which means that
But (x’,y’) is the result when you apply the transformation to (0,1), or
Then as we saw above, the rotation transformation must have the image of (1,0) as the first column and the image of (0,1) as the second column, or
or, as XKCD (http://xkcd.com/) sees it (notice that the rotation is by -90° and , !!PICTURE HERE!!
The situation for 3D rotations is different because a rotation in 3D space must leave a fixed line through the origin. In fact we really only handle the special cases where the fixed line is one of the coordinate axes. Let’s start with the easiest one.
A rotation around the Z-axis is a 2D rotation as above with the third dimension fixed. So the matrix for this rotation is pretty clearly
A rotation around the X-axis is pretty similar. If we look down the X-axis, we see the following 2D coordinates: !!PICTURE HERE!! with !!CHECK WORD!!, the axis of rotation. This looks like an exact analogue of the XY-plane, and so we can see that the rotation matrix must leave X fixed and operate only on Y and Z as
For rotations around the Y-axis, the view down the Y-axis looks different from the one down the Z-axis; it is !!PICTURE HERE!!
Here a positive-angle is from the X-axis towards the Z-axis, but , !!CHECK WORD!! so the rotation axis dimension is pointing in the opposite direction from the Y-axis. Thus a the angle for the rotation is the negative of the angle we would see in the axes above, and since cos is an even function but sin is odd, we have the rotation matrix
around the Y-axis.
When you want to get a rotation around a different line than a coordinate axis, the usual approach is to find two rotations that, when composed, take a coordinate line into the fixed line you want. You can then apply these two rotations, apply the rotation you want around the coordinate line, and apply the inverses of the two rotations (in inverse order) to construct the general rotation. The sequence goes like this:
apply a rotation around the Z-axis to move your fixed line into the YZ-plane
apply a rotation around the X-axis to move that line to the Y-axis
apply the rotation by your desired angle around the Y-axis
apply the inverse to move your rotation line back into the YZ-plane
apply the inverse to move your rotation line back to the original line.
Whew! This can all be put into a function – or you can simply keep everything in terms of rotations around X, Y, and Z.
If we are working in homogeneous coordinates, we see that all of the rotation operations take place in standard 3D space and so the fourth coordinate is not changed. Thus the general pattern for all the rotations in homogeneous coordinates is
where the * terms are the terms from the 3D rotations above.
Scaling
Scaling is the action of multiplying each coordinate of a point by a constant amount. As an example, let . Then
So this is a linear transformation. If we look at what this transformation does to each of the coordinate vectors, we have
So the matrix for this transformation is and, in general, a scaling matrix looks like where the are the scaling factors for x, y, and z respectively.
In case of only 2D transformations, scaling simply scales down to two dimensions and we simply have
In case we are working with homogeneous coordinates, a scaling transformation only acts on the three primary components and leaves the homogeneous component alone, so we simply have the matrix for the scaling transformation.
Translation
Notice that a translation function cannot be a linear transformation on normal space because it does not take the origin to the origin. These are examples of affine transformations, transformations that are composed of a linear transformation, such as a rotation, scaling, or shear, and a translation. In order to write a translation matrix, we need to use homogeneous coordinates.
If we want to add to the X-coordinate and to the Y-coordinate of every point in 2D space, we see that
so that the matrx
gives a 2D translation.
The 3D case is basically the same, and by the same argument we see that the 3D translation is given by
hese are linear transformations in the space one degree higher than the geometry you are working with. In fact, the main reason for including homogeneous coordinates is the math for graphics is to be able to handle translations (and thus all basic transformations) as linear transformations represented by matrices.
Shear
The shear transformation is not widely used in computer graphics, but can be used for things like the oblique view in engineering drawings. The concept of a shear is to add a multiple of one coordinate to another coordinate of each point, or, for example,
The matrix for this shear transformation looks like .
In general, the matrix for a shear transformation will look like the identity matrix with one non-zero element A off the diagonal. If A is in row i, column j, then the matrix will add A times the jth coordinate of the vector to the ith coordinate.
For the oblique view of engineering drawings, we look at the shear matrices that add a certain amount of the z-coordinate to each of the x- and y-coordinates. The matrices are
that take . !!CHECK WORD!! The values of A and B are adjusted to give precisely the view that you want, and the z-term of the result is usually dropped to give the needed 2D view of the 3D object. An example is the classical cabinet view shown below: !!PICTURE HERE!!
To experiment with these transformations, we have two interactive applets. The first one lets you apply the 2D transformations to a 2D figure.
( use the 2D transformation applet around here )
The second applet lets you apply the 3D transformations to a 3D figure.
( use the 3D transformation applet around here. It would be better if the object were more complex. )
Matrix Inverses
In general, getting the inverse of a matrix can be difficult, but for the basic graphics transformations the inverses are easy because we can simply undo the geometric action of the original transformation.
The inverse of the scaling matrix is clearly
The inverse of a rotation transformation by angle is clearly the rotation around the same line by the angle .
The inverse of the translation matrix is clearly
The inverse of the simple shear transformation is also straightforward. Since a simple shear adds a multiple of one vector component to another component, the inverse only needs to subtract that multiple. So we have
and the 3D case is a simple extension of this.
So we have a major observation: If any transformation is the product of basic graphics transformations, it is easy to find the inverse of its matrix (and hence its inverse transformation) as the product of the inverses of the components in reverse order. Or:
Transformations and Graphics Environments
Attention – this concept needs a bit of programming background; it involves stacks.
When you are defining the geometry for a graphics image, you will sometimes want to model your scene as a hierarchy of simpler objects. You might have a desk, for example, that is made up of several parts (legs, drawers, shelves); the drawers may have handles or other parts; you may want to put several things on top of the desk; and so on. It’s common to define general models for each simple part, and then to put the pieces together in a common space, called the “world space.”
Each simple part will be defined in its own “model space,” and then you can apply transformations that move all the parts into the right place in the more complex part. In turn, that whole more complex part may be moved into another position, and so on – you can build up quite complex models this way. One common technique for this kind of hierarchical modeling is to build a “scene graph” that shows how everything is assembled and the transformations that are used in the assembly.
As an example, consider the simple picture of a bunny head, basically made up of several spheres. Each sphere is scaled (making it an ellipsoid of the right size), rotated into the right orientation, and then translated into the proper place. The tree next to the picture shows how this is organized. !!PICTURES HERE!!
In order to make this work, you have to apply each set of transformations to its own sphere and then “forget” those transformations so you can apply the transformations for the next piece. You could, of course, use inverses to undo the transformations, but that’s slow and invites roundoff errors from many multiplications.
instead, it is common to maintain a “transformation stack” that holds the history of every place you want to get back to – all the transformations you have saved. This is a stack of 4x4 matrices that implement the transformations. You also have an active transformation to which you apply any new transformations by matrix multiplication.
To save a transformation to get back to later, you push a copy of the current active transformation (as a 4x4 matrix) onto the stack. Later, when you have applied whatever new transformations you need and want to get back to the last saved transformation, you pop the top matrix off the stack and make it the current active transformation. Presto – all the transformations you had used since the corresponding push operation are gone.
So let’s get back to the rabbit. We want to create the rabbit head, and we have whatever active transformation was in place when we wanted to draw the head. Then we have
- push
- scale
- sphere for main part
- pop
- translate
- scale
- sphere for left eye
- pop
- Translate
- Scale
- sphere for right eye
- pop
- Translate
- Rotate
- Scale
- sphere for left ear
- pop
- Translate
- Rotate
- Scale
- sphere for right ear
- pop
Teaching Materials
- There are currently no teaching materials for this page. Add teaching materials.
References
references
Leave a message on the discussion page by clicking the 'discussion' tab at the top of this image page.