ocra-recipes
Doxygen documentation for the ocra-recipes repository
YarpUtilities.h
Go to the documentation of this file.
1 #ifndef YARP_UTILITIES_H
2 #define YARP_UTILITIES_H
3 
4 #include <iostream>
5 #include <vector>
6 #include <Eigen/Dense>
7 #include <Eigen/Lgsm>
8 #include <yarp/os/Bottle.h>
9 
10 namespace ocra
11 {
12 
13 namespace util
14 {
15 
16 inline std::vector<double> pourBottleIntoStdVector(yarp::os::Bottle bottle, int& indexesToSkip)
17 {
18  int nVals = bottle.get(0).asInt();
19  std::vector<double> returnVector(nVals);
20  for (auto i = 0; i < nVals; ++i) {
21  returnVector[i] = bottle.tail().get(i).asDouble();
22  }
23  indexesToSkip = nVals + 1;
24  return returnVector;
25 }
26 
27 inline Eigen::VectorXd pourBottleIntoEigenVector(yarp::os::Bottle bottle, int& indexesToSkip)
28 {
29  int nVals = bottle.get(0).asInt();
30  Eigen::VectorXd returnVector(nVals);
31  for (auto i = 0; i < nVals; ++i) {
32  returnVector(i) = bottle.tail().get(i).asDouble();
33  }
34  indexesToSkip = nVals + 1;
35  return returnVector;
36 }
37 
38 inline Eigen::VectorXi pourBottleIntoEigenVectorXi(yarp::os::Bottle bottle, int& indexesToSkip)
39 {
40  int nVals = bottle.get(0).asInt();
41  Eigen::VectorXi returnVector(nVals);
42  for (auto i = 0; i < nVals; ++i) {
43  returnVector(i) = bottle.tail().get(i).asInt();
44  }
45  indexesToSkip = nVals + 1;
46  return returnVector;
47 }
48 
49 inline Eigen::Displacementd pourBottleIntoDisplacementd(yarp::os::Bottle bottle, int& indexesToSkip)
50 {
51  int nVals = bottle.get(0).asInt();
52  double x = bottle.get(1).asDouble();
53  double y = bottle.get(2).asDouble();
54  double z = bottle.get(3).asDouble();
55  double qw = bottle.get(4).asDouble();
56  double qx = bottle.get(5).asDouble();
57  double qy = bottle.get(6).asDouble();
58  double qz = bottle.get(7).asDouble();
59  Eigen::Displacementd disp(x,y,z,qw,qx,qy,qz);
60  indexesToSkip = nVals + 1;
61  return disp;
62 }
63 
64 inline Eigen::Twistd pourBottleIntoTwistd(yarp::os::Bottle bottle, int& indexesToSkip)
65 {
66  int nVals = bottle.get(0).asInt();
67  double rx = bottle.get(1).asDouble();
68  double ry = bottle.get(2).asDouble();
69  double rz = bottle.get(3).asDouble();
70  double vx = bottle.get(4).asDouble();
71  double vy = bottle.get(5).asDouble();
72  double vz = bottle.get(6).asDouble();
73  Eigen::Twistd twist(rx,ry,rz,vx,vy,vz);
74  indexesToSkip = nVals + 1;
75  return twist;
76 }
77 
78 inline Eigen::Wrenchd pourBottleIntoWrenchd(yarp::os::Bottle bottle, int& indexesToSkip)
79 {
80  int nVals = bottle.get(0).asInt();
81  double tx = bottle.get(1).asDouble();
82  double ty = bottle.get(2).asDouble();
83  double tz = bottle.get(3).asDouble();
84  double fx = bottle.get(4).asDouble();
85  double fy = bottle.get(5).asDouble();
86  double fz = bottle.get(6).asDouble();
87  Eigen::Wrenchd wrench(tx,ty,tz,fx,fy,fz);
88  indexesToSkip = nVals + 1;
89  return wrench;
90 }
91 
92 inline Eigen::MatrixXd pourBottleIntoEigenMatrix(yarp::os::Bottle bottle, int& indexesToSkip)
93 {
94  int nRows = bottle.get(0).asInt();
95  int nCols = bottle.get(1).asInt();
96  Eigen::MatrixXd returnMatrix(nRows,nCols);
97 
98  int startIndex = 2;
99  for (auto i = 0; i < nRows; ++i) {
100  for (auto j = 0; j < nCols; ++j) {
101  returnMatrix(i,j) = bottle.get(startIndex).asDouble();
102  ++startIndex;
103  }
104  }
105  indexesToSkip = nRows + nCols + 1;
106  return returnMatrix;
107 }
108 
109 inline void pourStdVectorIntoBottle(const std::vector<double>& vec, yarp::os::Bottle& bottle)
110 {
111  if(vec.size()>0) {
112  bottle.addInt(vec.size());
113  for (auto val : vec) {
114  bottle.addDouble(val);
115  }
116  } else {
117  bottle.addInt(0);
118  }
119 }
120 
121 inline void pourEigenVectorIntoBottle(const Eigen::VectorXd& vec, yarp::os::Bottle& bottle)
122 {
123  if(vec.size()>0) {
124  bottle.addInt(vec.size());
125  for (auto i=0; i<vec.size(); ++i) {
126  bottle.addDouble(vec(i));
127  }
128  } else {
129  bottle.addInt(0);
130  }
131 }
132 
133 inline void pourEigenVectorXiIntoBottle(const Eigen::VectorXi& vec, yarp::os::Bottle& bottle)
134 {
135  if(vec.size()>0) {
136  bottle.addInt(vec.size());
137  for (auto i=0; i<vec.size(); ++i) {
138  bottle.addInt(vec(i));
139  }
140  } else {
141  bottle.addInt(0);
142  }
143 }
144 
145 inline void pourEigenMatrixIntoBottle(const Eigen::MatrixXd& mat, yarp::os::Bottle& bottle)
146 {
147  if(mat.size()>0) {
148  bottle.addInt(mat.rows());
149  bottle.addInt(mat.cols());
150  for (auto i=0; i<mat.rows(); ++i) {
151  for (auto j=0; j<mat.cols(); ++j) {
152  bottle.addDouble(mat(i,j));
153  }
154  }
155  } else {
156  bottle.addInt(0);
157  }
158 }
159 
160 inline void pourDisplacementdIntoBottle(const Eigen::Displacementd& disp, yarp::os::Bottle& bottle)
161 {
162  const int DISPLACEMENT_VECTOR_SIZE = 7;
163  bottle.addInt(DISPLACEMENT_VECTOR_SIZE);
164 
165  bottle.addDouble(disp.x());
166  bottle.addDouble(disp.y());
167  bottle.addDouble(disp.z());
168  bottle.addDouble(disp.qw());
169  bottle.addDouble(disp.qx());
170  bottle.addDouble(disp.qy());
171  bottle.addDouble(disp.qz());
172 
173 }
174 
175 inline void pourTwistdIntoBottle(const Eigen::Twistd& twist, yarp::os::Bottle& bottle)
176 {
177  const int TWIST_VECTOR_SIZE = 6;
178  bottle.addInt(TWIST_VECTOR_SIZE);
179 
180  double rx = twist(0);
181  double ry = twist(1);
182  double rz = twist(2);
183  double vx = twist(3);
184  double vy = twist(4);
185  double vz = twist(5);
186 
187  bottle.addDouble(rx);
188  bottle.addDouble(ry);
189  bottle.addDouble(rz);
190  bottle.addDouble(vx);
191  bottle.addDouble(vy);
192  bottle.addDouble(vz);
193 }
194 
195 inline void pourWrenchdIntoBottle(const Eigen::Wrenchd& wrench, yarp::os::Bottle& bottle)
196 {
197  const int WRENCH_VECTOR_SIZE = 6;
198  bottle.addInt(WRENCH_VECTOR_SIZE);
199 
200  bottle.addDouble(wrench.tx());
201  bottle.addDouble(wrench.ty());
202  bottle.addDouble(wrench.tz());
203  bottle.addDouble(wrench.fx());
204  bottle.addDouble(wrench.fy());
205  bottle.addDouble(wrench.fz());
206 }
207 
208 inline yarp::os::Bottle trimBottle(const yarp::os::Bottle& bottle, int startIndex, int endIndex=-1)
209 {
210  int btlSize = bottle.size();
211  int lastIndex = btlSize - 1;
212  int trimFrom, trimTo;
213 
214 
215  if (endIndex==-1) {
216  trimTo = btlSize;
217  } else {
218  trimTo = (endIndex <= btlSize) ? endIndex : btlSize;
219  }
220 
221  trimFrom = (startIndex >= 0) ? startIndex : 0;
222  trimFrom = ( startIndex < (trimTo-2) ) ? startIndex : (trimTo-2);
223 
224  yarp::os::Bottle tmpBtl;
225  while(trimFrom<trimTo)
226  {
227  tmpBtl.add(bottle.get(trimFrom));
228  ++trimFrom;
229  }
230  return tmpBtl;
231 }
232 
233 
234 } // namespace utils
235 } // namespace ocra
236 #endif // YARP_UTILITIES_H
void pourEigenVectorXiIntoBottle(const Eigen::VectorXi &vec, yarp::os::Bottle &bottle)
Eigen::Wrenchd pourBottleIntoWrenchd(yarp::os::Bottle bottle, int &indexesToSkip)
Definition: YarpUtilities.h:78
std::vector< double > pourBottleIntoStdVector(yarp::os::Bottle bottle, int &indexesToSkip)
Definition: YarpUtilities.h:16
yarp::os::Bottle trimBottle(const yarp::os::Bottle &bottle, int startIndex, int endIndex=-1)
void pourDisplacementdIntoBottle(const Eigen::Displacementd &disp, yarp::os::Bottle &bottle)
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
Eigen::MatrixXd pourBottleIntoEigenMatrix(yarp::os::Bottle bottle, int &indexesToSkip)
Definition: YarpUtilities.h:92
Eigen::VectorXi pourBottleIntoEigenVectorXi(yarp::os::Bottle bottle, int &indexesToSkip)
Definition: YarpUtilities.h:38
void pourEigenMatrixIntoBottle(const Eigen::MatrixXd &mat, yarp::os::Bottle &bottle)
Eigen::Twistd pourBottleIntoTwistd(yarp::os::Bottle bottle, int &indexesToSkip)
Definition: YarpUtilities.h:64
void pourTwistdIntoBottle(const Eigen::Twistd &twist, yarp::os::Bottle &bottle)
void pourStdVectorIntoBottle(const std::vector< double > &vec, yarp::os::Bottle &bottle)
void pourEigenVectorIntoBottle(const Eigen::VectorXd &vec, yarp::os::Bottle &bottle)
Eigen::VectorXd pourBottleIntoEigenVector(yarp::os::Bottle bottle, int &indexesToSkip)
Definition: YarpUtilities.h:27
void pourWrenchdIntoBottle(const Eigen::Wrenchd &wrench, yarp::os::Bottle &bottle)
Eigen::Displacementd pourBottleIntoDisplacementd(yarp::os::Bottle bottle, int &indexesToSkip)
Definition: YarpUtilities.h:49