Matrix functions

Matrix33

3x3 Matrix which supports rotation, translation, scale and skew.

Matrices are laid out in row-major format and can be loaded directly into OpenGL. To convert to column-major format, transpose the array using the numpy.array.T method.

pyrr.matrix33.apply_to_vector(*args, **kwargs)[source]

Apply a matrix to a vector.

The matrix’s rotation are applied to the vector. Supports multiple matrices and vectors.

Parameters:
  • mat (numpy.array) – The rotation / translation matrix. Can be a list of matrices.
  • vec (numpy.array) – The vector to modify. Can be a numpy.array of vectors. ie. numpy.array([[x1,…], [x2,…], …])
Return type:

numpy.array

Returns:

The vectors rotated by the specified matrix.

pyrr.matrix33.create_direction_scale(direction, scale)[source]

Creates a matrix which can apply a directional scaling to a set of vectors.

An example usage for this is to flatten a mesh against a single plane.

Parameters:
  • direction (numpy.array) – a numpy.array of shape (3,) of the direction to scale.
  • scale (float) – a float value for the scaling along the specified direction. A scale of 0.0 will flatten the vertices into a single plane with the direction being the plane’s normal.
Return type:

numpy.array

Returns:

The scaling matrix.

pyrr.matrix33.create_from_axis_rotation(*args, **kwargs)[source]

Creates a matrix from the specified theta rotation around an axis.

Parameters:
  • axis (numpy.array) – A (3,) vector specifying the axis of rotation.
  • theta (float) – A rotation specified in radians.
Return type:

numpy.array

Returns:

A matrix with shape (3,3).

pyrr.matrix33.create_from_eulers(*args, **kwargs)[source]

Creates a matrix from the specified Euler rotations.

Parameters:eulers (numpy.array) – A set of euler rotations in the format specified by the euler modules.
Return type:numpy.array
Returns:A matrix with shape (3,3) with the euler’s rotation.
pyrr.matrix33.create_from_inverse_of_quaternion(*args, **kwargs)[source]

Creates a matrix with the inverse rotation of a quaternion.

Parameters:quat (numpy.array) – The quaternion to make the matrix from (shape 4).
Return type:numpy.array
Returns:A matrix with shape (3,3) that respresents the inverse of the quaternion.
pyrr.matrix33.create_from_matrix44(mat, dtype=None)[source]

Creates a Matrix33 from a Matrix44.

Return type:numpy.array
Returns:A matrix with shape (3,3) with the input matrix rotation.
pyrr.matrix33.create_from_quaternion(*args, **kwargs)[source]

Creates a matrix with the same rotation as a quaternion.

Parameters:quat – The quaternion to create the matrix from.
Return type:numpy.array
Returns:A matrix with shape (3,3) with the quaternion’s rotation.
pyrr.matrix33.create_from_scale(scale, dtype=None)[source]

Creates an identity matrix with the scale set.

Parameters:scale (numpy.array) – The scale to apply as a vector (shape 3).
Return type:numpy.array
Returns:A matrix with shape (3,3) with the scale set to the specified vector.
pyrr.matrix33.create_from_x_rotation(theta, dtype=None)[source]

Creates a matrix with the specified rotation about the X axis.

Parameters:theta (float) – The rotation, in radians, about the X-axis.
Return type:numpy.array
Returns:A matrix with the shape (3,3) with the specified rotation about the X-axis.
pyrr.matrix33.create_from_y_rotation(theta, dtype=None)[source]

Creates a matrix with the specified rotation about the Y axis.

Parameters:theta (float) – The rotation, in radians, about the Y-axis.
Return type:numpy.array
Returns:A matrix with the shape (3,3) with the specified rotation about the Y-axis.
pyrr.matrix33.create_from_z_rotation(theta, dtype=None)[source]

Creates a matrix with the specified rotation about the Z axis.

Parameters:theta (float) – The rotation, in radians, about the Z-axis.
Return type:numpy.array
Returns:A matrix with the shape (3,3) with the specified rotation about the Z-axis.
pyrr.matrix33.create_identity(dtype=None)[source]

Creates a new matrix33 and sets it to an identity matrix.

Return type:numpy.array
Returns:A matrix representing an identity matrix with shape (3,3).
pyrr.matrix33.inverse(mat)[source]

Returns the inverse of the matrix.

This is essentially a wrapper around numpy.linalg.inv.

Parameters:m (numpy.array) – A matrix.
Return type:numpy.array
Returns:The inverse of the specified matrix.
pyrr.matrix33.multiply(m1, m2)[source]

Multiply two matricies, m1 . m2.

This is essentially a wrapper around numpy.dot( m1, m2 )

Parameters:
  • m1 (numpy.array) – The first matrix. Can be a list of matrices.
  • m2 (numpy.array) – The second matrix. Can be a list of matrices.
Return type:

numpy.array

Returns:

A matrix that results from multiplying m1 by m2.

