inertial.cpp
Go to the documentation of this file.
1 
29 #include <stdexcept>
30 
31 #include <tinyxml2.h>
33 
36 #include <tesseract_urdf/origin.h>
37 #include <tesseract_urdf/utils.h>
38 
39 namespace tesseract_urdf
40 {
41 tesseract_scene_graph::Inertial::Ptr parseInertial(const tinyxml2::XMLElement* xml_element)
42 {
43  auto inertial = std::make_shared<tesseract_scene_graph::Inertial>();
44  const tinyxml2::XMLElement* origin = xml_element->FirstChildElement("origin");
45  if (origin != nullptr)
46  {
47  try
48  {
49  inertial->origin = parseOrigin(origin);
50  }
51  catch (...)
52  {
53  std::throw_with_nested(std::runtime_error("Inertial: Failed parsing element 'origin'!"));
54  }
55  }
56 
57  const tinyxml2::XMLElement* mass = xml_element->FirstChildElement("mass");
58  if (mass == nullptr)
59  std::throw_with_nested(std::runtime_error("Inertial: Missing element 'mass'!"));
60 
61  if (mass->QueryDoubleAttribute("value", &(inertial->mass)) != tinyxml2::XML_SUCCESS)
62  std::throw_with_nested(std::runtime_error("Inertial: Missing or failed parsing 'mass' attribute 'value'!"));
63 
64  const tinyxml2::XMLElement* inertia = xml_element->FirstChildElement("inertia");
65  if (inertia == nullptr)
66  std::throw_with_nested(std::runtime_error("Inertial: Missing element 'inertia'!"));
67 
68  if (inertia->QueryDoubleAttribute("ixx", &(inertial->ixx)) != tinyxml2::XML_SUCCESS)
69  std::throw_with_nested(std::runtime_error("Inertial: Missing or failed parsing attribute 'ixx'!"));
70 
71  if (inertia->QueryDoubleAttribute("ixy", &(inertial->ixy)) != tinyxml2::XML_SUCCESS)
72  std::throw_with_nested(std::runtime_error("Inertial: Missing or failed parsing attribute 'ixy'!"));
73 
74  if (inertia->QueryDoubleAttribute("ixz", &(inertial->ixz)) != tinyxml2::XML_SUCCESS)
75  std::throw_with_nested(std::runtime_error("Inertial: Missing or failed parsing attribute 'ixz'!"));
76 
77  if (inertia->QueryDoubleAttribute("iyy", &(inertial->iyy)) != tinyxml2::XML_SUCCESS)
78  std::throw_with_nested(std::runtime_error("Inertial: Missing or failed parsing attribute 'iyy'!"));
79 
80  if (inertia->QueryDoubleAttribute("iyz", &(inertial->iyz)) != tinyxml2::XML_SUCCESS)
81  std::throw_with_nested(std::runtime_error("Inertial: Missing or failed parsing attribute 'iyz'!"));
82 
83  if (inertia->QueryDoubleAttribute("izz", &(inertial->izz)) != tinyxml2::XML_SUCCESS)
84  std::throw_with_nested(std::runtime_error("Inertial: Missing or failed parsing attribute 'izz'!"));
85 
86  return inertial;
87 }
88 
89 tinyxml2::XMLElement* writeInertial(const std::shared_ptr<const tesseract_scene_graph::Inertial>& inertial,
90  tinyxml2::XMLDocument& doc)
91 {
92  if (inertial == nullptr)
93  std::throw_with_nested(std::runtime_error("Inertial is nullptr and cannot be converted to XML"));
94  tinyxml2::XMLElement* xml_element = doc.NewElement(INERTIAL_ELEMENT_NAME.data());
95 
96  if (!inertial->origin.matrix().isIdentity(std::numeric_limits<double>::epsilon()))
97  {
98  tinyxml2::XMLElement* xml_origin = writeOrigin(inertial->origin, doc);
99  xml_element->InsertEndChild(xml_origin);
100  }
101 
102  tinyxml2::XMLElement* xml_mass = doc.NewElement("mass");
103  xml_mass->SetAttribute("value", toString(inertial->mass).c_str());
104 
105  tinyxml2::XMLElement* xml_inertia = doc.NewElement("inertia");
106  xml_inertia->SetAttribute("ixx", toString(inertial->ixx).c_str());
107  xml_inertia->SetAttribute("ixy", toString(inertial->ixy).c_str());
108  xml_inertia->SetAttribute("ixz", toString(inertial->ixz).c_str());
109  xml_inertia->SetAttribute("iyy", toString(inertial->iyy).c_str());
110  xml_inertia->SetAttribute("iyz", toString(inertial->iyz).c_str());
111  xml_inertia->SetAttribute("izz", toString(inertial->izz).c_str());
112 
113  xml_element->InsertEndChild(xml_mass);
114  xml_element->InsertEndChild(xml_inertia);
115  return xml_element;
116 }
117 
118 } // namespace tesseract_urdf
tesseract_urdf::INERTIAL_ELEMENT_NAME
static constexpr std::string_view INERTIAL_ELEMENT_NAME
Definition: inertial.h:45
tesseract_urdf::writeInertial
tinyxml2::XMLElement * writeInertial(const std::shared_ptr< const tesseract_scene_graph::Inertial > &inertial, tinyxml2::XMLDocument &doc)
Definition: inertial.cpp:89
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
utils.h
TESSERACT_COMMON_IGNORE_WARNINGS_POP
tesseract_urdf::toString
std::string toString(const double &float_value, int precision=3)
Definition: utils.cpp:12
tesseract_scene_graph::Inertial::Ptr
std::shared_ptr< Inertial > Ptr
inertial.h
Parse inertial from xml string.
tesseract_urdf::parseInertial
std::shared_ptr< tesseract_scene_graph::Inertial > parseInertial(const tinyxml2::XMLElement *xml_element)
Parse xml element inertial.
Definition: inertial.cpp:41
tesseract_urdf::writeOrigin
tinyxml2::XMLElement * writeOrigin(const Eigen::Isometry3d &origin, tinyxml2::XMLDocument &doc)
Definition: origin.cpp:129
macros.h
tesseract_urdf
Definition: box.h:43


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