grid_rasterization_test.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
3 #include "../directions.h"
4 #include "../../../src/core/maps/grid_rasterization.h"
5 
6 //============================================================================//
7 //=== Tests ===//
8 //============================================================================//
9 
10 class RectangleGridRasterizationTest : public ::testing::Test {
11 protected:
12  static constexpr double Grid_Scale = 0.5; // use 0.5 to prevent FP erros
13  static constexpr double Cell_Len = Grid_Scale;
14  static constexpr double Half_Cell_Len = Cell_Len / 2;
15  static constexpr double Quat_Cell_Len = Half_Cell_Len / 2;
16 public:
17  RectangleGridRasterizationTest() : grid{100, 100, Grid_Scale} {}
18 
19 protected: // types
21  using AreaIds = std::set<AreaId>;
22 protected: // methods
23 
24  AreaIds area_to_ids(const Point2D &center, double side_len) {
25  auto half_side = side_len / 2;
26  auto rect = Rectangle{-half_side, half_side,
27  -half_side, half_side}.move_center(center);
28  auto raw_ids = GridRasterizedRectangle{grid, rect}.to_vector();
29  return AreaIds{raw_ids.begin(), raw_ids.end()};
30  }
31 
32  AreaIds area_to_ids(const Point2D &offset,
33  double bot_len, double top_len,
34  double left_len, double right_len) {
35  auto rect = Rectangle{-bot_len + offset.y, top_len + offset.y,
36  -left_len + offset.x, right_len + offset.x};
37  auto raw_ids = GridRasterizedRectangle{grid, rect}.to_vector();
38  return AreaIds{raw_ids.begin(), raw_ids.end()};
39  }
40 
41  /*
42  * ...................
43  * . . . .
44  * . . . .
45  * . @@@@@@@@@@@@@ .
46  * . @ . ^ . @ .
47  * . @ . | . @ .
48  * ...@.....|.....@...
49  * . @ . | . @ .
50  * . @ . *** . @ .
51  * . @<---* *--->@ .
52  * . @ . *** . @ .
53  * . @ . | . @ .
54  * ...@.....|.....@...
55  * . @ . | . @ .
56  * . @ . V . @ .
57  * . @@@@@@@@@@@@@ .
58  * . . . .
59  * . . . .
60  * ...................
61  */
62  void test_area_exceeding(const Directions &dirs) {
63  auto area_id = AreaId{};
64  for (area_id.x = -1; area_id.x <= 1; ++area_id.x) {
65  for (area_id.y = -1; area_id.y <= 1; ++area_id.y) {
66  // setup params
67  auto area_offset = grid.cell_to_world(area_id);
68  auto left = Quat_Cell_Len, right = Quat_Cell_Len,
69  top = Quat_Cell_Len, bot = Quat_Cell_Len;
70 
71  int expected_d_x_start = 0, expected_d_x_end = 0,
72  expected_d_y_start = 0, expected_d_y_end = 0;
73  if (dirs.left()) {
74  area_offset.x -= Quat_Cell_Len;
75  left += Half_Cell_Len;
76  expected_d_x_start = -1;
77  }
78  if (dirs.right()) {
79  area_offset.x += Quat_Cell_Len;
80  right += Half_Cell_Len;
81  expected_d_x_end = 1;
82  }
83  if (dirs.bot()) {
84  area_offset.y -= Quat_Cell_Len;
85  bot += Half_Cell_Len;
86  expected_d_y_start = -1;
87  }
88  if (dirs.top()) {
89  area_offset.y += Quat_Cell_Len;
90  top += Half_Cell_Len;
91  expected_d_y_end = 1;
92  }
93 
94  // generate expected
95  auto expected = AreaIds{};
96  for (int d_x = expected_d_x_start; d_x <= expected_d_x_end; ++d_x) {
97  for (int d_y = expected_d_y_start; d_y <= expected_d_y_end; ++d_y) {
98  expected.emplace(area_id.x + d_x, area_id.y + d_y);
99  }
100  }
101 
102  // generate actual
103  auto actual = area_to_ids(area_offset, bot, top, left, right);
104  // check area
105  ASSERT_EQ(expected, actual);
106  }
107  }
108  }
109 
110  /*
111  * . . . .
112  * . . . .
113  * ..@@@@@@@@@@@@@@@@@@@..
114  * @ . ^ . @
115  * @ . | . @
116  * @ ************* @
117  * @ * . . * @
118  * @ * . . * @
119  * ..@..*...........*..@..
120  * @ * . . * @
121  * @ * . . * @
122  * @<-* . . *->@
123  * @ * . . * @
124  * @ * . . * @
125  * ..@..*...........*..@..
126  * @ * . . * @
127  * @ * . . * @
128  * @ ************* @
129  * @ . | . @
130  * @ . V . @
131  * ..@@@@@@@@@@@@@@@@@@@..
132  * . . . .
133  * . . . .
134  */
135  void test_area_alignement(const Directions &dirs) {
136  auto area_id = AreaId{};
137  for (area_id.x = -1; area_id.x <= 1; ++area_id.x) {
138  for (area_id.y = -1; area_id.y <= 1; ++area_id.y) {
139  int d_x_limit = 1, d_y_limit = 1;
140  // generate expected
141  auto left = Cell_Len, right = Cell_Len, top = Cell_Len, bot = Cell_Len;
142  if (dirs.left()) { left += Half_Cell_Len; }
143  if (dirs.bot()) { bot += Half_Cell_Len; }
144 
145  if (dirs.right()) {
146  ++d_x_limit;
147  right += Half_Cell_Len;
148  }
149 
150  if (dirs.top()) {
151  ++d_y_limit;
152  top += Half_Cell_Len;
153  }
154 
155  auto expected = AreaIds{};
156  for (int d_x = -1; d_x <= d_x_limit; ++d_x) {
157  for (int d_y = -1; d_y <= d_y_limit; ++d_y) {
158  expected.emplace(area_id.x + d_x, area_id.y + d_y);
159  }
160  }
161 
162  // generate actual
163  auto actual = area_to_ids(grid.cell_to_world(area_id),
164  bot, top, left, right);
165  // check area
166  ASSERT_EQ(expected, actual);
167  }
168  }
169  }
170 
171 protected: // fields
173 };
174 
176  const RegularSquaresGrid::Coord &c2) {
177  if (c1.x == c2.x) { return c1.y < c2.y; }
178  return c1.x < c2.x;
179 }
180 
181 TEST_F(RectangleGridRasterizationTest, emptyAreaInsideCell) {
182  auto area_id = AreaId{};
183  for (area_id.x = -1; area_id.x <= 1; ++area_id.x) {
184  for (area_id.y = -1; area_id.y <= 1; ++area_id.y) {
185  ASSERT_EQ(area_to_ids({grid.cell_to_world(area_id)}, 0),
186  AreaIds{area_id});
187  }
188  }
189 }
190 
192  // left bot
193  ASSERT_EQ(area_to_ids({0, 0}, 0), (AreaIds{{0, 0}}));
194 
195  // left
196  ASSERT_EQ(area_to_ids({0, Half_Cell_Len}, 0), (AreaIds{{0, 0}}));
197 
198  // left top
199  ASSERT_EQ(area_to_ids({0, Cell_Len}, 0), (AreaIds{{0, 1}}));
200 
201  // top
202  ASSERT_EQ(area_to_ids({Half_Cell_Len, Cell_Len}, 0), (AreaIds{{0, 1}}));
203 
204  // right top
205  ASSERT_EQ(area_to_ids({Cell_Len, Cell_Len}, 0), (AreaIds{{1, 1}}));
206 
207  // right
208  ASSERT_EQ(area_to_ids({Cell_Len, Half_Cell_Len}, 0), (AreaIds{{1, 0}}));
209 
210  // right bot
211  ASSERT_EQ(area_to_ids({Cell_Len, 0}, 0), (AreaIds{{1, 0}}));
212 
213  // bot
214  ASSERT_EQ(area_to_ids({Half_Cell_Len, 0}, 0), (AreaIds{{0, 0}}));
215 }
216 
217 //----------------------------------------------------------------------------//
218 //--- An area exceeds a Cell ---//
219 
220 TEST_F(RectangleGridRasterizationTest, areaDoesNotExceedCell) {
222 }
223 
224 TEST_F(RectangleGridRasterizationTest, areaExceedsCellLeft) {
226 }
227 
228 TEST_F(RectangleGridRasterizationTest, areaExceedsCellRight) {
230 }
231 
232 TEST_F(RectangleGridRasterizationTest, areaExceedsCellRightLeft) {
234 }
235 
238 }
239 
240 TEST_F(RectangleGridRasterizationTest, areaExceedsCellTopLeft) {
242 }
243 
244 TEST_F(RectangleGridRasterizationTest, areaExceedsCellTopRight) {
246 }
247 
248 TEST_F(RectangleGridRasterizationTest, areaExceedsCellTopRightLeft) {
250 }
251 
254 }
255 
256 TEST_F(RectangleGridRasterizationTest, areaExceedsCellBotLeft) {
258 }
259 
260 TEST_F(RectangleGridRasterizationTest, areaExceedsCellBotRight) {
262 }
263 
264 TEST_F(RectangleGridRasterizationTest, areaExceedsCellBotRightLeft) {
266 }
267 
268 TEST_F(RectangleGridRasterizationTest, areaExceedsCellBotTop) {
270 }
271 
272 TEST_F(RectangleGridRasterizationTest, areaExceedsCellBotTopLeft) {
274 }
275 
276 TEST_F(RectangleGridRasterizationTest, areaExceedsCellBotTopRight) {
278 }
279 
280 TEST_F(RectangleGridRasterizationTest, areaExceedsCellBotTopRightLeft) {
282 }
283 
284 //----------------------------------------------------------------------------//
285 //--- An area is aligned with cell's border ---//
286 
288  // redundant, added for completeness
290 }
291 
294 }
295 
298 }
299 
300 TEST_F(RectangleGridRasterizationTest, areaAlignesRightLeft) {
302 }
303 
306 }
307 
310 }
311 
312 TEST_F(RectangleGridRasterizationTest, areaAlignesTopRight) {
314 }
315 
316 TEST_F(RectangleGridRasterizationTest, areaAlignesTopRightLeft) {
318 }
319 
322 }
323 
326 }
327 
328 TEST_F(RectangleGridRasterizationTest, areaAlignesBotRight) {
330 }
331 
332 TEST_F(RectangleGridRasterizationTest, areaAlignesBotRightLeft) {
334 }
335 
338 }
339 
340 TEST_F(RectangleGridRasterizationTest, areaAlignesBotTopLeft) {
342 }
343 
344 TEST_F(RectangleGridRasterizationTest, areaAlignesBotTopRight) {
346 }
347 
348 TEST_F(RectangleGridRasterizationTest, areaAlignesBotTopRightLeft) {
350 }
351 
352 //----------------------------------------------------------------------------//
353 
355  grid = RegularSquaresGrid{3, 2, 1};
356 
357  constexpr auto Inf = RegularSquaresGrid::Dbl_Inf;
358  auto actual = area_to_ids({0, 0}, Inf, Inf, Inf, Inf);
359  auto expected = std::set<RegularSquaresGrid::Coord>{
360  {-1, -1}, {-1, 0}, {0, -1}, {0, 0}, {1, -1}, {1, 0}};
361  ASSERT_EQ(expected, actual);
362 }
363 
364 //============================================================================//
365 
366 int main (int argc, char *argv[]) {
367  ::testing::InitGoogleTest(&argc, argv);
368  return RUN_ALL_TESTS();
369 }
AreaIds area_to_ids(const Point2D &offset, double bot_len, double top_len, double left_len, double right_len)
bool right() const
Definition: directions.h:17
static constexpr double Dbl_Inf
Directions & set_right()
Definition: directions.h:12
AreaIds area_to_ids(const Point2D &center, double side_len)
Point2D cell_to_world(const Coord &cell) const
LVRect move_center(const Point2D &new_center) const
bool bot() const
Definition: directions.h:19
bool left() const
Definition: directions.h:16
bool operator<(const RegularSquaresGrid::Coord &c1, const RegularSquaresGrid::Coord &c2)
Directions & set_left()
Definition: directions.h:11
void test_area_alignement(const Directions &dirs)
void test_area_exceeding(const Directions &dirs)
int main(int argc, char *argv[])
Directions & set_bot()
Definition: directions.h:14
bool top() const
Definition: directions.h:18
TEST_F(RectangleGridRasterizationTest, emptyAreaInsideCell)
Directions & set_top()
Definition: directions.h:13


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