annotate pytouhou/utils/vector.pyx @ 436:cb5c68598ab0

Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 07 Aug 2013 11:34:42 +0200
parents pytouhou/utils/vector.py@74471afbac37
children 7f016dfbdfb1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 ##
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 ## Copyright (C) 2012 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 ##
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 ##
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 ## GNU General Public License for more details.
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 ##
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
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: 370
diff changeset
15 from libc.math cimport sqrt
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17
436
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
18 cdef class Vector:
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
19 def __init__(self, float x, float y, float z):
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
20 self.x = x
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
21 self.y = y
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
22 self.z = z
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24
436
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
25 cdef Vector sub(self, Vector other):
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
26 cdef float x, y, z
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
27
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
28 x = self.x - other.x
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
29 y = self.y - other.y
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
30 z = self.z - other.z
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
31
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
32 return Vector(x, y, z)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34
436
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
35 cdef Vector cross(Vector vec1, Vector vec2):
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
36 return Vector(vec1.y * vec2.z - vec2.y * vec1.z,
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
37 vec1.z * vec2.x - vec2.z * vec1.x,
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
38 vec1.x * vec2.y - vec2.x * vec1.y)
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40
436
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
41 cdef float dot(Vector vec1, Vector vec2):
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
42 return vec1.x * vec2.x + vec2.y * vec1.y + vec1.z * vec2.z
370
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43
74471afbac37 Add a programmable pipeline renderer, and a --fixed-pipeline switch to use the old one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44
436
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
45 cdef Vector normalize(Vector vec):
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
46 cdef float normal
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
47
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
48 normal = 1 / sqrt(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z)
cb5c68598ab0 Cythonize pytouhou.utils.maths and pytouhou.utils.vector.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 370
diff changeset
49 return Vector(vec.x * normal, vec.y * normal, vec.z * normal)