com.threed.jpct
Class Matrix

java.lang.Object
  extended bycom.threed.jpct.Matrix
All Implemented Interfaces:
java.io.Serializable

public class Matrix
extends java.lang.Object
implements java.io.Serializable

This is jPCT's basic class for working with 4x4 matrices. All matrices in jPCT are row major.

See Also:
Serialized Form

Constructor Summary
Matrix()
          Creates a new matrix and sets it to the identity matrix.
Matrix(Matrix m)
          Creates a matrix from a given one.
 
Method Summary
 Matrix cloneMatrix()
          Creates a copy of this matrix.
 boolean equals(java.lang.Object obj)
           
 float[] getDump()
          Dumps a matrix row-wise into a float[16]-array.
 SimpleVector getTranslation()
          Returns the translation this matrix would represent when viewed as a translation matrix.
 SimpleVector getXAxis()
          Returns the x-axis this matrix would represent when viewed as a rotation matrix.
 SimpleVector getYAxis()
          Returns the y-axis this matrix would represent when viewed as a rotation matrix.
 SimpleVector getZAxis()
          Returns the z-axis this matrix would represent when viewed as a rotation matrix.
 void interpolate(Matrix source, Matrix dest, float weight)
          Fills the matrix with data interpolated between the source and the destination matrix.
 Matrix invert()
          Calculates the inverse of this matrix.
 Matrix invert3x3()
          Calculates the inverse of this matrix as if this matrix would be a 3x3 one (instead of the 4x4 it actually is).
 boolean isIdentity()
          Returns true, if this matrix is the identity matrix.
 void matMul(Matrix mat2)
          Multiplies this matrix with another one.
 void orthonormalize()
          Orthonormalizes a matrix using the Gramm-Schmidt orthonormalization algorithm.
 void orthonormalizeDouble()
          Orthonormalizes a matrix using the Gramm-Schmidt orthonormalization algorithm.
 void rotateAxis(SimpleVector axis, float angle)
          Rotates the matrix around an arbitrary axis.
 void rotateX(float w)
          Rotates the matrix around the X-axis (counter clockwise for positive w).
 void rotateY(float w)
          Rotates the matrix around the Y-axis (clockwise for positive w).
 void rotateZ(float w)
          Rotates the matrix around the Z-axis (counter clockwise for positive w).
 void scalarMul(float scalar)
          Multiplies this matrix with a scalar (the left upper 3x3 part only).
 void set(int row, int col, float value)
          Injects a value directly into the matrix at a position.
 void setDump(float[] dump)
          Reimports a dumped matrix (or every data from a float[16]-array) into a Matrix.
 void setIdentity()
          (Re-)sets this matrix to the identity matrix.
 void setTo(Matrix source)
          Sets this matrix' values to the ones of the source matrix.
 java.lang.String toString()
           
 void translate(float x, float y, float z)
          Applies a translation to this matrix.
 void translate(SimpleVector trans)
          Applies a translation to this matrix.
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Matrix

public Matrix()
Creates a new matrix and sets it to the identity matrix.


Matrix

public Matrix(Matrix m)
Creates a matrix from a given one. The new matrix will bi initialized with the same values that the source matrix has.

Parameters:
m - the source matrix
Method Detail

isIdentity

public final boolean isIdentity()
Returns true, if this matrix is the identity matrix.

Returns:
boolean true is it is, otherwise false

setIdentity

public final void setIdentity()
(Re-)sets this matrix to the identity matrix.


scalarMul

public final void scalarMul(float scalar)
Multiplies this matrix with a scalar (the left upper 3x3 part only).

Parameters:
scalar - the scalar to multiply with

matMul

public final void matMul(Matrix mat2)
Multiplies this matrix with another one.

Parameters:
mat2 - the matrix to multiply with

rotateX

public final void rotateX(float w)
Rotates the matrix around the X-axis (counter clockwise for positive w).

Parameters:
w - the rotation angle

rotateY

public final void rotateY(float w)
Rotates the matrix around the Y-axis (clockwise for positive w).

Parameters:
w - the rotation angle

rotateZ

public final void rotateZ(float w)
Rotates the matrix around the Z-axis (counter clockwise for positive w).

