eigen_typekit.cpp
Go to the documentation of this file.
1 // Copyright (C) 2008 Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
2 
3 // Author: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
4 // Maintainer: Ruben Smits <ruben dot smits at mech dot kuleuven dot be>
5 
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
15 
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 
21 #include <rtt/Property.hpp>
22 #include <rtt/PropertyBag.hpp>
24 #include <rtt/types/Operators.hpp>
27 #include <rtt/types/Types.hpp>
28 #include <rtt/Logger.hpp>
30 #include <rtt/internal/mystd.hpp>
35 #include <boost/lexical_cast.hpp>
36 
37 
39 template class RTT_EXPORT RTT::internal::DataSource< Eigen::VectorXd >;
41 template class RTT_EXPORT RTT::internal::AssignCommand< Eigen::VectorXd >;
45 template class RTT_EXPORT RTT::OutputPort< Eigen::VectorXd >;
46 template class RTT_EXPORT RTT::InputPort< Eigen::VectorXd >;
47 template class RTT_EXPORT RTT::Property< Eigen::VectorXd >;
48 template class RTT_EXPORT RTT::Attribute< Eigen::VectorXd >;
49 template class RTT_EXPORT RTT::Constant< Eigen::VectorXd >;
50 
52 template class RTT_EXPORT RTT::internal::DataSource< Eigen::MatrixXd >;
54 template class RTT_EXPORT RTT::internal::AssignCommand< Eigen::MatrixXd >;
58 template class RTT_EXPORT RTT::OutputPort< Eigen::MatrixXd >;
59 template class RTT_EXPORT RTT::InputPort< Eigen::MatrixXd >;
60 template class RTT_EXPORT RTT::Property< Eigen::MatrixXd >;
61 template class RTT_EXPORT RTT::Attribute< Eigen::MatrixXd >;
62 template class RTT_EXPORT RTT::Constant< Eigen::MatrixXd >;
63 
64 
65 #include <Eigen/Core>
66 namespace Eigen{
67 
68  using namespace RTT;
69  using namespace RTT::detail;
70  using namespace RTT::types;
71 
72  std::istream& operator>>(std::istream &is,MatrixXd& v){
73  return is;
74  }
75  std::istream& operator>>(std::istream &is,VectorXd& v){
76  return is;
77  }
78 
79  double& get_item(VectorXd& v, int index)
80  {
81  if (index >= (int) (v.size()) || index < 0)
83  return v[index];
84  }
85 
86  double get_item_copy(const VectorXd& v, int index)
87  {
88  if (index >= (int) (v.size()) || index < 0)
90  return v[index];
91  }
92 
93 
94  int get_size(const VectorXd& v)
95  {
96  return v.size();
97  }
98 
100 #if (RTT_VERSION_MAJOR*100+RTT_VERSION_MINOR) >= 206
101  , public MemberFactory
102 #endif
103  {
104  VectorTypeInfo():TemplateTypeInfo<VectorXd, true >("eigen_vector")
105  {
106  };
107 
108 #if (RTT_VERSION_MAJOR*100+RTT_VERSION_MINOR) >= 206
109  bool installTypeInfoObject(TypeInfo* ti) {
110  // aquire a shared reference to the this object
111  boost::shared_ptr< VectorTypeInfo > mthis = boost::dynamic_pointer_cast<VectorTypeInfo >( this->getSharedPtr() );
112  assert(mthis);
113  // Allow base to install first
115  // Install the factories for primitive types
116  ti->setMemberFactory( mthis );
117  // Don't delete us, we're memory-managed.
118  return false;
119  }
120 #endif
121 
122  bool resize(base::DataSourceBase::shared_ptr arg, int size) const
123  {
124  if (arg->isAssignable()) {
126  asarg->set().resize( size );
127  asarg->updated();
128  return true;
129  }
130  return false;
131  }
132 
133  virtual std::vector<std::string> getMemberNames() const {
134  // only discover the parts of this struct:
135  std::vector<std::string> result;
136  result.push_back("size");
137  result.push_back("capacity");
138  return result;
139  }
140 
142  // the only thing we do is to check for an integer in name, otherwise, assume a part (size) is accessed:
143  try {
144  unsigned int indx = boost::lexical_cast<unsigned int>(name);
145  // @todo could also return a direct reference to item indx using another DS type that respects updated().
146  return getMember( item, new RTT::internal::ConstantDataSource<int>(indx));
147  } catch(...) {}
148 
149  return getMember( item, new RTT::internal::ConstantDataSource<std::string>(name) );
150  }
151 
154  // discover if user gave us a part name or index:
157  if ( id_name ) {
158  if ( id_name->get() == "size" || id_name->get() == "capacity") {
159  try {
161  } catch(...) {}
162  }
163  }
164 
165  if ( id_indx ) {
166  try {
167  if ( item->isAssignable() )
169  else
171  } catch(...) {}
172  }
173  if (id_name) {
174  log(Error) << "EigenVectorTypeInfo: No such member : " << id_name->get() << endlog();
175  }
176  if (id_indx) {
177  log(Error) << "EigenVectorTypeInfo: Invalid index : " << id_indx->get() <<":"<< id_indx->getTypeName() << endlog();
178  }
179  if ( !id_name && ! id_indx)
180  log(Error) << "EigenVectorTypeInfo: Not a member or index : " << id <<":"<< id->getTypeName() << endlog();
182  }
183 
184 
185  virtual bool decomposeTypeImpl(const VectorXd& vec, PropertyBag& targetbag) const
186  {
187  targetbag.setType("eigen_vector");
188  int dimension = vec.rows();
189  std::string str;
190 
191  if(!targetbag.empty())
192  return false;
193 
194  for ( int i=0; i < dimension ; i++){
195  std::stringstream out;
196  out << i+1;
197  str = out.str();
198  targetbag.add( new Property<double>(str, str +"th element of vector",vec(i)) ); // Put variables in the bag
199  }
200 
201  return true;
202  };
203 
204  virtual bool composeTypeImpl(const PropertyBag& bag, VectorXd& result) const{
205 
206  if ( bag.getType() == "eigen_vector" ) {
207  int dimension = bag.size();
208  result.resize( dimension );
209 
210  // Get values
211  for (int i = 0; i < dimension ; i++) {
212  std::stringstream out;
213  out << i+1;
214  Property<double> elem = bag.getProperty(out.str());
215  if(elem.ready())
216  result(i) = elem.get();
217  else{
218  log(Error)<<"Could not read element "<<i+1<<endlog();
219  return false;
220  }
221  }
222  }else{
223  log(Error) << "Composing Property< VectorXd > :"
224  << " type mismatch, got type '"<< bag.getType()
225  << "', expected type "<<"eigen_vector."<<endlog();
226  return false;
227  }
228  return true;
229  };
230  };
231 
232  struct MatrixTypeInfo : public types::TemplateTypeInfo<MatrixXd,true>{
233  MatrixTypeInfo():TemplateTypeInfo<MatrixXd, true >("eigen_matrix"){
234  };
235 
236 
237  bool decomposeTypeImpl(const MatrixXd& mat, PropertyBag& targetbag) const{
238  targetbag.setType("eigen_matrix");
239  unsigned int dimension = mat.rows();
240  if(!targetbag.empty())
241  return false;
242 
243  for ( unsigned int i=0; i < dimension ; i++){
244  std::stringstream out;
245  out << i+1;
246  targetbag.add( new Property<VectorXd >(out.str(), out.str() +"th row of matrix",mat.row(i) )); // Put variables in the bag
247  }
248 
249  return true;
250  };
251 
252  bool composeTypeImpl(const PropertyBag& bag, MatrixXd& result) const{
253  if ( bag.getType() == "eigen_matrix" ) {
254  unsigned int rows = bag.size();
255  unsigned int cols = 0;
256  // Get values
257  for (unsigned int i = 0; i < rows ; i++) {
258  std::stringstream out;
259  out << i+1;
260  Property<PropertyBag> row_bag = bag.getProperty(out.str());
261  if(!row_bag.ready()){
262  log(Error)<<"Could not read row "<<i+1<<endlog();
263  return false;
264  }
265  Property<VectorXd > row_p(row_bag.getName(),row_bag.getDescription());
266  if(!(row_p.compose(row_bag))){
267  log(Error)<<"Could not compose row "<<i+1<<endlog();
268  return false;
269  }
270  if(row_p.ready()){
271  if(i==0){
272  cols = row_p.get().size();
273  result.resize(rows,cols);
274  } else
275  if(row_p.get().rows()!=(int)cols){
276  log(Error)<<"Row "<<i+1<<" size does not match matrix columns"<<endlog();
277  return false;
278  }
279  result.row(i)=row_p.get();
280  }else{
281  log(Error)<<"Property of Row "<<i+1<<"was not ready for use"<<endlog();
282  return false;
283  }
284  }
285  }else {
286  log(Error) << "Composing Property< MatrixXd > :"
287  << " type mismatch, got type '"<< bag.getType()
288  << "', expected type "<<"ublas_matrix."<<endlog();
289  return false;
290  }
291  return true;
292  };
293  };
294 
296  : public std::binary_function<const VectorXd&, int, double>
297  {
298  double operator()(const VectorXd& v, int index) const
299  {
300  if ( index >= (int)(v.size()) || index < 0)
301  return 0.0;
302  return v(index);
303  }
304  };
305 
306  struct get_size
307  : public std::unary_function<const VectorXd&, int>
308  {
309  int operator()(const VectorXd& cont ) const
310  {
311  return cont.rows();
312  }
313  };
314 
316  : public std::binary_function<int,double,const VectorXd&>
317  {
318  typedef const VectorXd& (Signature)( int, double );
319  mutable boost::shared_ptr< VectorXd > ptr;
321  ptr( new VectorXd ){}
322  const VectorXd& operator()(int size,double value ) const
323  {
324  ptr->resize(size);
325  (*ptr)=Eigen::VectorXd::Constant(size,value);
326  return *(ptr);
327  }
328  };
329 
331  : public std::unary_function<std::vector<double >,const VectorXd&>
332  {
333  typedef const VectorXd& (Signature)( std::vector<double > );
334  mutable boost::shared_ptr< VectorXd > ptr;
336  ptr( new VectorXd ){}
337  const VectorXd& operator()(std::vector<double > values) const
338  {
339  (*ptr)=VectorXd::Map(&values[0],values.size());
340  return *(ptr);
341  }
342  };
343 
345  : public std::unary_function<int,const VectorXd&>
346  {
347  typedef const VectorXd& (Signature)( int );
348  mutable boost::shared_ptr< VectorXd > ptr;
350  ptr( new VectorXd() ){}
351  const VectorXd& operator()(int size ) const
352  {
353  ptr->resize(size);
354  return *(ptr);
355  }
356  };
357 
359  : public std::ternary_function<const MatrixXd&, int, int, double>
360  {
361  double operator()(const MatrixXd& m, int i, int j) const{
362  if ( i >= (int)(m.rows()) || i < 0 || j<0 || j>= (int)(m.cols()))
363  return 0.0;
364  return m(i,j);
365  }
366  };
367 
369  : public std::binary_function<int,int,const MatrixXd&>
370  {
371  typedef const MatrixXd& (Signature)( int, int );
372  mutable boost::shared_ptr< MatrixXd > ptr;
374  ptr( new MatrixXd() ){}
375  const MatrixXd& operator()(int size1,int size2) const
376  {
377  ptr->resize(size1,size2);
378  return *(ptr);
379  }
380  };
381 
382 
383  std::string EigenTypekitPlugin::getName()
384  {
385  return "Eigen";
386  }
387 
388  bool EigenTypekitPlugin::loadTypes()
389  {
392  return true;
393  }
394 
395  bool EigenTypekitPlugin::loadConstructors()
396  {
397  RTT::types::Types()->type("eigen_vector")->addConstructor(types::newConstructor(vector_index_constructor()));
398  RTT::types::Types()->type("eigen_vector")->addConstructor(types::newConstructor(vector_index_value_constructor()));
399  RTT::types::Types()->type("eigen_vector")->addConstructor(types::newConstructor(vector_index_array_constructor()));
400  RTT::types::Types()->type("eigen_matrix")->addConstructor(types::newConstructor(matrix_i_j_constructor()));
401  return true;
402  }
403 
404  bool EigenTypekitPlugin::loadOperators()
405  {
407  //RTT::types::OperatorRepository::Instance()->add( newDotOperator( "size", get_size() ) );
408  //RTT::types::OperatorRepository::Instance()->add( newTernaryOperator( "[,]", matrix_index() ) );
409  return true;
410  }
411 }
412 
#define ORO_TYPEKIT_PLUGIN(TYPEKIT)
boost::shared_ptr< VectorXd > ptr
DataSourceType get() const
const VectorXd &( Signature)(int)
virtual result_t get() const =0
double operator()(const VectorXd &v, int index) const
void add(base::PropertyBase *p)
virtual bool decomposeTypeImpl(const VectorXd &vec, PropertyBag &targetbag) const
const MatrixXd & operator()(int size1, int size2) const
base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, base::DataSourceBase::shared_ptr id) const
std::istream & operator>>(std::istream &is, BufferPolicy &bp)
const VectorXd & operator()(int size) const
const std::string & getType() const
static shared_ptr Instance()
const MatrixXd &( Signature)(int, int)
virtual bool composeTypeImpl(const PropertyBag &bag, VectorXd &result) const
virtual void set(param_t t)=0
void setType(const std::string &newtype)
double get_item_copy(const VectorXd &v, int index)
base::DataSourceBase * newFunctorDataSource(Function f, const std::vector< base::DataSourceBase::shared_ptr > &args)
const VectorXd & operator()(std::vector< double > values) const
const VectorXd &( Signature)(int, double)
double & get_item(VectorXd &v, int index)
bool composeTypeImpl(const PropertyBag &bag, MatrixXd &result) const
bool decomposeTypeImpl(const MatrixXd &mat, PropertyBag &targetbag) const
TypeConstructor * newConstructor(Function *foo, bool automatic=false)
BinaryOperator< function > * newBinaryOperator(const char *op, function f)
const std::string & getDescription() const
virtual std::vector< std::string > getMemberNames() const
static AssignableDataSource< T > * narrow(base::DataSourceBase *db)
void setMemberFactory(MemberFactoryPtr mf)
TypeInfoRepository::shared_ptr Types()
bool empty() const
boost::shared_ptr< MatrixXd > ptr
boost::intrusive_ptr< DataSource< T > > shared_ptr
boost::intrusive_ptr< AssignableDataSource< T > > shared_ptr
base::PropertyBase * getProperty(const std::string &name) const
static shared_ptr Instance()
static DataSource< T > * narrow(base::DataSourceBase *db)
const VectorXd &( Signature)(std::vector< double >)
base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, const std::string &name) const
virtual std::string getTypeName() const
const std::string & getName() const
boost::intrusive_ptr< DataSourceBase > shared_ptr
boost::shared_ptr< VectorXd > ptr
size_t size() const
double operator()(const MatrixXd &m, int i, int j) const
const VectorXd & operator()(int size, double value) const
static Logger & log()
int operator()(const VectorXd &cont) const
bool resize(base::DataSourceBase::shared_ptr arg, int size) const
static Logger::LogFunction endlog()
boost::shared_ptr< VectorXd > ptr


eigen_typekit
Author(s): Ruben Smits
autogenerated on Wed Jul 3 2019 19:39:42