signal-base-py.cc
Go to the documentation of this file.
1 // Copyright 2010, Florent Lamiraux, Thomas Moulard, LAAS-CNRS.
2 
7 #include <dynamic-graph/signal.h>
8 #include <dynamic-graph/value.h>
9 
10 #include <boost/python.hpp>
11 #include <iostream>
12 #include <sstream>
13 
17 
19 
20 namespace bp = boost::python;
21 
22 namespace dynamicgraph {
23 namespace python {
24 
25 typedef Eigen::AngleAxis<double> VectorUTheta;
26 typedef Eigen::Quaternion<double> Quaternion;
27 
28 typedef Eigen::VectorXd Vector;
29 typedef Eigen::Vector3d Vector3;
30 typedef Eigen::Matrix<double, 7, 1> Vector7;
31 
32 typedef Eigen::MatrixXd Matrix;
33 typedef Eigen::Matrix<double, 3, 3> MatrixRotation;
34 typedef Eigen::Matrix<double, 4, 4> Matrix4;
35 typedef Eigen::Transform<double, 3, Eigen::Affine> MatrixHomogeneous;
36 typedef Eigen::Matrix<double, 6, 6> MatrixTwist;
37 
38 template <typename Time>
39 void exposeSignalBase(const char* name) {
40  typedef SignalBase<Time> S_t;
41  bp::class_<S_t, boost::noncopyable>(name, bp::no_init)
42  .add_property("time",
43  bp::make_function(
44  &S_t::getTime,
45  bp::return_value_policy<bp::copy_const_reference>()),
46  &S_t::setTime)
47  .add_property("name",
48  bp::make_function(
49  &S_t::getName,
50  bp::return_value_policy<bp::copy_const_reference>()))
51 
52  .def("getName", &S_t::getName,
53  bp::return_value_policy<bp::copy_const_reference>())
54  .def(
55  "getClassName",
56  +[](const S_t& s) -> std::string {
57  std::string ret;
58  s.getClassName(ret);
59  return ret;
60  })
61 
62  .def("plug", &S_t::plug, "Plug the signal to another signal")
63  .def("unplug", &S_t::unplug, "Unplug the signal")
64  .def("isPlugged", &S_t::isPlugged, "Whether the signal is plugged")
65  .def("getPlugged", &S_t::getPluged,
66  bp::return_value_policy<bp::reference_existing_object>(),
67  "To which signal the signal is plugged")
68 
69  .def("recompute", &S_t::recompute, "Recompute the signal at given time")
70 
71  .def(
72  "__str__",
73  +[](const S_t& s) -> std::string {
74  std::ostringstream oss;
75  s.display(oss);
76  return oss.str();
77  })
78  .def(
79  "displayDependencies",
80  +[](const S_t& s, int depth) -> std::string {
81  std::ostringstream oss;
82  s.displayDependencies(oss, depth);
83  return oss.str();
84  },
85  "Print the signal dependencies in a string");
86 }
87 
88 template <>
91  bp::class_<S_t, bp::bases<SignalBase<sigtime_t> >, boost::noncopyable> obj(
92  name.c_str(), bp::init<std::string>());
93  obj.add_property(
94  "value",
95  +[](const S_t& signal) -> Matrix4 {
96  return signal.accessCopy().matrix();
97  },
98  +[](S_t& signal, const Matrix4& v) {
99  // TODO it isn't hard to support pinocchio::SE3 type here.
100  // However, this adds a dependency to pinocchio.
101  signal.setConstant(MatrixHomogeneous(v));
102  },
103  "the signal value.");
104  return obj;
105 }
106 
108  exposeSignalBase<sigtime_t>("SignalBase");
109 
110  exposeSignalsOfType<bool, sigtime_t>("Bool");
111  exposeSignalsOfType<unsigned int, sigtime_t>("UInt");
112  exposeSignalsOfType<int, sigtime_t>("Int");
113  exposeSignalsOfType<uint64_t, sigtime_t>("UInt64");
114  exposeSignalsOfType<int64_t, sigtime_t>("Int64");
115  exposeSignalsOfType<double, sigtime_t>("Double");
116 
117  exposeSignalsOfType<Vector, sigtime_t>("Vector");
118  exposeSignalsOfType<Vector3, sigtime_t>("Vector3");
119  exposeSignalsOfType<Vector7, sigtime_t>("Vector7");
120 
121  exposeSignalsOfType<Matrix, sigtime_t>("Matrix");
122  exposeSignalsOfType<MatrixRotation, sigtime_t>("MatrixRotation");
123  exposeSignalsOfType<MatrixHomogeneous, sigtime_t>("MatrixHomogeneous");
124  exposeSignalsOfType<MatrixTwist, sigtime_t>("MatrixTwist");
125 
126  exposeSignalsOfType<Quaternion, sigtime_t>("Quaternion");
127  exposeSignalsOfType<VectorUTheta, sigtime_t>("VectorUTheta");
128 }
129 
130 namespace signalBase {
131 
132 template <class T>
134  bp::object o,
135  std::string& error) {
136  typedef SignalWrapper<T, sigtime_t> SignalWrapper_t;
137  if (!SignalWrapper_t::checkCallable(o, error)) {
138  return NULL;
139  }
140 
141  SignalWrapper_t* obj = new SignalWrapper_t(name, o);
142  return obj;
143 }
144 
146  Entity* obj = entity::create("PythonSignalContainer", "python_signals");
147  return dynamic_cast<PythonSignalContainer*>(obj);
148 }
149 
150 #define SIGNAL_WRAPPER_TYPE(IF, Enum, Type) \
151  IF(command::Value::typeName(command::Value::Enum).compare(type) == 0) { \
152  obj = createSignalWrapperTpl<Type>(name, object, error); \
153  }
154 
158 SignalBase<sigtime_t>* createSignalWrapper(const char* name, const char* type,
159  bp::object object) {
161  if (psc == NULL) return NULL;
162 
163  SignalBase<sigtime_t>* obj = NULL;
164  std::string error;
165  if (command::Value::typeName(command::Value::BOOL).compare(type) == 0) {
166  obj = createSignalWrapperTpl<bool>(name, object, error);
167  } else if (command::Value::typeName(command::Value::UNSIGNED).compare(type) ==
168  0) {
169  obj = createSignalWrapperTpl<unsigned int>(name, object, error);
170  } else if (command::Value::typeName(command::Value::INT).compare(type) == 0) {
171  obj = createSignalWrapperTpl<int>(name, object, error);
173  .compare(type) == 0) {
174  obj = createSignalWrapperTpl<uint64_t>(name, object, error);
175  } else if (command::Value::typeName(command::Value::LONGINT).compare(type) ==
176  0) {
177  obj = createSignalWrapperTpl<int64_t>(name, object, error);
178  } else if (command::Value::typeName(command::Value::FLOAT).compare(type) ==
179  0) {
180  obj = createSignalWrapperTpl<float>(name, object, error);
181  } else if (command::Value::typeName(command::Value::DOUBLE).compare(type) ==
182  0) {
183  obj = createSignalWrapperTpl<double>(name, object, error);
184  } else if (command::Value::typeName(command::Value::VECTOR).compare(type) ==
185  0) {
186  obj = createSignalWrapperTpl<Vector>(name, object, error);
187  } else {
188  error = "Type not understood";
189  }
190 
191  if (obj == NULL) throw std::runtime_error(error);
192  // Register signal into the python signal container
193  psc->signalRegistration(*obj);
194 
195  // Return the pointer
196  return obj;
197 }
198 
199 } // namespace signalBase
200 } // namespace python
201 } // namespace dynamicgraph
signal-ptr.h
dynamicgraph::Signal
dynamicgraph::python::exposeSignal< MatrixHomogeneous, sigtime_t >
auto exposeSignal< MatrixHomogeneous, sigtime_t >(const std::string &name)
Definition: signal-base-py.cc:89
signal-time-dependent.h
boost::python
dynamicgraph::python::PythonSignalContainer::signalRegistration
void signalRegistration(const SignalArray< sigtime_t > &signals)
Definition: signal-wrapper.cc:11
dynamicgraph::python::Vector
Eigen::VectorXd Vector
Definition: signal-base-py.cc:28
dynamicgraph::python::signalBase::createSignalWrapperTpl
SignalWrapper< T, sigtime_t > * createSignalWrapperTpl(const char *name, bp::object o, std::string &error)
Definition: signal-base-py.cc:133
dynamicgraph::python::SignalWrapper
Definition: signal-wrapper.hh:31
dynamicgraph::python::Matrix4
Eigen::Matrix< double, 4, 4 > Matrix4
Definition: signal-base-py.cc:34
signal-base.h
dynamicgraph
dynamicgraph::command::Value::INT
INT
dynamicgraph::Entity
dynamic-graph-py.hh
signal-wrapper.hh
dynamicgraph::python::exposeSignalBase
void exposeSignalBase(const char *name)
Definition: signal-base-py.cc:39
dynamicgraph::command::Value::UNSIGNED
UNSIGNED
dynamicgraph::python::signalBase::createSignalWrapper
SignalBase< sigtime_t > * createSignalWrapper(const char *name, const char *type, bp::object object)
Create an instance of SignalWrapper.
Definition: signal-base-py.cc:158
python
dynamicgraph::python::exposeSignals
void exposeSignals()
Definition: signal-base-py.cc:107
dynamicgraph::python::Matrix
Eigen::MatrixXd Matrix
Definition: signal-base-py.cc:32
dynamicgraph::python::MatrixRotation
Eigen::Matrix< double, 3, 3 > MatrixRotation
Definition: signal-base-py.cc:33
dynamicgraph::command::Value::BOOL
BOOL
value.h
dynamicgraph::command::Value::DOUBLE
DOUBLE
dynamicgraph::python::entity::create
Entity * create(const char *type, const char *name)
Create an instance of Entity.
Definition: entity-py.cc:57
setup.name
name
Definition: setup.in.py:179
linear-algebra.h
dynamicgraph::python::Vector3
Eigen::Vector3d Vector3
Definition: signal-base-py.cc:29
dynamicgraph::python::Quaternion
Eigen::Quaternion< double > Quaternion
Definition: signal-base-py.cc:26
dynamicgraph::python::Vector7
Eigen::Matrix< double, 7, 1 > Vector7
Definition: signal-base-py.cc:30
signal.hh
dynamicgraph::python::MatrixTwist
Eigen::Matrix< double, 6, 6 > MatrixTwist
Definition: signal-base-py.cc:36
dynamicgraph::command::Value::VECTOR
VECTOR
v
v
dynamicgraph::command::Value::LONGINT
LONGINT
dynamicgraph::python::MatrixHomogeneous
Eigen::Transform< double, 3, Eigen::Affine > MatrixHomogeneous
Definition: signal-base-py.cc:35
dynamicgraph::python::signalBase::getPythonSignalContainer
PythonSignalContainer * getPythonSignalContainer()
Definition: signal-base-py.cc:145
dynamicgraph::python::plug
void plug(SignalBase< sigtime_t > *signalOut, SignalBase< sigtime_t > *signalIn)
plug a signal into another one.
Definition: dynamic-graph-py.cc:35
dynamicgraph::command::Value::typeName
static std::string typeName(Type type)
dynamicgraph::SignalBase
dynamicgraph::command::Value::FLOAT
FLOAT
dynamicgraph::command::Value::UNSIGNEDLONGINT
UNSIGNEDLONGINT
dynamicgraph::python::PythonSignalContainer
Definition: signal-wrapper.hh:19
dynamicgraph::python::VectorUTheta
Eigen::AngleAxis< double > VectorUTheta
Definition: signal-base-py.cc:25


dynamic-graph-python
Author(s): Nicolas Mansard, Olivier Stasse
autogenerated on Fri Oct 27 2023 02:16:36