pybind11.hpp
Go to the documentation of this file.
1 #ifndef __pinocchio_python_pybind11_hpp__
2 #define __pinocchio_python_pybind11_hpp__
3 
35 
36 #include <iostream>
37 #include <pinocchio/fwd.hpp>
38 
39 // This lines forces clang-format to keep the include split here
40 #include <pybind11/pybind11.h>
41 
42 #include <eigenpy/eigenpy.hpp>
43 
44 namespace pinocchio
45 {
46  namespace python
47  {
48  namespace bp = boost::python;
49  namespace py = pybind11;
50 
51  template<typename T>
52  inline py::object to(T & t)
53  {
54  // Create PyObject using boost Python
55  bp::object obj = bp::api::object(t);
56  PyObject * pyobj = obj.ptr();
57  return pybind11::reinterpret_borrow<py::object>(pyobj);
58  }
59  template<typename T>
60  inline py::object to(T * t)
61  {
62  // Create PyObject using boost Python
63  typename bp::manage_new_object::apply<T *>::type converter;
64  PyObject * pyobj = converter(t);
65  // Create the Pybind11 object
66  return py::reinterpret_borrow<py::object>(pyobj);
67  }
68 
69  template<typename ReturnType>
70  inline ReturnType & from(py::handle model)
71  {
72  return bp::extract<ReturnType &>(model.ptr());
73  }
74 
75  template<typename T>
76  struct convert_type
77  {
78  typedef T type;
79  static inline T _to(T t)
80  {
81  return t;
82  }
83  static inline type _from(type t)
84  {
85  return t;
86  }
87  };
88  template<>
89  struct convert_type<void>
90  {
91  // typedef void type;
92  // static inline void _to() {}
93  };
94 
95  template<typename T>
97  {
98  typedef py::object type;
99  static inline type _to(T t)
100  {
101  return to<typename std::remove_pointer<
103  }
104  static inline T _from(type t)
105  {
107  }
108  };
109 
111 #define PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(CLASS) \
112  namespace pinocchio \
113  { \
114  namespace python \
115  { \
116  template<> \
117  struct convert_type<CLASS> : convert_boost_python_object<CLASS> \
118  { \
119  }; \
120  } \
121  }
122 
124 #define _SINGLE_ARG(...) __VA_ARGS__
125 #define PINOCCHIO_PYBIND11_ADD_ALL_CONVERT_TYPE(CLASS) \
126  PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS)) \
127  PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS const)) \
128  PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS &)) \
129  PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS const &)) \
130  PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS *)) \
131  PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS const *))
132 
133  namespace internal
134  {
135 
136  template<typename R, typename... Args>
137  auto call(R (*f)(Args...), typename convert_type<Args>::type... args)
138  {
140  }
141  template<typename... Args>
142  void call(void (*f)(Args...), typename convert_type<Args>::type... args)
143  {
145  }
146 
147  template<typename T>
149 
150  template<typename R, typename... Args>
151  struct function_wrapper<R (*)(Args...)>
152  {
153  static const size_t nargs = sizeof...(Args);
154 
155  typedef R result_type;
156 
157  template<size_t i>
158  struct arg
159  {
160  typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
161  };
162 
163  typedef R (*func_type)(Args...);
164 
165  func_type f;
166 
167  // typename convert_type<result_type>::type
168  auto operator()(typename convert_type<Args>::type... args)
169  {
170  return call(f, args...);
171  }
172  };
173  } // namespace internal
174 
184  template<typename R, typename... Args>
185  internal::function_wrapper<R (*)(Args...)> make_pybind11_function(R (*func)(Args...))
186  {
187  internal::function_wrapper<R (*)(Args...)> wrapper;
188  wrapper.f = func;
189  return wrapper;
190  }
191 
192  template<typename T>
193  py::object default_arg(T t)
194  {
195  py::object obj = to<T>(t);
196  // obj.inc_ref();
197  return obj;
198  }
199 
207 #define PINOCCHIO_PYBIND11_TYPE_CASTER(native_type, boost_python_name) \
208  namespace pybind11 \
209  { \
210  namespace detail \
211  { \
212  template<> \
213  struct type_caster<native_type> \
214  { \
215  PYBIND11_TYPE_CASTER(_SINGLE_ARG(native_type), boost_python_name); \
216  \
217  /* Python -> C++ */ \
218  bool load(pybind11::handle src, bool) \
219  { \
220  PyObject * source = src.ptr(); \
221  value = boost::python::extract<native_type>(source); \
222  return !PyErr_Occurred(); \
223  } \
224  /* C++ -> Python */ \
225  static pybind11::handle cast( \
226  native_type src, pybind11::return_value_policy /*policy*/, pybind11::handle /*parent*/) \
227  { \
228  return boost::python::api::object(src).ptr(); \
229  } \
230  }; \
231  } /* namespace detail */ \
232  } /* namespace pybind11 */
233 
234  } // namespace python
235 } // namespace pinocchio
236 
237 #undef _SINGLE_ARG
238 
239 #endif // #ifndef __pinocchio_python_pybind11_hpp__
simulation-contact-dynamics.T
int T
Definition: simulation-contact-dynamics.py:94
pinocchio::python::make_pybind11_function
internal::function_wrapper< R(*)(Args...)> make_pybind11_function(R(*func)(Args...))
Creates a function wrapper.
Definition: pybind11.hpp:185
boost::python
pinocchio::python::convert_boost_python_object::type
py::object type
Definition: pybind11.hpp:98
pinocchio::python::internal::function_wrapper
Definition: pybind11.hpp:148
eigenpy.hpp
pinocchio::python::convert_boost_python_object::_from
static T _from(type t)
Definition: pybind11.hpp:104
fwd.hpp
inverse-kinematics.i
int i
Definition: inverse-kinematics.py:20
pinocchio::python::convert_type::type
T type
Definition: pybind11.hpp:78
pinocchio::python::from
ReturnType & from(py::handle model)
Definition: pybind11.hpp:70
R
R
pinocchio::python::convert_type
Definition: pybind11.hpp:76
autodiff-rnea.f
f
Definition: autodiff-rnea.py:24
simulation-pendulum.type
type
Definition: simulation-pendulum.py:18
args
args
python
pinocchio::python::default_arg
py::object default_arg(T t)
Definition: pybind11.hpp:193
example_robot_data.__main__.nargs
nargs
Definition: __main__.py:8
pinocchio::python::convert_boost_python_object::_to
static type _to(T t)
Definition: pybind11.hpp:99
pinocchio::python::convert_type::_to
static T _to(T t)
Definition: pybind11.hpp:79
pinocchio::python::internal::call
auto call(R(*f)(Args...), typename convert_type< Args >::type... args)
Definition: pybind11.hpp:137
pinocchio::python::to
py::object to(T &t)
Definition: pybind11.hpp:52
t
Transform3f t
pinocchio::python::internal::function_wrapper< R(*)(Args...)>::operator()
auto operator()(typename convert_type< Args >::type... args)
Definition: pybind11.hpp:168
pinocchio::python::convert_type::_from
static type _from(type t)
Definition: pybind11.hpp:83
pinocchio::python::internal::function_wrapper< R(*)(Args...)>::f
func_type f
Definition: pybind11.hpp:165
pinocchio::python::convert_boost_python_object
Definition: pybind11.hpp:96
pinocchio::python::internal::function_wrapper< R(*)(Args...)>::arg::type
std::tuple_element< i, std::tuple< Args... > >::type type
Definition: pybind11.hpp:160
pinocchio::model
JointCollectionTpl & model
Definition: joint-configuration.hpp:1116
pinocchio::python::internal::function_wrapper< R(*)(Args...)>::result_type
R result_type
Definition: pybind11.hpp:155
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27


pinocchio
Author(s):
autogenerated on Sat Jun 1 2024 02:40:38