annotate pytouhou/utils/maths.pyx @ 523:6e3b3d5d4691

Make matrix a struct.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 18 Dec 2013 17:53:29 +0100
parents cb5c68598ab0
children 7f016dfbdfb1
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
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 436
diff changeset
17 from .matrix cimport new_matrix, new_identity
436
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
18 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
19
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20
436
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
21 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
22 return degrees * pi / 180
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
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 436
diff changeset
25 cdef Matrix *ortho_2d(float left, float right, float bottom, float top):
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 436
diff changeset
26 mat = new_identity()
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 436
diff changeset
27 data = <float*>mat
435
878273a984c4 Improve Matrix representation, using float[16] instead of imbricated python lists.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 412
diff changeset
28 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
29 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
30 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
31 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
32 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
33 return mat
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 436
diff changeset
36 cdef Matrix *look_at(Vector eye, Vector center, Vector up):
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 436
diff changeset
37 cdef Matrix mat
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 436
diff changeset
38
436
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
39 f = normalize(center.sub(eye))
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 u = normalize(up)
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 s = normalize(cross(f, u))
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 u = cross(s, f)
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 436
diff changeset
44 mat = Matrix(s.x, u.x, -f.x, 0,
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 436
diff changeset
45 s.y, u.y, -f.y, 0,
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 436
diff changeset
46 s.z, u.z, -f.z, 0,
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 436
diff changeset
47 -dot(s, eye), -dot(u, eye), dot(f, eye), 1)
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 436
diff changeset
48
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 436
diff changeset
49 return new_matrix(&mat)
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 436
diff changeset
52 cdef Matrix *perspective(float fovy, float aspect, float z_near, float z_far):
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 top = tan(radians(fovy / 2)) * z_near
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 bottom = -top
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 left = -top * aspect
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 right = top * aspect
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 436
diff changeset
58 mat = new_identity()
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 436
diff changeset
59 data = <float*>mat
435
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*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
61 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
62 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
63 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
64 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
65 data[4*3+3] = 0
412
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 return mat
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 436
diff changeset
69 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
70 # Some explanations on the magic constants:
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 # 192. = 384. / 2. = width / 2.
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 # 224. = 448. / 2. = height / 2.
5fe6cd6ceb48 Refactor the maths functions out of Renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 # 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
74 # 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
75 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
76 Vector(192. + dx, 224. - dy, 0.), Vector(0., -1., 0.))