Flexiv RDK APIs  2.1
utility.hpp
Go to the documentation of this file.
1 
6 #ifndef FLEXIV_RDK_UTILITY_HPP_
7 #define FLEXIV_RDK_UTILITY_HPP_
8 
9 #include "data.hpp"
10 #include <Eigen/Eigen>
11 #include <algorithm>
12 #include <sstream>
13 
14 namespace flexiv::rdk {
15 
16 namespace utility {
17 
19 inline bool IsSingleArmGroup(JointGroup group)
20 {
21  return group == JointGroup::ARM_1 || group == JointGroup::ARM_2;
22 }
23 
30 inline bool PrimitiveStateTrue(
31  const PrimitiveStates& primitive_states, const std::string& state_name)
32 {
33  const auto state_it = primitive_states.names_and_values.find(state_name);
34  if (state_it == primitive_states.names_and_values.end()) {
35  return false;
36  }
37 
38  if (!std::holds_alternative<int>(state_it->second)) {
39  return false;
40  }
41 
42  return std::get<int>(state_it->second) != 0;
43 }
44 
52  const std::map<JointGroup, PrimitiveStates>& primitive_states, const std::string& state_name)
53 {
54  return std::all_of(primitive_states.begin(), primitive_states.end(),
55  [&](const auto& entry) { return PrimitiveStateTrue(entry.second, state_name); });
56 }
57 
67  const std::map<JointGroup, PrimitiveStates>& primitive_states,
68  const std::map<JointGroup, std::string>& state_names)
69 {
70  return std::all_of(primitive_states.begin(), primitive_states.end(), [&](const auto& entry) {
71  if (!state_names.contains(entry.first)) {
72  return false;
73  }
74  return PrimitiveStateTrue(entry.second, state_names.at(entry.first));
75  });
76 }
77 
85 inline std::array<double, 3> Quat2EulerZYX(const std::array<double, 4>& quat)
86 {
87  // Form quaternion
88  Eigen::Quaterniond q(quat[0], quat[1], quat[2], quat[3]);
89 
90  // The returned array is in [z,y,x] order
91  auto euler_ZYX = q.toRotationMatrix().eulerAngles(2, 1, 0);
92 
93  // Convert to general [x,y,z] order
94  return (std::array<double, 3> {euler_ZYX[2], euler_ZYX[1], euler_ZYX[0]});
95 }
96 
100 inline double Rad2Deg(double rad)
101 {
102  constexpr double kPi = 3.14159265358979323846;
103  return (rad / kPi * 180.0);
104 }
105 
109 template <size_t N>
110 inline std::array<double, N> Rad2Deg(const std::array<double, N>& rad_arr)
111 {
112  std::array<double, N> deg_arr;
113  for (size_t i = 0; i < N; i++) {
114  deg_arr[i] = Rad2Deg(rad_arr[i]);
115  }
116  return deg_arr;
117 }
118 
122 inline std::vector<double> Rad2Deg(const std::vector<double>& rad_vec)
123 {
124  std::vector<double> deg_vec;
125  for (const auto& v : rad_vec) {
126  deg_vec.push_back(Rad2Deg(v));
127  }
128  return deg_vec;
129 }
130 
138 template <typename T>
139 inline std::string Vec2Str(
140  const std::vector<T>& vec, size_t decimal = 3, const std::string& separator = " ")
141 {
142  std::string padding = "";
143  std::ostringstream oss;
144  oss.precision(decimal);
145  oss << std::fixed;
146 
147  for (const auto& v : vec) {
148  oss << padding << v;
149  padding = separator;
150  }
151  return oss.str();
152 }
153 
161 template <typename T, size_t N>
162 inline std::string Arr2Str(
163  const std::array<T, N>& arr, size_t decimal = 3, const std::string& separator = " ")
164 {
165  std::vector<T> vec(N);
166  std::copy(arr.begin(), arr.end(), vec.begin());
167  return Vec2Str(vec, decimal, separator);
168 }
169 
177 inline std::string FlexivTypes2Str(
178  const rdk::FlexivDataTypes& variant, size_t decimal = 3, const std::string& separator = " ")
179 {
180  if (auto* val = std::get_if<int>(&variant)) {
181  return Vec2Str(std::vector<int> {*val}, decimal);
182  } else if (auto* val = std::get_if<double>(&variant)) {
183  return Vec2Str(std::vector<double> {*val}, decimal);
184  } else if (auto* val = std::get_if<std::string>(&variant)) {
185  return *val;
186  } else if (auto* val = std::get_if<rdk::Coord>(&variant)) {
187  return (*val).str();
188  } else if (auto* vec = std::get_if<std::vector<int>>(&variant)) {
189  return Vec2Str(*vec, decimal, separator);
190  } else if (auto* vec = std::get_if<std::vector<double>>(&variant)) {
191  return Vec2Str(*vec, decimal, separator);
192  } else if (auto* vec = std::get_if<std::vector<std::string>>(&variant)) {
193  return Vec2Str(*vec, decimal, separator);
194  } else if (auto* vec = std::get_if<std::vector<rdk::Coord>>(&variant)) {
195  std::string ret;
196  // Separate two Coord by " : "
197  for (const auto& v : (*vec)) {
198  ret += v.str() + " : ";
199  }
200  // Remove the trailing " : "
201  if (!ret.empty()) {
202  ret.erase(ret.size() - 3);
203  }
204  return ret;
205  }
206  return "";
207 }
208 
217 inline bool ProgramArgsExistAny(int argc, char** argv, const std::vector<std::string>& ref_strings)
218 {
219  for (int i = 0; i < argc; i++) {
220  for (const auto& v : ref_strings) {
221  if (v == std::string(argv[i])) {
222  return true;
223  }
224  }
225  }
226  return false;
227 }
228 
237 inline bool ProgramArgsExist(int argc, char** argv, const std::string& ref_strings)
238 {
239  return ProgramArgsExistAny(argc, argv, {ref_strings});
240 }
241 
242 } /* namespace utility */
243 } /* namespace flexiv::rdk */
244 
245 #endif /* FLEXIV_RDK_UTILITY_HPP_ */
Header file containing various constant expressions, data structures, and enums.
JointGroup
All possible joint groups of the robot.
Definition: data.hpp:58
std::variant< int, double, std::string, rdk::JPos, rdk::Coord, std::vector< int >, std::vector< double >, std::vector< std::string >, std::vector< rdk::JPos >, std::vector< rdk::Coord > > FlexivDataTypes
Definition: data.hpp:555
States data of a primitive.
Definition: data.hpp:606
std::map< std::string, FlexivDataTypes > names_and_values
Definition: data.hpp:614
std::array< double, 3 > Quat2EulerZYX(const std::array< double, 4 > &quat)
Convert quaternion to Euler angles with ZYX axis rotations.
Definition: utility.hpp:85
bool ProgramArgsExist(int argc, char **argv, const std::string &ref_strings)
Check if one specific string exists in the program arguments.
Definition: utility.hpp:237
std::string Vec2Str(const std::vector< T > &vec, size_t decimal=3, const std::string &separator=" ")
Convert an std::vector to a string.
Definition: utility.hpp:139
bool IsSingleArmGroup(JointGroup group)
Whether the specified group represents a single arm.
Definition: utility.hpp:19
bool PrimitiveStateTrue(const PrimitiveStates &primitive_states, const std::string &state_name)
Check whether one primitive state is true.
Definition: utility.hpp:30
std::string FlexivTypes2Str(const rdk::FlexivDataTypes &variant, size_t decimal=3, const std::string &separator=" ")
Convert the commonly used std::variant to a string.
Definition: utility.hpp:177
double Rad2Deg(double rad)
Convert radians to degrees for a single value.
Definition: utility.hpp:100
bool ProgramArgsExistAny(int argc, char **argv, const std::vector< std::string > &ref_strings)
Check if any provided strings exist in the program arguments.
Definition: utility.hpp:217
bool PrimitiveStateTrueForGroups(const std::map< JointGroup, PrimitiveStates > &primitive_states, const std::string &state_name)
Check whether one primitive state is true for all groups included in [primitive_states].
Definition: utility.hpp:51
std::string Arr2Str(const std::array< T, N > &arr, size_t decimal=3, const std::string &separator=" ")
Convert an std::array to a string.
Definition: utility.hpp:162