Mercurial > touhou
diff pytouhou/utils/matrix.pyx @ 131:fab7ad2f0d8b
Use Cython, improve performances!
author | Thibaut Girka <thib@sitedethib.com> |
---|---|
date | Sun, 11 Sep 2011 02:02:59 +0200 |
parents | pytouhou/utils/matrix.py@d1c82d43bbf3 |
children | 74471afbac37 |
line wrap: on
line diff
copy from pytouhou/utils/matrix.py copy to pytouhou/utils/matrix.pyx --- a/pytouhou/utils/matrix.py +++ b/pytouhou/utils/matrix.pyx @@ -12,35 +12,28 @@ ## GNU General Public License for more details. ## -#TODO: find/learn to use a proper lib +from libc.math cimport sin, cos + -from math import sin, cos - -class Matrix(object): - def __init__(self, data=None): +cdef class Matrix: + def __init__(Matrix self, data=None): self.data = data or [[0] * 4 for i in xrange(4)] - def mult(self, other_matrix): - d1 = self.data - d2 = other_matrix.data - return Matrix([[sum(d1[i][a] * d2[a][j] for a in xrange(4)) for j in xrange(4)] for i in xrange(4)]) - - - def flip(self): + cpdef flip(Matrix self): data = self.data a, b, c, d = data[0] data[0] = [-a, -b, -c, -d] - def scale(self, x, y, z): + cpdef scale(Matrix self, x, y, z): d1 = self.data d1[0] = [a * x for a in d1[0]] d1[1] = [a * y for a in d1[1]] d1[2] = [a * z for a in d1[2]] - def scale2d(self, x, y): + cpdef scale2d(Matrix self, x, y): data = self.data d1a, d1b, d1c, d1d = data[0] d2a, d2b, d2c, d2d = data[1] @@ -48,7 +41,7 @@ class Matrix(object): data[1] = [d2a * y, d2b * y, d2c * y, d2d * y] - def translate(self, x, y, z): + cpdef translate(Matrix self, x, y, z): data = self.data a, b, c = data[3][:3] a, b, c = a * x, b * y, c * z @@ -60,7 +53,7 @@ class Matrix(object): data[2] = [d3a + c, d3b + c, d3c + c, d3d + c] - def rotate_x(self, angle): + cpdef rotate_x(Matrix self, angle): d1 = self.data cos_a = cos(angle) sin_a = sin(angle) @@ -68,7 +61,7 @@ class Matrix(object): [sin_a * d1[1][i] + cos_a * d1[2][i] for i in range(4)]) - def rotate_y(self, angle): + cpdef rotate_y(Matrix self, angle): d1 = self.data cos_a = cos(angle) sin_a = sin(angle) @@ -76,50 +69,10 @@ class Matrix(object): [- sin_a * d1[0][i] + cos_a * d1[2][i] for i in range(4)]) - def rotate_z(self, angle): + cpdef rotate_z(Matrix self, angle): d1 = self.data cos_a = cos(angle) sin_a = sin(angle) d1[0], d1[1] = ([cos_a * d1[0][i] - sin_a * d1[1][i] for i in range(4)], [sin_a * d1[0][i] + cos_a * d1[1][i] for i in range(4)]) - - @classmethod - def get_translation_matrix(cls, x, y, z): - return cls([[1., 0., 0., x], - [0., 1., 0., y], - [0., 0., 1., z], - [0., 0., 0., 1.]]) - - - @classmethod - def get_scaling_matrix(cls, x, y, z): - return cls([[x, 0., 0., 0.], - [0., y, 0., 0.], - [0., 0., z, 0.], - [0., 0., 0., 1.]]) - - - @classmethod - def get_rotation_matrix(cls, angle, axis): - """Only handles axis = x, y or z.""" - cos_a = cos(angle) - sin_a = sin(angle) - if axis == 'x': - return Matrix([[ 1., 0., 0., 0.], - [ 0., cos_a, -sin_a, 0.], - [ 0., sin_a, cos_a, 0.], - [ 0., 0., 0., 1.]]) - elif axis == 'y': - return Matrix([[ cos_a, 0., sin_a, 0.], - [ 0., 1., 0., 0.], - [-sin_a, 0., cos_a, 0.], - [ 0., 0., 0., 1.]]) - elif axis == 'z': - return Matrix([[ cos_a, -sin_a, 0., 0.], - [ sin_a, cos_a, 0., 0.], - [ 0., 0., 1., 0.], - [ 0., 0., 0., 1.]]) - else: - raise Exception -