26#ifndef TESSERACT_COMMON_YAML_UTILS_H
27#define TESSERACT_COMMON_YAML_UTILS_H
31#include <yaml-cpp/yaml.h>
34#include <console_bridge/console.h>
49 std::stringstream stream;
58inline YAML::Node
fromYAMLString(
const std::string&
string) {
return YAML::Load(
string); }
65inline bool compareYAML(
const YAML::Node& node1,
const YAML::Node& node2)
70 if (node1.Type() != node2.Type())
75 case YAML::NodeType::Scalar:
79 auto v1 = node1.as<
bool>();
80 auto v2 = node2.as<
bool>();
83 catch (YAML::TypedBadConversion<bool>& )
87 auto v1 = node1.as<
int>();
88 auto v2 = node2.as<
int>();
91 catch (YAML::TypedBadConversion<int>& )
95 auto v1 = node1.as<
double>();
96 auto v2 = node2.as<
double>();
99 catch (YAML::TypedBadConversion<double>& )
103 auto v1 = node1.as<std::string>();
104 auto v2 = node2.as<std::string>();
107 catch (YAML::TypedBadConversion<std::string>& )
115 case YAML::NodeType::Null:
117 case YAML::NodeType::Undefined:
119 case YAML::NodeType::Map:
120 case YAML::NodeType::Sequence:
121 if (node1.size() != node2.size())
128 for (YAML::const_iterator it1 = node1.begin(),
it2; it1 != node1.end() && result; ++it1)
130 for (
it2 = node2.begin();
it2 != node2.end(); ++
it2)
135 if (
it2 == node2.end())
143 return std::equal(node1.begin(), node1.end(), node2.begin(),
compareYAML);
158 node[
"config"] = rhs.
config;
167 throw std::runtime_error(
"PluginInfo, missing 'class' entry!");
169 rhs.
class_name = node[
"class"].as<std::string>();
172 rhs.
config = node[
"config"];
197 if (!node[
"plugins"])
198 throw std::runtime_error(
"PluginInfoContainer, missing 'plugins' entry!");
200 const Node&
plugins = node[
"plugins"];
202 throw std::runtime_error(
"PluginInfoContainer, 'plugins' should contain a map of plugins!");
208 catch (
const std::exception& e)
210 throw std::runtime_error(std::string(
"PluginInfoContainer: Constructor failed to cast 'plugins' to "
211 "tesseract_common::PluginInfoMap! Details: ") +
219template <
typename T,
typename A>
220struct convert<std::set<T, A>>
222 static Node
encode(
const std::set<T, A>& rhs)
224 Node node(NodeType::Sequence);
225 for (
const auto&
element : rhs)
230 static bool decode(
const Node& node, std::set<T, A>& rhs)
232 if (!node.IsSequence())
236 for (
const auto&
element : node)
237 rhs.insert(
element.as<std::string>());
244struct convert<Eigen::Isometry3d>
246 static Node
encode(
const Eigen::Isometry3d& rhs)
249 xyz[
"x"] = rhs.translation().x();
250 xyz[
"y"] = rhs.translation().y();
251 xyz[
"z"] = rhs.translation().z();
253 const Eigen::Quaterniond
q(rhs.linear());
261 node[
"position"] = xyz;
262 node[
"orientation"] = quat;
267 static bool decode(
const Node& node, Eigen::Isometry3d& rhs)
269 Eigen::Isometry3d out = Eigen::Isometry3d::Identity();
271 const YAML::Node& p = node[
"position"];
272 out.translation().x() = p[
"x"].as<
double>();
273 out.translation().y() = p[
"y"].as<
double>();
274 out.translation().z() = p[
"z"].as<
double>();
276 const YAML::Node& o = node[
"orientation"];
277 if (o[
"x"] && o[
"y"] && o[
"z"] && o[
"w"])
279 Eigen::Quaterniond quat;
280 quat.x() = o[
"x"].as<
double>();
281 quat.y() = o[
"y"].as<
double>();
282 quat.z() = o[
"z"].as<
double>();
283 quat.w() = o[
"w"].as<
double>();
286 out.linear() = quat.toRotationMatrix();
288 else if (o[
"r"] && o[
"p"] && o[
"y"])
290 auto r = o[
"r"].as<
double>();
291 auto p = o[
"p"].as<
double>();
292 auto y = o[
"y"].as<
double>();
294 Eigen::AngleAxisd rollAngle(r, Eigen::Vector3d::UnitX());
295 Eigen::AngleAxisd pitchAngle(p, Eigen::Vector3d::UnitY());
296 Eigen::AngleAxisd yawAngle(y, Eigen::Vector3d::UnitZ());
298 Eigen::Quaterniond rpy{ yawAngle * pitchAngle * rollAngle };
300 out.linear() = rpy.toRotationMatrix();
304 throw std::runtime_error(
"Eigen::Isometry3d, failed to decode orientation missing (x, y, z, w) or (r, p, y)");
313struct convert<Eigen::VectorXd>
315 static Node
encode(
const Eigen::VectorXd& rhs)
318 for (
long i = 0; i < rhs.size(); ++i)
319 node.push_back(rhs(i));
324 static bool decode(
const Node& node, Eigen::VectorXd& rhs)
326 if (!node.IsSequence())
329 rhs.resize(
static_cast<Eigen::Index
>(node.size()));
330 for (
long i = 0; i < node.size(); ++i)
331 rhs(i) = node[i].as<
double>();
338struct convert<Eigen::Vector2d>
340 static Node
encode(
const Eigen::Vector2d& rhs)
343 for (
long i = 0; i < rhs.size(); ++i)
344 node.push_back(rhs(i));
349 static bool decode(
const Node& node, Eigen::Vector2d& rhs)
351 if (!node.IsSequence() || (node.size() != 2))
354 for (
long i = 0; i < 2; ++i)
355 rhs(i) = node[i].as<
double>();
366 const std::string SEARCH_PATHS_KEY{
"search_paths" };
367 const std::string SEARCH_LIBRARIES_KEY{
"search_libraries" };
368 const std::string FWD_KIN_PLUGINS_KEY{
"fwd_kin_plugins" };
369 const std::string INV_KIN_PLUGINS_KEY{
"inv_kin_plugins" };
371 YAML::Node kinematic_plugins;
384 return kinematic_plugins;
389 const std::string SEARCH_PATHS_KEY{
"search_paths" };
390 const std::string SEARCH_LIBRARIES_KEY{
"search_libraries" };
391 const std::string FWD_KIN_PLUGINS_KEY{
"fwd_kin_plugins" };
392 const std::string INV_KIN_PLUGINS_KEY{
"inv_kin_plugins" };
394 if (
const YAML::Node&
search_paths = node[SEARCH_PATHS_KEY])
396 std::set<std::string> sp;
401 catch (
const std::exception& e)
403 throw std::runtime_error(
"KinematicsPluginFactory: Constructor failed to cast '" + SEARCH_PATHS_KEY +
404 "' to std::set<std::string>! "
413 std::set<std::string> sl;
418 catch (
const std::exception& e)
420 throw std::runtime_error(
"KinematicsPluginFactory: Constructor failed to cast '" + SEARCH_LIBRARIES_KEY +
421 "' to std::set<std::string>! "
428 if (
const YAML::Node& fwd_kin_plugins = node[FWD_KIN_PLUGINS_KEY])
430 if (!fwd_kin_plugins.IsMap())
431 throw std::runtime_error(FWD_KIN_PLUGINS_KEY +
", should contain a map of group names to solver plugins!");
435 rhs.
fwd_plugin_infos = fwd_kin_plugins.as<std::map<std::string, tesseract_common::PluginInfoContainer>>();
437 catch (
const std::exception& e)
439 throw std::runtime_error(
"KinematicsPluginFactory: Constructor failed to cast '" + FWD_KIN_PLUGINS_KEY +
440 "' to std::map<std::string, "
441 "tesseract_common::PluginInfoContainer>! Details: " +
446 if (
const YAML::Node& inv_kin_plugins = node[INV_KIN_PLUGINS_KEY])
448 if (!inv_kin_plugins.IsMap())
449 throw std::runtime_error(INV_KIN_PLUGINS_KEY +
", should contain a map of group names to solver plugins!");
453 rhs.
inv_plugin_infos = inv_kin_plugins.as<std::map<std::string, tesseract_common::PluginInfoContainer>>();
455 catch (
const std::exception& e)
457 throw std::runtime_error(
"KinematicsPluginFactory: Constructor failed to cast '" + INV_KIN_PLUGINS_KEY +
458 "' to std::map<std::string, "
459 "tesseract_common::PluginInfoContainer>! Details: " +
473 const std::string SEARCH_PATHS_KEY{
"search_paths" };
474 const std::string SEARCH_LIBRARIES_KEY{
"search_libraries" };
475 const std::string DISCRETE_PLUGINS_KEY{
"discrete_plugins" };
476 const std::string CONTINUOUS_PLUGINS_KEY{
"continuous_plugins" };
478 YAML::Node contact_manager_plugins;
480 contact_manager_plugins[SEARCH_PATHS_KEY] = rhs.
search_paths;
491 return contact_manager_plugins;
496 const std::string SEARCH_PATHS_KEY{
"search_paths" };
497 const std::string SEARCH_LIBRARIES_KEY{
"search_libraries" };
498 const std::string DISCRETE_PLUGINS_KEY{
"discrete_plugins" };
499 const std::string CONTINUOUS_PLUGINS_KEY{
"continuous_plugins" };
501 if (
const YAML::Node&
search_paths = node[SEARCH_PATHS_KEY])
503 std::set<std::string> sp;
508 catch (
const std::exception& e)
510 throw std::runtime_error(
"ContactManagersPluginFactory: Constructor failed to cast '" + SEARCH_PATHS_KEY +
511 "' to std::set<std::string>! "
520 std::set<std::string> sl;
525 catch (
const std::exception& e)
527 throw std::runtime_error(
"ContactManagersPluginFactory: Constructor failed to cast '" + SEARCH_LIBRARIES_KEY +
528 "' to std::set<std::string>! "
538 throw std::runtime_error(DISCRETE_PLUGINS_KEY +
", should contain a map of contact manager names to plugins!");
544 catch (
const std::exception& e)
546 throw std::runtime_error(
"ContactManagersPluginFactory: Constructor failed to cast '" + DISCRETE_PLUGINS_KEY +
547 "' to tesseract_common::PluginInfoContainer! Details: " + e.what());
554 throw std::runtime_error(CONTINUOUS_PLUGINS_KEY +
", should contain a map of names to plugins!");
560 catch (
const std::exception& e)
562 throw std::runtime_error(
"ContactManagersPluginFactory: Constructor failed to cast '" + CONTINUOUS_PLUGINS_KEY +
563 "' to tesseract_common::PluginInfoContainer! Details: " + e.what());
576 const std::string SEARCH_PATHS_KEY{
"search_paths" };
577 const std::string SEARCH_LIBRARIES_KEY{
"search_libraries" };
578 const std::string EXECUTOR_PLUGINS_KEY{
"executors" };
579 const std::string NODE_PLUGINS_KEY{
"tasks" };
581 YAML::Node task_composer_plugins;
583 task_composer_plugins[SEARCH_PATHS_KEY] = rhs.
search_paths;
594 return task_composer_plugins;
599 const std::string SEARCH_PATHS_KEY{
"search_paths" };
600 const std::string SEARCH_LIBRARIES_KEY{
"search_libraries" };
601 const std::string EXECUTOR_PLUGINS_KEY{
"executors" };
602 const std::string NODE_PLUGINS_KEY{
"tasks" };
604 if (
const YAML::Node&
search_paths = node[SEARCH_PATHS_KEY])
606 std::set<std::string> sp;
611 catch (
const std::exception& e)
613 throw std::runtime_error(
"TaskComposerPluginInfo: Constructor failed to cast '" + SEARCH_PATHS_KEY +
614 "' to std::set<std::string>! "
623 std::set<std::string> sl;
628 catch (
const std::exception& e)
630 throw std::runtime_error(
"TaskComposerPluginInfo: Constructor failed to cast '" + SEARCH_LIBRARIES_KEY +
631 "' to std::set<std::string>! "
638 if (
const YAML::Node& executor_plugins = node[EXECUTOR_PLUGINS_KEY])
640 if (!executor_plugins.IsMap())
641 throw std::runtime_error(EXECUTOR_PLUGINS_KEY +
", should contain a map of task composer executor names to "
648 catch (
const std::exception& e)
650 throw std::runtime_error(
"TaskComposerPluginInfo: Constructor failed to cast '" + EXECUTOR_PLUGINS_KEY +
651 "' to tesseract_common::PluginInfoContainer! Details: " + e.what());
655 if (
const YAML::Node& node_plugins = node[NODE_PLUGINS_KEY])
657 if (!node_plugins.IsMap())
658 throw std::runtime_error(NODE_PLUGINS_KEY +
", should contain a map of names to plugins!");
664 catch (
const std::exception& e)
666 throw std::runtime_error(
"TaskComposerPluginInfo: Constructor failed to cast '" + NODE_PLUGINS_KEY +
667 "' to tesseract_common::PluginInfoContainer! Details: " + e.what());
681 for (
const auto& pair : rhs)
682 node[pair.first] = pair.second;
692 for (
const auto& pair : node)
693 rhs[pair.first.as<std::string>()] = pair.second.as<Eigen::Isometry3d>();
705 node[
"joints"] = rhs.
joints;
712 const YAML::Node& joints_node = node[
"joints"];
q[0]
Definition: kinematics_core_unit.cpp:15
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
Definition: macros.h:71
Definition: create_convex_hull.cpp:36
Definition: yaml_utils.h:148
Definition: allowed_collision_matrix.h:16
std::map< std::string, PluginInfo > PluginInfoMap
A map of PluginInfo to user defined name.
Definition: types.h:130
bool compareYAML(const YAML::Node &node1, const YAML::Node &node2)
Checks if the YAML::Nodes are identical.
Definition: yaml_utils.h:65
AlignedMap< std::string, Eigen::Isometry3d > TransformMap
Definition: types.h:66
std::string toYAMLString(const YAML::Node &node)
Converts a YAML::Node to a yaml string.
Definition: yaml_utils.h:47
YAML::Node fromYAMLString(const std::string &string)
Converts yaml string to a YAML::Node.
Definition: yaml_utils.h:58
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:393
static Node encode(const Eigen::Isometry3d &rhs)
Definition: yaml_utils.h:246
static bool decode(const Node &node, Eigen::Isometry3d &rhs)
Definition: yaml_utils.h:267
static Node encode(const Eigen::Vector2d &rhs)
Definition: yaml_utils.h:340
static bool decode(const Node &node, Eigen::Vector2d &rhs)
Definition: yaml_utils.h:349
static bool decode(const Node &node, Eigen::VectorXd &rhs)
Definition: yaml_utils.h:324
static Node encode(const Eigen::VectorXd &rhs)
Definition: yaml_utils.h:315
static bool decode(const Node &node, std::set< T, A > &rhs)
Definition: yaml_utils.h:230
static Node encode(const std::set< T, A > &rhs)
Definition: yaml_utils.h:222
static bool decode(const Node &node, tesseract_common::CalibrationInfo &rhs)
Definition: yaml_utils.h:710
static Node encode(const tesseract_common::CalibrationInfo &rhs)
Definition: yaml_utils.h:702
static Node encode(const tesseract_common::KinematicsPluginInfo &rhs)
Definition: yaml_utils.h:364
static bool decode(const Node &node, tesseract_common::KinematicsPluginInfo &rhs)
Definition: yaml_utils.h:387
static bool decode(const Node &node, tesseract_common::PluginInfoContainer &rhs)
Definition: yaml_utils.h:192
static Node encode(const tesseract_common::PluginInfoContainer &rhs)
Definition: yaml_utils.h:181
static Node encode(const tesseract_common::PluginInfo &rhs)
Definition: yaml_utils.h:152
static bool decode(const Node &node, tesseract_common::PluginInfo &rhs)
Definition: yaml_utils.h:163
static bool decode(const Node &node, tesseract_common::TaskComposerPluginInfo &rhs)
Definition: yaml_utils.h:597
static Node encode(const tesseract_common::TaskComposerPluginInfo &rhs)
Definition: yaml_utils.h:574
The CalibrationInfo struct.
Definition: types.h:257
tesseract_common::TransformMap joints
The joint origin calibration information.
Definition: types.h:269
The kinematics plugin information structure.
Definition: types.h:149
std::map< std::string, tesseract_common::PluginInfoContainer > inv_plugin_infos
A map of group name to inverse kinematics plugin information.
Definition: types.h:160
std::set< std::string > search_libraries
A list of library names without the prefix or suffix that contain plugins.
Definition: types.h:154
std::map< std::string, tesseract_common::PluginInfoContainer > fwd_plugin_infos
A map of group name to forward kinematics plugin information.
Definition: types.h:157
std::set< std::string > search_paths
A list of paths to search for plugins.
Definition: types.h:151
PluginInfoMap plugins
Definition: types.h:135
std::string default_plugin
Definition: types.h:134
The Plugin Information struct.
Definition: types.h:104
std::string class_name
The plugin class name.
Definition: types.h:106
YAML::Node config
The plugin config data.
Definition: types.h:109
The task composer plugin information structure.
Definition: types.h:221
tesseract_common::PluginInfoContainer task_plugin_infos
A map of name to task composer task plugin information.
Definition: types.h:232
tesseract_common::PluginInfoContainer executor_plugin_infos
A map of name to task composer executor plugin information.
Definition: types.h:229
std::set< std::string > search_libraries
A list of library names without the prefix or suffix that contain plugins.
Definition: types.h:226
std::set< std::string > search_paths
A list of paths to search for plugins.
Definition: types.h:223
Common Tesseract Utility Functions.
object discrete_plugin_infos plugins["plugin_key"]
Definition: tesseract_common_serialization_unit.cpp:148
std::vector< std::string > v2
Definition: tesseract_common_unit.cpp:418
v1["1"]
Definition: tesseract_common_unit.cpp:434
tinyxml2::XMLElement * element
Definition: tesseract_srdf_unit.cpp:815
auto it2
Definition: tesseract_srdf_unit.cpp:1274