Mercurial > touhou
comparison pytouhou/utils/maths.pyx @ 524:7f016dfbdfb1
Make vector a struct, allocate it directly on the stack, and thus pass it by copy, which is much less expensive than a python allocation.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 18 Dec 2013 18:15:40 +0100 |
parents | 6e3b3d5d4691 |
children |
comparison
equal
deleted
inserted
replaced
523:6e3b3d5d4691 | 524:7f016dfbdfb1 |
---|---|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 ## GNU General Public License for more details. | 12 ## GNU General Public License for more details. |
13 ## | 13 ## |
14 | 14 |
15 from libc.math cimport tan, M_PI as pi | 15 from libc.math cimport tan, M_PI as pi |
16 cimport cython | |
16 | 17 |
17 from .matrix cimport new_matrix, new_identity | 18 from .matrix cimport new_matrix, new_identity |
18 from .vector cimport Vector, normalize, cross, dot | 19 from .vector cimport Vector, sub, cross, dot, normalize |
19 | 20 |
20 | 21 |
21 cdef double radians(double degrees) nogil: | 22 cdef double radians(double degrees) nogil: |
22 return degrees * pi / 180 | 23 return degrees * pi / 180 |
23 | 24 |
24 | 25 |
25 cdef Matrix *ortho_2d(float left, float right, float bottom, float top): | 26 @cython.cdivision(True) |
27 cdef Matrix *ortho_2d(float left, float right, float bottom, float top) nogil: | |
26 mat = new_identity() | 28 mat = new_identity() |
27 data = <float*>mat | 29 data = <float*>mat |
28 data[4*0+0] = 2 / (right - left) | 30 data[4*0+0] = 2 / (right - left) |
29 data[4*1+1] = 2 / (top - bottom) | 31 data[4*1+1] = 2 / (top - bottom) |
30 data[4*2+2] = -1 | 32 data[4*2+2] = -1 |
34 | 36 |
35 | 37 |
36 cdef Matrix *look_at(Vector eye, Vector center, Vector up): | 38 cdef Matrix *look_at(Vector eye, Vector center, Vector up): |
37 cdef Matrix mat | 39 cdef Matrix mat |
38 | 40 |
39 f = normalize(center.sub(eye)) | 41 f = normalize(sub(center, eye)) |
40 u = normalize(up) | 42 u = normalize(up) |
41 s = normalize(cross(f, u)) | 43 s = normalize(cross(f, u)) |
42 u = cross(s, f) | 44 u = cross(s, f) |
43 | 45 |
44 mat = Matrix(s.x, u.x, -f.x, 0, | 46 mat = Matrix(s.x, u.x, -f.x, 0, |
47 -dot(s, eye), -dot(u, eye), dot(f, eye), 1) | 49 -dot(s, eye), -dot(u, eye), dot(f, eye), 1) |
48 | 50 |
49 return new_matrix(&mat) | 51 return new_matrix(&mat) |
50 | 52 |
51 | 53 |
52 cdef Matrix *perspective(float fovy, float aspect, float z_near, float z_far): | 54 @cython.cdivision(True) |
55 cdef Matrix *perspective(float fovy, float aspect, float z_near, float z_far) nogil: | |
53 top = tan(radians(fovy / 2)) * z_near | 56 top = tan(radians(fovy / 2)) * z_near |
54 bottom = -top | 57 bottom = -top |
55 left = -top * aspect | 58 left = -top * aspect |
56 right = top * aspect | 59 right = top * aspect |
57 | 60 |