pinocchio
4.1.0
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
Toggle main menu visibility
Loading...
Searching...
No Matches
pybind11.hpp
1
#pragma once
2
34
35
#include <iostream>
36
#include <pinocchio/fwd.hpp>
37
38
// This lines forces clang-format to keep the include split here
39
#include <pybind11/pybind11.h>
40
41
#include <eigenpy/eigenpy.hpp>
42
43
namespace
pinocchio
44
{
45
namespace
python
46
{
47
namespace
bp = boost::python;
48
namespace
py = pybind11;
49
50
template
<
typename
T>
51
inline
py::object to(T & t)
52
{
53
// Create PyObject using boost Python
54
bp::object obj = bp::api::object(t);
55
PyObject * pyobj = obj.ptr();
56
return
pybind11::reinterpret_borrow<py::object>(pyobj);
57
}
58
template
<
typename
T>
59
inline
py::object to(T * t)
60
{
61
// Create PyObject using boost Python
62
typename
bp::manage_new_object::apply<T *>::type converter;
63
PyObject * pyobj = converter(t);
64
// Create the Pybind11 object
65
return
py::reinterpret_borrow<py::object>(pyobj);
66
}
67
68
template
<
typename
ReturnType>
69
inline
ReturnType & from(py::handle model)
70
{
71
return
bp::extract<ReturnType &>(model.ptr());
72
}
73
74
template
<
typename
T>
75
struct
convert_type
76
{
77
typedef
T type;
78
static
inline
T _to(T t)
79
{
80
return
t;
81
}
82
static
inline
type _from(type t)
83
{
84
return
t;
85
}
86
};
87
template
<>
88
struct
convert_type
<void>
89
{
90
// typedef void type;
91
// static inline void _to() {}
92
};
93
94
template
<
typename
T>
95
struct
convert_boost_python_object
96
{
97
typedef
py::object type;
98
static
inline
type _to(T t)
99
{
100
return
to<std::remove_pointer_t<std::decay_t<T>>>(t);
101
}
102
static
inline
T _from(type t)
103
{
104
return
from<std::decay_t<T>>(t);
105
}
106
};
107
109
#define PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(CLASS) \
110
namespace pinocchio \
111
{ \
112
namespace python \
113
{ \
114
template<> \
115
struct convert_type<CLASS> : convert_boost_python_object<CLASS> \
116
{ \
117
}; \
118
} \
119
}
120
122
#define _SINGLE_ARG(...) __VA_ARGS__
123
#define PINOCCHIO_PYBIND11_ADD_ALL_CONVERT_TYPE(CLASS) \
124
PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS)) \
125
PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS const)) \
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
131
namespace
internal
132
{
133
134
template
<
typename
R,
typename
... Args>
135
auto
call
(R (*f)(Args...),
typename
convert_type<Args>::type... args)
136
{
137
return
convert_type<R>::_to(f(convert_type<Args>::_from(args)...));
138
}
139
template
<
typename
... Args>
140
void
call(
void
(*f)(Args...),
typename
convert_type<Args>::type... args)
141
{
142
f(convert_type<Args>::_from(args)...);
143
}
144
145
template
<
typename
T>
146
struct
function_wrapper;
147
148
template
<
typename
R,
typename
... Args>
149
struct
function_wrapper<R (*)(Args...)>
150
{
151
static
const
size_t
nargs =
sizeof
...(Args);
152
153
typedef
R result_type;
154
155
template
<
size_t
i>
156
struct
arg
157
{
158
typedef
typename
std::tuple_element<i, std::tuple<Args...>>::type type;
159
};
160
161
typedef
R (*func_type)(Args...);
162
163
func_type f;
164
165
// typename convert_type<result_type>::type
166
auto
operator()(
typename
convert_type<Args>::type... args)
167
{
168
return
call(f, args...);
169
}
170
};
171
}
// namespace internal
172
182
template
<
typename
R,
typename
... Args>
183
internal::function_wrapper<R (*)(Args...)> make_pybind11_function(R (*func)(Args...))
184
{
185
internal::function_wrapper<R (*)(Args...)> wrapper;
186
wrapper.f = func;
187
return
wrapper;
188
}
189
190
template
<
typename
T>
191
py::object default_arg(T t)
192
{
193
py::object obj = to<T>(t);
194
// obj.inc_ref();
195
return
obj;
196
}
197
205
#define PINOCCHIO_PYBIND11_TYPE_CASTER(native_type, boost_python_name) \
206
namespace pybind11 \
207
{ \
208
namespace detail \
209
{ \
210
template<> \
211
struct type_caster<native_type> \
212
{ \
213
PYBIND11_TYPE_CASTER(_SINGLE_ARG(native_type), boost_python_name); \
214
\
215
/* Python -> C++ */
\
216
bool load(pybind11::handle src, bool) \
217
{ \
218
PyObject * source = src.ptr(); \
219
value = boost::python::extract<native_type>(source); \
220
return !PyErr_Occurred(); \
221
} \
222
/* C++ -> Python */
\
223
static pybind11::handle cast( \
224
native_type src, pybind11::return_value_policy
/*policy*/
, pybind11::handle
/*parent*/
) \
225
{ \
226
return boost::python::api::object(src).ptr(); \
227
} \
228
}; \
229
}
/* namespace detail */
\
230
}
/* namespace pybind11 */
231
232
}
// namespace python
233
}
// namespace pinocchio
234
235
#undef _SINGLE_ARG
pinocchio
Main pinocchio namespace.
Definition
treeview.dox:11
pinocchio::python::call
Definition
force.hpp:26
pinocchio::python::convert_boost_python_object
Definition
pybind11.hpp:96
pinocchio::python::convert_type
Definition
pybind11.hpp:76
include
pinocchio
bindings
python
pybind11.hpp
Generated by
1.17.0