26 #ifndef TESSERACT_COMMON_YAML_EXTENSTIONS_H
27 #define TESSERACT_COMMON_YAML_EXTENSTIONS_H
31 #include <yaml-cpp/yaml.h>
50 node[
"config"] = rhs.
config;
59 throw std::runtime_error(
"PluginInfo, missing 'class' entry!");
61 rhs.
class_name = node[
"class"].as<std::string>();
64 rhs.
config = node[
"config"];
90 throw std::runtime_error(
"PluginInfoContainer, missing 'plugins' entry!");
92 const Node& plugins = node[
"plugins"];
94 throw std::runtime_error(
"PluginInfoContainer, 'plugins' should contain a map of plugins!");
100 catch (
const std::exception& e)
102 throw std::runtime_error(std::string(
"PluginInfoContainer: Constructor failed to cast 'plugins' to "
103 "tesseract_common::PluginInfoMap! Details: ") +
111 template <
typename T,
typename A>
112 struct convert<std::set<T, A>>
114 static Node
encode(
const std::set<T, A>& rhs)
116 Node node(NodeType::Sequence);
117 for (
const auto& element : rhs)
118 node.push_back(element);
122 static bool decode(
const Node& node, std::set<T, A>& rhs)
124 if (!node.IsSequence())
128 for (
const auto& element : node)
129 rhs.insert(element.as<std::string>());
136 struct convert<Eigen::Isometry3d>
138 static Node
encode(
const Eigen::Isometry3d& rhs)
141 xyz[
"x"] = rhs.translation().x();
142 xyz[
"y"] = rhs.translation().y();
143 xyz[
"z"] = rhs.translation().z();
145 const Eigen::Quaterniond q(rhs.linear());
153 node[
"position"] = xyz;
154 node[
"orientation"] = quat;
159 static bool decode(
const Node& node, Eigen::Isometry3d& rhs)
161 Eigen::Isometry3d out = Eigen::Isometry3d::Identity();
163 const YAML::Node& p = node[
"position"];
164 out.translation().x() = p[
"x"].as<
double>();
165 out.translation().y() = p[
"y"].as<
double>();
166 out.translation().z() = p[
"z"].as<
double>();
168 const YAML::Node& o = node[
"orientation"];
169 if (o[
"x"] && o[
"y"] && o[
"z"] && o[
"w"])
171 Eigen::Quaterniond quat;
172 quat.x() = o[
"x"].as<
double>();
173 quat.y() = o[
"y"].as<
double>();
174 quat.z() = o[
"z"].as<
double>();
175 quat.w() = o[
"w"].as<
double>();
178 out.linear() = quat.toRotationMatrix();
180 else if (o[
"r"] && o[
"p"] && o[
"y"])
182 auto r = o[
"r"].as<
double>();
183 auto p = o[
"p"].as<
double>();
184 auto y = o[
"y"].as<
double>();
186 Eigen::AngleAxisd rollAngle(r, Eigen::Vector3d::UnitX());
187 Eigen::AngleAxisd pitchAngle(p, Eigen::Vector3d::UnitY());
188 Eigen::AngleAxisd yawAngle(y, Eigen::Vector3d::UnitZ());
190 Eigen::Quaterniond rpy{ yawAngle * pitchAngle * rollAngle };
192 out.linear() = rpy.toRotationMatrix();
196 throw std::runtime_error(
"Eigen::Isometry3d, failed to decode orientation missing (x, y, z, w) or (r, p, y)");
205 struct convert<Eigen::VectorXd>
207 static Node
encode(
const Eigen::VectorXd& rhs)
210 for (
long i = 0; i < static_cast<long>(rhs.size()); ++i)
211 node.push_back(rhs(i));
216 static bool decode(
const Node& node, Eigen::VectorXd& rhs)
218 if (!node.IsSequence())
221 rhs.resize(
static_cast<long>(node.size()));
222 for (
long i = 0; i < static_cast<long>(node.size()); ++i)
223 rhs(i) = node[i].as<
double>();
230 struct convert<Eigen::Vector2d>
232 static Node
encode(
const Eigen::Vector2d& rhs)
235 for (
long i = 0; i < rhs.size(); ++i)
236 node.push_back(rhs(i));
241 static bool decode(
const Node& node, Eigen::Vector2d& rhs)
243 if (!node.IsSequence() || (node.size() != 2))
246 for (
long i = 0; i < 2; ++i)
247 rhs(i) = node[i].as<
double>();
254 struct convert<Eigen::Vector3d>
256 static Node
encode(
const Eigen::Vector3d& rhs)
259 for (
long i = 0; i < rhs.size(); ++i)
260 node.push_back(rhs(i));
265 static bool decode(
const Node& node, Eigen::Vector3d& rhs)
267 if (!node.IsSequence() || (node.size() != 3))
270 for (
long i = 0; i < 3; ++i)
271 rhs(i) = node[i].as<
double>();
282 const std::string SEARCH_PATHS_KEY{
"search_paths" };
283 const std::string SEARCH_LIBRARIES_KEY{
"search_libraries" };
284 const std::string FWD_KIN_PLUGINS_KEY{
"fwd_kin_plugins" };
285 const std::string INV_KIN_PLUGINS_KEY{
"inv_kin_plugins" };
287 YAML::Node kinematic_plugins;
300 return kinematic_plugins;
305 const std::string SEARCH_PATHS_KEY{
"search_paths" };
306 const std::string SEARCH_LIBRARIES_KEY{
"search_libraries" };
307 const std::string FWD_KIN_PLUGINS_KEY{
"fwd_kin_plugins" };
308 const std::string INV_KIN_PLUGINS_KEY{
"inv_kin_plugins" };
310 if (
const YAML::Node& search_paths = node[SEARCH_PATHS_KEY])
312 std::set<std::string> sp;
315 sp = search_paths.as<std::set<std::string>>();
317 catch (
const std::exception& e)
319 throw std::runtime_error(
"KinematicsPluginFactory: Constructor failed to cast '" + SEARCH_PATHS_KEY +
320 "' to std::set<std::string>! "
327 if (
const YAML::Node& search_libraries = node[SEARCH_LIBRARIES_KEY])
329 std::set<std::string> sl;
332 sl = search_libraries.as<std::set<std::string>>();
334 catch (
const std::exception& e)
336 throw std::runtime_error(
"KinematicsPluginFactory: Constructor failed to cast '" + SEARCH_LIBRARIES_KEY +
337 "' to std::set<std::string>! "
344 if (
const YAML::Node& fwd_kin_plugins = node[FWD_KIN_PLUGINS_KEY])
346 if (!fwd_kin_plugins.IsMap())
347 throw std::runtime_error(FWD_KIN_PLUGINS_KEY +
", should contain a map of group names to solver plugins!");
351 rhs.
fwd_plugin_infos = fwd_kin_plugins.as<std::map<std::string, tesseract_common::PluginInfoContainer>>();
353 catch (
const std::exception& e)
355 throw std::runtime_error(
"KinematicsPluginFactory: Constructor failed to cast '" + FWD_KIN_PLUGINS_KEY +
356 "' to std::map<std::string, "
357 "tesseract_common::PluginInfoContainer>! Details: " +
362 if (
const YAML::Node& inv_kin_plugins = node[INV_KIN_PLUGINS_KEY])
364 if (!inv_kin_plugins.IsMap())
365 throw std::runtime_error(INV_KIN_PLUGINS_KEY +
", should contain a map of group names to solver plugins!");
369 rhs.
inv_plugin_infos = inv_kin_plugins.as<std::map<std::string, tesseract_common::PluginInfoContainer>>();
371 catch (
const std::exception& e)
373 throw std::runtime_error(
"KinematicsPluginFactory: Constructor failed to cast '" + INV_KIN_PLUGINS_KEY +
374 "' to std::map<std::string, "
375 "tesseract_common::PluginInfoContainer>! Details: " +
389 const std::string SEARCH_PATHS_KEY{
"search_paths" };
390 const std::string SEARCH_LIBRARIES_KEY{
"search_libraries" };
391 const std::string DISCRETE_PLUGINS_KEY{
"discrete_plugins" };
392 const std::string CONTINUOUS_PLUGINS_KEY{
"continuous_plugins" };
394 YAML::Node contact_manager_plugins;
396 contact_manager_plugins[SEARCH_PATHS_KEY] = rhs.
search_paths;
407 return contact_manager_plugins;
412 const std::string SEARCH_PATHS_KEY{
"search_paths" };
413 const std::string SEARCH_LIBRARIES_KEY{
"search_libraries" };
414 const std::string DISCRETE_PLUGINS_KEY{
"discrete_plugins" };
415 const std::string CONTINUOUS_PLUGINS_KEY{
"continuous_plugins" };
417 if (
const YAML::Node& search_paths = node[SEARCH_PATHS_KEY])
419 std::set<std::string> sp;
422 sp = search_paths.as<std::set<std::string>>();
424 catch (
const std::exception& e)
426 throw std::runtime_error(
"ContactManagersPluginFactory: Constructor failed to cast '" + SEARCH_PATHS_KEY +
427 "' to std::set<std::string>! "
434 if (
const YAML::Node& search_libraries = node[SEARCH_LIBRARIES_KEY])
436 std::set<std::string> sl;
439 sl = search_libraries.as<std::set<std::string>>();
441 catch (
const std::exception& e)
443 throw std::runtime_error(
"ContactManagersPluginFactory: Constructor failed to cast '" + SEARCH_LIBRARIES_KEY +
444 "' to std::set<std::string>! "
451 if (
const YAML::Node& discrete_plugins = node[DISCRETE_PLUGINS_KEY])
453 if (!discrete_plugins.IsMap())
454 throw std::runtime_error(DISCRETE_PLUGINS_KEY +
", should contain a map of contact manager names to plugins!");
460 catch (
const std::exception& e)
462 throw std::runtime_error(
"ContactManagersPluginFactory: Constructor failed to cast '" + DISCRETE_PLUGINS_KEY +
463 "' to tesseract_common::PluginInfoContainer! Details: " + e.what());
467 if (
const YAML::Node& continuous_plugins = node[CONTINUOUS_PLUGINS_KEY])
469 if (!continuous_plugins.IsMap())
470 throw std::runtime_error(CONTINUOUS_PLUGINS_KEY +
", should contain a map of names to plugins!");
476 catch (
const std::exception& e)
478 throw std::runtime_error(
"ContactManagersPluginFactory: Constructor failed to cast '" + CONTINUOUS_PLUGINS_KEY +
479 "' to tesseract_common::PluginInfoContainer! Details: " + e.what());
492 const std::string SEARCH_PATHS_KEY{
"search_paths" };
493 const std::string SEARCH_LIBRARIES_KEY{
"search_libraries" };
494 const std::string EXECUTOR_PLUGINS_KEY{
"executors" };
495 const std::string NODE_PLUGINS_KEY{
"tasks" };
497 YAML::Node task_composer_plugins;
499 task_composer_plugins[SEARCH_PATHS_KEY] = rhs.
search_paths;
510 return task_composer_plugins;
515 const std::string SEARCH_PATHS_KEY{
"search_paths" };
516 const std::string SEARCH_LIBRARIES_KEY{
"search_libraries" };
517 const std::string EXECUTOR_PLUGINS_KEY{
"executors" };
518 const std::string NODE_PLUGINS_KEY{
"tasks" };
520 if (
const YAML::Node& search_paths = node[SEARCH_PATHS_KEY])
522 std::set<std::string> sp;
525 sp = search_paths.as<std::set<std::string>>();
527 catch (
const std::exception& e)
529 throw std::runtime_error(
"TaskComposerPluginInfo: Constructor failed to cast '" + SEARCH_PATHS_KEY +
530 "' to std::set<std::string>! "
537 if (
const YAML::Node& search_libraries = node[SEARCH_LIBRARIES_KEY])
539 std::set<std::string> sl;
542 sl = search_libraries.as<std::set<std::string>>();
544 catch (
const std::exception& e)
546 throw std::runtime_error(
"TaskComposerPluginInfo: Constructor failed to cast '" + SEARCH_LIBRARIES_KEY +
547 "' to std::set<std::string>! "
554 if (
const YAML::Node& executor_plugins = node[EXECUTOR_PLUGINS_KEY])
556 if (!executor_plugins.IsMap())
557 throw std::runtime_error(EXECUTOR_PLUGINS_KEY +
", should contain a map of task composer executor names to "
564 catch (
const std::exception& e)
566 throw std::runtime_error(
"TaskComposerPluginInfo: Constructor failed to cast '" + EXECUTOR_PLUGINS_KEY +
567 "' to tesseract_common::PluginInfoContainer! Details: " + e.what());
571 if (
const YAML::Node& node_plugins = node[NODE_PLUGINS_KEY])
573 if (!node_plugins.IsMap())
574 throw std::runtime_error(NODE_PLUGINS_KEY +
", should contain a map of names to plugins!");
580 catch (
const std::exception& e)
582 throw std::runtime_error(
"TaskComposerPluginInfo: Constructor failed to cast '" + NODE_PLUGINS_KEY +
583 "' to tesseract_common::PluginInfoContainer! Details: " + e.what());
597 for (
const auto& pair : rhs)
598 node[pair.first] = pair.second;
608 for (
const auto& pair : node)
609 rhs[pair.first.as<std::string>()] = pair.second.as<Eigen::Isometry3d>();
621 node[
"joints"] = rhs.
joints;
628 const YAML::Node& joints_node = node[
"joints"];
642 for (
const auto& seg : rhs)
645 for (
const auto& p : seg)
646 seg_node.push_back(p);
648 node.push_back(seg_node);
656 if (!node.IsSequence())
659 rhs.reserve(node.size());
660 for (
const auto& seg_node : node)
663 seg.reserve(seg_node.size());
664 for (
const auto& p_node : seg_node)
665 seg.push_back(p_node.as<Eigen::Isometry3d>());
681 static const std::map<tesseract_common::CollisionMarginPairOverrideType, std::string> m = {
687 return Node(m.at(rhs));
693 static const std::map<std::string, tesseract_common::CollisionMarginPairOverrideType> inv = {
700 if (!node.IsScalar())
703 auto it = inv.find(node.Scalar());
718 Node node(NodeType::Map);
719 for (
const auto& pair : rhs)
721 Node key_node(NodeType::Sequence);
722 key_node.push_back(pair.first.first);
723 key_node.push_back(pair.first.second);
726 key_node.SetStyle(YAML::EmitterStyle::Flow);
728 node[key_node] = pair.second;
739 for (
auto it = node.begin(); it != node.end(); ++it)
741 Node key_node = it->first;
742 if (!key_node.IsSequence() || key_node.size() != 2)
745 std::pair<std::string, std::string> key;
746 key.first = key_node[0].as<std::string>();
747 key.second = key_node[1].as<std::string>();
749 auto v = it->second.as<
double>();
750 rhs.emplace(std::move(key), v);
780 Node node(NodeType::Map);
781 for (
const auto& pair : rhs)
783 Node key_node(NodeType::Sequence);
784 key_node.push_back(pair.first.first);
785 key_node.push_back(pair.first.second);
788 key_node.SetStyle(YAML::EmitterStyle::Flow);
790 node[key_node] = pair.second;
801 for (
auto it = node.begin(); it != node.end(); ++it)
803 Node key_node = it->first;
804 if (!key_node.IsSequence() || key_node.size() != 2)
807 std::pair<std::string, std::string> key;
808 key.first = key_node[0].as<std::string>();
809 key.second = key_node[1].as<std::string>();
811 auto v = it->second.as<std::string>();
812 rhs.emplace(std::move(key), v);
837 template <
typename T,
typename A>
838 struct convert<std::unordered_map<T, A>>
840 static Node
encode(
const std::unordered_map<T, A>& rhs)
842 Node node(NodeType::Map);
843 for (
const auto& pair : rhs)
844 node[pair.first] = pair.second;
849 static bool decode(
const Node& node, std::unordered_map<T, A>& rhs)
854 for (
auto it = node.begin(); it != node.end(); ++it)
855 rhs[it->first.as<T>()] = it->second.as<A>();
863 #endif // TESSERACT_COMMON_YAML_EXTENSTIONS_H