12 #include <gtest/gtest.h> 17 GridMap map({
"layer_a",
"layer_b"});
19 map[
"layer_a"].setConstant(1.0);
20 map[
"layer_b"].setConstant(2.0);
22 EXPECT_EQ(map.getSize()[0], mapCopy.
getSize()[0]);
23 EXPECT_EQ(map.getSize()[1], mapCopy.
getSize()[1]);
24 EXPECT_EQ(map.getLength().x(), mapCopy.
getLength().x());
25 EXPECT_EQ(map.getLength().y(), mapCopy.
getLength().y());
26 EXPECT_EQ(map.getPosition().x(), mapCopy.
getPosition().x());
27 EXPECT_EQ(map.getPosition().y(), mapCopy.
getPosition().y());
28 EXPECT_EQ(map.getLayers().size(), mapCopy.
getLayers().size());
29 EXPECT_EQ(map[
"layer_a"](0, 0), mapCopy[
"layer_a"](0, 0));
30 EXPECT_EQ(map[
"layer_b"](0, 0), mapCopy[
"layer_b"](0, 0));
35 GridMap map({
"layer_a",
"layer_b"});
37 map[
"layer_a"].setConstant(1.0);
38 map[
"layer_b"].setConstant(2.0);
41 EXPECT_EQ(map.getSize()[0], mapCopy.
getSize()[0]);
42 EXPECT_EQ(map.getSize()[1], mapCopy.
getSize()[1]);
43 EXPECT_EQ(map.getLength().x(), mapCopy.
getLength().x());
44 EXPECT_EQ(map.getLength().y(), mapCopy.
getLength().y());
45 EXPECT_EQ(map.getPosition().x(), mapCopy.
getPosition().x());
46 EXPECT_EQ(map.getPosition().y(), mapCopy.
getPosition().y());
47 EXPECT_EQ(map.getLayers().size(), mapCopy.
getLayers().size());
48 EXPECT_EQ(map[
"layer_a"](0, 0), mapCopy[
"layer_a"](0, 0));
49 EXPECT_EQ(map[
"layer_b"](0, 0), mapCopy[
"layer_b"](0, 0));
56 map.
add(
"layer", 0.0);
58 std::vector<BufferRegion> regions;
62 EXPECT_EQ(3, startIndex(0));
63 EXPECT_EQ(2, startIndex(1));
65 Eigen::Matrix<bool, 8, 5> isValidExpected;
66 isValidExpected <<
false,
false,
false,
false,
false,
67 false,
false,
false,
false,
false,
68 false,
false,
false,
false,
false,
69 false,
false,
true,
true,
true,
70 false,
false,
true,
true,
true,
71 false,
false,
true,
true,
true,
72 false,
false,
true,
true,
true,
73 false,
false,
true,
true,
true;
74 for(
int row{0}; row < 8; row++){
75 for(
int col{0}; col < 5; col++){
76 EXPECT_EQ(map.
isValid(
Index(row, col)), isValidExpected(row, col)) <<
"Value of map.isValid at ["<<row <<
", " << col <<
"] is unexpected!";
80 EXPECT_EQ(2, regions.size());
81 EXPECT_EQ(0, regions[0].getStartIndex()[0]);
82 EXPECT_EQ(0, regions[0].getStartIndex()[1]);
83 EXPECT_EQ(3, regions[0].getSize()[0]);
84 EXPECT_EQ(5, regions[0].getSize()[1]);
85 EXPECT_EQ(0, regions[1].getStartIndex()[0]);
86 EXPECT_EQ(0, regions[1].getStartIndex()[1]);
87 EXPECT_EQ(8, regions[1].getSize()[0]);
88 EXPECT_EQ(2, regions[1].getSize()[1]);
95 constexpr
auto heightLayerName =
"height";
98 map.
add(heightLayerName, 0.0);
100 map.
get(heightLayerName)(0,0) = 1.0;
103 Eigen::Isometry3d transform;
105 transform.translation().x() = 0.0;
106 transform.translation().y() = 0.0;
107 transform.translation().z() = 0.0;
109 transform.linear()(0,0) = 0.0;
110 transform.linear()(0,1) = -1.0;
111 transform.linear()(0,2) = 0.0;
113 transform.linear()(1,0) = 1.0;
114 transform.linear()(1,1) = 0.0;
115 transform.linear()(1,2) = 0.0;
117 transform.linear()(2,0) = 0.0;
118 transform.linear()(2,1) = 0.0;
119 transform.linear()(2,2) = 1.0;
127 EXPECT_EQ(map.
get(heightLayerName).size(), transformedMap.
get(heightLayerName).size());
128 EXPECT_DOUBLE_EQ(map.
get(heightLayerName)(0,0), transformedMap.
get(heightLayerName)(19,0));
133 GridMap map({
"layer_a",
"layer_b"});
135 map[
"layer_a"].setConstant(1.0);
136 map[
"layer_b"].setConstant(2.0);
141 const Position clippedPositionInMap = map.getClosestPositionInMap(positionInMap);
142 const Position clippedPositionOutMap = map.getClosestPositionInMap(positionOutMap);
145 EXPECT_NEAR(clippedPositionInMap.x(),positionInMap.x(), 1e-6);
146 EXPECT_NEAR(clippedPositionInMap.y(), positionInMap.y(), 1e-6);
149 EXPECT_TRUE(!map.isInside(positionOutMap));
152 EXPECT_TRUE(map.isInside(clippedPositionOutMap));
183 outsidePosition =
Position(1.0, 1.0);
184 auto closestInsidePosition = map.getClosestPositionInMap(outsidePosition);
185 bool isInside = map.getIndex(closestInsidePosition, insideIndex);
187 auto expectedPosition =
Position(0.5, 0.5);
188 auto expectedIndex =
Index(0, 0);
191 EXPECT_DOUBLE_EQ(expectedPosition.x(), closestInsidePosition.x());
192 EXPECT_DOUBLE_EQ(expectedPosition.y(), closestInsidePosition.y());
195 EXPECT_EQ(expectedIndex.x(), insideIndex.x()) <<
"closestInsidePosition" << closestInsidePosition;
196 EXPECT_EQ(expectedIndex.y(), insideIndex.y()) <<
"closestInsidePosition" << closestInsidePosition;
199 EXPECT_TRUE(isInside) <<
"position is: " << std::endl
200 << closestInsidePosition << std::endl
201 <<
" index is: " << std::endl
202 << insideIndex << std::endl;
205 outsidePosition =
Position(1.0, 0.0);
206 closestInsidePosition = map.getClosestPositionInMap(outsidePosition);
207 isInside = map.getIndex(closestInsidePosition, insideIndex);
209 expectedPosition =
Position(0.5, 0.0);
210 expectedIndex =
Index(0, 10);
213 EXPECT_DOUBLE_EQ(expectedPosition.x(), closestInsidePosition.x());
214 EXPECT_DOUBLE_EQ(expectedPosition.y(), closestInsidePosition.y());
217 EXPECT_EQ(expectedIndex.x(), insideIndex.x()) <<
"closestInsidePosition" << closestInsidePosition;
218 EXPECT_EQ(expectedIndex.y(), insideIndex.y()) <<
"closestInsidePosition" << closestInsidePosition;
221 EXPECT_TRUE(isInside) <<
"position is: " << std::endl
222 << closestInsidePosition << std::endl
223 <<
" index is: " << std::endl
224 << insideIndex << std::endl;
227 outsidePosition =
Position(1.0, -1.0);
228 closestInsidePosition = map.getClosestPositionInMap(outsidePosition);
229 isInside = map.getIndex(closestInsidePosition, insideIndex);
231 expectedPosition =
Position(0.5, -0.5);
232 expectedIndex =
Index(0, 19);
235 EXPECT_DOUBLE_EQ(expectedPosition.x(), closestInsidePosition.x());
236 EXPECT_DOUBLE_EQ(expectedPosition.y(), closestInsidePosition.y());
239 EXPECT_EQ(expectedIndex.x(), insideIndex.x()) <<
"closestInsidePosition" << closestInsidePosition;
240 EXPECT_EQ(expectedIndex.y(), insideIndex.y()) <<
"closestInsidePosition" << closestInsidePosition;
243 EXPECT_TRUE(isInside) <<
"position is: " << std::endl
244 << closestInsidePosition << std::endl
245 <<
" index is: " << std::endl
246 << insideIndex << std::endl;
249 outsidePosition =
Position(0.0, 1.0);
250 closestInsidePosition = map.getClosestPositionInMap(outsidePosition);
251 isInside = map.getIndex(closestInsidePosition, insideIndex);
253 expectedPosition =
Position(0.0, 0.5);
254 expectedIndex =
Index(10, 0);
257 EXPECT_DOUBLE_EQ(expectedPosition.x(), closestInsidePosition.x());
258 EXPECT_DOUBLE_EQ(expectedPosition.y(), closestInsidePosition.y());
261 EXPECT_EQ(expectedIndex.x(), insideIndex.x()) <<
"closestInsidePosition" << closestInsidePosition;
262 EXPECT_EQ(expectedIndex.y(), insideIndex.y()) <<
"closestInsidePosition" << closestInsidePosition;
265 EXPECT_TRUE(isInside) <<
"position is: " << std::endl
266 << closestInsidePosition << std::endl
267 <<
" index is: " << std::endl
268 << insideIndex << std::endl;
271 outsidePosition =
Position(0.0, -1.0);
272 closestInsidePosition = map.getClosestPositionInMap(outsidePosition);
273 isInside = map.getIndex(closestInsidePosition, insideIndex);
275 expectedPosition =
Position(0.0, -0.5);
276 expectedIndex =
Index(10, 19);
279 EXPECT_DOUBLE_EQ(expectedPosition.x(), closestInsidePosition.x());
280 EXPECT_DOUBLE_EQ(expectedPosition.y(), closestInsidePosition.y());
283 EXPECT_EQ(expectedIndex.x(), insideIndex.x()) <<
"closestInsidePosition" << closestInsidePosition;
284 EXPECT_EQ(expectedIndex.y(), insideIndex.y()) <<
"closestInsidePosition" << closestInsidePosition;
287 EXPECT_TRUE(isInside) <<
"position is: " << std::endl
288 << closestInsidePosition << std::endl
289 <<
" index is: " << std::endl
290 << insideIndex << std::endl;
293 outsidePosition =
Position(-1.0, 1.0);
294 closestInsidePosition = map.getClosestPositionInMap(outsidePosition);
295 isInside = map.getIndex(closestInsidePosition, insideIndex);
297 expectedPosition =
Position(-0.5, 0.5);
298 expectedIndex =
Index(19, 0);
301 EXPECT_DOUBLE_EQ(expectedPosition.x(), closestInsidePosition.x());
302 EXPECT_DOUBLE_EQ(expectedPosition.y(), closestInsidePosition.y());
305 EXPECT_EQ(expectedIndex.x(), insideIndex.x()) <<
"closestInsidePosition" << closestInsidePosition;
306 EXPECT_EQ(expectedIndex.y(), insideIndex.y()) <<
"closestInsidePosition" << closestInsidePosition;
309 EXPECT_TRUE(isInside) <<
"position is: " << std::endl
310 << closestInsidePosition << std::endl
311 <<
" index is: " << std::endl
312 << insideIndex << std::endl;
315 outsidePosition =
Position(-1.0, 0.0);
316 closestInsidePosition = map.getClosestPositionInMap(outsidePosition);
317 isInside = map.getIndex(closestInsidePosition, insideIndex);
319 expectedPosition =
Position(-0.5, 0.0);
320 expectedIndex =
Index(19, 10);
323 EXPECT_DOUBLE_EQ(expectedPosition.x(), closestInsidePosition.x());
324 EXPECT_DOUBLE_EQ(expectedPosition.y(), closestInsidePosition.y());
327 EXPECT_EQ(expectedIndex.x(), insideIndex.x()) <<
"closestInsidePosition" << closestInsidePosition;
328 EXPECT_EQ(expectedIndex.y(), insideIndex.y()) <<
"closestInsidePosition" << closestInsidePosition;
331 EXPECT_TRUE(isInside) <<
"position is: " << std::endl
332 << closestInsidePosition << std::endl
333 <<
" index is: " << std::endl
334 << insideIndex << std::endl;
337 outsidePosition =
Position(-1.0, -1.0);
338 closestInsidePosition = map.getClosestPositionInMap(outsidePosition);
339 isInside = map.getIndex(closestInsidePosition, insideIndex);
341 expectedPosition =
Position(-0.5, -0.5);
342 expectedIndex =
Index(19, 19);
345 EXPECT_DOUBLE_EQ(expectedPosition.x(), closestInsidePosition.x());
346 EXPECT_DOUBLE_EQ(expectedPosition.y(), closestInsidePosition.y());
349 EXPECT_EQ(expectedIndex.x(), insideIndex.x()) <<
"closestInsidePosition" << closestInsidePosition;
350 EXPECT_EQ(expectedIndex.y(), insideIndex.y()) <<
"closestInsidePosition" << closestInsidePosition;
353 EXPECT_TRUE(isInside) <<
"position is: " << std::endl
354 << closestInsidePosition << std::endl
355 <<
" index is: " << std::endl
356 << insideIndex << std::endl;
359 TEST(AddDataFrom, ExtendMapAligned)
364 map1.
add(
"zero", 0.0);
365 map1.
add(
"one", 1.0);
369 map2.
add(
"one", 1.1);
370 map2.
add(
"two", 2.0);
375 EXPECT_TRUE(map1.
exists(
"two"));
377 EXPECT_DOUBLE_EQ(6.0, map1.
getLength().x());
378 EXPECT_DOUBLE_EQ(6.0, map1.
getLength().y());
386 TEST(AddDataFrom, ExtendMapNotAligned)
392 map1.
add(
"one", 1.0);
393 map1.
add(
"zero", 0.0);
397 map2.
add(
"nan", 1.0);
398 map2.
add(
"one", 1.1);
399 map2.
add(
"two", 2.0);
402 std::vector<std::string> stringVector;
403 stringVector.emplace_back(
"nan");
404 map1.
addDataFrom(map2,
true,
false,
false, stringVector);
408 EXPECT_FALSE(map1.
exists(
"two"));
410 EXPECT_DOUBLE_EQ(8.0, map1.
getLength().x());
411 EXPECT_DOUBLE_EQ(8.0, map1.
getLength().y());
414 EXPECT_FALSE(map1.
isValid(index,
"nan"));
424 map1.
add(
"zero", 0.0);
429 map2.
add(
"one", 1.0);
430 map2.
add(
"two", 2.0);
437 EXPECT_TRUE(map1.
exists(
"two"));
439 EXPECT_DOUBLE_EQ(5.0, map1.
getLength().x());
440 EXPECT_DOUBLE_EQ(5.0, map1.
getLength().y());
444 EXPECT_FALSE(map1.
isValid(index,
"one"));
448 TEST(ValueAtPosition, NearestNeighbor)
453 map.at(
"types",
Index(0,0)) = 0.5;
454 map.at(
"types",
Index(0,1)) = 3.8;
455 map.at(
"types",
Index(0,2)) = 2.0;
456 map.at(
"types",
Index(1,0)) = 2.1;
457 map.at(
"types",
Index(1,1)) = 1.0;
458 map.at(
"types",
Index(1,2)) = 2.0;
459 map.at(
"types",
Index(2,0)) = 1.0;
460 map.at(
"types",
Index(2,1)) = 2.0;
461 map.at(
"types",
Index(2,2)) = 2.0;
463 double value = map.atPosition(
"types",
Position(1.35,-0.4));
464 EXPECT_DOUBLE_EQ((
float)3.8, value);
466 value = map.atPosition(
"types",
Position(-0.3,0.0));
467 EXPECT_DOUBLE_EQ(1.0, value);
470 TEST(ValueAtPosition, LinearInterpolated)
475 map.at(
"types",
Index(0,0)) = 0.5;
476 map.at(
"types",
Index(0,1)) = 3.8;
477 map.at(
"types",
Index(0,2)) = 2.0;
478 map.at(
"types",
Index(1,0)) = 2.1;
479 map.at(
"types",
Index(1,1)) = 1.0;
480 map.at(
"types",
Index(1,2)) = 2.0;
481 map.at(
"types",
Index(2,0)) = 1.0;
482 map.at(
"types",
Index(2,1)) = 2.0;
483 map.at(
"types",
Index(2,2)) = 2.0;
487 EXPECT_DOUBLE_EQ(2.0, value);
490 EXPECT_DOUBLE_EQ(1.5, value);
493 EXPECT_NEAR(2.1963200, value, 0.0000001);
void setGeometry(const Length &length, const double resolution, const Position &position=Position::Zero())
GridMap getTransformedMap(const Eigen::Isometry3d &transform, const std::string &heightLayerName, const std::string &newFrameId, const double sampleRatio=0.0) const
float & atPosition(const std::string &layer, const Position &position)
void setBasicLayers(const std::vector< std::string > &basicLayers)
const Index & getStartIndex() const
const Matrix & get(const std::string &layer) const
const Size & getSize() const
const std::vector< std::string > & getLayers() const
bool move(const Position &position, std::vector< BufferRegion > &newRegions)
bool isInside(const Position &position) const
bool addDataFrom(const GridMap &other, bool extendMap, bool overwriteData, bool copyAllLayers, std::vector< std::string > layers=std::vector< std::string >())
bool isValid(const Index &index) const
const std::string & getFrameId() const
void add(const std::string &layer, const double value=NAN)
bool exists(const std::string &layer) const
bool getIndex(const Position &position, Index &index) const
bool getPosition(const Index &index, Position &position) const
TEST(PositionFromIndex, Simple)
const Length & getLength() const