Parameters:
w - the rotation angle

rotateAxis

public final void rotateAxis(SimpleVector axis,
                             float angle)
Rotates the matrix around an arbitrary axis. The method is more powerful than the normal rotate-around-an-axis methods, but also a bit slower. The resulting matrix will be orthonormalized to ensure numerical accuracy.

Parameters:
axis - a direction-vector pointing into the axis direction
angle - the angle of the rotation

interpolate

public final void interpolate(Matrix source,
                              Matrix dest,
                              float weight)
Fills the matrix with data interpolated between the source and the destination matrix. The interpolation is (source*(1-weight)+dest*weigth), so it's a simple linear interpolation. The interpolated matrix is orthonormal. However, it's possible that the interpolation will result in a NaN-Matrix, if the matrices to interpolate are too different from each other. Keep that in mind...

Parameters:
source - the source matrix
dest - the destination matrix
weight - the weight of the destination matrix (0-1)

getTranslation

public final SimpleVector getTranslation()
Returns the translation this matrix would represent when viewed as a translation matrix.

Returns:
the translation

getXAxis

public final SimpleVector getXAxis()
Returns the x-axis this matrix would represent when viewed as a rotation matrix.

Returns:
the x-axis

getYAxis

public final SimpleVector getYAxis()
Returns the y-axis this matrix would represent when viewed as a rotation matrix.

Returns:
the y-axis

getZAxis

public final SimpleVector getZAxis()
Returns the z-axis this matrix would represent when viewed as a rotation matrix.

Returns:
the z-axis

translate

public final void translate(SimpleVector trans)
Applies a translation to this matrix.

Parameters:
trans - the translation

translate

public final void translate(float x,
                            float y,
                            float z)
Applies a translation to this matrix. This implementation should help to avoid the unnecessary creation of a SimpleVector if the coordinates already exist as three seperated ones.

Parameters:
x - the x component of the translation
y - the y component of the translation
z - the z component of the translation

cloneMatrix

public final Matrix cloneMatrix()
Creates a copy of this matrix.

Returns:
the copied matrix

invert

public final Matrix invert()
Calculates the inverse of this matrix.

Returns:
the inverse

invert3x3

public final Matrix invert3x3()
Calculates the inverse of this matrix as if this matrix would be a 3x3 one (instead of the 4x4 it actually is). This can be useful to invert matrices that are rotation matrices only, because they usually can be treated as 3x3 and it's much faster to do it this way than to use the normal invert()-method. In case of doubt, one may be better off using the normal invert()-method though.

Returns:
the inverse
See Also:
invert()

orthonormalizeDouble

public final void orthonormalizeDouble()
Orthonormalizes a matrix using the Gramm-Schmidt orthonormalization algorithm. This can be useful for rotation matrices to ensure that they stay orthonormal over time (i.e. after a lot of rotations have been applied to them). jPCT doesn't do this automatically, because there seems to be no need for it at the moment. But if there ever is, here is the method for doing it... This method uses doubles internally (hence its name) and is therefor more precise than orthonormalize() but also a little bit slower (should be neglectable anyway).

See Also:
orthonormalize()

orthonormalize

public final void orthonormalize()
Orthonormalizes a matrix using the Gramm-Schmidt orthonormalization algorithm. This can be useful for rotation matrices to ensure that they stay orthonormal over time (i.e. after a lot of rotations have been applied to them). jPCT doesn't do this automatically, because there seems to be no need for it at the moment. But if there ever is, here is the method for doing it...


getDump

public final float[] getDump()
Dumps a matrix row-wise into a float[16]-array.

Returns:
the "dump"

setDump

public final void setDump(float[] dump)
Reimports a dumped matrix (or every data from a float[16]-array) into a Matrix.

Parameters:
dump - the "dump"

setTo

public final void setTo(Matrix source)
Sets this matrix' values to the ones of the source matrix.

Parameters:
source - the source matrix

set

public final void set(int row,
                      int col,
                      float value)
Injects a value directly into the matrix at a position.

Parameters:
row - the row
col - the column
value - the value to set

toString

public java.lang.String toString()

equals

public boolean equals(java.lang.Object obj)