box.cpp
Go to the documentation of this file.
1 
29 #include <stdexcept>
30 
31 #include <boost/algorithm/string.hpp>
32 #include <tesseract_common/utils.h>
33 #include <tinyxml2.h>
35 
37 #include <tesseract_urdf/box.h>
38 
39 namespace tesseract_urdf
40 {
41 tesseract_geometry::Box::Ptr parseBox(const tinyxml2::XMLElement* xml_element)
42 {
43  std::string size_string;
44  if (tesseract_common::QueryStringAttribute(xml_element, "size", size_string) != tinyxml2::XML_SUCCESS)
45  std::throw_with_nested(std::runtime_error("Box: Missing or failed parsing box attribute size!"));
46 
47  std::vector<std::string> tokens;
48  boost::split(tokens, size_string, boost::is_any_of(" "), boost::token_compress_on);
49  if (tokens.size() != 3 || !tesseract_common::isNumeric(tokens))
50  std::throw_with_nested(std::runtime_error("Box: Failed converting box attribute size to vector!"));
51 
52  double l{ 0 }, w{ 0 }, h{ 0 };
53  // No need to check return values because the tokens are verified above
57 
58  if (!(l > 0))
59  std::throw_with_nested(std::runtime_error("Box: The length must be greater than zero!"));
60 
61  if (!(w > 0))
62  std::throw_with_nested(std::runtime_error("Box: The width must be greater than zero!"));
63 
64  if (!(h > 0))
65  std::throw_with_nested(std::runtime_error("Box: The height must be greater than zero!"));
66 
67  return std::make_shared<tesseract_geometry::Box>(l, w, h);
68 }
69 
70 tinyxml2::XMLElement* writeBox(const std::shared_ptr<const tesseract_geometry::Box>& box, tinyxml2::XMLDocument& doc)
71 {
72  if (box == nullptr)
73  std::throw_with_nested(std::runtime_error("Box is nullptr and cannot be converted to XML"));
74  tinyxml2::XMLElement* xml_element = doc.NewElement(BOX_ELEMENT_NAME.data());
75  Eigen::IOFormat eigen_format(Eigen::StreamPrecision, Eigen::DontAlignCols, " ", " ");
76  std::stringstream size_string;
77  size_string << Eigen::Vector3d(box->getX(), box->getY(), box->getZ()).format(eigen_format);
78  xml_element->SetAttribute("size", size_string.str().c_str());
79  return xml_element;
80 }
81 
82 } // namespace tesseract_urdf
utils.h
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
box.h
Parse box 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_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::writeBox
tinyxml2::XMLElement * writeBox(const std::shared_ptr< const tesseract_geometry::Box > &box, tinyxml2::XMLDocument &doc)
Definition: box.cpp:70
tesseract_common::isNumeric
bool isNumeric(const std::string &s)
tesseract_common::toNumeric< double >
template bool toNumeric< double >(const std::string &, double &)
macros.h
tesseract_urdf
Definition: box.h:43
box.h


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