regular_squares_grid_test.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
3 #include <memory>
4 #include <vector>
5 #include <algorithm>
6 #include <limits>
7 
8 #include "../../../src/core/maps/regular_squares_grid.h"
9 
10 //============================================================================//
11 //=== Tests ===//
12 //============================================================================//
13 
14 class RSGSegmentRasterizationTest : public ::testing::Test {
15 protected:
16  static constexpr double Grid_Scale = 0.1;
17 public:
18  RSGSegmentRasterizationTest() : grid{100, 100, Grid_Scale} {}
19 
20 protected: // methods
21 
23  return grid.cell_to_world(pnt);
24  }
25 
26 protected: // fields
28 };
29 
30 using DSegment = std::vector<DiscretePoint2D>;
31 
32 //--------------------------//
33 // === Segment -> Cells === //
34 
36  ASSERT_EQ(DSegment({{7, 5}}),
37  grid.world_to_cells({{0.75, 0.56}, {0.75, 0.56}}));
38 }
39 
41  ASSERT_EQ(DSegment({{7, 5}}),
42  grid.world_to_cells({{0.71, 0.53}, {0.75, 0.51}}));
43 }
44 
45 // 7x7 square, lines to each cell on edge, cw
46 
48  ASSERT_EQ(DSegment({{0, 0}, {0, 1}, {0, 2}, {0, 3}}),
49  grid.world_to_cells({cell_middle({0, 0}), cell_middle({0, 3})}));
50 }
51 
53  ASSERT_EQ(DSegment({{0, 0}, {0, 1}, {1, 2}, {1, 3}}),
54  grid.world_to_cells({cell_middle({0, 0}), cell_middle({1, 3})}));
55 }
56 
58  ASSERT_EQ(DSegment({{0, 0}, {0, 1}, {1, 1}, {1, 2}, {2, 2}, {2, 3}}),
59  grid.world_to_cells({cell_middle({0, 0}), cell_middle({2, 3})}));
60 }
61 
63  ASSERT_EQ(DSegment({{0, 0}, {1, 1}, {2, 2}, {3, 3}}),
64  grid.world_to_cells({cell_middle({0, 0}), cell_middle({3, 3})}));
65 }
66 
68  ASSERT_EQ(DSegment({{0, 0}, {1, 0}, {1, 1}, {2, 1}, {2, 2}, {3, 2}}),
69  grid.world_to_cells({cell_middle({0, 0}), cell_middle({3, 2})}));
70 }
71 
73  ASSERT_EQ(DSegment({{0, 0}, {1, 0}, {2, 1}, {3, 1}}),
74  grid.world_to_cells({cell_middle({0, 0}), cell_middle({3, 1})}));
75 }
76 
78  ASSERT_EQ(DSegment({{0, 0}, {1, 0}, {2, 0}, {3, 0}}),
79  grid.world_to_cells({cell_middle({0, 0}), cell_middle({3, 0})}));
80 }
81 
83  ASSERT_EQ(DSegment({{0, 0}, {1, 0}, {2, -1}, {3, -1}}),
84  grid.world_to_cells({cell_middle({0, 0}), cell_middle({3, -1})}));
85 }
86 
88  ASSERT_EQ(DSegment({{0, 0}, {1, 0}, {1, -1}, {2, -1}, {2, -2}, {3, -2}}),
89  grid.world_to_cells({cell_middle({0, 0}), cell_middle({3, -2})}));
90 }
91 
93  ASSERT_EQ(DSegment({{0, 0}, {1, -1}, {2, -2}, {3, -3}}),
94  grid.world_to_cells({cell_middle({0, 0}), cell_middle({3, -3})}));
95 }
96 
98  ASSERT_EQ(DSegment({{0, 0}, {0, -1}, {1, -1}, {1, -2}, {2, -2}, {2, -3}}),
99  grid.world_to_cells({cell_middle({0, 0}), cell_middle({2, -3})}));
100 }
101 
103  ASSERT_EQ(DSegment({{0, 0}, {0, -1}, {1, -2}, {1, -3}}),
104  grid.world_to_cells({cell_middle({0, 0}), cell_middle({1, -3})}));
105 }
106 
108  ASSERT_EQ(DSegment({{0, 0}, {0, -1}, {0, -2}, {0, -3}}),
109  grid.world_to_cells({cell_middle({0, 0}), cell_middle({0, -3})}));
110 }
111 
113  ASSERT_EQ(DSegment({{0, 0}, {0, -1}, {-1, -2}, {-1, -3}}),
114  grid.world_to_cells({cell_middle({0, 0}), cell_middle({-1, -3})}));
115 }
116 
118  ASSERT_EQ(DSegment({{0, 0}, {0, -1}, {-1, -1}, {-1, -2}, {-2, -2}, {-2, -3}}),
119  grid.world_to_cells({cell_middle({0, 0}), cell_middle({-2, -3})}));
120 }
121 
123  ASSERT_EQ(DSegment({{0, 0}, {-1, -1}, {-2, -2}, {-3, -3}}),
124  grid.world_to_cells({cell_middle({0, 0}), cell_middle({-3, -3})}));
125 }
126 
128  ASSERT_EQ(DSegment({{0, 0}, {-1, 0}, {-1, -1}, {-2, -1}, {-2, -2}, {-3, -2}}),
129  grid.world_to_cells({cell_middle({0, 0}), cell_middle({-3, -2})}));
130 }
131 
133  ASSERT_EQ(DSegment({{0, 0}, {-1, 0}, {-2, -1}, {-3, -1}}),
134  grid.world_to_cells({cell_middle({0, 0}), cell_middle({-3, -1})}));
135 }
136 
138  ASSERT_EQ(DSegment({{0, 0}, {-1, 0}, {-2, 0}, {-3, 0}}),
139  grid.world_to_cells({cell_middle({0, 0}), cell_middle({-3, 0})}));
140 }
141 
143  ASSERT_EQ(DSegment({{0, 0}, {-1, 0}, {-2, 1}, {-3, 1}}),
144  grid.world_to_cells({cell_middle({0, 0}), cell_middle({-3, 1})}));
145 }
146 
148  ASSERT_EQ(DSegment({{0, 0}, {-1, 0}, {-1, 1}, {-2, 1}, {-2, 2}, {-3, 2}}),
149  grid.world_to_cells({cell_middle({0, 0}), cell_middle({-3, 2})}));
150 }
151 
153  ASSERT_EQ(DSegment({{0, 0}, {-1, 1}, {-2, 2}, {-3, 3}}),
154  grid.world_to_cells({cell_middle({0, 0}), cell_middle({-3, 3})}));
155 }
156 
158  ASSERT_EQ(DSegment({{0, 0}, {0, 1}, {-1, 1}, {-1, 2}, {-2, 2}, {-2, 3}}),
159  grid.world_to_cells({cell_middle({0, 0}), cell_middle({-2, 3})}));
160 }
161 
163  ASSERT_EQ(DSegment({{0, 0}, {0, 1}, {-1, 2}, {-1, 3}}),
164  grid.world_to_cells({cell_middle({0, 0}), cell_middle({-1, 3})}));
165 }
166 
167 // horiz
169  ASSERT_EQ(DSegment({{7, 5}, {8, 5}, {9, 5}, {10, 5}}),
170  grid.world_to_cells({{0.71, 0.53}, {1.02, 0.53}}));
171 }
172 
174  ASSERT_EQ(DSegment({{-4, -6}, {-3, -6}, {-2, -6}, {-1, -6}}),
175  grid.world_to_cells({{-0.37, -0.56}, {-0.05, -0.51}}));
176 }
177 
179  ASSERT_EQ(DSegment({{7, 5}, {8, 5}, {9, 5}, {10, 5}}),
180  grid.world_to_cells({{0.71, 0.57}, {1.02, 0.52}}));
181 }
182 
184  ASSERT_EQ(DSegment({{10, 5}, {9, 5}, {8, 5}, {7, 5}}),
185  grid.world_to_cells({{1.02, 0.53}, {0.71, 0.53}}));
186 }
187 
188 //vert
190  ASSERT_EQ(DSegment({{7, 5}, {7, 6}, {7, 7}, {7, 8}}),
191  grid.world_to_cells({{0.73, 0.52}, {0.73, 0.85}}));
192 }
193 
195  ASSERT_EQ(DSegment({{7, 5}, {7, 6}, {7, 7}, {7, 8}}),
196  grid.world_to_cells({{0.77, 0.51}, {0.73, 0.87}}));
197 }
198 
200  ASSERT_EQ(DSegment({{7, 5}, {7, 6}, {7, 7}, {7, 8}}),
201  grid.world_to_cells({{0.71, 0.51}, {0.75, 0.83}}));
202 }
203 
205  ASSERT_EQ(DSegment({{7, 8}, {7, 7}, {7, 6}, {7, 5}}),
206  grid.world_to_cells({{0.73, 0.89}, {0.73, 0.52}}));
207 }
208 
210  ASSERT_EQ(DSegment({{0, 0}, {0, 1}, {0, 2}, {0, 3},
211  {1, 3}, {1, 4}, {1, 5}, {1, 6}}),
212  grid.world_to_cells({cell_middle({0, 0}), cell_middle({1, 6})}));
213 }
214 
215 //diag
217  ASSERT_EQ(grid.world_to_cells({{0.51, 0.71}, {0.81, 1.01}}),
218  DSegment({{5, 7}, {6, 8}, {7, 9}, {8, 10}}));
219 }
220 
222  ASSERT_EQ(grid.world_to_cells({{0.81, 1.01}, {0.51, 0.71}}),
223  DSegment({{8, 10}, {7, 9}, {6, 8}, {5, 7}}));
224 }
225 
227  ASSERT_EQ(grid.world_to_cells({{-0.12, -0.17}, {0.17, 0.12}}),
228  DSegment({{-2, -2}, {-1, -2}, {-1, -1},
229  {0, -1}, {0, 0}, {1, 0}, {1, 1}}));
230 }
231 
233  ASSERT_EQ(grid.world_to_cells({{0.17, 0.12}, {-0.12, -0.17}}),
234  DSegment({{1, 1}, {1, 0}, {0, 0},
235  {0, -1}, {-1, -1}, {-1, -2}, {-2, -2}}));
236 }
237 
239  ASSERT_EQ(grid.world_to_cells({{-0.17, -0.12}, {0.12, 0.17}}),
240  DSegment({{-2, -2}, {-2, -1}, {-1, -1},
241  {-1, 0}, {0, 0}, {0, 1}, {1, 1}}));
242 }
243 
245  ASSERT_EQ(grid.world_to_cells({{0.12, 0.17}, {-0.17, -0.12}}),
246  DSegment({{1, 1}, {0, 1}, {0, 0},
247  {-1, 0}, {-1, -1}, {-2, -1}, {-2, -2}}));
248 }
249 
250 TEST_F(RSGSegmentRasterizationTest, diagFwdThroughCenter) {
251  ASSERT_EQ(grid.world_to_cells({{-0.17, -0.12}, {0.17, 0.12}}),
252  DSegment({{-2, -2}, {-2, -1}, {-1, -1},
253  {0, 0}, {1, 0}, {1, 1}}));
254 }
255 
256 TEST_F(RSGSegmentRasterizationTest, diagBwdThroughCenter) {
257  ASSERT_EQ(grid.world_to_cells({{0.17, 0.12}, {-0.17, -0.12},}),
258  DSegment({{1, 1}, {1, 0}, {0, 0},
259  {-1, -1}, {-2, -1}, {-2, -2}}));
260 }
261 
262 // coincide with coordinate line
263 
264 TEST_F(RSGSegmentRasterizationTest, horizontalLineAlinedFwd) {
265  ASSERT_EQ(grid.world_to_cells({{0, 0}, {0, 0.45}}),
266  DSegment({{0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}}));
267 }
268 
269 TEST_F(RSGSegmentRasterizationTest, horizontalLineAlinedBwd) {
270  ASSERT_EQ(grid.world_to_cells({{0, 0}, {0, -0.35}}),
271  DSegment({{0, 0}, {0, -1}, {0, -2}, {0, -3}, {0, -4}}));
272 }
273 
274 TEST_F(RSGSegmentRasterizationTest, verticalLineAlinedFwd) {
275  ASSERT_EQ(grid.world_to_cells({{0, 0}, {0.45, 0}}),
276  DSegment({{0, 0}, {1, 0}, {2, 0}, {3, 0}, {4, 0}}));
277 }
278 
279 TEST_F(RSGSegmentRasterizationTest, verticalLineAlinedBwd) {
280  ASSERT_EQ(grid.world_to_cells({{0, 0}, {-0.35, 0}}),
281  DSegment({{0, 0}, {-1, 0}, {-2, 0}, {-3, 0}, {-4, 0}}));
282 }
283 
284 //============================================================================//
285 
286 class RSGInfinityScalingTest : public ::testing::Test {
287 protected:
288  static_assert(std::numeric_limits<double>::has_infinity,
289  "Infinity is not defined for doubles.");
290  static constexpr double Inf = std::numeric_limits<double>::infinity();
291 protected:
292  RSGInfinityScalingTest() : grid{1, 1, Inf} {}
293 
294  void test_grid_access(double x, double y) {
295  ASSERT_EQ(DiscretePoint2D(0, 0), grid.world_to_cell(x, y));
296  }
297 protected: // fields
299 };
300 
302  test_grid_access(0, 0);
303  test_grid_access(std::numeric_limits<double>::max(),
304  std::numeric_limits<double>::max());
305  /* FIXME
306  test_grid_access(std::numeric_limits<double>::infinity(),
307  std::numeric_limits<double>::infinity());
308  */
309 }
310 
312  test_grid_access(-5, 3);
313  test_grid_access(std::numeric_limits<double>::lowest(),
314  std::numeric_limits<double>::max());
315  /* FIXME
316  test_grid_access(-std::numeric_limits<double>::infinity(),
317  std::numeric_limits<double>::infinity());
318  */
319 }
320 
322  test_grid_access(-5, -3);
323  test_grid_access(std::numeric_limits<double>::lowest(),
324  std::numeric_limits<double>::lowest());
325  /* FIXME
326  test_grid_access(-std::numeric_limits<double>::infinity(),
327  -std::numeric_limits<double>::infinity());
328  */
329 }
330 
332  test_grid_access(5, -3);
333  test_grid_access(std::numeric_limits<double>::max(),
334  std::numeric_limits<double>::lowest());
335  /* FIXME
336  test_grid_access(std::numeric_limits<double>::infinity(),
337  -std::numeric_limits<double>::infinity());
338  */
339 }
340 
341 //============================================================================//
342 //============================================================================//
343 //============================================================================//
344 
345 int main (int argc, char *argv[]) {
346  ::testing::InitGoogleTest(&argc, argv);
347  return RUN_ALL_TESTS();
348 }
int main(int argc, char *argv[])
std::vector< DiscretePoint2D > DSegment
Coord world_to_cell(const Point2D &pt) const
Point2D cell_to_world(const Coord &cell) const
std::vector< Coord > world_to_cells(const Segment2D &s) const
TEST_F(RSGSegmentRasterizationTest, samePoint)
void test_grid_access(double x, double y)
Point2D cell_middle(const DiscretePoint2D &pnt)


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