Flexiv RDK APIs  0.10
Utility.hpp
Go to the documentation of this file.
1 
6 #ifndef FLEXIVRDK_UTILITY_HPP_
7 #define FLEXIVRDK_UTILITY_HPP_
8 
9 #include <flexiv/Exception.hpp>
10 #include <Eigen/Eigen>
11 #include <array>
12 #include <vector>
13 
14 namespace flexiv {
15 namespace utility {
16 
25 inline std::vector<double> quat2EulerZYX(const std::vector<double>& quat)
26 {
27  // Check input size
28  if (quat.size() != 4) {
29  throw InputException(
30  "[flexiv::utility] quat2EulerZYX: input vector is not size 4");
31  }
32 
33  // Form quaternion
34  Eigen::Quaterniond q(quat[0], quat[1], quat[2], quat[3]);
35 
36  // The returned vector is in [z,y,x] order
37  auto eulerZYX = q.toRotationMatrix().eulerAngles(2, 1, 0);
38 
39  // Convert to general [x,y,z] order
40  return (std::vector<double> {eulerZYX[2], eulerZYX[1], eulerZYX[0]});
41 }
42 
46 inline double rad2Deg(double rad)
47 {
48  constexpr double k_pi = 3.14159265358979323846;
49  return (rad / k_pi * 180.0);
50 }
51 
55 inline std::vector<double> rad2Deg(const std::vector<double>& radVec)
56 {
57  std::vector<double> degVec = {};
58  for (const auto& v : radVec) {
59  degVec.push_back(rad2Deg(v));
60  }
61  return degVec;
62 }
63 
72 template <typename T>
73 inline std::string vec2Str(
74  const std::vector<T>& vec, size_t decimal = 3, bool trailingSpace = true)
75 {
76  auto padding = "";
77  std::stringstream ss;
78  ss.precision(decimal);
79  ss << std::fixed;
80 
81  for (const auto& v : vec) {
82  ss << padding << v;
83  padding = " ";
84  }
85 
86  if (trailingSpace) {
87  ss << " ";
88  }
89  return ss.str();
90 }
91 
100 template <typename T, size_t N>
101 inline std::string arr2Str(
102  const std::array<T, N>& arr, size_t decimal = 3, bool trailingSpace = true)
103 {
104  auto padding = "";
105  std::stringstream ss;
106  ss.precision(decimal);
107  ss << std::fixed;
108 
109  for (const auto& v : arr) {
110  ss << padding << v;
111  padding = " ";
112  }
113 
114  if (trailingSpace) {
115  ss << " ";
116  }
117  return ss.str();
118 }
119 
129  int argc, char** argv, const std::vector<std::string>& refStrs)
130 {
131  for (int i = 0; i < argc; i++) {
132  for (const auto& v : refStrs) {
133  if (v == std::string(argv[i])) {
134  return true;
135  }
136  }
137  }
138  return false;
139 }
140 
149 inline bool programArgsExist(int argc, char** argv, const std::string& refStr)
150 {
151  return programArgsExistAny(argc, argv, {refStr});
152 }
153 
163 inline std::string parsePtStates(
164  const std::vector<std::string>& ptStates, const std::string& parseTarget)
165 {
166  for (const auto& state : ptStates) {
167  // Skip if empty
168  if (state.empty()) {
169  continue;
170  }
171  std::stringstream ss(state);
172  std::string buffer;
173  std::vector<std::string> parsedState;
174  while (ss >> buffer) {
175  parsedState.push_back(buffer);
176  }
177  if (parsedState.front() == parseTarget) {
178  return parsedState.back();
179  }
180  }
181 
182  return "";
183 }
184 
185 } /* namespace utility */
186 } /* namespace flexiv */
187 
188 #endif /* FLEXIVRDK_UTILITY_HPP_ */
std::string vec2Str(const std::vector< T > &vec, size_t decimal=3, bool trailingSpace=true)
Convert a std::vector to a string.
Definition: Utility.hpp:73
std::string arr2Str(const std::array< T, N > &arr, size_t decimal=3, bool trailingSpace=true)
Convert a std::array to a string.
Definition: Utility.hpp:101
std::string parsePtStates(const std::vector< std::string > &ptStates, const std::string &parseTarget)
Parse the value of a specified primitive state from the ptStates string list.
Definition: Utility.hpp:163
bool programArgsExist(int argc, char **argv, const std::string &refStr)
Check if one specific string exists in the program arguments.
Definition: Utility.hpp:149
bool programArgsExistAny(int argc, char **argv, const std::vector< std::string > &refStrs)
Check if any provided strings exist in the program arguments.
Definition: Utility.hpp:128
double rad2Deg(double rad)
Convert radians to degrees for a single value.
Definition: Utility.hpp:46
std::vector< double > quat2EulerZYX(const std::vector< double > &quat)
Convert quaternion to Euler angles with ZYX axis rotations.
Definition: Utility.hpp:25
Thrown if the user input is not valid.
Definition: Exception.hpp:69