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 #pragma GCC diagnostic ignored "-Wunused-parameter"
44 #include <unsupported/Eigen/CXX11/Tensor>
45 #pragma GCC diagnostic pop
46 
49 
50 namespace Eigen
51 {
53 typedef const Eigen::Ref<const Eigen::VectorXd>& VectorXdRefConst;
54 typedef const Eigen::Ref<const Eigen::MatrixXd>& MatrixXdRefConst;
55 typedef typename Eigen::Ref<Eigen::VectorXd> VectorXdRef;
56 typedef typename Eigen::Ref<Eigen::MatrixXd> MatrixXdRef;
57 
58 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);
59 Eigen::VectorXd IdentityTransform();
60 
61 template <typename T>
62 using MatrixType = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>;
63 template <typename Scalar, int rank, typename sizeType>
64 inline MatrixType<Scalar> TensorToMatrix(const Eigen::Tensor<Scalar, rank>& tensor, const sizeType rows, const sizeType cols)
65 {
66  return Eigen::Map<const MatrixType<Scalar>>(tensor.data(), rows, cols);
67 }
68 
69 template <typename Scalar, typename... Dims>
70 inline Eigen::Tensor<Scalar, sizeof...(Dims)> MatrixToTensor(const MatrixType<Scalar>& matrix, Dims... dims)
71 {
72  constexpr int rank = sizeof...(Dims);
73  return Eigen::TensorMap<Eigen::Tensor<const Scalar, rank>>(matrix.data(), {dims...});
74 }
75 
76 } // namespace Eigen
77 
78 namespace exotica
79 {
80 enum class RotationType
81 {
82  QUATERNION,
83  RPY,
84  ZYX,
85  ZYZ,
86  ANGLE_AXIS,
87  MATRIX
88 };
89 
90 KDL::Rotation GetRotation(Eigen::VectorXdRefConst data, RotationType type);
91 
92 Eigen::VectorXd SetRotation(const KDL::Rotation& data, RotationType type);
93 
94 inline int GetRotationTypeLength(const RotationType& type)
95 {
96  static constexpr int types[] = {4, 3, 3, 3, 3, 9};
97  return types[static_cast<int>(type)];
98 }
99 
100 inline RotationType GetRotationTypeFromString(const std::string& rotation_type)
101 {
102  if (rotation_type == "Quaternion")
103  {
105  }
106  else if (rotation_type == "RPY")
107  {
108  return RotationType::RPY;
109  }
110  else if (rotation_type == "ZYX")
111  {
112  return RotationType::ZYX;
113  }
114  else if (rotation_type == "ZYZ")
115  {
116  return RotationType::ZYZ;
117  }
118  else if (rotation_type == "AngleAxis")
119  {
121  }
122  else if (rotation_type == "Matrix")
123  {
124  return RotationType::MATRIX;
125  }
126  else
127  {
128  ThrowPretty("Unsupported rotation type '" << rotation_type << "'");
129  }
130 }
131 
132 KDL::Frame GetFrame(Eigen::VectorXdRefConst val);
133 
135 
136 Eigen::MatrixXd GetFrame(const KDL::Frame& val);
137 
138 Eigen::VectorXd GetFrameAsVector(const KDL::Frame& val, RotationType type = RotationType::RPY);
139 
140 Eigen::VectorXd GetRotationAsVector(const KDL::Frame& val, RotationType type);
141 
142 inline void NormalizeQuaternionInConfigurationVector(Eigen::Ref<Eigen::VectorXd> q)
143 {
144  q.segment<4>(3) = Eigen::Quaterniond(q.segment<4>(3)).normalized().coeffs();
145 }
146 
147 inline void SetDefaultQuaternionInConfigurationVector(Eigen::Ref<Eigen::VectorXd> q)
148 {
149  q.segment<4>(3) = Eigen::Quaterniond(1.0, 0.0, 0.0, 0.0).normalized().coeffs();
150 }
151 
152 typedef Eigen::Array<KDL::Frame, Eigen::Dynamic, 1> ArrayFrame;
153 typedef Eigen::Array<KDL::Twist, Eigen::Dynamic, 1> ArrayTwist;
154 typedef Eigen::Array<KDL::Jacobian, Eigen::Dynamic, 1> ArrayJacobian;
155 typedef Eigen::Array<Eigen::MatrixXd, Eigen::Dynamic, 1> Hessian;
156 typedef Eigen::Array<Eigen::Array<Eigen::MatrixXd, Eigen::Dynamic, 1>, Eigen::Dynamic, 1> ArrayHessian;
157 
158 typedef Eigen::Ref<ArrayFrame> ArrayFrameRef;
159 typedef Eigen::Ref<ArrayTwist> ArrayTwistRef;
160 typedef Eigen::Ref<ArrayJacobian> ArrayJacobianRef;
161 typedef Eigen::Ref<Hessian> HessianRef;
162 typedef Eigen::Ref<ArrayHessian> ArrayHessianRef;
163 
164 typedef Eigen::internal::ref_selector<ArrayFrame>::type ArrayFrameRefConst;
165 typedef Eigen::internal::ref_selector<ArrayTwist>::type ArrayTwistRefConst;
166 typedef Eigen::internal::ref_selector<ArrayJacobian>::type ArrayJacobianRefConst;
167 typedef Eigen::internal::ref_selector<Hessian>::type HessianRefConst;
168 typedef Eigen::internal::ref_selector<ArrayHessian>::type ArrayHessianRefConst;
169 
170 inline bool IsContainerType(std::string type)
171 {
172  return type == "exotica::Initializer";
173 }
174 
175 inline bool IsVectorType(std::string type)
176 {
177  return type.substr(0, 11) == "std::vector";
178 }
179 
180 inline bool IsVectorContainerType(std::string type)
181 {
182  return type == "std::vector<exotica::Initializer>";
183 }
184 
185 template <class Key, class Val>
186 [[deprecated("Replaced by GetKeysFromMap and GetValuesFromMap")]] std::vector<Val> MapToVec(const std::map<Key, Val>& map) {
187  std::vector<Val> ret;
188  for (auto& it : map)
189  {
190  ret.push_back(it.second);
191  }
192  return ret;
193 }
194 
195 template <class Key, class Val>
196 std::vector<Key> GetKeysFromMap(const std::map<Key, Val>& map)
197 {
198  std::vector<Key> ret;
199  for (auto& it : map)
200  {
201  ret.push_back(it.first);
202  }
203  return ret;
204 }
205 
206 template <class Key, class Val>
207 std::vector<Val> GetValuesFromMap(const std::map<Key, Val>& map)
208 {
209  std::vector<Val> ret;
210  for (auto& it : map)
211  {
212  ret.push_back(it.second);
213  }
214  return ret;
215 }
216 
217 template <class Key, class Val>
218 void AppendMap(std::map<Key, Val>& orig, const std::map<Key, Val>& extra)
219 {
220  orig.insert(extra.begin(), extra.end());
221 }
222 
223 template <class Val>
224 void AppendVector(std::vector<Val>& orig, const std::vector<Val>& extra)
225 {
226  orig.insert(orig.end(), extra.begin(), extra.end());
227 }
228 
229 inline std::string Trim(const std::string& s)
230 {
231  auto wsfront = std::find_if_not(s.begin(), s.end(), [](int c) { return std::isspace(c); });
232  return std::string(wsfront, std::find_if_not(s.rbegin(), std::string::const_reverse_iterator(wsfront), [](int c) { return std::isspace(c); }).base());
233 }
234 
235 template <typename T>
236 T ToNumber(const std::string& /*val*/)
237 {
238  throw std::runtime_error("conversion not implemented!");
239 }
240 
241 template <>
242 inline float ToNumber<float>(const std::string& val)
243 {
244  return std::stof(val);
245 }
246 
247 template <>
248 inline double ToNumber<double>(const std::string& val)
249 {
250  return std::stod(val);
251 }
252 
253 template <>
254 inline int ToNumber<int>(const std::string& val)
255 {
256  return std::stoi(val);
257 }
258 
259 template <typename T, const int S> // Eigen::Vector<S><T>
260 inline Eigen::Matrix<T, S, 1> ParseVector(const std::string value)
261 {
262  Eigen::Matrix<T, S, 1> ret;
263  std::string temp_entry;
264  int i = 0;
265 
266  std::istringstream text_parser(value);
267 
268  while (text_parser >> temp_entry)
269  {
270  ret.conservativeResize(++i);
271  try
272  {
273  ret[i - 1] = ToNumber<T>(temp_entry);
274  }
275  catch (const std::invalid_argument& /* e */)
276  {
277  ret[i - 1] = std::numeric_limits<T>::quiet_NaN();
278  }
279  }
280  if (i == 0) WARNING_NAMED("Parser", "Empty vector!")
281  if (S != Eigen::Dynamic && S != i)
282  {
283  ThrowPretty("Wrong vector size! Requested: " + std::to_string(S) + ", Provided: " + std::to_string(i));
284  }
285  return ret;
286 }
287 
288 inline bool ParseBool(const std::string value)
289 {
290  if (value == "0" || value == "false" || value == "False")
291  {
292  return false;
293  }
294  else if (value == "1" || value == "true" || value == "True")
295  {
296  return true;
297  }
298  else
299  {
300  bool ret;
301  std::istringstream text_parser(value);
302  text_parser >> ret;
303  if ((text_parser.fail() || text_parser.bad()))
304  {
305  ThrowPretty("Can't parse boolean value!");
306  }
307  return ret;
308  }
309 }
310 
311 inline double ParseDouble(const std::string value)
312 {
313  double ret;
314  std::istringstream text_parser(value);
315 
316  text_parser >> ret;
317  if ((text_parser.fail() || text_parser.bad()))
318  {
319  ThrowPretty("Can't parse value!");
320  }
321  return ret;
322 }
323 
324 inline int ParseInt(const std::string value)
325 {
326  int ret;
327  std::istringstream text_parser(value);
328 
329  text_parser >> ret;
330  if ((text_parser.fail() || text_parser.bad()))
331  {
332  ThrowPretty("Can't parse value!");
333  }
334  return ret;
335 }
336 
337 inline std::vector<std::string> ParseList(const std::string& value, char token = ',')
338 {
339  std::stringstream ss(value);
340  std::string item;
341  std::vector<std::string> ret;
342  while (std::getline(ss, item, token))
343  {
344  ret.push_back(Trim(item));
345  }
346  if (ret.size() == 0) WARNING_NAMED("Parser", "Empty vector!")
347  return ret;
348 }
349 
350 inline std::vector<int> ParseIntList(const std::string value)
351 {
352  std::stringstream ss(value);
353  std::string item;
354  std::vector<int> ret;
355  while (std::getline(ss, item, ' '))
356  {
357  int tmp;
358  std::istringstream text_parser(item);
359  text_parser >> tmp;
360  if ((text_parser.fail() || text_parser.bad()))
361  {
362  ThrowPretty("Can't parse value!");
363  }
364  ret.push_back(tmp);
365  }
366  if (ret.size() == 0) WARNING_NAMED("Parser", "Empty vector!")
367  return ret;
368 }
369 
370 inline std::vector<bool> ParseBoolList(const std::string value)
371 {
372  std::stringstream ss(value);
373  std::string item;
374  std::vector<bool> ret;
375  while (std::getline(ss, item, ' '))
376  {
377  bool tmp = ParseBool(item);
378  ret.push_back(tmp);
379  }
380  if (ret.empty()) WARNING_NAMED("Parser", "Empty vector!")
381  return ret;
382 }
383 } // namespace exotica
384 
385 #endif // EXOTICA_CORE_CONVERSIONS_H_
exotica::ArrayJacobianRefConst
Eigen::internal::ref_selector< ArrayJacobian >::type ArrayJacobianRefConst
Definition: conversions.h:166
exotica::AppendVector
void AppendVector(std::vector< Val > &orig, const std::vector< Val > &extra)
Definition: conversions.h:224
Eigen
exotica::GetFrame
KDL::Frame GetFrame(Eigen::VectorXdRefConst val)
Definition: conversions.cpp:52
printable.h
Eigen::VectorTransform
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
s
XmlRpcServer s
exotica::ToNumber
T ToNumber(const std::string &)
Definition: conversions.h:236
exotica::IsVectorContainerType
bool IsVectorContainerType(std::string type)
Definition: conversions.h:180
exotica::ParseDouble
double ParseDouble(const std::string value)
Definition: conversions.h:311
exotica::ToNumber< float >
float ToNumber< float >(const std::string &val)
Definition: conversions.h:242
exotica::NormalizeQuaternionInConfigurationVector
void NormalizeQuaternionInConfigurationVector(Eigen::Ref< Eigen::VectorXd > q)
Definition: conversions.h:142
exotica::GetFrameFromMatrix
KDL::Frame GetFrameFromMatrix(Eigen::MatrixXdRefConst val)
Definition: conversions.cpp:71
exotica::IsVectorType
bool IsVectorType(std::string type)
Definition: conversions.h:175
exotica::ToNumber< int >
int ToNumber< int >(const std::string &val)
Definition: conversions.h:254
exotica::ArrayFrameRefConst
Eigen::internal::ref_selector< ArrayFrame >::type ArrayFrameRefConst
Definition: conversions.h:164
exotica::IsContainerType
bool IsContainerType(std::string type)
Definition: conversions.h:170
exotica::ArrayTwistRef
Eigen::Ref< ArrayTwist > ArrayTwistRef
Definition: conversions.h:159
exotica::RotationType::ZYX
@ ZYX
exotica
Definition: collision_scene.h:46
exotica::Hessian
Eigen::Array< Eigen::MatrixXd, Eigen::Dynamic, 1 > Hessian
Definition: conversions.h:155
exotica::ParseBoolList
std::vector< bool > ParseBoolList(const std::string value)
Definition: conversions.h:370
exotica::RotationType::ANGLE_AXIS
@ ANGLE_AXIS
Eigen::MatrixToTensor
Eigen::Tensor< Scalar, sizeof...(Dims)> MatrixToTensor(const MatrixType< Scalar > &matrix, Dims... dims)
Definition: conversions.h:70
exotica::GetFrameAsVector
Eigen::VectorXd GetFrameAsVector(const KDL::Frame &val, RotationType type=RotationType::RPY)
Definition: conversions.cpp:98
exotica::RotationType::MATRIX
@ MATRIX
exotica::GetRotation
KDL::Rotation GetRotation(Eigen::VectorXdRefConst data, RotationType type)
Definition: conversions.cpp:114
exotica::GetKeysFromMap
std::vector< Key > GetKeysFromMap(const std::map< Key, Val > &map)
Definition: conversions.h:196
exotica::GetValuesFromMap
std::vector< Val > GetValuesFromMap(const std::map< Key, Val > &map)
Definition: conversions.h:207
exotica::ArrayHessianRef
Eigen::Ref< ArrayHessian > ArrayHessianRef
Definition: conversions.h:162
exotica::RotationType::ZYZ
@ ZYZ
Eigen::VectorXdRef
Eigen::Ref< Eigen::VectorXd > VectorXdRef
Definition: conversions.h:55
Eigen::VectorXdRefConst
const typedef Eigen::Ref< const Eigen::VectorXd > & VectorXdRefConst
Convenience wrapper for storing references to sub-matrices/vectors.
Definition: conversions.h:53
exotica::GetRotationTypeFromString
RotationType GetRotationTypeFromString(const std::string &rotation_type)
Definition: conversions.h:100
Eigen::TensorToMatrix
MatrixType< Scalar > TensorToMatrix(const Eigen::Tensor< Scalar, rank > &tensor, const sizeType rows, const sizeType cols)
Definition: conversions.h:64
exotica::ArrayHessianRefConst
Eigen::internal::ref_selector< ArrayHessian >::type ArrayHessianRefConst
Definition: conversions.h:168
exotica::RotationType::RPY
@ RPY
exotica::ArrayHessian
Eigen::Array< Eigen::Array< Eigen::MatrixXd, Eigen::Dynamic, 1 >, Eigen::Dynamic, 1 > ArrayHessian
Definition: conversions.h:156
exotica::ArrayTwistRefConst
Eigen::internal::ref_selector< ArrayTwist >::type ArrayTwistRefConst
Definition: conversions.h:165
exotica::MapToVec
std::vector< Val > MapToVec(const std::map< Key, Val > &map)
Definition: conversions.h:186
exotica::SetDefaultQuaternionInConfigurationVector
void SetDefaultQuaternionInConfigurationVector(Eigen::Ref< Eigen::VectorXd > q)
Definition: conversions.h:147
Eigen::MatrixXdRef
Eigen::Ref< Eigen::MatrixXd > MatrixXdRef
Definition: conversions.h:56
Eigen::MatrixXdRefConst
const typedef Eigen::Ref< const Eigen::MatrixXd > & MatrixXdRefConst
Definition: conversions.h:54
exotica::HessianRef
Eigen::Ref< Hessian > HessianRef
Definition: conversions.h:161
exotica::ParseIntList
std::vector< int > ParseIntList(const std::string value)
Definition: conversions.h:350
exotica::ArrayJacobianRef
Eigen::Ref< ArrayJacobian > ArrayJacobianRef
Definition: conversions.h:160
exotica::HessianRefConst
Eigen::internal::ref_selector< Hessian >::type HessianRefConst
Definition: conversions.h:167
exotica::RotationType::QUATERNION
@ QUATERNION
exotica::ParseInt
int ParseInt(const std::string value)
Definition: conversions.h:324
Eigen::IdentityTransform
Eigen::VectorXd IdentityTransform()
Definition: conversions.cpp:44
exotica::GetRotationAsVector
Eigen::VectorXd GetRotationAsVector(const KDL::Frame &val, RotationType type)
Definition: conversions.cpp:109
ThrowPretty
#define ThrowPretty(m)
Definition: exception.h:36
WARNING_NAMED
#define WARNING_NAMED(name, x)
Definition: printable.h:63
exotica::ArrayJacobian
Eigen::Array< KDL::Jacobian, Eigen::Dynamic, 1 > ArrayJacobian
Definition: conversions.h:154
exotica::SetRotation
Eigen::VectorXd SetRotation(const KDL::Rotation &data, RotationType type)
Definition: conversions.cpp:150
exotica::ParseVector
Eigen::Matrix< T, S, 1 > ParseVector(const std::string value)
Definition: conversions.h:260
exception.h
exotica::GetRotationTypeLength
int GetRotationTypeLength(const RotationType &type)
Definition: conversions.h:94
Eigen::MatrixType
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > MatrixType
Definition: conversions.h:62
exotica::ArrayTwist
Eigen::Array< KDL::Twist, Eigen::Dynamic, 1 > ArrayTwist
Definition: conversions.h:153
exotica::Trim
std::string Trim(const std::string &s)
Definition: conversions.h:229
exotica::RotationType
RotationType
Definition: conversions.h:80
exotica::ParseBool
bool ParseBool(const std::string value)
Definition: conversions.h:288
exotica::ToNumber< double >
double ToNumber< double >(const std::string &val)
Definition: conversions.h:248
exotica::AppendMap
void AppendMap(std::map< Key, Val > &orig, const std::map< Key, Val > &extra)
Definition: conversions.h:218
exotica::ArrayFrameRef
Eigen::Ref< ArrayFrame > ArrayFrameRef
Definition: conversions.h:158
exotica::ParseList
std::vector< std::string > ParseList(const std::string &value, char token=',')
Definition: conversions.h:337
exotica::ArrayFrame
Eigen::Array< KDL::Frame, Eigen::Dynamic, 1 > ArrayFrame
Definition: conversions.h:152


exotica_core
Author(s): Yiming Yang, Michael Camilleri
autogenerated on Fri Aug 2 2024 08:43:02