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


planner_cspace
Author(s): Atsushi Watanabe
autogenerated on Mon Jul 3 2023 02:39:06