Go to the documentation of this file.
26 #ifndef TESSERACT_COMMON_UTILS_H
27 #define TESSERACT_COMMON_UTILS_H
39 #include <Eigen/Geometry>
53 #if __cplusplus < 201703L
55 static std::mt19937
mersenne{
static_cast<std::mt19937::result_type
>(std::time(
nullptr)) };
59 inline std::mt19937
mersenne{
static_cast<std::mt19937::result_type
>(std::time(
nullptr)) };
62 template <
typename... Args>
63 std::string
strFormat(
const std::string& format, Args... args)
65 int size_s = std::snprintf(
nullptr, 0, format.c_str(), args...) + 1;
67 throw std::runtime_error(
"Error during formatting.");
69 auto size =
static_cast<size_t>(size_s);
70 auto buf = std::make_unique<char[]>(size);
71 std::snprintf(buf.get(), size, format.c_str(), args...);
72 return std::string{ buf.get(), buf.get() + size - 1 };
80 std::string
fileToString(
const std::filesystem::path& filepath);
88 void twistChangeRefPoint(Eigen::Ref<Eigen::VectorXd> twist,
const Eigen::Ref<const Eigen::Vector3d>& ref_point);
95 void twistChangeBase(Eigen::Ref<Eigen::VectorXd> twist,
const Eigen::Isometry3d& change_base);
102 void jacobianChangeBase(Eigen::Ref<Eigen::MatrixXd> jacobian,
const Eigen::Isometry3d& change_base);
110 void jacobianChangeRefPoint(Eigen::Ref<Eigen::MatrixXd> jacobian,
const Eigen::Ref<const Eigen::Vector3d>& ref_point);
113 Eigen::VectorXd
concat(
const Eigen::VectorXd& a,
const Eigen::VectorXd& b);
131 Eigen::VectorXd
calcTransformError(
const Eigen::Isometry3d& t1,
const Eigen::Isometry3d& t2);
142 const Eigen::Isometry3d& source,
143 const Eigen::Isometry3d& source_perturbed);
155 const Eigen::Isometry3d& target_perturbed,
156 const Eigen::Isometry3d& source,
157 const Eigen::Isometry3d& source_perturbed);
190 bool isNumeric(
const std::vector<std::string>& sv);
203 void ltrim(std::string& s);
209 void rtrim(std::string& s);
215 void trim(std::string& s);
226 template <
typename T>
228 const std::vector<T>& vec1,
229 const std::vector<T>& vec2,
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; })
234 if (vec1.size() != vec2.size())
238 return std::equal(vec1.begin(), vec1.end(), vec2.begin(), equal_pred);
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);
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; })
260 if (map_1.size() != map_2.size())
263 for (
const auto& entry : map_1)
266 const auto cp = map_2.find(entry.first);
267 if (cp == map_2.end())
270 if (!value_eq(cp->second, entry.second))
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; })
289 if (set_1.size() != set_2.size())
292 for (
const auto& entry : set_1)
295 const auto cp = set_2.find(entry);
296 if (cp == set_2.end())
299 if (!value_eq(*cp, entry))
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; })
318 if (array_1.size() != array_2.size())
321 for (std::size_t idx = 0; idx < array_1.size(); idx++)
323 if (!value_eq(array_1[idx], array_2[idx]))
335 template <
typename T>
336 bool pointersEqual(
const std::shared_ptr<T>& p1,
const std::shared_ptr<T>& p2)
338 return (p1 && p2 && *p1 == *p2) || (!p1 && !p2);
346 template <
typename T>
352 return p2 !=
nullptr;
365 void reorder(Eigen::Ref<Eigen::VectorXd> v, std::vector<Eigen::Index> order);
373 int QueryStringValue(
const tinyxml2::XMLElement* xml_element, std::string& value);
381 int QueryStringText(
const tinyxml2::XMLElement* xml_element, std::string& text);
389 int QueryStringValue(
const tinyxml2::XMLAttribute* xml_attribute, std::string& value);
399 int QueryStringAttribute(
const tinyxml2::XMLElement* xml_element,
const char* name, std::string& value);
408 std::string
StringAttribute(
const tinyxml2::XMLElement* xml_element,
const char* name, std::string default_value);
464 double max_diff = 1e-6,
465 double max_rel_diff = std::numeric_limits<double>::epsilon());
480 const Eigen::Ref<const Eigen::VectorXd>& v2,
481 double max_diff = 1e-6,
482 double max_rel_diff = std::numeric_limits<double>::epsilon());
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);
507 template <
typename FloatType>
513 std::stringstream ss;
514 ss.imbue(std::locale::classic());
521 if (ss.fail() || !ss.eof())
537 bool remove_duplicates =
true);
540 #endif // TESSERACT_COMMON_UTILS_H
std::string getTempPath()
Get the host temp directory path.
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.
bool toNumeric(const std::string &s, FloatType &value)
Convert a string to a numeric value type.
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.
Eigen::VectorXd concat(const Eigen::VectorXd &a, const Eigen::VectorXd &b)
Concatenate two vector.
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.
std::unordered_map< tesseract_common::LinkNamesPair, std::string, tesseract_common::PairHash > AllowedCollisionEntries
bool pointersComparison(const std::shared_ptr< T > &p1, const std::shared_ptr< T > &p2)
Comparison operator for the objects 2 points point to.
void trim(std::string &s)
Trim left and right of string.
bool pointersEqual(const std::shared_ptr< T > &p1, const std::shared_ptr< T > &p2)
Checks if 2 pointers point to objects that are ==.
void jacobianChangeRefPoint(Eigen::Ref< Eigen::MatrixXd > jacobian, const Eigen::Ref< const Eigen::Vector3d > &ref_point)
Change the reference point of the jacobian.
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.
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
void twistChangeBase(Eigen::Ref< Eigen::VectorXd > twist, const Eigen::Isometry3d &change_base)
Change the base coordinate system of the Twist.
int QueryStringValue(const tinyxml2::XMLElement *xml_element, std::string &value)
Query a string value from xml element.
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.
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,...
void twistChangeRefPoint(Eigen::Ref< Eigen::VectorXd > twist, const Eigen::Ref< const Eigen::Vector3d > &ref_point)
Change the reference point of the provided Twist.
void reorder(Eigen::Ref< Eigen::VectorXd > v, std::vector< Eigen::Index > order)
Reorder Eigen::VectorXd implace given index list.
std::string strFormat(const std::string &format, Args... args)
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.
int QueryDoubleAttributeRequired(const tinyxml2::XMLElement *xml_element, const char *name, double &value)
Query a double attribute from an xml element and print error log.
static std::mt19937 mersenne
Random number generator.
void printNestedException(const std::exception &e, int level=0)
Print a nested exception.
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.
std::string getTimestampString()
Get Timestamp string.
int QueryStringAttribute(const tinyxml2::XMLElement *xml_element, const char *name, std::string &value)
Query a string attribute from an xml element.
std::string fileToString(const std::filesystem::path &filepath)
Read in the contents of the file into a string.
int QueryStringText(const tinyxml2::XMLElement *xml_element, std::string &text)
Query a string Text from xml element.
Eigen::VectorXd calcTransformError(const Eigen::Isometry3d &t1, const Eigen::Isometry3d &t2)
Calculate error between two transforms expressed in t1 coordinate system.
#define TESSERACT_COMMON_IGNORE_WARNINGS_POP
void ltrim(std::string &s)
Left trim string.
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.
bool isNumeric(const std::string &s)
Determine if a string is a number.
void jacobianChangeBase(Eigen::Ref< Eigen::MatrixXd > jacobian, const Eigen::Isometry3d &change_base)
Change the base coordinate system of the jacobian.
void rtrim(std::string &s)
Right trim string.
int QueryIntAttributeRequired(const tinyxml2::XMLElement *xml_element, const char *name, int &value)
Query a int attribute from an xml element and print error log.
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.
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.
Eigen::Vector4d computeRandomColor()
This computes a random color RGBA [0, 1] with alpha set to 1.
tesseract_common
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:01:40