Cython wrapper final

fixes-turtlebasket
Daniel Song 2020-05-21 14:08:37 +01:00
parent d5dc76021f
commit a4f540a6c8
5 changed files with 708 additions and 646 deletions

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,7 @@ from libc.stdlib cimport free, malloc
from cpython.mem cimport PyMem_Malloc, PyMem_Free from cpython.mem cimport PyMem_Malloc, PyMem_Free
# Declare C function and data # Declare C function and data types
cdef extern from "openGJK.h": cdef extern from "openGJK.h":
struct bd: struct bd:
int numpoints int numpoints
@ -28,11 +28,8 @@ def pygjk(bod1, bod2):
simplex s simplex s
bd bd1 bd bd1
bd bd2 bd bd2
int i, j
double answer double answer
print("Break 1")#--------------------------------------------------
# Convert 1D array to 2D, if any # Convert 1D array to 2D, if any
if bod1.ndim < 2: if bod1.ndim < 2:
bod1 = np.append([bod1], [[1.,1.,1.]], axis = 0) bod1 = np.append([bod1], [[1.,1.,1.]], axis = 0)
@ -40,7 +37,6 @@ def pygjk(bod1, bod2):
else: else:
bd1.numpoints = np.size(bod1,0) bd1.numpoints = np.size(bod1,0)
print(bd1.numpoints)
if bod2.ndim < 2: if bod2.ndim < 2:
bod2 = np.append([bod2], [[1.,1.,1.]], axis = 0) bod2 = np.append([bod2], [[1.,1.,1.]], axis = 0)
@ -48,28 +44,26 @@ def pygjk(bod1, bod2):
else: else:
bd2.numpoints = np.size(bod2,0) bd2.numpoints = np.size(bod2,0)
print(bd2.numpoints)
# Allocate memory for pointer (not working) # Allocate memory for pointer (not working)
bd1.coord = <double **> malloc(bd1.numpoints * sizeof(double *)) bd1.coord = <double **> malloc(bd1.numpoints * sizeof(double *))
if not bd1.coord:
raise NameError('Not enough memory for bd1.coord')
bd2.coord = <double **> malloc(bd2.numpoints * sizeof(double *)) bd2.coord = <double **> malloc(bd2.numpoints * sizeof(double *))
if not bd2.coord:
raise NameError('Not enough memory for bd2.coord')
print("Break 2")#--------------------------------------------------
# Create numpy-array MemoryView # Create numpy-array MemoryView
cdef: cdef:
double [:,:] narr1 = bod1 double [:,:] narr1 = bod1
double [:,:] narr2 = bod2 double [:,:] narr2 = bod2
print(narr2[0,0]) # output a <double>, works fine # Assign coordinate values
print("Break 3")#--------------------------------------------------
# Assign coordinate values (Segmentation Fault Here!!, )
for i in range(0, bd1.numpoints): for i in range(0, bd1.numpoints):
bd1.coord[i] = <double *> malloc(3 * sizeof(double)) bd1.coord[i] = <double *> malloc(3 * sizeof(double))
if not bd1.coord[i]:
raise NameError('Not enough memory for bd1.coord[]')
bd1.coord[i][0] = narr1[i,0] bd1.coord[i][0] = narr1[i,0]
bd1.coord[i][1] = narr1[i,1] bd1.coord[i][1] = narr1[i,1]
bd1.coord[i][2] = narr1[i,2] bd1.coord[i][2] = narr1[i,2]
@ -77,21 +71,24 @@ def pygjk(bod1, bod2):
for j in range(0, bd2.numpoints): for j in range(0, bd2.numpoints):
bd2.coord[j] = <double *> malloc(3 * sizeof(double)) bd2.coord[j] = <double *> malloc(3 * sizeof(double))
if not bd2.coord[j]:
raise NameError('Not enough memory for bd2.coord[]')
bd2.coord[j][0] = narr2[j,0] bd2.coord[j][0] = narr2[j,0]
bd2.coord[j][1] = narr2[j,1] bd2.coord[j][1] = narr2[j,1]
bd2.coord[j][2] = narr2[j,2] bd2.coord[j][2] = narr2[j,2]
print("Break 4")#--------------------------------------------------
# Call C function # Call C function
answer = gjk(bd1, bd2, &s) answer = gjk(bd1, bd2, &s)
# Free the memory # Free the memory
for i in range(0, bd1.numpoints): for ii in range(0, bd1.numpoints):
free(bd1.coord[i]) free(bd1.coord[ii])
for j in range(0, bd2.numpoints): free(bd1.coord)
free(bd2.coord[j])
for jj in range(0, bd2.numpoints):
free(bd2.coord[jj])
free(bd2.coord)
return answer return answer

View File

@ -4,7 +4,9 @@ from Cython.Build import cythonize
exts = Extension( exts = Extension(
"openGJKpy", "openGJKpy",
sources = ["openGJK_cython.pyx", "openGJK.c"] sources = ["openGJK_cython.pyx", "openGJK.c"],
# extra_compile_args = ['-fopenmp'],
# extra_link_args = ['-fopenmp']
) )
setup(ext_modules = cythonize( [exts] )) setup(ext_modules = cythonize( [exts] ))

View File

@ -49,8 +49,7 @@ def test_line_line_distance(delta):
@pytest.mark.parametrize("delta", [0.1**(3*i) for i in range(6)]) @pytest.mark.parametrize("delta", [0.1**(3*i) for i in range(6)])
def test_tri_distance(delta): def test_tri_distance(delta):
tri_1 = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0]], dtype=np.float64) tri_1 = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0]], dtype=np.float64)
tri_2 = np.array([[1, delta, 0], [3, 1.2, 0], [ tri_2 = np.array([[1, delta, 0], [3, 1.2, 0], [1, 1, 0]], dtype=np.float64)
1, 1, 0]], dtype=np.float64)
P1 = tri_1[2] P1 = tri_1[2]
P2 = tri_1[1] P2 = tri_1[1]
point = tri_2[0] point = tri_2[0]