Hi there,
I am using Angus J's Polygonclipper in cython (or rather the cython wrapper "pyclipper":
https://pypi.python.org/pypi/pyclipper).
See
http://www.angusj.com/delphi/clipper.phpThe function in cython gets a polygon, then calculates some computaional extensive stuff and finally returns the area of the resulting polygon:
pyclipper_cython.pyxcimport cython
import numpy as np
cimport numpy as np
import pyclipper
pc = pyclipper.Pyclipper()
from libc.stdlib cimport malloc, free
DTYPE_I =
np.intctypedef np.int_t DTYPE_I_t
@cython.cdivision(True)
@cython.boundscheck(False)
@cython.wraparound(False)
def my_function(np.ndarray[DTYPE_I_t, ndim=2] polygon):
cdef int rows = polygon.shape[0]
cdef int columns = polygon.shape[1]
# found here:
http://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/ cdef int **solution_polygon = <int **>malloc(rows * sizeof(int *))
for i in range(rows):
solution_polygon[i] = <int *>malloc(columns * sizeof(int))
# some extensive operations ...
for i in range(rows):
for j in range(columns):
solution_polygon[i][j] = polygon[i, j] + 1
# for the sake of simplicity, let us just calculate the area of the polygon:
area = pyclipper.Area(solution_polygon)
as usually i call this funtion like this:
call_my_function.pyimport numpy as np
import pyximport
pyximport.install(setup_args={"include_dirs":np.get_include()}, reload_support=True)
import pyclipper_cython
my_polygon = np.array([[190, 210], [240, 210], [240, 130], [190, 130]], dtype=
np.int)
pyclipper_cython.my_funtion(my_polygon)
unfortunately this fails with the message:
Error compiling Cython file:
------------------------------------------------------------
...
for i in range(rows):
for j in range(columns):
solution_polygon[i][j] = polygon[i, j]+3
# for the sake of simplicity, let us just calculate the area of the polygon:
area = pyclipper.Area(solution_polygon)
^
------------------------------------------------------------
pyclipper_cython.pyx:35:43: Cannot convert 'int **' to Python objectAny suggestions on how i would be able to work with c-arrays?
many thanks!