conversions.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018, University of Edinburgh
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without specific
15 // prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
30 #ifndef EXOTICA_CORE_CONVERSIONS_H_
31 #define EXOTICA_CORE_CONVERSIONS_H_
32 
33 #include <map>
34 #include <memory>
35 #include <vector>
36 
37 #include <kdl/frames.hpp>
38 #include <kdl/jacobian.hpp>
39 
40 #include <Eigen/Dense>
41 #pragma GCC diagnostic push
42 #pragma GCC diagnostic ignored "-Wfloat-conversion"
43 #include <unsupported/Eigen/CXX11/Tensor>
44 #pragma GCC diagnostic pop
45 
48 
49 namespace Eigen
50 {
52 typedef const Eigen::Ref<const Eigen::VectorXd>& VectorXdRefConst;
53 typedef const Eigen::Ref<const Eigen::MatrixXd>& MatrixXdRefConst;
54 typedef typename Eigen::Ref<Eigen::VectorXd> VectorXdRef;
55 typedef typename Eigen::Ref<Eigen::MatrixXd> MatrixXdRef;
56 
57 Eigen::VectorXd VectorTransform(double px = 0.0, double py = 0.0, double pz = 0.0, double qx = 0.0, double qy = 0.0, double qz = 0.0, double qw = 1.0);
58 Eigen::VectorXd IdentityTransform();
59 
60 template <typename T>
61 using MatrixType = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>;
62 template <typename Scalar, int rank, typename sizeType>
63 inline MatrixType<Scalar> TensorToMatrix(const Eigen::Tensor<Scalar, rank>& tensor, const sizeType rows, const sizeType cols)
64 {
65  return Eigen::Map<const MatrixType<Scalar>>(tensor.data(), rows, cols);
66 }
67 
68 template <typename Scalar, typename... Dims>
69 inline Eigen::Tensor<Scalar, sizeof...(Dims)> MatrixToTensor(const MatrixType<Scalar>& matrix, Dims... dims)
70 {
71  constexpr int rank = sizeof...(Dims);
72  return Eigen::TensorMap<Eigen::Tensor<const Scalar, rank>>(matrix.data(), {dims...});
73 }
74 
75 } // namespace Eigen
76 
77 namespace exotica
78 {
79 enum class RotationType
80 {
81  QUATERNION,
82  RPY,
83  ZYX,
84  ZYZ,
85  ANGLE_AXIS,
86  MATRIX
87 };
88 
90 
91 Eigen::VectorXd SetRotation(const KDL::Rotation& data, RotationType type);
92 
93 inline int GetRotationTypeLength(const RotationType& type)
94 {
95  static constexpr int types[] = {4, 3, 3, 3, 3, 9};
96  return types[static_cast<int>(type)];
97 }
98 
99 inline RotationType GetRotationTypeFromString(const std::string& rotation_type)
100 {
101  if (rotation_type == "Quaternion")
102  {
103  return RotationType::QUATERNION;
104  }
105  else if (rotation_type == "RPY")
106  {
107  return RotationType::RPY;
108  }
109  else if (rotation_type == "ZYX")
110  {
111  return RotationType::ZYX;
112  }
113  else if (rotation_type == "ZYZ")
114  {
115  return RotationType::ZYZ;
116  }
117  else if (rotation_type == "AngleAxis")
118  {
119  return RotationType::ANGLE_AXIS;
120  }
121  else if (rotation_type == "Matrix")
122  {
123  return RotationType::MATRIX;
124  }
125  else
126  {
127  ThrowPretty("Unsupported rotation type '" << rotation_type << "'");
128  }
129 }
130 
132 
134 
135 Eigen::MatrixXd GetFrame(const KDL::Frame& val);
136 
137 Eigen::VectorXd GetFrameAsVector(const KDL::Frame& val, RotationType type = RotationType::RPY);
138 
139 Eigen::VectorXd GetRotationAsVector(const KDL::Frame& val, RotationType type);
140 
141 inline void NormalizeQuaternionInConfigurationVector(Eigen::Ref<Eigen::VectorXd> q)
142 {
143  q.segment<4>(3) = Eigen::Quaterniond(q.segment<4>(3)).normalized().coeffs();
144 }
145 
146 inline void SetDefaultQuaternionInConfigurationVector(Eigen::Ref<Eigen::VectorXd> q)
147 {
148  q.segment<4>(3) = Eigen::Quaterniond(1.0, 0.0, 0.0, 0.0).normalized().coeffs();
149 }
150 
151 typedef Eigen::Array<KDL::Frame, Eigen::Dynamic, 1> ArrayFrame;
152 typedef Eigen::Array<KDL::Twist, Eigen::Dynamic, 1> ArrayTwist;
153 typedef Eigen::Array<KDL::Jacobian, Eigen::Dynamic, 1> ArrayJacobian;
154 typedef Eigen::Array<Eigen::MatrixXd, Eigen::Dynamic, 1> Hessian;
155 typedef Eigen::Array<Eigen::Array<Eigen::MatrixXd, Eigen::Dynamic, 1>, Eigen::Dynamic, 1> ArrayHessian;
156 
157 typedef Eigen::Ref<ArrayFrame> ArrayFrameRef;
158 typedef Eigen::Ref<ArrayTwist> ArrayTwistRef;
159 typedef Eigen::Ref<ArrayJacobian> ArrayJacobianRef;
160 typedef Eigen::Ref<Hessian> HessianRef;
161 typedef Eigen::Ref<ArrayHessian> ArrayHessianRef;
162 
163 typedef Eigen::internal::ref_selector<ArrayFrame>::type ArrayFrameRefConst;
164 typedef Eigen::internal::ref_selector<ArrayTwist>::type ArrayTwistRefConst;
165 typedef Eigen::internal::ref_selector<ArrayJacobian>::type ArrayJacobianRefConst;
166 typedef Eigen::internal::ref_selector<Hessian>::type HessianRefConst;
167 typedef Eigen::internal::ref_selector<ArrayHessian>::type ArrayHessianRefConst;
168 
169 inline bool IsContainerType(std::string type)
170 {
171  return type == "exotica::Initializer";
172 }
173 
174 inline bool IsVectorType(std::string type)
175 {
176  return type.substr(0, 11) == "std::vector";
177 }
178 
179 inline bool IsVectorContainerType(std::string type)
180 {
181  return type == "std::vector<exotica::Initializer>";
182 }
183 
184 template <class Key, class Val>
185 [[deprecated("Replaced by GetKeysFromMap and GetValuesFromMap")]] std::vector<Val> MapToVec(const std::map<Key, Val>& map) {
186  std::vector<Val> ret;
187  for (auto& it : map)
188  {
189  ret.push_back(it.second);
190  }
191  return ret;
192 }
193 
194 template <class Key, class Val>
195 std::vector<Key> GetKeysFromMap(const std::map<Key, Val>& map)
196 {
197  std::vector<Key> ret;
198  for (auto& it : map)
199  {
200  ret.push_back(it.first);
201  }
202  return ret;
203 }
204 
205 template <class Key, class Val>
206 std::vector<Val> GetValuesFromMap(const std::map<Key, Val>& map)
207 {
208  std::vector<Val> ret;
209  for (auto& it : map)
210  {
211  ret.push_back(it.second);
212  }
213  return ret;
214 }
215 
216 template <class Key, class Val>
217 void AppendMap(std::map<Key, Val>& orig, const std::map<Key, Val>& extra)
218 {
219  orig.insert(extra.begin(), extra.end());
220 }
221 
222 template <class Val>
223 void AppendVector(std::vector<Val>& orig, const std::vector<Val>& extra)
224 {
225  orig.insert(orig.end(), extra.begin(), extra.end());
226 }
227 
228 inline std::string Trim(const std::string& s)
229 {
230  auto wsfront = std::find_if_not(s.begin(), s.end(), [](int c) { return std::isspace(c); });
231  return std::string(wsfront, std::find_if_not(s.rbegin(), std::string::const_reverse_iterator(wsfront), [](int c) { return std::isspace(c); }).base());
232 }
233 
234 template <typename T>
235 T ToNumber(const std::string& val)
236 {
237  throw std::runtime_error("conversion not implemented!");
238 }
239 
240 template <>
241 inline float ToNumber<float>(const std::string& val)
242 {
243  return std::stof(val);
244 }
245 
246 template <>
247 inline double ToNumber<double>(const std::string& val)
248 {
249  return std::stod(val);
250 }
251 
252 template <>
253 inline int ToNumber<int>(const std::string& val)
254 {
255  return std::stoi(val);
256 }
257 
258 template <typename T, const int S> // Eigen::Vector<S><T>
259 inline Eigen::Matrix<T, S, 1> ParseVector(const std::string value)
260 {
261  Eigen::Matrix<T, S, 1> ret;
262  std::string temp_entry;
263  int i = 0;
264 
265  std::istringstream text_parser(value);
266 
267  while (text_parser >> temp_entry)
268  {
269  ret.conservativeResize(++i);
270  try
271  {
272  ret[i - 1] = ToNumber<T>(temp_entry);
273  }
274  catch (const std::invalid_argument& /* e */)
275  {
276  ret[i - 1] = std::numeric_limits<T>::quiet_NaN();
277  }
278  }
279  if (i == 0) WARNING_NAMED("Parser", "Empty vector!")
280  if (S != Eigen::Dynamic && S != i)
281  {
282  ThrowPretty("Wrong vector size! Requested: " + std::to_string(S) + ", Provided: " + std::to_string(i));
283  }
284  return ret;
285 }
286 
287 inline bool ParseBool(const std::string value)
288 {
289  bool ret;
290  std::istringstream text_parser(value);
291  text_parser >> ret;
292  return ret;
293 }
294 
295 inline double ParseDouble(const std::string value)
296 {
297  double ret;
298  std::istringstream text_parser(value);
299 
300  text_parser >> ret;
301  if ((text_parser.fail() || text_parser.bad()))
302  {
303  ThrowPretty("Can't parse value!");
304  }
305  return ret;
306 }
307 
308 inline int ParseInt(const std::string value)
309 {
310  int ret;
311  std::istringstream text_parser(value);
312 
313  text_parser >> ret;
314  if ((text_parser.fail() || text_parser.bad()))
315  {
316  ThrowPretty("Can't parse value!");
317  }
318  return ret;
319 }
320 
321 inline std::vector<std::string> ParseList(const std::string& value, char token = ',')
322 {
323  std::stringstream ss(value);
324  std::string item;
325  std::vector<std::string> ret;
326  while (std::getline(ss, item, token))
327  {
328  ret.push_back(Trim(item));
329  }
330  if (ret.size() == 0) WARNING_NAMED("Parser", "Empty vector!")
331  return ret;
332 }
333 
334 inline std::vector<int> ParseIntList(const std::string value)
335 {
336  std::stringstream ss(value);
337  std::string item;
338  std::vector<int> ret;
339  while (std::getline(ss, item, ' '))
340  {
341  int tmp;
342  std::istringstream text_parser(item);
343  text_parser >> tmp;
344  if ((text_parser.fail() || text_parser.bad()))
345  {
346  ThrowPretty("Can't parse value!");
347  }
348  ret.push_back(tmp);
349  }
350  if (ret.size() == 0) WARNING_NAMED("Parser", "Empty vector!")
351  return ret;
352 }
353 
354 inline std::vector<bool> ParseBoolList(const std::string value)
355 {
356  std::stringstream ss(value);
357  std::string item;
358  std::vector<bool> ret;
359  while (std::getline(ss, item, ' '))
360  {
361  bool tmp;
362  std::istringstream text_parser(item);
363  text_parser >> tmp;
364  if ((text_parser.fail() || text_parser.bad()))
365  {
366  ThrowPretty("Can't parse value!");
367  }
368  ret.push_back(tmp);
369  }
370  if (ret.empty()) WARNING_NAMED("Parser", "Empty vector!")
371  return ret;
372 }
373 } // namespace exotica
374 
375 #endif // EXOTICA_CORE_CONVERSIONS_H_
void AppendMap(std::map< Key, Val > &orig, const std::map< Key, Val > &extra)
Definition: conversions.h:217
std::string Trim(const std::string &s)
Definition: conversions.h:228
float ToNumber< float >(const std::string &val)
Definition: conversions.h:241
Eigen::Array< KDL::Twist, Eigen::Dynamic, 1 > ArrayTwist
Definition: conversions.h:152
double ToNumber< double >(const std::string &val)
Definition: conversions.h:247
void NormalizeQuaternionInConfigurationVector(Eigen::Ref< Eigen::VectorXd > q)
Definition: conversions.h:141
double ParseDouble(const std::string value)
Definition: conversions.h:295
KDL::Frame GetFrameFromMatrix(Eigen::MatrixXdRefConst val)
Definition: conversions.cpp:71
#define WARNING_NAMED(name, x)
Definition: printable.h:63
int ToNumber< int >(const std::string &val)
Definition: conversions.h:253
Eigen::MatrixXd GetFrame(const KDL::Frame &val)
Definition: conversions.cpp:91
Eigen::Ref< Eigen::VectorXd > VectorXdRef
Definition: conversions.h:54
bool IsContainerType(std::string type)
Definition: conversions.h:169
bool IsVectorContainerType(std::string type)
Definition: conversions.h:179
Eigen::Ref< ArrayTwist > ArrayTwistRef
Definition: conversions.h:158
std::vector< std::string > ParseList(const std::string &value, char token= ',')
Definition: conversions.h:321
#define ThrowPretty(m)
Definition: exception.h:36
Eigen::VectorXd GetFrameAsVector(const KDL::Frame &val, RotationType type=RotationType::RPY)
Definition: conversions.cpp:98
std::vector< Key > GetKeysFromMap(const std::map< Key, Val > &map)
Definition: conversions.h:195
std::vector< bool > ParseBoolList(const std::string value)
Definition: conversions.h:354
bool IsVectorType(std::string type)
Definition: conversions.h:174
Eigen::internal::ref_selector< ArrayFrame >::type ArrayFrameRefConst
Definition: conversions.h:163
RotationType GetRotationTypeFromString(const std::string &rotation_type)
Definition: conversions.h:99
Eigen::Tensor< Scalar, sizeof...(Dims)> MatrixToTensor(const MatrixType< Scalar > &matrix, Dims...dims)
Definition: conversions.h:69
T ToNumber(const std::string &val)
Definition: conversions.h:235
Eigen::internal::ref_selector< ArrayHessian >::type ArrayHessianRefConst
Definition: conversions.h:167
std::vector< Val > GetValuesFromMap(const std::map< Key, Val > &map)
Definition: conversions.h:206
Eigen::VectorXd IdentityTransform()
Definition: conversions.cpp:44
TFSIMD_FORCE_INLINE Vector3 normalized() const
Eigen::Ref< ArrayHessian > ArrayHessianRef
Definition: conversions.h:161
Eigen::internal::ref_selector< ArrayTwist >::type ArrayTwistRefConst
Definition: conversions.h:164
const Eigen::Ref< const Eigen::MatrixXd > & MatrixXdRefConst
Definition: conversions.h:53
Eigen::internal::ref_selector< Hessian >::type HessianRefConst
Definition: conversions.h:166
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixType
Definition: conversions.h:61
KDL::Rotation GetRotation(Eigen::VectorXdRefConst data, RotationType type)
int ParseInt(const std::string value)
Definition: conversions.h:308
Eigen::Array< Eigen::MatrixXd, Eigen::Dynamic, 1 > Hessian
Definition: conversions.h:154
Eigen::Array< Eigen::Array< Eigen::MatrixXd, Eigen::Dynamic, 1 >, Eigen::Dynamic, 1 > ArrayHessian
Definition: conversions.h:155
void SetDefaultQuaternionInConfigurationVector(Eigen::Ref< Eigen::VectorXd > q)
Definition: conversions.h:146
std::vector< int > ParseIntList(const std::string value)
Definition: conversions.h:334
Eigen::VectorXd VectorTransform(double px=0.0, double py=0.0, double pz=0.0, double qx=0.0, double qy=0.0, double qz=0.0, double qw=1.0)
Definition: conversions.cpp:38
const Eigen::Ref< const Eigen::VectorXd > & VectorXdRefConst
Convenience wrapper for storing references to sub-matrices/vectors.
Definition: conversions.h:52
Eigen::VectorXd SetRotation(const KDL::Rotation &data, RotationType type)
std::vector< Val > MapToVec(const std::map< Key, Val > &map)
Definition: conversions.h:185
int GetRotationTypeLength(const RotationType &type)
Definition: conversions.h:93
Eigen::Ref< Hessian > HessianRef
Definition: conversions.h:160
Eigen::Array< KDL::Jacobian, Eigen::Dynamic, 1 > ArrayJacobian
Definition: conversions.h:153
Eigen::Ref< Eigen::MatrixXd > MatrixXdRef
Definition: conversions.h:55
Eigen::Ref< ArrayJacobian > ArrayJacobianRef
Definition: conversions.h:159
bool ParseBool(const std::string value)
Definition: conversions.h:287
Eigen::Matrix< T, S, 1 > ParseVector(const std::string value)
Definition: conversions.h:259
Eigen::VectorXd GetRotationAsVector(const KDL::Frame &val, RotationType type)
MatrixType< Scalar > TensorToMatrix(const Eigen::Tensor< Scalar, rank > &tensor, const sizeType rows, const sizeType cols)
Definition: conversions.h:63
void AppendVector(std::vector< Val > &orig, const std::vector< Val > &extra)
Definition: conversions.h:223
Eigen::internal::ref_selector< ArrayJacobian >::type ArrayJacobianRefConst
Definition: conversions.h:165
Eigen::Ref< ArrayFrame > ArrayFrameRef
Definition: conversions.h:157
Eigen::Array< KDL::Frame, Eigen::Dynamic, 1 > ArrayFrame
Definition: conversions.h:151


exotica_core
Author(s): Yiming Yang, Michael Camilleri
autogenerated on Sat Apr 10 2021 02:34:49