link.cpp
Go to the documentation of this file.
1 
29 #include <stdexcept>
30 
31 #include <boost/filesystem.hpp>
32 #include <tesseract_common/utils.h>
33 #include <tinyxml2.h>
35 
40 #include <tesseract_urdf/link.h>
41 #include <tesseract_urdf/utils.h>
42 #include <tesseract_urdf/visual.h>
43 
44 namespace tesseract_urdf
45 {
47 parseLink(const tinyxml2::XMLElement* xml_element,
48  const tesseract_common::ResourceLocator& locator,
49  bool make_convex_meshes,
50  std::unordered_map<std::string, tesseract_scene_graph::Material::Ptr>& available_materials)
51 {
52  std::string link_name;
53  if (tesseract_common::QueryStringAttribute(xml_element, "name", link_name) != tinyxml2::XML_SUCCESS)
54  std::throw_with_nested(std::runtime_error("Link: Missing or failed parsing attribute 'name'!"));
55 
56  auto l = std::make_shared<tesseract_scene_graph::Link>(link_name);
57 
58  // get inertia if it exists
59  const tinyxml2::XMLElement* inertial = xml_element->FirstChildElement("inertial");
60  if (inertial != nullptr)
61  {
62  try
63  {
64  l->inertial = parseInertial(inertial);
65  }
66  catch (...)
67  {
68  std::throw_with_nested(
69  std::runtime_error("Link: Error parsing 'inertial' element for link '" + link_name + "'!"));
70  }
71  }
72 
73  // get visual if it exists
74  for (const tinyxml2::XMLElement* visual = xml_element->FirstChildElement("visual"); visual != nullptr;
75  visual = visual->NextSiblingElement("visual"))
76  {
78  try
79  {
80  temp_visual = parseVisual(visual, locator, available_materials);
81  }
82  catch (...)
83  {
84  std::throw_with_nested(std::runtime_error("Link: Error parsing 'visual' element for link '" + link_name + "'!"));
85  }
86 
87  l->visual.push_back(temp_visual);
88  }
89 
90  // get collision if exists
91  for (const tinyxml2::XMLElement* collision = xml_element->FirstChildElement("collision"); collision != nullptr;
92  collision = collision->NextSiblingElement("collision"))
93  {
95  try
96  {
97  temp_collision = parseCollision(collision, locator, make_convex_meshes);
98  }
99  catch (...)
100  {
101  std::throw_with_nested(
102  std::runtime_error("Link: Error parsing 'collision' element for link '" + link_name + "'!"));
103  }
104 
105  l->collision.push_back(temp_collision);
106  }
107 
108  return l;
109 }
110 
111 tinyxml2::XMLElement* writeLink(const std::shared_ptr<const tesseract_scene_graph::Link>& link,
112  tinyxml2::XMLDocument& doc,
113  const std::string& package_path)
114 {
115  if (link == nullptr)
116  std::throw_with_nested(std::runtime_error("Link is nullptr and cannot be converted to XML"));
117  tinyxml2::XMLElement* xml_element = doc.NewElement(LINK_ELEMENT_NAME.data());
118 
119  // Set name
120  xml_element->SetAttribute("name", link->getName().c_str());
121 
122  // Set inertia if it exists
123  if (link->inertial != nullptr)
124  {
125  tinyxml2::XMLElement* xml_inertial = writeInertial(link->inertial, doc);
126  xml_element->InsertEndChild(xml_inertial);
127  }
128 
129  // Set visual if it exists
130  int id = -1;
131  if (link->visual.size() > 1)
132  id = 0;
133  for (const tesseract_scene_graph::Visual::Ptr& vis : link->visual)
134  {
135  try
136  {
137  std::filesystem::create_directory(std::filesystem::path(trailingSlash(package_path) + "visual/"));
138  tinyxml2::XMLElement* xml_visual = writeVisual(vis, doc, package_path, link->getName(), id++);
139  xml_element->InsertEndChild(xml_visual);
140  }
141  catch (...)
142  {
143  std::throw_with_nested(std::runtime_error("Could not write visual to XML for link `" + link->getName() + "`!"));
144  }
145  }
146 
147  // Set collision if it exists
148  id = -1;
149  if (link->collision.size() > 1)
150  id = 0;
151  for (const tesseract_scene_graph::Collision::Ptr& col : link->collision)
152  {
153  try
154  {
155  std::filesystem::create_directory(std::filesystem::path(trailingSlash(package_path) + "collision/"));
156  tinyxml2::XMLElement* xml_collision = writeCollision(col, doc, package_path, link->getName(), id++);
157  xml_element->InsertEndChild(xml_collision);
158  }
159  catch (...)
160  {
161  std::throw_with_nested(
162  std::runtime_error("Could not write collision to XML for link `" + link->getName() + "`!"));
163  }
164  }
165 
166  return xml_element;
167 }
168 
169 } // namespace tesseract_urdf
tesseract_urdf::writeCollision
tinyxml2::XMLElement * writeCollision(const std::shared_ptr< const tesseract_scene_graph::Collision > &collision, tinyxml2::XMLDocument &doc, const std::string &package_path, const std::string &link_name, int id)
writeCollision Write collision object to URDF XML
Definition: collision.cpp:90
utils.h
resource_locator.h
tesseract_urdf::trailingSlash
std::string trailingSlash(const std::string &path)
Definition: utils.cpp:20
tesseract_urdf::writeInertial
tinyxml2::XMLElement * writeInertial(const std::shared_ptr< const tesseract_scene_graph::Inertial > &inertial, tinyxml2::XMLDocument &doc)
Definition: inertial.cpp:89
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
tesseract_urdf::writeVisual
tinyxml2::XMLElement * writeVisual(const std::shared_ptr< const tesseract_scene_graph::Visual > &visual, tinyxml2::XMLDocument &doc, const std::string &package_path, const std::string &link_name, int id)
writeVisual Write one visual geometry object to URDF XML
Definition: visual.cpp:110
tesseract_urdf::parseCollision
std::shared_ptr< tesseract_scene_graph::Collision > parseCollision(const tinyxml2::XMLElement *xml_element, const tesseract_common::ResourceLocator &locator, bool make_convex_meshes)
Parse xml element collision.
Definition: collision.cpp:45
utils.h
tesseract_urdf::writeLink
tinyxml2::XMLElement * writeLink(const std::shared_ptr< const tesseract_scene_graph::Link > &link, tinyxml2::XMLDocument &doc, const std::string &package_path)
writeLink Write a link to URDF XML
Definition: link.cpp:111
tesseract_urdf::parseVisual
std::shared_ptr< tesseract_scene_graph::Visual > parseVisual(const tinyxml2::XMLElement *xml_element, const tesseract_common::ResourceLocator &locator, std::unordered_map< std::string, std::shared_ptr< tesseract_scene_graph::Material >> &available_materials)
Parse xml element visual.
tesseract_scene_graph::Collision::Ptr
std::shared_ptr< Collision > Ptr
tesseract_common::ResourceLocator
tesseract_common::QueryStringAttribute
int QueryStringAttribute(const tinyxml2::XMLElement *xml_element, const char *name, std::string &value)
TESSERACT_COMMON_IGNORE_WARNINGS_POP
tesseract_urdf::parseLink
std::shared_ptr< tesseract_scene_graph::Link > parseLink(const tinyxml2::XMLElement *xml_element, const tesseract_common::ResourceLocator &locator, bool make_convex_meshes, std::unordered_map< std::string, std::shared_ptr< tesseract_scene_graph::Material >> &available_materials)
Parse xml element link.
inertial.h
Parse inertial from xml string.
tesseract_urdf::parseInertial
std::shared_ptr< tesseract_scene_graph::Inertial > parseInertial(const tinyxml2::XMLElement *xml_element)
Parse xml element inertial.
Definition: inertial.cpp:41
tesseract_scene_graph::Visual::Ptr
std::shared_ptr< Visual > Ptr
tesseract_urdf::LINK_ELEMENT_NAME
static constexpr std::string_view LINK_ELEMENT_NAME
Definition: link.h:47
macros.h
tesseract_urdf
Definition: box.h:43
collision.h
Parse collision from xml string.
visual.h
Parse visual from xml string.


tesseract_urdf
Author(s): Levi Armstrong
autogenerated on Thu Apr 24 2025 03:10:44