Package nub.primitives
Class Matrix
- java.lang.Object
-
- nub.primitives.Matrix
-
public class Matrix extends Object
4x4 matrix affine matrix implementation. Matrix is represented in column major order: | m0 m4 m8 m12 | | m1 m5 m9 m13 | | m2 m6 m10 m14 | | m3 m7 m11 m15 |
-
-
Field Summary
Fields Modifier and Type Field Description float[]_matrix
-
Constructor Summary
Constructors Constructor Description Matrix()Constructor for an identity matrix.Matrix(float[] source)Same asthis(data, true).Matrix(float[] source, boolean columnMajorOrder)Sets the matrix contents from the 16 entrysourcefloat array given incolumnMajorOrderor row major order (ifcolumnMajorOrderisfalse).Matrix(float m0, float m1, float m2, float m3, float m4, float m5, float m6, float m7, float m8, float m9, float m10, float m11, float m12, float m13, float m14, float m15)Same asthis(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, true).Matrix(float m0, float m1, float m2, float m3, float m4, float m5, float m6, float m7, float m8, float m9, float m10, float m11, float m12, float m13, float m14, float m15, boolean columnMajorOrder)Same asset(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, columnMajorOrder).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidapply(Matrix matrix)Multiply this matrix bymatrix.Matrixcopy()Returns a copy of this matrix.floatdeterminant()float[]get(float[] target)Same asreturn get(target, true).float[]get(float[] target, boolean columnMajorOrder)Copies the matrix contents into a 16 entrytargetfloat array.static MatrixhudProjection(int width, int height)Returns a hud (heads-up-display) projection matrix according to the windowwidthandheightdimensions.static MatrixhudView(int width, int height)Returns a hud (heads-up-display) view according to the windowwidthandheightdimensions.static Matrixinverse(Matrix matrix)Returns the inverse ofmatrix.booleaninvert()Invert this matrix.booleaninvert(Matrix matrix)Invert this matrix intomatrix, i.e., doesn't modify this matrix.floatm00()Sets the 1st-row, 1st-column matrix entry.floatm01()Returns the 1st-row, 2nd-column matrix entry.floatm02()Returns the 1st-row, 3rd-column matrix entry.floatm03()Returns the 1st-row, 4th-column matrix entry.floatm10()Returns the 2nd-row, 1st-column matrix entry.floatm11()Returns the 2nd-row, 2nd-column matrix entry.floatm12()Returns the 2nd-row, 3rd-column matrix entry.floatm13()Returns the 2nd-row, 4th-column matrix entry.floatm20()Returns the 3rd-row, 1st-column matrix entry.floatm21()Returns the 3rd-row, 2nd-column matrix entry.floatm22()Returns the 3rd-row, 3rd-column matrix entry.floatm23()Returns the 3rd-row, 4th-column matrix entry.floatm30()Returns the 4th-row, 1st-column matrix entry.floatm31()Returns the 4th-row, 2nd-column matrix entry.floatm32()Returns the 4th-row, 3rd-column matrix entry.floatm33()Returns the 4th-row, 4th-column matrix entry.booleanmatches(Matrix matrix)Returns whether or not this matrix matches other.float[]multiply(float[] source, float[] target)Multiply a three or four element vectorsourceagainst this matrix and store the result intarget.static Matrixmultiply(Matrix a, Matrix b)Returnsa x b.static voidmultiply(Matrix a, Matrix b, Matrix c)Definecasa x b.Vectormultiply(Vector source)Same asreturn multiply(source, null).Vectormultiply(Vector source, Vector target)Multiply this matrix by thesourcevector and stores the result in thetargetvector which is then returned.static Matrixorthographic(float width, float height, float zNear, float zFar)Returns an orthographic projection matrix from the given parameters.static Matrixperspective(float magnitude, float aspectRatio, float zNear, float zFar)Returns a perspective projection matrix from the given parameters.voidreset()Sets the identity matrix.voidrotate(float angle)Same as_rotateZ(angle).voidrotate(float angle, float v0, float v1, float v2)Multiply this matrix by the rotation matrix defined from the(v0,v1,v2)andangle.voidrotateX(float angle)Multiply this matrix by the rotation around x-axis matrix defined fromangle.voidrotateY(float angle)Multiply this matrix by the rotation around y-axis matrix defined fromangle.voidrotateZ(float angle)Multiply this matrix by the rotation around z-axis matrix defined fromangle.voidscale(float s)Multiply this matrix by the scaling matrix defined froms.voidscale(float sx, float sy)Multiply this matrix by the scaling matrix defined fromsxandsy.voidscale(float x, float y, float z)Multiply this matrix by the scaling matrix defined fromx,yandz.voidset(float[] source)Same asset(source, true).voidset(float[] source, boolean columnMajorOrder)Sets the matrix contents from the 16 entry floatsourcearray.voidset(float m0, float m1, float m2, float m3, float m4, float m5, float m6, float m7, float m8, float m9, float m10, float m11, float m12, float m13, float m14, float m15)voidset(float m0, float m1, float m2, float m3, float m4, float m5, float m6, float m7, float m8, float m9, float m10, float m11, float m12, float m13, float m14, float m15, boolean columnMajorOrder)Sets the column-major matrix contents from the given 16 consecutive values.voidset(Matrix matrix)Sets the matrix contents from thematrixcontents.voidsetM00(float value)Sets the 1st-row, 1st-column matrix entry.voidsetM01(float value)Sets the 1st-row, 2nd-column matrix entry.voidsetM02(float value)Sets the 1st-row, 3rd-column matrix entry.voidsetM03(float value)Sets the 1st-row, 4th-column matrix entry.voidsetM10(float value)Sets the 2nd-row, 1st-column matrix entry.voidsetM11(float value)Sets the 2nd-row, 2nd-column matrix entry.voidsetM12(float value)Sets the 2nd-row, 3rd-column matrix entry.voidsetM13(float value)Sets the 2nd-row, 4th-column matrix entry.voidsetM20(float value)Sets the 3rd-row, 1st-column matrix entry.voidsetM21(float value)Sets the 3rd-row, 2nd-column matrix entry.voidsetM22(float value)Sets the 3rd-row, 3rd-column matrix entry.voidsetM23(float value)Sets the 3rd-row, 4th-column matrix entry.voidsetM30(float value)Sets the 4th-row, 1st-column matrix entry.voidsetM31(float value)Sets the 4th-row, 2nd-column matrix entry.voidsetM32(float value)Sets the 4th-row, 3rd-column matrix entry.voidsetM33(float value)Sets the 4th-row, 4th-column matrix entry.voidshearX(float angle)Multiply this matrix by the x-shearing matrix defined fromangle.voidshearY(float angle)Multiply this matrix by the y-shearing matrix defined fromangle.StringtoString()Return this matrix components as a String.voidtranslate(float tx, float ty)Same astranslate(tx, ty, 0.voidtranslate(float tx, float ty, float tz)Multiply this matrix by the translation matrix defined fromtx,tyandtz.voidtranspose()Transpose this matrix.static Matrixview(Vector position, Quaternion orientation)Returns the inverse of the matrix associated with the eyepositionandorientation.
-
-
-
Constructor Detail
-
Matrix
public Matrix()
Constructor for an identity matrix.
-
Matrix
public Matrix(float m0, float m1, float m2, float m3, float m4, float m5, float m6, float m7, float m8, float m9, float m10, float m11, float m12, float m13, float m14, float m15)Same asthis(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, true).
-
Matrix
public Matrix(float m0, float m1, float m2, float m3, float m4, float m5, float m6, float m7, float m8, float m9, float m10, float m11, float m12, float m13, float m14, float m15, boolean columnMajorOrder)Same asset(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15, columnMajorOrder).
-
Matrix
public Matrix(float[] source)
Same asthis(data, true).- See Also:
Matrix(float[], boolean)
-
Matrix
public Matrix(float[] source, boolean columnMajorOrder)Sets the matrix contents from the 16 entrysourcefloat array given incolumnMajorOrderor row major order (ifcolumnMajorOrderisfalse).
-
-
Method Detail
-
matches
public boolean matches(Matrix matrix)
Returns whether or not this matrix matches other.- Parameters:
matrix- other matrix
-
setM00
public void setM00(float value)
Sets the 1st-row, 1st-column matrix entry.
-
setM01
public void setM01(float value)
Sets the 1st-row, 2nd-column matrix entry.
-
setM02
public void setM02(float value)
Sets the 1st-row, 3rd-column matrix entry.
-
setM03
public void setM03(float value)
Sets the 1st-row, 4th-column matrix entry.
-
setM10
public void setM10(float value)
Sets the 2nd-row, 1st-column matrix entry.
-
setM11
public void setM11(float value)
Sets the 2nd-row, 2nd-column matrix entry.
-
setM12
public void setM12(float value)
Sets the 2nd-row, 3rd-column matrix entry.
-
setM13
public void setM13(float value)
Sets the 2nd-row, 4th-column matrix entry.
-
setM20
public void setM20(float value)
Sets the 3rd-row, 1st-column matrix entry.
-
setM21
public void setM21(float value)
Sets the 3rd-row, 2nd-column matrix entry.
-
setM22
public void setM22(float value)
Sets the 3rd-row, 3rd-column matrix entry.
-
setM23
public void setM23(float value)
Sets the 3rd-row, 4th-column matrix entry.
-
setM30
public void setM30(float value)
Sets the 4th-row, 1st-column matrix entry.
-
setM31
public void setM31(float value)
Sets the 4th-row, 2nd-column matrix entry.
-
setM32
public void setM32(float value)
Sets the 4th-row, 3rd-column matrix entry.
-
setM33
public void setM33(float value)
Sets the 4th-row, 4th-column matrix entry.
-
m00
public float m00()
Sets the 1st-row, 1st-column matrix entry.
-
m01
public float m01()
Returns the 1st-row, 2nd-column matrix entry.
-
m02
public float m02()
Returns the 1st-row, 3rd-column matrix entry.
-
m03
public float m03()
Returns the 1st-row, 4th-column matrix entry.
-
m10
public float m10()
Returns the 2nd-row, 1st-column matrix entry.
-
m11
public float m11()
Returns the 2nd-row, 2nd-column matrix entry.
-
m12
public float m12()
Returns the 2nd-row, 3rd-column matrix entry.
-
m13
public float m13()
Returns the 2nd-row, 4th-column matrix entry.
-
m20
public float m20()
Returns the 3rd-row, 1st-column matrix entry.
-
m21
public float m21()
Returns the 3rd-row, 2nd-column matrix entry.
-
m22
public float m22()
Returns the 3rd-row, 3rd-column matrix entry.
-
m23
public float m23()
Returns the 3rd-row, 4th-column matrix entry.
-
m30
public float m30()
Returns the 4th-row, 1st-column matrix entry.
-
m31
public float m31()
Returns the 4th-row, 2nd-column matrix entry.
-
m32
public float m32()
Returns the 4th-row, 3rd-column matrix entry.
-
m33
public float m33()
Returns the 4th-row, 4th-column matrix entry.
-
reset
public void reset()
Sets the identity matrix.
-
copy
public Matrix copy()
Returns a copy of this matrix.
-
get
public float[] get(float[] target)
Same asreturn get(target, true).- See Also:
get(float[], boolean)
-
get
public float[] get(float[] target, boolean columnMajorOrder)Copies the matrix contents into a 16 entrytargetfloat array. If target is null (or not the correct size), a new array will be created. Column or row major order is defined by thecolumnMajorOrderboolean parameter.
-
set
public void set(Matrix matrix)
Sets the matrix contents from thematrixcontents.
-
set
public void set(float[] source)
Same asset(source, true).- See Also:
set(float[], boolean)
-
set
public void set(float[] source, boolean columnMajorOrder)Sets the matrix contents from the 16 entry floatsourcearray. Column or row major order is defined by thecolumnMajorOrderboolean parameter.
-
set
public void set(float m0, float m1, float m2, float m3, float m4, float m5, float m6, float m7, float m8, float m9, float m10, float m11, float m12, float m13, float m14, float m15)
-
set
public void set(float m0, float m1, float m2, float m3, float m4, float m5, float m6, float m7, float m8, float m9, float m10, float m11, float m12, float m13, float m14, float m15, boolean columnMajorOrder)Sets the column-major matrix contents from the given 16 consecutive values.
-
translate
public void translate(float tx, float ty)Same astranslate(tx, ty, 0.
-
translate
public void translate(float tx, float ty, float tz)Multiply this matrix by the translation matrix defined fromtx,tyandtz.
-
rotate
public void rotate(float angle)
Same as_rotateZ(angle).- See Also:
rotateZ(float)
-
rotateX
public void rotateX(float angle)
Multiply this matrix by the rotation around x-axis matrix defined fromangle.
-
rotateY
public void rotateY(float angle)
Multiply this matrix by the rotation around y-axis matrix defined fromangle.
-
rotateZ
public void rotateZ(float angle)
Multiply this matrix by the rotation around z-axis matrix defined fromangle.
-
rotate
public void rotate(float angle, float v0, float v1, float v2)Multiply this matrix by the rotation matrix defined from the(v0,v1,v2)andangle.
-
scale
public void scale(float s)
Multiply this matrix by the scaling matrix defined froms.
-
scale
public void scale(float sx, float sy)Multiply this matrix by the scaling matrix defined fromsxandsy.
-
scale
public void scale(float x, float y, float z)Multiply this matrix by the scaling matrix defined fromx,yandz.
-
shearX
public void shearX(float angle)
Multiply this matrix by the x-shearing matrix defined fromangle.
-
shearY
public void shearY(float angle)
Multiply this matrix by the y-shearing matrix defined fromangle.
-
apply
public void apply(Matrix matrix)
Multiply this matrix bymatrix.
-
multiply
public Vector multiply(Vector source)
Same asreturn multiply(source, null).- See Also:
multiply(Vector, Vector)
-
multiply
public Vector multiply(Vector source, Vector target)
Multiply this matrix by thesourcevector and stores the result in thetargetvector which is then returned.
-
multiply
public float[] multiply(float[] source, float[] target)Multiply a three or four element vectorsourceagainst this matrix and store the result intarget. Iftargetis null or not length 3 or 4, a new float array (length 3) will be returned.
-
transpose
public void transpose()
Transpose this matrix.
-
invert
public boolean invert(Matrix matrix)
Invert this matrix intomatrix, i.e., doesn't modify this matrix.matrixshould be non-null.
-
inverse
public static Matrix inverse(Matrix matrix)
Returns the inverse ofmatrix. Throws a runtime exception ifmatrixisn't invertible.
-
invert
public boolean invert()
Invert this matrix.- Returns:
- true if successful
-
determinant
public float determinant()
- Returns:
- the determinant of the matrix
-
toString
public String toString()
Return this matrix components as a String.
-
perspective
public static Matrix perspective(float magnitude, float aspectRatio, float zNear, float zFar)
Returns a perspective projection matrix from the given parameters.Compute the
magnitudeastan(field-of-view / 2)if you want to set the matrix from the field-of-view.All parameter values should be positive, but
magnitudewhich may be negative in case you want to invert the projected image across the eye y-axis.
-
orthographic
public static Matrix orthographic(float width, float height, float zNear, float zFar)
Returns an orthographic projection matrix from the given parameters.All parameter values should be positive, but
heightwhich may be negative in case you want to invert the projected image across the eye y-axis.
-
view
public static Matrix view(Vector position, Quaternion orientation)
Returns the inverse of the matrix associated with the eyepositionandorientation.The view matrix converts from the world coordinates system to the eye coordinates system, so that coordinates can then be projected on screen using a projection matrix.
-
hudProjection
public static Matrix hudProjection(int width, int height)
Returns a hud (heads-up-display) projection matrix according to the windowwidthandheightdimensions. It should be used in conjunction withhudView(int, int)which properly positions the (hud) eye. See 9.030 How do I draw 2D controls over my 3D rendering?
-
hudView
public static Matrix hudView(int width, int height)
Returns a hud (heads-up-display) view according to the windowwidthandheightdimensions. It should be used in conjunction withhudProjection(int, int)which properly projects the scene to fit the window size. See 9.030 How do I draw 2D controls over my 3D rendering?
-
-