30#ifndef _vctPythonUtilities_h
31#define _vctPythonUtilities_h
35#include <numpy/numpyconfig.h>
39#if defined(NPY_1_8_API_VERSION)
40#define NPY_NO_DEPRECATED_API NPY_1_8_API_VERSION
42#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
44#include <numpy/arrayobject.h>
50#define cast_array(A) reinterpret_cast<PyArrayObject *>(A)
53#ifndef PyArray_REFCOUNT
54#define PyArray_REFCOUNT(obj) Py_REFCNT(obj)
71 if (!PyArray_Check(input)) {
72 PyErr_SetString(PyExc_TypeError,
"Object must be a NumPy array");
79template <
class _elementType>
82 PyErr_Format(PyExc_ValueError,
"Unsupported data type: %s",
typeid(_elementType).name());
90 if (PyArray_ObjectType(input, 0) != NPY_BOOL) {
91 PyErr_SetString(PyExc_ValueError,
"Array must be of type bool");
101 if (PyArray_ObjectType(input, 0) != NPY_INT8) {
102 PyErr_SetString(PyExc_ValueError,
"Array must be of type char (int8)");
112 if (PyArray_ObjectType(input, 0) != NPY_UINT8) {
113 PyErr_SetString(PyExc_ValueError,
"Array must be of type unsigned char (uint8)");
123 if (PyArray_ObjectType(input, 0) != NPY_INT16) {
124 PyErr_SetString(PyExc_ValueError,
"Array must be of type short (int16)");
134 if (PyArray_ObjectType(input, 0) != NPY_UINT16) {
135 PyErr_SetString(PyExc_ValueError,
"Array must be of type unsigned short (uint16)");
146#if (CISST_DATA_MODEL == CISST_ILP32) || (CISST_DATA_MODEL == CISST_LLP64)
147 if ((PyArray_ObjectType(input, 0) != NPY_INT) && (PyArray_ObjectType(input, 0) != NPY_LONG))
149 if (PyArray_ObjectType(input, 0) != NPY_INT)
152 PyErr_SetString(PyExc_ValueError,
"Array must be of type int (int32)");
163#if (CISST_DATA_MODEL == CISST_ILP32) || (CISST_DATA_MODEL == CISST_LLP64)
164 if ((PyArray_ObjectType(input, 0) != NPY_UINT) && (PyArray_ObjectType(input, 0) != NPY_ULONG))
166 if (PyArray_ObjectType(input, 0) != NPY_UINT)
169 PyErr_SetString(PyExc_ValueError,
"Array must be of type unsigned int (uint32)");
179#if (CISST_DATA_MODEL == CISST_ILP32) || (CISST_DATA_MODEL == CISST_LLP64)
180 if (PyArray_ObjectType(input, 0) != NPY_INT32) {
181 PyErr_SetString(PyExc_ValueError,
"Array must be of type long int (int32 on this platform)");
185 if (PyArray_ObjectType(input, 0) != NPY_INT64) {
186 PyErr_SetString(PyExc_ValueError,
"Array must be of type long int (int64 on this platform)");
197#if (CISST_DATA_MODEL == CISST_ILP32) || (CISST_DATA_MODEL == CISST_LLP64)
198 if (PyArray_ObjectType(input, 0) != NPY_UINT32) {
199 PyErr_SetString(PyExc_ValueError,
"Array must be of type unsigned long int (uint32 on this platform)");
203 if (PyArray_ObjectType(input, 0) != NPY_UINT64) {
204 PyErr_SetString(PyExc_ValueError,
"Array must be of type unsigned long int (uint64 on this platform)");
212#if CISST_LONG_LONG_NATIVE
216 if (PyArray_ObjectType(input, 0) != NPY_INT64) {
217 PyErr_SetString(PyExc_ValueError,
"Array must be of type long long int (int64)");
226 if (PyArray_ObjectType(input, 0) != NPY_UINT64) {
227 PyErr_SetString(PyExc_ValueError,
"Array must be of type unsigned long long int (uint64)");
234#if CISST_SIZE_T_NATIVE
238 if (PyArray_ObjectType(input, 0) != NPY_UINT64) {
239 PyErr_SetString(PyExc_ValueError,
"Array must be of type size_t (uint64)");
249 if (PyArray_ObjectType(input, 0) != NPY_FLOAT) {
250 PyErr_SetString(PyExc_ValueError,
"Array must be of type float");
260 if (PyArray_ObjectType(input, 0) != NPY_DOUBLE) {
261 PyErr_SetString(PyExc_ValueError,
"Array must be of type double");
268template <
class _elementType>
324#if (CISST_DATA_MODEL == CISST_ILP32) || (CISST_DATA_MODEL == CISST_LLP64)
356#if CISST_LONG_LONG_NATIVE
371#if CISST_SIZE_T_NATIVE
390 if (PyArray_NDIM(input) != 1) {
391 PyErr_SetString(PyExc_ValueError,
"Array must be 1D (vector)");
400 if (PyArray_NDIM(input) != 2) {
401 PyErr_SetString(PyExc_ValueError,
"Array must be 2D (matrix)");
408template <vct::
size_type _dimension>
411 if (PyArray_NDIM(input) != _dimension) {
412 std::stringstream stream;
413 stream <<
"Array must have " << _dimension <<
" dimension(s)";
414 std::string msg = stream.str();
415 PyErr_SetString(PyExc_ValueError, msg.c_str());
424 const int flags = PyArray_FLAGS(input);
425 if(!(flags & NPY_ARRAY_WRITEABLE)) {
426 PyErr_SetString(PyExc_ValueError,
"Array must be writable");
433template <vct::
size_type _size, vct::str
ide_type _str
ide,
class _elementType,
class _dataPtrType>
439 if (inputSize != targetSize) {
440 std::stringstream stream;
441 stream <<
"Input vector's size must be " << targetSize;
442 std::string msg = stream.str();
443 PyErr_SetString(PyExc_ValueError, msg.c_str());
450template <
class _vectorOwnerType,
typename _elementType>
460 class _elementType,
class _dataPtrType>
468 if ( inputRows != targetRows
469 || inputCols != targetCols) {
470 std::stringstream stream;
471 stream <<
"Input matrix's size must be " << targetRows <<
" rows by " << targetCols <<
" columns";
472 std::string msg = stream.str();
473 PyErr_SetString(PyExc_ValueError, msg.c_str());
480template <
class _matrixOwnerType,
typename _elementType>
490 const int flags = PyArray_FLAGS(input);
491 if(!(flags & NPY_ARRAY_OWNDATA)) {
492 PyErr_SetString(PyExc_ValueError,
"Array must own its data");
499template <vct::
size_type _size, vct::str
ide_type _str
ide,
class _elementType,
class _dataPtrType>
507template <
class _vectorOwnerType,
typename _elementType>
511 const int flags = PyArray_FLAGS(input);
512 if(!(flags & NPY_ARRAY_OWNDATA)) {
513 PyErr_SetString(PyExc_ValueError,
"Array must own its data");
522 class _elementType,
class _dataPtrType>
530template <
class _matrixOwnerType,
typename _elementType>
534 const int flags = PyArray_FLAGS(input);
535 if(!(flags & NPY_ARRAY_OWNDATA)) {
536 PyErr_SetString(PyExc_ValueError,
"Array must own its data");
546 PyErr_SetString(PyExc_ValueError,
"Array must not be referenced by other objects. Try making a deep copy of the array and call the function again.");
553template <vct::
size_type _size, vct::str
ide_type _str
ide,
class _elementType,
class _dataPtrType>
561template <
class _vectorOwnerType,
typename _elementType>
566 PyErr_SetString(PyExc_ValueError,
"Array must not be referenced by other objects. Try making a deep copy of the array and call the function again.");
575 class _elementType,
class _dataPtrType>
583template <
class _matrixOwnerType,
typename _elementType>
588 PyErr_SetString(PyExc_ValueError,
"Array must not be referenced by other objects. Try making a deep copy of the array and call the function again.");
Definition vctForwardDeclarations.h:145
Definition vctForwardDeclarations.h:119
A template for a fixed size matrix with fixed spacing in memory.
Definition vctFixedSizeConstMatrixBase.h:104
size_type cols() const
Definition vctFixedSizeConstMatrixBase.h:282
size_type rows() const
Definition vctFixedSizeConstMatrixBase.h:277
A template for a fixed length vector with fixed spacing in memory.
Definition vctFixedSizeConstVectorBase.h:108
size_type size(void) const
Definition vctFixedSizeConstVectorBase.h:205
Assert macros definitions.
Portability across compilers and operating systems tools.
#define CMN_UNUSED(argument)
Definition cmnPortability.h:497
size_t size_type
Definition vctContainerTraits.h:35
ptrdiff_t stride_type
Definition vctContainerTraits.h:37
Declaration of vctDynamicConstMatrixBase.
Declaration of vctDynamicConstNArrayBase.
Declaration of vctDynamicConstVectorBase.
Declaration of vctFixedSizeConstMatrixBase.
Declaration of vctFixedSizeConstVectorBase.
bool vctThrowUnlessDimensionN(PyArrayObject *input)
Definition vctPythonUtilities.h:409
bool vctThrowUnlessNotReferenced(PyObject *input)
Definition vctPythonUtilities.h:543
bool vctThrowUnlessIsPyArray(PyObject *input)
Definition vctPythonUtilities.h:69
bool vctThrowUnlessIsWritable(PyArrayObject *input)
Definition vctPythonUtilities.h:422
int vctPythonType(void)
Definition vctPythonUtilities.h:269
bool vctThrowUnlessCorrectVectorSize(PyArrayObject *input, const vctFixedSizeConstVectorBase< _size, _stride, _elementType, _dataPtrType > &target)
Definition vctPythonUtilities.h:434
bool vctThrowUnlessDimension1(PyArrayObject *input)
Definition vctPythonUtilities.h:388
bool vctThrowUnlessCorrectMatrixSize(PyArrayObject *input, const vctFixedSizeConstMatrixBase< _rows, _cols, _rowStride, _colStride, _elementType, _dataPtrType > &target)
Definition vctPythonUtilities.h:461
bool vctThrowUnlessIsSameTypeArray(PyObject *CMN_UNUSED(input))
Definition vctPythonUtilities.h:80
bool vctThrowUnlessOwnsData(PyArrayObject *input)
Definition vctPythonUtilities.h:488
#define PyArray_REFCOUNT(obj)
Definition vctPythonUtilities.h:54
bool vctThrowUnlessDimension2(PyArrayObject *input)
Definition vctPythonUtilities.h:398