Tesseract
Motion Planning Environment
Loading...
Searching...
No Matches
tesseract_urdf_common_unit.h
Go to the documentation of this file.
1#ifndef TESSERACT_URDF_COMMON_UNIT_H
2#define TESSERACT_URDF_COMMON_UNIT_H
3
6#include <fstream>
7#include <iostream>
8#include <vector>
9
10#include <gtest/gtest.h>
11#include <console_bridge/console.h>
12#include <tinyxml2.h>
14
19
20template <typename ElementType>
21bool runTest(ElementType& type,
22 std::function<ElementType(const tinyxml2::XMLElement*, const int)> func,
23 const std::string& xml_string,
24 const std::string& element_name,
25 int version)
26{
27 tinyxml2::XMLDocument xml_doc;
28 EXPECT_TRUE(xml_doc.Parse(xml_string.c_str()) == tinyxml2::XML_SUCCESS);
29
30 tinyxml2::XMLElement* element = xml_doc.FirstChildElement(element_name.c_str());
31 EXPECT_TRUE(element != nullptr);
32
33 try
34 {
35 type = func(element, version);
36 }
37 catch (const std::exception& e)
38 {
40 return false;
41 }
42
43 return true;
44}
45
46template <typename ElementType>
48 ElementType& type,
49 std::function<ElementType(const tinyxml2::XMLElement*, const tesseract_common::ResourceLocator&, bool, const int)>
50 func,
51 const std::string& xml_string,
52 const std::string& element_name,
54 int version,
55 bool visual)
56{
57 tinyxml2::XMLDocument xml_doc;
58 EXPECT_TRUE(xml_doc.Parse(xml_string.c_str()) == tinyxml2::XML_SUCCESS);
59
60 tinyxml2::XMLElement* element = xml_doc.FirstChildElement(element_name.c_str());
61 EXPECT_TRUE(element != nullptr);
62
63 try
64 {
65 type = func(element, locator, visual, version);
66 }
67 catch (const std::exception& e)
68 {
70 return false;
71 }
72
73 return true;
74}
75
76template <typename ElementType>
78 ElementType& type,
79 std::function<ElementType(const tinyxml2::XMLElement*, const tesseract_common::ResourceLocator&, const int)> func,
80 const std::string& xml_string,
81 const std::string& element_name,
83 int version)
84{
85 tinyxml2::XMLDocument xml_doc;
86 EXPECT_TRUE(xml_doc.Parse(xml_string.c_str()) == tinyxml2::XML_SUCCESS);
87
88 tinyxml2::XMLElement* element = xml_doc.FirstChildElement(element_name.c_str());
89 EXPECT_TRUE(element != nullptr);
90
91 try
92 {
93 type = func(element, locator, version);
94 }
95 catch (const std::exception& e)
96 {
98 return false;
99 }
100
101 return true;
102}
103
104template <typename ElementType>
105bool runTest(ElementType& type,
106 std::function<ElementType(const tinyxml2::XMLElement*,
108 std::unordered_map<std::string, tesseract_scene_graph::Material::Ptr>&,
109 const int)> func,
110 const std::string& xml_string,
111 const std::string& element_name,
113 std::unordered_map<std::string, tesseract_scene_graph::Material::Ptr>& available_materials,
114 int version)
115{
116 tinyxml2::XMLDocument xml_doc;
117 EXPECT_TRUE(xml_doc.Parse(xml_string.c_str()) == tinyxml2::XML_SUCCESS);
118
119 tinyxml2::XMLElement* element = xml_doc.FirstChildElement(element_name.c_str());
120 EXPECT_TRUE(element != nullptr);
121
122 try
123 {
124 type = func(element, locator, available_materials, version);
125 }
126 catch (const std::exception& e)
127 {
129 return false;
130 }
131
132 return true;
133}
134
135template <typename ElementType>
136bool runTest(ElementType& type,
137 std::function<ElementType(const tinyxml2::XMLElement*,
138 std::unordered_map<std::string, tesseract_scene_graph::Material::Ptr>&,
139 bool,
140 const int)> func,
141 const std::string& xml_string,
142 const std::string& element_name,
143 std::unordered_map<std::string, tesseract_scene_graph::Material::Ptr>& available_materials,
144 int version,
145 const bool visual)
146{
147 tinyxml2::XMLDocument xml_doc;
148 EXPECT_TRUE(xml_doc.Parse(xml_string.c_str()) == tinyxml2::XML_SUCCESS);
149
150 tinyxml2::XMLElement* element = xml_doc.FirstChildElement(element_name.c_str());
151 EXPECT_TRUE(element != nullptr);
152
153 try
154 {
155 type = func(element, available_materials, visual, version);
156 }
157 catch (const std::exception& e)
158 {
160 return false;
161 }
162
163 return true;
164}
165
171inline std::string stripNewline(const std::string& in)
172{
173 std::string out;
174 if (!in.empty() && in.back() == '\n')
175 out = in.substr(0, in.size() - 1);
176 else
177 out = in;
178 return out;
179}
180
181inline std::string toString(const tinyxml2::XMLElement* element)
182{
183 std::string ret;
184 if (element != nullptr)
185 {
186 tinyxml2::XMLPrinter printer;
187 element->Accept(&printer);
188 std::stringstream ss;
189 ss << printer.CStr();
190 ret = stripNewline(ss.str());
191 }
192 return ret;
193}
194
202template <typename TessType>
203int writeTest(TessType& type,
204 std::function<tinyxml2::XMLElement*(const TessType&, tinyxml2::XMLDocument&)> func,
205 std::string& text)
206{
207 tinyxml2::XMLDocument doc;
208 int status = 0;
209 try
210 {
211 tinyxml2::XMLElement* element = func(type, doc);
212 text = toString(element);
213 if (element != nullptr)
214 status = 0;
215 else
216 status = 2;
217 }
218 catch (...)
219 {
220 text = "";
221 status = 1;
222 }
223 return status;
224}
225
234template <typename TessType>
235int writeTest(TessType& type,
236 std::function<tinyxml2::XMLElement*(const TessType&, tinyxml2::XMLDocument&, const std::string&)> func,
237 std::string& text,
238 const std::string& directory)
239{
240 tinyxml2::XMLDocument doc;
241 int status = 0;
242 try
243 {
244 tinyxml2::XMLElement* element = func(type, doc, directory);
245 text = toString(element);
246 if (element != nullptr)
247 status = 0;
248 else
249 status = 2;
250 }
251 catch (...)
252 {
253 text = "";
254 status = 1;
255 }
256 return status;
257}
258
268template <typename TessType>
270 TessType& type,
271 std::function<
272 tinyxml2::XMLElement*(const TessType&, tinyxml2::XMLDocument&, const std::string&, const std::string&)> func,
273 std::string& text,
274 const std::string& directory,
275 const std::string& filename)
276{
277 tinyxml2::XMLDocument doc;
278 int status = 0;
279 try
280 {
281 tinyxml2::XMLElement* element = func(type, doc, directory, filename);
282 text = toString(element);
283 if (element != nullptr)
284 status = 0;
285 else
286 status = 2;
287 }
288 catch (...)
289 {
290 text = "";
291 status = 1;
292 }
293 return status;
294}
295
306template <typename TessType>
307int writeTest(TessType& type,
308 std::function<tinyxml2::XMLElement*(const TessType&,
309 tinyxml2::XMLDocument&,
310 const std::string&,
311 const std::string&,
312 const int)> func,
313 std::string& text,
314 const std::string& directory,
315 const std::string& link_name,
316 const int id)
317{
318 tinyxml2::XMLDocument doc;
319 int status = 0;
320 try
321 {
322 tinyxml2::XMLElement* element = func(type, doc, directory, link_name, id);
323 text = toString(element);
324 if (element != nullptr)
325 status = 0;
326 else
327 status = 2;
328 }
329 catch (...)
330 {
331 text = "";
332 status = 1;
333 }
334 return status;
335}
336
337inline void writeTextToFile(const std::string& path, const std::string& text)
338{
339 std::ofstream file;
340 file.open(path);
341 file << text;
342 file.close();
343}
344
345#endif // TESSERACT_URDF_COMMON_UNIT_H
Abstract class for resource loaders.
Definition: resource_locator.h:43
EXPECT_TRUE(tesseract_common::isIdentical< tesseract_collision::ObjectPairKey >(pairs, check_pairs, false))
Common Tesseract Macros.
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
Definition: macros.h:71
Definition: create_convex_hull.cpp:36
void printNestedException(const std::exception &e, int level=0)
Print a nested exception.
Definition: utils.cpp:193
Locate and retrieve resource data.
ResourceLocator::Ptr locator
Definition: resource_locator_unit.cpp:57
Common Tesseract Utility Functions.
mCollisionCheckConfig contact_request type
Definition: tesseract_environment_collision.cpp:103
tinyxml2::XMLElement * element
Definition: tesseract_srdf_unit.cpp:815
tesseract_common::fs::path path
Definition: tesseract_srdf_unit.cpp:1992
std::string xml_string
Definition: tesseract_srdf_unit.cpp:567
tinyxml2::XMLDocument xml_doc
Definition: tesseract_srdf_unit.cpp:812
Locate and retrieve resource data in tesseract_support.
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH TESSERACT_COMMON_IGNORE_WARNINGS_POP bool runTest(ElementType &type, std::function< ElementType(const tinyxml2::XMLElement *, const int)> func, const std::string &xml_string, const std::string &element_name, int version)
Definition: tesseract_urdf_common_unit.h:21
std::string stripNewline(const std::string &in)
stripNewline - For some reason, tinyxml2 printers add a trailing newline. This function removes it.
Definition: tesseract_urdf_common_unit.h:171
int writeTest(TessType &type, std::function< tinyxml2::XMLElement *(const TessType &, tinyxml2::XMLDocument &)> func, std::string &text)
writeTest - test write functions
Definition: tesseract_urdf_common_unit.h:203
std::string toString(const tinyxml2::XMLElement *element)
Definition: tesseract_urdf_common_unit.h:181
void writeTextToFile(const std::string &path, const std::string &text)
Definition: tesseract_urdf_common_unit.h:337
std::unordered_map< std::string, tesseract_scene_graph::Material::Ptr > available_materials
Definition: tesseract_urdf_material_unit.cpp:20
A urdf parser for tesseract.