geometry.cpp
Go to the documentation of this file.
1 
29 #include <stdexcept>
30 
31 #include <Eigen/Geometry>
32 #include <tinyxml2.h>
34 
39 #include <tesseract_urdf/box.h>
41 #include <tesseract_urdf/cone.h>
42 #include <tesseract_urdf/capsule.h>
44 #include <tesseract_urdf/mesh.h>
45 #include <tesseract_urdf/octomap.h>
47 #include <tesseract_urdf/sphere.h>
48 
49 namespace tesseract_urdf
50 {
51 tesseract_geometry::Geometry::Ptr parseGeometry(const tinyxml2::XMLElement* xml_element,
52  const tesseract_common::ResourceLocator& locator,
53  bool visual,
54  bool make_convex_meshes)
55 {
56  const tinyxml2::XMLElement* geometry = xml_element->FirstChildElement();
57  if (geometry == nullptr)
58  std::throw_with_nested(std::runtime_error("Geometry: Error missing 'geometry' element!"));
59 
60  std::string geometry_type;
61  int status = tesseract_common::QueryStringValue(geometry, geometry_type);
62  if (status != tinyxml2::XML_SUCCESS)
63  std::throw_with_nested(std::runtime_error("Geometry: Error parsing 'geometry' element, invalid geometry type!"));
64 
65  // URDF-supported elements
66  if (geometry_type == SPHERE_ELEMENT_NAME)
67  {
69  try
70  {
71  sphere = parseSphere(geometry);
72  }
73  catch (...)
74  {
75  std::throw_with_nested(std::runtime_error("Geometry: Failed parsing geometry type 'sphere'!"));
76  }
77 
78  return sphere;
79  }
80 
81  if (geometry_type == BOX_ELEMENT_NAME)
82  {
84  try
85  {
86  box = parseBox(geometry);
87  }
88  catch (...)
89  {
90  std::throw_with_nested(std::runtime_error("Geometry: Failed parsing geometry type 'box'!"));
91  }
92 
93  return box;
94  }
95 
96  if (geometry_type == CYLINDER_ELEMENT_NAME)
97  {
99  try
100  {
101  cylinder = parseCylinder(geometry);
102  }
103  catch (...)
104  {
105  std::throw_with_nested(std::runtime_error("Geometry: Failed parsing geometry type 'cylinder'!"));
106  }
107 
108  return cylinder;
109  }
110 
111  if (geometry_type == MESH_ELEMENT_NAME)
112  {
113  std::vector<tesseract_geometry::PolygonMesh::Ptr> meshes;
114  try
115  {
116  meshes = parseMesh(geometry, locator, visual, make_convex_meshes);
117  }
118  catch (...)
119  {
120  std::throw_with_nested(std::runtime_error("Geometry: Failed parsing geometry type 'mesh'!"));
121  }
122 
123  if (meshes.size() > 1)
124  return std::make_shared<tesseract_geometry::CompoundMesh>(meshes);
125 
126  return meshes.front();
127  }
128 
129  // Custom Tesseract elements
130  if (geometry_type == CONE_ELEMENT_NAME)
131  {
133  try
134  {
135  cone = parseCone(geometry);
136  }
137  catch (...)
138  {
139  std::throw_with_nested(std::runtime_error("Geometry: Failed parsing geometry type 'cone'!"));
140  }
141 
142  return cone;
143  }
144 
145  if (geometry_type == CAPSULE_ELEMENT_NAME)
146  {
148  try
149  {
150  capsule = parseCapsule(geometry);
151  }
152  catch (...)
153  {
154  std::throw_with_nested(std::runtime_error("Geometry: Failed parsing geometry type 'capsule'!"));
155  }
156 
157  return capsule;
158  }
159 
160  if (geometry_type == OCTOMAP_ELEMENT_NAME)
161  {
163  try
164  {
165  octree = parseOctomap(geometry, locator, visual);
166  }
167  catch (...)
168  {
169  std::throw_with_nested(std::runtime_error("Geometry: Failed parsing geometry type 'octomap'!"));
170  }
171 
172  return octree;
173  }
174 
175  if (geometry_type == SDF_MESH_ELEMENT_NAME)
176  {
177  std::vector<tesseract_geometry::SDFMesh::Ptr> meshes;
178  try
179  {
180  meshes = parseSDFMesh(geometry, locator, visual);
181  }
182  catch (...)
183  {
184  std::throw_with_nested(std::runtime_error("Geometry: Failed parsing geometry type 'sdf_mesh'!"));
185  }
186 
187  if (meshes.size() > 1)
188  return std::make_shared<tesseract_geometry::CompoundMesh>(meshes);
189 
190  return meshes.front();
191  }
192 
193  std::throw_with_nested(std::runtime_error("Geometry: Invalid geometry type '" + geometry_type + "'!"));
194 }
195 
196 tinyxml2::XMLElement* writeGeometry(const std::shared_ptr<const tesseract_geometry::Geometry>& geometry,
197  tinyxml2::XMLDocument& doc,
198  const std::string& package_path,
199  const std::string& filename)
200 {
201  if (geometry == nullptr)
202  std::throw_with_nested(std::runtime_error("Geometry is nullptr and cannot be converted to XML"));
203  tinyxml2::XMLElement* xml_element = doc.NewElement(GEOMETRY_ELEMENT_NAME.data());
204 
205  tesseract_geometry::GeometryType type = geometry->getType();
206 
208  {
209  try
210  {
211  tinyxml2::XMLElement* xml_sphere =
212  writeSphere(std::static_pointer_cast<const tesseract_geometry::Sphere>(geometry), doc);
213  xml_element->InsertEndChild(xml_sphere);
214  }
215  catch (...)
216  {
217  std::throw_with_nested(std::runtime_error("Could not write geometry marked as sphere!"));
218  }
219  }
221  {
222  try
223  {
224  tinyxml2::XMLElement* xml_cylinder =
225  writeCylinder(std::static_pointer_cast<const tesseract_geometry::Cylinder>(geometry), doc);
226  xml_element->InsertEndChild(xml_cylinder);
227  }
228  catch (...)
229  {
230  std::throw_with_nested(std::runtime_error("Could not write geometry marked as cylinder!"));
231  }
232  }
234  {
235  try
236  {
237  tinyxml2::XMLElement* xml_capsule =
238  writeCapsule(std::static_pointer_cast<const tesseract_geometry::Capsule>(geometry), doc);
239  xml_element->InsertEndChild(xml_capsule);
240  }
241  catch (...)
242  {
243  std::throw_with_nested(std::runtime_error("Could not write geometry marked as capsule!"));
244  }
245  }
247  {
248  try
249  {
250  tinyxml2::XMLElement* xml_cone =
251  writeCone(std::static_pointer_cast<const tesseract_geometry::Cone>(geometry), doc);
252  xml_element->InsertEndChild(xml_cone);
253  }
254  catch (...)
255  {
256  std::throw_with_nested(std::runtime_error("Could not write geometry marked as cone!"));
257  }
258  }
260  {
261  try
262  {
263  tinyxml2::XMLElement* xml_box = writeBox(std::static_pointer_cast<const tesseract_geometry::Box>(geometry), doc);
264  xml_element->InsertEndChild(xml_box);
265  }
266  catch (...)
267  {
268  std::throw_with_nested(std::runtime_error("Could not write geometry marked as box!"));
269  }
270  }
272  {
273  std::throw_with_nested(std::runtime_error("Cannot write geometry of type PLANE to XML! Consider using box."));
274  }
276  {
277  try
278  {
279  tinyxml2::XMLElement* xml_mesh =
280  writeMesh(std::static_pointer_cast<const tesseract_geometry::PolygonMesh>(geometry),
281  doc,
282  package_path,
283  filename + ".ply");
284  xml_element->InsertEndChild(xml_mesh);
285  }
286  catch (...)
287  {
288  std::throw_with_nested(std::runtime_error("Could not write geometry marked as mesh!"));
289  }
290  }
292  {
293  try
294  {
295  tinyxml2::XMLElement* xml_sdf_mesh = writeSDFMesh(
296  std::static_pointer_cast<const tesseract_geometry::SDFMesh>(geometry), doc, package_path, filename + ".ply");
297  xml_element->InsertEndChild(xml_sdf_mesh);
298  }
299  catch (...)
300  {
301  std::throw_with_nested(std::runtime_error("Could not write geometry marked as SDF mesh!"));
302  }
303  }
305  {
306  try
307  {
308  tinyxml2::XMLElement* xml_octree = writeOctomap(
309  std::static_pointer_cast<const tesseract_geometry::Octree>(geometry), doc, package_path, filename + ".bt");
310  xml_element->InsertEndChild(xml_octree);
311  }
312  catch (...)
313  {
314  std::throw_with_nested(std::runtime_error("Could not write geometry marked as octree!"));
315  }
316  }
317  else
318  {
319  std::throw_with_nested(std::runtime_error("Unknown geometry type, cannot write to XML!"));
320  }
321 
322  return xml_element;
323 }
324 
325 } // namespace tesseract_urdf
tesseract_urdf::writeCone
tinyxml2::XMLElement * writeCone(const std::shared_ptr< const tesseract_geometry::Cone > &cone, tinyxml2::XMLDocument &doc)
Definition: cone.cpp:52
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_urdf::parseSphere
std::shared_ptr< tesseract_geometry::Sphere > parseSphere(const tinyxml2::XMLElement *xml_element)
Parse a xml sphere element.
Definition: sphere.cpp:40
tesseract_geometry::Geometry::Ptr
std::shared_ptr< Geometry > Ptr
tesseract_geometry::GeometryType::CONE
@ CONE
geometry.h
Parse geometry from xml string.
sdf_mesh.h
tesseract_urdf::SPHERE_ELEMENT_NAME
static constexpr std::string_view SPHERE_ELEMENT_NAME
Definition: sphere.h:45
sphere.h
Parse sphere from xml string.
tesseract_geometry::GeometryType
GeometryType
convex_hull_utils.h
tesseract_geometry::Cylinder::Ptr
std::shared_ptr< Cylinder > Ptr
resource_locator.h
tesseract_urdf::writeOctomap
tinyxml2::XMLElement * writeOctomap(const std::shared_ptr< const tesseract_geometry::Octree > &octree, tinyxml2::XMLDocument &doc, const std::string &package_path, const std::string &filename)
writeOctomap Write octomap to URDF XML. This is non-standard URDF / tesseract-exclusive
Definition: octomap.cpp:101
tesseract_urdf::MESH_ELEMENT_NAME
static constexpr std::string_view MESH_ELEMENT_NAME
Definition: mesh.h:47
tesseract_urdf::OCTOMAP_ELEMENT_NAME
static constexpr std::string_view OCTOMAP_ELEMENT_NAME
Definition: octomap.h:46
common.h
tesseract_common::QueryStringValue
int QueryStringValue(const tinyxml2::XMLAttribute *xml_attribute, std::string &value)
tesseract_urdf::GEOMETRY_ELEMENT_NAME
static constexpr std::string_view GEOMETRY_ELEMENT_NAME
Definition: geometry.h:46
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
tesseract_urdf::BOX_ELEMENT_NAME
static constexpr std::string_view BOX_ELEMENT_NAME
Definition: box.h:45
tesseract_geometry::GeometryType::MESH
@ MESH
tesseract_urdf::writeMesh
tinyxml2::XMLElement * writeMesh(const std::shared_ptr< const tesseract_geometry::PolygonMesh > &mesh, tinyxml2::XMLDocument &doc, const std::string &package_path, const std::string &filename)
writeMesh Write a mesh to URDF XML and PLY file
Definition: mesh.cpp:131
tesseract_geometry::GeometryType::CYLINDER
@ CYLINDER
geometries.h
box.h
Parse box from xml string.
cone.h
Parse cone from xml string.
tesseract_urdf::parseSDFMesh
std::vector< std::shared_ptr< tesseract_geometry::SDFMesh > > parseSDFMesh(const tinyxml2::XMLElement *xml_element, const tesseract_common::ResourceLocator &locator, bool visual)
Parse xml element sdf_mesh.
Definition: sdf_mesh.cpp:46
tesseract_geometry::GeometryType::PLANE
@ PLANE
tesseract_urdf::writeCylinder
tinyxml2::XMLElement * writeCylinder(const std::shared_ptr< const tesseract_geometry::Cylinder > &cylinder, tinyxml2::XMLDocument &doc)
Definition: cylinder.cpp:52
tesseract_geometry::GeometryType::SPHERE
@ SPHERE
tesseract_urdf::CYLINDER_ELEMENT_NAME
static constexpr std::string_view CYLINDER_ELEMENT_NAME
Definition: cylinder.h:45
tesseract_common::ResourceLocator
tesseract_urdf::parseOctomap
std::shared_ptr< tesseract_geometry::Octree > parseOctomap(const tinyxml2::XMLElement *xml_element, const tesseract_common::ResourceLocator &locator, bool visual)
Parse xml element octomap.
Definition: octomap.cpp:47
tesseract_geometry::GeometryType::CAPSULE
@ CAPSULE
mesh.h
Parse mesh from xml string.
octomap.h
Parse octomap from xml string.
TESSERACT_COMMON_IGNORE_WARNINGS_POP
tesseract_urdf::parseCylinder
std::shared_ptr< tesseract_geometry::Cylinder > parseCylinder(const tinyxml2::XMLElement *xml_element)
Parse a xml cylinder element.
Definition: cylinder.cpp:40
tesseract_urdf::parseBox
std::shared_ptr< tesseract_geometry::Box > parseBox(const tinyxml2::XMLElement *xml_element)
Parse a xml box element.
Definition: box.cpp:41
tesseract_geometry::Box::Ptr
std::shared_ptr< Box > Ptr
tesseract_urdf::CAPSULE_ELEMENT_NAME
static constexpr std::string_view CAPSULE_ELEMENT_NAME
Definition: capsule.h:45
tesseract_geometry::Octree::Ptr
std::shared_ptr< Octree > Ptr
tesseract_urdf::parseCapsule
std::shared_ptr< tesseract_geometry::Capsule > parseCapsule(const tinyxml2::XMLElement *xml_element)
Parse a xml capsule element.
Definition: capsule.cpp:40
tesseract_geometry::GeometryType::SDF_MESH
@ SDF_MESH
tesseract_urdf::parseCone
std::shared_ptr< tesseract_geometry::Cone > parseCone(const tinyxml2::XMLElement *xml_element)
Parse a xml cone element.
Definition: cone.cpp:40
tesseract_urdf::writeSphere
tinyxml2::XMLElement * writeSphere(const std::shared_ptr< const tesseract_geometry::Sphere > &sphere, tinyxml2::XMLDocument &doc)
Definition: sphere.cpp:49
tesseract_urdf::writeBox
tinyxml2::XMLElement * writeBox(const std::shared_ptr< const tesseract_geometry::Box > &box, tinyxml2::XMLDocument &doc)
Definition: box.cpp:70
capsule.h
Parse capsule from xml string.
tesseract_geometry::Cone::Ptr
std::shared_ptr< Cone > Ptr
type
type
tesseract_geometry::GeometryType::BOX
@ BOX
tesseract_geometry::Capsule::Ptr
std::shared_ptr< Capsule > Ptr
tesseract_geometry::Sphere::Ptr
std::shared_ptr< Sphere > Ptr
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_geometry::GeometryType::OCTREE
@ OCTREE
macros.h
tesseract_urdf
Definition: box.h:43
cylinder.h
Parse cylinder from xml string.
tesseract_urdf::writeCapsule
tinyxml2::XMLElement * writeCapsule(const std::shared_ptr< const tesseract_geometry::Capsule > &capsule, tinyxml2::XMLDocument &doc)
Definition: capsule.cpp:52
tesseract_urdf::CONE_ELEMENT_NAME
static constexpr std::string_view CONE_ELEMENT_NAME
Definition: cone.h:45
tesseract_urdf::SDF_MESH_ELEMENT_NAME
static constexpr std::string_view SDF_MESH_ELEMENT_NAME
Definition: sdf_mesh.h:46
tesseract_urdf::writeSDFMesh
tinyxml2::XMLElement * writeSDFMesh(const std::shared_ptr< const tesseract_geometry::SDFMesh > &sdf_mesh, tinyxml2::XMLDocument &doc, const std::string &package_path, const std::string &filename)
writeSDFMesh Write SDF Mesh to URDF XML. This is non-standard URDF / tesseract-exclusive
Definition: sdf_mesh.cpp:96
tesseract_urdf::parseMesh
std::vector< std::shared_ptr< tesseract_geometry::PolygonMesh > > parseMesh(const tinyxml2::XMLElement *xml_element, const tesseract_common::ResourceLocator &locator, bool visual, bool make_convex)
Parse xml element mesh.
Definition: mesh.cpp:47


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