grid_map_patcher_test.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
3 #include <algorithm>
4 #include <iterator>
5 
6 #include "../../core/mock_grid_cell.h"
7 
8 #include "../../../src/utils/data_generation/map_primitives.h"
9 #include "../../../src/utils/data_generation/grid_map_patcher.h"
10 #include "../../../src/core/maps/plain_grid_map.h"
11 
12 class GridMapPatcherTest : public ::testing::Test {
13 protected: // methods
15  : map{std::make_shared<MockGridCell>(),
17 
18 protected: // consts
19  static constexpr int Map_Width = 100;
20  static constexpr int Map_Height = 100;
21  static constexpr double Map_Scale = 0.1;
22 protected: // fields
23 
25  const DiscretePoint2D &offset,
26  int w_scale, int h_scale) {
27  assert(fs.top() <= 1 && fs.bot() <= 1 && 0 <= fs.left() && 0 <= fs.right());
28  double left = offset.x + fs.left() * w_scale;
29  double top = offset.y + (fs.top() - 1) * h_scale + 1;
30  return Rectangle{top - fs.vside_len() * h_scale, top,
31  left, left + fs.hside_len() * w_scale};
32  }
33 
34  void test_patching(const TextRasterMapPrimitive &mp, int w_scale, int h_scale,
35  const DiscretePoint2D &patch_offset = {0, 0},
36  bool use_auto_offset = false) {
37  auto offset = patch_offset;
38  if (use_auto_offset) {
39  gm_patcher.apply_text_raster(map, mp.to_stream(), w_scale, h_scale);
40  offset = DiscretePoint2D{-mp.width() * w_scale / 2,
41  mp.height() * h_scale / 2};
42  } else {
44  offset, w_scale, h_scale);
45  }
46 
47  // free area bounds
48  std::vector<Rectangle> free_space = mp.free_space();
49  std::vector<Rectangle> free_areas;
50  std::transform(free_space.begin(), free_space.end(),
51  std::back_inserter(free_areas),
52  [&,this](const Rectangle &free_space) {
53  return scale_and_move_free_space(free_space, offset,
54  w_scale, h_scale);
55  });
56 
57  // check area bounds
58  const int Check_Left = offset.x - 1;
59  const int Check_Right = Check_Left + mp.width() * w_scale + 1;
60  const int Check_Top = offset.y + 1;
61  const int Check_Bot = Check_Top - mp.height() * h_scale - 1;
62 
63  DiscretePoint2D coord;
64  for (coord.y = Check_Top; Check_Bot <= coord.y; --coord.y) {
65  for (coord.x = Check_Left; coord.x <= Check_Right; ++coord.x) {
66  // std::cout << map[coord] << " ";
67  if (coord.x == Check_Left || coord.x == Check_Right ||
68  coord.y == Check_Top || coord.y == Check_Bot) {
69  ASSERT_EQ(MockGridCell::Default_Occ_Prob, map[coord]);
70  continue;
71  }
72 
73  auto c_mid = Point2D{coord.x + map.scale()/2, coord.y + map.scale()/2};
74  auto is_free = true;
75  for (auto &fa : free_areas) {
76  is_free &= fa.contains(c_mid);
77  if (!is_free) { break; }
78  }
79  double expected_cell_value = is_free ? 0.0 : 1.0;
80  ASSERT_EQ(expected_cell_value, map[coord]);
81  }
82  //std::cout << std::endl;
83  }
84  }
85 
88 };
89 
90 //-- Scale
91 
92 TEST_F(GridMapPatcherTest, patchCecumNoScaleNoOffset) {
94  auto cecum_mp = CecumTextRasterMapPrimitive{7, 4, bnd_pos};
95  test_patching(cecum_mp, 1, 1, DiscretePoint2D{0, 0});
96 }
97 
98 
99 TEST_F(GridMapPatcherTest, patchCecumHorizScaleNoOffset) {
101  auto cecum_mp = CecumTextRasterMapPrimitive{7, 8, bnd_pos};
102  test_patching(cecum_mp, 2, 1);
103 }
104 
105 TEST_F(GridMapPatcherTest, patchCecumVertScaleNoOffset) {
107  auto cecum_mp = CecumTextRasterMapPrimitive{4, 3, bnd_pos};
108  test_patching(cecum_mp, 1, 3);
109 }
110 
111 TEST_F(GridMapPatcherTest, patchCecumEvenScaleNoOffset) {
113  auto cecum_mp = CecumTextRasterMapPrimitive{4, 9, bnd_pos};
114 
115  test_patching(cecum_mp, 8, 8);
116 }
117 
118 //-- Offset
119 
120 TEST_F(GridMapPatcherTest, patchCecumNoScaleLeftOffset) {
122  auto cecum_mp = CecumTextRasterMapPrimitive{4, 5, bnd_pos};
123 
124  test_patching(cecum_mp, 1, 1, DiscretePoint2D{-1, 0});
125 }
126 
127 TEST_F(GridMapPatcherTest, patchCecumNoScaleRightOffset) {
129  auto cecum_mp = CecumTextRasterMapPrimitive{4, 5, bnd_pos};
130 
131  test_patching(cecum_mp, 1, 1, DiscretePoint2D{3, 0});
132 }
133 
134 TEST_F(GridMapPatcherTest, patchCecumNoScaleDownOffset) {
136  auto cecum_mp = CecumTextRasterMapPrimitive{4, 5, bnd_pos};
137 
138  test_patching(cecum_mp, 1, 1, DiscretePoint2D{-6, 0});
139 }
140 
141 TEST_F(GridMapPatcherTest, patchCecumNoScaleUpOffset) {
143  auto cecum_mp = CecumTextRasterMapPrimitive{7, 5, bnd_pos};
144 
145  test_patching(cecum_mp, 1, 1, DiscretePoint2D{7, 0});
146 }
147 
148 TEST_F(GridMapPatcherTest, patchCecumNoScaleLeftUpOffset) {
150  auto cecum_mp = CecumTextRasterMapPrimitive{9, 5, bnd_pos};
151 
152  test_patching(cecum_mp, 1, 1, DiscretePoint2D{-5, 6});
153 }
154 
155 TEST_F(GridMapPatcherTest, patchCecumNoScaleLeftDownOffset) {
157  auto cecum_mp = CecumTextRasterMapPrimitive{16, 8, bnd_pos};
158 
159  test_patching(cecum_mp, 1, 1, DiscretePoint2D{-4, -1});
160 }
161 
162 TEST_F(GridMapPatcherTest, patchCecumNoScaleRightUpOffset) {
164  auto cecum_mp = CecumTextRasterMapPrimitive{4, 7, bnd_pos};
165 
166  test_patching(cecum_mp, 1, 1, DiscretePoint2D{3, 7});
167 }
168 
169 TEST_F(GridMapPatcherTest, patchCecumNoScaleRightDownOffset) {
171  auto cecum_mp = CecumTextRasterMapPrimitive{3, 5, bnd_pos};
172 
173  test_patching(cecum_mp, 1, 1, DiscretePoint2D{7, 5});
174 }
175 
176 TEST_F(GridMapPatcherTest, patchCecumUnevenScaleOffset) {
178  auto cecum_mp = CecumTextRasterMapPrimitive{4, 5, bnd_pos};
179 
180  test_patching(cecum_mp, 6, 4, DiscretePoint2D{11, 15});
181 }
182 
183 //-- Auto Offset
184 
185 TEST_F(GridMapPatcherTest, patchCecumEvenScaleAutoOffset) {
187  auto cecum_mp = CecumTextRasterMapPrimitive{7, 6, bnd_pos};
188 
189  test_patching(cecum_mp, 7, 7, {}, true);
190 }
191 
192 TEST_F(GridMapPatcherTest, patchCecumUnevenScaleAutoOffset) {
194  auto cecum_mp = CecumTextRasterMapPrimitive{6, 7, bnd_pos};
195 
196  test_patching(cecum_mp, 3, 8, {}, true);
197 }
198 
199 //------------------------------------------------------------------------------
200 
201 int main (int argc, char *argv[]) {
202  ::testing::InitGoogleTest(&argc, argv);
203  return RUN_ALL_TESTS();
204 }
static constexpr int Map_Height
virtual std::vector< Rectangle > free_space() const
std::istream & to_stream() const override
Rectangle scale_and_move_free_space(const Rectangle &fs, const DiscretePoint2D &offset, int w_scale, int h_scale)
static constexpr int Map_Width
virtual int width() const
static constexpr double Map_Scale
UnboundedPlainGridMap map
void apply_text_raster(MapType &dst_map, std::istream &src_raster, const DiscretePoint2D &dst_offset, int w_zoom=1, int h_zoom=1)
static constexpr double Default_Occ_Prob
Definition: mock_grid_cell.h:8
int main(int argc, char *argv[])
TEST_F(GridMapPatcherTest, patchCecumNoScaleNoOffset)
virtual int height() const
virtual double scale() const
void test_patching(const TextRasterMapPrimitive &mp, int w_scale, int h_scale, const DiscretePoint2D &patch_offset={0, 0}, bool use_auto_offset=false)


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