static PyObject *uflow_inference2(PyObject *self, PyObject *args) { /* * In python you call it like this: * * matrix-2d matrix-2d matrix-3d integer string array-1d * labelResult* uflow_inference2( sourceEdgeCosts, sinkEdgeCosts, inputImage, nhoodSize, nbrEdgeCostMethod, callbackParams ) * * where callback fn has this signature: * * float edgeCallback( floatImage3DMat, pixelRowInt, pixelColInt, pixelNbrRowInt, pixelNbrColInt, floatParams1DArray ) */ PyArrayObject *matSourceEdge, *matSinkEdge, *matInputImage, *arrCallbackParams, *matOut; double *cMatSourceEdge, *cMatSinkEdge, *cCallbackParams, *cMatInputImage; int *cMatOut; // callback? int nhoodSize; int rows, cols, dims[2];/* Parse tuples separately since args will differ between C fcns */ const char* nbrEdgeCostMethod; if (!PyArg_ParseTuple(args, "O!O!O!isO!", &PyArray_Type, &matSourceEdge, &PyArray_Type, &matSinkEdge, &PyArray_Type, &matInputImage, &nhoodSize, &nbrEdgeCostMethod, &PyArray_Type, &arrCallbackParams)) return NULL; if (NULL == matSourceEdge || NULL==matSinkEdge || NULL==matInputImage || NULL==arrCallbackParams) return NULL; /* Check that object input is 'double' type and a matrix Not needed if python wrapper function checks before call to this routine */ if (not_doublematrix(matSourceEdge)) return NULL; if (not_doublematrix(matSinkEdge)) return NULL; if (not_doublematrix(matInputImage)) return NULL; if (not_doublevector( arrCallbackParams)) return NULL; /* Get the dimensions of the input */ rows=dims[0]=matSourceEdge->dimensions[0]; cols=dims[1]=matSourceEdge->dimensions[1]; assert( rows == matSinkEdge->dimensions[0] && cols == matSinkEdge->dimensions[1] ); assert( rows == matInputImage->dimensions[0] && cols == matInputImage->dimensions[1] ); /* /\* turn arrays into c pointers, linear, but row major *\/ */ /* cMatSourceEdge = pyvector_to_Carrayptrs<double>( matSourceEdge ); */ /* cMatSinkEdge = pyvector_to_Carrayptrs<double>( matSinkEdge ); */ /* cMatInputImage = pyvector_to_Carrayptrs<double>( matSourceEdge ); */ /* cCallbackParams = pyvector_to_Carrayptrs<double>( arrCallbackParams ); */ /* /\* Make a new integer matrix of same dims *\/ */ /* matOut = (PyArrayObject *) PyArray_FromDims(2,dims,NPY_INT); */ /* cMatOut = pyvector_to_Carrayptrs<int>( matOut ); */ /* computation... */ ultraflow_inference2( nhoodSize, rows, cols, cMatSourceEdge, cMatSinkEdge, cMatInputImage, nbrEdgeCostMethod, cCallbackParams, cMatOut ); return PyArray_Return(matOut); }
static PyObject* exampleCPPfunction(PyObject *self, PyObject *args){ // Passed variables PyArrayObject *array1, *array2; int multiplier; double divisor; // Pointer for converted NumPy Arrays double *cArray1, *cArray2; // Extract variables and arrays from ARGS // O! belongs to one array O -> type; ! -> local Python variable // i belongs to an integer variable // d belongs to ab double variable if (!PyArg_ParseTuple(args, "O!O!id", &PyArray_Type, &array1, &PyArray_Type, &array2, &multiplier, &divisor)) return NULL; // Check wheter all array are of type double if (not_doublevector(array1)) return NULL; if (not_doublevector(array2)) return NULL; // Convert NumPy arrays to C-Arrays cArray1=pyvector_to_Carrayptrs(array1); cArray2=pyvector_to_Carrayptrs(array2); // Lengths of the arrays unsigned int array1Size = array1->dimensions[0]; // The actual calculation part. NOTE it is assumed, // that both arrays have the same dimension for(unsigned int i=0; i<array1Size; i++) { cArray2[i] = cArray1[i]*multiplier/divisor; } return Py_BuildValue("i", 1); }
/** * parseFloatVec3(vec_in, vec_out) * * @param[in] vec_in the python array to extract elements from * @param[out] vec_out float array of the numbers * @return true if successful, false if not * * Convert a python array type to a 3 element float * vector. */ static bool parseFloatVecN(PyArrayObject *vec_in, float *vec_out, int N) { /* verify it is a valid vector */ if (not_doublevector(vec_in)) return false; if (PyArray_DIM(vec_in,0) != N) { PyErr_Format(PyExc_ValueError, "Vector length incorrect. Received %ld and expected %d", PyArray_DIM(vec_in,0), N); return false; } NpyIter *iter; NpyIter_IterNextFunc *iternext; /* create the iterators */ iter = NpyIter_New(vec_in, NPY_ITER_READONLY, NPY_KEEPORDER, NPY_NO_CASTING, NULL); if (iter == NULL) goto fail; iternext = NpyIter_GetIterNext(iter, NULL); if (iternext == NULL) { NpyIter_Deallocate(iter); goto fail; } double ** dataptr = (double **) NpyIter_GetDataPtrArray(iter); /* iterate over the arrays */ int i = 0; do { vec_out[i++] = **dataptr; } while(iternext(iter) && (i < N)); NpyIter_Deallocate(iter); return true; fail: fprintf(stderr, "Parse fail\r\n"); return false; }