org.sunflow.math

Class Matrix4


public final class Matrix4
extends java.lang.Object

This class is used to represent general geometric transformations in 3D. Note that the rotation matrices assume a right-handed convention.

Field Summary

static Matrix4
IDENTITY
static Matrix4
ZERO

Constructor Summary

Matrix4()
Creates an empty matrix.
Matrix4(float m00, float m01, float m02, float m03, float m10, float m11, float m12, float m13, float m20, float m21, float m22, float m23, float m30, float m31, float m32, float m33)
Creates a matrix with the specified elements
Matrix4(float[] m, boolean rowMajor)
Initialize a matrix from the specified 16 element array.

Method Summary

float[]
asColMajor()
float[]
asRowMajor()
static Matrix4
blend(Matrix4 m0, Matrix4 m1, float t)
float
determinant()
Compute the matrix determinant.
static Matrix4
fromBasis(OrthoNormalBasis basis)
Creates a rotation matrix from an OrthonormalBasis.
Matrix4
inverse()
Compute the inverse of this matrix and return it as a new object.
Matrix4
multiply(Matrix4 m)
Computes this*m and return the result as a new Matrix4
static Matrix4
rotate(float x, float y, float z, float theta)
Creates a rotation matrix about the specified axis.
static Matrix4
rotateX(float theta)
Creates a rotation matrix about the X axis.
static Matrix4
rotateY(float theta)
Creates a rotation matrix about the Y axis.
static Matrix4
rotateZ(float theta)
Creates a rotation matrix about the Z axis.
static Matrix4
scale(float s)
Create a uniform scaling matrix.
static Matrix4
scale(float sx, float sy, float sz)
Creates a non-uniform scaling matrix.
BoundingBox
transform(BoundingBox b)
Transforms each corner of the specified axis-aligned bounding box and returns a new bounding box which incloses the transformed corners.
Point3
transformP(Point3 p)
Computes this*p and returns the result as a new Point3 object.
float
transformPX(float x, float y, float z)
Computes the x component of this*(x,y,z,1).
float
transformPY(float x, float y, float z)
Computes the y component of this*(x,y,z,1).
float
transformPZ(float x, float y, float z)
Computes the z component of this*(x,y,z,1).
Vector3
transformTransposeV(Vector3 v)
Computes (this^T)*v and returns the result as a new Vector3 object.
float
transformTransposeVX(float x, float y, float z)
Computes the x component of (this^T)*(x,y,z,0).
float
transformTransposeVY(float x, float y, float z)
Computes the y component of (this^T)*(x,y,z,0).
float
transformTransposeVZ(float x, float y, float z)
Computes the z component of (this^T)*(x,y,z,0).
Vector3
transformV(Vector3 v)
Computes this*v and returns the result as a new Vector3 object.
float
transformVX(float x, float y, float z)
Computes the x component of this*(x,y,z,0).
float
transformVY(float x, float y, float z)
Computes the y component of this*(x,y,z,0).
float
transformVZ(float x, float y, float z)
Computes the z component of this*(x,y,z,0).
static Matrix4
translation(float x, float y, float z)
Create a translation matrix for the specified vector.

Field Details

IDENTITY

public static final Matrix4 IDENTITY

ZERO

public static final Matrix4 ZERO

Constructor Details

Matrix4

public Matrix4()
Creates an empty matrix. All elements are 0.

Matrix4

public Matrix4(float m00,
               float m01,
               float m02,
               float m03,
               float m10,
               float m11,
               float m12,
               float m13,
               float m20,
               float m21,
               float m22,
               float m23,
               float m30,
               float m31,
               float m32,
               float m33)
Creates a matrix with the specified elements
Parameters:
m00 - value at row 0, col 0
m01 - value at row 0, col 1
m02 - value at row 0, col 2
m03 - value at row 0, col 3
m10 - value at row 1, col 0
m11 - value at row 1, col 1
m12 - value at row 1, col 2
m13 - value at row 1, col 3
m20 - value at row 2, col 0
m21 - value at row 2, col 1
m22 - value at row 2, col 2
m23 - value at row 2, col 3
m30 - value at row 3, col 0
m31 - value at row 3, col 1
m32 - value at row 3, col 2
m33 - value at row 3, col 3

Matrix4

public Matrix4(float[] m,
               boolean rowMajor)
Initialize a matrix from the specified 16 element array. The matrix may be given in row or column major form.
Parameters:
m - a 16 element array in row or column major form
rowMajor - true if the array is in row major form, falseif it is in column major form

Method Details

asColMajor

public final float[] asColMajor()

asRowMajor

public final float[] asRowMajor()

blend

public static final Matrix4 blend(Matrix4 m0,
                                  Matrix4 m1,
                                  float t)

determinant

public final float determinant()
Compute the matrix determinant.
Returns:
determinant of this matrix

fromBasis

public static final Matrix4 fromBasis(OrthoNormalBasis basis)
Creates a rotation matrix from an OrthonormalBasis.
Parameters:
basis -

inverse

public final Matrix4 inverse()
Compute the inverse of this matrix and return it as a new object. If the matrix is not invertible, null is returned.
Returns:
the inverse of this matrix, or null if not invertible

multiply

public final Matrix4 multiply(Matrix4 m)
Computes this*m and return the result as a new Matrix4
Parameters:
m - right hand side of the multiplication
Returns:
a new Matrix4 object equal to this*m

rotate

public static final Matrix4 rotate(float x,
                                   float y,
                                   float z,
                                   float theta)
