openGJK/Cython/openGJK_cython.pyx

61 lines
1.1 KiB
Cython
Raw Normal View History

2020-05-18 16:54:28 -07:00
#cython: language_level=3, boundscheck=False
import numpy as np
2020-05-19 09:44:27 -07:00
from libc.stdlib cimport free
2020-05-18 16:54:28 -07:00
cdef extern from "openGJK.h":
struct bd:
int numpoints
double s[3]
double** coord
struct simplex:
int nvrtx
double vrtx[4][3]
int wids[4]
double lambdas[4]
double gjk(bd bd1, bd bd2, simplex *s)
def pygjk(bod1, bod2):
"""Returns distance between two bodies, input: array of nodal coordinates for each body"""
2020-05-19 09:44:27 -07:00
print("Hello!!")
2020-05-18 16:54:28 -07:00
cdef:
2020-05-19 09:44:27 -07:00
simplex ss
2020-05-18 16:54:28 -07:00
bd bd1
bd bd2
int i, j
2020-05-19 09:44:27 -07:00
double answer
2020-05-18 16:54:28 -07:00
2020-05-19 09:44:27 -07:00
if bod1.ndim < 2:
bod1 = np.append([bod1], [[1.,1.,1.]], axis = 0)
if bod2.ndim < 2:
bod2 = np.append([bod2], [[1.,1.,1.]], axis = 0)
2020-05-18 16:54:28 -07:00
2020-05-19 09:44:27 -07:00
bd1.numpoints = np.size(bod1,0)
bd2.numpoints = np.size(bod2,0)
cdef:
double [:,:] narr1 = bod1 # create numpy-array MemoryView
double [:,:] narr2 = bod2
2020-05-18 16:54:28 -07:00
for i in range(0, bd1.numpoints):
2020-05-19 09:44:27 -07:00
bd1.coord[i] = hex(id(bod1[i]))
2020-05-18 16:54:28 -07:00
for j in range(0, bd2.numpoints):
2020-05-19 09:44:27 -07:00
bd2.coord[i][0] = narr2[j,0]
bd2.coord[i][1] = narr2[j,1]
bd2.coord[i][2] = narr2[j,2]
answer = gjk(bd1, bd2, &ss)
2020-05-18 16:54:28 -07:00
2020-05-19 09:44:27 -07:00
free(bd1.coord)
free(bd2.coord)
2020-05-18 16:54:28 -07:00
2020-05-19 09:44:27 -07:00
return answer
2020-05-18 16:54:28 -07:00