30 #ifndef EXOTICA_CORE_CONVERSIONS_H_
31 #define EXOTICA_CORE_CONVERSIONS_H_
37 #include <kdl/frames.hpp>
38 #include <kdl/jacobian.hpp>
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
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);
62 using MatrixType = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>;
63 template <
typename Scalar,
int rank,
typename sizeType>
66 return Eigen::Map<const MatrixType<Scalar>>(tensor.data(), rows, cols);
69 template <
typename Scalar,
typename... Dims>
72 constexpr
int rank =
sizeof...(Dims);
73 return Eigen::TensorMap<Eigen::Tensor<const Scalar, rank>>(matrix.data(), {dims...});
96 static constexpr
int types[] = {4, 3, 3, 3, 3, 9};
97 return types[
static_cast<int>(type)];
102 if (rotation_type ==
"Quaternion")
106 else if (rotation_type ==
"RPY")
110 else if (rotation_type ==
"ZYX")
114 else if (rotation_type ==
"ZYZ")
118 else if (rotation_type ==
"AngleAxis")
122 else if (rotation_type ==
"Matrix")
128 ThrowPretty(
"Unsupported rotation type '" << rotation_type <<
"'");
136 Eigen::MatrixXd
GetFrame(
const KDL::Frame& val);
144 q.segment<4>(3) = Eigen::Quaterniond(q.segment<4>(3)).normalized().coeffs();
149 q.segment<4>(3) = Eigen::Quaterniond(1.0, 0.0, 0.0, 0.0).normalized().coeffs();
152 typedef Eigen::Array<KDL::Frame, Eigen::Dynamic, 1>
ArrayFrame;
153 typedef Eigen::Array<KDL::Twist, Eigen::Dynamic, 1>
ArrayTwist;
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;
172 return type ==
"exotica::Initializer";
177 return type.substr(0, 11) ==
"std::vector";
182 return type ==
"std::vector<exotica::Initializer>";
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;
190 ret.push_back(it.second);
195 template <
class Key,
class Val>
198 std::vector<Key> ret;
201 ret.push_back(it.first);
206 template <
class Key,
class Val>
209 std::vector<Val> ret;
212 ret.push_back(it.second);
217 template <
class Key,
class Val>
218 void AppendMap(std::map<Key, Val>& orig,
const std::map<Key, Val>& extra)
220 orig.insert(extra.begin(), extra.end());
224 void AppendVector(std::vector<Val>& orig,
const std::vector<Val>& extra)
226 orig.insert(orig.end(), extra.begin(), extra.end());
229 inline std::string
Trim(
const std::string& s)
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());
235 template <
typename T>
238 throw std::runtime_error(
"conversion not implemented!");
244 return std::stof(val);
250 return std::stod(val);
256 return std::stoi(val);
259 template <
typename T, const
int S>
260 inline Eigen::Matrix<T, S, 1>
ParseVector(
const std::string value)
262 Eigen::Matrix<T, S, 1> ret;
263 std::string temp_entry;
266 std::istringstream text_parser(value);
268 while (text_parser >> temp_entry)
270 ret.conservativeResize(++i);
273 ret[i - 1] = ToNumber<T>(temp_entry);
275 catch (
const std::invalid_argument& )
277 ret[i - 1] = std::numeric_limits<T>::quiet_NaN();
281 if (S != Eigen::Dynamic && S != i)
283 ThrowPretty(
"Wrong vector size! Requested: " + std::to_string(S) +
", Provided: " + std::to_string(i));
290 if (value ==
"0" || value ==
"false" || value ==
"False")
294 else if (value ==
"1" || value ==
"true" || value ==
"True")
301 std::istringstream text_parser(value);
303 if ((text_parser.fail() || text_parser.bad()))
314 std::istringstream text_parser(value);
317 if ((text_parser.fail() || text_parser.bad()))
327 std::istringstream text_parser(value);
330 if ((text_parser.fail() || text_parser.bad()))
337 inline std::vector<std::string>
ParseList(
const std::string& value,
char token =
',')
339 std::stringstream ss(value);
341 std::vector<std::string> ret;
342 while (std::getline(ss, item, token))
344 ret.push_back(
Trim(item));
346 if (ret.size() == 0)
WARNING_NAMED(
"Parser",
"Empty vector!")
352 std::stringstream ss(value);
354 std::vector<int> ret;
355 while (std::getline(ss, item,
' '))
358 std::istringstream text_parser(item);
360 if ((text_parser.fail() || text_parser.bad()))
366 if (ret.size() == 0)
WARNING_NAMED(
"Parser",
"Empty vector!")
372 std::stringstream ss(value);
374 std::vector<bool> ret;
375 while (std::getline(ss, item,
' '))
385 #endif // EXOTICA_CORE_CONVERSIONS_H_