octomap.cpp
Go to the documentation of this file.
1 
29 #include <stdexcept>
30 
31 #include <tesseract_common/utils.h>
32 #include <tinyxml2.h>
34 
36 #include <tesseract_urdf/octomap.h>
37 #include <tesseract_urdf/octree.h>
39 #include <tesseract_urdf/utils.h>
40 
41 #ifdef TESSERACT_PARSE_POINT_CLOUDS
43 #endif
44 
45 namespace tesseract_urdf
46 {
47 tesseract_geometry::Octree::Ptr parseOctomap(const tinyxml2::XMLElement* xml_element,
48  const tesseract_common::ResourceLocator& locator,
49  const bool /*visual*/)
50 {
51  std::string shape_type;
52  if (tesseract_common::QueryStringAttribute(xml_element, "shape_type", shape_type) != tinyxml2::XML_SUCCESS)
53  std::throw_with_nested(std::runtime_error("Octomap: Missing or failed parsing attribute 'shape_type'!"));
54 
56  if (shape_type == "box")
58  else if (shape_type == "sphere_inside")
60  else if (shape_type == "sphere_outside")
62  else
63  std::throw_with_nested(std::runtime_error("Octomap: Invalid sub shape type, must be 'box', 'sphere_inside', or "
64  "'sphere_outside'!"));
65 
66  bool prune = false;
67  xml_element->QueryBoolAttribute("prune", &prune);
68 
69  const tinyxml2::XMLElement* octree_element = xml_element->FirstChildElement(OCTREE_ELEMENT_NAME.data());
70  if (octree_element != nullptr)
71  {
72  try
73  {
74  return parseOctree(octree_element, locator, sub_type, prune);
75  }
76  catch (...)
77  {
78  std::throw_with_nested(std::runtime_error("Octomap:"));
79  }
80  }
81 
82 #ifdef TESSERACT_PARSE_POINT_CLOUDS
83  const tinyxml2::XMLElement* pcd_element = xml_element->FirstChildElement(POINT_CLOUD_ELEMENT_NAME.data());
84  if (pcd_element != nullptr)
85  {
86  try
87  {
88  return parsePointCloud(pcd_element, locator, sub_type, prune);
89  }
90  catch (...)
91  {
92  std::throw_with_nested(std::runtime_error("Octomap:"));
93  }
94  }
95 #endif
96 
97  std::throw_with_nested(std::runtime_error("Octomap: Missing element '" + std::string(OCTREE_ELEMENT_NAME) + "' or '" +
98  std::string(POINT_CLOUD_ELEMENT_NAME) + "'; must define one!"));
99 }
100 
101 tinyxml2::XMLElement* writeOctomap(const std::shared_ptr<const tesseract_geometry::Octree>& octree,
102  tinyxml2::XMLDocument& doc,
103  const std::string& package_path,
104  const std::string& filename)
105 {
106  if (octree == nullptr)
107  std::throw_with_nested(std::runtime_error("Octree is nullptr and cannot be converted to XML"));
108  tinyxml2::XMLElement* xml_element = doc.NewElement(OCTOMAP_ELEMENT_NAME.data());
109 
110  std::string type_string;
111  if (octree->getSubType() == tesseract_geometry::OctreeSubType::BOX)
112  type_string = "box";
113  else if (octree->getSubType() == tesseract_geometry::OctreeSubType::SPHERE_INSIDE)
114  type_string = "sphere_inside";
115  else if (octree->getSubType() == tesseract_geometry::OctreeSubType::SPHERE_OUTSIDE)
116  type_string = "sphere_outside";
117  else
118  std::throw_with_nested(std::runtime_error("Octree subtype is invalid and cannot be converted to XML"));
119  xml_element->SetAttribute("shape_type", type_string.c_str());
120 
121  xml_element->SetAttribute("prune", octree->getPruned());
122 
123  try
124  {
125  tinyxml2::XMLElement* xml_octree = writeOctree(octree, doc, package_path, filename);
126  xml_element->InsertEndChild(xml_octree);
127  }
128  catch (...)
129  {
130  std::throw_with_nested(std::runtime_error("Octomap: Could not write octree to file"));
131  }
132 
133  return xml_element;
134 }
135 
136 } // namespace tesseract_urdf
utils.h
resource_locator.h
tesseract_urdf::parseOctree
std::shared_ptr< tesseract_geometry::Octree > parseOctree(const tinyxml2::XMLElement *xml_element, const tesseract_common::ResourceLocator &locator, tesseract_geometry::OctreeSubType shape_type, bool prune)
Parse xml element octree.
Definition: octree.cpp:45
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::OCTREE_ELEMENT_NAME
static constexpr std::string_view OCTREE_ELEMENT_NAME
Definition: octree.h:46
tesseract_geometry::OctreeSubType::SPHERE_OUTSIDE
@ SPHERE_OUTSIDE
tesseract_urdf::OCTOMAP_ELEMENT_NAME
static constexpr std::string_view OCTOMAP_ELEMENT_NAME
Definition: octomap.h:46
tesseract_urdf::writeOctree
tinyxml2::XMLElement * writeOctree(const std::shared_ptr< const tesseract_geometry::Octree > &octree, tinyxml2::XMLDocument &doc, const std::string &package_path, const std::string &filename)
writeOctree Write octree out to file, and generate appropriate xml
point_cloud.h
Parse PCL point cloud to octree from xml string.
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
utils.h
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_urdf::parsePointCloud
std::shared_ptr< tesseract_geometry::Octree > parsePointCloud(const tinyxml2::XMLElement *xml_element, const tesseract_common::ResourceLocator &locator, tesseract_geometry::OctreeSubType shape_type, bool prune)
Parse xml element point_cloud.
Definition: point_cloud.cpp:45
octomap.h
Parse octomap from xml string.
tesseract_common::QueryStringAttribute
int QueryStringAttribute(const tinyxml2::XMLElement *xml_element, const char *name, std::string &value)
TESSERACT_COMMON_IGNORE_WARNINGS_POP
tesseract_geometry::Octree::Ptr
std::shared_ptr< Octree > Ptr
octree.h
tesseract_urdf::POINT_CLOUD_ELEMENT_NAME
static constexpr std::string_view POINT_CLOUD_ELEMENT_NAME
Definition: point_cloud.h:46
macros.h
octree.h
Parse octree from xml string.
tesseract_urdf
Definition: box.h:43
tesseract_geometry::OctreeSubType
OctreeSubType
tesseract_geometry::OctreeSubType::BOX
@ BOX
tesseract_geometry::OctreeSubType::SPHERE_INSIDE
@ SPHERE_INSIDE


tesseract_urdf
Author(s): Levi Armstrong
autogenerated on Mon Apr 21 2025 03:01:38