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#

class Matrix
classmethod alloc() Matrix#

Allocates a new Matrix.

Added in version 1.0.

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.

determinant() float#

Computes the determinant of the given matrix.

Added in version 1.0.

equal(b: Matrix) bool#

Checks whether the two given Matrix matrices are equal.

Added in version 1.10.

Parameters:

b – a Matrix

equal_fast(b: Matrix) bool#

Checks whether the two given Matrix matrices 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 either equal() or 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
  }

Added in version 1.10.

Parameters:

b – a Matrix

free() None#

Frees the resources allocated by alloc().

Added in version 1.0.

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 row and col index.

Added in version 1.0.

Parameters:
  • row – the row index

  • col – the column index

get_x_scale() float#

Retrieves the scaling factor on the X axis in m.

Added in version 1.0.

get_x_translation() float#

Retrieves the translation component on the X axis from m.

Added in version 1.10.

get_y_scale() float#

Retrieves the scaling factor on the Y axis in m.

Added in version 1.0.

get_y_translation() float#

Retrieves the translation component on the Y axis from m.

Added in version 1.10.

get_z_scale() float#

Retrieves the scaling factor on the Z axis in m.

Added in version 1.0.

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 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 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 Matrix with 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 Matrix using 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 Matrix with 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 Matrix compatible with Frustum.

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_identity() Matrix#

Initializes a Matrix with the identity matrix.

Added in version 1.0.

init_look_at(eye: Vec3, center: Vec3, up: Vec3) Matrix#

Initializes a 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 m to transform a model of such a camera into world-space. However, it is more common to use the inverse of m 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.

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 Matrix with 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 Matrix with 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 m to represent a rotation of angle degrees on the axis represented by the axis vector.

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 Matrix with 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 Matrix with 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 Matrix with 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 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.

Added in version 1.0.

Parameters:
  • b – a Matrix

  • factor – the linear interpolation factor

inverse() Tuple[bool, Matrix]#

Inverts the given matrix.

Added in version 1.0.

is_2d() bool#

Checks whether the given Matrix is compatible with an a 2D affine transformation matrix.

Added in version 1.0.

is_backface_visible() bool#

Checks whether a Matrix has a visible back face.

Added in version 1.0.

is_identity() bool#

Checks whether the given Matrix is the identity matrix.

Added in version 1.0.

is_singular() bool#

Checks whether a matrix is singular.

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 Matrix matrices and checks whether their values are within the given epsilon of each other.

Added in version 1.10.

Parameters:
  • b – a Matrix

  • epsilon – the threshold between the two matrices

normalize() Matrix#

Normalizes the given Matrix.

Added in version 1.0.

perspective(depth: float) Matrix#

Applies a perspective of depth to the matrix.

Added in version 1.0.

Parameters:

depth – the depth of the perspective

print_() None#
project_point(p: Point) Point#

Projects a Point using the matrix m.

Added in version 1.0.

Parameters:

p – a Point

project_rect(r: Rect) Quad#

Projects all corners of a Rect using the given matrix.

See also: project_point()

Added in version 1.2.

Parameters:

r – a Rect

project_rect_bounds(r: Rect) Rect#

Projects a Rect using 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 given angle and axis vector.

This is the equivalent of calling init_rotate() and then multiplying the matrix m with 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 given Euler.

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 given Quaternion.

This is the equivalent of calling to_matrix() and then multiplying m with 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 given angle.

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 given angle.

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 given angle.

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 matrix m with 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 factor on 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 factor on 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 factor on 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 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 Matrix and an affine matrix type from other libraries.

Added in version 1.0.

to_float() list[float]#

Converts a Matrix to an array of floating point values.

Added in version 1.0.

transform_bounds(r: Rect) Rect#

Transforms each corner of a Rect using the given matrix m.

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 Box using the given matrix m.

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 Point using the matrix m.

Unlike transform_vec3(), this function will take into account the fourth row vector of the Matrix when 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 Point3D using the matrix m.

Unlike transform_vec3(), this function will take into account the fourth row vector of the Matrix when 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 Ray using the given matrix m.

Added in version 1.4.

Parameters:

r – a Ray

transform_rect(r: Rect) Quad#

Transforms each corner of a Rect using the given matrix m.

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 Sphere using the given matrix m. 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 Vec3 using the matrix m.

This function will multiply the X, Y, and Z row vectors of the matrix m with the corresponding components of the vector v. 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 Vec4 using the matrix m.

See also: graphene_simd4x4f_vec4_mul()

Added in version 1.0.

Parameters:

v – a Vec4

translate(pos: Point3D) None#

Adds a translation transformation to m using the coordinates of the given Point3D.

This is the equivalent of calling init_translate() and then multiplying m with the translation matrix.

Added in version 1.0.

Parameters:

pos – a Point3D

transpose() Matrix#

Transposes the given matrix.

Added in version 1.0.

unproject_point3d(modelview: Matrix, point: Point3D) Point3D#

Unprojects the given point using the projection matrix and a modelview matrix.

Added in version 1.2.

Parameters:
  • modelview – a Matrix for the modelview matrix; this is the inverse of the modelview used when projecting the point

  • point – a Point3D with the coordinates of the point

untransform_bounds(r: Rect, bounds: Rect) Rect#

Undoes the transformation on the corners of a Rect using the given matrix, within the given axis aligned rectangular bounds.

Added in version 1.0.

Parameters:
  • r – a Rect

  • bounds – the bounds of the transformation

untransform_point(p: Point, bounds: Rect) Tuple[bool, Point]#

Undoes the transformation of a Point using the given matrix, within the given axis aligned rectangular bounds.

Added in version 1.0.

Parameters:
  • p – a Point

  • bounds – the bounds of the transformation

Fields#

class Matrix
value#