1 |
|
|
// Copyright 2010, Florent Lamiraux, Thomas Moulard, LAAS-CNRS. |
2 |
|
|
|
3 |
|
|
#include "dynamic-graph/python/convert-dg-to-py.hh" |
4 |
|
|
|
5 |
|
|
#include <dynamic-graph/signal-base.h> |
6 |
|
|
#include <dynamic-graph/signal-caster.h> |
7 |
|
|
#include <dynamic-graph/signal.h> |
8 |
|
|
|
9 |
|
|
#include <boost/python.hpp> |
10 |
|
|
#include <boost/python/stl_iterator.hpp> |
11 |
|
|
#include <iostream> |
12 |
|
|
#include <sstream> |
13 |
|
|
|
14 |
|
|
#include "dynamic-graph/python/python-compat.hh" |
15 |
|
|
|
16 |
|
|
namespace dynamicgraph { |
17 |
|
|
|
18 |
|
|
using ::dynamicgraph::SignalBase; |
19 |
|
|
|
20 |
|
|
namespace python { |
21 |
|
|
namespace convert { |
22 |
|
|
|
23 |
|
|
namespace bp = boost::python; |
24 |
|
|
|
25 |
|
|
command::Value toValue(bp::object o, const command::Value::Type& valueType) { |
26 |
|
|
using command::Value; |
27 |
|
|
switch (valueType) { |
28 |
|
|
case (Value::BOOL): |
29 |
|
|
return Value(bp::extract<bool>(o)); |
30 |
|
|
case (Value::UNSIGNED): |
31 |
|
|
return Value(bp::extract<unsigned>(o)); |
32 |
|
|
case (Value::INT): |
33 |
|
|
return Value(bp::extract<int>(o)); |
34 |
|
|
case (Value::FLOAT): |
35 |
|
|
return Value(bp::extract<float>(o)); |
36 |
|
|
case (Value::DOUBLE): |
37 |
|
|
return Value(bp::extract<double>(o)); |
38 |
|
|
case (Value::STRING): |
39 |
|
|
return Value(bp::extract<std::string>(o)); |
40 |
|
|
case (Value::VECTOR): |
41 |
|
|
// TODO for backward compatibility, support tuple or list ? |
42 |
|
|
// I don't think so |
43 |
|
|
return Value(bp::extract<Vector>(o)); |
44 |
|
|
case (Value::MATRIX): |
45 |
|
|
// TODO for backward compatibility, support tuple or list ? |
46 |
|
|
// I don't think so |
47 |
|
|
return Value(bp::extract<Matrix>(o)); |
48 |
|
|
case (Value::MATRIX4D): |
49 |
|
|
return Value(bp::extract<Eigen::Matrix4d>(o)); |
50 |
|
|
case (Value::VALUES): |
51 |
|
|
// TODO the vector of values cannot be built since |
52 |
|
|
// - the value type inside the vector are not know |
53 |
|
|
// - inferring the value type from the Python type is not implemented. |
54 |
|
|
throw std::invalid_argument( |
55 |
|
|
"not implemented: cannot create a vector of values"); |
56 |
|
|
break; |
57 |
|
|
default: |
58 |
|
|
std::cerr << "Only int, double and string are supported." << std::endl; |
59 |
|
|
} |
60 |
|
|
return Value(); |
61 |
|
|
} |
62 |
|
|
|
63 |
|
1 |
bp::object fromValue(const command::Value& value) { |
64 |
|
|
using command::Value; |
65 |
✗✗✗✗ ✗✗✗✗ ✗✗✓ |
1 |
switch (value.type()) { |
66 |
|
|
case (Value::BOOL): |
67 |
|
|
return bp::object(value.boolValue()); |
68 |
|
|
case (Value::UNSIGNED): |
69 |
|
|
return bp::object(value.unsignedValue()); |
70 |
|
|
case (Value::INT): |
71 |
|
|
return bp::object(value.intValue()); |
72 |
|
|
case (Value::FLOAT): |
73 |
|
|
return bp::object(value.floatValue()); |
74 |
|
|
case (Value::DOUBLE): |
75 |
|
|
return bp::object(value.doubleValue()); |
76 |
|
|
case (Value::STRING): |
77 |
|
|
return bp::object(value.stringValue()); |
78 |
|
|
case (Value::VECTOR): |
79 |
|
|
return bp::object(value.vectorValue()); |
80 |
|
|
case (Value::MATRIX): |
81 |
|
|
return bp::object(value.matrixXdValue()); |
82 |
|
|
case (Value::MATRIX4D): |
83 |
|
|
return bp::object(value.matrix4dValue()); |
84 |
|
|
case (Value::VALUES): { |
85 |
|
|
bp::list list; |
86 |
|
|
for (const Value& v : value.constValuesValue()) list.append(fromValue(v)); |
87 |
|
|
return list; |
88 |
|
|
} |
89 |
|
1 |
case (Value::NONE): |
90 |
|
|
default: |
91 |
|
1 |
return bp::object(); |
92 |
|
|
} |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
} // namespace convert |
96 |
|
|
} // namespace python |
97 |
|
|
} // namespace dynamicgraph |