VISR  0.12.0
Versatile Interactive Scene Renderer
vector_from_ndarray.hpp
Go to the documentation of this file.
1 /* Copyright Institute of Sound and Vibration Research - All rights reserved */
2 
4 
5 #include <pybind11/pybind11.h>
6 #include <pybind11/numpy.h>
7 
8 namespace visr
9 {
10 
11 namespace python
12 {
13 namespace bindinghelpers
14 {
15 
25 template<typename DataType>
26 efl::BasicVector<DataType> vectorFromNdArray( pybind11::array_t<SampleType> const & array, std::size_t alignment = 0 )
27 {
28  if( array.ndim() != 1 )
29  {
30  throw std::invalid_argument( "efl::BasicVector from numpy ndarray: Input array must be 1D" );
31  }
32  // Should not happen, because we use the typed array_t template type.
33  if( not array.dtype().is( pybind11::dtype::of<DataType>() ) )
34  {
35  throw std::invalid_argument( "efl::BasicVector from numpy ndarray: Input matrix has a different data type (dtype)." );
36  }
37  std::size_t const numElements = static_cast<pybind11::ssize_t>(array.shape()[0]);
38  efl::BasicVector<DataType> ret( numElements, alignment );
39  for( std::size_t elIdx( 0 ); elIdx < numElements; ++elIdx )
40  {
41  ret[ elIdx ] = *static_cast<DataType const *>(array.data( elIdx ));
42  }
43  return ret;
44 }
45 
46 } // namepace bindinghelpers
47 } // namespace python
48 } // namespace visr
Definition: options.cpp:10
efl::BasicVector< DataType > vectorFromNdArray(pybind11::array_t< SampleType > const &array, std::size_t alignment=0)
Definition: vector_from_ndarray.hpp:26
Definition: basic_vector.hpp:28