annotate pytouhou/utils/matrix.py @ 130:11ab06f4c4c6

Introduce characters!
author Thibaut Girka <thib@sitedethib.com>
date Sat, 10 Sep 2011 22:48:56 +0200
parents d1c82d43bbf3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 44
diff changeset
1 # -*- encoding: utf-8 -*-
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 44
diff changeset
2 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 44
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 44
diff changeset
4 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 44
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 44
diff changeset
6 ## it under the terms of the GNU General Public License as published
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 44
diff changeset
7 ## by the Free Software Foundation; version 3 only.
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 44
diff changeset
8 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 44
diff changeset
9 ## This program is distributed in the hope that it will be useful,
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 44
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 44
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 44
diff changeset
12 ## GNU General Public License for more details.
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 44
diff changeset
13 ##
ab826bc29aa2 Add some documentation, GPLv3 headers, README and COPYING file.
Thibaut Girka <thib@sitedethib.com>
parents: 44
diff changeset
14
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
15 #TODO: find/learn to use a proper lib
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
16
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17 from math import sin, cos
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
18
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
19 class Matrix(object):
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
20 def __init__(self, data=None):
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
21 self.data = data or [[0] * 4 for i in xrange(4)]
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
22
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
23
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
24 def mult(self, other_matrix):
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
25 d1 = self.data
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
26 d2 = other_matrix.data
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
27 return Matrix([[sum(d1[i][a] * d2[a][j] for a in xrange(4)) for j in xrange(4)] for i in xrange(4)])
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
29
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
30 def flip(self):
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
31 data = self.data
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
32 a, b, c, d = data[0]
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
33 data[0] = [-a, -b, -c, -d]
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
34
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
35
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
36 def scale(self, x, y, z):
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
37 d1 = self.data
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
38 d1[0] = [a * x for a in d1[0]]
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
39 d1[1] = [a * y for a in d1[1]]
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
40 d1[2] = [a * z for a in d1[2]]
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
41
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
42
31
55973a3f1222 Some more optimization!
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
43 def scale2d(self, x, y):
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
44 data = self.data
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
45 d1a, d1b, d1c, d1d = data[0]
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
46 d2a, d2b, d2c, d2d = data[1]
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
47 data[0] = [d1a * x, d1b * x, d1c * x, d1d * x]
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
48 data[1] = [d2a * y, d2b * y, d2c * y, d2d * y]
31
55973a3f1222 Some more optimization!
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
49
55973a3f1222 Some more optimization!
Thibaut Girka <thib@sitedethib.com>
parents: 30
diff changeset
50
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
51 def translate(self, x, y, z):
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
52 data = self.data
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
53 a, b, c = data[3][:3]
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
54 a, b, c = a * x, b * y, c * z
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
55 d1a, d1b, d1c, d1d = data[0]
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
56 d2a, d2b, d2c, d2d = data[1]
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
57 d3a, d3b, d3c, d3d = data[2]
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
58 data[0] = [d1a + a, d1b + a, d1c + a, d1d + a]
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
59 data[1] = [d2a + b, d2b + b, d2c + b, d2d + b]
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
60 data[2] = [d3a + c, d3b + c, d3c + c, d3d + c]
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
61
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
62
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
63 def rotate_x(self, angle):
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
64 d1 = self.data
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
65 cos_a = cos(angle)
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
66 sin_a = sin(angle)
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
67 d1[1], d1[2] = ([cos_a * d1[1][i] - sin_a * d1[2][i] for i in range(4)],
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
68 [sin_a * d1[1][i] + cos_a * d1[2][i] for i in range(4)])
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
69
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
70
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
71 def rotate_y(self, angle):
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
72 d1 = self.data
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
73 cos_a = cos(angle)
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
74 sin_a = sin(angle)
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
75 d1[0], d1[2] = ([cos_a * d1[0][i] + sin_a * d1[2][i] for i in range(4)],
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
76 [- sin_a * d1[0][i] + cos_a * d1[2][i] for i in range(4)])
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
77
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
78
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
79 def rotate_z(self, angle):
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
80 d1 = self.data
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
81 cos_a = cos(angle)
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
82 sin_a = sin(angle)
123
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
83 d1[0], d1[1] = ([cos_a * d1[0][i] - sin_a * d1[1][i] for i in range(4)],
d1c82d43bbf3 Various optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 52
diff changeset
84 [sin_a * d1[0][i] + cos_a * d1[1][i] for i in range(4)])
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
85
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
86
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
87 @classmethod
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
88 def get_translation_matrix(cls, x, y, z):
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
89 return cls([[1., 0., 0., x],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
90 [0., 1., 0., y],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
91 [0., 0., 1., z],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
92 [0., 0., 0., 1.]])
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
93
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
94
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
95 @classmethod
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
96 def get_scaling_matrix(cls, x, y, z):
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
97 return cls([[x, 0., 0., 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
98 [0., y, 0., 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
99 [0., 0., z, 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
100 [0., 0., 0., 1.]])
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
101
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
102
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
103 @classmethod
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
104 def get_rotation_matrix(cls, angle, axis):
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
105 """Only handles axis = x, y or z."""
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
106 cos_a = cos(angle)
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
107 sin_a = sin(angle)
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
108 if axis == 'x':
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
109 return Matrix([[ 1., 0., 0., 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
110 [ 0., cos_a, -sin_a, 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
111 [ 0., sin_a, cos_a, 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
112 [ 0., 0., 0., 1.]])
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
113 elif axis == 'y':
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
114 return Matrix([[ cos_a, 0., sin_a, 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
115 [ 0., 1., 0., 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
116 [-sin_a, 0., cos_a, 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
117 [ 0., 0., 0., 1.]])
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
118 elif axis == 'z':
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
119 return Matrix([[ cos_a, -sin_a, 0., 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
120 [ sin_a, cos_a, 0., 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
121 [ 0., 0., 1., 0.],
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
122 [ 0., 0., 0., 1.]])
4
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
123 else:
787d2eb13c2d Add buggy stageviewer!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
124 raise Exception
28
f405b947624d Massive sprite updating/matrix handling optimizations
Thibaut Girka <thib@sitedethib.com>
parents: 4
diff changeset
125