This assignment is going to focus on three dimensional transformations, which can be described by 4×4 matrices. As I mentioned in class, you'll want to package this up into a Java class with various access routines:
public class Matrix3D { double matrix[][] = new double[4][4]; public void set(int i, int j, double value) { matrix[i][j] = value; } // SET ONE VALUE public double get(int i, int j) { return matrix[i][j]; } // GET ONE VALUE ... }You can use these set and get routines in various ways. For example, you can copy one matrix into another by:
public void copy(Matrix3D src) { for (int i = 0 ; i < 4 ; i++) for (int j = 0 ; j < 4 ; j++) set(i,j, src.get(i,j)); }
To review what we discussed in class about linear transformations:
3D Transformation Matrices:
Here are the basic 3D matrix primitives:
For example, your method within class Matrix3D to build a translation matrix might look like this:
identity:
1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 translationMatrix(a,b,c):
1 0 0 a 0 1 0 b 0 0 1 c 0 0 0 1 xRotationMatrix(θ):
1 0 0 0 0 cos(θ) -sin(θ) 0 0 sin(θ) cos(θ) 0 0 0 0 1 yRotationMatrix(θ):
cos(θ) 0 sin(θ) 0 0 1 0 0 -sin(θ) 0 cos(θ) 0 0 0 0 1 zRotationMatrix(θ):
cos(θ) -sin(θ) 0 0 sin(θ) cos(θ) 0 0 0 0 1 0 0 0 0 1 scaleMatrix(a,b,c):
a 0 0 0 0 b 0 0 0 0 c 0 0 0 0 1
public class Matrix3D { ... public void translationMatrix(double a, double b, double c) { identity(); matrix[0][3] = a; matrix[1][3] = b; matrix[2][3] = c; } }You'll also want to implement a method to do matrix multiplication. Remember that you do this by multiplying the elements in the rows of the first matrix and the columns of the second matrix. You will find it convenient to implement a method that copies the contents of one matrix into another. Then to multiply two matrices A and B:
static Matrix3D temp = new Matrix3D(); ... temp.copy(A); // FIRST COPY TO A TEMPORARY MATRIX for (int i = 0 ; i < 4 ; i++) for (int j = 0 ; j < 4 ; j++) { double sum = 0; for (int k = 0 ; k < 4 ; k++) sum += temp.get(i,k) * B.get(k,j); matrix[i][j] = sum;You should also make methods:
translate(a, b, c); rotateX(theta); rotateY(theta); rotateZ(theta); scale(a, b, c);Each of these first creates the translation, rotation or scale matrix, respectively, and then does a matrix multiply. For example:
public void translate(double a, double b, double c) { temp.translationMatrix(a,b,c); // CREATE A TRANSLATION MATRIX multiply(temp); // MULTIPLY BY THAT MATRIX }This will give you everything you need to combine matrix operations. For example, here is a sequence of steps to first translate, then rotate, and then scale a matrix.
Matrix3D m = new Matrix3D(); ... m.identity(); m.translate(1,0,0); m.rotateY(Math.PI/2); m.scale(.5,.5,.5);Finally, you'll need a method that applies a matrix transformation to a point. Here's how you might implement that:
public void transform(double src[], double dst[]) { dst[0] = matrix[0][0] * src[0] + matrix[0][1] * src[1] + matrix[0][2] * src[2] + matrix[0][3]; dst[1] = matrix[1][0] * src[0] + matrix[1][1] * src[1] + matrix[1][2] * src[2] + matrix[1][3]; dst[2] = matrix[2][0] * src[0] + matrix[2][1] * src[1] + matrix[2][2] * src[2] + matrix[2][3]; }
Your assignment for Thursday is to: