Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Matrix.java
- // Easel
- //
- // Created by Philip Rhodes on 7/27/05.
- // Copyright 2005 __MyCompanyName__. All rights reserved.
- //
- /** This is the parent class of all our matrix classes. It provides
- * various generic matrix operations such as setToIdentity, left and
- * right multiply, copy, and transform methods for multiplying arrays
- * of coordinates by a matrix.
- * Note that this class is not abstract, and is useful by itself for
- * holding the results of matrix multiplications.
- */
- public class Matrix {
- // If a data member is declared static, there is only one data member
- // for the entire CLASS. This means if we declare 5 Matrix objects,
- // they all must share tempMatrix, tp1, etc.
- // tempMatrix is used as "spare" space for things like multiplication.
- // This avoids having to allocate and garbage collect memory repeatedly.
- static float [][] tempMatrix = new float[4][4];
- // tp1 and tp2
- static float [] tp1 = new float[] {0,0,0,1}; // Let's stick the 1 in now.
- static float [] tp2 = new float[] {0,0,0,1}; // Let's stick the 1 in now.
- // We can use this identity matrix for setToIdentity.
- static float [][] identity = {{1.0f, 0.0f, 0.0f, 0.0f},
- {0.0f, 1.0f, 0.0f, 0.0f},
- {0.0f, 0.0f, 1.0f, 0.0f},
- {0.0f, 0.0f, 0.0f, 1.0f}
- };
- // This data member is not static, and holds the matrix values for this
- // object.
- float [][] matrix;
- /** Default constructor. Construct a 4x4 matrix and set it equal to the identity matrix. */
- public Matrix(){
- float [][] identity = {{1.0f, 0.0f, 0.0f, 0.0f},
- {0.0f, 1.0f, 0.0f, 0.0f},
- {0.0f, 0.0f, 1.0f, 0.0f},
- {0.0f, 0.0f, 0.0f, 1.0f}
- };
- matrix = identity;
- }
- /** Construct a new matrix and set it equal to m0*m1.*/
- protected Matrix(Matrix m0, Matrix m1){
- this(); // Explicitly call the default constructor
- this.lMult(m1);
- this.lMult(m0);
- }
- /** Construct a new matrix and set it equal to m0*m1*m2. */
- protected Matrix(Matrix m0, Matrix m1, Matrix m2){
- this();
- this.lMult(m2);
- this.lMult(m1);
- this.lMult(m0);
- }
- /** Construct a new matrix and set it equal to m0*m1*m2*m3. */
- protected Matrix(Matrix m0, Matrix m1, Matrix m2, Matrix m3){
- this();
- this.lMult(m3);
- this.lMult(m2);
- this.lMult(m1);
- this.lMult(m0);
- }
- /** Construct a new matrix and set it equal to m0*m1*m2*m3*m4. */
- protected Matrix(Matrix m0, Matrix m1, Matrix m2, Matrix m3, Matrix m4){
- this();
- this.lMult(m4);
- this.lMult(m3);
- this.lMult(m2);
- this.lMult(m1);
- this.lMult(m0);
- }
- /** Set this matrix object equal to the identity matrix. */
- public void setToIdentity(){
- matrix = Matrix.identity;
- }
- /** Copy the contents of the parameter matrix m to this matrix. */
- public void copy(Matrix m){
- matrix = m.getMatrixContent();
- }
- private float[][] getMatrixContent()
- {
- return matrix;
- }
- /** Right multiply this matrix by the parameter matrix m.
- * That is, if t is this matrix, then t = t * m
- */
- public void rMult(Matrix m){
- // Write me!
- // Refer to the following lMult for hints
- float [][] result = new float[4][4];
- //using tempMatrix
- for(int final_row_i=0; final_row_i<4; final_row_i++)
- {
- for(int final_col_i=0; final_col_i<4; final_col_i++)
- {
- float temp = 0.0f;
- for(int count=0; count<4;count++)
- {
- temp += matrix[count][final_row_i]*m.getMatrixContent()[final_col_i][count];
- }
- result[final_row_i][final_col_i]=temp;
- }
- }
- matrix = result;
- }
- /** Left multiply this matrix by the parameter matrix m.
- * That is, if t is this matrix, then t = m * t
- */
- public void lMult(Matrix m){
- float [][] result = new float[4][4];
- //using tempMatrix
- for(int final_row_i=0; final_row_i<4; final_row_i++)
- {
- for(int final_col_i=0; final_col_i<4; final_col_i++)
- {
- float temp = 0.0f;
- for(int count=0; count<4;count++)
- {
- temp += m.getMatrixContent()[final_row_i][count]*matrix[count][final_col_i];
- }
- result[final_row_i][final_col_i]=temp;
- }
- }
- matrix = result;
- }
- /** The array parameter should contain vertex coords in interleaved order.
- * e.g. x0,y0,z0,x1,y1,z1,x2,y2,z2.....
- * This method will left multiply the vertices by this matrix, changing
- * the values in the array parameter. A reference to the array is returned
- * for convenience.
- */
- public float [] transform(float [] vertices){
- vertices = transform(vertices, 0, (vertices.length)/3);
- return vertices;
- }
- /** The array parameter should contain vertex coords in interleaved order.
- * e.g. x0,y0,z0,x1,y1,z1,x2,y2,z2.....
- * Starting at the vertex specified by startVertex, This method will left
- * multiply the specified number of vertices by this matrix, changing
- * the values in the array parameter. A reference to the array is returned
- * for convenience.
- */
- public float [] transform(float [] vertices, int startVertex, int numVertices){
- for(int i=startVertex; i<startVertex+numVertices; i++)
- {
- float temp_coords[] = new float[4];
- for(int j=0; j<3; j++)
- {
- temp_coords[j] = vertices[i*3+j];
- }
- temp_coords[3]=1.0f;
- for(int j=0; j<3; j++)
- {
- float coord_temp = 0.0f;
- for(int m=0; m<4; m++)
- {
- coord_temp += matrix[j][m]*temp_coords[m];
- }
- vertices[i*3+j]=coord_temp;
- }
- }
- return vertices;
- }
- /** Return a String representation of this matrix.
- * This allows you to write things like System.out.println(m),
- * where m is a matrix object.
- */
- public String toString(){
- StringBuffer result = new StringBuffer();
- for(int i=0; i<4; i++){
- for(int j=0; j<4; j++){
- result.append(matrix[i][j]);
- result.append(" ");
- }
- result.append("\n");
- }
- return new String(result);
- }
- /*
- public static void main(String args[]) {
- Matrix m1 = new Matrix();
- m1.matrix[0][0]=5.0f;
- m1.matrix[1][1]=3.0f;
- m1.matrix[2][2]=6.0f;
- System.out.println(m1);
- Matrix m2 = new Matrix();
- m2.matrix[0][0]=2.0f;
- m2.matrix[0][1]=9.0f;
- m2.matrix[1][1]=3.0f;
- m2.matrix[2][2]=4.0f;
- m1.rMult(m2);
- System.out.println(m2);
- System.out.println(m1);
- m1.setToIdentity();
- System.out.println(m1);
- m1.copy(m2);
- System.out.println(m1);
- float [] test_vertices = new float[6];
- for(int i=0; i<test_vertices.length; i++)
- {
- test_vertices[i] = (float)i;
- System.out.print(test_vertices[i]+" ");
- }
- System.out.println();
- //test_vertices = m1.transform(test_vertices, 1, 1);
- test_vertices = m1.transform(test_vertices);
- for(int i=0; i<test_vertices.length; i++)
- {
- System.out.print(test_vertices[i]+" ");
- }
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement