rescalable_caching_grid_map_test.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
3 #include <limits>
4 #include <memory>
5 #include <cmath>
6 
7 #include "../mock_grid_cell.h"
8 
9 #include "../../../src/core/maps/grid_rasterization.h"
10 #include "../../../src/core/maps/rescalable_caching_grid_map.h"
11 #include "../../../src/core/maps/plain_grid_map.h"
12 #include "../../../src/core/maps/lazy_tiled_grid_map.h"
13 
14 constexpr double Default_Occupancy_Prob = -42.0;
15 
16 //------------------------------------------------------------------------------
17 
18 class RescalableCachingGridMapTest : public ::testing::Test {
19 protected:
20  template <typename BackMapType = UnboundedPlainGridMap>
22 protected: // methods
24  : cell_proto{std::make_shared<MockGridCell>(Default_Occupancy_Prob)} {}
25 protected:
26 
27  template <int W, int H, int ExpectedCoarsestScaleId>
29  auto map = TesteeMapType<UnboundedPlainGridMap>{cell_proto, {W, H, 1}};
30  ASSERT_EQ(ExpectedCoarsestScaleId, map.coarsest_scale_id());
31  }
32 
33  void smoke_map_init(GridMap &map) {
34  auto val = AreaOccupancyObservation{true, Occupancy{0, 0},
35  Point2D{0, 0}, 1};
36 
37  auto coord = DiscretePoint2D{0, 0};
38  for (coord.x = 0; coord.x < map.width(); ++coord.x) {
39  for (coord.y = 0; coord.y < map.height(); ++coord.y) {
40  map.update(map.internal2external(coord), val);
41  assert(are_equal(map[map.internal2external(coord)],
42  val.occupancy.prob_occ));
43  val.occupancy.prob_occ += 1;
44  }
45  }
46  }
47 
48  double estimate_max_value(const GridMap &map, const Rectangle &area) const {
49  double expected = std::numeric_limits<double>::lowest();
50  for (auto &coord : GridRasterizedRectangle{map, area, false}.to_vector()) {
51  expected = std::max(double(map[coord]), expected);
52  }
53 
54  return expected;
55  }
56 
57  template<typename T, int Width, int Height>
59  auto map = TesteeMapType<T>{cell_proto, {Width, Height, 1}};
60  map.set_scale_id(map.finest_scale_id());
61  smoke_map_init(map);
62  // read values
63  int scale = 1;
64  for (unsigned scale_id = map.finest_scale_id();
65  scale_id <= map.coarsest_scale_id(); ++scale_id) {
66  map.set_scale_id(scale_id);
67  auto raw_crd = DiscretePoint2D{0, 0};
68  for (raw_crd.x = 0; raw_crd.x < map.width(); ++raw_crd.x) {
69  for (raw_crd.y = 0; raw_crd.y < map.height(); ++raw_crd.y) {
70  auto coord = map.internal2external(raw_crd);
71  auto area = map.world_cell_bounds(coord);
72  map.set_scale_id(map.finest_scale_id());
73  double expected = estimate_max_value(map, area);
74  map.set_scale_id(scale_id);
75  ASSERT_EQ(expected, double(map[coord]));
76  }
77  }
78  scale *= map.Map_Scale_Factor;;
79  }
80  }
81 
82 protected: // fields
83  std::shared_ptr<GridCell> cell_proto;
84 };
85 
86 //------------------------------------------------------------------------------
87 // Estimated scales number estimation on init
88 
89 TEST_F(RescalableCachingGridMapTest, initPowerOf2MapSize) {
90  test_max_scale_id_estimation<16, 16, 4>();
91 }
92 
94  test_max_scale_id_estimation<17, 17, 5>();
95 }
96 
98  test_max_scale_id_estimation<13, 8, 4>();
99 }
100 
102  test_max_scale_id_estimation<3, 15, 4>();
103 }
104 
105 //------------------------------------------------------------------------------
106 // Approximated Maps access
107 
108 TEST_F(RescalableCachingGridMapTest, dimensionsManualScaleSwitching) {
109  unsigned prev_w = 32, prev_h = 32;
110  auto map = TesteeMapType<>{cell_proto, {16, 16, 1}};
111 
112  for (unsigned scale_id = map.finest_scale_id();
113  scale_id <= map.coarsest_scale_id(); ++scale_id) {
114  map.set_scale_id(scale_id);
115  ASSERT_LT(map.width(), prev_w);
116  ASSERT_LT(map.height(), prev_h);
117  prev_w = map.width();
118  prev_h = map.height();
119  }
120  ASSERT_EQ(prev_w, 1);
121  ASSERT_EQ(prev_h, 1);
122 }
123 
124 TEST_F(RescalableCachingGridMapTest, theCoarsestMapSmokeCheck) {
125  auto map = TesteeMapType<>{cell_proto, {16, 16, 1}};
126  map.set_scale_id(map.coarsest_scale_id());
127 
128  ASSERT_EQ(DiscretePoint2D(0, 0), map.world_to_cell(100000, 100000));
129  ASSERT_EQ(DiscretePoint2D(0, 0), map.world_to_cell(-100000, 100000));
130  ASSERT_EQ(DiscretePoint2D(0, 0), map.world_to_cell(-100000, -100000));
131  ASSERT_EQ(DiscretePoint2D(0, 0), map.world_to_cell(100000, -100000));
132 }
133 
134 
135 //------------------------------------------------------------------------------
136 // Map reading writing
137 
139  auto map = TesteeMapType<>{cell_proto, {3, 15, 1}};
140  for (unsigned scale_id = map.finest_scale_id();
141  scale_id <= map.coarsest_scale_id(); ++scale_id) {
142  map.set_scale_id(scale_id);
143  auto coord = DiscretePoint2D{0, 0};
144  for (; coord.x < map.width(); ++coord.x) {
145  for (; coord.y < map.height(); ++coord.y) {
146  ASSERT_EQ(double(map[map.internal2external(coord)]),
148  }
149  }
150  }
151 }
152 
153 //------------------------------------------------------------------------------
154 // Approximations update
155 
157  test_read_write_inside_map<UnboundedPlainGridMap, 16, 16>();
158 }
159 
161  test_read_write_inside_map<UnboundedPlainGridMap, 16, 1>();
162 }
163 
165  test_read_write_inside_map<UnboundedPlainGridMap, 1, 16>();
166 }
167 
169  test_read_write_inside_map<UnboundedPlainGridMap, 15, 15>();
170 }
171 
173  test_read_write_inside_map<UnboundedPlainGridMap, 33, 33>();
174 }
175 
177  test_read_write_inside_map<UnboundedPlainGridMap, 15, 3>();
178 }
179 
181  test_read_write_inside_map<UnboundedPlainGridMap, 3, 15>();
182 }
183 
184 TEST_F(RescalableCachingGridMapTest, readWriteUPGM_1000x1000) {
185  test_read_write_inside_map<UnboundedPlainGridMap, 1000, 1000>();
186 }
187 
188 TEST_F(RescalableCachingGridMapTest, readWriteULTGM_1000x1000) {
189  test_read_write_inside_map<UnboundedLazyTiledGridMap, 1000, 1000>();
190 }
191 
192 TEST_F(RescalableCachingGridMapTest, approximationLevelExtension) {
193  auto map = TesteeMapType<>{cell_proto, {16, 16, 1}};
194  map.set_scale_id(map.finest_scale_id());
195  ASSERT_EQ(map.coarsest_scale_id(), 4);
196 
197  // init map
198  auto val = AreaOccupancyObservation{true, Occupancy{128, 0},
199  Point2D{0, 0}, 1};
200  {
201  auto updated_point = Point2D{15, 15};
202  map.update(map.world_to_cell(updated_point), val);
203  ASSERT_EQ(map.coarsest_scale_id(), 5);
204  }
205 
206  {
207  auto updated_point = Point2D{16, 16};
208  map.update(map.world_to_cell(updated_point), val);
209  ASSERT_EQ(map.coarsest_scale_id(), 6);
210  }
211 }
212 
213 int main (int argc, char *argv[]) {
214  ::testing::InitGoogleTest(&argc, argv);
215  return RUN_ALL_TESTS();
216 }
void update(const Coord &area_id, const AreaOccupancyObservation &aoo) override
Updates area with a given observation.
Definition: grid_map.h:41
constexpr double Default_Occupancy_Prob
CONSTEXPR bool are_equal(const T &a, const T &b, const T &eps)
Definition: math_utils.h:17
double estimate_max_value(const GridMap &map, const Rectangle &area) const
Coord internal2external(const Coord &coord) const
int main(int argc, char *argv[])
virtual int height() const
double occupancy(const Coord &area_id) const override
Returns known information about the occupancy of a given area.
Definition: grid_map.h:48
TEST_F(RescalableCachingGridMapTest, initPowerOf2MapSize)
virtual int width() const


slam_constructor
Author(s): JetBrains Research, OSLL team
autogenerated on Mon Jun 10 2019 15:08:25