miniglm

Minimalist pythonic matrix, vector, quaternion math.

Matrices are stored in column-major order.

This module is using glm.

Install

pip install miniglm

Alternatives

Numpy and Pyrr are great alternatives. This package was built to provide similar features.

Documentation

mat4_perspective(fovy, ratio, znear, zfar) → Mat4
Parameters:
  • fovy (float) – fovy
  • ratio (float) – ratio
  • znear (float) – znear
  • zfar (float) – zfar
Returns:

perspective

Return type:

Mat4

from miniglm import mat4_perspective, mat4_look_at, radians

proj = mat4_perspective(radians(45.0), 16.0 / 9.0, 0.1, 1000.0)
look = mat4_look_at(
    (3.0, 2.0, 1.0),
    (0.0, 0.0, 0.0),
    (0.0, 0.0, 1.0),
)

mvp = proj * look
mat4_ortho(left, right, bottom, top, znear, zfar) → Mat4
Parameters:
  • left (float) – left
  • right (float) – right
  • bottom (float) – bottom
  • top (float) – top
  • znear (float) – znear
  • zfar (float) – zfar
Returns:

ortho

Return type:

Mat4

from miniglm import mat4_ortho, mat4_look_at, radians

proj = mat4_ortho(-10.0, 10.0, -10.0, 10.0, 0.1, 1000.0)
look = mat4_look_at(
    (0.0, 0.0, 100.0),
    (0.0, 0.0, 0.0),
    (0.0, 1.0, 0.0),
)

mvp = proj * look
mat4_look_at(eye, center, up) → Mat4
Parameters:
  • eye (Vec3) – eye
  • center (Vec3) – center
  • up (Vec3) – up
Returns:

look_at

Return type:

Mat4

class Vec2(iterable)
# new instance
>>> a = Vec2([3.0, 4.0])
>>> a
(3.0, 4.0)

# length
>>> a.length
5.0

>>> b = Vec2([10.0, 10.0])

# normal
>>> b.normal
(0.7071068286895752, 0.7071068286895752)

# dot product
>>> a.dot(b)
70.0

>>> light = Vec2([1.0, -1.0])

# reflect
>>> light.reflect(Vec2([0.0, 1.0]))
(1.0, 1.0)

# refract
>>> light.refract(Vec2([0.0, 1.0]), 0.0)
(0.0, -1.0)

# refract
>>> light.refract(Vec2([0.0, 1.0]), 1.0)
(1.0, -1.0)

# unpack
>>> x, y = a

# convert to list
>>> list(a)
[3.0, 4.0]

# convert to bytes
>>> bytes(a)
b'\x00\x00@@\x00\x00\x80@'

# basic math
>>> c = a + b * 2.0
>>> c
(23.0, 24.0)

# indexing
>>> c[1]
24.0

# element-wise multiplication
>>> a * a
(9.0, 16.0)
length

float – length

normal

Vec2 – normal

tup

tuple – tuple

dot(rhs) → float
Parameters:rhs (Vec2) – rhs
Returns:dot product
Return type:float
reflect(norm) → Vec2
Parameters:norm (Vec2) – norm
Returns:reflect
Return type:Vec2
refract(norm, eta) → Vec2
Parameters:
  • norm (Vec2) – norm
  • eta (float) – eta
Returns:

refract

Return type:

Vec2

class Vec3(iterable)
# new instance
>>> a = Vec3([1.0, 2.0, 3.0])
>>> a
(1.0, 2.0, 3.0)

# length
>>> a.length
3.7416574954986572

>>> b = Vec3([10.0, 10.0, 10.0])

# normal
>>> b.normal
(0.5773501992225647, 0.5773501992225647, 0.5773501992225647)

# dot product
>>> a.dot(b)
60.0

# cross product
>>> a.cross(b)
(-10.0, 20.0, -10.0)

>>> light = Vec3([1.0, 0.0, -1.0])
>>> up = Vec3([0.0, 0.0, 1.0])

# reflect
>>> light.reflect(up)
(1.0, 0.0, 1.0)

# refract
>>> light.refract(up, 0.0)
(0.0, 0.0, -1.0)

# refract
>>> light.refract(up, 1.0)
(1.0, 0.0, -1.0)

# unpack
>>> x, y, z = a

# convert to list
>>> list(a)
[1.0, 2.0, 3.0]

# convert to bytes
>>> bytes(a)
b'\x00\x00\x80?\x00\x00\x00@\x00\x00@@'

# basic math
>>> c = a + b * 2.0
>>> c
(21.0, 22.0, 23.0)

# indexing
>>> c[0]
21.0

# element-wise multiplication
>>> a * a
(1.0, 4.0, 9.0)
length

