cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vctPythonUtilities.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Anton Deguet
7  Created on: 2005-08-21
8 
9  (C) Copyright 2005-2014 Johns Hopkins University (JHU), All Rights Reserved.
10 
11 --- begin cisst license - do not edit ---
12 
13 This software is provided "as is" under an open source license, with
14 no warranty. The complete license can be found in license.txt and
15 http://www.cisst.org/cisst/license.txt.
16 
17 --- end cisst license ---
18 
19 */
20 
21 
22 /* This file is to be used only for the generation of SWIG wrappers.
23  It includes all the regular header files from the libraries as well
24  as some header files created only for the wrapping process
25  (e.g. vctDynamicMatrixRotation3.h).
26 
27  For any wrapper using %import "cisstVector.i", the file
28  cisstVector/vctPython.h should be included in the %header %{ ... %}
29  section of the interface file. */
30 
31 
32 #ifndef _cisstVector_i_h
33 #define _cisstVector_i_h
34 
35 
36 /* Put header files here */
37 #include <Python.h>
38 #include <arrayobject.h>
39 #include <cisstCommon/cmnAssert.h>
47 
48 
49 bool vctThrowUnlessIsPyArray(PyObject * input)
50 {
51  if (!PyArray_Check(input)) {
52  PyErr_SetString(PyExc_TypeError, "Object must be a NumPy array");
53  return false;
54  }
55  return true;
56 }
57 
58 
59 template <class _elementType>
61 {
62  PyErr_SetString(PyExc_ValueError, "Unsupported data type");
63  return false;
64 }
65 
66 
67 template <>
69 {
70  if (PyArray_ObjectType(input, 0) != NPY_BOOL) {
71  PyErr_SetString(PyExc_ValueError, "Array must be of type bool");
72  return false;
73  }
74  return true;
75 }
76 
77 
78 template <>
80 {
81  if (PyArray_ObjectType(input, 0) != NPY_INT8) {
82  PyErr_SetString(PyExc_ValueError, "Array must be of type char (int8)");
83  return false;
84  }
85  return true;
86 }
87 
88 
89 template <>
91 {
92  if (PyArray_ObjectType(input, 0) != NPY_UINT8) {
93  PyErr_SetString(PyExc_ValueError, "Array must be of type unsigned char (uint8)");
94  return false;
95  }
96  return true;
97 }
98 
99 
100 template <>
102 {
103  if (PyArray_ObjectType(input, 0) != NPY_INT16) {
104  PyErr_SetString(PyExc_ValueError, "Array must be of type short (int16)");
105  return false;
106  }
107  return true;
108 }
109 
110 
111 template <>
113 {
114  if (PyArray_ObjectType(input, 0) != NPY_UINT16) {
115  PyErr_SetString(PyExc_ValueError, "Array must be of type unsigned short (uint16)");
116  return false;
117  }
118  return true;
119 }
120 
121 
122 template <>
124 {
125  // NPY_INT and NPY_LONG are considered different types; NPY_INT32 is an alias for one of these
126 #if (CISST_DATA_MODEL == CISST_ILP32) || (CISST_DATA_MODEL == CISST_LLP64)
127  if ((PyArray_ObjectType(input, 0) != NPY_INT) && (PyArray_ObjectType(input, 0) != NPY_LONG))
128 #else
129  if (PyArray_ObjectType(input, 0) != NPY_INT)
130 #endif
131  {
132  PyErr_SetString(PyExc_ValueError, "Array must be of type int (int32)");
133  return false;
134  }
135  return true;
136 }
137 
138 
139 template <>
141 {
142  // NPY_UINT and NPY_ULONG are considered different types; NPY_UINT32 is an alias for one of these
143 #if (CISST_DATA_MODEL == CISST_ILP32) || (CISST_DATA_MODEL == CISST_LLP64)
144  if ((PyArray_ObjectType(input, 0) != NPY_UINT) && (PyArray_ObjectType(input, 0) != NPY_ULONG))
145 #else
146  if (PyArray_ObjectType(input, 0) != NPY_UINT)
147 #endif
148  {
149  PyErr_SetString(PyExc_ValueError, "Array must be of type unsigned int (uint32)");
150  return false;
151  }
152  return true;
153 }
154 
155 
156 template <>
158 {
159  if (PyArray_ObjectType(input, 0) != NPY_INT64) {
160  PyErr_SetString(PyExc_ValueError, "Array must be of type long int (int64)");
161  return false;
162  }
163  return true;
164 }
165 
166 
167 template <>
169 {
170  if (PyArray_ObjectType(input, 0) != NPY_UINT64) {
171  PyErr_SetString(PyExc_ValueError, "Array must be of type unsigned long int (uint64)");
172  return false;
173  }
174  return true;
175 }
176 
177 
178 template <>
180 {
181  if (PyArray_ObjectType(input, 0) != NPY_FLOAT) {
182  PyErr_SetString(PyExc_ValueError, "Array must be of type float");
183  return false;
184  }
185  return true;
186 }
187 
188 
189 template <>
191 {
192  if (PyArray_ObjectType(input, 0) != NPY_DOUBLE) {
193  PyErr_SetString(PyExc_ValueError, "Array must be of type double");
194  return false;
195  }
196  return true;
197 }
198 
199 
200 template <class _elementType>
201 int vctPythonType(void)
202 {
203  return NPY_NOTYPE; // unsupported type
204 }
205 
206 
207 template <>
209 {
210  return NPY_BOOL;
211 }
212 
213 
214 template <>
216 {
217  return NPY_INT8;
218 }
219 
220 
221 template <>
223 {
224  return NPY_UINT8;
225 }
226 
227 
228 template <>
230 {
231  return NPY_INT16;
232 }
233 
234 
235 template <>
237 {
238  return NPY_UINT16;
239 }
240 
241 
242 template <>
244 {
245  return NPY_INT32;
246 }
247 
248 
249 template <>
251 {
252  return NPY_UINT32;
253 }
254 
255 
256 template <>
258 {
259  return NPY_INT64;
260 }
261 
262 
263 template <>
265 {
266  return NPY_UINT64;
267 }
268 
269 
270 template <>
272 {
273  return NPY_DOUBLE;
274 }
275 
276 
277 bool vctThrowUnlessDimension1(PyObject * input)
278 {
279  if (PyArray_NDIM(input) != 1) {
280  PyErr_SetString(PyExc_ValueError, "Array must be 1D (vector)");
281  return false;
282  }
283  return true;
284 }
285 
286 
287 bool vctThrowUnlessDimension2(PyObject * input)
288 {
289  if (PyArray_NDIM(input) != 2) {
290  PyErr_SetString(PyExc_ValueError, "Array must be 2D (matrix)");
291  return false;
292  }
293  return true;
294 }
295 
296 
297 template <vct::size_type _dimension>
298 bool vctThrowUnlessDimensionN(PyObject * input)
299 {
300  if (PyArray_NDIM(input) != _dimension) {
301  std::stringstream stream;
302  stream << "Array must have " << _dimension << " dimension(s)";
303  std::string msg = stream.str();
304  PyErr_SetString(PyExc_ValueError, msg.c_str());
305  return false;
306  }
307  return true;
308 }
309 
310 
311 bool vctThrowUnlessIsWritable(PyObject * input)
312 {
313  const int flags = PyArray_FLAGS(input);
314  if(!(flags & NPY_WRITEABLE)) {
315  PyErr_SetString(PyExc_ValueError, "Array must be writable");
316  return false;
317  }
318  return true;
319 }
320 
321 
322 template <vct::size_type _size, vct::stride_type _stride, class _elementType, class _dataPtrType>
323 bool vctThrowUnlessCorrectVectorSize(PyObject * input,
325 {
326  const vct::size_type inputSize = PyArray_DIM(input, 0);
327  const vct::size_type targetSize = target.size();
328  if (inputSize != targetSize) {
329  std::stringstream stream;
330  stream << "Input vector's size must be " << targetSize;
331  std::string msg = stream.str();
332  PyErr_SetString(PyExc_ValueError, msg.c_str());
333  return false;
334  }
335  return true;
336 }
337 
338 
339 template <class _vectorOwnerType, typename _elementType>
342 {
343  return true;
344 }
345 
346 
347 template <vct::size_type _rows, vct::size_type _cols,
348  vct::stride_type _rowStride, vct::stride_type _colStride,
349  class _elementType, class _dataPtrType>
350 bool vctThrowUnlessCorrectMatrixSize(PyObject * input,
352 {
353  const vct::size_type inputRows = PyArray_DIM(input, 0);
354  const vct::size_type inputCols = PyArray_DIM(input, 1);
355  const vct::size_type targetRows = target.rows();
356  const vct::size_type targetCols = target.cols();
357  if ( inputRows != targetRows
358  || inputCols != targetCols) {
359  std::stringstream stream;
360  stream << "Input matrix's size must be " << targetRows << " rows by " << targetCols << " columns";
361  std::string msg = stream.str();
362  PyErr_SetString(PyExc_ValueError, msg.c_str());
363  return false;
364  }
365  return true;
366 }
367 
368 
369 template <class _matrixOwnerType, typename _elementType>
372 {
373  return true;
374 }
375 
376 
377 bool vctThrowUnlessOwnsData(PyObject * input)
378 {
379  const int flags = PyArray_FLAGS(input);
380  if(!(flags & NPY_OWNDATA)) {
381  PyErr_SetString(PyExc_ValueError, "Array must own its data");
382  return false;
383  }
384  return true;
385 }
386 
387 
388 template <vct::size_type _size, vct::stride_type _stride, class _elementType, class _dataPtrType>
389 bool vctThrowUnlessOwnsData(PyObject * CMN_UNUSED(input),
391 {
392  return true;
393 }
394 
395 
396 template <class _vectorOwnerType, typename _elementType>
397 bool vctThrowUnlessOwnsData(PyObject * input,
399 {
400  const int flags = PyArray_FLAGS(input);
401  if(!(flags & NPY_OWNDATA)) {
402  PyErr_SetString(PyExc_ValueError, "Array must own its data");
403  return false;
404  }
405  return true;
406 }
407 
408 
409 template <vct::size_type _rows, vct::size_type _cols,
410  vct::stride_type _rowStride, vct::stride_type _colStride,
411  class _elementType, class _dataPtrType>
412 bool vctThrowUnlessOwnsData(PyObject * CMN_UNUSED(input),
414 {
415  return true;
416 }
417 
418 
419 template <class _matrixOwnerType, typename _elementType>
420 bool vctThrowUnlessOwnsData(PyObject * input,
422 {
423  const int flags = PyArray_FLAGS(input);
424  if(!(flags & NPY_OWNDATA)) {
425  PyErr_SetString(PyExc_ValueError, "Array must own its data");
426  return false;
427  }
428  return true;
429 }
430 
431 
432 bool vctThrowUnlessNotReferenced(PyObject * input)
433 {
434  if (PyArray_REFCOUNT(input) > 4) {
435  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.");
436  return false;
437  }
438  return true;
439 }
440 
441 
442 template <vct::size_type _size, vct::stride_type _stride, class _elementType, class _dataPtrType>
445 {
446  return true;
447 }
448 
449 
450 template <class _vectorOwnerType, typename _elementType>
451 bool vctThrowUnlessNotReferenced(PyObject * input,
453 {
454  if (PyArray_REFCOUNT(input) > 4) {
455  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.");
456  return false;
457  }
458  return true;
459 }
460 
461 
462 template <vct::size_type _rows, vct::size_type _cols,
463  vct::stride_type _rowStride, vct::stride_type _colStride,
464  class _elementType, class _dataPtrType>
467 {
468  return true;
469 }
470 
471 
472 template <class _matrixOwnerType, typename _elementType>
473 bool vctThrowUnlessNotReferenced(PyObject * input,
475 {
476  if (PyArray_REFCOUNT(input) > 4) {
477  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.");
478  return false;
479  }
480  return true;
481 }
482 
483 
484 
485 #endif // _cisstVector_i_h
A template for a fixed size matrix with fixed spacing in memory.
Definition: vctFixedSizeConstMatrixBase.h:103
Assert macros definitions.
bool vctThrowUnlessIsPyArray(PyObject *input)
Definition: vctPythonUtilities.h:49
Declaration of vctDynamicConstMatrixBase.
bool vctThrowUnlessIsSameTypeArray< long int >(PyObject *input)
Definition: vctPythonUtilities.h:157
Portability across compilers and operating systems tools.
#define CMN_UNUSED(argument)
Definition: cmnPortability.h:479
bool vctThrowUnlessIsWritable(PyObject *input)
Definition: vctPythonUtilities.h:311
bool vctThrowUnlessDimension1(PyObject *input)
Definition: vctPythonUtilities.h:277
bool vctThrowUnlessDimensionN(PyObject *input)
Definition: vctPythonUtilities.h:298
int vctPythonType< unsigned long int >(void)
Definition: vctPythonUtilities.h:264
bool vctThrowUnlessIsSameTypeArray< unsigned long int >(PyObject *input)
Definition: vctPythonUtilities.h:168
bool vctThrowUnlessIsSameTypeArray< float >(PyObject *input)
Definition: vctPythonUtilities.h:179
size_t size_type
Definition: vctContainerTraits.h:35
int vctPythonType< unsigned short >(void)
Definition: vctPythonUtilities.h:236
bool vctThrowUnlessIsSameTypeArray< unsigned char >(PyObject *input)
Definition: vctPythonUtilities.h:90
int vctPythonType< int >(void)
Definition: vctPythonUtilities.h:243
bool vctThrowUnlessNotReferenced(PyObject *input)
Definition: vctPythonUtilities.h:432
Typedef for different transformations.
int vctPythonType< long int >(void)
Definition: vctPythonUtilities.h:257
Definition: vctDynamicConstMatrixBase.h:77
bool vctThrowUnlessIsSameTypeArray< unsigned int >(PyObject *input)
Definition: vctPythonUtilities.h:140
bool vctThrowUnlessDimension2(PyObject *input)
Definition: vctPythonUtilities.h:287
int vctPythonType< bool >(void)
Definition: vctPythonUtilities.h:208
size_type size(void) const
Definition: vctFixedSizeConstVectorBase.h:205
int vctPythonType< short >(void)
Definition: vctPythonUtilities.h:229
bool vctThrowUnlessCorrectVectorSize(PyObject *input, const vctFixedSizeConstVectorBase< _size, _stride, _elementType, _dataPtrType > &target)
Definition: vctPythonUtilities.h:323
int vctPythonType< unsigned int >(void)
Definition: vctPythonUtilities.h:250
int vctPythonType< unsigned char >(void)
Definition: vctPythonUtilities.h:222
int vctPythonType< char >(void)
Definition: vctPythonUtilities.h:215
bool vctThrowUnlessIsSameTypeArray< unsigned short >(PyObject *input)
Definition: vctPythonUtilities.h:112
bool vctThrowUnlessIsSameTypeArray< bool >(PyObject *input)
Definition: vctPythonUtilities.h:68
int vctPythonType(void)
Definition: vctPythonUtilities.h:201
Declaration of vctFixedSizeConstVectorBase.
bool vctThrowUnlessIsSameTypeArray(PyObject *CMN_UNUSED(input))
Definition: vctPythonUtilities.h:60
Declaration of vctDynamicConstVectorBase.
ptrdiff_t stride_type
Definition: vctContainerTraits.h:37
bool vctThrowUnlessCorrectMatrixSize(PyObject *input, const vctFixedSizeConstMatrixBase< _rows, _cols, _rowStride, _colStride, _elementType, _dataPtrType > &target)
Definition: vctPythonUtilities.h:350
Definition: vctDynamicConstVectorBase.h:77
A template for a fixed length vector with fixed spacing in memory.
Definition: vctFixedSizeConstVectorBase.h:107
bool vctThrowUnlessIsSameTypeArray< short >(PyObject *input)
Definition: vctPythonUtilities.h:101
int vctPythonType< double >(void)
Definition: vctPythonUtilities.h:271
bool vctThrowUnlessIsSameTypeArray< int >(PyObject *input)
Definition: vctPythonUtilities.h:123
bool vctThrowUnlessIsSameTypeArray< char >(PyObject *input)
Definition: vctPythonUtilities.h:79
bool vctThrowUnlessOwnsData(PyObject *input)
Definition: vctPythonUtilities.h:377
bool vctThrowUnlessIsSameTypeArray< double >(PyObject *input)
Definition: vctPythonUtilities.h:190
Declaration of vctFixedSizeConstMatrixBase.
Declaration of vctDynamicConstNArrayBase.