/* * pMatrix.java v0.01 May 16th, 1998 * * A general purpose (rotation and translation) matrix. * (05/16/1998,05/17/1998,4/2005) * * Copyright(c) 1998, Alex S, Particle */ import java.lang.String; import java.util.Vector; /** * pMatrix class, to handle all the matrix thingies. * the class represents a 4x4 matrix, with some * useful operations defined on it. * * Note: This thing is Right Handed!; just like OpenGL :-) */ public class pMatrix { /** * the matrix stored as 2D array. */ protected double[] matrix; /** * the stack to hold working matrices. */ protected static Vector stack; /** * get the stack going... */ static{ stack = new Vector(); } /** * default constructor, initializes the matrix, * and makes sure it's an "intentity" matrix. */ public pMatrix(){ matrix = new double[16]; ident(); } /** * create a matrix from a 16 element array */ public pMatrix(double[] m){ matrix = new double[16]; System.arraycopy(m,0,matrix, 0, 16); } /** * a copy constructor. * * @param m The matrix to copy. */ public pMatrix(pMatrix m){ matrix = new double[16]; System.arraycopy(m.matrix, 0, matrix, 0, 16); } /** * pushes the current matrix onto the stack, * (the current matrix is still there though) */ public void push(){ pMatrix tmp = new pMatrix(this); stack.addElement(tmp.matrix); } /** * pops the last pushed matrix from the stack, * and makes it current. (the previous one is * erased) *
* NOTE: no error checking is performed, you WILL
* get a NoSuchElementException if you're not careful
* and try to pop an empty stack.
*
* @return The freshly poped matrix.
*/
public pMatrix pop(){
matrix = (double[])stack.lastElement();
stack.removeElement(matrix);
return this;
}
/**
* makes this matrix into an identity matrix.
* (current info of the matrix is erased)
*
* @return Current identity matrix.
*/
public pMatrix ident(){
for (int i=0;i<4;i++)
for (int j=0;j<4;j++)
matrix[(i<<2)+j] = i == j ? 1:0;
return this;
}
/**
* add another matrix to this one.
* (changes current matrix)
*
* @param m The matrix to add.
* @return The current changed matrix.
*/
public pMatrix add(pMatrix m){
for (int i=0;i<16;i++)
matrix[i] += m.matrix[i];
return this;
}
/**
* subtract a matrix from this one
* (changes the current matrix)
*
* @param m The matrix to subtract.
* @return The current changed matrix.
*/
public pMatrix sub(pMatrix m){
for (int i=0;i<16;i++)
matrix[i] -= m.matrix[i];
return this;
}
/**
* get function to the rest of the world.
*
* @param i The column.
* @param j The row.
* @return The location of that row and column.
*/
public double get(int i,int j){
return matrix[(i<<2)+j];
}
/**
* set function for the rest of the world.
*
* @param i The column.
* @param j The row.
* @param v The value of the new location.
*/
public void set(int i,int j,double v){
matrix[(i<<2)+j] = v;
}
/**
* function to multiply this matrix by another.
* (the result is a cross multiply of this matrix
* by the parameter matrix.)
* (current matrix changes!)
*
* @param m The matrix to multiply by.
* @return The current changed matrix.
*/
public pMatrix mult(pMatrix m){
pMatrix tmp = new pMatrix(this);
for (int i=0;i<4;i++)
for (int j=0;j<4;j++) {
matrix[(i<<2)+j] = 0;
for (int k=0;k<4;k++)
matrix[(i<<2)+j] += tmp.matrix[(i<<2)+k] * m.matrix[(k<<2)+j];
}
return this;
}
/**
* function to transform a vector,
* (a vector consists of a 4 element array of doubles,
* the last element of the array is reserved)
*
* @param v The vector to transform.
* @return The transformed vector.
*/
public double[] mult(double[] v){
double[] tmp = new double[4];
int i;
for (i=0;i<4;i++)
tmp[i] = v[i];
for (i=0;i<4;i++) {
v[i] = 0;
for (int j=0;j<4;j++)
v[i] += matrix[(i<<2)+j] * tmp[j];
}
return v;
}
/**
* function to transform an array of vectors...
*
* @param a The array of arrays of vectors.
* @return The transformed array...
*/
public double[][] mult(double[][] a){
for (int i=0;i