Lab 12 Graphical Transformations 3D

Background

This is basically an extension of Lab 8 to 3 dimensions. You should look over Lab 8 and also Section 11.11 in the text.

What's New in 3D

Now that you've reviewed transformations in 2D, let's cover what's new in 3D.
Data
Obviously the first change is in the data; each point now has 3 coordinates (x,y,z) and, if we are using homogeneous coordinates (and we are), it would be stored as a vector with a 1 in the 4th spot: pt =[x; y; z; 1];. For example, here's the 8 vertices of a cube in homogeneous coordinates:
```cube = [0, 1, 1, 0, 0, 1, 1, 0
0, 0, 0, 0, 1, 1, 1, 1
0, 0, 1, 1, 0, 0, 1, 1
1, 1, 1, 1, 1, 1, 1, 1];
```

A less obvious change is that a list of points is not enough to design a shape, we also need a list of edges (or which points connect to which points). The easiest way to do this is just make a M by 2 matrix, where each row contains the numbers of the points that make an edge. For example, for the cube we have 12 edges:
```edge = [1 2
1 5
1 4
2 3
2 6
3 4
3 7
4 8
5 6
5 8
6 7
7 8];
```
Note that each edge is just listed once.
Plotting
There are two ways to plot our figure. One it to use MATLAB's built in 3D plotting capabilities and the other is to project the 3D data into 2D and then plot the 2D data.

Here's a function to plot our figure using MATALB's 3D routines:

```function plotfig3d(pt,edge)
% plots a 3D wire frame using points in pt and edges in edge
M = size(edge,1);   % the number of edges
for i = 1:M         % loop thru the edges
plot3(pt(1,edge(i,1:2)), pt(2,edge(i,1:2)), pt(3,edge(i,1:2)))
if (i==1), hold on, end
end
hold off
```
Save this in an M-file as plotfig3d, and if you loaded the cube and edge data from above, you can plot it with:
```plotfig3d(cube,edge)
```
You should get a nice picture of a 3D cube.

To plot in 2D we need a rule (called a projection) on how to convert 3D points into 2D. The easiest version is direct projection along the y-axis. In this case a point (x,y,z) is projected to the point (x,z). A projection is a linear transformation, so we can represent it as a matrix. In homogeneous coordinates, this projection is a 3 by 4 matrix: P = [1 0 0 0; 0 0 1 0; 0 0 0 1];. There are other possibilities that we can work with later. Here's a MATLAB function for plotting with this projection:

```function plotfig2d(pt,edge)
% plots a 3D wire frame using points in pt and edges in edge
% which have been projected into 2D

M = size(edge,1);   % the number of edges
P = [1 0 0 0; 0 0 1 0; 0 0 0 1];   % build the projection
pt = P*pt;          % apply the projection
for i = 1:M         % loop thru the edges
plot(pt(1,edge(i,1:2)), pt(2,edge(i,1:2)))
if (i==1), hold on, end
end
hold off
```
Save this in an M-file as plotfig2d, and if you loaded the cube and edge data from above, you can plot it with:
```plotfig2d(cube,edge)
```
For this you should just get a square as we are looking at the cube end on.
Transformations
The 3D transformations are typically the obvious extension of the 2D transformations to 4 by 4 matrices. The only type that is somewhat different is the rotation. Since we can rotate about any of the three axes, we have three different rotation matrices. Basically we take the 2D rotation matrix with sine's and cosine's and put it in a 4 by 4 matrix where the axis we are rotating around gets only a 1 in its row and column. For example, to rotate about the x-axis we'd have
```ang = pi/4;
T = [1 0 0 0; 0 cos(ang) -sin(ang) 0; 0 sin(ang) cos(ang) 0; 0 0 0 1];
pt2 = T*pt;
plotfig3d(pt2,edge)
```
Just as in 2D, we can perform more complex transformations by taking the product of basic transformations.

Exercises

1. Create a 3D object (that is not a cube) and create the 2 arrays for it: pt and edge. pt uses homogeneous coordinates so it will be 4 by N, while edge is M by 2. I strongly recommend that you put it in a M-file so that you can reuse it as needed and can make changes. Write out the arrays you used or print out your M-file.

2. Save the two plot functions and plot your object using both to test them. Make whatever changes you want to your figure to make it look good.

3. Create two transformations and call them T1 and T2. Create a 3rd transformation T3 = T1*T2. Apply each transformation to your points and create new point sets pt1, pt2 and pt3. (ptk = Tk*pt). Plot the original and 3 transformations on the same graph via:

```plotfig3d(pt,edge)
hold on
plotfig3d(pt1,edge)
hold on
plotfig3d(pt2,edge)
hold on
plotfig3d(pt3,edge)
```
Print this out or export for email.

4. Now plot them using 2D on 4 separate graphs using the following:

```subplot(221)   % uses a 2x2 grid of plots
plotfig2d(pt,edge)
subplot(222)   % plot in 2nd spot
plotfig2d(pt1,edge)
subplot(223)   % plot in 3rd spot
plotfig2d(pt2,edge)
subplot(224)   % plot in 4th spot
plotfig2d(pt3,edge)
```
Print this out or export for email.

Mail: ccollins@math.utk.edu