Graphene.Matrix¶
Fields¶
Name |
Type |
Access |
Description |
---|---|---|---|
value |
r |
Methods¶
Details¶
- class Graphene.Matrix¶
A structure capable of holding a 4x4 matrix.
The contents of the
Graphene.Matrix
structure are private and should never be accessed directly.- classmethod alloc()¶
- Returns:
the newly allocated matrix
- Return type:
Allocates a new
Graphene.Matrix
.New in version 1.0.
- decompose()¶
- Returns:
true
if the matrix could be decomposed- translate:
the translation vector
- scale:
the scale vector
- rotate:
the rotation quaternion
- shear:
the shear vector
- perspective:
the perspective vector
- Return type:
(
bool
, translate:Graphene.Vec3
, scale:Graphene.Vec3
, rotate:Graphene.Quaternion
, shear:Graphene.Vec3
, perspective:Graphene.Vec4
)
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.
- determinant()¶
- Returns:
the value of the determinant
- Return type:
Computes the determinant of the given matrix.
New in version 1.0.
- equal(b)¶
- Parameters:
b (
Graphene.Matrix
) – aGraphene.Matrix
- Returns:
true
if the two matrices are equal, andfalse
otherwise- Return type:
Checks whether the two given
Graphene.Matrix
matrices are equal.New in version 1.10.
- equal_fast(b)¶
- Parameters:
b (
Graphene.Matrix
) – aGraphene.Matrix
- Returns:
true
if the matrices are equal. andfalse
otherwise- Return type:
Checks whether the two given
Graphene.Matrix
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 eitherGraphene.Matrix.equal
() orGraphene.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 }
New in version 1.10.
- free()¶
Frees the resources allocated by
Graphene.Matrix.alloc
().New in version 1.0.
- get_row(index_)¶
- Parameters:
index (
int
) – the index of the row vector, between 0 and 3- Returns:
return location for the
Graphene.Vec4
that is used to store the row vector- Return type:
res:
Graphene.Vec4
Retrieves the given row vector at index_ inside a matrix.
New in version 1.0.
- get_value(row, col)¶
- Parameters:
- Returns:
the value at the given indices
- Return type:
Retrieves the value at the given row and col index.
New in version 1.0.
- get_x_scale()¶
- Returns:
the value of the scaling factor
- Return type:
Retrieves the scaling factor on the X axis in self.
New in version 1.0.
- get_x_translation()¶
- Returns:
the translation component
- Return type:
Retrieves the translation component on the X axis from self.
New in version 1.10.
- get_y_scale()¶
- Returns:
the value of the scaling factor
- Return type:
Retrieves the scaling factor on the Y axis in self.
New in version 1.0.
- get_y_translation()¶
- Returns:
the translation component
- Return type:
Retrieves the translation component on the Y axis from self.
New in version 1.10.
- get_z_scale()¶
- Returns:
the value of the scaling factor
- Return type:
Retrieves the scaling factor on the Z axis in self.
New in version 1.0.
- get_z_translation()¶
- Returns:
the translation component
- Return type:
Retrieves the translation component on the Z axis from self.
New in version 1.10.
- init_from_2d(xx, yx, xy, yy, x_0, y_0)¶
- Parameters:
- Returns:
the initialized matrix
- Return type:
Initializes a
Graphene.Matrix
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
.New in version 1.0.
- init_from_float(v)¶
- Parameters:
v ([
float
]) – an array of at least 16 floating point values- Returns:
the initialized matrix
- Return type:
Initializes a
Graphene.Matrix
with the given array of floating point values.New in version 1.0.
- init_from_matrix(src)¶
- Parameters:
src (
Graphene.Matrix
) – aGraphene.Matrix
- Returns:
the initialized matrix
- Return type:
Initializes a
Graphene.Matrix
using the values of the given matrix.New in version 1.0.
- init_from_vec4(v0, v1, v2, v3)¶
- Parameters:
v0 (
Graphene.Vec4
) – the first row vectorv1 (
Graphene.Vec4
) – the second row vectorv2 (
Graphene.Vec4
) – the third row vectorv3 (
Graphene.Vec4
) – the fourth row vector
- Returns:
the initialized matrix
- Return type:
Initializes a
Graphene.Matrix
with the given four row vectors.New in version 1.0.
- init_frustum(left, right, bottom, top, z_near, z_far)¶
- Parameters:
left (
float
) – distance of the left clipping planeright (
float
) – distance of the right clipping planebottom (
float
) – distance of the bottom clipping planetop (
float
) – distance of the top clipping planez_near (
float
) – distance of the near clipping planez_far (
float
) – distance of the far clipping plane
- Returns:
the initialized matrix
- Return type:
Initializes a
Graphene.Matrix
compatible withGraphene.Frustum
.See also:
Graphene.Frustum.init_from_matrix
()New in version 1.2.
- init_identity()¶
- Returns:
the initialized matrix
- Return type:
Initializes a
Graphene.Matrix
with the identity matrix.New in version 1.0.
- init_look_at(eye, center, up)¶
- Parameters:
eye (
Graphene.Vec3
) – the vector describing the position to look fromcenter (
Graphene.Vec3
) – the vector describing the position to look atup (
Graphene.Vec3
) – the vector describing the world’s upward direction; usually, this is theGraphene.Vec3.y_axis
() vector
- Returns:
the initialized matrix
- Return type:
Initializes a
Graphene.Matrix
so that it positions the “camera” at the given eye coordinates towards an object at the center coordinates. The top of the camera is aligned to the direction of the up 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 self to transform a model of such a camera into world-space. However, it is more common to use the inverse of self 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.
New in version 1.0.
- init_ortho(left, right, top, bottom, z_near, z_far)¶
- Parameters:
left (
float
) – the left edge of the clipping planeright (
float
) – the right edge of the clipping planetop (
float
) – the top edge of the clipping planebottom (
float
) – the bottom edge of the clipping planez_near (
float
) – the distance of the near clipping planez_far (
float
) – the distance of the far clipping plane
- Returns:
the initialized matrix
- Return type:
Initializes a
Graphene.Matrix
with an orthographic projection.New in version 1.0.
- init_perspective(fovy, aspect, z_near, z_far)¶
- Parameters:
- Returns:
the initialized matrix
- Return type:
Initializes a
Graphene.Matrix
with a perspective projection.New in version 1.0.
- init_rotate(angle, axis)¶
- Parameters:
angle (
float
) – the rotation angle, in degreesaxis (
Graphene.Vec3
) – the axis vector as aGraphene.Vec3
- Returns:
the initialized matrix
- Return type:
Initializes self to represent a rotation of angle degrees on the axis represented by the axis vector.
New in version 1.0.
- init_scale(x, y, z)¶
- Parameters:
- Returns:
the initialized matrix
- Return type:
Initializes a
Graphene.Matrix
with the given scaling factors.New in version 1.0.
- init_skew(x_skew, y_skew)¶
- Parameters:
- Returns:
the initialized matrix
- Return type:
Initializes a
Graphene.Matrix
with a skew transformation with the given factors.New in version 1.0.
- init_translate(p)¶
- Parameters:
p (
Graphene.Point3D
) – the translation coordinates- Returns:
the initialized matrix
- Return type:
Initializes a
Graphene.Matrix
with a translation to the given coordinates.New in version 1.0.
- interpolate(b, factor)¶
- Parameters:
b (
Graphene.Matrix
) – aGraphene.Matrix
factor (
float
) – the linear interpolation factor
- Returns:
return location for the interpolated matrix
- Return type:
res:
Graphene.Matrix
Linearly interpolates the two given
Graphene.Matrix
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.
New in version 1.0.
- inverse()¶
- Returns:
true
if the matrix is invertible- res:
return location for the inverse matrix
- Return type:
(
bool
, res:Graphene.Matrix
)
Inverts the given matrix.
New in version 1.0.
- is_2d()¶
- Returns:
true
if the matrix is compatible with an affine transformation matrix- Return type:
Checks whether the given
Graphene.Matrix
is compatible with an a 2D affine transformation matrix.New in version 1.0.
- is_backface_visible()¶
- Returns:
true
if the back face of the matrix is visible- Return type:
Checks whether a
Graphene.Matrix
has a visible back face.New in version 1.0.
- is_identity()¶
- Returns:
true
if the matrix is the identity matrix- Return type:
Checks whether the given
Graphene.Matrix
is the identity matrix.New in version 1.0.
- is_singular()¶
- Returns:
true
if the matrix is singular- Return type:
Checks whether a matrix is singular.
New in version 1.0.
- multiply(b)¶
- Parameters:
b (
Graphene.Matrix
) – aGraphene.Matrix
- Returns:
return location for the matrix result
- Return type:
res:
Graphene.Matrix
Multiplies two
Graphene.Matrix
.Matrix multiplication is not commutative in general; the order of the factors matters. The product of this multiplication is (self × b)
New in version 1.0.
- near(b, epsilon)¶
- Parameters:
b (
Graphene.Matrix
) – aGraphene.Matrix
epsilon (
float
) – the threshold between the two matrices
- Returns:
true
if the two matrices are near each other, andfalse
otherwise- Return type:
Compares the two given
Graphene.Matrix
matrices and checks whether their values are within the given epsilon of each other.New in version 1.10.
- normalize()¶
- Returns:
return location for the normalized matrix
- Return type:
res:
Graphene.Matrix
Normalizes the given
Graphene.Matrix
.New in version 1.0.
- perspective(depth)¶
- Parameters:
depth (
float
) – the depth of the perspective- Returns:
return location for the perspective matrix
- Return type:
res:
Graphene.Matrix
Applies a perspective of depth to the matrix.
New in version 1.0.
- 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.
New in version 1.0.
- project_point(p)¶
- Parameters:
p (
Graphene.Point
) – aGraphene.Point
- Returns:
return location for the projected point
- Return type:
res:
Graphene.Point
Projects a
Graphene.Point
using the matrix self.New in version 1.0.
- project_rect(r)¶
- Parameters:
r (
Graphene.Rect
) – aGraphene.Rect
- Returns:
return location for the projected rectangle
- Return type:
res:
Graphene.Quad
Projects all corners of a
Graphene.Rect
using the given matrix.See also:
Graphene.Matrix.project_point
()New in version 1.2.
- project_rect_bounds(r)¶
- Parameters:
r (
Graphene.Rect
) – aGraphene.Rect
- Returns:
return location for the projected rectangle
- Return type:
res:
Graphene.Rect
Projects a
Graphene.Rect
using the given matrix.The resulting rectangle is the axis aligned bounding rectangle capable of fully containing the projected rectangle.
New in version 1.0.
- rotate(angle, axis)¶
- Parameters:
angle (
float
) – the rotation angle, in degreesaxis (
Graphene.Vec3
) – the rotation axis, as aGraphene.Vec3
Adds a rotation transformation to self, using the given angle and axis vector.
This is the equivalent of calling
Graphene.Matrix.init_rotate
() and then multiplying the matrix self with the rotation matrix.New in version 1.0.
- rotate_euler(e)¶
- Parameters:
e (
Graphene.Euler
) – a rotation described by aGraphene.Euler
Adds a rotation transformation to self, using the given
Graphene.Euler
.New in version 1.2.
- rotate_quaternion(q)¶
- Parameters:
q (
Graphene.Quaternion
) – a rotation described by aGraphene.Quaternion
Adds a rotation transformation to self, using the given
Graphene.Quaternion
.This is the equivalent of calling
Graphene.Quaternion.to_matrix
() and then multiplying self with the rotation matrix.New in version 1.2.
- rotate_x(angle)¶
- Parameters:
angle (
float
) – the rotation angle, in degrees
Adds a rotation transformation around the X axis to self, using the given angle.
See also:
Graphene.Matrix.rotate
()New in version 1.0.
- rotate_y(angle)¶
- Parameters:
angle (
float
) – the rotation angle, in degrees
Adds a rotation transformation around the Y axis to self, using the given angle.
See also:
Graphene.Matrix.rotate
()New in version 1.0.
- rotate_z(angle)¶
- Parameters:
angle (
float
) – the rotation angle, in degrees
Adds a rotation transformation around the Z axis to self, using the given angle.
See also:
Graphene.Matrix.rotate
()New in version 1.0.
- scale(factor_x, factor_y, factor_z)¶
- Parameters:
Adds a scaling transformation to self, using the three given factors.
This is the equivalent of calling
Graphene.Matrix.init_scale
() and then multiplying the matrix self with the scale matrix.New in version 1.0.
- skew_xy(factor)¶
- Parameters:
factor (
float
) – skew factor
Adds a skew of factor on the X and Y axis to the given matrix.
New in version 1.0.
- skew_xz(factor)¶
- Parameters:
factor (
float
) – skew factor
Adds a skew of factor on the X and Z axis to the given matrix.
New in version 1.0.
- skew_yz(factor)¶
- Parameters:
factor (
float
) – skew factor
Adds a skew of factor on the Y and Z axis to the given matrix.
New in version 1.0.
- to_2d()¶
- Returns:
true
if the matrix is compatible with an affine transformation matrix- xx:
return location for the xx member
- yx:
return location for the yx member
- xy:
return location for the xy member
- yy:
return location for the yy member
- x_0:
return location for the x0 member
- y_0:
return location for the y0 member
- Return type:
(
bool
, xx:float
, yx:float
, xy:float
, yy:float
, x_0:float
, y_0:float
)
Converts a
Graphene.Matrix
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
and an affine matrix type from other libraries.New in version 1.0.
- to_float()¶
- Returns:
return location for an array of floating point values. The array must be capable of holding at least 16 values.
- Return type:
v: [
float
]
Converts a
Graphene.Matrix
to an array of floating point values.New in version 1.0.
- transform_bounds(r)¶
- Parameters:
r (
Graphene.Rect
) – aGraphene.Rect
- Returns:
return location for the bounds of the transformed rectangle
- Return type:
res:
Graphene.Rect
Transforms each corner of a
Graphene.Rect
using the given matrix self.The result is the axis aligned bounding rectangle containing the coplanar quadrilateral.
See also:
Graphene.Matrix.transform_point
()New in version 1.0.
- transform_box(b)¶
- Parameters:
b (
Graphene.Box
) – aGraphene.Box
- Returns:
return location for the bounds of the transformed box
- Return type:
res:
Graphene.Box
Transforms the vertices of a
Graphene.Box
using the given matrix self.The result is the axis aligned bounding box containing the transformed vertices.
New in version 1.2.
- transform_point(p)¶
- Parameters:
p (
Graphene.Point
) – aGraphene.Point
- Returns:
return location for the transformed
Graphene.Point
- Return type:
res:
Graphene.Point
Transforms the given
Graphene.Point
using the matrix self.Unlike
Graphene.Matrix.transform_vec3
(), this function will take into account the fourth row vector of theGraphene.Matrix
when computing the dot product of each row vector of the matrix.See also: graphene_simd4x4f_point3_mul()
New in version 1.0.
- transform_point3d(p)¶
- Parameters:
p (
Graphene.Point3D
) – aGraphene.Point3D
- Returns:
return location for the result
- Return type:
res:
Graphene.Point3D
Transforms the given
Graphene.Point3D
using the matrix self.Unlike
Graphene.Matrix.transform_vec3
(), this function will take into account the fourth row vector of theGraphene.Matrix
when computing the dot product of each row vector of the matrix.See also: graphene_simd4x4f_point3_mul()
New in version 1.2.
- transform_ray(r)¶
- Parameters:
r (
Graphene.Ray
) – aGraphene.Ray
- Returns:
return location for the transformed ray
- Return type:
res:
Graphene.Ray
Transform a
Graphene.Ray
using the given matrix self.New in version 1.4.
- transform_rect(r)¶
- Parameters:
r (
Graphene.Rect
) – aGraphene.Rect
- Returns:
return location for the transformed quad
- Return type:
res:
Graphene.Quad
Transforms each corner of a
Graphene.Rect
using the given matrix self.The result is a coplanar quadrilateral.
See also:
Graphene.Matrix.transform_point
()New in version 1.0.
- transform_sphere(s)¶
- Parameters:
s (
Graphene.Sphere
) – aGraphene.Sphere
- Returns:
return location for the bounds of the transformed sphere
- Return type:
res:
Graphene.Sphere
Transforms a
Graphene.Sphere
using the given matrix self. The result is the bounding sphere containing the transformed sphere.New in version 1.2.
- transform_vec3(v)¶
- Parameters:
v (
Graphene.Vec3
) – aGraphene.Vec3
- Returns:
return location for a
Graphene.Vec3
- Return type:
res:
Graphene.Vec3
Transforms the given
Graphene.Vec3
using the matrix self.This function will multiply the X, Y, and Z row vectors of the matrix self with the corresponding components of the vector v. The W row vector will be ignored.
See also: graphene_simd4x4f_vec3_mul()
New in version 1.0.
- transform_vec4(v)¶
- Parameters:
v (
Graphene.Vec4
) – aGraphene.Vec4
- Returns:
return location for a
Graphene.Vec4
- Return type:
res:
Graphene.Vec4
Transforms the given
Graphene.Vec4
using the matrix self.See also: graphene_simd4x4f_vec4_mul()
New in version 1.0.
- translate(pos)¶
- Parameters:
pos (
Graphene.Point3D
) – aGraphene.Point3D
Adds a translation transformation to self using the coordinates of the given
Graphene.Point3D
.This is the equivalent of calling
Graphene.Matrix.init_translate
() and then multiplying self with the translation matrix.New in version 1.0.
- transpose()¶
- Returns:
return location for the transposed matrix
- Return type:
res:
Graphene.Matrix
Transposes the given matrix.
New in version 1.0.
- unproject_point3d(modelview, point)¶
- Parameters:
modelview (
Graphene.Matrix
) – aGraphene.Matrix
for the modelview matrix; this is the inverse of the modelview used when projecting the pointpoint (
Graphene.Point3D
) – aGraphene.Point3D
with the coordinates of the point
- Returns:
return location for the unprojected point
- Return type:
res:
Graphene.Point3D
Unprojects the given point using the self matrix and a modelview matrix.
New in version 1.2.
- untransform_bounds(r, bounds)¶
- Parameters:
r (
Graphene.Rect
) – aGraphene.Rect
bounds (
Graphene.Rect
) – the bounds of the transformation
- Returns:
return location for the untransformed rectangle
- Return type:
res:
Graphene.Rect
Undoes the transformation on the corners of a
Graphene.Rect
using the given matrix, within the given axis aligned rectangular bounds.New in version 1.0.
- untransform_point(p, bounds)¶
- Parameters:
p (
Graphene.Point
) – aGraphene.Point
bounds (
Graphene.Rect
) – the bounds of the transformation
- Returns:
true
if the point was successfully untransformed- res:
return location for the untransformed point
- Return type:
(
bool
, res:Graphene.Point
)
Undoes the transformation of a
Graphene.Point
using the given matrix, within the given axis aligned rectangular bounds.New in version 1.0.