Refactored some Mat4 class methods to module level functions.
import math as _math
+from functools import lru_cache as _lru_cache
-class Mat4(tuple):
- """A 4x4 Matrix
-
- `Mat4` is a simple immutable 4x4 Matrix, with a few operators.
- Two types of multiplication are possible. The "*" operator
- will perform elementwise multiplication, wheras the "@"
- operator will perform Matrix multiplication. Internally
- data is stored in a linear 1D array, allowing direct passing
- to OpenGL.
- """
-
- def __new__(cls, array=None):
- """Create a 4x4 Matrix
-
- A Matrix can be created with list or tuple of 16 values.
- If nothing is provided, an "identity Matrix" will be created
- (1.0 on the main diagonal). Matrix objects are immutable.
-
- :Parameters:
- `array` : tuple of float or int
- A tuple containing 16 values.
- """
- assert array is None or len(array) == 16, "Mat4 requires 16 values"
- array = array or (1.0, 0.0, 0.0, 0.0,
- 0.0, 1.0, 0.0, 0.0,
- 0.0, 0.0, 1.0, 0.0,
- 0.0, 0.0, 0.0, 1.0)
- return super().__new__(Mat4, array)
-
- @classmethod
- def create_orthogonal(cls, left, right, bottom, top, znear, zfar):
+@_lru_cache()
+def create_orthogonal(left, right, bottom, top, znear, zfar):
width = right - left
height = top - bottom
depth = zfar - znear
@@ -49,8 +21,9 @@ class Mat4(tuple):
0.0, 0.0, sz, 0.0,
tx, ty, tz, 1.0))
- @classmethod
- def create_perspective(cls, left, right, bottom, top, znear, zfar, fov=60):
+
+@_lru_cache()
+def create_perspective(left, right, bottom, top, znear, zfar, fov=60):
width = right - left
height = top - bottom
@@ -75,6 +48,36 @@ class Mat4(tuple):
0, 0, q, -1,
0, 0, qn, 0))
+
+class Mat4(tuple):
+ """A 4x4 Matrix
+
+ `Mat4` is a simple immutable 4x4 Matrix, with a few operators.
+ Two types of multiplication are possible. The "*" operator
+ will perform elementwise multiplication, wheras the "@"
+ operator will perform Matrix multiplication. Internally
+ data is stored in a linear 1D array, allowing direct passing
+ to OpenGL.
+ """
+
+ def __new__(cls, array=None):
+ """Create a 4x4 Matrix
+
+ A Matrix can be created with list or tuple of 16 values.
+ If nothing is provided, an "identity Matrix" will be created
+ (1.0 on the main diagonal). Matrix objects are immutable.
+
+ :Parameters:
+ `array` : tuple of float or int
+ A tuple containing 16 values.
+ """
+ assert array is None or len(array) == 16, "Mat4 requires 16 values"
+ array = array or (1.0, 0.0, 0.0, 0.0,
+ 0.0, 1.0, 0.0, 0.0,
+ 0.0, 0.0, 1.0, 0.0,
+ 0.0, 0.0, 0.0, 1.0)
+ return super().__new__(Mat4, array)
+
def translate(self, x=0, y=0, z=0):
return Mat4((*self[:12], self[12] + x, self[13] + y, self[14] + z, self[15]))
