groups.cpp
Go to the documentation of this file.
1 
29 #include <tinyxml2.h>
30 #include <iostream>
32 
33 #include <tesseract_common/utils.h>
35 #include <tesseract_srdf/groups.h>
36 
37 namespace tesseract_srdf
38 {
39 std::tuple<GroupNames, ChainGroups, JointGroups, LinkGroups>
41  const tinyxml2::XMLElement* srdf_xml,
42  const std::array<int, 3>& /*version*/)
43 {
45 
46  GroupNames group_names;
47  ChainGroups chain_groups;
48  LinkGroups link_groups;
49  JointGroups joint_groups;
50 
51  for (const tinyxml2::XMLElement* xml_element = srdf_xml->FirstChildElement("group"); xml_element != nullptr;
52  xml_element = xml_element->NextSiblingElement("group"))
53  {
54  std::string group_name;
55  int status = tesseract_common::QueryStringAttributeRequired(xml_element, "name", group_name);
56  if (status != tinyxml2::XML_SUCCESS)
57  std::throw_with_nested(std::runtime_error("Group: Missing or failed to parse attribute 'name'!"));
58 
59  std::vector<std::string> links;
60  std::vector<std::string> joints;
61  std::vector<std::pair<std::string, std::string>> chains;
62 
63  // get the links in the groups
64  for (const tinyxml2::XMLElement* link_xml = xml_element->FirstChildElement("link"); link_xml != nullptr;
65  link_xml = link_xml->NextSiblingElement("link"))
66  {
67  std::string link_name;
68  int status = tesseract_common::QueryStringAttributeRequired(link_xml, "name", link_name);
69  if (status != tinyxml2::XML_SUCCESS)
70  std::throw_with_nested(std::runtime_error(
71  strFormat("Group: '%s' link element is missing or failed to parse attribute 'name'!", group_name.c_str())));
72 
73  if (!scene_graph.getLink(link_name))
74  std::throw_with_nested(std::runtime_error(strFormat(
75  "Group: '%s' link '%s' is not known to the Scene Graph!", group_name.c_str(), link_name.c_str())));
76 
77  links.push_back(link_name);
78  }
79 
80  // get the joints in the groups
81  for (const tinyxml2::XMLElement* joint_xml = xml_element->FirstChildElement("joint"); joint_xml != nullptr;
82  joint_xml = joint_xml->NextSiblingElement("joint"))
83  {
84  std::string joint_name;
85  int status = tesseract_common::QueryStringAttributeRequired(joint_xml, "name", joint_name);
86  if (status != tinyxml2::XML_SUCCESS)
87  std::throw_with_nested(std::runtime_error(strFormat("Group: '%s' joint element is missing or failed to parse "
88  "attribute 'name'!",
89  group_name.c_str())));
90 
91  if (!scene_graph.getJoint(joint_name))
92  std::throw_with_nested(std::runtime_error(strFormat(
93  "Group: '%s' joint '%s' is not known to the Scene Graph!", group_name.c_str(), joint_name.c_str())));
94 
95  joints.push_back(joint_name);
96  }
97 
98  // get the chains in the groups
99  for (const tinyxml2::XMLElement* chain_xml = xml_element->FirstChildElement("chain"); chain_xml != nullptr;
100  chain_xml = chain_xml->NextSiblingElement("chain"))
101  {
102  std::string base_link_name, tip_link_name;
103  int status = tesseract_common::QueryStringAttributeRequired(chain_xml, "base_link", base_link_name);
104  if (status != tinyxml2::XML_SUCCESS)
105  std::throw_with_nested(std::runtime_error(strFormat("Group: '%s' chain element is missing or failed to parse "
106  "attribute "
107  "'base_link'!",
108  group_name.c_str())));
109 
110  status = tesseract_common::QueryStringAttributeRequired(chain_xml, "tip_link", tip_link_name);
111  if (status != tinyxml2::XML_SUCCESS)
112  std::throw_with_nested(std::runtime_error(strFormat("Group: '%s' chain element is missing or failed to parse "
113  "attribute "
114  "'tip_link'!",
115  group_name.c_str())));
116 
117  if (!scene_graph.getLink(base_link_name))
118  std::throw_with_nested(std::runtime_error(strFormat("Group: '%s' chain element base link '%s' is not known to "
119  "the Scene Graph!",
120  group_name.c_str(),
121  base_link_name.c_str())));
122 
123  if (!scene_graph.getLink(tip_link_name))
124  std::throw_with_nested(std::runtime_error(strFormat("Group: '%s' chain element tip link '%s' is not known to "
125  "the Scene Graph!",
126  group_name.c_str(),
127  tip_link_name.c_str())));
128 
129  chains.emplace_back(base_link_name, tip_link_name);
130  }
131 
132  if (!chains.empty() && links.empty() && joints.empty())
133  {
134  chain_groups[group_name] = chains;
135  group_names.insert(group_name);
136  }
137  else if (chains.empty() && !links.empty() && joints.empty())
138  {
139  link_groups[group_name] = links;
140  group_names.insert(group_name);
141  }
142  else if (chains.empty() && links.empty() && !joints.empty())
143  {
144  joint_groups[group_name] = joints;
145  group_names.insert(group_name);
146  }
147  else
148  {
149  std::throw_with_nested(
150  std::runtime_error(strFormat("Group: '%s' is empty or multiple types were provided!", group_name.c_str())));
151  }
152  }
153 
154  return std::make_tuple(group_names, chain_groups, joint_groups, link_groups);
155 }
156 } // namespace tesseract_srdf
graph.h
tesseract_srdf
Main namespace.
Definition: collision_margins.h:43
tesseract_common::QueryStringAttributeRequired
int QueryStringAttributeRequired(const tinyxml2::XMLElement *xml_element, const char *name, std::string &value)
utils.h
tesseract_srdf::JointGroups
std::unordered_map< std::string, JointGroup > JointGroups
Definition: kinematics_information.h:57
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
tesseract_srdf::LinkGroups
std::unordered_map< std::string, LinkGroup > LinkGroups
Definition: kinematics_information.h:59
tesseract_scene_graph::SceneGraph
tesseract_common::strFormat
std::string strFormat(const std::string &format, Args... args)
tesseract_srdf::parseGroups
std::tuple< GroupNames, ChainGroups, JointGroups, LinkGroups > parseGroups(const tesseract_scene_graph::SceneGraph &scene_graph, const tinyxml2::XMLElement *srdf_xml, const std::array< int, 3 > &version)
Parse groups from srdf xml element.
Definition: groups.cpp:40
tesseract_srdf::ChainGroups
std::unordered_map< std::string, ChainGroup > ChainGroups
Definition: kinematics_information.h:55
tesseract_srdf::GroupNames
std::set< std::string > GroupNames
Definition: kinematics_information.h:60
TESSERACT_COMMON_IGNORE_WARNINGS_POP
#define TESSERACT_COMMON_IGNORE_WARNINGS_POP
tesseract_scene_graph::SceneGraph::getJoint
std::shared_ptr< const Joint > getJoint(const std::string &name) const
groups.h
Parse groups data from srdf file.
tesseract_scene_graph::SceneGraph::getLink
std::shared_ptr< const Link > getLink(const std::string &name) const
macros.h


tesseract_srdf
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:02:04