float – length

normal

Vec3 – normal

tup

tuple – tuple

dot(rhs) → float
Parameters:rhs (Vec3) – rhs
Returns:dot product
Return type:float
cross(rhs) → float
Parameters:rhs (Vec3) – rhs
Returns:cross product
Return type:Vec3
reflect(norm) → Vec3
Parameters:norm (Vec3) – norm
Returns:reflect
Return type:Vec3
refract(norm, eta) → Vec3
Parameters:
  • norm (Vec3) – norm
  • eta (float) – eta
Returns:

refract

Return type:

Vec3

class Vec4(iterable)
# new instance
>>> a = Vec4([1.0, 2.0, 3.0, 4.0])
>>> a
(1.0, 2.0, 3.0, 4.0)

# length
>>> a.length
5.4772257804870605

>>> b = Vec4([10.0, 10.0, 10.0, 10.0])

# normal
>>> b.normal
(0.5, 0.5, 0.5, 0.5)

# dot product
>>> a.dot(b)
100.0

>>> light = Vec4([1.0, -2.0, 3.0, -4.0])
>>> up = Vec4([0.0, 0.0, 0.0, 1.0])

# reflect
>>> light.reflect(up)
(1.0, -2.0, 3.0, 4.0)

# refract
>>> light.refract(up, 0.0)
(0.0, -0.0, 0.0, -1.0)

# refract
>>> light.refract(up, 1.0)
(1.0, -2.0, 3.0, -4.0)

# unpack
>>> x, y, z, w = a

# convert to list
>>> list(a)
[1.0, 2.0, 3.0, 4.0]

# convert to bytes
>>> bytes(a)
b'\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@'

# basic math
>>> c = a + b * 2.0
>>> c
(21.0, 22.0, 23.0, 24.0)

# indexing
>>> c[1]
22.0

# element-wise multiplication
>>> a * a
(1.0, 4.0, 9.0, 16.0)
length

float – length

normal

Vec4 – normal

tup

tuple – tuple

dot(rhs) → float
Parameters:rhs (Vec4) – rhs
Returns:dot product
Return type:float
reflect(norm) → Vec4
Parameters:norm (Vec4) – norm
Returns:reflect
Return type:Vec4
refract(norm, eta) → Vec4
Parameters:
  • norm (Vec4) – norm
  • eta (float) – eta
Returns:

refract

Return type:

Vec4

class Mat2(iterable)
# new instance
>>> a = Mat2([
...     1.0, 2.0,
...     3.0, 4.0,
... ])

>>> a
(1.0, 2.0, 3.0, 4.0)

# transpose
>>> a.trans
(1.0, 3.0, 2.0, 4.0)

# determinant
>>> a.det
-2.0

# inverse
>>> a.inv
(-2.0, 1.0, 1.5, -0.5)

>>> b = Mat2([
...     1.0, 1.0,
...     0.0, 2.0,
... ])

# matrix * matrix
>>> a * b
(4.0, 6.0, 6.0, 8.0)

# matrix * vector
>>> v = Vec2([1.0, 0.0])
>>> a * v
(1.0, 2.0)

# matrix * float
>>> a * 2.5
(2.5, 5.0, 7.5, 10.0)

# rows
>>> a.row(0)
(1.0, 3.0)

# columns
>>> a.col(0)
(1.0, 2.0)

# bytes
>>> bytes(a)
b'\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@'

# indexing
>>> a[2]
3.0
trans

Mat2 – transpose

det

float – determinant

inv

Mat2 – inverse

tup

Mat2 – tuple

row(i) → Vec2
Parameters:i (int) – i
Returns:row
Return type:Vec2
col(i) → Vec2
Parameters:i (int) – i
Returns:column
Return type:Vec2
class Mat3(iterable)
# new instance
>>> a = Mat3([
...     0.0, 2.0, 0.0,
...     0.0, 0.0, 1.0,
...     1.0, 0.0, 0.0,
... ])

>>> a
(0.0, 2.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0)

# transpose
>>> a.trans
(0.0, 0.0, 1.0, 2.0, 0.0, 0.0, 0.0, 1.0, 0.0)

# determinant
>>> a.det
-2.0

# inverse
>>> a.inv
(0.0, -0.0, 1.0, 0.5, 0.0, -0.0, 0.0, 1.0, 0.0)

>>> b = Mat3([
...     1.0, 0.0, 0.0,
...     0.0, 2.0, 0.0,
...     0.0, 0.0, 3.0,
... ])

# matrix * matrix
>>> a * b
(0.0, 2.0, 0.0, 0.0, 0.0, 2.0, 3.0, 0.0, 0.0)

