utils.h
Go to the documentation of this file.
1 
26 #ifndef TESSERACT_COMMON_UTILS_H
27 #define TESSERACT_COMMON_UTILS_H
28 
31 #include <array>
32 #include <vector>
33 #include <set>
34 #include <string>
35 #include <sstream>
36 #include <stdexcept>
37 #include <random>
38 #include <Eigen/Core>
39 #include <Eigen/Geometry>
41 
43 #include <filesystem>
44 
45 namespace tinyxml2
46 {
47 class XMLElement; // NOLINT
48 class XMLAttribute;
49 } // namespace tinyxml2
50 
51 namespace tesseract_common
52 {
53 #if __cplusplus < 201703L
54 
55 static std::mt19937 mersenne{ static_cast<std::mt19937::result_type>(std::time(nullptr)) };
56 #else
57 
58 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
59 inline std::mt19937 mersenne{ static_cast<std::mt19937::result_type>(std::time(nullptr)) };
60 #endif
61 
62 template <typename... Args>
63 std::string strFormat(const std::string& format, Args... args)
64 {
65  int size_s = std::snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
66  if (size_s <= 0)
67  throw std::runtime_error("Error during formatting.");
68 
69  auto size = static_cast<size_t>(size_s);
70  auto buf = std::make_unique<char[]>(size); // NOLINT
71  std::snprintf(buf.get(), size, format.c_str(), args...);
72  return std::string{ buf.get(), buf.get() + size - 1 }; // We don't want the '\0' inside
73 }
74 
80 std::string fileToString(const std::filesystem::path& filepath);
81 
88 void twistChangeRefPoint(Eigen::Ref<Eigen::VectorXd> twist, const Eigen::Ref<const Eigen::Vector3d>& ref_point);
89 
95 void twistChangeBase(Eigen::Ref<Eigen::VectorXd> twist, const Eigen::Isometry3d& change_base);
96 
102 void jacobianChangeBase(Eigen::Ref<Eigen::MatrixXd> jacobian, const Eigen::Isometry3d& change_base);
103 
110 void jacobianChangeRefPoint(Eigen::Ref<Eigen::MatrixXd> jacobian, const Eigen::Ref<const Eigen::Vector3d>& ref_point);
111 
113 Eigen::VectorXd concat(const Eigen::VectorXd& a, const Eigen::VectorXd& b);
114 
122 Eigen::Vector3d calcRotationalError(const Eigen::Ref<const Eigen::Matrix3d>& R);
123 
131 Eigen::VectorXd calcTransformError(const Eigen::Isometry3d& t1, const Eigen::Isometry3d& t2);
132 
141 Eigen::VectorXd calcJacobianTransformErrorDiff(const Eigen::Isometry3d& target,
142  const Eigen::Isometry3d& source,
143  const Eigen::Isometry3d& source_perturbed);
144 
154 Eigen::VectorXd calcJacobianTransformErrorDiff(const Eigen::Isometry3d& target,
155  const Eigen::Isometry3d& target_perturbed,
156  const Eigen::Isometry3d& source,
157  const Eigen::Isometry3d& source_perturbed);
158 
163 Eigen::Vector4d computeRandomColor();
164 
170 void printNestedException(const std::exception& e, int level = 0);
171 
176 std::string getTempPath();
177 
183 bool isNumeric(const std::string& s);
184 
190 bool isNumeric(const std::vector<std::string>& sv);
191 
197 Eigen::VectorXd generateRandomNumber(const Eigen::Ref<const Eigen::MatrixX2d>& limits);
198 
203 void ltrim(std::string& s);
204 
209 void rtrim(std::string& s);
210 
215 void trim(std::string& s);
216 
226 template <typename T>
228  const std::vector<T>& vec1,
229  const std::vector<T>& vec2,
230  bool ordered = true,
231  const std::function<bool(const T&, const T&)>& equal_pred = [](const T& v1, const T& v2) { return v1 == v2; },
232  const std::function<bool(const T&, const T&)>& comp = [](const T& v1, const T& v2) { return v1 < v2; })
233 {
234  if (vec1.size() != vec2.size())
235  return false;
236 
237  if (ordered)
238  return std::equal(vec1.begin(), vec1.end(), vec2.begin(), equal_pred);
239 
240  std::vector<T> v1 = vec1;
241  std::vector<T> v2 = vec2;
242  std::sort(v1.begin(), v1.end(), comp);
243  std::sort(v2.begin(), v2.end(), comp);
244  return std::equal(v1.begin(), v1.end(), v2.begin(), equal_pred);
245 }
246 
253 template <typename KeyValueContainerType, typename ValueType>
255  const KeyValueContainerType& map_1,
256  const KeyValueContainerType& map_2,
257  const std::function<bool(const ValueType&, const ValueType&)>& value_eq =
258  [](const ValueType& v1, const ValueType& v2) { return v1 == v2; })
259 {
260  if (map_1.size() != map_2.size())
261  return false;
262 
263  for (const auto& entry : map_1)
264  {
265  // Check if the key exists
266  const auto cp = map_2.find(entry.first);
267  if (cp == map_2.end())
268  return false;
269  // Check if the value is the same
270  if (!value_eq(cp->second, entry.second))
271  return false;
272  }
273  return true;
274 }
275 
282 template <typename ValueType>
284  const std::set<ValueType>& set_1,
285  const std::set<ValueType>& set_2,
286  const std::function<bool(const ValueType&, const ValueType&)>& value_eq =
287  [](const ValueType& v1, const ValueType& v2) { return v1 == v2; })
288 {
289  if (set_1.size() != set_2.size())
290  return false;
291 
292  for (const auto& entry : set_1)
293  {
294  // Check if the key exists
295  const auto cp = set_2.find(entry);
296  if (cp == set_2.end())
297  return false;
298  // Check if the value is the same
299  if (!value_eq(*cp, entry))
300  return false;
301  }
302  return true;
303 }
304 
311 template <typename ValueType, std::size_t Size>
313  const std::array<ValueType, Size>& array_1,
314  const std::array<ValueType, Size>& array_2,
315  const std::function<bool(const ValueType&, const ValueType&)>& value_eq =
316  [](const ValueType& v1, const ValueType& v2) { return v1 == v2; })
317 {
318  if (array_1.size() != array_2.size())
319  return false;
320 
321  for (std::size_t idx = 0; idx < array_1.size(); idx++)
322  {
323  if (!value_eq(array_1[idx], array_2[idx]))
324  return false;
325  }
326  return true;
327 }
328 
335 template <typename T>
336 bool pointersEqual(const std::shared_ptr<T>& p1, const std::shared_ptr<T>& p2)
337 {
338  return (p1 && p2 && *p1 == *p2) || (!p1 && !p2);
339 }
346 template <typename T>
347 bool pointersComparison(const std::shared_ptr<T>& p1, const std::shared_ptr<T>& p2)
348 {
349  if (p1 && p2)
350  return *p1 < *p2;
351  // If p2 is !nullptr then return true. p1 or both are nullptr should be false
352  return p2 != nullptr;
353 }
358 std::string getTimestampString();
359 
365 void reorder(Eigen::Ref<Eigen::VectorXd> v, std::vector<Eigen::Index> order);
366 
373 int QueryStringValue(const tinyxml2::XMLElement* xml_element, std::string& value);
374 
381 int QueryStringText(const tinyxml2::XMLElement* xml_element, std::string& text);
382 
389 int QueryStringValue(const tinyxml2::XMLAttribute* xml_attribute, std::string& value);
390 
399 int QueryStringAttribute(const tinyxml2::XMLElement* xml_element, const char* name, std::string& value);
400 
408 std::string StringAttribute(const tinyxml2::XMLElement* xml_element, const char* name, std::string default_value);
409 
422 int QueryStringAttributeRequired(const tinyxml2::XMLElement* xml_element, const char* name, std::string& value);
423 
436 int QueryDoubleAttributeRequired(const tinyxml2::XMLElement* xml_element, const char* name, double& value);
437 
450 int QueryIntAttributeRequired(const tinyxml2::XMLElement* xml_element, const char* name, int& value);
451 
462 bool almostEqualRelativeAndAbs(double a,
463  double b,
464  double max_diff = 1e-6,
465  double max_rel_diff = std::numeric_limits<double>::epsilon());
466 
479 bool almostEqualRelativeAndAbs(const Eigen::Ref<const Eigen::VectorXd>& v1,
480  const Eigen::Ref<const Eigen::VectorXd>& v2,
481  double max_diff = 1e-6,
482  double max_rel_diff = std::numeric_limits<double>::epsilon());
483 
496 bool almostEqualRelativeAndAbs(const Eigen::Ref<const Eigen::VectorXd>& v1,
497  const Eigen::Ref<const Eigen::VectorXd>& v2,
498  const Eigen::Ref<const Eigen::VectorXd>& max_diff,
499  const Eigen::Ref<const Eigen::VectorXd>& max_rel_diff);
500 
507 template <typename FloatType>
508 bool toNumeric(const std::string& s, FloatType& value)
509 {
510  if (s.empty())
511  return false;
512 
513  std::stringstream ss;
514  ss.imbue(std::locale::classic());
515 
516  ss << s;
517 
518  FloatType out;
519  ss >> out;
520 
521  if (ss.fail() || !ss.eof())
522  return false;
523 
524  value = out;
525  return true;
526 }
527 
535 std::vector<std::string> getAllowedCollisions(const std::vector<std::string>& link_names,
536  const AllowedCollisionEntries& acm_entries,
537  bool remove_duplicates = true);
538 
539 } // namespace tesseract_common
540 #endif // TESSERACT_COMMON_UTILS_H
allowed_collision_matrix.h
tesseract_common::getTempPath
std::string getTempPath()
Get the host temp directory path.
Definition: utils.cpp:262
tesseract_common::QueryStringAttributeRequired
int QueryStringAttributeRequired(const tinyxml2::XMLElement *xml_element, const char *name, std::string &value)
Query a string attribute from an xml element and print error log.
Definition: utils.cpp:385
tesseract_common
Definition: allowed_collision_matrix.h:19
tesseract_common::toNumeric
bool toNumeric(const std::string &s, FloatType &value)
Convert a string to a numeric value type.
Definition: utils.h:508
tesseract_common::generateRandomNumber
Eigen::VectorXd generateRandomNumber(const Eigen::Ref< const Eigen::MatrixX2d > &limits)
Given a set of limits it will generate a vector of random numbers between the limit.
Definition: utils.cpp:288
tesseract_common::concat
Eigen::VectorXd concat(const Eigen::VectorXd &a, const Eigen::VectorXd &b)
Concatenate two vector.
Definition: utils.cpp:119
tesseract_common::isIdentical
bool isIdentical(const std::vector< T > &vec1, const std::vector< T > &vec2, bool ordered=true, const std::function< bool(const T &, const T &)> &equal_pred=[](const T &v1, const T &v2) { return v1==v2;}, const std::function< bool(const T &, const T &)> &comp=[](const T &v1, const T &v2) { return v1< v2;})
Check if two vector of strings are identical.
Definition: utils.h:227
tesseract_common::AllowedCollisionEntries
std::unordered_map< tesseract_common::LinkNamesPair, std::string, tesseract_common::PairHash > AllowedCollisionEntries
Definition: allowed_collision_matrix.h:24
tesseract_common::pointersComparison
bool pointersComparison(const std::shared_ptr< T > &p1, const std::shared_ptr< T > &p2)
Comparison operator for the objects 2 points point to.
Definition: utils.h:347
tesseract_common::trim
void trim(std::string &s)
Trim left and right of string.
Definition: utils.cpp:304
tesseract_common::pointersEqual
bool pointersEqual(const std::shared_ptr< T > &p1, const std::shared_ptr< T > &p2)
Checks if 2 pointers point to objects that are ==.
Definition: utils.h:336
tesseract_common::jacobianChangeRefPoint
void jacobianChangeRefPoint(Eigen::Ref< Eigen::MatrixXd > jacobian, const Eigen::Ref< const Eigen::Vector3d > &ref_point)
Change the reference point of the jacobian.
Definition: utils.cpp:111
macros.h
Common Tesseract Macros.
tesseract_common::isIdenticalSet
bool isIdenticalSet(const std::set< ValueType > &set_1, const std::set< ValueType > &set_2, const std::function< bool(const ValueType &, const ValueType &)> &value_eq=[](const ValueType &v1, const ValueType &v2) { return v1==v2;})
Checks if 2 sets are identical.
Definition: utils.h:283
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
Definition: macros.h:71
tesseract_common::twistChangeBase
void twistChangeBase(Eigen::Ref< Eigen::VectorXd > twist, const Eigen::Isometry3d &change_base)
Change the base coordinate system of the Twist.
Definition: utils.cpp:98
tesseract_common::QueryStringValue
int QueryStringValue(const tinyxml2::XMLElement *xml_element, std::string &value)
Query a string value from xml element.
Definition: utils.cpp:339
tesseract_common::almostEqualRelativeAndAbs
bool almostEqualRelativeAndAbs(double a, double b, double max_diff=1e-6, double max_rel_diff=std::numeric_limits< double >::epsilon())
Check if two double are relatively equal.
Definition: utils.cpp:445
tesseract_common::calcRotationalError
Eigen::Vector3d calcRotationalError(const Eigen::Ref< const Eigen::Matrix3d > &R)
Calculate the rotation error vector given a rotation error matrix where the angle is between [-pi,...
Definition: utils.cpp:151
tesseract_common::twistChangeRefPoint
void twistChangeRefPoint(Eigen::Ref< Eigen::VectorXd > twist, const Eigen::Ref< const Eigen::Vector3d > &ref_point)
Change the reference point of the provided Twist.
Definition: utils.cpp:91
tesseract_common::reorder
void reorder(Eigen::Ref< Eigen::VectorXd > v, std::vector< Eigen::Index > order)
Reorder Eigen::VectorXd implace given index list.
Definition: utils.cpp:319
tesseract_common::strFormat
std::string strFormat(const std::string &format, Args... args)
Definition: utils.h:63
tesseract_common::isIdenticalArray
bool isIdenticalArray(const std::array< ValueType, Size > &array_1, const std::array< ValueType, Size > &array_2, const std::function< bool(const ValueType &, const ValueType &)> &value_eq=[](const ValueType &v1, const ValueType &v2) { return v1==v2;})
Checks if 2 arrays are identical.
Definition: utils.h:312
tesseract_common::QueryDoubleAttributeRequired
int QueryDoubleAttributeRequired(const tinyxml2::XMLElement *xml_element, const char *name, double &value)
Query a double attribute from an xml element and print error log.
Definition: utils.cpp:405
tesseract_common::mersenne
static std::mt19937 mersenne
Random number generator.
Definition: utils.h:55
tesseract_common::printNestedException
void printNestedException(const std::exception &e, int level=0)
Print a nested exception.
Definition: utils.cpp:246
tesseract_common::isIdenticalMap
bool isIdenticalMap(const KeyValueContainerType &map_1, const KeyValueContainerType &map_2, const std::function< bool(const ValueType &, const ValueType &)> &value_eq=[](const ValueType &v1, const ValueType &v2) { return v1==v2;})
Checks if 2 maps are identical.
Definition: utils.h:254
tesseract_common::getTimestampString
std::string getTimestampString()
Get Timestamp string.
Definition: utils.cpp:310
tesseract_common::QueryStringAttribute
int QueryStringAttribute(const tinyxml2::XMLElement *xml_element, const char *name, std::string &value)
Query a string attribute from an xml element.
Definition: utils.cpp:369
tesseract_common::fileToString
std::string fileToString(const std::filesystem::path &filepath)
Read in the contents of the file into a string.
Definition: utils.cpp:75
tesseract_common::QueryStringText
int QueryStringText(const tinyxml2::XMLElement *xml_element, std::string &text)
Query a string Text from xml element.
Definition: utils.cpp:349
tesseract_common::calcTransformError
Eigen::VectorXd calcTransformError(const Eigen::Isometry3d &t1, const Eigen::Isometry3d &t2)
Calculate error between two transforms expressed in t1 coordinate system.
Definition: utils.cpp:157
TESSERACT_COMMON_IGNORE_WARNINGS_POP
#define TESSERACT_COMMON_IGNORE_WARNINGS_POP
Definition: macros.h:72
tesseract_common::ltrim
void ltrim(std::string &s)
Left trim string.
Definition: utils.cpp:300
tesseract_common::getAllowedCollisions
std::vector< std::string > getAllowedCollisions(const std::vector< std::string > &link_names, const AllowedCollisionEntries &acm_entries, bool remove_duplicates=true)
Gets allowed collisions for a set of link names.
Definition: utils.cpp:492
tesseract_common::isNumeric
bool isNumeric(const std::string &s)
Determine if a string is a number.
Definition: utils.cpp:267
tesseract_common::jacobianChangeBase
void jacobianChangeBase(Eigen::Ref< Eigen::MatrixXd > jacobian, const Eigen::Isometry3d &change_base)
Change the base coordinate system of the jacobian.
Definition: utils.cpp:104
tesseract_common::rtrim
void rtrim(std::string &s)
Right trim string.
Definition: utils.cpp:302
tesseract_common::QueryIntAttributeRequired
int QueryIntAttributeRequired(const tinyxml2::XMLElement *xml_element, const char *name, int &value)
Query a int attribute from an xml element and print error log.
Definition: utils.cpp:425
tinyxml2
Definition: utils.h:45
tesseract_common::StringAttribute
std::string StringAttribute(const tinyxml2::XMLElement *xml_element, const char *name, std::string default_value)
Get string attribute if exist. If it does not exist it returns the default value.
Definition: utils.cpp:378
tesseract_common::calcJacobianTransformErrorDiff
Eigen::VectorXd calcJacobianTransformErrorDiff(const Eigen::Isometry3d &target, const Eigen::Isometry3d &source, const Eigen::Isometry3d &source_perturbed)
Calculate jacobian transform error difference expressed in the target frame coordinate system.
Definition: utils.cpp:163
tesseract_common::computeRandomColor
Eigen::Vector4d computeRandomColor()
This computes a random color RGBA [0, 1] with alpha set to 1.
Definition: utils.cpp:231


tesseract_common
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:01:40