forked from marcan/blitzloop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.py
More file actions
133 lines (118 loc) · 4.87 KB
/
matrix.py
File metadata and controls
133 lines (118 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2012-2017 Hector Martin "marcan" <hector@marcansoft.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 or version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
class Matrix(object):
def __init__(self, other=None, nice=None):
self.stack = []
if other is not None:
if isinstance(other, list):
self.m = other
self.nice = False
else:
self.m = list(other.m)
self.nice = other.nice
else:
self.reset()
if nice is not None:
self.nice = nice
def reset(self):
self.m = [
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
]
self.nice = True
def push(self):
self.stack.append((self.m, self.nice))
self.m = list(self.m)
def pop(self):
self.m, self.nice = self.stack.pop()
def scale(self, sx, sy, sz=1.0):
if not self.nice:
self *= Matrix().scale(sx, sy, sz)
return self
else:
self.m[0] *= sx
self.m[5] *= sy
self.m[10] *= sz
return self
def translate(self, tx, ty, tz=0.0):
if not self.nice:
self *= Matrix().translate(tx, ty, tz)
return self
self.m[12] += tx * self.m[0]
self.m[13] += ty * self.m[5]
self.m[14] += tz * self.m[10]
return self
def __mul__(self, other):
if self.nice and other.nice:
return Matrix(self._nicemult(self.m, other.m), True)
else:
return Matrix(self._mult(self.m, other.m), False)
def __imul__(self, other):
if self.nice and other.nice:
self.m = self._nicemult(self.m, other.m)
else:
self.m = self._mult(self.m, other.m)
self.nice = False
return self
@staticmethod
def _mult(B, A):
C = [
A[0] * B[0] + A[1] * B[4] + A[2] * B[8] + A[3] * B[12],
A[0] * B[1] + A[1] * B[5] + A[2] * B[9] + A[3] * B[13],
A[0] * B[2] + A[1] * B[6] + A[2] * B[10] + A[3] * B[14],
A[0] * B[3] + A[1] * B[7] + A[2] * B[11] + A[3] * B[15],
A[4] * B[0] + A[5] * B[4] + A[6] * B[8] + A[7] * B[12],
A[4] * B[1] + A[5] * B[5] + A[6] * B[9] + A[7] * B[13],
A[4] * B[2] + A[5] * B[6] + A[6] * B[10] + A[7] * B[14],
A[4] * B[3] + A[5] * B[7] + A[6] * B[11] + A[7] * B[15],
A[8] * B[0] + A[9] * B[4] + A[10] * B[8] + A[11] * B[12],
A[8] * B[1] + A[9] * B[5] + A[10] * B[9] + A[11] * B[13],
A[8] * B[2] + A[9] * B[6] + A[10] * B[10] + A[11] * B[14],
A[8] * B[3] + A[9] * B[7] + A[10] * B[11] + A[11] * B[15],
A[12] * B[0] + A[13] * B[4] + A[14] * B[8] + A[15] * B[12],
A[12] * B[1] + A[13] * B[5] + A[14] * B[9] + A[15] * B[13],
A[12] * B[2] + A[13] * B[6] + A[14] * B[10] + A[15] * B[14],
A[12] * B[3] + A[13] * B[7] + A[14] * B[11] + A[15] * B[15],
]
return C
@staticmethod
def _nicemult(B, A):
C = [
A[0] * B[0], 0.0, 0.0, 0.0,
0.0, A[5] * B[5], 0.0, 0.0,
0.0, 0.0, A[10] * B[10], 0.0,
A[12] * B[0] + B[12], A[13] * B[5] + B[13], A[14] * B[10] + B[14], 1.0,
]
return C
def __str__(self):
s = ""
for i in range(4):
s += " [" + ",".join("%d" % self.m[4*j+i] for j in range(4)) + "],\n"
return "[\n" + s + "]\n"
def __eq__(self, other):
return self.m == other.m
if __name__ == "__main__":
assert Matrix().translate(1,2).translate(3,4) == Matrix().translate(4,6)
assert Matrix().scale(1,2).scale(3,4) == Matrix().scale(3,8)
assert Matrix().scale(2,3).translate(4,5) == Matrix().translate(8,15).scale(2,3)
assert Matrix().scale(2,3).translate(4,5) == Matrix().scale(2,3) * Matrix().translate(4,5)
assert Matrix(nice=False).translate(1,2).translate(3,4) == Matrix().translate(4,6)
assert Matrix(nice=False).scale(1,2).scale(3,4) == Matrix().scale(3,8)
assert Matrix(nice=False).scale(2,3).translate(4,5) == Matrix().translate(8,15).scale(2,3)
assert Matrix(nice=False).scale(2,3).translate(4,5) == Matrix().scale(2,3) * Matrix().translate(4,5)