annotate pytouhou/utils/maths.pyx @ 509:292fea5c584e

Some more type optimisations.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 25 Nov 2013 19:12:56 +0100
parents cb5c68598ab0
children 6e3b3d5d4691
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 ##
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 ## Copyright (C) 2013 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 ##
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 ##
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 ## GNU General Public License for more details.
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 ##
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14
436
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
15 from libc.math cimport tan, M_PI as pi
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16
436
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
17 from .vector cimport Vector, normalize, cross, dot
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19
436
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
20 cdef double radians(double degrees) nogil:
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
21 return degrees * pi / 180
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
22
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
23
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
24 cdef Matrix ortho_2d(float left, float right, float bottom, float top):
435
878273a984c4 Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
25 cdef float *data
878273a984c4 Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
26
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 mat = Matrix()
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28 data = mat.data
435
878273a984c4 Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
29 data[4*0+0] = 2 / (right - left)
878273a984c4 Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
30 data[4*1+1] = 2 / (top - bottom)
878273a984c4 Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
31 data[4*2+2] = -1
878273a984c4 Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
32 data[4*3+0] = -(right + left) / (right - left)
878273a984c4 Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
33 data[4*3+1] = -(top + bottom) / (top - bottom)
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 return mat
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36
436
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
37 cdef Matrix look_at(Vector eye, Vector center, Vector up):
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
38 f = normalize(center.sub(eye))
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 u = normalize(up)
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 s = normalize(cross(f, u))
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 u = cross(s, f)
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42
436
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
43 return Matrix([s.x, u.x, -f.x, 0,
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
44 s.y, u.y, -f.y, 0,
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
45 s.z, u.z, -f.z, 0,
435
878273a984c4 Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
46 -dot(s, eye), -dot(u, eye), dot(f, eye), 1])
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48
436
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
49 cdef Matrix perspective(float fovy, float aspect, float z_near, float z_far):
435
878273a984c4 Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
50 cdef float *data
878273a984c4 Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
51
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 top = tan(radians(fovy / 2)) * z_near
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 bottom = -top
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 left = -top * aspect
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 right = top * aspect
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 mat = Matrix()
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 data = mat.data
435
878273a984c4 Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
59 data[4*0+0] = (2 * z_near) / (right - left)
878273a984c4 Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
60 data[4*1+1] = (2 * z_near) / (top - bottom)
878273a984c4 Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
61 data[4*2+2] = -(z_far + z_near) / (z_far - z_near)
878273a984c4 Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
62 data[4*2+3] = -1
878273a984c4 Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
63 data[4*3+2] = -(2 * z_far * z_near) / (z_far - z_near)
878273a984c4 Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
64 data[4*3+3] = 0
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 return mat
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67
436
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
68 cdef Matrix setup_camera(float dx, float dy, float dz):
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 # Some explanations on the magic constants:
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 # 192. = 384. / 2. = width / 2.
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 # 224. = 448. / 2. = height / 2.
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2))
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 # This is so that objects on the (O, x, y) plane use pixel coordinates
436
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
74 return look_at(Vector(192., 224., - 835.979370 * dz),
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
75 Vector(192. + dx, 224. - dy, 0.), Vector(0., -1., 0.))