Skip to content

Commit 038a285

Browse files
author
wep21
committed
Add test for conversion
Signed-off-by: wep21 <border_goldenmarket@yahoo.co.jp>
1 parent 5d6b865 commit 038a285

File tree

4 files changed

+271
-1
lines changed

4 files changed

+271
-1
lines changed

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ ament_auto_add_library(${PROJECT_NAME} SHARED
2020
if(BUILD_TESTING)
2121
find_package(ament_lint_auto REQUIRED)
2222
ament_lint_auto_find_test_dependencies()
23+
24+
ament_add_gtest(test_${PROJECT_NAME}
25+
test/test_conversions.cpp
26+
)
27+
target_link_libraries(test_${PROJECT_NAME}
28+
gtest_main
29+
octomap
30+
octomap_ros
31+
)
2332
endif()
2433

2534
ament_auto_package()

package.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616

1717
<buildtool_depend>ament_cmake_auto</buildtool_depend>
1818

19-
<depend>octomap_msgs</depend>
2019
<depend>octomap</depend>
20+
<depend>octomap_msgs</depend>
2121
<depend>sensor_msgs</depend>
2222
<depend>tf2</depend>
2323

24+
<test_depend>ament_cmake_gtest</test_depend>
2425
<test_depend>ament_lint_auto</test_depend>
2526
<test_depend>ament_lint_common</test_depend>
2627

src/conversions.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
// POSSIBILITY OF SUCH DAMAGE.
3636

3737
#include <sensor_msgs/point_cloud2_iterator.hpp>
38+
3839
#include <octomap_ros/conversions.hpp>
3940

4041
namespace octomap

test/test_conversions.cpp

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
// Copyright 2021, Daisuke Nishimatsu. All rights reserved.
2+
//
3+
// Redistribution and use in source and binary forms, with or without
4+
// modification, are permitted provided that the following conditions are met:
5+
//
6+
// * Redistributions of source code must retain the above copyright
7+
// notice, this list of conditions and the following disclaimer.
8+
//
9+
// * Redistributions in binary form must reproduce the above copyright
10+
// notice, this list of conditions and the following disclaimer in the
11+
// documentation and/or other materials provided with the distribution.
12+
//
13+
// * Neither the name of the Willow Garage nor the names of its
14+
// contributors may be used to endorse or promote products derived from
15+
// this software without specific prior written permission.
16+
//
17+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
// POSSIBILITY OF SUCH DAMAGE.
28+
29+
#include <sensor_msgs/point_cloud2_iterator.hpp>
30+
31+
#include <octomap_ros/conversions.hpp>
32+
33+
#include <gtest/gtest.h>
34+
35+
constexpr double epsilon = 1e-6;
36+
37+
TEST(conversions, pointOctomapToMsg)
38+
{
39+
using octomap::pointOctomapToMsg;
40+
41+
const double x = 1.0;
42+
const double y = 2.0;
43+
const double z = 3.0;
44+
45+
const auto octo_p = octomap::point3d(x, y, z);
46+
const auto geom_p = pointOctomapToMsg(octo_p);
47+
48+
EXPECT_DOUBLE_EQ(geom_p.x, x);
49+
EXPECT_DOUBLE_EQ(geom_p.y, y);
50+
EXPECT_DOUBLE_EQ(geom_p.z, z);
51+
}
52+
53+
TEST(conversions, pointMsgToOctomap)
54+
{
55+
using octomap::pointMsgToOctomap;
56+
57+
const double x = 1.0;
58+
const double y = 2.0;
59+
const double z = 3.0;
60+
61+
const auto geom_p = geometry_msgs::build<geometry_msgs::msg::Point>().x(x).y(y).z(z);
62+
const auto octo_p = pointMsgToOctomap(geom_p);
63+
64+
EXPECT_DOUBLE_EQ(octo_p.x(), x);
65+
EXPECT_DOUBLE_EQ(octo_p.y(), y);
66+
EXPECT_DOUBLE_EQ(octo_p.z(), z);
67+
}
68+
69+
TEST(conversions, pointOctomapToTf)
70+
{
71+
using octomap::pointOctomapToTf;
72+
73+
const double x = 1.0;
74+
const double y = 2.0;
75+
const double z = 3.0;
76+
77+
const auto octo_p = octomap::point3d(x, y, z);
78+
const auto tf_v = pointOctomapToTf(octo_p);
79+
80+
EXPECT_DOUBLE_EQ(tf_v.x(), x);
81+
EXPECT_DOUBLE_EQ(tf_v.y(), y);
82+
EXPECT_DOUBLE_EQ(tf_v.z(), z);
83+
}
84+
85+
TEST(conversions, pointTfToOctomap)
86+
{
87+
using octomap::pointTfToOctomap;
88+
89+
const double x = 1.0;
90+
const double y = 2.0;
91+
const double z = 3.0;
92+
93+
const auto tf_v = tf2::Vector3(x, y, z);
94+
const auto octo_p = pointTfToOctomap(tf_v);
95+
96+
EXPECT_DOUBLE_EQ(octo_p.x(), x);
97+
EXPECT_DOUBLE_EQ(octo_p.y(), y);
98+
EXPECT_DOUBLE_EQ(octo_p.z(), z);
99+
}
100+
101+
TEST(conversions, quaternionOctomapToTf)
102+
{
103+
using octomap::quaternionOctomapToTf;
104+
105+
const double u = 1.0;
106+
const double x = 2.0;
107+
const double y = 3.0;
108+
const double z = 4.0;
109+
110+
const auto octo_q = octomath::Quaternion(u, x, y, z);
111+
const auto tf_q = quaternionOctomapToTf(octo_q);
112+
113+
EXPECT_DOUBLE_EQ(tf_q.w(), u);
114+
EXPECT_DOUBLE_EQ(tf_q.x(), x);
115+
EXPECT_DOUBLE_EQ(tf_q.y(), y);
116+
EXPECT_DOUBLE_EQ(tf_q.z(), z);
117+
}
118+
119+
TEST(conversions, quaternionTfToOctomap)
120+
{
121+
using octomap::quaternionTfToOctomap;
122+
123+
const double w = 1.0;
124+
const double x = 2.0;
125+
const double y = 3.0;
126+
const double z = 4.0;
127+
128+
const auto tf_q = tf2::Quaternion(x, y, z, w);
129+
const auto octo_q = quaternionTfToOctomap(tf_q);
130+
131+
EXPECT_DOUBLE_EQ(octo_q.u(), w);
132+
EXPECT_DOUBLE_EQ(octo_q.x(), x);
133+
EXPECT_DOUBLE_EQ(octo_q.y(), y);
134+
EXPECT_DOUBLE_EQ(octo_q.z(), z);
135+
}
136+
137+
TEST(conversions, poseOctomapToTf)
138+
{
139+
using octomap::poseOctomapToTf;
140+
141+
const double x = 1.0;
142+
const double y = 2.0;
143+
const double z = 3.0;
144+
145+
const auto octo_p = octomap::point3d(x, y, z);
146+
147+
const double rot_u = 1.0;
148+
const double rot_x = 2.0;
149+
const double rot_y = 3.0;
150+
const double rot_z = 4.0;
151+
152+
const auto octo_q = octomath::Quaternion(rot_u, rot_x, rot_y, rot_z);
153+
154+
const auto octo_pose = octomap::pose6d(octo_p, octo_q);
155+
const auto tf_trans = poseOctomapToTf(octo_pose);
156+
const auto length = octo_q.norm();
157+
158+
EXPECT_DOUBLE_EQ(tf_trans.getOrigin().x(), x);
159+
EXPECT_DOUBLE_EQ(tf_trans.getOrigin().y(), y);
160+
EXPECT_DOUBLE_EQ(tf_trans.getOrigin().z(), z);
161+
EXPECT_NEAR(tf_trans.getRotation().w() * length, rot_u, epsilon);
162+
EXPECT_NEAR(tf_trans.getRotation().x() * length, rot_x, epsilon);
163+
EXPECT_NEAR(tf_trans.getRotation().y() * length, rot_y, epsilon);
164+
EXPECT_NEAR(tf_trans.getRotation().z() * length, rot_z, epsilon);
165+
}
166+
167+
TEST(conversions, poseTfToOctomap)
168+
{
169+
using octomap::poseTfToOctomap;
170+
171+
const double x = 1.0;
172+
const double y = 2.0;
173+
const double z = 3.0;
174+
175+
const auto tf_v = tf2::Vector3(x, y, z);
176+
177+
const double rot_w = 1.0;
178+
const double rot_x = 2.0;
179+
const double rot_y = 3.0;
180+
const double rot_z = 4.0;
181+
182+
const auto tf_q = tf2::Quaternion(rot_x, rot_y, rot_z, rot_w);
183+
184+
const auto tf_trans = tf2::Transform(tf_q, tf_v);
185+
const auto octo_pose = poseTfToOctomap(tf_trans);
186+
const auto length = tf_q.length();
187+
188+
EXPECT_DOUBLE_EQ(octo_pose.trans().x(), x);
189+
EXPECT_DOUBLE_EQ(octo_pose.trans().y(), y);
190+
EXPECT_DOUBLE_EQ(octo_pose.trans().z(), z);
191+
EXPECT_NEAR(octo_pose.rot().u() * length, rot_w, epsilon);
192+
EXPECT_NEAR(octo_pose.rot().x() * length, rot_x, epsilon);
193+
EXPECT_NEAR(octo_pose.rot().y() * length, rot_y, epsilon);
194+
EXPECT_NEAR(octo_pose.rot().z() * length, rot_z, epsilon);
195+
}
196+
197+
TEST(conversions, pointsOctomapToPointCloud2)
198+
{
199+
using octomap::pointsOctomapToPointCloud2;
200+
201+
octomap::point3d_list points{{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}, {7.0, 8.0, 9.0}};
202+
sensor_msgs::msg::PointCloud2 ros_points{};
203+
sensor_msgs::PointCloud2Modifier modifier{ros_points};
204+
modifier.setPointCloud2FieldsByString(1, "xyz");
205+
pointsOctomapToPointCloud2(points, ros_points);
206+
sensor_msgs::PointCloud2Iterator<float> iter_x(ros_points, "x");
207+
sensor_msgs::PointCloud2Iterator<float> iter_y(ros_points, "y");
208+
sensor_msgs::PointCloud2Iterator<float> iter_z(ros_points, "z");
209+
210+
EXPECT_EQ(modifier.size(), 3U);
211+
EXPECT_DOUBLE_EQ(*iter_x, 1.0);
212+
EXPECT_DOUBLE_EQ(*iter_y, 2.0);
213+
EXPECT_DOUBLE_EQ(*iter_z, 3.0);
214+
++iter_x;
215+
++iter_y;
216+
++iter_z;
217+
EXPECT_DOUBLE_EQ(*iter_x, 4.0);
218+
EXPECT_DOUBLE_EQ(*iter_y, 5.0);
219+
EXPECT_DOUBLE_EQ(*iter_z, 6.0);
220+
++iter_x;
221+
++iter_y;
222+
++iter_z;
223+
EXPECT_DOUBLE_EQ(*iter_x, 7.0);
224+
EXPECT_DOUBLE_EQ(*iter_y, 8.0);
225+
EXPECT_DOUBLE_EQ(*iter_z, 9.0);
226+
}
227+
228+
TEST(conversions, pointCloud2ToOctomap)
229+
{
230+
using octomap::pointCloud2ToOctomap;
231+
232+
sensor_msgs::msg::PointCloud2 ros_points{};
233+
sensor_msgs::PointCloud2Modifier modifier{ros_points};
234+
modifier.setPointCloud2FieldsByString(1, "xyz");
235+
modifier.resize(3);
236+
sensor_msgs::PointCloud2Iterator<float> iter_x(ros_points, "x");
237+
sensor_msgs::PointCloud2Iterator<float> iter_y(ros_points, "y");
238+
sensor_msgs::PointCloud2Iterator<float> iter_z(ros_points, "z");
239+
int val{0};
240+
for (size_t i = 0; i < modifier.size(); ++i, ++iter_x, ++iter_y, ++iter_z) {
241+
*iter_x = static_cast<float>(++val);
242+
*iter_y = static_cast<float>(++val);
243+
*iter_z = static_cast<float>(++val);
244+
}
245+
246+
octomap::Pointcloud octo_points{};
247+
pointCloud2ToOctomap(ros_points, octo_points);
248+
249+
EXPECT_EQ(octo_points.size(), 3U);
250+
EXPECT_DOUBLE_EQ(octo_points[0].x(), 1.0);
251+
EXPECT_DOUBLE_EQ(octo_points[0].y(), 2.0);
252+
EXPECT_DOUBLE_EQ(octo_points[0].z(), 3.0);
253+
EXPECT_DOUBLE_EQ(octo_points[1].x(), 4.0);
254+
EXPECT_DOUBLE_EQ(octo_points[1].y(), 5.0);
255+
EXPECT_DOUBLE_EQ(octo_points[1].z(), 6.0);
256+
EXPECT_DOUBLE_EQ(octo_points[2].x(), 7.0);
257+
EXPECT_DOUBLE_EQ(octo_points[2].y(), 8.0);
258+
EXPECT_DOUBLE_EQ(octo_points[2].z(), 9.0);
259+
}

0 commit comments

Comments
 (0)