tesseract_urdf_octree_unit.cpp
Go to the documentation of this file.
3 #include <gtest/gtest.h>
4 #include <Eigen/Geometry>
6 #include <octomap/OcTree.h>
8 
10 #include <tesseract_urdf/octree.h>
11 #include <tesseract_urdf/octomap.h>
14 
15 TEST(TesseractURDFUnit, parse_octree) // NOLINT
16 {
18  // {
19  // // Create octomap and add save it
20  // pcl::PointCloud<pcl::PointXYZ> full_cloud;
21  // double delta = 0.05;
22  // int length = static_cast<int>(1 / delta);
23 
24  // for (int x = 0; x < length; ++x)
25  // for (int y = 0; y < length; ++y)
26  // for (int z = 0; z < length; ++z)
27  // full_cloud.push_back(pcl::PointXYZ(-0.5f + static_cast<float>(x * delta),
28  // -0.5f + static_cast<float>(y * delta),
29  // -0.5f + static_cast<float>(z * delta)));
30 
31  // pcl::io::savePCDFile(tesseract_common::getTempPath() + "box_pcd.pcd", full_cloud, true);
32  // }
33 
34  {
35  std::string str = R"(<tesseract:octomap shape_type="box" extra="0 0 0">
36  <tesseract:octree filename="package://tesseract_support/meshes/box_2m.bt" extra="0 0 0"/>
37  </tesseract:octomap>)";
39  EXPECT_TRUE(runTest<tesseract_geometry::Octree::Ptr>(
40  geom, &tesseract_urdf::parseOctomap, str, tesseract_urdf::OCTOMAP_ELEMENT_NAME.data(), resource_locator, true));
42  EXPECT_TRUE(geom->getOctree() != nullptr);
43  EXPECT_EQ(geom->calcNumSubShapes(), 8);
44  }
45 
46  {
47  std::string str = R"(<tesseract:octomap shape_type="box" prune="true">
48  <tesseract:octree filename="package://tesseract_support/meshes/box_2m.bt"/>
49  </tesseract:octomap>)";
51  EXPECT_TRUE(runTest<tesseract_geometry::Octree::Ptr>(
52  geom, &tesseract_urdf::parseOctomap, str, tesseract_urdf::OCTOMAP_ELEMENT_NAME.data(), resource_locator, true));
54  EXPECT_TRUE(geom->getOctree() != nullptr);
55  EXPECT_EQ(geom->calcNumSubShapes(), 8);
56  }
57 
58  {
59  std::string str = R"(<tesseract:octomap shape_type="sphere_inside" prune="true">
60  <tesseract:octree filename="package://tesseract_support/meshes/box_2m.bt"/>
61  </tesseract:octomap>)";
63  EXPECT_TRUE(runTest<tesseract_geometry::Octree::Ptr>(
64  geom, &tesseract_urdf::parseOctomap, str, tesseract_urdf::OCTOMAP_ELEMENT_NAME.data(), resource_locator, true));
66  EXPECT_TRUE(geom->getOctree() != nullptr);
67  EXPECT_EQ(geom->calcNumSubShapes(), 8);
68  }
69 
70 #ifdef TESSERACT_PARSE_POINT_CLOUDS
71  {
72  std::string str = R"(<tesseract:octomap shape_type="box">
73  <tesseract:point_cloud filename="package://tesseract_support/meshes/box_pcd.pcd" resolution="0.1"/>
74  </tesseract:octomap>)";
76  EXPECT_TRUE(runTest<tesseract_geometry::Octree::Ptr>(
77  geom, &tesseract_urdf::parseOctomap, str, tesseract_urdf::OCTOMAP_ELEMENT_NAME.data(), resource_locator, true));
79  EXPECT_TRUE(geom->getOctree() != nullptr);
80  EXPECT_EQ(geom->calcNumSubShapes(), 1000);
81  EXPECT_NEAR(geom->getOctree()->getResolution(), 0.1, 1e-5);
82  }
83 
84  {
85  std::string str = R"(<tesseract:octomap shape_type="box" prune="true">
86  <tesseract:point_cloud filename="package://tesseract_support/meshes/box_pcd.pcd" resolution="0.1"/>
87  </tesseract:octomap>)";
89  EXPECT_TRUE(runTest<tesseract_geometry::Octree::Ptr>(
90  geom, &tesseract_urdf::parseOctomap, str, tesseract_urdf::OCTOMAP_ELEMENT_NAME.data(), resource_locator, true));
92  EXPECT_TRUE(geom->getOctree() != nullptr);
93  EXPECT_EQ(geom->calcNumSubShapes(), 496);
94  EXPECT_NEAR(geom->getOctree()->getResolution(), 0.1, 1e-5);
95  }
96 
97  {
98  std::string str = R"(<tesseract:octomap shape_type="box" prune="true">
99  <tesseract:point_cloud />
100  </tesseract:octomap>)";
102  EXPECT_FALSE(runTest<tesseract_geometry::Octree::Ptr>(
103  geom, &tesseract_urdf::parseOctomap, str, tesseract_urdf::OCTOMAP_ELEMENT_NAME.data(), resource_locator, true));
104  }
105 
106  { // Failure invalid resolution
107  std::string str = R"(<tesseract:octomap shape_type="box">
108  <tesseract:point_cloud filename="package://tesseract_support/meshes/box_pcd.pcd" resolution="a"/>
109  </tesseract:octomap>)";
111  EXPECT_FALSE(runTest<tesseract_geometry::Octree::Ptr>(
112  geom, &tesseract_urdf::parseOctomap, str, tesseract_urdf::OCTOMAP_ELEMENT_NAME.data(), resource_locator, true));
113  }
114 
115  { // Failure resource does not exist
116  std::string str = R"(<tesseract:octomap shape_type="box">
117  <tesseract:point_cloud filename="package://tesseract_support/meshes/does_not_exist.pcd" resolution="0.1"/>
118  </tesseract:octomap>)";
120  EXPECT_FALSE(runTest<tesseract_geometry::Octree::Ptr>(
121  geom, &tesseract_urdf::parseOctomap, str, tesseract_urdf::OCTOMAP_ELEMENT_NAME.data(), resource_locator, true));
122  }
123 
124  { // Failure resource is not a point cloud
125  std::string str = R"(<tesseract:octomap shape_type="box">
126  <tesseract:point_cloud filename="package://tesseract_support/meshes/box_2m.bt" resolution="0.1"/>
127  </tesseract:octomap>)";
129  EXPECT_FALSE(runTest<tesseract_geometry::Octree::Ptr>(
130  geom, &tesseract_urdf::parseOctomap, str, tesseract_urdf::OCTOMAP_ELEMENT_NAME.data(), resource_locator, true));
131  }
132 #endif
133 
134  {
135  std::string str = R"(<tesseract:octomap shape_type="sphere_outside" prune="true">
136  <tesseract:octree filename="package://tesseract_support/meshes/box_2m.bt"/>
137  </tesseract:octomap>)";
139  EXPECT_TRUE(runTest<tesseract_geometry::Octree::Ptr>(
140  geom, &tesseract_urdf::parseOctomap, str, tesseract_urdf::OCTOMAP_ELEMENT_NAME.data(), resource_locator, true));
142  EXPECT_TRUE(geom->getOctree() != nullptr);
143  EXPECT_EQ(geom->calcNumSubShapes(), 8);
144  }
145 
146  {
147  std::string str = R"(<tesseract:octomap shape_type="sphere_outside" prune="true">
148  <tesseract:octree />
149  </tesseract:octomap>)";
151  EXPECT_FALSE(runTest<tesseract_geometry::Octree::Ptr>(
152  geom, &tesseract_urdf::parseOctomap, str, tesseract_urdf::OCTOMAP_ELEMENT_NAME.data(), resource_locator, true));
153  }
154 
155  {
156  std::string str = R"(<tesseract:octomap shape_type="sphere_outside" prune="true">
157  <tesseract:octree filename="abc"/>
158  </tesseract:octomap>)";
160  EXPECT_FALSE(runTest<tesseract_geometry::Octree::Ptr>(
161  geom, &tesseract_urdf::parseOctomap, str, tesseract_urdf::OCTOMAP_ELEMENT_NAME.data(), resource_locator, true));
162  }
163 
164  {
165  std::string str = R"(<tesseract:octomap shape_type="sphere_outside" prune="true">
166  <tesseract:octree />
167  </tesseract:octomap>)";
169  EXPECT_FALSE(runTest<tesseract_geometry::Octree::Ptr>(
170  geom, &tesseract_urdf::parseOctomap, str, tesseract_urdf::OCTOMAP_ELEMENT_NAME.data(), resource_locator, true));
171  }
172 
173  {
174  std::string str = R"(<tesseract:octomap shape_type="star" prune="true">
175  <tesseract:octree filename="package://tesseract_support/meshes/box_2m.bt"/>
176  </tesseract:octomap>)";
178  EXPECT_FALSE(runTest<tesseract_geometry::Octree::Ptr>(
179  geom, &tesseract_urdf::parseOctomap, str, tesseract_urdf::OCTOMAP_ELEMENT_NAME.data(), resource_locator, true));
180  }
181 
182  {
183  std::string str = R"(<tesseract:octomap shape_type="star" prune="true">
184  <tesseract:octree/>
185  </tesseract:octomap>)";
187  EXPECT_FALSE(runTest<tesseract_geometry::Octree::Ptr>(
188  geom, &tesseract_urdf::parseOctomap, str, tesseract_urdf::OCTOMAP_ELEMENT_NAME.data(), resource_locator, true));
189  }
190 
191  {
192  std::string str = "<tesseract:octomap />";
194  EXPECT_FALSE(runTest<tesseract_geometry::Octree::Ptr>(
195  geom, &tesseract_urdf::parseOctomap, str, tesseract_urdf::OCTOMAP_ELEMENT_NAME.data(), resource_locator, true));
196  }
197 }
198 
199 TEST(TesseractURDFUnit, write_octree) // NOLINT
200 {
201  {
202  tesseract_geometry::Octree::Ptr geom = std::make_shared<tesseract_geometry::Octree>(
203  std::make_shared<octomap::OcTree>(1.0), tesseract_geometry::OctreeSubType::BOX);
204  std::string text;
205  EXPECT_EQ(0,
206  writeTest<tesseract_geometry::Octree::Ptr>(
207  geom, &tesseract_urdf::writeOctree, text, tesseract_common::getTempPath(), std::string("oct0.bt")));
208  EXPECT_NE(text, "");
209  }
210 
211  { // Trigger failed-to-write
212  tesseract_geometry::Octree::Ptr geom = std::make_shared<tesseract_geometry::Octree>(
213  std::make_shared<octomap::OcTree>(1.0), tesseract_geometry::OctreeSubType::BOX);
214  std::string text;
215  EXPECT_EQ(1,
216  writeTest<tesseract_geometry::Octree::Ptr>(
217  geom, &tesseract_urdf::writeOctree, text, tesseract_common::getTempPath(), std::string("")));
218  EXPECT_EQ(text, "");
219  }
220 
221  { // trigger nullptr input
222  tesseract_geometry::Octree::Ptr geom = nullptr;
223  std::string text;
224  EXPECT_EQ(1,
225  writeTest<tesseract_geometry::Octree::Ptr>(
226  geom, &tesseract_urdf::writeOctree, text, tesseract_common::getTempPath(), std::string("oct2.bt")));
227  EXPECT_EQ(text, "");
228  }
229 }
230 
231 TEST(TesseractURDFUnit, write_octomap) // NOLINT
232 {
233  { // box
234  tesseract_geometry::Octree::Ptr geom = std::make_shared<tesseract_geometry::Octree>(
235  std::make_shared<octomap::OcTree>(1.0), tesseract_geometry::OctreeSubType::BOX);
236  std::string text;
237  EXPECT_EQ(0,
238  writeTest<tesseract_geometry::Octree::Ptr>(
239  geom, &tesseract_urdf::writeOctomap, text, tesseract_common::getTempPath(), std::string("octo0.bt")));
240  EXPECT_NE(text, "");
241  }
242 
243  { // sphere inside
244  tesseract_geometry::Octree::Ptr geom = std::make_shared<tesseract_geometry::Octree>(
245  std::make_shared<octomap::OcTree>(1.0), tesseract_geometry::OctreeSubType::SPHERE_INSIDE);
246  std::string text;
247  EXPECT_EQ(0,
248  writeTest<tesseract_geometry::Octree::Ptr>(
249  geom, &tesseract_urdf::writeOctomap, text, tesseract_common::getTempPath(), std::string("octo1.bt")));
250  EXPECT_NE(text, "");
251  }
252 
253  { // sphere outside
254  tesseract_geometry::Octree::Ptr geom = std::make_shared<tesseract_geometry::Octree>(
255  std::make_shared<octomap::OcTree>(1.0), tesseract_geometry::OctreeSubType::SPHERE_OUTSIDE);
256  std::string text;
257  EXPECT_EQ(0,
258  writeTest<tesseract_geometry::Octree::Ptr>(
259  geom, &tesseract_urdf::writeOctomap, text, tesseract_common::getTempPath(), std::string("octo2.bt")));
260  EXPECT_NE(text, "");
261  }
262 
263  { // Trigger failed-to-write
264  tesseract_geometry::Octree::Ptr geom = std::make_shared<tesseract_geometry::Octree>(
265  std::make_shared<octomap::OcTree>(1.0), tesseract_geometry::OctreeSubType::BOX);
266  std::string text;
267  EXPECT_EQ(1,
268  writeTest<tesseract_geometry::Octree::Ptr>(
269  geom, &tesseract_urdf::writeOctomap, text, tesseract_common::getTempPath(), std::string("")));
270  EXPECT_EQ(text, "");
271  }
272 
273  { // trigger nullptr input
274  tesseract_geometry::Octree::Ptr geom = nullptr;
275  std::string text;
276  EXPECT_EQ(1,
277  writeTest<tesseract_geometry::Octree::Ptr>(
278  geom, &tesseract_urdf::writeOctomap, text, tesseract_common::getTempPath(), std::string("oct2.bt")));
279  EXPECT_EQ(text, "");
280  }
281 }
tesseract_common::getTempPath
std::string getTempPath()
utils.h
resource_locator.h
tesseract_urdf::writeOctomap
tinyxml2::XMLElement * writeOctomap(const std::shared_ptr< const tesseract_geometry::Octree > &octree, tinyxml2::XMLDocument &doc, const std::string &package_path, const std::string &filename)
writeOctomap Write octomap to URDF XML. This is non-standard URDF / tesseract-exclusive
Definition: octomap.cpp:101
tesseract_geometry::OctreeSubType::SPHERE_OUTSIDE
@ SPHERE_OUTSIDE
tesseract_urdf::OCTOMAP_ELEMENT_NAME
static constexpr std::string_view OCTOMAP_ELEMENT_NAME
Definition: octomap.h:46
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
TEST
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH TESSERACT_COMMON_IGNORE_WARNINGS_POP TEST(TesseractURDFUnit, parse_octree)
Definition: tesseract_urdf_octree_unit.cpp:15
TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
#define TESSERACT_COMMON_IGNORE_WARNINGS_PUSH
OcTree.h
EXPECT_TRUE
#define EXPECT_TRUE(args)
tesseract_urdf_common_unit.h
EXPECT_NEAR
#define EXPECT_NEAR(a, b, prec)
tesseract_urdf::parseOctomap
std::shared_ptr< tesseract_geometry::Octree > parseOctomap(const tinyxml2::XMLElement *xml_element, const tesseract_common::ResourceLocator &locator, bool visual)
Parse xml element octomap.
Definition: octomap.cpp:47
octomap.h
Parse octomap from xml string.
TESSERACT_COMMON_IGNORE_WARNINGS_POP
tesseract_geometry::Octree::Ptr
std::shared_ptr< Octree > Ptr
octree.h
tesseract_common::GeneralResourceLocator
macros.h
octree.h
Parse octree from xml string.
EXPECT_EQ
#define EXPECT_EQ(a, b)
EXPECT_FALSE
#define EXPECT_FALSE(args)
tesseract_geometry::OctreeSubType::BOX
@ BOX
tesseract_geometry::OctreeSubType::SPHERE_INSIDE
@ SPHERE_INSIDE


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