python.hpp
Go to the documentation of this file.
1 /* License: Apache 2.0. See LICENSE file in root directory.
2 Copyright(c) 2017 Intel Corporation. All Rights Reserved. */
3 
4 #ifndef LIBREALSENSE_PYTHON_HPP
5 #define LIBREALSENSE_PYTHON_HPP
6 
7 #include <pybind11/pybind11.h>
8 
9 // convenience functions
10 #include <pybind11/operators.h>
11 
12 // STL conversions
13 #include <pybind11/stl.h>
14 
15 // std::chrono::*
16 #include <pybind11/chrono.h>
17 
18 // makes certain STL containers opaque to prevent expensive copies
19 #include <pybind11/stl_bind.h>
20 
21 // makes std::function conversions work
22 #include <pybind11/functional.h>
23 
24 #define NAME pyrealsense2
25 #define SNAME "pyrealsense2"
26 // For rs2_format
27 #include "../include/librealsense2/h/rs_sensor.h"
28 
29 namespace py = pybind11;
30 using namespace pybind11::literals;
31 
32 // Hacky little bit of half-functions to make .def(BIND_DOWNCAST) look nice for binding as/is functions
33 #define BIND_DOWNCAST(class, downcast) "is_"#downcast, &rs2::class::is<rs2::downcast>).def("as_"#downcast, &rs2::class::as<rs2::downcast>
34 
35 // Binding enums
36 const std::string rs2_prefix{ "rs2_" };
38 #define BIND_ENUM(module, rs2_enum_type, RS2_ENUM_COUNT, docstring) BIND_ENUM_CUSTOM( module, rs2_enum_type, 0, RS2_ENUM_COUNT-1, docstring)
39 #define BIND_ENUM_CUSTOM(module, rs2_enum_type, FIRST, LAST, docstring) \
40  static std::string rs2_enum_type##pyclass_name = std::string(#rs2_enum_type).substr(rs2_prefix.length()); \
41  /* Above 'static' is required in order to keep the string alive since py::class_ does not copy it */ \
42  py::enum_<rs2_enum_type> py_##rs2_enum_type(module, rs2_enum_type##pyclass_name.c_str(), docstring); \
43  /* std::cout << std::endl << "## " << rs2_enum_type##pyclass_name << ":" << std::endl; */ \
44  for (int i = FIRST; i <= LAST; i++) \
45  { \
46  rs2_enum_type v = static_cast<rs2_enum_type>(i); \
47  const char* enum_name = rs2_enum_type##_to_string(v); \
48  auto python_name = make_pythonic_str(enum_name); \
49  py_##rs2_enum_type.value(python_name.c_str(), v); \
50  /* std::cout << " - " << python_name << std::endl; */ \
51  }
52 
53 // Binding arrays and matrices
54 template<typename T, size_t SIZE>
55 void copy_raw_array(T(&dst)[SIZE], const std::array<T, SIZE>& src)
56 {
57  for (size_t i = 0; i < SIZE; i++)
58  {
59  dst[i] = src[i];
60  }
61 }
62 
63 template<typename T, size_t NROWS, size_t NCOLS>
64 void copy_raw_2d_array(T(&dst)[NROWS][NCOLS], const std::array<std::array<T, NCOLS>, NROWS>& src)
65 {
66  for (size_t i = 0; i < NROWS; i++)
67  {
68  for (size_t j = 0; j < NCOLS; j++)
69  {
70  dst[i][j] = src[i][j];
71  }
72  }
73 }
74 template <typename T, size_t N>
75 std::string array_to_string(const T(&arr)[N])
76 {
77  std::ostringstream oss;
78  oss << "[";
79  for (int i = 0; i < N; i++)
80  {
81  if (i != 0)
82  oss << ", ";
83  oss << arr[i];
84  }
85  oss << "]";
86  return oss.str();
87 }
88 
89 template <typename T, size_t N, size_t M>
90 std::string matrix_to_string(const T(&arr)[N][M])
91 {
92  std::ostringstream oss;
93  oss << "[";
94  for (int i = 0; i < N; i++)
95  {
96  if (i != 0)
97  oss << ", ";
98  oss << "[";
99  for (int j = 0; j < M; j++)
100  {
101  if (j != 0)
102  oss << ", ";
103  oss << arr[i][j];
104  }
105  oss << "]";
106  }
107  oss << "]";
108  return oss.str();
109 }
110 
111 #define BIND_RAW_ARRAY_GETTER(T, member, valueT, SIZE) [](const T& self) -> const std::array<valueT, SIZE>& { return reinterpret_cast<const std::array<valueT, SIZE>&>(self.member); }
112 #define BIND_RAW_ARRAY_SETTER(T, member, valueT, SIZE) [](T& self, const std::array<valueT, SIZE>& src) { copy_raw_array(self.member, src); }
113 
114 #define BIND_RAW_2D_ARRAY_GETTER(T, member, valueT, NROWS, NCOLS) [](const T& self) -> const std::array<std::array<valueT, NCOLS>, NROWS>& { return reinterpret_cast<const std::array<std::array<valueT, NCOLS>, NROWS>&>(self.member); }
115 #define BIND_RAW_2D_ARRAY_SETTER(T, member, valueT, NROWS, NCOLS) [](T& self, const std::array<std::array<valueT, NCOLS>, NROWS>& src) { copy_raw_2d_array(self.member, src); }
116 
117 #define BIND_RAW_ARRAY_PROPERTY(T, member, valueT, SIZE) #member, BIND_RAW_ARRAY_GETTER(T, member, valueT, SIZE), BIND_RAW_ARRAY_SETTER(T, member, valueT, SIZE)
118 #define BIND_RAW_2D_ARRAY_PROPERTY(T, member, valueT, NROWS, NCOLS) #member, BIND_RAW_2D_ARRAY_GETTER(T, member, valueT, NROWS, NCOLS), BIND_RAW_2D_ARRAY_SETTER(T, member, valueT, NROWS, NCOLS)
119 
120 // TODO: Fill in missing formats
121 // Map format->data type for various things
122 template <rs2_format> struct FmtToType { using type = uint8_t; }; // Default to uint8_t
123 #define MAP_FMT_TO_TYPE(F, T) template <> struct FmtToType<F> { using type = T; }
125 //MAP_FMT_TO_TYPE(RS2_FORMAT_DISPARITY16, );
134 //MAP_FMT_TO_TYPE(RS2_FORMAT_RAW10, );
138 //MAP_FMT_TO_TYPE(RS2_FORMAT_MOTION_RAW, );
140 //MAP_FMT_TO_TYPE(RS2_FORMAT_GPIO_RAW, );
141 //MAP_FMT_TO_TYPE(RS2_FORMAT_6DOF, );
145 //MAP_FMT_TO_TYPE(RS2_FORMAT_MJPEG, );
147 //MAP_FMT_TO_TYPE(RS2_FORMAT_Y12I, );
148 //MAP_FMT_TO_TYPE(RS2_FORMAT_INZI, );
150 //MAP_FMT_TO_TYPE(RS2_FORMAT_W10, );
152 template <rs2_format FMT> struct itemsize {
153  static constexpr size_t func() { return sizeof(typename FmtToType<FMT>::type); }
154 };
155 template <rs2_format FMT> struct fmtstring {
157 };
158 
159 template<template<rs2_format> class F>
161  switch (fmt) {
170  case RS2_FORMAT_Y8: return F<RS2_FORMAT_Y8>::func();
189  case RS2_FORMAT_FG: return F<RS2_FORMAT_FG>::func();
190  // c++11 standard doesn't allow throw in constexpr function switch case
191  case RS2_FORMAT_COUNT: throw std::runtime_error("format.count is not a valid value for arguments of type format!");
192  default: return F<RS2_FORMAT_ANY>::func();
193  }
194 }
195 
196 // Support Python's buffer protocol
197 class BufData {
198 public:
199  void *_ptr = nullptr; // Pointer to the underlying storage
200  size_t _itemsize = 0; // Size of individual items in bytes
201  std::string _format; // For homogeneous buffers, this should be set to format_descriptor<T>::format()
202  size_t _ndim = 0; // Number of dimensions
203  std::vector<size_t> _shape; // Shape of the tensor (1 entry per dimension)
204  std::vector<size_t> _strides; // Number of entries between adjacent entries (for each per dimension)
205 public:
206  BufData(void *ptr, size_t itemsize, const std::string& format, size_t ndim, const std::vector<size_t> &shape, const std::vector<size_t> &strides)
207  : _ptr(ptr), _itemsize(itemsize), _format(format), _ndim(ndim), _shape(shape), _strides(strides) {}
208  BufData(void *ptr, size_t itemsize, const std::string& format, size_t size)
209  : BufData(ptr, itemsize, format, 1, std::vector<size_t> { size }, std::vector<size_t> { itemsize }) { }
210  BufData(void *ptr, // Raw data pointer
211  size_t itemsize, // Size of the type in bytes
212  const std::string& format, // Data type's format descriptor (e.g. "@f" for float xyz)
213  size_t dim, // number of data elements per group (e.g. 3 for float xyz)
214  size_t count) // Number of groups
215  : BufData(ptr, itemsize, format, 2, std::vector<size_t> { count, dim }, std::vector<size_t> { itemsize*dim, itemsize }) { }
216 };
217 
218 /*PYBIND11_MAKE_OPAQUE(std::vector<rs2::stream_profile>)*/
219 
220 // Partial module definition functions
221 void init_c_files(py::module &m);
222 void init_types(py::module &m);
223 void init_frame(py::module &m);
224 void init_options(py::module &m);
225 void init_processing(py::module &m);
226 void init_sensor(py::module &m);
227 void init_device(py::module &m);
228 void init_record_playback(py::module &m);
229 void init_context(py::module &m);
230 void init_pipeline(py::module &m);
231 void init_internal(py::module &m);
232 void init_export(py::module &m);
233 void init_advanced_mode(py::module &m);
234 void init_serializable_device(py::module& m);
235 void init_util(py::module &m);
236 
237 #endif // LIBREALSENSE_PYTHON_HPP
auto fmt_to_value(rs2_format fmt) -> typename std::result_of< decltype(&F< RS2_FORMAT_ANY >::func)()>::type
Definition: python.hpp:160
void init_context(py::module &m)
Definition: pyrs_context.cpp:7
std::string matrix_to_string(const T(&arr)[N][M])
Definition: python.hpp:90
const GLfloat * m
Definition: glext.h:6814
std::string array_to_string(const T(&arr)[N])
Definition: python.hpp:75
static constexpr size_t func()
Definition: python.hpp:153
GLenum GLenum dst
Definition: glext.h:1751
BufData(void *ptr, size_t itemsize, const std::string &format, size_t dim, size_t count)
Definition: python.hpp:210
unsigned short uint16_t
Definition: stdint.h:79
GLsizei const GLchar *const * string
void copy_raw_array(T(&dst)[SIZE], const std::array< T, SIZE > &src)
Definition: python.hpp:55
GLenum src
Definition: glext.h:1751
unsigned char uint8_t
Definition: stdint.h:78
void copy_raw_2d_array(T(&dst)[NROWS][NCOLS], const std::array< std::array< T, NCOLS >, NROWS > &src)
Definition: python.hpp:64
static const std::string func()
Definition: python.hpp:156
std::string make_pythonic_str(std::string str)
Definition: c_files.cpp:9
std::string _format
Definition: python.hpp:201
GLsizeiptr size
void init_device(py::module &m)
Definition: pyrs_device.cpp:9
GLsizei const GLuint const GLintptr const GLsizei * strides
Definition: glext.h:2584
void init_internal(py::module &m)
void init_record_playback(py::module &m)
void init_export(py::module &m)
Definition: pyrs_export.cpp:7
GLint GLint GLsizei GLint GLenum format
BufData(void *ptr, size_t itemsize, const std::string &format, size_t size)
Definition: python.hpp:208
GLint j
rs2_format
A stream&#39;s format identifies how binary data is encoded within a frame.
Definition: rs_sensor.h:59
void init_util(py::module &m)
Definition: pyrsutil.cpp:7
GLenum func
BufData(void *ptr, size_t itemsize, const std::string &format, size_t ndim, const std::vector< size_t > &shape, const std::vector< size_t > &strides)
Definition: python.hpp:206
#define MAP_FMT_TO_TYPE(F, T)
Definition: python.hpp:123
GLenum array
Definition: glext.h:7022
const std::string rs2_prefix
Definition: python.hpp:36
std::vector< size_t > _shape
Definition: python.hpp:203
void init_pipeline(py::module &m)
void init_sensor(py::module &m)
Definition: pyrs_sensor.cpp:9
GLenum type
void init_advanced_mode(py::module &m)
void init_frame(py::module &m)
Definition: pyrs_frame.cpp:7
GLint GLsizei count
M
Definition: rmse.py:42
int i
void init_processing(py::module &m)
std::vector< size_t > _strides
Definition: python.hpp:204
void init_options(py::module &m)
Definition: pyrs_options.cpp:7
void init_serializable_device(py::module &m)
void init_types(py::module &m)
Definition: pyrs_types.cpp:7
uint8_t type
Definition: python.hpp:122
void init_c_files(py::module &m)
Definition: c_files.cpp:21


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:39