Matrices

Matrix33

Represents a 3x3 Matrix.

The Matrix33 class provides a number of convenient functions and conversions.

import numpy as np
from pyrr import Quaternion, Matrix33, Matrix44, Vector3

m = Matrix33()
m = Matrix33([[1.,0.,0.],[0.,1.,0.],[0.,0.,1.]])

# copy constructor
m = Matrix44(Matrix44())

# explicit creation
m = Matrix33.identity()
m = Matrix33.from_matrix44(Matrix44())

# inferred conversions
m = Matrix33(Quaternion())
m = Matrix33(Matrix44())

# multiply matricies together
m = Matrix33() * Matrix33()
m = Matrix33() * Matrix44()

# extract a quaternion from a matrix
q = m.quaternion

# convert from quaternion back to matrix
m = q.matrix33
m = Matrix33(q)

# rotate a matrix by a quaternion
m = Matrix33.identity() * Quaternion()

# rotate a vector 3 by a matrix
v = Matrix33.from_x_rotation(np.pi) * Vector3([1.,2.,3.])

# undo a rotation
m = Matrix33.from_x_rotation(np.pi)
v = m * Vector3([1.,1.,1.])
# ~m is the same as m.inverse
v = ~m * v

# access specific parts of the matrix
# first row
m1 = m.m1
# first element, first row
m11 = m.m11
# third element, third row
m33 = m.m33
# first row, same as m1
r1 = m.r1
# first column
c1 = m.c1
class pyrr.objects.matrix33.Matrix33[source]
c1

The first column of this Matrix as a numpy.ndarray.

c2

The second column of this Matrix as a numpy.ndarray.

c3

The third column of this Matrix as a numpy.ndarray.

classmethod from_matrix44(matrix, dtype=None)[source]

Creates a Matrix33 from a Matrix44.

The Matrix44 translation will be lost.

m1

The first row of this Matrix as a numpy.ndarray.

m11

The [0,0] value of this Matrix.

m12

The [0,1] value of this Matrix.

m13

The [0,2] value of this Matrix.

m2

The second row of this Matrix as a numpy.ndarray.

m21

The [1,0] value of this Matrix.

m22

The [1,1] value of this Matrix.

m23

The [1,2] value of this Matrix.

m3

The third row of this Matrix as a numpy.ndarray.

m31

The [2,0] value of this Matrix.

m32

The [2,1] value of this Matrix.

m33

The [2,2] value of this Matrix.

matrix33

Returns the Matrix33.

This can be handy if you’re not sure what type of Matrix class you have but require a Matrix33.

matrix44

Returns a Matrix44 representing this matrix.

quaternion

Returns a Quaternion representing this matrix.

r1

The first row of this Matrix as a numpy.ndarray. This is the same as m1.

r2

The second row of this Matrix as a numpy.ndarray. This is the same as m2.

r3

The third row of this Matrix as a numpy.ndarray. This is the same as m3.

Matrix44

Represents a 4x4 Matrix.

The Matrix44 class provides a number of convenient functions and conversions.

import numpy as np
from pyrr import Quaternion, Matrix33, Matrix44, Vector4

m = Matrix44()
m = Matrix44([[1.,0.,0.,0.],[0.,1.,0.,0.],[0.,0.,1.,0.],[0.,0.,0.,1.]])

# copy constructor
m = Matrix44(Matrix44())

# explicit creation
m = Matrix44.identity()
m = Matrix44.from_matrix44(Matrix44())

# inferred conversions
m = Matrix44(Quaternion())
m = Matrix44(Matrix33())

# multiply matricies together
m = Matrix44() * Matrix44()

# extract a quaternion from a matrix
q = m.quaternion

# convert from quaternion back to matrix
m = q.matrix44
m = Matrix44(q)

# rotate a matrix by a quaternion
m = Matrix44.identity() * Quaternion()

# rotate a vector 4 by a matrix
v = Matrix44.from_x_rotation(np.pi) * Vector4([1.,2.,3.,1.])

# undo a rotation
m = Matrix44.from_x_rotation(np.pi)
v = m * Vector4([1.,1.,1.,1.])
# ~m is the same as m.inverse
v = ~m * v

# access specific parts of the matrix
# first row
m1 = m.m1
# first element, first row
m11 = m.m11
# fourth element, fourth row
m44 = m.m44
# first row, same as m1
r1 = m.r1
# first column
c1 = m.c1
class pyrr.objects.matrix44.Matrix44[source]
c1

The first column of this Matrix as a numpy.ndarray.

c2

The second column of this Matrix as a numpy.ndarray.

c3

The third column of this Matrix as a numpy.ndarray.

c4

The fourth column of this Matrix as a numpy.ndarray.

decompose()[source]

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

Parameters:m (numpy.array) – A matrix.
Returns:tuple (scale, rotation, translation) Vector3 scale Quaternion rotation Vector3 translation
classmethod from_matrix33(matrix, dtype=None)[source]

Creates a Matrix44 from a Matrix33.

classmethod from_translation(translation, dtype=None)[source]

Creates a Matrix44 from the specified translation.

classmethod look_at(eye, target, up, dtype=None)[source]

Creates a Matrix44 for use as a lookAt matrix.

m1

The first row of this Matrix as a numpy.ndarray.

m11

The [0,0] value of this Matrix.

m12

The [0,1] value of this Matrix.

m13

The [0,2] value of this Matrix.

m14

The [0,3] value of this Matrix.

m2

The second row of this Matrix as a numpy.ndarray.

m21

The [1,0] value of this Matrix.

m22

The [1,1] value of this Matrix.

m23

The [1,2] value of this Matrix.

m24

The [1,3] value of this Matrix.

m3

The third row of this Matrix as a numpy.ndarray.

m31

The [2,0] value of this Matrix.

m32

The [2,1] value of this Matrix.

m33

The [2,2] value of this Matrix.

m34

The [2,3] value of this Matrix.

m4

The fourth row of this Matrix as a numpy.ndarray.

m41

The [3,0] value of this Matrix.

m42

The [3,1] value of this Matrix.

m43

The [3,2] value of this Matrix.

m44

The [3,3] value of this Matrix.

matrix33

Returns a Matrix33 representing this matrix.

matrix44

Returns the Matrix44.

This can be handy if you’re not sure what type of Matrix class you have but require a Matrix44.

classmethod orthogonal_projection(left, right, top, bottom, near, far, dtype=None)[source]

Creates a Matrix44 for use as an orthogonal projection matrix.

classmethod perspective_projection(fovy, aspect, near, far, dtype=None)[source]

Creates a Matrix44 for use as a perspective projection matrix.

classmethod perspective_projection_bounds(left, right, top, bottom, near, far, dtype=None)[source]

Creates a Matrix44 for use as a perspective projection matrix.

quaternion

Returns a Quaternion representing this matrix.

r1

The first row of this Matrix as a numpy.ndarray. This is the same as m1.

r2

The second row of this Matrix as a numpy.ndarray. This is the same as m2.

r3

The third row of this Matrix as a numpy.ndarray. This is the same as m3.

r4

The fourth row of this Matrix as a numpy.ndarray. This is the same as m4.