Matrix#
- class Matrix(*args, **kwargs)#
A structure capable of holding a 4x4 matrix.
The contents of the Matrix structure are private and
should never be accessed directly.
Constructors#
Methods#
- class Matrix
- decompose() tuple[bool, Vec3, Vec3, Quaternion, Vec3, 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.
- equal(b: Matrix) bool#
Checks whether the two given
Matrixmatrices are equal.Added in version 1.10.
- Parameters:
b – a
Matrix
- equal_fast(b: Matrix) bool#
Checks whether the two given
Matrixmatrices are byte-by-byte equal.While this function is faster than
equal(), it can also return false negatives, so it should be used in conjuction with eitherequal()ornear(). 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 }
Added in version 1.10.
- Parameters:
b – a
Matrix
- get_row(index_: int) Vec4#
Retrieves the given row vector at
index_inside a matrix.Added in version 1.0.
- Parameters:
index – the index of the row vector, between 0 and 3
- get_value(row: int, col: int) float#
Retrieves the value at the given
rowandcolindex.Added in version 1.0.
- Parameters:
row – the row index
col – the column index
- get_x_translation() float#
Retrieves the translation component on the X axis from
m.Added in version 1.10.
- get_y_translation() float#
Retrieves the translation component on the Y axis from
m.Added in version 1.10.
- get_z_translation() float#
Retrieves the translation component on the Z axis from
m.Added in version 1.10.
- init_from_2d(xx: float, yx: float, xy: float, yy: float, x_0: float, y_0: float) Matrix#
Initializes a
Matrixfrom 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
Matrix.Added in version 1.0.
- Parameters:
xx – the xx member
yx – the yx member
xy – the xy member
yy – the yy member
x_0 – the x0 member
y_0 – the y0 member
- init_from_float(v: list[float]) Matrix#
Initializes a
Matrixwith the given array of floating point values.Added in version 1.0.
- Parameters:
v – an array of at least 16 floating point values
- init_from_matrix(src: Matrix) Matrix#
Initializes a
Matrixusing the values of the given matrix.Added in version 1.0.
- Parameters:
src – a
Matrix
- init_from_vec4(v0: Vec4, v1: Vec4, v2: Vec4, v3: Vec4) Matrix#
Initializes a
Matrixwith the given four row vectors.Added in version 1.0.
- Parameters:
v0 – the first row vector
v1 – the second row vector
v2 – the third row vector
v3 – the fourth row vector
- init_frustum(left: float, right: float, bottom: float, top: float, z_near: float, z_far: float) Matrix#
Initializes a
Matrixcompatible withFrustum.See also:
init_from_matrix()Added in version 1.2.
- Parameters:
left – distance of the left clipping plane
right – distance of the right clipping plane
bottom – distance of the bottom clipping plane
top – distance of the top clipping plane
z_near – distance of the near clipping plane
z_far – distance of the far clipping plane
- init_look_at(eye: Vec3, center: Vec3, up: Vec3) Matrix#
Initializes a
Matrixso that it positions the “camera” at the giveneyecoordinates towards an object at thecentercoordinates. The top of the camera is aligned to the direction of theupvector.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
mto transform a model of such a camera into world-space. However, it is more common to use the inverse ofmto 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.Added in version 1.0.
- Parameters:
eye – the vector describing the position to look from
center – the vector describing the position to look at
up – the vector describing the world’s upward direction; usually, this is the
y_axis()vector
- init_ortho(left: float, right: float, top: float, bottom: float, z_near: float, z_far: float) Matrix#
Initializes a
Matrixwith an orthographic projection.Added in version 1.0.
- Parameters:
left – the left edge of the clipping plane
right – the right edge of the clipping plane
top – the top edge of the clipping plane
bottom – the bottom edge of the clipping plane
z_near – the distance of the near clipping plane
z_far – the distance of the far clipping plane
- init_perspective(fovy: float, aspect: float, z_near: float, z_far: float) Matrix#
Initializes a
Matrixwith a perspective projection.Added in version 1.0.
- Parameters:
fovy – the field of view angle, in degrees
aspect – the aspect value
z_near – the near Z plane
z_far – the far Z plane
- init_rotate(angle: float, axis: Vec3) Matrix#
Initializes
mto represent a rotation ofangledegrees on the axis represented by theaxisvector.Added in version 1.0.
- Parameters:
angle – the rotation angle, in degrees
axis – the axis vector as a
Vec3
- init_scale(x: float, y: float, z: float) Matrix#
Initializes a
Matrixwith the given scaling factors.Added in version 1.0.
- Parameters:
x – the scale factor on the X axis
y – the scale factor on the Y axis
z – the scale factor on the Z axis
- init_skew(x_skew: float, y_skew: float) Matrix#
Initializes a
Matrixwith a skew transformation with the given factors.Added in version 1.0.
- Parameters:
x_skew – skew factor, in radians, on the X axis
y_skew – skew factor, in radians, on the Y axis
- init_translate(p: Point3D) Matrix#
Initializes a
Matrixwith a translation to the given coordinates.Added in version 1.0.
- Parameters:
p – the translation coordinates
- interpolate(b: Matrix, factor: float) Matrix#
Linearly interpolates the two given
Matrixby 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.
Added in version 1.0.
- Parameters:
b – a
Matrixfactor – the linear interpolation factor
- is_2d() bool#
Checks whether the given
Matrixis compatible with an a 2D affine transformation matrix.Added in version 1.0.
- multiply(b: Matrix) Matrix#
Multiplies two
Matrix.Matrix multiplication is not commutative in general; the order of the factors matters. The product of this multiplication is (
a×b)Added in version 1.0.
- Parameters:
b – a
Matrix
- near(b: Matrix, epsilon: float) bool#
Compares the two given
Matrixmatrices and checks whether their values are within the givenepsilonof each other.Added in version 1.10.
- Parameters:
b – a
Matrixepsilon – the threshold between the two matrices
- perspective(depth: float) Matrix#
Applies a perspective of
depthto the matrix.Added in version 1.0.
- Parameters:
depth – the depth of the perspective
- project_point(p: Point) Point#
Projects a
Pointusing the matrixm.Added in version 1.0.
- Parameters:
p – a
Point
- project_rect(r: Rect) Quad#
Projects all corners of a
Rectusing the given matrix.See also:
project_point()Added in version 1.2.
- Parameters:
r – a
Rect
- project_rect_bounds(r: Rect) Rect#
Projects a
Rectusing the given matrix.The resulting rectangle is the axis aligned bounding rectangle capable of fully containing the projected rectangle.
Added in version 1.0.
- Parameters:
r – a
Rect
- rotate(angle: float, axis: Vec3) None#
Adds a rotation transformation to
m, using the givenangleandaxisvector.This is the equivalent of calling
init_rotate()and then multiplying the matrixmwith the rotation matrix.Added in version 1.0.
- Parameters:
angle – the rotation angle, in degrees
axis – the rotation axis, as a
Vec3
- rotate_euler(e: Euler) None#
Adds a rotation transformation to
m, using the givenEuler.Added in version 1.2.
- Parameters:
e – a rotation described by a
Euler
- rotate_quaternion(q: Quaternion) None#
Adds a rotation transformation to
m, using the givenQuaternion.This is the equivalent of calling
to_matrix()and then multiplyingmwith the rotation matrix.Added in version 1.2.
- Parameters:
q – a rotation described by a
Quaternion
- rotate_x(angle: float) None#
Adds a rotation transformation around the X axis to
m, using the givenangle.See also:
rotate()Added in version 1.0.
- Parameters:
angle – the rotation angle, in degrees
- rotate_y(angle: float) None#
Adds a rotation transformation around the Y axis to
m, using the givenangle.See also:
rotate()Added in version 1.0.
- Parameters:
angle – the rotation angle, in degrees
- rotate_z(angle: float) None#
Adds a rotation transformation around the Z axis to
m, using the givenangle.See also:
rotate()Added in version 1.0.
- Parameters:
angle – the rotation angle, in degrees
- scale(factor_x: float, factor_y: float, factor_z: float) None#
Adds a scaling transformation to
m, using the three given factors.This is the equivalent of calling
init_scale()and then multiplying the matrixmwith the scale matrix.Added in version 1.0.
- Parameters:
factor_x – scaling factor on the X axis
factor_y – scaling factor on the Y axis
factor_z – scaling factor on the Z axis
- skew_xy(factor: float) None#
Adds a skew of
factoron the X and Y axis to the given matrix.Added in version 1.0.
- Parameters:
factor – skew factor
- skew_xz(factor: float) None#
Adds a skew of
factoron the X and Z axis to the given matrix.Added in version 1.0.
- Parameters:
factor – skew factor
- skew_yz(factor: float) None#
Adds a skew of
factoron the Y and Z axis to the given matrix.Added in version 1.0.
- Parameters:
factor – skew factor
- to_2d() tuple[bool, float, float, float, float, float, float]#
Converts a
Matrixto 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
Matrixand an affine matrix type from other libraries.Added in version 1.0.
- to_float() list[float]#
Converts a
Matrixto an array of floating point values.Added in version 1.0.
- transform_bounds(r: Rect) Rect#
Transforms each corner of a
Rectusing the given matrixm.The result is the axis aligned bounding rectangle containing the coplanar quadrilateral.
See also:
transform_point()Added in version 1.0.
- Parameters:
r – a
Rect
- transform_box(b: Box) Box#
Transforms the vertices of a
Boxusing the given matrixm.The result is the axis aligned bounding box containing the transformed vertices.
Added in version 1.2.
- Parameters:
b – a
Box
- transform_point(p: Point) Point#
Transforms the given
Pointusing the matrixm.Unlike
transform_vec3(), this function will take into account the fourth row vector of theMatrixwhen computing the dot product of each row vector of the matrix.See also: graphene_simd4x4f_point3_mul()
Added in version 1.0.
- Parameters:
p – a
Point
- transform_point3d(p: Point3D) Point3D#
Transforms the given
Point3Dusing the matrixm.Unlike
transform_vec3(), this function will take into account the fourth row vector of theMatrixwhen computing the dot product of each row vector of the matrix.See also: graphene_simd4x4f_point3_mul()
Added in version 1.2.
- Parameters:
p – a
Point3D
- transform_ray(r: Ray) Ray#
Transform a
Rayusing the given matrixm.Added in version 1.4.
- Parameters:
r – a
Ray
- transform_rect(r: Rect) Quad#
Transforms each corner of a
Rectusing the given matrixm.The result is a coplanar quadrilateral.
See also:
transform_point()Added in version 1.0.
- Parameters:
r – a
Rect
- transform_sphere(s: Sphere) Sphere#
Transforms a
Sphereusing the given matrixm. The result is the bounding sphere containing the transformed sphere.Added in version 1.2.
- Parameters:
s – a
Sphere
- transform_vec3(v: Vec3) Vec3#
Transforms the given
Vec3using the matrixm.This function will multiply the X, Y, and Z row vectors of the matrix
mwith the corresponding components of the vectorv. The W row vector will be ignored.See also: graphene_simd4x4f_vec3_mul()
Added in version 1.0.
- Parameters:
v – a
Vec3
- transform_vec4(v: Vec4) Vec4#
Transforms the given
Vec4using the matrixm.See also: graphene_simd4x4f_vec4_mul()
Added in version 1.0.
- Parameters:
v – a
Vec4
- translate(pos: Point3D) None#
Adds a translation transformation to
musing the coordinates of the givenPoint3D.This is the equivalent of calling
init_translate()and then multiplyingmwith the translation matrix.Added in version 1.0.
- Parameters:
pos – a
Point3D
- unproject_point3d(modelview: Matrix, point: Point3D) Point3D#
Unprojects the given
pointusing theprojectionmatrix and amodelviewmatrix.Added in version 1.2.
Fields#
- class Matrix
- value#