wbc
base_types_conversion.h
Go to the documentation of this file.
1#pragma once
2
3#include <boost/python.hpp>
4#include <boost/python/implicit.hpp>
5#include <boost/python/module.hpp>
6#include <boost/python/numpy.hpp>
7#include <base/JointState.hpp>
8#include <base/samples/Joints.hpp>
9#include <base/JointLimits.hpp>
10
11namespace py = boost::python;
12namespace np = boost::python::numpy;
13
14namespace wbc_py{
15
16base::samples::Joints tobaseSamplesJoints(const base::NamedVector<base::JointState> &joints_in){
17 base::samples::Joints joints_out;
18 joints_out.elements = joints_in.elements;
19 joints_out.names = joints_in.names;
20 joints_out.time = base::Time::now();
21 return joints_out;
22}
23
24base::NamedVector<base::JointState> toNamedVector(const base::samples::Joints &joints_in){
25 base::NamedVector<base::JointState> joints_out;
26 joints_out.elements = joints_in.elements;
27 joints_out.names = joints_in.names;
28 return joints_out;
29}
30
31base::NamedVector<base::JointLimitRange> fromJointLimits(const base::JointLimits &limits_in){
32 base::NamedVector<base::JointLimitRange> limits_out;
33 limits_out.elements = limits_in.elements;
34 limits_out.names = limits_in.names;
35 return limits_out;
36}
37
38
39}
40
41namespace pygen {
42
43template <class JointVectorType>
46 {
47 py::converter::registry::push_back(&convertible, &construct, py::type_id<JointVectorType>());
48 }
49
50 static void* convertible(PyObject* obj_ptr)
51 {
52 if (!PySequence_Check(obj_ptr))
53 return 0;
54
55 py::list arr = py::extract<py::list>(obj_ptr);
56 return obj_ptr;
57 }
58
59 static void construct(PyObject* obj_ptr, py::converter::rvalue_from_python_stage1_data* data)
60 {
61 py::list arr = py::extract<py::list>(obj_ptr);
62 auto len = py::len(arr);
63
64 using storage_type = py::converter::rvalue_from_python_storage<JointVectorType>;
65 void* storage = reinterpret_cast<storage_type*>(data)->storage.bytes;
66
67 new (storage) JointVectorType;
68 JointVectorType& vec = *static_cast<JointVectorType*>(storage);
69
70 vec.resize(len);
71
72 for (long i = 0; i < len; ++i)
73 vec[i] = py::extract<base::JointState>(arr[i]);
74
75 data->convertible = storage;
76 }
77};
78
79} // namespace pygen
Definition base_types_conversion.h:41
Definition base_types_conversion.h:14
base::samples::Joints tobaseSamplesJoints(const base::NamedVector< base::JointState > &joints_in)
Definition base_types_conversion.h:16
base::NamedVector< base::JointState > toNamedVector(const base::samples::Joints &joints_in)
Definition base_types_conversion.h:24
base::NamedVector< base::JointLimitRange > fromJointLimits(const base::JointLimits &limits_in)
Definition base_types_conversion.h:31
Definition base_types_conversion.h:44
static void * convertible(PyObject *obj_ptr)
Definition base_types_conversion.h:50
static void construct(PyObject *obj_ptr, py::converter::rvalue_from_python_stage1_data *data)
Definition base_types_conversion.h:59
python_list_to_joint_vector()
Definition base_types_conversion.h:45