test_grid_metric_converter.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, the neonavigation authors
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the copyright holder 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 OWNER 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 
30 #define _USE_MATH_DEFINES
31 #include <cmath>
32 #include <vector>
33 
34 #include <costmap_cspace_msgs/MapMetaData3D.h>
36 #include <tf2/utils.h>
37 
38 #include <gtest/gtest.h>
39 
40 namespace planner_cspace
41 {
42 namespace planner_3d
43 {
44 TEST(GridMetricConverter, SinglePose)
45 {
46  costmap_cspace_msgs::MapMetaData3D map_info;
47  map_info.linear_resolution = 0.1;
48  map_info.angular_resolution = M_PI / 8;
49  map_info.origin.position.x = 100.0;
50  map_info.origin.position.y = 100.0;
51 
52  const float metric[][3] =
53  {
54  { 100.01, 100.01, M_PI / 8 },
55  { 100.01, 100.01, -M_PI / 8 },
56  { 105.01, 102.09, M_PI / 2 },
57  { 100.21, 102.51, -M_PI / 2 },
58  { 99.99, 100.01, 0 },
59  { 100.01, 100.01, 2.1 * M_PI / 8 },
60  { 100.01, 100.01, 1.9 * M_PI / 8 }
61  };
62  const int grid[][3] =
63  {
64  { 0, 0, 1 },
65  { 0, 0, -1 },
66  { 50, 20, 4 },
67  { 2, 25, -4 }, // angle must not normalized
68  { -1, 0, 0 }, // x, y must be rounded downword
69  { 0, 0, 2 }, // angle must be rounded to nearest integer
70  { 0, 0, 2 } // angle must be rounded to nearest integer
71  };
72  for (size_t i = 0; i < sizeof(grid) / sizeof(grid[0]); ++i)
73  {
74  // Convert metric to grid
75  int output_grid[3];
77  map_info,
78  output_grid[0], output_grid[1], output_grid[2],
79  metric[i][0], metric[i][1], metric[i][2]);
80  for (size_t j = 0; j < 3; ++j)
81  ASSERT_EQ(output_grid[j], grid[i][j]);
82 
83  // Convert grid to metric
84  float output_metric[3];
86  map_info,
87  grid[i][0], grid[i][1], grid[i][2],
88  output_metric[0], output_metric[1], output_metric[2]);
89  for (size_t j = 0; j < 3; ++j)
90  ASSERT_NEAR(output_metric[j], metric[i][j], map_info.linear_resolution / 2);
91  }
92 }
93 
94 TEST(GridMetricConverter, Path)
95 {
96  costmap_cspace_msgs::MapMetaData3D map_info;
97  map_info.linear_resolution = 0.1;
98  map_info.angle = 8;
99  map_info.angular_resolution = M_PI / map_info.angle;
100  map_info.origin.position.x = 100.0;
101  map_info.origin.position.y = 100.0;
102 
103  const float metric_expected[][3] =
104  {
105  { 100.15, 100.15, M_PI / 8 },
106  { 100.15, 100.25, M_PI / 8 },
107  { 100.25, 100.35, M_PI / 8 },
108  { 100.35, 100.35, 2 * M_PI / 8 }
109  };
110  const int grid[][3] =
111  {
112  { 1, 1, 1 },
113  { 1, 2, 1 },
114  { 2, 3, 1 },
115  { 3, 3, 2 }
116  };
117  std::vector<CyclicVecFloat<3, 2>> path_grid;
118  for (const auto& g : grid)
119  path_grid.push_back(CyclicVecFloat<3, 2>(g[0], g[1], g[2]));
120 
121  nav_msgs::Path path;
122  grid_metric_converter::grid2MetricPath(map_info, path_grid, path);
123  size_t ref = 0;
124  for (const geometry_msgs::PoseStamped& p : path.poses)
125  {
126  const double diff_dist = std::hypot(
127  metric_expected[ref][0] - p.pose.position.x,
128  metric_expected[ref][1] - p.pose.position.y);
129  double diff_yaw = std::abs(tf2::getYaw(p.pose.orientation) - metric_expected[ref][2]);
130 
131  if (diff_yaw < -M_PI)
132  diff_yaw += 2.0 * M_PI;
133  else if (diff_yaw > M_PI)
134  diff_yaw -= 2.0 * M_PI;
135 
136  ASSERT_LT(diff_dist, map_info.linear_resolution * 2);
137  ASSERT_LT(std::abs(diff_yaw), map_info.angular_resolution * 2);
138 
139  if (diff_dist < map_info.linear_resolution / 2 &&
140  std::abs(diff_yaw) < map_info.angular_resolution / 2)
141  {
142  ++ref;
143  if (ref == sizeof(metric_expected) / sizeof(metric_expected[0]))
144  return;
145  }
146  }
147  // some reference points are not met
148  ASSERT_TRUE(false);
149 }
150 } // namespace planner_3d
151 } // namespace planner_cspace
152 
153 int main(int argc, char** argv)
154 {
155  testing::InitGoogleTest(&argc, argv);
156 
157  return RUN_ALL_TESTS();
158 }
TEST(CostmapBBF, ForEach)
void metric2Grid(const costmap_cspace_msgs::MapMetaData3D &map_info, int &x, int &y, int &yaw, const float gx, const float gy, const float gyaw)
double getYaw(const A &a)
void grid2MetricPath(const costmap_cspace_msgs::MapMetaData3D &map_info, const STL_CONTAINER< CyclicVecFloat< 3, 2 >, std::allocator< CyclicVecFloat< 3, 2 >>> &path_grid, nav_msgs::Path &path)
void grid2Metric(const costmap_cspace_msgs::MapMetaData3D &map_info, const T x, const T y, const T yaw, float &gx, float &gy, float &gyaw)
int main(int argc, char **argv)


planner_cspace
Author(s): Atsushi Watanabe
autogenerated on Wed May 12 2021 02:20:42