# matrix * vector
>>> v = Vec3([1.0, 0.0, 2.0])
>>> a * v
(2.0, 2.0, 0.0)

# matrix * float
>>> a * 2.5
(0.0, 5.0, 0.0, 0.0, 0.0, 2.5, 2.5, 0.0, 0.0)

# rows
>>> a.row(0)
(0.0, 0.0, 1.0)

# columns
>>> a.col(0)
(0.0, 2.0, 0.0)

# bytes
>>> bytes(a)
b'\x00\x00\x00\x00...\x00\x00\x00\x00'

# indexing
>>> a[6]
1.0

# new instance from a quaternion
>>> q = Quat([0.0, 0.0, 0.0, 1.0])
>>> m = Mat3(q)
>>> m
(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
trans

Mat3 – transpose

det

float – determinant

inv

Mat3 – inverse

tup

Mat3 – tuple

row(i) → Vec3
Parameters:i (int) – i
Returns:row
Return type:Vec3
col(i) → Vec3
Parameters:i (int) – i
Returns:column
Return type:Vec3
class Mat4(iterable)
# new instance
>>> a = Mat4([
...     0.0, 2.0, 0.0, 0.0,
...     0.0, 0.0, 1.0, 0.0,
...     1.0, 0.0, 0.0, 0.0,
...     0.0, 0.0, 0.0, 4.0,
... ])

>>> a
(0.0, 2.0, 0.0, 0.0,
 0.0, 0.0, 1.0, 0.0,
 1.0, 0.0, 0.0, 0.0,
 0.0, 0.0, 0.0, 4.0)

# transpose
>>> a.trans
(0.0, 0.0, 1.0, 0.0,
 2.0, 0.0, 0.0, 0.0,
 0.0, 1.0, 0.0, 0.0,
 0.0, 0.0, 0.0, 4.0)

# determinant
>>> a.det
8.0

# inverse
>>> a.inv
(0.0, -0.0, 1.0, -0.0,
 0.5, 0.0, -0.0, 0.0,
 0.0, 1.0, 0.0, -0.0,
-0.0, 0.0, -0.0, 0.25)

>>> b = Mat4([
...     1.0, 0.0, 0.0, 0.0,
...     0.0, 2.0, 0.0, 0.0,
...     0.0, 0.0, 3.0, 0.0,
...     0.0, 0.0, 0.0, 4.0,
... ])

# matrix * matrix
>>> a * b
(0.0, 2.0, 0.0, 0.0,
 0.0, 0.0, 2.0, 0.0,
 3.0, 0.0, 0.0, 0.0,
 0.0, 0.0, 0.0, 16.0)

# matrix * vector
>>> v = Vec4([1.0, 0.0, 2.0, 7.0])
>>> a * v
(2.0, 2.0, 0.0, 28.0)

# matrix * float
>>> a * 2.5
(0.0, 5.0, 0.0, 0.0,
 0.0, 0.0, 2.5, 0.0,
 2.5, 0.0, 0.0, 0.0,
 0.0, 0.0, 0.0, 10.0)

# rows
>>> a.row(0)
(0.0, 0.0, 1.0, 0.0)

# columns
>>> a.col(0)
(0.0, 2.0, 0.0, 0.0)

# bytes
>>> bytes(a)
b'\x00\x00\x00\x00...\x00\x00\x80@'

# indexing
>>> a[15]
4.0
trans

Mat4 – transpose

det

float – determinant

inv

Mat4 – inverse

tup

Mat4 – tuple

row(i) → Vec4
Parameters:i (int) – i
Returns:row
Return type:Vec4
col(i) → Vec4
Parameters:i (int) – i
Returns:column
Return type:Vec4
class Quat(iterable)
# new instance
>>> a = Quat([0.0, 0.0, 0.0, 1.0])
>>> a
(0.0, 0.0, 0.0, 1.0)

# new instance from a matrix
>>> m = Mat3([1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0])
>>> q = Quat(m)
>>> q
(0.0, 0.0, 0.0, 1.0)
length

float – length

normal

Quat – normal

conj

Quat – conj

inv

Quat – inv

axis

Vec3 – axis

angle

float – angle

tup

tuple – tup

dot(rhs) → float
Parameters:rhs (Quat) – rhs
Returns:dot product
Return type:float
cross(rhs) → Quat
Parameters:rhs (Quat) – rhs
Returns:cross product
Return type:Quat
slerp(rhs, coef) → Quat
Parameters:
  • rhs (Quat) – rhs
  • coef (float) – coefficient
Returns:

slerp

Return type:

Quat

lerp(rhs, coef) → Quat
Parameters:
  • rhs (Quat) – rhs
  • coef (float) – coefficient
Returns:

slerp

Return type:

Quat