octree.cpp
Go to the documentation of this file.
1 
29 #include <stdexcept>
30 
31 #include <boost/iostreams/device/array.hpp>
32 #include <boost/iostreams/stream.hpp>
33 #include <tesseract_common/utils.h>
34 #include <tinyxml2.h>
35 #include <octomap/OcTree.h>
37 
40 #include <tesseract_urdf/octree.h>
41 #include <tesseract_urdf/utils.h>
42 
43 namespace tesseract_urdf
44 {
45 tesseract_geometry::Octree::Ptr parseOctree(const tinyxml2::XMLElement* xml_element,
46  const tesseract_common::ResourceLocator& locator,
48  bool prune)
49 {
50  std::string filename;
51  if (tesseract_common::QueryStringAttribute(xml_element, "filename", filename) != tinyxml2::XML_SUCCESS)
52  std::throw_with_nested(std::runtime_error("Octree: Missing or failed parsing attribute 'filename'!"));
53 
54  tesseract_common::Resource::Ptr resource = locator.locateResource(filename);
55  if (!resource || !resource->isFile())
56  std::throw_with_nested(std::runtime_error("Octree: Missing resource '" + filename + "'!"));
57 
58  auto ot = std::make_shared<octomap::OcTree>(resource->getFilePath());
59 
60  if (ot == nullptr || ot->size() == 0)
61  std::throw_with_nested(std::runtime_error("Octree: Error importing from '" + filename + "'!"));
62 
63  if (prune)
65 
66  auto geom = std::make_shared<tesseract_geometry::Octree>(ot, shape_type);
67  if (geom == nullptr)
68  std::throw_with_nested(std::runtime_error("Octree: Error creating octree geometry type from octomap::octree!"));
69 
70  return geom;
71 }
72 
73 tinyxml2::XMLElement* writeOctree(const tesseract_geometry::Octree::ConstPtr& octree,
74  tinyxml2::XMLDocument& doc,
75  const std::string& package_path,
76  const std::string& filename)
77 {
78  if (octree == nullptr)
79  std::throw_with_nested(std::runtime_error("Octree is nullptr and cannot be converted to XML"));
80  tinyxml2::XMLElement* xml_element = doc.NewElement(OCTREE_ELEMENT_NAME.data());
81 
82  std::string filepath = trailingSlash(package_path) + noLeadingSlash(filename);
83 
84  // This copy is unfortunate, but avoiding the copy requires flowing mutability up to a lot of
85  // functions and their arguments. Don't know why writeBinary is non-const anyway, but we'll live.
86  std::shared_ptr<octomap::OcTree> underlying_tree = std::make_shared<octomap::OcTree>(*(octree->getOctree()));
87 
88  if (!underlying_tree->writeBinary(filepath))
89  std::throw_with_nested(std::runtime_error("Could not write octree to file `" + filepath + "`!"));
90 
91  xml_element->SetAttribute("filename", makeURDFFilePath(package_path, filename).c_str());
92 
93  return xml_element;
94 }
95 
96 } // namespace tesseract_urdf
tesseract_urdf::makeURDFFilePath
std::string makeURDFFilePath(const std::string &package_path, const std::string &filename)
Definition: utils.cpp:55
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::OCTREE_ELEMENT_NAME
static constexpr std::string_view OCTREE_ELEMENT_NAME
Definition: octree.h:46
tesseract_urdf::trailingSlash
std::string trailingSlash(const std::string &path)
Definition: utils.cpp:20
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
tesseract_common::ResourceLocator::locateResource
virtual std::shared_ptr< Resource > locateResource(const std::string &url) const=0
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
OcTree.h
utils.h
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_geometry::Octree::Ptr
std::shared_ptr< Octree > Ptr
tesseract_geometry::Octree::ConstPtr
std::shared_ptr< const Octree > ConstPtr
octree.h
tesseract_common::Resource::Ptr
std::shared_ptr< Resource > Ptr
tesseract_geometry::Octree::prune
static void prune(octomap::OcTree &octree)
macros.h
octree.h
Parse octree from xml string.
tesseract_urdf
Definition: box.h:43
tesseract_urdf::noLeadingSlash
std::string noLeadingSlash(const std::string &filename)
Definition: utils.cpp:45
tesseract_geometry::OctreeSubType
OctreeSubType


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