Advertisement
Luninariel

289 - Matrix

Apr 8th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.61 KB | None | 0 0
  1. //
  2. //  Matrix.java
  3. //  Easel
  4. //
  5. //  Created by Philip Rhodes on 7/27/05.
  6. //  Copyright 2005 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. /** This is the parent class of all our matrix classes. It provides
  10.  *  various generic matrix operations such as setToIdentity, left and
  11.  *  right multiply, copy, and transform methods for multiplying arrays
  12.  *  of coordinates by a matrix.
  13.  *  Note that this class is not abstract, and is useful by itself for
  14.  *  holding the results of matrix multiplications.
  15.  */
  16. public class Matrix {
  17.  
  18.    // If a data member is declared static, there is only one data member
  19.    // for the entire CLASS.  This means if we declare 5 Matrix objects,
  20.    // they all must share tempMatrix, tp1, etc.
  21.    
  22.    // tempMatrix is used as "spare" space for things like multiplication.
  23.    // This avoids having to allocate and garbage collect memory repeatedly.
  24.    static float [][] tempMatrix = new float[4][4];
  25.    
  26.    // tp1 and tp2
  27.    static float [] tp1 = new float[] {0,0,0,1}; // Let's stick the 1 in now.
  28.    static float [] tp2 = new float[] {0,0,0,1}; // Let's stick the 1 in now.
  29.    
  30.    // We can use this identity matrix for setToIdentity.
  31.    static float [][] identity = {{1.0f, 0.0f, 0.0f, 0.0f},
  32.                                  {0.0f, 1.0f, 0.0f, 0.0f},
  33.                                  {0.0f, 0.0f, 1.0f, 0.0f},    
  34.                                  {0.0f, 0.0f, 0.0f, 1.0f}
  35.                                 };
  36.    
  37.    // This data member is not static, and holds the matrix values for this
  38.    // object.
  39.    float [][] matrix;
  40.    
  41.    /** Default constructor. Construct a 4x4 matrix and set it equal to the identity matrix. */
  42.    public Matrix(){
  43.  
  44.       float  [][] identity = {{1.0f, 0.0f, 0.0f, 0.0f},
  45.                               {0.0f, 1.0f, 0.0f, 0.0f},
  46.                               {0.0f, 0.0f, 1.0f, 0.0f},    
  47.                               {0.0f, 0.0f, 0.0f, 1.0f}
  48.                            };
  49.  
  50.       matrix = identity;
  51.      
  52.    }  
  53.    
  54.    /** Construct a new matrix and set it equal to m0*m1.*/
  55.    protected Matrix(Matrix m0, Matrix m1){
  56.    
  57.       this(); // Explicitly call the default constructor
  58.      
  59.       this.lMult(m1);
  60.       this.lMult(m0);
  61.    }  
  62.  
  63.    /** Construct a new matrix and set it equal to m0*m1*m2. */
  64.    protected Matrix(Matrix m0, Matrix m1, Matrix m2){
  65.    
  66.       this();
  67.      
  68.       this.lMult(m2);
  69.       this.lMult(m1);
  70.       this.lMult(m0);
  71.    }  
  72.  
  73.    /** Construct a new matrix and set it equal to m0*m1*m2*m3. */
  74.    protected Matrix(Matrix m0, Matrix m1, Matrix m2, Matrix m3){
  75.    
  76.       this();
  77.      
  78.       this.lMult(m3);
  79.       this.lMult(m2);
  80.       this.lMult(m1);
  81.       this.lMult(m0);
  82.    }  
  83.  
  84.     /** Construct a new matrix and set it equal to m0*m1*m2*m3*m4. */
  85.    protected Matrix(Matrix m0, Matrix m1, Matrix m2, Matrix m3, Matrix m4){
  86.    
  87.       this();
  88.      
  89.       this.lMult(m4);
  90.       this.lMult(m3);
  91.       this.lMult(m2);
  92.       this.lMult(m1);
  93.       this.lMult(m0);
  94.    }  
  95.  
  96.  
  97.    /** Set this matrix object equal to the identity matrix. */
  98.    public void setToIdentity(){  
  99.       matrix = Matrix.identity;
  100.    }
  101.    
  102.    /** Copy the contents of the parameter matrix m to this matrix. */
  103.    public void copy(Matrix m){
  104.       matrix = m.getMatrixContent();
  105.    }
  106.  
  107.    private float[][] getMatrixContent()
  108.    {
  109.         return matrix;
  110.    }
  111.    
  112.    /** Right multiply this matrix by the parameter matrix m.
  113.     *  That is, if t is this matrix, then t = t * m
  114.     */
  115.    public void rMult(Matrix m){
  116.       // Write me!
  117.       // Refer to the following lMult for hints
  118.        float  [][] result = new float[4][4];
  119.        //using tempMatrix
  120.  
  121.        for(int final_row_i=0; final_row_i<4; final_row_i++)
  122.        {
  123.            for(int final_col_i=0; final_col_i<4; final_col_i++)
  124.            {
  125.                float temp = 0.0f;
  126.                for(int count=0; count<4;count++)
  127.                {
  128.                    temp += matrix[count][final_row_i]*m.getMatrixContent()[final_col_i][count];
  129.                }
  130.                result[final_row_i][final_col_i]=temp;
  131.            }
  132.        }
  133.        matrix = result;
  134.    
  135.    
  136.    }
  137.  
  138.    /** Left multiply this matrix by the parameter matrix m.
  139.     *  That is, if t is this matrix, then t = m * t
  140.     */
  141.    public void lMult(Matrix m){
  142.      
  143.       float  [][] result = new float[4][4];
  144.       //using tempMatrix
  145.      
  146.       for(int final_row_i=0; final_row_i<4; final_row_i++)
  147.       {
  148.           for(int final_col_i=0; final_col_i<4; final_col_i++)
  149.           {
  150.               float temp = 0.0f;
  151.               for(int count=0; count<4;count++)
  152.               {
  153.                   temp += m.getMatrixContent()[final_row_i][count]*matrix[count][final_col_i];  
  154.               }
  155.               result[final_row_i][final_col_i]=temp;
  156.           }
  157.       }
  158.       matrix = result;
  159.    }
  160.    
  161.  
  162.    /** The array parameter should contain vertex coords in interleaved order.
  163.     *  e.g.  x0,y0,z0,x1,y1,z1,x2,y2,z2.....
  164.     *  This method will left multiply the vertices by this matrix, changing
  165.     *  the values in the array parameter.  A reference to the array is returned
  166.     *  for convenience.
  167.     */
  168.    public float [] transform(float [] vertices){
  169.      
  170.         vertices = transform(vertices, 0, (vertices.length)/3);
  171.         return vertices;
  172.    }
  173.    
  174.    /** The array parameter should contain vertex coords in interleaved order.
  175.     *  e.g.  x0,y0,z0,x1,y1,z1,x2,y2,z2.....
  176.     *  Starting at the vertex specified by startVertex, This method will left
  177.     *  multiply the specified number of vertices by this matrix, changing
  178.     *  the values in the array parameter.  A reference to the array is returned
  179.     *  for convenience.
  180.     */
  181.    public float [] transform(float [] vertices, int startVertex, int numVertices){
  182.      
  183.       for(int i=startVertex; i<startVertex+numVertices; i++)
  184.       {
  185.           float temp_coords[] = new float[4];
  186.           for(int j=0; j<3; j++)
  187.           {
  188.               temp_coords[j] = vertices[i*3+j];
  189.           }
  190.           temp_coords[3]=1.0f;
  191.  
  192.           for(int j=0; j<3; j++)
  193.           {
  194.               float coord_temp = 0.0f;
  195.               for(int m=0; m<4; m++)
  196.               {
  197.                   coord_temp += matrix[j][m]*temp_coords[m];
  198.               }
  199.               vertices[i*3+j]=coord_temp;
  200.           }
  201.       }
  202.       return vertices;
  203.    }
  204.    
  205.    /** Return a String representation of this matrix.
  206.     *  This allows you to write things like System.out.println(m),
  207.     *  where m is a matrix object.
  208.     */
  209.    public String toString(){
  210.      
  211.       StringBuffer result = new StringBuffer();
  212.  
  213.       for(int i=0; i<4; i++){
  214.          for(int j=0; j<4; j++){
  215.          
  216.             result.append(matrix[i][j]);
  217.             result.append(" ");
  218.          }
  219.          
  220.          result.append("\n");
  221.       }
  222.    
  223.       return new String(result);
  224.    }
  225.    /*
  226.      public static void main(String args[]) {
  227.             Matrix m1 = new Matrix();
  228.             m1.matrix[0][0]=5.0f;
  229.             m1.matrix[1][1]=3.0f;
  230.             m1.matrix[2][2]=6.0f;
  231.            
  232.             System.out.println(m1);
  233.            
  234.             Matrix m2 = new Matrix();
  235.             m2.matrix[0][0]=2.0f;
  236.             m2.matrix[0][1]=9.0f;
  237.             m2.matrix[1][1]=3.0f;
  238.             m2.matrix[2][2]=4.0f;
  239.            
  240.             m1.rMult(m2);
  241.             System.out.println(m2);
  242.             System.out.println(m1);
  243.            
  244.             m1.setToIdentity();
  245.             System.out.println(m1);
  246.            
  247.             m1.copy(m2);
  248.             System.out.println(m1);
  249.            
  250.             float [] test_vertices = new float[6];
  251.            
  252.             for(int i=0; i<test_vertices.length; i++)
  253.             {
  254.                 test_vertices[i] = (float)i;
  255.                 System.out.print(test_vertices[i]+" ");
  256.             }
  257.             System.out.println();
  258.            
  259.             //test_vertices = m1.transform(test_vertices, 1, 1);
  260.             test_vertices = m1.transform(test_vertices);
  261.             for(int i=0; i<test_vertices.length; i++)
  262.             {
  263.                 System.out.print(test_vertices[i]+" ");
  264.             }
  265.            
  266.      }
  267.      */
  268.  
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement