Previous How to create a barycentre python module |
Parent How to create a barycentre python module |
Outline | Next The C++ module file |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/*************************************** Auteur : Pierre Aubert Mail : aubertp7@gmail.com Licence : CeCILL-C ****************************************/ #ifndef __BARYCENTRE_WRAPPER_H__ #define __BARYCENTRE_WRAPPER_H__ #include <Python.h> #include "structmember.h" PyObject * barycentreWrapper(PyObject *self, PyObject *args); #endif |
Now, let's write the barycentreWrapper.cpp file :
Again in this example, since we are using numpy in the module, we have to define the NO_IMPORT_ARRAY to avoid multiple definitions of the same numpy function. And we also have to specify the version of the numpy API to avoid warnings :
1 2 3 4 5 6 |
#define NO_IMPORT_ARRAY #ifndef DISABLE_COOL_ARRAY #define PY_ARRAY_UNIQUE_SYMBOL core_ARRAY_API #endif #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION |
1 2 3 4 5 6 |
#include <iostream> #include <numpy/arrayobject.h> #include <bytearrayobject.h> #include "barycentre_intrinsics.h" #include "barycentreWrapper.h" |
Here, we parse the output table and the two input tables with 'O' which corresponds to a PyObject type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
///Do the barycentre computation /** @param self : parent of the function if it exist * @param args : arguments passed to the function * @return result of the barycentre */ PyObject * barycentreWrapper(PyObject *self, PyObject *args){ PyArrayObject *objTabX = NULL, *objTabY = NULL, *objTabA = NULL; if(!PyArg_ParseTuple(args, "OOO", &objTabX, &objTabY, &objTabA)){ PyErr_SetString(PyExc_RuntimeError, "barycentreWrapper : wrong set of arguments. Expect tabX, tabY, tabA\n"); return NULL; } if(PyArray_NDIM(objTabX) != 1 || PyArray_NDIM(objTabY) != 1 || PyArray_NDIM(objTabA) != 1){ PyErr_SetString(PyExc_TypeError, "barycentreWrapper : input table must be a one dimension array"); return NULL; } if(PyArray_DIMS(objTabX)[0] != PyArray_DIMS(objTabY)[0] || PyArray_DIMS(objTabX)[0] != PyArray_DIMS(objTabA)[0]){ PyErr_SetString(PyExc_TypeError, "barycentreWrapper : input table must be of the same size"); return NULL; } long unsigned int sizeElement(PyArray_DIMS(objTabX)[0]); const float * tabX = (const float*)PyArray_DATA(objTabX); const float * tabY = (const float*)PyArray_DATA(objTabY); const float * tabA = (float*)PyArray_DATA(objTabA); float gx(0.0f), gy(0.0f); barycentre(gx, gy, tabX, tabY, tabA, sizeElement); return Py_BuildValue("ff", gx, gy); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
/*************************************** Auteur : Pierre Aubert Mail : aubertp7@gmail.com Licence : CeCILL-C ****************************************/ #define NO_IMPORT_ARRAY #ifndef DISABLE_COOL_ARRAY #define PY_ARRAY_UNIQUE_SYMBOL core_ARRAY_API #endif #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #include <iostream> #include <numpy/arrayobject.h> #include <bytearrayobject.h> #include "barycentre_intrinsics.h" #include "barycentreWrapper.h" ///Do the barycentre computation /** @param self : parent of the function if it exist * @param args : arguments passed to the function * @return result of the barycentre */ PyObject * barycentreWrapper(PyObject *self, PyObject *args){ PyArrayObject *objTabX = NULL, *objTabY = NULL, *objTabA = NULL; if(!PyArg_ParseTuple(args, "OOO", &objTabX, &objTabY, &objTabA)){ PyErr_SetString(PyExc_RuntimeError, "barycentreWrapper : wrong set of arguments. Expect tabX, tabY, tabA\n"); return NULL; } if(PyArray_NDIM(objTabX) != 1 || PyArray_NDIM(objTabY) != 1 || PyArray_NDIM(objTabA) != 1){ PyErr_SetString(PyExc_TypeError, "barycentreWrapper : input table must be a one dimension array"); return NULL; } if(PyArray_DIMS(objTabX)[0] != PyArray_DIMS(objTabY)[0] || PyArray_DIMS(objTabX)[0] != PyArray_DIMS(objTabA)[0]){ PyErr_SetString(PyExc_TypeError, "barycentreWrapper : input table must be of the same size"); return NULL; } long unsigned int sizeElement(PyArray_DIMS(objTabX)[0]); const float * tabX = (const float*)PyArray_DATA(objTabX); const float * tabY = (const float*)PyArray_DATA(objTabY); const float * tabA = (float*)PyArray_DATA(objTabA); float gx(0.0f), gy(0.0f); barycentre(gx, gy, tabX, tabY, tabA, sizeElement); return Py_BuildValue("ff", gx, gy); } |
Previous How to create a barycentre python module |
Parent How to create a barycentre python module |
Outline | Next The C++ module file |