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 #define DECLARE_RTT_VECTOR_EXPORTS( VectorType ) \
38 template class RTT_EXPORT RTT::internal::DataSourceTypeInfo< VectorType >; \
39 template class RTT_EXPORT RTT::internal::DataSource< VectorType >; \
40 template class RTT_EXPORT RTT::internal::AssignableDataSource< VectorType >; \
41 template class RTT_EXPORT RTT::internal::AssignCommand< VectorType >; \
42 template class RTT_EXPORT RTT::internal::ValueDataSource< VectorType >; \
43 template class RTT_EXPORT RTT::internal::ConstantDataSource< VectorType >; \
44 template class RTT_EXPORT RTT::internal::ReferenceDataSource< VectorType >; \
45 template class RTT_EXPORT RTT::OutputPort< VectorType >; \
46 template class RTT_EXPORT RTT::InputPort< VectorType >; \
47 template class RTT_EXPORT RTT::Property< VectorType >; \
48 template class RTT_EXPORT RTT::Attribute< VectorType >; \
49 template class RTT_EXPORT RTT::Constant< VectorType >;
50 
51 DECLARE_RTT_VECTOR_EXPORTS( Eigen::VectorXd )
56 
57 #define DECLARE_RTT_MATRIX_EXPORTS( MatrixType ) \
58 template class RTT_EXPORT RTT::internal::DataSourceTypeInfo< MatrixType >; \
59 template class RTT_EXPORT RTT::internal::DataSource< MatrixType >; \
60 template class RTT_EXPORT RTT::internal::AssignableDataSource< MatrixType >; \
61 template class RTT_EXPORT RTT::internal::AssignCommand< MatrixType >; \
62 template class RTT_EXPORT RTT::internal::ValueDataSource< MatrixType >; \
63 template class RTT_EXPORT RTT::internal::ConstantDataSource< MatrixType >; \
64 template class RTT_EXPORT RTT::internal::ReferenceDataSource< MatrixType >; \
65 template class RTT_EXPORT RTT::OutputPort< MatrixType >; \
66 template class RTT_EXPORT RTT::InputPort< MatrixType >; \
67 template class RTT_EXPORT RTT::Property< MatrixType >; \
68 template class RTT_EXPORT RTT::Attribute< MatrixType >; \
69 template class RTT_EXPORT RTT::Constant< MatrixType >;
70 
71 DECLARE_RTT_MATRIX_EXPORTS( Eigen::MatrixXd )
75 
76 #include <Eigen/Core>
77 namespace Eigen{
78 
79  using namespace RTT;
80  using namespace RTT::detail;
81  using namespace RTT::types;
82 
83  template<typename Derived>
84  std::istream& operator>>(std::istream &is, EigenBase<Derived>& v){
85  return is;
86  }
87 
88  template<typename VectorType>
89  double& get_item(VectorType& v, int index)
90  {
91  if (index >= (int) (v.size()) || index < 0)
93  return v[index];
94  }
95 
96  template<typename VectorType>
97  double get_item_copy(const VectorType& v, int index)
98  {
99  if (index >= (int) (v.size()) || index < 0)
101  return v[index];
102  }
103 
104  template<typename VectorType>
105  int get_size(const VectorType& v)
106  {
107  return v.size();
108  }
109 
110  template<class VectorType>
111  struct VectorTypeInfo : public types::TemplateTypeInfo<VectorType,true>
112 #if (RTT_VERSION_MAJOR*100+RTT_VERSION_MINOR) >= 206
113  , public MemberFactory
114 #endif
115  {
116  VectorTypeInfo(const std::string& type_name):TemplateTypeInfo<VectorType, true >(type_name)
117  {
118  };
119 
120 #if (RTT_VERSION_MAJOR*100+RTT_VERSION_MINOR) >= 206
121  bool installTypeInfoObject(TypeInfo* ti) {
122  // aquire a shared reference to the this object
123  typedef typename boost::shared_ptr< VectorTypeInfo > sh_ptr;
124  sh_ptr mthis = boost::dynamic_pointer_cast<VectorTypeInfo >( this->getSharedPtr() );
125 
126  assert(mthis);
127  // Allow base to install first
129  // Install the factories for primitive types
130  ti->setMemberFactory( mthis );
131  // Don't delete us, we're memory-managed.
132  return false;
133  }
134 #endif
135 
136  bool resize(base::DataSourceBase::shared_ptr arg, int size) const
137  {
138  if (arg->isAssignable()) {
141  asarg->set().conservativeResizeLike(VectorType::Zero(size));
142  asarg->updated();
143  return true;
144  }
145  return false;
146  }
147 
148  virtual std::vector<std::string> getMemberNames() const {
149  // only discover the parts of this struct:
150  std::vector<std::string> result;
151  result.push_back("size");
152  result.push_back("capacity");
153  return result;
154  }
155 
157  // the only thing we do is to check for an integer in name, otherwise, assume a part (size) is accessed:
158  try {
159  unsigned int indx = boost::lexical_cast<unsigned int>(name);
160  // @todo could also return a direct reference to item indx using another DS type that respects updated().
161  return getMember( item, new RTT::internal::ConstantDataSource<int>(indx));
162  } catch(...) {}
163 
164  return getMember( item, new RTT::internal::ConstantDataSource<std::string>(name) );
165  }
166 
169  // discover if user gave us a part name or index:
172  if ( id_name ) {
173  if ( id_name->get() == "size" || id_name->get() == "capacity") {
174  try {
176  } catch(...) {}
177  }
178  }
179 
180  if ( id_indx ) {
181  try {
182  if ( item->isAssignable() )
183  {
184  return RTT::internal::newFunctorDataSource(get_item<VectorType>, RTT::internal::GenerateDataSource()(item.get(), id_indx.get() ) );
185  }
186  else
187  {
188  return RTT::internal::newFunctorDataSource(get_item_copy<VectorType>, RTT::internal::GenerateDataSource()(item.get(), id_indx.get() ) );
189  }
190  } catch(...) {}
191  }
192  if (id_name) {
193  log(Error) << "EigenVectorTypeInfo: No such member : " << id_name->get() << endlog();
194  }
195  if (id_indx) {
196  log(Error) << "EigenVectorTypeInfo: Invalid index : " << id_indx->get() <<":"<< id_indx->getTypeName() << endlog();
197  }
198  if ( !id_name && ! id_indx)
199  log(Error) << "EigenVectorTypeInfo: Not a member or index : " << id <<":"<< id->getTypeName() << endlog();
201  }
202 
203 
204  virtual bool decomposeTypeImpl(const VectorType& vec, PropertyBag& targetbag) const
205  {
206  targetbag.setType(this->getTypeName());
207  int dimension = vec.rows();
208  std::string str;
209 
210  if(!targetbag.empty())
211  return false;
212 
213  for ( int i=0; i < dimension ; i++){
214  std::stringstream out;
215  out << i+1;
216  str = out.str();
217  targetbag.add( new Property<double>(str, str +"th element of vector",vec(i)) ); // Put variables in the bag
218  }
219 
220  return true;
221  };
222 
223  virtual bool composeTypeImpl(const PropertyBag& bag, VectorType& result) const{
224 
225  if ( bag.getType() == this->getTypeName() ) {
226  int dimension = bag.size();
227  result.conservativeResizeLike(VectorType::Zero(dimension));
228 
229  // Get values
230  for (int i = 0; i < dimension ; i++) {
231  std::stringstream out;
232  out << i+1;
233  Property<double> elem = bag.getProperty(out.str());
234  if(elem.ready())
235  result(i) = elem.get();
236  else{
237  log(Error)<<"Could not read element "<<i+1<<endlog();
238  return false;
239  }
240  }
241  }else{
242  log(Error) << "Composing Property< " << this->getTypeName() << " > :"
243  << " type mismatch, got type '"<< bag.getType()
244  << "', expected type "<<"eigen_vector."<<endlog();
245  return false;
246  }
247  return true;
248  };
249  };
250 
251  template<typename MatrixType>
252  struct MatrixTypeInfo : public types::TemplateTypeInfo<MatrixType,true>{
253  MatrixTypeInfo(const std::string& type_name):TemplateTypeInfo<MatrixType, true >(type_name){
254  };
255 
256 
257  bool decomposeTypeImpl(const MatrixType& mat, PropertyBag& targetbag) const{
258  targetbag.setType(this->getTypeName());
259  unsigned int dimension = mat.rows();
260  if(!targetbag.empty())
261  return false;
262 
263  for ( unsigned int i=0; i < dimension ; i++){
264  std::stringstream out;
265  out << i+1;
266  targetbag.add( new Property<VectorXd >(out.str(), out.str() +"th row of matrix",mat.row(i) )); // Put variables in the bag
267  }
268 
269  return true;
270  };
271 
272  bool composeTypeImpl(const PropertyBag& bag, MatrixType& result) const{
273  if ( bag.getType() == this->getTypeName() ) {
274  unsigned int rows = bag.size();
275  unsigned int cols = 0;
276  // Get values
277  for (unsigned int i = 0; i < rows ; i++) {
278  std::stringstream out;
279  out << i+1;
280  Property<PropertyBag> row_bag = bag.getProperty(out.str());
281  if(!row_bag.ready()){
282  log(Error)<<"Could not read row "<<i+1<<endlog();
283  return false;
284  }
285  Property<VectorXd > row_p(row_bag.getName(),row_bag.getDescription());
286  if(!(row_p.compose(row_bag))){
287  log(Error)<<"Could not compose row "<<i+1<<endlog();
288  return false;
289  }
290  if(row_p.ready()){
291  if(i==0){
292  cols = row_p.get().size();
293  result.resize(rows,cols);
294  } else
295  if(row_p.get().rows()!=(int)cols){
296  log(Error)<<"Row "<<i+1<<" size does not match matrix columns"<<endlog();
297  return false;
298  }
299  result.row(i)=row_p.get();
300  }else{
301  log(Error)<<"Property of Row "<<i+1<<"was not ready for use"<<endlog();
302  return false;
303  }
304  }
305  }else {
306  log(Error) << "Composing Property< " << this->getTypeName() << " > :"
307  << " type mismatch, got type '"<< bag.getType()
308  << "', expected type "<<"ublas_matrix."<<endlog();
309  return false;
310  }
311  return true;
312  };
313  };
314 
315  template<class VectorType>
317  : public std::binary_function<const VectorType&, int, double>
318  {
319  double operator()(const VectorType& v, int index) const
320  {
321  if ( index >= (int)(v.size()) || index < 0)
322  return 0.0;
323  return v(index);
324  }
325  };
326 
327  template<class VectorType>
329  : public std::unary_function<const VectorType&, int>
330  {
331  int operator()(const VectorType& cont ) const
332  {
333  return cont.rows();
334  }
335  };
336 
337  template<class VectorType>
339  : public std::binary_function<int,double,VectorType>
340  {
341  typedef VectorType (Signature)( int, double );
342  VectorType operator()(int size,double value ) const
343  {
344  return VectorType::Constant(size,value);
345  }
346  };
347 
348  template<class VectorType>
350  : public std::unary_function<std::vector<double>,VectorType>
351  {
352  typedef VectorType (Signature)( std::vector<double> );
353  VectorType operator()(std::vector<double> values) const
354  {
355  return VectorType::Map(values.data(),values.size());
356  }
357  };
358 
359  template<class VectorType>
361  : public std::unary_function<std::vector<double>,VectorType>
362  {
363  typedef VectorType (Signature)( std::vector<double> );
364  VectorType operator()(std::vector<double> values) const
365  {
366  int size = VectorType::RowsAtCompileTime;
367  if(size != Eigen::Dynamic && size != values.size())
368  {
369  log(Debug) << "Cannot copy an std vector of size " << values.size()
370  << " into an eigen vector of (fixed) size " << size
371  << endlog();
372  return VectorType::Constant(size, RTT::internal::NA<double&>::na());
373  }
374  // NOTE: this can resize the eigen_vector
375  return VectorType::Map(values.data(),values.size());
376  }
377  };
378 
379  template<class VectorType>
381  : public std::unary_function<int,VectorType>
382  {
383  typedef VectorType (Signature)( int );
384  VectorType operator()(int size ) const
385  {
386  return VectorType::Zero(size);
387  }
388  };
389 
390  template<typename MatrixType>
392  : public std::ternary_function<const MatrixType&, int, int, double>
393  {
394  double operator()(const MatrixType& m, int i, int j) const{
395  if ( i >= (int)(m.rows()) || i < 0 || j<0 || j>= (int)(m.cols()))
396  return 0.0;
397  return m(i,j);
398  }
399  };
400 
401  template<typename MatrixType>
403  : public std::binary_function<int,int,MatrixType>
404  {
405  typedef MatrixType (Signature)( int, int );
406  MatrixType operator()(int size1,int size2) const
407  {
408  return MatrixType::Zero(size1,size2);
409  }
410  };
411 
413  {
414  return "Eigen";
415  }
416 
418  {
419  RTT::types::TypeInfoRepository::Instance()->addType( new VectorTypeInfo<VectorXd>("eigen_vector") );
420  RTT::types::TypeInfoRepository::Instance()->addType( new VectorTypeInfo<Vector2d>("eigen_vector2") );
421  RTT::types::TypeInfoRepository::Instance()->addType( new VectorTypeInfo<Vector3d>("eigen_vector3") );
422  RTT::types::TypeInfoRepository::Instance()->addType( new VectorTypeInfo<Vector4d>("eigen_vector4") );
423  RTT::types::TypeInfoRepository::Instance()->addType( new VectorTypeInfo<Vector6d>("eigen_vector6") );
424  RTT::types::TypeInfoRepository::Instance()->addType( new MatrixTypeInfo<MatrixXd>("eigen_matrix") );
425  RTT::types::TypeInfoRepository::Instance()->addType( new MatrixTypeInfo<Matrix2d>("eigen_matrix2") );
426  RTT::types::TypeInfoRepository::Instance()->addType( new MatrixTypeInfo<Matrix3d>("eigen_matrix3") );
427  RTT::types::TypeInfoRepository::Instance()->addType( new MatrixTypeInfo<Matrix4d>("eigen_matrix4") );
428  return true;
429  }
430 
432  {
433  RTT::types::Types()->type("eigen_vector")->addConstructor(types::newConstructor(vector_size_constructor<VectorXd>()));
434  RTT::types::Types()->type("eigen_vector")->addConstructor(types::newConstructor(vector_size_value_constructor<VectorXd>()));
435 
436  RTT::types::Types()->type("eigen_vector")->addConstructor(types::newConstructor(vector_array_constructor<VectorXd>(),true));
437  RTT::types::Types()->type("eigen_vector2")->addConstructor(types::newConstructor(vector_fixed_array_constructor<Vector2d>(),true));
438  RTT::types::Types()->type("eigen_vector3")->addConstructor(types::newConstructor(vector_fixed_array_constructor<Vector3d>(),true));
439  RTT::types::Types()->type("eigen_vector4")->addConstructor(types::newConstructor(vector_fixed_array_constructor<Vector4d>(),true));
440  RTT::types::Types()->type("eigen_vector6")->addConstructor(types::newConstructor(vector_fixed_array_constructor<Vector6d>(),true));
441 
442  RTT::types::Types()->type("eigen_matrix")->addConstructor(types::newConstructor(matrix_i_j_constructor<MatrixXd>()));
443  return true;
444  }
445 
447  {
453  //RTT::types::OperatorRepository::Instance()->add( newDotOperator( "size", get_size() ) );
454  //RTT::types::OperatorRepository::Instance()->add( newTernaryOperator( "[,]", matrix_index() ) );
455  return true;
456  }
457 }
458 
#define ORO_TYPEKIT_PLUGIN(TYPEKIT)
DataSourceType get() const
virtual bool composeTypeImpl(const PropertyBag &bag, VectorType &result) const
virtual result_t get() const =0
Matrix< double, 6, 1 > Vector6d
void add(base::PropertyBase *p)
VectorType operator()(int size, double value) const
virtual std::string getName()
virtual std::vector< std::string > getMemberNames() const
const std::string & getType() const
static shared_ptr Instance()
MatrixType operator()(int size1, int size2) const
bool composeTypeImpl(const PropertyBag &bag, MatrixType &result) const
base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, const std::string &name) const
void setType(const std::string &newtype)
VectorType operator()(std::vector< double > values) const
base::DataSourceBase * newFunctorDataSource(Function f, const std::vector< base::DataSourceBase::shared_ptr > &args)
double operator()(const MatrixType &m, int i, int j) const
#define DECLARE_RTT_VECTOR_EXPORTS(VectorType)
VectorTypeInfo(const std::string &type_name)
bool decomposeTypeImpl(const MatrixType &mat, PropertyBag &targetbag) const
bool resize(base::DataSourceBase::shared_ptr arg, int size) const
#define DECLARE_RTT_MATRIX_EXPORTS(MatrixType)
MatrixTypeInfo(const std::string &type_name)
BinaryOperator< function > * newBinaryOperator(const char *op, function f)
Debug
const std::string & getDescription() const
static AssignableDataSource< T > * narrow(base::DataSourceBase *db)
void setMemberFactory(MemberFactoryPtr mf)
TypeInfoRepository::shared_ptr Types()
bool empty() const
Error
VectorType operator()(std::vector< double > values) const
boost::intrusive_ptr< DataSource< T > > shared_ptr
boost::intrusive_ptr< AssignableDataSource< T > > shared_ptr
VectorType operator()(int size) const
base::PropertyBase * getProperty(const std::string &name) const
static shared_ptr Instance()
virtual bool decomposeTypeImpl(const VectorType &vec, PropertyBag &targetbag) const
static DataSource< T > * narrow(base::DataSourceBase *db)
double get_item_copy(const VectorType &v, int index)
double & get_item(VectorType &v, int index)
bool installTypeInfoObject(TypeInfo *ti)
int get_size(const VectorType &v)
std::istream & operator>>(std::istream &is, EigenBase< Derived > &v)
base::DataSourceBase::shared_ptr getMember(base::DataSourceBase::shared_ptr item, base::DataSourceBase::shared_ptr id) const
virtual std::string getTypeName() const
const std::string & getName() const
int operator()(const VectorType &cont) const
boost::intrusive_ptr< DataSourceBase > shared_ptr
double operator()(const VectorType &v, int index) const
size_t size() const
virtual bool loadConstructors()


eigen_typekit
Author(s): Ruben Smits
autogenerated on Tue Jan 5 2021 03:41:40