Cython wrapper trial 3 seg-fault

fixes-turtlebasket
Daniel Song 2020-05-20 01:46:42 +01:00
parent b526fe1a9f
commit 20f60a18b0
5 changed files with 21478 additions and 15 deletions

Binary file not shown.

View File

@ -671,7 +671,6 @@ inline static void subalgorithm(struct simplex *s, double *v) {
} }
} }
#define DEBUG
double gjk(struct bd bd1, struct bd bd2, struct simplex *s) { double gjk(struct bd bd1, struct bd bd2, struct simplex *s) {

File diff suppressed because it is too large Load Diff

View File

@ -1,7 +1,9 @@
#cython: language_level=3, boundscheck=False #cython: language_level=3, boundscheck=False
import numpy as np
from libc.stdlib cimport free
import numpy as np
from libc.stdlib cimport free, malloc
# Declare C function and data
cdef extern from "openGJK.h": cdef extern from "openGJK.h":
struct bd: struct bd:
int numpoints int numpoints
@ -16,17 +18,27 @@ cdef extern from "openGJK.h":
double gjk(bd bd1, bd bd2, simplex *s) double gjk(bd bd1, bd bd2, simplex *s)
# Create Python function
def pygjk(bod1, bod2): def pygjk(bod1, bod2):
"""Returns distance between two bodies, input: array of nodal coordinates for each body"""
print("Hello!!") # Declare data types
cdef: cdef:
simplex ss simplex s
bd bd1 bd bd1
bd bd2 bd bd2
int i, j int i, j
double answer double answer
print("Break 1")#--------------------------------------------------
# Allocate memory for pointers
bd1.coord = <double**> malloc(sizeof(double)*bod1.ndim)
bd1.coord[0] = <double*> malloc(sizeof(double)*3)
bd2.coord = <double**> malloc(sizeof(double)*bod2.ndim)
bd2.coord[0] = <double*> malloc(sizeof(double)*3)
# 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)
if bod2.ndim < 2: if bod2.ndim < 2:
@ -35,26 +47,37 @@ def pygjk(bod1, bod2):
bd1.numpoints = np.size(bod1,0) bd1.numpoints = np.size(bod1,0)
bd2.numpoints = np.size(bod2,0) bd2.numpoints = np.size(bod2,0)
print("Break 2")#--------------------------------------------------
# Create numpy-array MemoryView
cdef: cdef:
double [:,:] narr1 = bod1 # create numpy-array MemoryView double [:,:] narr1 = bod1
double [:,:] narr2 = bod2 double [:,:] narr2 = bod2
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] = hex(id(bod1[i])) bd1.coord[i][0] = narr1[i,0]
bd1.coord[i][1] = narr1[i,1]
bd1.coord[i][2] = narr1[i,2]
for j in range(0, bd2.numpoints): for j in range(0, bd2.numpoints):
bd2.coord[i][0] = narr2[j,0] bd2.coord[j][0] = narr2[j,0]
bd2.coord[i][1] = narr2[j,1] bd2.coord[j][1] = narr2[j,1]
bd2.coord[i][2] = narr2[j,2] bd2.coord[j][2] = narr2[j,2]
answer = gjk(bd1, bd2, &ss) print("Break 4")#--------------------------------------------------
# Call C function
answer = gjk(bd1, bd2, &s)
# Free the memory
free(bd1.coord) free(bd1.coord)
free(bd2.coord) free(bd2.coord)
return answer return answer

5
Cython/pygjk_trial.py Normal file
View File

@ -0,0 +1,5 @@
import numpy as np
import openGJKpy as opengjk
a = np.array([[1.,1.,1.],[1.,1.,1.]])
b = np.array([[11.,1.,1.],[1.,1.,1.]])
d = opengjk.pygjk(a,b)