SubmapIteratorTest.cpp
Go to the documentation of this file.
1 /*
2  * SubmapIteratorTest.cpp
3  *
4  * Created on: Sep 15, 2014
5  * Author: Péter Fankhauser
6  * Institute: ETH Zurich, ANYbotics
7  */
8 
11 
12 // Eigen
13 #include <Eigen/Core>
14 
15 // gtest
16 #include <gtest/gtest.h>
17 
18 // Limits
19 #include <cfloat>
20 
21 // Vector
22 #include <vector>
23 
24 using namespace std;
25 using namespace Eigen;
26 using namespace grid_map;
27 
28 TEST(SubmapIterator, Simple) {
29  Eigen::Array2i submapTopLeftIndex(3, 1);
30  Eigen::Array2i submapBufferSize(3, 2);
31  Eigen::Array2i index;
32  Eigen::Array2i submapIndex;
33 
34  vector<string> types;
35  types.push_back("type");
36  GridMap map(types);
37  map.setGeometry(Array2d(8.1, 5.1), 1.0, Vector2d(0.0, 0.0)); // bufferSize(8, 5)
38 
39  SubmapIterator iterator(map, submapTopLeftIndex, submapBufferSize);
40 
41  EXPECT_FALSE(iterator.isPastEnd());
42  EXPECT_EQ(submapTopLeftIndex(0), (*iterator)(0));
43  EXPECT_EQ(submapTopLeftIndex(1), (*iterator)(1));
44  EXPECT_EQ(0, iterator.getSubmapIndex()(0));
45  EXPECT_EQ(0, iterator.getSubmapIndex()(1));
46 
47  ++iterator;
48  EXPECT_FALSE(iterator.isPastEnd());
49  EXPECT_EQ(3, (*iterator)(0));
50  EXPECT_EQ(2, (*iterator)(1));
51  EXPECT_EQ(0, iterator.getSubmapIndex()(0));
52  EXPECT_EQ(1, iterator.getSubmapIndex()(1));
53 
54  ++iterator;
55  EXPECT_FALSE(iterator.isPastEnd());
56  EXPECT_EQ(4, (*iterator)(0));
57  EXPECT_EQ(1, (*iterator)(1));
58  EXPECT_EQ(1, iterator.getSubmapIndex()(0));
59  EXPECT_EQ(0, iterator.getSubmapIndex()(1));
60 
61  ++iterator;
62  EXPECT_FALSE(iterator.isPastEnd());
63  EXPECT_EQ(4, (*iterator)(0));
64  EXPECT_EQ(2, (*iterator)(1));
65  EXPECT_EQ(1, iterator.getSubmapIndex()(0));
66  EXPECT_EQ(1, iterator.getSubmapIndex()(1));
67 
68  ++iterator;
69  EXPECT_FALSE(iterator.isPastEnd());
70  EXPECT_EQ(5, (*iterator)(0));
71  EXPECT_EQ(1, (*iterator)(1));
72  EXPECT_EQ(2, iterator.getSubmapIndex()(0));
73  EXPECT_EQ(0, iterator.getSubmapIndex()(1));
74 
75  ++iterator;
76  EXPECT_FALSE(iterator.isPastEnd());
77  EXPECT_EQ(5, (*iterator)(0));
78  EXPECT_EQ(2, (*iterator)(1));
79  EXPECT_EQ(2, iterator.getSubmapIndex()(0));
80  EXPECT_EQ(1, iterator.getSubmapIndex()(1));
81 
82  ++iterator;
83  EXPECT_TRUE(iterator.isPastEnd());
84  EXPECT_EQ(5, (*iterator)(0));
85  EXPECT_EQ(2, (*iterator)(1));
86  EXPECT_EQ(2, iterator.getSubmapIndex()(0));
87  EXPECT_EQ(1, iterator.getSubmapIndex()(1));
88 }
89 
90 TEST(SubmapIterator, CircularBuffer) {
91  Eigen::Array2i submapTopLeftIndex(6, 3);
92  Eigen::Array2i submapBufferSize(2, 4);
93  Eigen::Array2i index;
94  Eigen::Array2i submapIndex;
95 
96  vector<string> types;
97  types.push_back("type");
98  GridMap map(types);
99  map.setGeometry(Length(8.1, 5.1), 1.0, Position(0.0, 0.0)); // bufferSize(8, 5)
100  map.move(Position(-3.0, -2.0)); // bufferStartIndex(3, 2)
101 
102  SubmapIterator iterator(map, submapTopLeftIndex, submapBufferSize);
103 
104  EXPECT_FALSE(iterator.isPastEnd());
105  EXPECT_EQ(submapTopLeftIndex(0), (*iterator)(0));
106  EXPECT_EQ(submapTopLeftIndex(1), (*iterator)(1));
107  EXPECT_EQ(0, iterator.getSubmapIndex()(0));
108  EXPECT_EQ(0, iterator.getSubmapIndex()(1));
109 
110  ++iterator;
111  EXPECT_FALSE(iterator.isPastEnd());
112  EXPECT_EQ(6, (*iterator)(0));
113  EXPECT_EQ(4, (*iterator)(1));
114  EXPECT_EQ(0, iterator.getSubmapIndex()(0));
115  EXPECT_EQ(1, iterator.getSubmapIndex()(1));
116 
117  ++iterator;
118  EXPECT_FALSE(iterator.isPastEnd());
119  EXPECT_EQ(6, (*iterator)(0));
120  EXPECT_EQ(0, (*iterator)(1));
121  EXPECT_EQ(0, iterator.getSubmapIndex()(0));
122  EXPECT_EQ(2, iterator.getSubmapIndex()(1));
123 
124  ++iterator;
125  EXPECT_FALSE(iterator.isPastEnd());
126  EXPECT_EQ(6, (*iterator)(0));
127  EXPECT_EQ(1, (*iterator)(1));
128  EXPECT_EQ(0, iterator.getSubmapIndex()(0));
129  EXPECT_EQ(3, iterator.getSubmapIndex()(1));
130 
131  ++iterator;
132  EXPECT_FALSE(iterator.isPastEnd());
133  EXPECT_EQ(7, (*iterator)(0));
134  EXPECT_EQ(3, (*iterator)(1));
135  EXPECT_EQ(1, iterator.getSubmapIndex()(0));
136  EXPECT_EQ(0, iterator.getSubmapIndex()(1));
137 
138  ++iterator;
139  EXPECT_FALSE(iterator.isPastEnd());
140  EXPECT_EQ(7, (*iterator)(0));
141  EXPECT_EQ(4, (*iterator)(1));
142  EXPECT_EQ(1, iterator.getSubmapIndex()(0));
143  EXPECT_EQ(1, iterator.getSubmapIndex()(1));
144 
145  ++iterator;
146  EXPECT_FALSE(iterator.isPastEnd());
147  EXPECT_EQ(7, (*iterator)(0));
148  EXPECT_EQ(0, (*iterator)(1));
149  EXPECT_EQ(1, iterator.getSubmapIndex()(0));
150  EXPECT_EQ(2, iterator.getSubmapIndex()(1));
151 
152  ++iterator;
153  EXPECT_FALSE(iterator.isPastEnd());
154  EXPECT_EQ(7, (*iterator)(0));
155  EXPECT_EQ(1, (*iterator)(1));
156  EXPECT_EQ(1, iterator.getSubmapIndex()(0));
157  EXPECT_EQ(3, iterator.getSubmapIndex()(1));
158 
159  ++iterator;
160  EXPECT_TRUE(iterator.isPastEnd());
161  EXPECT_EQ(7, (*iterator)(0));
162  EXPECT_EQ(1, (*iterator)(1));
163  EXPECT_EQ(1, iterator.getSubmapIndex()(0));
164  EXPECT_EQ(3, iterator.getSubmapIndex()(1));
165 }
166 
195 TEST(SubmapIterator, InterleavedExecutionWithMove) {
196  grid_map::Index submapTopLeftIndex(3, 1);
197  grid_map::Size submapSize(2, 2);
198 
199  GridMap map({"layer"});
200 
201  map.setGeometry(Length(10, 10), 1.0, Position(0.0, 0.0)); // bufferSize(8, 5)
202 
203  auto& layer = map.get("layer");
204 
205  // Initialize the layer as sketched.
206  for (size_t colIndex = 0; colIndex < layer.cols(); colIndex++) {
207  layer.col(colIndex).setConstant(colIndex);
208  }
209 
210  std::cout << "(4,7) contains " << map.at("layer", {4, 7}) << std::endl;
211  // Instantiate the submap iterator as sketched.
212  SubmapIterator iterator(map, submapTopLeftIndex, submapSize);
213 
214  // check that the submap iterator returns {1,1,2,2}
215  auto checkCorrectValues = [](std::array<double, 4> given) {
216  int countOnes = 0, countTwos = 0;
217  for (auto& value : given) {
218  if (std::abs(value - 1.0) < 1e-6) {
219  countOnes++;
220  } else if (std::abs(value - 2.0) < 1e-6) {
221  countTwos++;
222  } else {
223  FAIL() << "Submap iterator returned unexpected value.";
224  }
225  }
226  EXPECT_EQ(countOnes, 2);
227  EXPECT_EQ(countTwos, 2);
228  };
229 
230  std::array<double, 4> returnedSequence;
231  returnedSequence.fill(0);
232 
233  for (size_t submapIndex = 0; submapIndex < 4; submapIndex++) {
234  returnedSequence.at(submapIndex) = map.at("layer", *iterator);
235  ++iterator;
236  }
237 
238  checkCorrectValues(returnedSequence);
239 
240  // Reset the iterator and now check that it still returns the same sequence when we move the map interleaved with iterating.
241  iterator = SubmapIterator(map, submapTopLeftIndex, submapSize);
242  returnedSequence.fill(0);
243  for (size_t submapIndex = 0; submapIndex < 4; submapIndex++) {
244  if (submapIndex == 2) {
245  // Now move the map as depicted.
246  map.move(Position(2.0, 2.0));
247  }
248  returnedSequence.at(submapIndex) = map.at("layer", *iterator);
249  ++iterator;
250  }
251  checkCorrectValues(returnedSequence);
252 
253  // TODO (mwulf, mgaertner): This behavior is not yet implemented:
254  //
255  // // Reset the iterator and now check that the iterator throws? if the submap moved out of range.
256  // iterator = SubmapIterator(map, submapTopLeftIndex, submapSize);
257  //
258  // EXPECT_ANY_THROW(for (size_t submapIndex = 0; submapIndex < 4; submapIndex++) {
259  // if (submapIndex == 2) {
260  // // Now move the map so that the submap gets out of range.
261  // map.move(Position(20.0, 20.0));
262  // }
263  // returnedSequence.at(submapIndex) = map.at("layer", *iterator);
264  // ++iterator;
265  // });
266 }
Eigen::Array2i Index
Definition: TypeDefs.hpp:22
void setGeometry(const Length &length, const double resolution, const Position &position=Position::Zero())
Definition: GridMap.cpp:45
Eigen::Array2i Size
Definition: TypeDefs.hpp:23
TEST(SubmapIterator, Simple)
bool move(const Position &position, std::vector< BufferRegion > &newRegions)
Definition: GridMap.cpp:451
Eigen::Vector2d Position
Definition: TypeDefs.hpp:18
const Index & getSubmapIndex() const
Eigen::Array2d Length
Definition: TypeDefs.hpp:24


grid_map_core
Author(s): Péter Fankhauser
autogenerated on Tue Jun 1 2021 02:13:27