pinocchio  3.7.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
 
Loading...
Searching...
No Matches
pybind11.hpp
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
44namespace 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>
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<>
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<
102 typename std::remove_reference<typename std::remove_cv<T>::type>::type>::type>(t);
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 {
144 f(convert_type<Args>::_from(args)...);
145 }
146
147 template<typename T>
148 struct function_wrapper;
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__
Main pinocchio namespace.
Definition treeview.dox:11