Class Matrix
- All Implemented Interfaces:
Proxy
The contents of the graphene_matrix_t
structure are private and
should never be accessed directly.
-
Constructor Summary
ConstructorDescriptionMatrix()
Allocate a new Matrix.Allocate a new Matrix.Matrix
(MemorySegment address) Create a Matrix proxy instance for the provided memory address.Allocate a new Matrix with the fields set to the provided values.Allocate a new Matrix with the fields set to the provided values. -
Method Summary
Modifier and TypeMethodDescriptionstatic Matrix
alloc()
Allocates a newgraphene_matrix_t
.boolean
Decomposes a transformation matrix into its component transformations.float
Computes the determinant of the given matrix.boolean
Checks whether the two givengraphene_matrix_t
matrices are equal.boolean
Checks whether the two givengraphene_matrix_t
matrices are byte-by-byte equal.void
free()
Frees the resources allocated by graphene_matrix_alloc().static MemoryLayout
The memory layout of the native struct.void
Retrieves the given row vector atindex
inside a matrix.static Type
getType()
Get the GType of the Matrix classfloat
getValue
(int row, int col) Retrieves the value at the givenrow
andcol
index.float
Retrieves the scaling factor on the X axis in this Matrix.float
Retrieves the translation component on the X axis from this Matrix.float
Retrieves the scaling factor on the Y axis in this Matrix.float
Retrieves the translation component on the Y axis from this Matrix.float
Retrieves the scaling factor on the Z axis in this Matrix.float
Retrieves the translation component on the Z axis from this Matrix.initFrom2d
(double xx, double yx, double xy, double yy, double x0, double y0) Initializes agraphene_matrix_t
from the values of an affine transformation matrix.initFromFloat
(float[] v) Initializes agraphene_matrix_t
with the given array of floating point values.initFromMatrix
(Matrix src) Initializes agraphene_matrix_t
using the values of the given matrix.initFromVec4
(Vec4 v0, Vec4 v1, Vec4 v2, Vec4 v3) Initializes agraphene_matrix_t
with the given four row vectors.initFrustum
(float left, float right, float bottom, float top, float zNear, float zFar) Initializes agraphene_matrix_t
compatible withgraphene_frustum_t
.Initializes agraphene_matrix_t
with the identity matrix.initLookAt
(Vec3 eye, Vec3 center, Vec3 up) Initializes agraphene_matrix_t
so that it positions the "camera" at the giveneye
coordinates towards an object at thecenter
coordinates.initOrtho
(float left, float right, float top, float bottom, float zNear, float zFar) Initializes agraphene_matrix_t
with an orthographic projection.initPerspective
(float fovy, float aspect, float zNear, float zFar) Initializes agraphene_matrix_t
with a perspective projection.initRotate
(float angle, Vec3 axis) Initializes this Matrix to represent a rotation ofangle
degrees on the axis represented by theaxis
vector.initScale
(float x, float y, float z) Initializes agraphene_matrix_t
with the given scaling factors.initSkew
(float xSkew, float ySkew) Initializes agraphene_matrix_t
with a skew transformation with the given factors.Initializes agraphene_matrix_t
with a translation to the given coordinates.void
interpolate
(Matrix b, double factor, Matrix res) Linearly interpolates the two givengraphene_matrix_t
by interpolating the decomposed transformations separately.boolean
Inverts the given matrix.boolean
is2d()
Checks whether the givengraphene_matrix_t
is compatible with an a 2D affine transformation matrix.boolean
Checks whether agraphene_matrix_t
has a visible back face.boolean
Checks whether the givengraphene_matrix_t
is the identity matrix.boolean
Checks whether a matrix is singular.void
Multiplies twographene_matrix_t
.boolean
Compares the two givengraphene_matrix_t
matrices and checks whether their values are within the givenepsilon
of each other.void
Normalizes the givengraphene_matrix_t
.void
perspective
(float depth, Matrix res) Applies a perspective ofdepth
to the matrix.void
print()
Prints the contents of a matrix to the standard error stream.void
projectPoint
(Point p, Point res) Projects agraphene_point_t
using the matrix this Matrix.void
projectRect
(Rect r, Quad res) Projects all corners of agraphene_rect_t
using the given matrix.void
projectRectBounds
(Rect r, Rect res) Projects agraphene_rect_t
using the given matrix.Read the value of the fieldvalue
.void
Adds a rotation transformation to this Matrix, using the givenangle
andaxis
vector.void
rotateEuler
(Euler e) Adds a rotation transformation to this Matrix, using the givengraphene_euler_t
.void
Adds a rotation transformation to this Matrix, using the givengraphene_quaternion_t
.void
rotateX
(float angle) Adds a rotation transformation around the X axis to this Matrix, using the givenangle
.void
rotateY
(float angle) Adds a rotation transformation around the Y axis to this Matrix, using the givenangle
.void
rotateZ
(float angle) Adds a rotation transformation around the Z axis to this Matrix, using the givenangle
.void
scale
(float factorX, float factorY, float factorZ) Adds a scaling transformation to this Matrix, using the three given factors.void
skewXy
(float factor) Adds a skew offactor
on the X and Y axis to the given matrix.void
skewXz
(float factor) Adds a skew offactor
on the X and Z axis to the given matrix.void
skewYz
(float factor) Adds a skew offactor
on the Y and Z axis to the given matrix.boolean
to2d
(Out<Double> xx, Out<Double> yx, Out<Double> xy, Out<Double> yy, Out<Double> x0, Out<Double> y0) Converts agraphene_matrix_t
to an affine transformation matrix, if the given matrix is compatible.void
toFloat
(float[] v) Converts agraphene_matrix_t
to an array of floating point values.void
transformBounds
(Rect r, Rect res) Transforms each corner of agraphene_rect_t
using the given matrix this Matrix.void
transformBox
(Box b, Box res) Transforms the vertices of agraphene_box_t
using the given matrix this Matrix.void
transformPoint
(Point p, Point res) Transforms the givengraphene_point_t
using the matrix this Matrix.void
transformPoint3d
(Point3D p, Point3D res) Transforms the givengraphene_point3d_t
using the matrix this Matrix.void
transformRay
(Ray r, Ray res) Transform agraphene_ray_t
using the given matrix this Matrix.void
transformRect
(Rect r, Quad res) Transforms each corner of agraphene_rect_t
using the given matrix this Matrix.void
transformSphere
(Sphere s, Sphere res) Transforms agraphene_sphere_t
using the given matrix this Matrix.void
transformVec3
(Vec3 v, Vec3 res) Transforms the givengraphene_vec3_t
using the matrix this Matrix.void
transformVec4
(Vec4 v, Vec4 res) Transforms the givengraphene_vec4_t
using the matrix this Matrix.void
Adds a translation transformation to this Matrix using the coordinates of the givengraphene_point3d_t
.void
Transposes the given matrix.void
unprojectPoint3d
(Matrix modelview, Point3D point, Point3D res) Unprojects the givenpoint
using the this Matrix matrix and amodelview
matrix.void
untransformBounds
(Rect r, Rect bounds, Rect res) Undoes the transformation on the corners of agraphene_rect_t
using the given matrix, within the given axis aligned rectangularbounds
.boolean
untransformPoint
(Point p, Rect bounds, Point res) Undoes the transformation of agraphene_point_t
using the given matrix, within the given axis aligned rectangularbounds
.void
writeValue
(Simd4X4F value) Write a value in the fieldvalue
.Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
-
Constructor Details
-
Matrix
Create a Matrix proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
Matrix
Allocate a new Matrix.- Parameters:
arena
- to control the memory allocation scope
-
Matrix
public Matrix()Allocate a new Matrix. The memory is allocated withArena.ofAuto()
. -
Matrix
-
Matrix
Allocate a new Matrix with the fields set to the provided values. The memory is allocated withArena.ofAuto()
.- Parameters:
value
- value for the fieldvalue
-
-
Method Details
-
getType
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
readValue
Read the value of the fieldvalue
.- Returns:
- The value of the field
value
-
writeValue
Write a value in the fieldvalue
.- Parameters:
value
- The new value for the fieldvalue
-
alloc
-
decompose
public boolean decompose(Vec3 translate, Vec3 scale, Quaternion rotate, Vec3 shear, Vec4 perspective) Decomposes a transformation matrix into its component transformations.The algorithm for decomposing a matrix is taken from the CSS3 Transforms specification; specifically, the decomposition code is based on the equivalent code published in "Graphics Gems II", edited by Jim Arvo, and available online.
- Parameters:
translate
- the translation vectorscale
- the scale vectorrotate
- the rotation quaternionshear
- the shear vectorperspective
- the perspective vector- Returns:
true
if the matrix could be decomposed
-
determinant
public float determinant()Computes the determinant of the given matrix.- Returns:
- the value of the determinant
-
equal
Checks whether the two givengraphene_matrix_t
matrices are equal.- Parameters:
b
- agraphene_matrix_t
- Returns:
true
if the two matrices are equal, andfalse
otherwise
-
equalFast
Checks whether the two givengraphene_matrix_t
matrices are byte-by-byte equal.While this function is faster than graphene_matrix_equal(), it can also return false negatives, so it should be used in conjuction with either graphene_matrix_equal() or graphene_matrix_near(). For instance:
if (graphene_matrix_equal_fast (a, b)) { // matrices are definitely the same } else { if (graphene_matrix_equal (a, b)) // matrices contain the same values within an epsilon of FLT_EPSILON else if (graphene_matrix_near (a, b, 0.0001)) // matrices contain the same values within an epsilon of 0.0001 else // matrices are not equal }
- Parameters:
b
- agraphene_matrix_t
- Returns:
true
if the matrices are equal. andfalse
otherwise
-
free
public void free()Frees the resources allocated by graphene_matrix_alloc(). -
getRow
Retrieves the given row vector atindex
inside a matrix.- Parameters:
index
- the index of the row vector, between 0 and 3res
- return location for thegraphene_vec4_t
that is used to store the row vector
-
getValue
public float getValue(int row, int col) Retrieves the value at the givenrow
andcol
index.- Parameters:
row
- the row indexcol
- the column index- Returns:
- the value at the given indices
-
getXScale
public float getXScale()Retrieves the scaling factor on the X axis in this Matrix.- Returns:
- the value of the scaling factor
-
getXTranslation
public float getXTranslation()Retrieves the translation component on the X axis from this Matrix.- Returns:
- the translation component
-
getYScale
public float getYScale()Retrieves the scaling factor on the Y axis in this Matrix.- Returns:
- the value of the scaling factor
-
getYTranslation
public float getYTranslation()Retrieves the translation component on the Y axis from this Matrix.- Returns:
- the translation component
-
getZScale
public float getZScale()Retrieves the scaling factor on the Z axis in this Matrix.- Returns:
- the value of the scaling factor
-
getZTranslation
public float getZTranslation()Retrieves the translation component on the Z axis from this Matrix.- Returns:
- the translation component
-
initFrom2d
Initializes agraphene_matrix_t
from the values of an affine transformation matrix.The arguments map to the following matrix layout:
⎛ xx yx ⎞ ⎛ a b 0 ⎞ ⎜ xy yy ⎟ = ⎜ c d 0 ⎟ ⎝ x0 y0 ⎠ ⎝ tx ty 1 ⎠
This function can be used to convert between an affine matrix type from other libraries and a
graphene_matrix_t
.- Parameters:
xx
- the xx memberyx
- the yx memberxy
- the xy memberyy
- the yy memberx0
- the x0 membery0
- the y0 member- Returns:
- the initialized matrix
-
initFromFloat
Initializes agraphene_matrix_t
with the given array of floating point values.- Parameters:
v
- an array of at least 16 floating point values- Returns:
- the initialized matrix
-
initFromMatrix
-
initFromVec4
-
initFrustum
public Matrix initFrustum(float left, float right, float bottom, float top, float zNear, float zFar) Initializes agraphene_matrix_t
compatible withgraphene_frustum_t
.See also: graphene_frustum_init_from_matrix()
- Parameters:
left
- distance of the left clipping planeright
- distance of the right clipping planebottom
- distance of the bottom clipping planetop
- distance of the top clipping planezNear
- distance of the near clipping planezFar
- distance of the far clipping plane- Returns:
- the initialized matrix
-
initIdentity
Initializes agraphene_matrix_t
with the identity matrix.- Returns:
- the initialized matrix
-
initLookAt
Initializes agraphene_matrix_t
so that it positions the "camera" at the giveneye
coordinates towards an object at thecenter
coordinates. The top of the camera is aligned to the direction of theup
vector.Before the transform, the camera is assumed to be placed at the origin, looking towards the negative Z axis, with the top side of the camera facing in the direction of the Y axis and the right side in the direction of the X axis.
In theory, one could use this Matrix to transform a model of such a camera into world-space. However, it is more common to use the inverse of this Matrix to transform another object from world coordinates to the view coordinates of the camera. Typically you would then apply the camera projection transform to get from view to screen coordinates.
- Parameters:
eye
- the vector describing the position to look fromcenter
- the vector describing the position to look atup
- the vector describing the world's upward direction; usually, this is the graphene_vec3_y_axis() vector- Returns:
- the initialized matrix
-
initOrtho
Initializes agraphene_matrix_t
with an orthographic projection.- Parameters:
left
- the left edge of the clipping planeright
- the right edge of the clipping planetop
- the top edge of the clipping planebottom
- the bottom edge of the clipping planezNear
- the distance of the near clipping planezFar
- the distance of the far clipping plane- Returns:
- the initialized matrix
-
initPerspective
Initializes agraphene_matrix_t
with a perspective projection.- Parameters:
fovy
- the field of view angle, in degreesaspect
- the aspect valuezNear
- the near Z planezFar
- the far Z plane- Returns:
- the initialized matrix
-
initRotate
-
initScale
Initializes agraphene_matrix_t
with the given scaling factors.- Parameters:
x
- the scale factor on the X axisy
- the scale factor on the Y axisz
- the scale factor on the Z axis- Returns:
- the initialized matrix
-
initSkew
Initializes agraphene_matrix_t
with a skew transformation with the given factors.- Parameters:
xSkew
- skew factor, in radians, on the X axisySkew
- skew factor, in radians, on the Y axis- Returns:
- the initialized matrix
-
initTranslate
-
interpolate
Linearly interpolates the two givengraphene_matrix_t
by interpolating the decomposed transformations separately.If either matrix cannot be reduced to their transformations then the interpolation cannot be performed, and this function will return an identity matrix.
- Parameters:
b
- agraphene_matrix_t
factor
- the linear interpolation factorres
- return location for the interpolated matrix
-
inverse
Inverts the given matrix.- Parameters:
res
- return location for the inverse matrix- Returns:
true
if the matrix is invertible
-
is2d
public boolean is2d()Checks whether the givengraphene_matrix_t
is compatible with an a 2D affine transformation matrix.- Returns:
true
if the matrix is compatible with an affine transformation matrix
-
isBackfaceVisible
public boolean isBackfaceVisible()Checks whether agraphene_matrix_t
has a visible back face.- Returns:
true
if the back face of the matrix is visible
-
isIdentity
public boolean isIdentity()Checks whether the givengraphene_matrix_t
is the identity matrix.- Returns:
true
if the matrix is the identity matrix
-
isSingular
public boolean isSingular()Checks whether a matrix is singular.- Returns:
true
if the matrix is singular
-
multiply
-
near
Compares the two givengraphene_matrix_t
matrices and checks whether their values are within the givenepsilon
of each other.- Parameters:
b
- agraphene_matrix_t
epsilon
- the threshold between the two matrices- Returns:
true
if the two matrices are near each other, andfalse
otherwise
-
normalize
Normalizes the givengraphene_matrix_t
.- Parameters:
res
- return location for the normalized matrix
-
perspective
Applies a perspective ofdepth
to the matrix.- Parameters:
depth
- the depth of the perspectiveres
- return location for the perspective matrix
-
print
public void print()Prints the contents of a matrix to the standard error stream.This function is only useful for debugging; there are no guarantees made on the format of the output.
-
projectPoint
-
projectRect
-
projectRectBounds
-
rotate
Adds a rotation transformation to this Matrix, using the givenangle
andaxis
vector.This is the equivalent of calling graphene_matrix_init_rotate() and then multiplying the matrix this Matrix with the rotation matrix.
- Parameters:
angle
- the rotation angle, in degreesaxis
- the rotation axis, as agraphene_vec3_t
-
rotateEuler
Adds a rotation transformation to this Matrix, using the givengraphene_euler_t
.- Parameters:
e
- a rotation described by agraphene_euler_t
-
rotateQuaternion
Adds a rotation transformation to this Matrix, using the givengraphene_quaternion_t
.This is the equivalent of calling graphene_quaternion_to_matrix() and then multiplying this Matrix with the rotation matrix.
- Parameters:
q
- a rotation described by agraphene_quaternion_t
-
rotateX
public void rotateX(float angle) Adds a rotation transformation around the X axis to this Matrix, using the givenangle
.See also: graphene_matrix_rotate()
- Parameters:
angle
- the rotation angle, in degrees
-
rotateY
public void rotateY(float angle) Adds a rotation transformation around the Y axis to this Matrix, using the givenangle
.See also: graphene_matrix_rotate()
- Parameters:
angle
- the rotation angle, in degrees
-
rotateZ
public void rotateZ(float angle) Adds a rotation transformation around the Z axis to this Matrix, using the givenangle
.See also: graphene_matrix_rotate()
- Parameters:
angle
- the rotation angle, in degrees
-
scale
public void scale(float factorX, float factorY, float factorZ) Adds a scaling transformation to this Matrix, using the three given factors.This is the equivalent of calling graphene_matrix_init_scale() and then multiplying the matrix this Matrix with the scale matrix.
- Parameters:
factorX
- scaling factor on the X axisfactorY
- scaling factor on the Y axisfactorZ
- scaling factor on the Z axis
-
skewXy
public void skewXy(float factor) Adds a skew offactor
on the X and Y axis to the given matrix.- Parameters:
factor
- skew factor
-
skewXz
public void skewXz(float factor) Adds a skew offactor
on the X and Z axis to the given matrix.- Parameters:
factor
- skew factor
-
skewYz
public void skewYz(float factor) Adds a skew offactor
on the Y and Z axis to the given matrix.- Parameters:
factor
- skew factor
-
to2d
public boolean to2d(Out<Double> xx, Out<Double> yx, Out<Double> xy, Out<Double> yy, Out<Double> x0, Out<Double> y0) Converts agraphene_matrix_t
to an affine transformation matrix, if the given matrix is compatible.The returned values have the following layout:
⎛ xx yx ⎞ ⎛ a b 0 ⎞ ⎜ xy yy ⎟ = ⎜ c d 0 ⎟ ⎝ x0 y0 ⎠ ⎝ tx ty 1 ⎠
This function can be used to convert between a
graphene_matrix_t
and an affine matrix type from other libraries.- Parameters:
xx
- return location for the xx memberyx
- return location for the yx memberxy
- return location for the xy memberyy
- return location for the yy memberx0
- return location for the x0 membery0
- return location for the y0 member- Returns:
true
if the matrix is compatible with an affine transformation matrix
-
toFloat
public void toFloat(float[] v) Converts agraphene_matrix_t
to an array of floating point values.- Parameters:
v
- return location for an array of floating point values. The array must be capable of holding at least 16 values.
-
transformBounds
Transforms each corner of agraphene_rect_t
using the given matrix this Matrix.The result is the axis aligned bounding rectangle containing the coplanar quadrilateral.
See also: graphene_matrix_transform_point()
- Parameters:
r
- agraphene_rect_t
res
- return location for the bounds of the transformed rectangle
-
transformBox
-
transformPoint
Transforms the givengraphene_point_t
using the matrix this Matrix.Unlike graphene_matrix_transform_vec3(), this function will take into account the fourth row vector of the
graphene_matrix_t
when computing the dot product of each row vector of the matrix.See also: graphene_simd4x4f_point3_mul()
- Parameters:
p
- agraphene_point_t
res
- return location for the transformedgraphene_point_t
-
transformPoint3d
Transforms the givengraphene_point3d_t
using the matrix this Matrix.Unlike graphene_matrix_transform_vec3(), this function will take into account the fourth row vector of the
graphene_matrix_t
when computing the dot product of each row vector of the matrix.See also: graphene_simd4x4f_point3_mul()
- Parameters:
p
- agraphene_point3d_t
res
- return location for the result
-
transformRay
-
transformRect
-
transformSphere
-
transformVec3
Transforms the givengraphene_vec3_t
using the matrix this Matrix.This function will multiply the X, Y, and Z row vectors of the matrix this Matrix with the corresponding components of the vector
v
. The W row vector will be ignored.See also: graphene_simd4x4f_vec3_mul()
- Parameters:
v
- agraphene_vec3_t
res
- return location for agraphene_vec3_t
-
transformVec4
-
translate
Adds a translation transformation to this Matrix using the coordinates of the givengraphene_point3d_t
.This is the equivalent of calling graphene_matrix_init_translate() and then multiplying this Matrix with the translation matrix.
- Parameters:
pos
- agraphene_point3d_t
-
transpose
Transposes the given matrix.- Parameters:
res
- return location for the transposed matrix
-
unprojectPoint3d
Unprojects the givenpoint
using the this Matrix matrix and amodelview
matrix.- Parameters:
modelview
- agraphene_matrix_t
for the modelview matrix; this is the inverse of the modelview used when projecting the pointpoint
- agraphene_point3d_t
with the coordinates of the pointres
- return location for the unprojected point
-
untransformBounds
Undoes the transformation on the corners of agraphene_rect_t
using the given matrix, within the given axis aligned rectangularbounds
.- Parameters:
r
- agraphene_rect_t
bounds
- the bounds of the transformationres
- return location for the untransformed rectangle
-
untransformPoint
Undoes the transformation of agraphene_point_t
using the given matrix, within the given axis aligned rectangularbounds
.- Parameters:
p
- agraphene_point_t
bounds
- the bounds of the transformationres
- return location for the untransformed point- Returns:
true
if the point was successfully untransformed
-