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 entrysource
float array given incolumnMajorOrder
or row major order (ifcolumnMajorOrder
isfalse
).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 void
apply(Matrix matrix)
Multiply this matrix bymatrix
.Matrix
copy()
Returns a copy of this matrix.float
determinant()
float[]
get(float[] target)
Same asreturn get(target, true)
.float[]
get(float[] target, boolean columnMajorOrder)
Copies the matrix contents into a 16 entrytarget
float array.static Matrix
hudProjection(int width, int height)
Returns a hud (heads-up-display) projection matrix according to the windowwidth
andheight
dimensions.static Matrix
hudView(int width, int height)
Returns a hud (heads-up-display) view according to the windowwidth
andheight
dimensions.static Matrix
inverse(Matrix matrix)
Returns the inverse ofmatrix
.boolean
invert()
Invert this matrix.boolean
invert(Matrix matrix)
Invert this matrix intomatrix
, i.e., doesn't modify this matrix.float
m00()
Sets the 1st-row, 1st-column matrix entry.float
m01()
Returns the 1st-row, 2nd-column matrix entry.float
m02()
Returns the 1st-row, 3rd-column matrix entry.float
m03()
Returns the 1st-row, 4th-column matrix entry.float
m10()
Returns the 2nd-row, 1st-column matrix entry.float
m11()
Returns the 2nd-row, 2nd-column matrix entry.float
m12()
Returns the 2nd-row, 3rd-column matrix entry.float
m13()
Returns the 2nd-row, 4th-column matrix entry.float
m20()
Returns the 3rd-row, 1st-column matrix entry.float
m21()
Returns the 3rd-row, 2nd-column matrix entry.float
m22()
Returns the 3rd-row, 3rd-column matrix entry.float
m23()
Returns the 3rd-row, 4th-column matrix entry.float
m30()
Returns the 4th-row, 1st-column matrix entry.float
m31()
Returns the 4th-row, 2nd-column matrix entry.float
m32()
Returns the 4th-row, 3rd-column matrix entry.float
m33()
Returns the 4th-row, 4th-column matrix entry.boolean
matches(Matrix matrix)
Returns whether or not this matrix matches other.float[]
multiply(float[] source, float[] target)
Multiply a three or four element vectorsource
against this matrix and store the result intarget
.static Matrix
multiply(Matrix a, Matrix b)
Returnsa x b
.static void
multiply(Matrix a, Matrix b, Matrix c)
Definec
asa x b
.Vector
multiply(Vector source)
Same asreturn multiply(source, null)
.Vector
multiply(Vector source, Vector target)
Multiply this matrix by thesource
vector and stores the result in thetarget
vector which is then returned.static Matrix
orthographic(float width, float height, float zNear, float zFar)
Returns an orthographic projection matrix from the given parameters.static Matrix
perspective(float magnitude, float aspectRatio, float zNear, float zFar)
Returns a perspective projection matrix from the given parameters.void
reset()
Sets the identity matrix.void
rotate(float angle)
Same as_rotateZ(angle)
.void
rotate(float angle, float v0, float v1, float v2)
Multiply this matrix by the rotation matrix defined from the(v0,v1,v2)
andangle
.void
rotateX(float angle)
Multiply this matrix by the rotation around x-axis matrix defined fromangle
.void
rotateY(float angle)
Multiply this matrix by the rotation around y-axis matrix defined fromangle
.void
rotateZ(float angle)
Multiply this matrix by the rotation around z-axis matrix defined fromangle
.void
scale(float s)
Multiply this matrix by the scaling matrix defined froms
.void
scale(float sx, float sy)
Multiply this matrix by the scaling matrix defined fromsx
andsy
.void
scale(float x, float y, float z)
Multiply this matrix by the scaling matrix defined fromx
,y
andz
.void
set(float[] source)
Same asset(source, true)
.void
set(float[] source, boolean columnMajorOrder)
Sets the matrix contents from the 16 entry floatsource
array.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)
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.void
set(Matrix matrix)
Sets the matrix contents from thematrix
contents.void
setM00(float value)
Sets the 1st-row, 1st-column matrix entry.void
setM01(float value)
Sets the 1st-row, 2nd-column matrix entry.void
setM02(float value)
Sets the 1st-row, 3rd-column matrix entry.void
setM03(float value)
Sets the 1st-row, 4th-column matrix entry.void
setM10(float value)
Sets the 2nd-row, 1st-column matrix entry.void
setM11(float value)
Sets the 2nd-row, 2nd-column matrix entry.void
setM12(float value)
Sets the 2nd-row, 3rd-column matrix entry.void
setM13(float value)
Sets the 2nd-row, 4th-column matrix entry.void
setM20(float value)
Sets the 3rd-row, 1st-column matrix entry.void
setM21(float value)
Sets the 3rd-row, 2nd-column matrix entry.void
setM22(float value)
Sets the 3rd-row, 3rd-column matrix entry.void
setM23(float value)
Sets the 3rd-row, 4th-column matrix entry.void
setM30(float value)
Sets the 4th-row, 1st-column matrix entry.void
setM31(float value)
Sets the 4th-row, 2nd-column matrix entry.void
setM32(float value)
Sets the 4th-row, 3rd-column matrix entry.void
setM33(float value)
Sets the 4th-row, 4th-column matrix entry.void
shearX(float angle)
Multiply this matrix by the x-shearing matrix defined fromangle
.void
shearY(float angle)
Multiply this matrix by the y-shearing matrix defined fromangle
.String
toString()
Return this matrix components as a String.void
translate(float tx, float ty)
Same astranslate(tx, ty, 0
.void
translate(float tx, float ty, float tz)
Multiply this matrix by the translation matrix defined fromtx
,ty
andtz
.void
transpose()
Transpose this matrix.static Matrix
view(Vector position, Quaternion orientation)
Returns the inverse of the matrix associated with the eyeposition
andorientation
.
-
-
-
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 entrysource
float array given incolumnMajorOrder
or row major order (ifcolumnMajorOrder
isfalse
).
-
-
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 entrytarget
float array. If target is null (or not the correct size), a new array will be created. Column or row major order is defined by thecolumnMajorOrder
boolean parameter.
-
set
public void set(Matrix matrix)
Sets the matrix contents from thematrix
contents.
-
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 floatsource
array. Column or row major order is defined by thecolumnMajorOrder
boolean 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
,ty
andtz
.
-
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 fromsx
andsy
.
-
scale
public void scale(float x, float y, float z)
Multiply this matrix by the scaling matrix defined fromx
,y
andz
.
-
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 thesource
vector and stores the result in thetarget
vector which is then returned.
-
multiply
public float[] multiply(float[] source, float[] target)
Multiply a three or four element vectorsource
against this matrix and store the result intarget
. Iftarget
is 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.matrix
should be non-null.
-
inverse
public static Matrix inverse(Matrix matrix)
Returns the inverse ofmatrix
. Throws a runtime exception ifmatrix
isn'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
magnitude
astan(field-of-view / 2)
if you want to set the matrix from the field-of-view.All parameter values should be positive, but
magnitude
which 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
height
which 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 eyeposition
andorientation
.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 windowwidth
andheight
dimensions. 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 windowwidth
andheight
dimensions. 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?
-
-