Matrix44

4x4 Matrix which supports rotation, translation, scale and skew.

Matrices are laid out in row-major format and can be loaded directly into OpenGL. To convert to column-major format, transpose the array using the numpy.array.T method.

pyrr.matrix44.apply_to_vector(*args, **kwargs)[source]

Apply a matrix to a vector.

The matrix’s rotation and translation are applied to the vector. Supports multiple matrices and vectors.

Parameters:
  • mat (numpy.array) – The rotation / translation matrix. Can be a list of matrices.
  • vec (numpy.array) – The vector to modify. Can be a numpy.array of vectors. ie. numpy.array([[x1,…], [x2,…], …])
Return type:

numpy.array

Returns:

The vectors rotated by the specified matrix.

pyrr.matrix44.create_from_axis_rotation(*args, **kwargs)[source]

Creates a matrix from the specified rotation theta around an axis.

Parameters:
  • axis (numpy.array) – A (3,) vector.
  • theta (float) – A rotation in radians.
Return type:

numpy.array

Returns:

A matrix with shape (4,4).

pyrr.matrix44.create_from_eulers(*args, **kwargs)[source]

Creates a matrix from the specified Euler rotations.

Parameters:eulers (numpy.array) – A set of euler rotations in the format specified by the euler modules.
Return type:numpy.array
Returns:A matrix with shape (4,4) with the euler’s rotation.
pyrr.matrix44.create_from_inverse_of_quaternion(*args, **kwargs)[source]

Creates a matrix with the inverse rotation of a quaternion.

This can be used to go from object space to inertial space.

Parameters:quat (numpy.array) – The quaternion to make the matrix from (shape 4).
Return type:numpy.array
Returns:A matrix with shape (4,4) that respresents the inverse of the quaternion.
pyrr.matrix44.create_from_matrix33(mat, dtype=None)[source]

Creates a Matrix44 from a Matrix33.

The translation will be 0,0,0.

Return type:numpy.array
Returns:A matrix with shape (4,4) with the input matrix rotation.
pyrr.matrix44.create_from_quaternion(*args, **kwargs)[source]

Creates a matrix with the same rotation as a quaternion.

Parameters:quat – The quaternion to create the matrix from.
Return type:numpy.array
Returns:A matrix with shape (4,4) with the quaternion’s rotation.
pyrr.matrix44.create_from_scale(scale, dtype=None)[source]

Creates an identity matrix with the scale set.

Parameters:scale (numpy.array) – The scale to apply as a vector (shape 3).
Return type:numpy.array
Returns:A matrix with shape (4,4) with the scale set to the specified vector.
pyrr.matrix44.create_from_translation(*args, **kwargs)[source]

Creates an identity matrix with the translation set.

Parameters:vec (numpy.array) – The translation vector (shape 3 or 4).
Return type:numpy.array
Returns:A matrix with shape (4,4) that represents a matrix with the translation set to the specified vector.
pyrr.matrix44.create_from_x_rotation(theta, dtype=None)[source]

Creates a matrix with the specified rotation about the X axis.

Parameters:theta (float) – The rotation, in radians, about the X-axis.
Return type:numpy.array
Returns:A matrix with the shape (4,4) with the specified rotation about the X-axis.
pyrr.matrix44.create_from_y_rotation(theta, dtype=None)[source]

Creates a matrix with the specified rotation about the Y axis.

Parameters:theta (float) – The rotation, in radians, about the Y-axis.
Return type:numpy.array
Returns:A matrix with the shape (4,4) with the specified rotation about the Y-axis.
pyrr.matrix44.create_from_z_rotation(theta, dtype=None)[source]

Creates a matrix with the specified rotation about the Z axis.

Parameters:theta (float) – The rotation, in radians, about the Z-axis.
Return type:numpy.array
Returns:A matrix with the shape (4,4) with the specified rotation about the Z-axis.
pyrr.matrix44.create_identity(dtype=None)[source]

Creates a new matrix44 and sets it to an identity matrix.

Return type:numpy.array
Returns:A matrix representing an identity matrix with shape (4,4).
pyrr.matrix44.create_look_at(eye, target, up, dtype=None)[source]

Creates a look at matrix according to OpenGL standards.

Parameters:
  • eye (numpy.array) – Position of the camera in world coordinates.
  • target (numpy.array) – The position in world coordinates that the camera is looking at.
  • up (numpy.array) – The up vector of the camera.
Return type:

numpy.array

Returns:

A look at matrix that can be used as a viewMatrix

pyrr.matrix44.create_matrix33_view(mat)[source]

Returns a view into the matrix in Matrix33 format.

This is different from matrix33.create_from_matrix44, in that changes to the returned matrix will also alter the original matrix.

Return type:numpy.array
Returns:A view into the matrix in the format of a matrix33 (shape (3,3)).
pyrr.matrix44.create_orthogonal_projection(left, right, bottom, top, near, far, dtype=None)[source]

