visual.cpp
Go to the documentation of this file.
1 
29 #include <stdexcept>
30 
31 #include <Eigen/Geometry>
32 #include <tesseract_common/utils.h>
33 #include <tinyxml2.h>
35 
36 #include <tesseract_urdf/visual.h>
37 #include <tesseract_urdf/origin.h>
43 
44 namespace tesseract_urdf
45 {
47 parseVisual(const tinyxml2::XMLElement* xml_element,
48  const tesseract_common::ResourceLocator& locator,
49  std::unordered_map<std::string, tesseract_scene_graph::Material::Ptr>& available_materials)
50 {
51  // get name
52  std::string visual_name = tesseract_common::StringAttribute(xml_element, "name", "");
53 
54  // get origin
55  Eigen::Isometry3d visual_origin = Eigen::Isometry3d::Identity();
56  const tinyxml2::XMLElement* origin = xml_element->FirstChildElement("origin");
57  if (origin != nullptr)
58  {
59  try
60  {
61  visual_origin = parseOrigin(origin);
62  }
63  catch (...)
64  {
65  std::throw_with_nested(std::runtime_error("Visual: Error parsing 'origin' element!"));
66  }
67  }
68 
69  // get material
71  const tinyxml2::XMLElement* material = xml_element->FirstChildElement("material");
72  if (material != nullptr)
73  {
74  try
75  {
76  visual_material = parseMaterial(material, available_materials, true);
77  }
78  catch (...)
79  {
80  std::throw_with_nested(std::runtime_error("Visual: Error parsing 'material' element!"));
81  }
82  }
83 
84  // get geometry
85  const tinyxml2::XMLElement* geometry = xml_element->FirstChildElement("geometry");
86  if (geometry == nullptr)
87  std::throw_with_nested(std::runtime_error("Visual: Error missing 'geometry' element!"));
88 
90  try
91  {
92  // Set `make_convex_meshes` argument to false for visual geometry
93  // Note: mesh elements can still override
94  geom = parseGeometry(geometry, locator, true, false);
95  }
96  catch (...)
97  {
98  std::throw_with_nested(std::runtime_error("Visual: Error parsing 'geometry' element!"));
99  }
100 
101  auto visual = std::make_shared<tesseract_scene_graph::Visual>();
102  visual->name = visual_name;
103  visual->origin = visual_origin;
104  visual->geometry = geom;
105  visual->material = visual_material;
106 
107  return visual;
108 }
109 
110 tinyxml2::XMLElement* writeVisual(const std::shared_ptr<const tesseract_scene_graph::Visual>& visual,
111  tinyxml2::XMLDocument& doc,
112  const std::string& package_path,
113  const std::string& link_name,
114  const int id = -1)
115 {
116  if (visual == nullptr)
117  std::throw_with_nested(std::runtime_error("Visual is nullptr and cannot be converted to XML"));
118 
119  tinyxml2::XMLElement* xml_element = doc.NewElement(VISUAL_ELEMENT_NAME.data());
120 
121  if (!visual->name.empty())
122  xml_element->SetAttribute("name", visual->name.c_str());
123 
124  if (!visual->origin.matrix().isIdentity(std::numeric_limits<double>::epsilon()))
125  {
126  tinyxml2::XMLElement* xml_origin = writeOrigin(visual->origin, doc);
127  xml_element->InsertEndChild(xml_origin);
128  }
129 
130  if (visual->material != nullptr)
131  {
132  tinyxml2::XMLElement* xml_material = writeMaterial(visual->material, doc);
133  xml_element->InsertEndChild(xml_material);
134  }
135 
136  // Construct Filename, without extension (could be .ply or .bt)
137  std::string filename = link_name;
138  if (!visual->name.empty())
139  filename = filename + "_" + visual->name;
140  else
141  filename = filename + "_visual";
142 
143  // If a package path was specified, save in a visual sub-directory
144  if (!package_path.empty())
145  filename = "visual/" + filename;
146 
147  // If there is more than one visual object for this link, append the id
148  if (id >= 0)
149  filename = filename + "_" + std::to_string(id);
150 
151  try
152  {
153  std::string filename = "visual/" + link_name + "_visual";
154  if (id >= 0)
155  filename += "_" + std::to_string(id);
156  tinyxml2::XMLElement* xml_geometry = writeGeometry(visual->geometry, doc, package_path, filename);
157  xml_element->InsertEndChild(xml_geometry);
158  }
159  catch (...)
160  {
161  std::throw_with_nested(std::runtime_error("Could not write geometry for visual '" + visual->name + "'!"));
162  }
163 
164  return xml_element;
165 }
166 
167 } // namespace tesseract_urdf
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_geometry::Geometry::Ptr
std::shared_ptr< Geometry > Ptr
geometry.h
Parse geometry from xml string.
utils.h
resource_locator.h
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
tesseract_urdf::writeVisual
tinyxml2::XMLElement * writeVisual(const std::shared_ptr< const tesseract_scene_graph::Visual > &visual, tinyxml2::XMLDocument &doc, const std::string &package_path, const std::string &link_name, int id)
writeVisual Write one visual geometry object to URDF XML
Definition: visual.cpp:110
tesseract_urdf::parseVisual
std::shared_ptr< tesseract_scene_graph::Visual > parseVisual(const tinyxml2::XMLElement *xml_element, const tesseract_common::ResourceLocator &locator, std::unordered_map< std::string, std::shared_ptr< tesseract_scene_graph::Material >> &available_materials)
Parse xml element visual.
material.h
Parse material from xml string.
tesseract_urdf::VISUAL_ELEMENT_NAME
static constexpr std::string_view VISUAL_ELEMENT_NAME
Definition: visual.h:47
tesseract_common::ResourceLocator
TESSERACT_COMMON_IGNORE_WARNINGS_POP
tesseract_scene_graph::Material::getDefaultMaterial
static std::shared_ptr< Material > getDefaultMaterial()
geometry.h
tesseract_urdf::writeMaterial
tinyxml2::XMLElement * writeMaterial(const std::shared_ptr< const tesseract_scene_graph::Material > &material, tinyxml2::XMLDocument &doc)
Definition: material.cpp:124
tesseract_urdf::writeOrigin
tinyxml2::XMLElement * writeOrigin(const Eigen::Isometry3d &origin, tinyxml2::XMLDocument &doc)
Definition: origin.cpp:129
tesseract_scene_graph::Visual::Ptr
std::shared_ptr< Visual > 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_common::StringAttribute
std::string StringAttribute(const tinyxml2::XMLElement *xml_element, const char *name, std::string default_value)
tesseract_scene_graph::Material::Ptr
std::shared_ptr< Material > Ptr
macros.h
tesseract_urdf
Definition: box.h:43
tesseract_urdf::parseMaterial
std::shared_ptr< tesseract_scene_graph::Material > parseMaterial(const tinyxml2::XMLElement *xml_element, std::unordered_map< std::string, std::shared_ptr< tesseract_scene_graph::Material >> &available_materials, bool allow_anonymous)
Parse xml element material.
visual.h
Parse visual from xml string.


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