Creates a rotation matrix about the specified axis. The axis vector need not be normalized.
Parameters:
x - x component of the axis vector
y - y component of the axis vector
z - z component of the axis vector
theta - angle to rotate about the axis in radians
Returns:
a new Matrix4 object representing the rotation

rotateX

public static final Matrix4 rotateX(float theta)
Creates a rotation matrix about the X axis.
Parameters:
theta - angle to rotate about the X axis in radians
Returns:
a new Matrix4 object representing the rotation

rotateY

public static final Matrix4 rotateY(float theta)
Creates a rotation matrix about the Y axis.
Parameters:
theta - angle to rotate about the Y axis in radians
Returns:
a new Matrix4 object representing the rotation

rotateZ

public static final Matrix4 rotateZ(float theta)
Creates a rotation matrix about the Z axis.
Parameters:
theta - angle to rotate about the Z axis in radians
Returns:
a new Matrix4 object representing the rotation

scale

public static final Matrix4 scale(float s)
Create a uniform scaling matrix.
Parameters:
s - scale factor for all three axes
Returns:
a new Matrix4 object representing the uniform scale

scale

public static final Matrix4 scale(float sx,
                                  float sy,
                                  float sz)
Creates a non-uniform scaling matrix.
Parameters:
sx - scale factor in the x dimension
sy - scale factor in the y dimension
sz - scale factor in the z dimension
Returns:
a new Matrix4 object representing the non-uniform scale

transform

public final BoundingBox transform(BoundingBox b)
Transforms each corner of the specified axis-aligned bounding box and returns a new bounding box which incloses the transformed corners.
Parameters:
b - original bounding box
Returns:
a new BoundingBox object which encloses the transform version of b

transformP

public final Point3 transformP(Point3 p)
Computes this*p and returns the result as a new Point3 object. This method assumes the bottom row of the matrix is [0,0,0,1].
Parameters:
p - point to multiply
Returns:
a new Point3 object equal to this*v

transformPX

public final float transformPX(float x,
                               float y,
                               float z)
Computes the x component of this*(x,y,z,1).
Parameters:
x - x coordinate of the vector to multiply
y - y coordinate of the vector to multiply
z - z coordinate of the vector to multiply
Returns:
x coordinate transformation result

transformPY

public final float transformPY(float x,
                               float y,
                               float z)
Computes the y component of this*(x,y,z,1).
Parameters:
x - x coordinate of the vector to multiply
y - y coordinate of the vector to multiply
z - z coordinate of the vector to multiply
Returns:
y coordinate transformation result

transformPZ

public final float transformPZ(float x,
                               float y,
                               float z)
Computes the z component of this*(x,y,z,1).
Parameters:
x - x coordinate of the vector to multiply
y - y coordinate of the vector to multiply
z - z coordinate of the vector to multiply
Returns:
z coordinate transformation result

transformTransposeV

public final Vector3 transformTransposeV(Vector3 v)
Computes (this^T)*v and returns the result as a new Vector3 object. This method assumes the bottom row of the matrix is [0,0,0,1].
Parameters:
v - vector to multiply
Returns:
a new Vector3 object equal to (this^T)*v

transformTransposeVX

public final float transformTransposeVX(float x,
                                        float y,
                                        float z)
Computes the x component of (this^T)*(x,y,z,0).
Parameters:
x - x coordinate of the vector to multiply
y - y coordinate of the vector to multiply
z - z coordinate of the vector to multiply
Returns:
x coordinate transformation result

transformTransposeVY

public final float transformTransposeVY(float x,
                                        float y,
                                        float z)
Computes the y component of (this^T)*(x,y,z,0).
Parameters:
x - x coordinate of the vector to multiply
y - y coordinate of the vector to multiply
z - z coordinate of the vector to multiply
Returns:
y coordinate transformation result

transformTransposeVZ

public final float transformTransposeVZ(float x,
                                        float y,
                                        float z)
Computes the z component of (this^T)*(x,y,z,0).
Parameters:
x - x coordinate of the vector to multiply
y - y coordinate of the vector to multiply
z - z coordinate of the vector to multiply
Returns:
zcoordinate transformation result

transformV

public final Vector3 transformV(Vector3 v)
Computes this*v and returns the result as a new Vector3 object. This method assumes the bottom row of the matrix is [0,0,0,1].
Parameters:
v - vector to multiply
Returns:
a new Vector3 object equal to this*v

transformVX

public final float transformVX(float x,
                               float y,
                               float z)
Computes the x component of this*(x,y,z,0).
Parameters:
x - x coordinate of the vector to multiply
y - y coordinate of the vector to multiply
z - z coordinate of the vector to multiply
Returns:
x coordinate transformation result

transformVY

public final float transformVY(float x,
                               float y,
                               float z)
Computes the y component of this*(x,y,z,0).
Parameters:
x - x coordinate of the vector to multiply
y - y coordinate of the vector to multiply
z - z coordinate of the vector to multiply
Returns:
y coordinate transformation result

transformVZ

public final float transformVZ(float x,
                               float y,
                               float z)
Computes the z component of this*(x,y,z,0).
Parameters:
x - x coordinate of the vector to multiply
y - y coordinate of the vector to multiply
z - z coordinate of the vector to multiply
Returns:
z coordinate transformation result

translation

public static final Matrix4 translation(float x,
                                        float y,
                                        float z)
Create a translation matrix for the specified vector.
Parameters:
x - x component of translation
y - y component of translation
z - z component of translation
Returns:
a new Matrix4 object representing the translation