Creates an orthogonal projection matrix.

Parameters:
  • left (float) – The left of the near plane relative to the plane’s centre.
  • right (float) – The right of the near plane relative to the plane’s centre.
  • top (float) – The top of the near plane relative to the plane’s centre.
  • bottom (float) – The bottom of the near plane relative to the plane’s centre.
  • near (float) – The distance of the near plane from the camera’s origin. It is recommended that the near plane is set to 1.0 or above to avoid rendering issues at close range.
  • far (float) – The distance of the far plane from the camera’s origin.
Return type:

numpy.array

Returns:

A projection matrix representing the specified orthogonal perspective.

pyrr.matrix44.create_orthogonal_projection_matrix(left, right, bottom, top, near, far, dtype=None)[source]

Creates an orthogonal projection matrix.

Parameters:
  • left (float) – The left of the near plane relative to the plane’s centre.
  • right (float) – The right of the near plane relative to the plane’s centre.
  • top (float) – The top of the near plane relative to the plane’s centre.
  • bottom (float) – The bottom of the near plane relative to the plane’s centre.
  • near (float) – The distance of the near plane from the camera’s origin. It is recommended that the near plane is set to 1.0 or above to avoid rendering issues at close range.
  • far (float) – The distance of the far plane from the camera’s origin.
Return type:

numpy.array

Returns:

A projection matrix representing the specified orthogonal perspective.

pyrr.matrix44.create_perspective_projection(fovy, aspect, near, far, dtype=None)[source]

Creates perspective projection matrix.

Parameters:
  • fovy (float) – field of view in y direction in degrees
  • aspect (float) – aspect ratio of the view (width / height)
  • near (float) – distance from the viewer to the near clipping plane (only positive)
  • far (float) – distance from the viewer to the far clipping plane (only positive)
Return type:

numpy.array

Returns:

A projection matrix representing the specified perpective.

pyrr.matrix44.create_perspective_projection_from_bounds(left, right, bottom, top, near, far, dtype=None)[source]

Creates a perspective projection matrix using the specified near plane dimensions.

Parameters:
  • left (float) – The left of the near plane relative to the plane’s centre.
  • right (float) – The right of the near plane relative to the plane’s centre.
  • top (float) – The top of the near plane relative to the plane’s centre.
  • bottom (float) – The bottom of the near plane relative to the plane’s centre.
  • near (float) – The distance of the near plane from the camera’s origin. It is recommended that the near plane is set to 1.0 or above to avoid rendering issues at close range.
  • far (float) – The distance of the far plane from the camera’s origin.
Return type:

numpy.array

Returns:

A projection matrix representing the specified perspective.

pyrr.matrix44.create_perspective_projection_matrix(fovy, aspect, near, far, dtype=None)[source]

Creates perspective projection matrix.

Parameters:
  • fovy (float) – field of view in y direction in degrees
  • aspect (float) – aspect ratio of the view (width / height)
  • near (float) – distance from the viewer to the near clipping plane (only positive)
  • far (float) – distance from the viewer to the far clipping plane (only positive)
Return type:

numpy.array

Returns:

A projection matrix representing the specified perpective.

pyrr.matrix44.create_perspective_projection_matrix_from_bounds(left, right, bottom, top, near, far, dtype=None)[source]

Creates a perspective projection matrix using the specified near plane dimensions.

Parameters:
  • left (float) – The left of the near plane relative to the plane’s centre.
  • right (float) – The right of the near plane relative to the plane’s centre.
  • top (float) – The top of the near plane relative to the plane’s centre.
  • bottom (float) – The bottom of the near plane relative to the plane’s centre.
  • near (float) – The distance of the near plane from the camera’s origin. It is recommended that the near plane is set to 1.0 or above to avoid rendering issues at close range.
  • far (float) – The distance of the far plane from the camera’s origin.
Return type:

numpy.array

Returns:

A projection matrix representing the specified perspective.

pyrr.matrix44.decompose(m)[source]

Decomposes an affine transformation matrix into its scale, rotation and translation components.

Parameters:m (numpy.array) – A matrix.
Returns:tuple (scale, rotation, translation) numpy.array scale vector3 numpy.array rotation quaternion numpy.array translation vector3
pyrr.matrix44.inverse(m)[source]

Returns the inverse of the matrix.

This is essentially a wrapper around numpy.linalg.inv.

Parameters:m (numpy.array) – A matrix.
Return type:numpy.array
Returns:The inverse of the specified matrix.
pyrr.matrix44.multiply(m1, m2)[source]

Multiply two matricies, m1 . m2.

This is essentially a wrapper around numpy.dot(m1, m2)

Parameters:
  • m1 (numpy.array) – The first matrix. Can be a list of matrices.
  • m2 (numpy.array) – The second matrix. Can be a list of matrices.
Return type:

numpy.array

Returns:

A matrix that results from multiplying m1 by m2.