GridMapCvProcessingTest.cpp
Go to the documentation of this file.
1 /*
2  * GridMapCvProcessingTest.cpp
3  *
4  * Created on: May 3, 2017
5  * Author: Peter Fankhauser
6  */
7 
8 #include <chrono>
9 #include <cv_bridge/cv_bridge.h>
12 #include <gtest/gtest.h>
13 
15 
16 using namespace std;
17 using namespace grid_map;
18 
19 TEST(GridMapCvProcessing, changeResolution) {
20  // Create grid map.
21  GridMap mapIn;
22  mapIn.setGeometry(grid_map::Length(2.0, 1.0), 0.01);
23  mapIn.add("layer", 1.0);
24  for (grid_map::CircleIterator iterator(mapIn, mapIn.getPosition(), 0.2); !iterator.isPastEnd(); ++iterator) {
25  mapIn.at("layer", *iterator) = 2.0;
26  }
27 
28  // Change resolution.
29  GridMap mapOut;
30  EXPECT_TRUE(GridMapCvProcessing::changeResolution(mapIn, mapOut, 0.1));
31 
32  // Check data.
33  EXPECT_TRUE((mapIn.getLength() == mapOut.getLength()).all());
34  EXPECT_TRUE(mapIn.getPosition() == mapOut.getPosition());
35  EXPECT_TRUE((mapIn.getSize() == mapOut.getSize() * 10).all());
36  EXPECT_EQ(mapIn["layer"](0, 0), mapOut["layer"](0, 0)); // Corner.
37  EXPECT_EQ(mapIn.atPosition("layer", mapIn.getPosition()), mapOut.atPosition("layer", mapOut.getPosition())); // Center.
38 }
39 
40 TEST(GridMapCvProcessing, changeResolutionForMovedMap) {
41  // Create grid map.
42  GridMap mapIn;
43  mapIn.setGeometry(grid_map::Length(2.0, 1.0), 0.01);
44  Position position(0.3, 0.4);
45  mapIn.move(position);
46  mapIn.add("layer", 1.0);
47  for (grid_map::CircleIterator iterator(mapIn, position, 0.2); !iterator.isPastEnd(); ++iterator) {
48  mapIn.at("layer", *iterator) = 2.0;
49  }
50 
51  // Change resolution.
52  GridMap mapOut;
53  EXPECT_TRUE(GridMapCvProcessing::changeResolution(mapIn, mapOut, 0.1));
54 
55  // Check data.
56  EXPECT_TRUE((mapIn.getLength() == mapOut.getLength()).all());
57  EXPECT_TRUE(mapIn.getPosition() == mapOut.getPosition());
58  EXPECT_TRUE((mapIn.getSize() == mapOut.getSize() * 10).all());
59  EXPECT_EQ(mapIn["layer"](0, 0), mapOut["layer"](0, 0)); // Corner.
60  EXPECT_EQ(mapIn.atPosition("layer", mapIn.getPosition()), mapOut.atPosition("layer", mapOut.getPosition())); // Center.
61 }
62 
76 TEST(GridMap, TransformRotate90) {
77  // Initial map.
78  GridMap map;
79  const auto heightLayerName = "height";
80 
81  map.setGeometry(Length(1.0, 2.0), 0.1, Position(0.0, 0.0));
82  map.add(heightLayerName, 0.0);
83  map.setBasicLayers(map.getLayers());
84  map.get(heightLayerName)(0, 0) = 1.0;
85 
86  // Transformation (90° rotation).
87  Eigen::Isometry3d transform = Eigen::Translation3d(0, 0, 0) * Eigen::AngleAxisd(M_PI / 2, Vector3::UnitZ());
88 
89  // Apply affine transformation.
90  const GridMap transformedMap = GridMapCvProcessing::getTransformedMap(GridMap(map), transform, heightLayerName, map.getFrameId());
91 
92  // Check if map has been rotated by 90° about z
93  // Check dimensions.
94  EXPECT_NEAR(map.getLength().x(), transformedMap.getLength().y(), 1e-6);
95  EXPECT_NEAR(map.getLength().y(), transformedMap.getLength().x(), 1e-6);
96  EXPECT_EQ(map.get(heightLayerName).size(), transformedMap.get(heightLayerName).size());
97 
98  // Check if marker was rotated.
99  EXPECT_DOUBLE_EQ(map.get(heightLayerName)(0, 0), transformedMap.get(heightLayerName)(19, 0));
100  EXPECT_DOUBLE_EQ(transformedMap.get(heightLayerName)(19, 0), 1.0);
101 
102  // Check map position.
103  EXPECT_DOUBLE_EQ(map.getPosition().x(), transformedMap.getPosition().x());
104  EXPECT_DOUBLE_EQ(map.getPosition().y(), transformedMap.getPosition().y());
105 }
106 
120 TEST(GridMap, TransformRotate180) {
121  // Initial map.
122  GridMap map;
123  const auto heightLayerName = "height";
124 
125  map.setGeometry(Length(1.0, 2.0), 0.1, Position(0.0, 0.0));
126  map.add(heightLayerName, 0.0);
127  map.setBasicLayers(map.getLayers());
128  map.get(heightLayerName)(0, 0) = 1.0;
129 
130  // Transformation (180° rotation).
131  Eigen::Isometry3d transform = Eigen::Translation3d(0, 0, 0) * Eigen::AngleAxisd(M_PI, Vector3::UnitZ());
132 
133  // Apply affine transformation.
134  const GridMap transformedMap = GridMapCvProcessing::getTransformedMap(GridMap(map), transform, heightLayerName, map.getFrameId());
135 
136  // Check if map has been rotated by 180° about z
137 
138  // Check dimensions.
139  EXPECT_NEAR(map.getLength().x(), transformedMap.getLength().x(), 1e-6);
140  EXPECT_NEAR(map.getLength().y(), transformedMap.getLength().y(), 1e-6);
141  EXPECT_EQ(map.get(heightLayerName).size(), transformedMap.get(heightLayerName).size());
142 
143  // Check if marker was rotated.
144  EXPECT_DOUBLE_EQ(map.get(heightLayerName)(0, 0), transformedMap.get(heightLayerName)(9, 19));
145  EXPECT_DOUBLE_EQ(transformedMap.get(heightLayerName)(9, 19), 1.0);
146 
147  // Check map position.
148  EXPECT_DOUBLE_EQ(map.getPosition().x(), transformedMap.getPosition().x());
149  EXPECT_DOUBLE_EQ(map.getPosition().y(), transformedMap.getPosition().y());
150 }
151 
165 TEST(GridMap, TransformRotate270) {
166  // Initial map.
167  GridMap map;
168  const auto heightLayerName = "height";
169 
170  map.setGeometry(Length(1.0, 2.0), 0.1, Position(0.0, 0.0));
171  map.add(heightLayerName, 0.0);
172  map.setBasicLayers(map.getLayers());
173  map.get(heightLayerName)(0, 0) = 1.0;
174 
175  // Transformation (270° rotation).
176  Eigen::Isometry3d transform = Eigen::Translation3d(0, 0, 0) * Eigen::AngleAxisd(-M_PI / 2, Vector3::UnitZ());
177 
178  // Apply affine transformation.
179  const GridMap transformedMap = GridMapCvProcessing::getTransformedMap(GridMap(map), transform, heightLayerName, map.getFrameId());
180 
181  // Check if map has been rotated by 270° about z
182 
183  // Check dimensions.
184  EXPECT_NEAR(map.getLength().x(), transformedMap.getLength().y(), 1e-6);
185  EXPECT_NEAR(map.getLength().y(), transformedMap.getLength().x(), 1e-6);
186  EXPECT_EQ(map.get(heightLayerName).size(), transformedMap.get(heightLayerName).size());
187 
188  // Check if marker was rotated.
189  EXPECT_DOUBLE_EQ(map.get(heightLayerName)(0, 0), transformedMap.get(heightLayerName)(0, 9));
190  EXPECT_DOUBLE_EQ(transformedMap.get(heightLayerName)(0, 9), 1.0);
191 
192  // Check map position.
193  EXPECT_DOUBLE_EQ(map.getPosition().x(), transformedMap.getPosition().x());
194  EXPECT_DOUBLE_EQ(map.getPosition().y(), transformedMap.getPosition().y());
195 }
196 
197 
211 TEST(GridMap, TransformRotate360) {
212  // Initial map.
213  GridMap map;
214  const auto heightLayerName = "height";
215 
216  map.setGeometry(Length(1.0, 2.0), 0.1, Position(0.0, 0.0));
217  map.add(heightLayerName, 0.0);
218  map.setBasicLayers(map.getLayers());
219  map.get(heightLayerName)(0, 0) = 1.0;
220 
221  // Transformation (360° rotation).
222  Eigen::Isometry3d transform = Eigen::Translation3d(0, 0, 0) * Eigen::AngleAxisd(M_PI * 2, Vector3::UnitZ());
223 
224  // Apply affine transformation.
225  const GridMap transformedMap = GridMapCvProcessing::getTransformedMap(GridMap(map), transform, heightLayerName, map.getFrameId());
226 
227  // Check if map has been rotated by 360° about z
228 
229  // Check dimensions
230  EXPECT_NEAR(map.getLength().x(), transformedMap.getLength().x(), 1e-6);
231  EXPECT_NEAR(map.getLength().y(), transformedMap.getLength().y(), 1e-6);
232  EXPECT_EQ(map.get(heightLayerName).size(), transformedMap.get(heightLayerName).size());
233 
234  // Check if marker was rotated.
235  EXPECT_DOUBLE_EQ(map.get(heightLayerName)(0, 0), transformedMap.get(heightLayerName)(0, 0));
236  EXPECT_DOUBLE_EQ(transformedMap.get(heightLayerName)(0, 0), 1.0);
237 
238  // Check map position.
239  EXPECT_DOUBLE_EQ(map.getPosition().x(), transformedMap.getPosition().x());
240  EXPECT_DOUBLE_EQ(map.getPosition().y(), transformedMap.getPosition().y());
241 }
242 
265 TEST(GridMap, TransformRotate45) {
266  // Initial map.
267  GridMap map;
268  const auto heightLayerName = "height";
269  double resolution = 0.02;
270  map.setGeometry(Length(1.0, 2.0), resolution, Position(0.0, 0.0));
271  map.add(heightLayerName, 1.0);
272  map.setBasicLayers(map.getLayers());
273 
274  // Transformation (45° rotation).
275  Eigen::Isometry3d transform = Eigen::Translation3d(0, 0, 0) * Eigen::AngleAxisd(M_PI / 4, Vector3::UnitZ());
276 
277  // Apply affine transformation.
278  const GridMap transformedMap = GridMapCvProcessing::getTransformedMap(GridMap(map), transform, heightLayerName, map.getFrameId());
279 
280  // Check if map has been rotated by 45° about z
281 
282  // Check dimensions.
283  EXPECT_NEAR(transformedMap.getLength().x(), 2.121, resolution);
284  EXPECT_NEAR(transformedMap.getLength().y(), 2.121, resolution);
285 
286  // Check that filled area stays the same.
287  EXPECT_TRUE(transformedMap.get(heightLayerName).hasNaN());
288  EXPECT_NEAR(transformedMap.get(heightLayerName).sumOfFinites() / map.get(heightLayerName).size(), 1.0, 1e-2);
289 }
290 
291 TEST(GridMap, TransformTranslationXZY) {
292  // Initial map.
293  GridMap map;
294  const auto heightLayerName = "height";
295  const auto otherLayerName = "other_layer";
296  map.setGeometry(Length(1.0, 2.0), 0.1, Position(0.0, 0.0));
297  map.add(heightLayerName, 0.0);
298  map.add(otherLayerName, 0.0);
299  map.setBasicLayers(map.getLayers());
300 
301  // Translation by (1, -0.5, 0.2):
302  Eigen::Isometry3d transform = Eigen::Translation3d(1, -0.5, 0.2) * Eigen::AngleAxisd(0, Vector3::UnitZ());
303 
304  // Apply affine transformation.
305  const GridMap transformedMap = GridMapCvProcessing::getTransformedMap(GridMap(map), transform, heightLayerName, map.getFrameId());
306 
307  // Check if map has still the same size
308  EXPECT_NEAR(map.getLength().x(), transformedMap.getLength().x(), 1e-6);
309  EXPECT_NEAR(map.getLength().y(), transformedMap.getLength().y(), 1e-6);
310  EXPECT_EQ(map.get(heightLayerName).size(), transformedMap.get(heightLayerName).size());
311 
312  // Check if position was updated.
313  EXPECT_NEAR(transformedMap.getPosition().x(), transform.translation().x(), 1e-6);
314  EXPECT_NEAR(transformedMap.getPosition().y(), transform.translation().y(), 1e-6);
315 
316  // Check if height values were updated.
317  EXPECT_NEAR(map.get(heightLayerName)(0, 0) + transform.translation().z(), transformedMap.get(heightLayerName)(0, 0), 1e-6);
318 
319  // Check that other layers were kept as before.
320  EXPECT_NEAR(map.get(otherLayerName)(0, 0), transformedMap.get(otherLayerName)(0, 0), 1e-6);
321 }
322 
323 TEST(GridMap, TransformUnaligned) {
324  // Initial map.
325  GridMap map;
326  const auto heightLayerName = "height";
327  map.setGeometry(Length(1.0, 2.0), 0.1, Position(0.0, 0.0));
328  map.add(heightLayerName, 0.0);
329  map.setBasicLayers(map.getLayers());
330 
331  // A small pitch rotation by 22.5°
332  Eigen::Isometry3d transform = Eigen::Translation3d(0, 0, 0) * Eigen::AngleAxisd(M_PI / 8, Vector3::UnitY());
333 
334  // Check that this unaligned transformation is not accepted.
335  ASSERT_ANY_THROW(const GridMap transformedMap =
336  GridMapCvProcessing::getTransformedMap(GridMap(map), transform, heightLayerName, map.getFrameId()));
337 }
338 
348 TEST(GridMap, TransformComputationTime) {
349  // Initial map.
350  GridMap map;
351  const auto heightLayerName = "height";
352  map.setGeometry(Length(4.0, 4.0), 0.02, Position(0.0, 0.0));
353  map.add(heightLayerName, 0.0);
354  map.setBasicLayers(map.getLayers());
355 
356  // Translation by (1, -0.5, 0.2):
357  Eigen::Isometry3d transform = Eigen::Translation3d(1, -0.5, 0.2) * Eigen::AngleAxisd(M_PI / 4, Vector3::UnitZ());
358 
359  // Setup timing metrics.
360  long sumTimes = 0;
361  long averageTime = 0;
362  long minTime = std::numeric_limits<long>::max();
363  long maxTime = std::numeric_limits<long>::lowest();
364 
365  // Benchmark the function.
366  size_t numberOfSamples = 100;
367  for (size_t sampleNumber = 0; sampleNumber < numberOfSamples; sampleNumber++) {
368  auto timestampBefore = std::chrono::high_resolution_clock::now();
369  const GridMap transformedMap = GridMapCvProcessing::getTransformedMap(GridMap(map), transform, heightLayerName, map.getFrameId());
370  auto timestampAfter = std::chrono::high_resolution_clock::now();
371 
372  long time = std::chrono::duration_cast<std::chrono::nanoseconds>(timestampAfter - timestampBefore).count();
373  sumTimes += time;
374  if (time < minTime) {
375  minTime = time;
376  }
377  if (time > maxTime) {
378  maxTime = time;
379  }
380  }
381  averageTime = sumTimes / numberOfSamples;
382 
383  std::cout << "Transforming a 2 x 2 m GridMap with a resolution of 2 cm by 45° took: " << std::endl
384  << "avg: " << averageTime * 1e-6f << "ms, \tmin: " << minTime * 1e-6f << "ms, \tmax " << maxTime * 1e-6f << "ms." << std::endl;
385 }
const Length & getLength() const
void setGeometry(const Length &length, const double resolution, const Position &position=Position::Zero())
float & atPosition(const std::string &layer, const Position &position)
f
bool getPosition(const Index &index, Position &position) const
void setBasicLayers(const std::vector< std::string > &basicLayers)
const std::string & getFrameId() const
bool move(const Position &position, std::vector< BufferRegion > &newRegions)
const std::vector< std::string > & getLayers() const
Eigen::Vector2d Position
const Matrix & get(const std::string &layer) const
float & at(const std::string &layer, const Index &index)
void add(const std::string &layer, const double value=NAN)
TEST(GridMapCvProcessing, changeResolution)
Eigen::Array2d Length
const Size & getSize() const


grid_map_cv
Author(s): Péter Fankhauser , Magnus Gärtner
autogenerated on Tue Jun 1 2021 02:13:32