test_distance_map_fast_update.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2023, 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 #include <cmath>
31 #include <limits>
32 #include <string>
33 #include <vector>
34 
35 #include <gtest/gtest.h>
36 
37 #include <omp.h>
38 
39 #include <costmap_cspace_msgs/MapMetaData3D.h>
45 
46 namespace planner_cspace
47 {
48 namespace planner_3d
49 {
50 using Vec3 = CyclicVecInt<3, 2>;
51 using Astar = GridAstar<3, 2>;
52 
53 class DistanceMapTest : public ::testing::Test
54 {
55 protected:
56  const int w_ = 12;
57  const int h_ = 8;
58  const int angle_ = 10;
59 
60  const Astar::Vecf ec_;
61  const float tolerance_ = 0.4;
62 
63  Astar::Gridmap<char, 0x80> cm_rough_;
65 
68 
70  : ec_(0.5f, 0.5f, 0.2f)
74  {
75  const int range = 0;
76  const int local_range = 0;
77  omp_set_num_threads(2);
78 
79  costmap_cspace_msgs::MapMetaData3D map_info;
80  map_info.width = w_;
81  map_info.height = h_;
82  map_info.angle = angle_;
83  map_info.linear_resolution = 1.0;
84  map_info.angular_resolution = M_PI * 2 / angle_;
85 
86  CostCoeff cc;
87  cc.weight_costmap_ = 1.0f;
88  cc.weight_remembered_ = 0.0f;
89  cc.angle_resolution_aspect_ = 2.0f / tanf(map_info.angular_resolution);
90  dm_full_.setParams(cc, 64 * 2);
91  dm_fast_.setParams(cc, 64 * 2);
92 
93  const Astar::Vec size3d(w_, h_, angle_);
94  const Astar::Vec size2d(w_, h_, 1);
95  Astar::Gridmap<char, 0x40> cm;
96  Astar::Gridmap<char, 0x80> cm_hyst;
98  new GridAstarModel3D(
99  map_info,
100  ec_,
101  local_range,
102  dm_full_.gridmap(), cm, cm_hyst, cm_rough_,
103  cc, range));
104  cm.reset(size3d);
105  cm_hyst.reset(size3d);
106  cm_rough_.reset(size2d);
107  bbf_costmap_->reset(size2d);
108 
109  cm.clear(0);
110  cm_hyst.clear(0);
111  cm_rough_.clear(0);
112  bbf_costmap_->clear();
113 
114  const DistanceMap::Params dmp =
115  {
116  .euclid_cost = ec_,
117  .range = range,
118  .local_range = local_range,
119  .longcut_range = 0,
120  .size = size2d,
121  .resolution = map_info.linear_resolution,
122  };
123  dm_full_.init(model, dmp);
124  dm_fast_.init(model, dmp);
125  }
126 
128  {
129  /*
130  [ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
131  [ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
132  [ ][X][X][X][X][X][X][X][X][X][X][ ]
133  [ ][X][X][X][X][X][X][X][X][X][X][ ]
134  [ ][X][X][X][X][X][X][X][X][X][X][ ]
135  [ ][X][X][X][X][X][X][X][X][X][X][ ]
136  [ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][G]
137  [ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
138  */
139  cm_rough_.clear(0);
140  for (int x = 1; x < w_ - 1; x++)
141  {
142  cm_rough_[Astar::Vec(x, 2, 0)] = 100;
143  cm_rough_[Astar::Vec(x, 3, 0)] = 100;
144  cm_rough_[Astar::Vec(x, 4, 0)] = 100;
145  cm_rough_[Astar::Vec(x, 5, 0)] = 100;
146  }
147  }
148 
149  bool validate(const std::string& msg) const
150  {
151  for (int y = 0; y < h_; y++)
152  {
153  for (int x = 0; x < w_; x++)
154  {
155  const Astar::Vec pos(x, y, 0);
156  if (cm_rough_[pos] == 100)
157  {
158  continue;
159  }
160  EXPECT_NEAR(dm_full_[pos], dm_fast_[pos], 0.5) << msg + " failed at " + xyStr(x, y);
161  if (::testing::Test::HasFailure())
162  {
163  return false;
164  }
165  }
166  }
167  return true;
168  }
169 };
170 
172 {
173  setupCostmap();
174 
175  const Astar::Vec s(9, 1, 0);
176  const Astar::Vec e(11, 6, 0);
177 
178  dm_full_.create(s, e);
179  dm_fast_.create(s, e);
180  debugOutput(dm_fast_, cm_rough_, s, e);
181 
182  cm_rough_[Astar::Vec(10, 0, 0)] = 100;
183  cm_rough_[Astar::Vec(10, 1, 0)] = 100;
184 
185  dm_full_.create(s, e);
186  dm_fast_.update(s, e, DistanceMap::Rect(Astar::Vec(9, 1, 0), Astar::Vec(11, 1, 0)));
187  debugOutput(dm_fast_, cm_rough_, s, e);
188 
189  if (!validate("obstacle in range"))
190  {
191  fprintf(stderr, "expected:\n");
192  debugOutput(dm_full_, cm_rough_, s, e);
193  return;
194  }
195 }
196 
197 TEST_F(DistanceMapTest, BlockedEdge)
198 {
199  setupCostmap();
200 
201  Astar::Vec s(4, 1, 0);
202  const Astar::Vec e(11, 6, 0);
203 
204  dm_full_.create(s, e);
205  dm_fast_.create(s, e);
206  debugOutput(dm_fast_, cm_rough_, s, e);
207 
208  s = Astar::Vec(6, 1, 0);
209 
210  cm_rough_[Astar::Vec(9, 0, 0)] = 100;
211  cm_rough_[Astar::Vec(9, 1, 0)] = 100;
212  cm_rough_[Astar::Vec(10, 0, 0)] = 100;
213  cm_rough_[Astar::Vec(10, 1, 0)] = 100;
214  cm_rough_[Astar::Vec(11, 0, 0)] = 100;
215  cm_rough_[Astar::Vec(11, 1, 0)] = 100;
216 
217  dm_full_.create(s, e);
218  dm_fast_.update(s, e, DistanceMap::Rect(Astar::Vec(2, 0, 0), Astar::Vec(10, 1, 0)));
219  debugOutput(dm_fast_, cm_rough_, s, e);
220 
221  if (!validate("obstacle in range"))
222  {
223  fprintf(stderr, "expected:\n");
224  debugOutput(dm_full_, cm_rough_, s, e);
225  return;
226  }
227 
228  s = Astar::Vec(4, 1, 0);
229 
230  dm_full_.create(s, e);
231  dm_fast_.update(s, e, DistanceMap::Rect(Astar::Vec(2, 0, 0), Astar::Vec(10, 1, 0)));
232  debugOutput(dm_fast_, cm_rough_, s, e);
233 
234  if (!validate("going around"))
235  {
236  fprintf(stderr, "expected:\n");
237  debugOutput(dm_full_, cm_rough_, s, e);
238  return;
239  }
240 
241  s = Astar::Vec(0, 1, 0);
242  cm_rough_[Astar::Vec(9, 0, 0)] = 0;
243  cm_rough_[Astar::Vec(9, 1, 0)] = 0;
244  cm_rough_[Astar::Vec(10, 0, 0)] = 0;
245  cm_rough_[Astar::Vec(10, 1, 0)] = 0;
246  cm_rough_[Astar::Vec(11, 0, 0)] = 0;
247  cm_rough_[Astar::Vec(11, 1, 0)] = 0;
248 
249  dm_full_.create(s, e);
250  dm_fast_.update(s, e, DistanceMap::Rect(Astar::Vec(0, 0, 0), Astar::Vec(10, 1, 0)));
251  debugOutput(dm_fast_, cm_rough_, s, e);
252 
253  if (!validate("obstacle out of range"))
254  {
255  fprintf(stderr, "expected:\n");
256  debugOutput(dm_full_, cm_rough_, s, e);
257  return;
258  }
259 }
260 } // namespace planner_3d
261 } // namespace planner_cspace
262 
263 int main(int argc, char** argv)
264 {
265  testing::InitGoogleTest(&argc, argv);
266 
267  return RUN_ALL_TESTS();
268 }
planner_cspace::planner_3d::CostCoeff
Definition: planner_3d/grid_astar_model.h:51
planner_cspace::planner_3d::TEST_F
TEST_F(DistanceMapTest, Create)
Definition: test_distance_map.cpp:218
planner_cspace::planner_3d::DistanceMapTest::bbf_costmap_
CostmapBBF::Ptr bbf_costmap_
Definition: test_distance_map.cpp:68
planner_cspace
Definition: bbf.h:33
msg
msg
planner_cspace::planner_3d::DistanceMapTest::tolerance_
const float tolerance_
Definition: test_distance_map.cpp:65
s
XmlRpcServer s
planner_cspace::planner_3d::GridAstarModel3D
Definition: planner_3d/grid_astar_model.h:72
planner_cspace::planner_3d::CostmapBBFImpl
Definition: costmap_bbf.h:62
planner_cspace::planner_3d::DistanceMapTest::dm_fast_
DistanceMap dm_fast_
Definition: test_distance_map_fast_update.cpp:67
main
int main(int argc, char **argv)
Definition: test_distance_map_fast_update.cpp:263
grid_astar_model.h
validate
ROSCPP_DECL bool validate(const std::string &name, std::string &error)
f
f
costmap_bbf.h
planner_cspace::planner_3d::DistanceMapTest::dm_full_
DistanceMap dm_full_
Definition: test_distance_map_fast_update.cpp:66
planner_cspace::planner_3d::CostCoeff::angle_resolution_aspect_
float angle_resolution_aspect_
Definition: planner_3d/grid_astar_model.h:68
distance_map_utils.h
planner_cspace::planner_3d::DistanceMap::Params
Definition: distance_map.h:70
planner_cspace::planner_3d::DistanceMapTest::cm_rough_
Astar::Gridmap< char, 0x80 > cm_rough_
Definition: test_distance_map.cpp:67
planner_cspace::planner_3d::DistanceMapTest::DistanceMapTest
DistanceMapTest()
Definition: test_distance_map_fast_update.cpp:69
planner_cspace::planner_3d::CostCoeff::weight_remembered_
float weight_remembered_
Definition: planner_3d/grid_astar_model.h:60
distance_map.h
planner_cspace::GridAstar< 3, 2 >::Vecf
CyclicVecFloat< DIM, NONCYCLIC > Vecf
Definition: grid_astar.h:68
planner_cspace::planner_3d::DistanceMapTest::ec_
const Astar::Vecf ec_
Definition: test_distance_map.cpp:64
planner_cspace::planner_3d::Astar
GridAstar< 3, 2 > Astar
Definition: distance_map_utils.h:44
grid_astar.h
planner_cspace::planner_3d::DistanceMapTest::w_
const int w_
Definition: test_distance_map.cpp:57
planner_cspace::planner_3d::DistanceMapTest::angle_
const int angle_
Definition: test_distance_map.cpp:59
planner_cspace::planner_3d::DistanceMap
Definition: distance_map.h:47
planner_cspace::planner_3d::GridAstarModel3D::Ptr
std::shared_ptr< GridAstarModel3D > Ptr
Definition: planner_3d/grid_astar_model.h:76
planner_cspace::planner_3d::DistanceMapTest::setupCostmap
void setupCostmap()
Definition: test_distance_map_fast_update.cpp:127
planner_cspace::planner_3d::CostCoeff::weight_costmap_
float weight_costmap_
Definition: planner_3d/grid_astar_model.h:57
planner_cspace::planner_3d::DistanceMap::gridmap
const Astar::Gridmap< float > & gridmap() const
Definition: distance_map.h:114
planner_cspace::GridAstar< 3, 2 >::Vec
CyclicVecInt< DIM, NONCYCLIC > Vec
Definition: grid_astar.h:67
planner_cspace::planner_3d::DistanceMap::init
void init(const GridAstarModel3D::Ptr model, const Params &p)
Definition: distance_map.cpp:198
planner_cspace::planner_3d::DistanceMapTest::validate
bool validate(const std::string &msg) const
Definition: test_distance_map_fast_update.cpp:149
planner_cspace::planner_3d::DistanceMap::setParams
void setParams(const CostCoeff &cc, const int num_cost_estim_task)
Definition: distance_map.cpp:192
planner_cspace::planner_3d::DistanceMap::Rect
Definition: distance_map.h:52
planner_cspace::planner_3d::DistanceMapTest
Definition: test_distance_map.cpp:54
planner_cspace::planner_3d::Vec3
CyclicVecInt< 3, 2 > Vec3
Definition: test_distance_map.cpp:51
planner_cspace::planner_3d::CostmapBBF::Ptr
std::shared_ptr< CostmapBBF > Ptr
Definition: costmap_bbf.h:47
planner_cspace::planner_3d::DistanceMap::Params::euclid_cost
Astar::Vecf euclid_cost
Definition: distance_map.h:72
planner_cspace::planner_3d::DistanceMapTest::h_
const int h_
Definition: test_distance_map.cpp:58
planner_cspace::CyclicVecBase
Definition: cyclic_vec.h:78


planner_cspace
Author(s): Atsushi Watanabe
autogenerated on Fri May 16 2025 02:15:23