collision.cpp
Go to the documentation of this file.
1 
29 #include <stdexcept>
30 
31 #include <Eigen/Geometry>
32 #include <tesseract_common/utils.h>
33 #include <tinyxml2.h>
35 
41 #include <tesseract_urdf/origin.h>
42 
43 namespace tesseract_urdf
44 {
45 tesseract_scene_graph::Collision::Ptr parseCollision(const tinyxml2::XMLElement* xml_element,
46  const tesseract_common::ResourceLocator& locator,
47  bool make_convex_meshes)
48 {
49  // get name
50  std::string collision_name = tesseract_common::StringAttribute(xml_element, "name", "");
51 
52  // get origin
53  Eigen::Isometry3d collision_origin = Eigen::Isometry3d::Identity();
54  const tinyxml2::XMLElement* origin = xml_element->FirstChildElement("origin");
55  if (origin != nullptr)
56  {
57  try
58  {
59  collision_origin = parseOrigin(origin);
60  }
61  catch (...)
62  {
63  std::throw_with_nested(std::runtime_error("Collision: Error parsing 'origin' element!"));
64  }
65  }
66 
67  // get geometry
68  const tinyxml2::XMLElement* geometry = xml_element->FirstChildElement("geometry");
69  if (geometry == nullptr)
70  std::throw_with_nested(std::runtime_error("Collision: Error missing 'geometry' element!"));
71 
73  try
74  {
75  geom = parseGeometry(geometry, locator, false, make_convex_meshes);
76  }
77  catch (...)
78  {
79  std::throw_with_nested(std::runtime_error("Collision: Error parsing 'geometry' element!"));
80  }
81 
82  auto collision = std::make_shared<tesseract_scene_graph::Collision>();
83  collision->name = collision_name;
84  collision->origin = collision_origin;
85  collision->geometry = geom;
86 
87  return collision;
88 }
89 
90 tinyxml2::XMLElement* writeCollision(const std::shared_ptr<const tesseract_scene_graph::Collision>& collision,
91  tinyxml2::XMLDocument& doc,
92  const std::string& package_path,
93  const std::string& link_name,
94  const int id = -1)
95 {
96  if (collision == nullptr)
97  std::throw_with_nested(std::runtime_error("Collision is nullptr and cannot be converted to XML"));
98 
99  tinyxml2::XMLElement* xml_element = doc.NewElement(COLLISION_ELEMENT_NAME.data());
100 
101  if (!collision->name.empty())
102  xml_element->SetAttribute("name", collision->name.c_str());
103 
104  if (!collision->origin.matrix().isIdentity(std::numeric_limits<double>::epsilon()))
105  {
106  tinyxml2::XMLElement* xml_origin = writeOrigin(collision->origin, doc);
107  xml_element->InsertEndChild(xml_origin);
108  }
109 
110  // Construct filename, without extension (could be .ply or .bt)
111  std::string filename = link_name;
112  if (!collision->name.empty())
113  filename = filename + "_" + collision->name;
114  else
115  filename = filename + "_collision";
116 
117  // If a package path was specified, save in a collision sub-directory
118  if (!package_path.empty())
119  filename = "collision/" + filename;
120 
121  // If there is more than one collision object for this link, append the id
122  if (id >= 0)
123  filename = filename + "_" + std::to_string(id);
124 
125  try
126  {
127  tinyxml2::XMLElement* xml_geometry = writeGeometry(collision->geometry, doc, package_path, filename);
128  xml_element->InsertEndChild(xml_geometry);
129  }
130  catch (...)
131  {
132  std::throw_with_nested(std::runtime_error("Could not write geometry for collision '" + collision->name + "'!"));
133  }
134 
135  return xml_element;
136 }
137 
138 } // namespace tesseract_urdf
tesseract_urdf::parseGeometry
std::shared_ptr< tesseract_geometry::Geometry > parseGeometry(const tinyxml2::XMLElement *xml_element, const tesseract_common::ResourceLocator &locator, bool visual, bool make_convex_meshes)
Parse xml element geometry.
Definition: geometry.cpp:51
tesseract_geometry::Geometry::Ptr
std::shared_ptr< Geometry > Ptr
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
geometry.h
Parse geometry from xml string.
utils.h
resource_locator.h
tesseract_urdf::parseOrigin
Eigen::Isometry3d parseOrigin(const tinyxml2::XMLElement *xml_element)
Parse xml element origin.
Definition: origin.cpp:42
origin.h
Parse origin from xml string.
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
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
tesseract_scene_graph::Collision::Ptr
std::shared_ptr< Collision > Ptr
tesseract_common::ResourceLocator
TESSERACT_COMMON_IGNORE_WARNINGS_POP
geometry.h
tesseract_urdf::writeOrigin
tinyxml2::XMLElement * writeOrigin(const Eigen::Isometry3d &origin, tinyxml2::XMLDocument &doc)
Definition: origin.cpp:129
tesseract_urdf::writeGeometry
tinyxml2::XMLElement * writeGeometry(const std::shared_ptr< const tesseract_geometry::Geometry > &geometry, tinyxml2::XMLDocument &doc, const std::string &package_path, const std::string &filename)
writeGeometry Write geometry to URDF XML
Definition: geometry.cpp:196
tesseract_urdf::COLLISION_ELEMENT_NAME
static constexpr std::string_view COLLISION_ELEMENT_NAME
Definition: collision.h:46
tesseract_common::StringAttribute
std::string StringAttribute(const tinyxml2::XMLElement *xml_element, const char *name, std::string default_value)
macros.h
tesseract_urdf
Definition: box.h:43
collision.h
Parse collision from xml string.


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