diff --git a/.gitignore b/.gitignore index 1ffe0e0..eccc0a4 100644 --- a/.gitignore +++ b/.gitignore @@ -176,7 +176,42 @@ sysinfo.txt # Office ~*.pptx +#Cython https://github.com/cython/cython/blob/master/.gitignore +*.pyc +*.pyo +__pycache__ +*.o + +.*cache*/ + +*/cython/*.c + +/TEST_TMP/ +/build/ +/cython_build/ +/wheelhouse*/ +!tests/build/ +/dist/ +.gitrev +.coverage +*.patch +*.diff +*.orig +*.prof +*.rej +*.log +*.dep +*.swp +*~ +callgrind.out.* + +.ipynb_checkpoints + +# Jetbrains IDE project files +/.idea +/*.iml + # Pytest .cache .pytest_cache -.mypy_cache \ No newline at end of file +.mypy_cache diff --git a/CMakeLists.txt b/CMakeLists.txt index e5d6913..e28ac70 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,21 +9,21 @@ # # # This file is part of openGJK. # # # -# openGJK is free software: you can redistribute it and/or modify # +# OpenGJK is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # any later version. # # # -# openGJK is distributed in the hope that it will be useful, # +# OpenGJK is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See The # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # -# along with openGJK. If not, see . # +# along with OpenGJK. If not, see . # # # # openGJK: open-source Gilbert-Johnson-Keerthi algorithm # -# Copyright (C) Mattia Montanari 2018 - 2019 # +# Copyright (C) Mattia Montanari 2018 - 2020 # # http://iel.eng.ox.ac.uk/?page_id=504 # # # # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # diff --git a/examples/cython/openGJK_cython.pxd b/examples/cython/openGJK_cython.pxd new file mode 100644 index 0000000..ea7a1ae --- /dev/null +++ b/examples/cython/openGJK_cython.pxd @@ -0,0 +1,44 @@ +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# ##### # # # # +# #### ##### ###### # # # # # # # # +# # # # # # ## # # # # # # +# # # # # ##### # # # # #### # ### # +# # # ##### # # # # # # # # # # # +# # # # # # ## # # # # # # # +# #### # ###### # # ##### ##### # # # +# # +# This file is part of openGJK. # +# # +# OpenGJK is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# any later version. # +# # +# OpenGJK is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See The # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with OpenGJK. If not, see . # +# # +# openGJK: open-source Gilbert-Johnson-Keerthi algorithm # +# Copyright (C) Mattia Montanari 2018 - 2020 # +# http://iel.eng.ox.ac.uk/?page_id=504 # +# # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +# Declare C function and data types +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) diff --git a/examples/cython/openGJK_cython.pyx b/examples/cython/openGJK_cython.pyx new file mode 100644 index 0000000..4ee60e5 --- /dev/null +++ b/examples/cython/openGJK_cython.pyx @@ -0,0 +1,111 @@ +#!python +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# ##### # # # # +# #### ##### ###### # # # # # # # # +# # # # # # ## # # # # # # +# # # # # ##### # # # # #### # ### # +# # # ##### # # # # # # # # # # # +# # # # # # ## # # # # # # # +# #### # ###### # # ##### ##### # # # +# # +# This file is part of openGJK. # +# # +# OpenGJK is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# any later version. # +# # +# OpenGJK is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See The # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with OpenGJK. If not, see . # +# # +# openGJK: open-source Gilbert-Johnson-Keerthi algorithm # +# Copyright (C) Mattia Montanari 2018 - 2020 # +# http://iel.eng.ox.ac.uk/?page_id=504 # +# # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +# cython: language_level=3 +# distutils: sources = ../../src/openGJK.c +# distutils: include_dirs = ../../include/openGJK + +cimport openGJK_cython + +import numpy as np +from libc.stdlib cimport free, malloc +from cpython.mem cimport PyMem_Malloc, PyMem_Free + +# Create Python function +def pygjk(bod1, bod2): + + # Declare data types + cdef: + simplex s + bd bd1 + bd bd2 + double dist2 + + # Convert 1D array to 2D, if any + if bod1.ndim < 2: + bod1 = np.append([bod1], [[1.,1.,1.]], axis = 0) + bd1.numpoints = np.size(bod1,0) - 1 + else: + bd1.numpoints = np.size(bod1,0) + + + if bod2.ndim < 2: + bod2 = np.append([bod2], [[1.,1.,1.]], axis = 0) + bd2.numpoints = np.size(bod2,0) - 1 + else: + bd2.numpoints = np.size(bod2,0) + + + # Allocate memory for bodies + bd1.coord = malloc(bd1.numpoints * sizeof(double *)) + if not bd1.coord: + raise NameError('Not enough memory for bd1.coord') + for i in range(0, bd1.numpoints): + bd1.coord[i] = malloc(3 * sizeof(double)) + if not bd1.coord[i]: + raise NameError('Not enough memory for bd1.coord[]') + + bd2.coord = malloc(bd2.numpoints * sizeof(double *)) + if not bd2.coord: + raise NameError('Not enough memory for bd2.coord') + for j in range(0, bd2.numpoints): + bd2.coord[j] = malloc(3 * sizeof(double)) + if not bd2.coord[j]: + raise NameError('Not enough memory for bd2.coord[]') + + # Create numpy-array MemoryView + cdef: + double [:,:] narr1 = bod1 + double [:,:] narr2 = bod2 + + # Assign coordinate values + for i in range(0, bd1.numpoints): + for j in range(0,3): + bd1.coord[i][j] = narr1[i,j] + + for i in range(0, bd2.numpoints): + for j in range(0,3): + bd2.coord[i][j] = narr2[i,j] + + # Call C function + dist2 = gjk(bd1, bd2, &s) + + # Free the memory + for ii in range(0, bd1.numpoints): + free(bd1.coord[ii]) + free(bd1.coord) + + for jj in range(0, bd2.numpoints): + free(bd2.coord[jj]) + free(bd2.coord) + + return dist2 + diff --git a/examples/cython/pygjk_trial.py b/examples/cython/pygjk_trial.py new file mode 100644 index 0000000..3b68b7a --- /dev/null +++ b/examples/cython/pygjk_trial.py @@ -0,0 +1,38 @@ +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# ##### # # # # +# #### ##### ###### # # # # # # # # +# # # # # # ## # # # # # # +# # # # # ##### # # # # #### # ### # +# # # ##### # # # # # # # # # # # +# # # # # # ## # # # # # # # +# #### # ###### # # ##### ##### # # # +# # +# This file is part of openGJK. # +# # +# OpenGJK is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# any later version. # +# # +# OpenGJK is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See The # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with OpenGJK. If not, see . # +# # +# openGJK: open-source Gilbert-Johnson-Keerthi algorithm # +# Copyright (C) Mattia Montanari 2018 - 2020 # +# http://iel.eng.ox.ac.uk/?page_id=504 # +# # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +import numpy as np +import openGJK_cython 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) + +print(d) \ No newline at end of file diff --git a/examples/cython/setup.py b/examples/cython/setup.py new file mode 100644 index 0000000..801ff67 --- /dev/null +++ b/examples/cython/setup.py @@ -0,0 +1,44 @@ +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # +# ##### # # # # +# #### ##### ###### # # # # # # # # +# # # # # # ## # # # # # # +# # # # # ##### # # # # #### # ### # +# # # ##### # # # # # # # # # # # +# # # # # # ## # # # # # # # +# #### # ###### # # ##### ##### # # # +# # +# This file is part of openGJK. # +# # +# OpenGJK is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# any later version. # +# # +# OpenGJK is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See The # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with OpenGJK. If not, see . # +# # +# openGJK: open-source Gilbert-Johnson-Keerthi algorithm # +# Copyright (C) Mattia Montanari 2018 - 2020 # +# http://iel.eng.ox.ac.uk/?page_id=504 # +# # +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # + +from setuptools import Extension, setup +from Cython.Build import cythonize + +exts = Extension( + "openGJK_cython", + sources = ["openGJK_cython.pyx"], + extra_compile_args=['-I../../include/','-fopenmp'], + extra_link_args=['-fopenmp'], +) + +setup( + name='openGJK-cython-version', + ext_modules = cythonize( [exts] ) +) \ No newline at end of file