changeset 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 43ecf0f98f4d
files pytouhou/utils/maths.pxd pytouhou/utils/maths.pyx pytouhou/utils/vector.pxd pytouhou/utils/vector.pyx
diffstat 4 files changed, 20 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/utils/maths.pxd
+++ b/pytouhou/utils/maths.pxd
@@ -1,5 +1,5 @@
 from .matrix cimport Matrix
 
-cdef Matrix *ortho_2d(float left, float right, float bottom, float top)
-cdef Matrix *perspective(float fovy, float aspect, float zNear, float zFar)
+cdef Matrix *ortho_2d(float left, float right, float bottom, float top) nogil
+cdef Matrix *perspective(float fovy, float aspect, float zNear, float zFar) nogil
 cdef Matrix *setup_camera(float dx, float dy, float dz)
--- a/pytouhou/utils/maths.pyx
+++ b/pytouhou/utils/maths.pyx
@@ -13,16 +13,18 @@
 ##
 
 from libc.math cimport tan, M_PI as pi
+cimport cython
 
 from .matrix cimport new_matrix, new_identity
-from .vector cimport Vector, normalize, cross, dot
+from .vector cimport Vector, sub, cross, dot, normalize
 
 
 cdef double radians(double degrees) nogil:
     return degrees * pi / 180
 
 
-cdef Matrix *ortho_2d(float left, float right, float bottom, float top):
+@cython.cdivision(True)
+cdef Matrix *ortho_2d(float left, float right, float bottom, float top) nogil:
     mat = new_identity()
     data = <float*>mat
     data[4*0+0] = 2 / (right - left)
@@ -36,7 +38,7 @@ cdef Matrix *ortho_2d(float left, float 
 cdef Matrix *look_at(Vector eye, Vector center, Vector up):
     cdef Matrix mat
 
-    f = normalize(center.sub(eye))
+    f = normalize(sub(center, eye))
     u = normalize(up)
     s = normalize(cross(f, u))
     u = cross(s, f)
@@ -49,7 +51,8 @@ cdef Matrix *look_at(Vector eye, Vector 
     return new_matrix(&mat)
 
 
-cdef Matrix *perspective(float fovy, float aspect, float z_near, float z_far):
+@cython.cdivision(True)
+cdef Matrix *perspective(float fovy, float aspect, float z_near, float z_far) nogil:
     top = tan(radians(fovy / 2)) * z_near
     bottom = -top
     left = -top * aspect
--- a/pytouhou/utils/vector.pxd
+++ b/pytouhou/utils/vector.pxd
@@ -1,8 +1,7 @@
-cdef class Vector:
-    cdef float x, y, z
+cdef struct Vector:
+    float x, y, z
 
-    cdef Vector sub(self, Vector other)
-
+cdef Vector sub(Vector vec1, Vector vec2)
 cdef Vector cross(Vector vec1, Vector vec2)
-cdef float dot(Vector vec1, Vector vec2)
+cdef float dot(Vector vec1, Vector vec2) nogil
 cdef Vector normalize(Vector vec)
--- a/pytouhou/utils/vector.pyx
+++ b/pytouhou/utils/vector.pyx
@@ -13,23 +13,13 @@
 ##
 
 from libc.math cimport sqrt
-
-
-cdef class Vector:
-    def __init__(self, float x, float y, float z):
-        self.x = x
-        self.y = y
-        self.z = z
+cimport cython
 
 
-    cdef Vector sub(self, Vector other):
-        cdef float x, y, z
-
-        x = self.x - other.x
-        y = self.y - other.y
-        z = self.z - other.z
-
-        return Vector(x, y, z)
+cdef Vector sub(Vector vec1, Vector vec2):
+    return Vector(vec1.x - vec2.x,
+                  vec1.y - vec2.y,
+                  vec1.z - vec2.z)
 
 
 cdef Vector cross(Vector vec1, Vector vec2):
@@ -38,10 +28,11 @@ cdef Vector cross(Vector vec1, Vector ve
                   vec1.x * vec2.y - vec2.x * vec1.y)
 
 
-cdef float dot(Vector vec1, Vector vec2):
+cdef float dot(Vector vec1, Vector vec2) nogil:
     return vec1.x * vec2.x + vec2.y * vec1.y + vec1.z * vec2.z
 
 
+@cython.cdivision(True)
 cdef Vector normalize(Vector vec):
     cdef float normal