GridMapTest.cpp
Go to the documentation of this file.
00001 /*
00002  * GridMapTest.cpp
00003  *
00004  *  Created on: Aug 26, 2015
00005  *      Author: Péter Fankhauser
00006  *       Institute: ETH Zurich, ANYbotics
00007  */
00008 
00009 #include "grid_map_core/GridMap.hpp"
00010 
00011 // gtest
00012 #include <gtest/gtest.h>
00013 
00014 // Math
00015 #include <math.h>
00016 
00017 using namespace std;
00018 using namespace grid_map;
00019 
00020 TEST(GridMap, CopyConstructor)
00021 {
00022   GridMap map({"layer_a", "layer_b"});
00023   map.setGeometry(Length(1.0, 2.0), 0.1, Position(0.1, 0.2));
00024   map["layer_a"].setConstant(1.0);
00025   map["layer_b"].setConstant(2.0);
00026   GridMap mapCopy(map);
00027   EXPECT_EQ(map.getSize()[0], mapCopy.getSize()[0]);
00028   EXPECT_EQ(map.getSize()[1], mapCopy.getSize()[1]);
00029   EXPECT_EQ(map.getLength().x(), mapCopy.getLength().x());
00030   EXPECT_EQ(map.getLength().y(), mapCopy.getLength().y());
00031   EXPECT_EQ(map.getPosition().x(), mapCopy.getPosition().x());
00032   EXPECT_EQ(map.getPosition().y(), mapCopy.getPosition().y());
00033   EXPECT_EQ(map.getLayers().size(), mapCopy.getLayers().size());
00034   EXPECT_EQ(map["layer_a"](0, 0), mapCopy["layer_a"](0, 0));
00035   EXPECT_EQ(map["layer_b"](0, 0), mapCopy["layer_b"](0, 0));
00036 }
00037 
00038 TEST(GridMap, CopyAssign)
00039 {
00040   GridMap map({"layer_a", "layer_b"});
00041   map.setGeometry(Length(1.0, 2.0), 0.1, Position(0.1, 0.2));
00042   map["layer_a"].setConstant(1.0);
00043   map["layer_b"].setConstant(2.0);
00044   GridMap mapCopy;
00045   mapCopy = map;
00046   EXPECT_EQ(map.getSize()[0], mapCopy.getSize()[0]);
00047   EXPECT_EQ(map.getSize()[1], mapCopy.getSize()[1]);
00048   EXPECT_EQ(map.getLength().x(), mapCopy.getLength().x());
00049   EXPECT_EQ(map.getLength().y(), mapCopy.getLength().y());
00050   EXPECT_EQ(map.getPosition().x(), mapCopy.getPosition().x());
00051   EXPECT_EQ(map.getPosition().y(), mapCopy.getPosition().y());
00052   EXPECT_EQ(map.getLayers().size(), mapCopy.getLayers().size());
00053   EXPECT_EQ(map["layer_a"](0, 0), mapCopy["layer_a"](0, 0));
00054   EXPECT_EQ(map["layer_b"](0, 0), mapCopy["layer_b"](0, 0));
00055 }
00056 
00057 TEST(GridMap, Move)
00058 {
00059   GridMap map;
00060   map.setGeometry(Length(8.1, 5.1), 1.0, Position(0.0, 0.0)); // bufferSize(8, 5)
00061   map.add("layer", 0.0);
00062   map.setBasicLayers(map.getLayers());
00063   std::vector<BufferRegion> regions;
00064   map.move(Position(-3.0, -2.0), regions);
00065   Index startIndex = map.getStartIndex();
00066 
00067   EXPECT_EQ(3, startIndex(0));
00068   EXPECT_EQ(2, startIndex(1));
00069 
00070   EXPECT_FALSE(map.isValid(Index(0, 0))); // TODO Check entire map.
00071   EXPECT_TRUE(map.isValid(Index(3, 2)));
00072   EXPECT_FALSE(map.isValid(Index(2, 2)));
00073   EXPECT_FALSE(map.isValid(Index(3, 1)));
00074   EXPECT_TRUE(map.isValid(Index(7, 4)));
00075 
00076   EXPECT_EQ(2, regions.size());
00077   EXPECT_EQ(0, regions[0].getStartIndex()[0]);
00078   EXPECT_EQ(0, regions[0].getStartIndex()[1]);
00079   EXPECT_EQ(3, regions[0].getSize()[0]);
00080   EXPECT_EQ(5, regions[0].getSize()[1]);
00081   EXPECT_EQ(0, regions[1].getStartIndex()[0]);
00082   EXPECT_EQ(0, regions[1].getStartIndex()[1]);
00083   EXPECT_EQ(8, regions[1].getSize()[0]);
00084   EXPECT_EQ(2, regions[1].getSize()[1]);
00085 }
00086 
00087 TEST(GridMap, Transform)
00088 {
00089   // Initial map.
00090   GridMap map;
00091   const auto heightLayerName = "height";
00092 
00093   map.setGeometry(Length(1.0, 2.0), 0.1, Position(0.0, 0.0));
00094   map.add(heightLayerName, 0.0);
00095   map.setBasicLayers(map.getLayers());
00096   map.get(heightLayerName)(0,0) = 1.0;
00097 
00098   // Transformation (90° rotation).
00099   Eigen::Isometry3d transform;
00100 
00101   transform.translation().x() = 0.0;
00102   transform.translation().y() = 0.0;
00103   transform.translation().z() = 0.0;
00104 
00105   transform.linear()(0,0) =  0.0;
00106   transform.linear()(0,1) = -1.0;
00107   transform.linear()(0,2) =  0.0;
00108 
00109   transform.linear()(1,0) =  1.0;
00110   transform.linear()(1,1) =  0.0;
00111   transform.linear()(1,2) =  0.0;
00112 
00113   transform.linear()(2,0) =  0.0;
00114   transform.linear()(2,1) =  0.0;
00115   transform.linear()(2,2) =  1.0;
00116 
00117   // Apply affine transformation.
00118   const GridMap transformedMap = map.getTransformedMap(transform, heightLayerName, map.getFrameId(), 0.25);
00119 
00120   // Check if map has been rotated by 90° about z
00121   EXPECT_NEAR(map.getLength().x(), transformedMap.getLength().y(), 1e-6);
00122   EXPECT_NEAR(map.getLength().y(), transformedMap.getLength().x(), 1e-6);
00123   EXPECT_EQ(map.get(heightLayerName).size(), transformedMap.get(heightLayerName).size());
00124   EXPECT_DOUBLE_EQ(map.get(heightLayerName)(0,0), transformedMap.get(heightLayerName)(19,0));
00125 }
00126 
00127 TEST(AddDataFrom, ExtendMapAligned)
00128 {
00129   GridMap map1, map2;
00130   map1.setGeometry(Length(5.1, 5.1), 1.0, Position(0.0, 0.0)); // bufferSize(5, 5)
00131   map1.add("zero", 0.0);
00132   map1.add("one", 1.0);
00133   map1.setBasicLayers(map1.getLayers());
00134 
00135   map2.setGeometry(Length(3.1, 3.1), 1.0, Position(2.0, 2.0));
00136   map2.add("one", 1.1);
00137   map2.add("two", 2.0);
00138   map2.setBasicLayers(map1.getLayers());
00139 
00140   map1.addDataFrom(map2, true, true, true);
00141 
00142   EXPECT_TRUE(map1.exists("two"));
00143   EXPECT_TRUE(map1.isInside(Position(3.0, 3.0)));
00144   EXPECT_DOUBLE_EQ(6.0, map1.getLength().x());
00145   EXPECT_DOUBLE_EQ(6.0, map1.getLength().y());
00146   EXPECT_DOUBLE_EQ(0.5, map1.getPosition().x());
00147   EXPECT_DOUBLE_EQ(0.5, map1.getPosition().y());
00148   EXPECT_NEAR(1.1, map1.atPosition("one", Position(2, 2)), 1e-4);
00149   EXPECT_DOUBLE_EQ(1.0, map1.atPosition("one", Position(-2, -2)));
00150   EXPECT_DOUBLE_EQ(0.0, map1.atPosition("zero", Position(0.0, 0.0)));
00151 }
00152 
00153 TEST(AddDataFrom, ExtendMapNotAligned)
00154 {
00155   GridMap map1, map2;
00156   map1.setGeometry(Length(6.1, 6.1), 1.0, Position(0.0, 0.0)); // bufferSize(6, 6)
00157   map1.add("nan");
00158   map1.add("one", 1.0);
00159   map1.add("zero", 0.0);
00160   map1.setBasicLayers(map1.getLayers());
00161 
00162   map2.setGeometry(Length(3.1, 3.1), 1.0, Position(3.2, 3.2));
00163   map2.add("nan", 1.0);
00164   map2.add("one", 1.1);
00165   map2.add("two", 2.0);
00166   map2.setBasicLayers(map1.getLayers());
00167 
00168   std::vector<std::string> stringVector;
00169   stringVector.push_back("nan");
00170   map1.addDataFrom(map2, true, false, false, stringVector);
00171   Index index;
00172   map1.getIndex(Position(-2, -2), index);
00173 
00174   EXPECT_FALSE(map1.exists("two"));
00175   EXPECT_TRUE(map1.isInside(Position(4.0, 4.0)));
00176   EXPECT_DOUBLE_EQ(8.0, map1.getLength().x());
00177   EXPECT_DOUBLE_EQ(8.0, map1.getLength().y());
00178   EXPECT_DOUBLE_EQ(1.0, map1.getPosition().x());
00179   EXPECT_DOUBLE_EQ(1.0, map1.getPosition().y());
00180   EXPECT_FALSE(map1.isValid(index, "nan"));
00181   EXPECT_DOUBLE_EQ(1.0, map1.atPosition("one", Position(0.0, 0.0)));
00182   EXPECT_DOUBLE_EQ(1.0, map1.atPosition("nan", Position(3.0, 3.0)));
00183 }
00184 
00185 TEST(AddDataFrom, CopyData)
00186 {
00187   GridMap map1, map2;
00188   map1.setGeometry(Length(5.1, 5.1), 1.0, Position(0.0, 0.0)); // bufferSize(5, 5)
00189   map1.add("zero", 0.0);
00190   map1.add("one");
00191   map1.setBasicLayers(map1.getLayers());
00192 
00193   map2.setGeometry(Length(3.1, 3.1), 1.0, Position(2.0, 2.0));
00194   map2.add("one", 1.0);
00195   map2.add("two", 2.0);
00196   map2.setBasicLayers(map1.getLayers());
00197 
00198   map1.addDataFrom(map2, false, false, true);
00199   Index index;
00200   map1.getIndex(Position(-2, -2), index);
00201 
00202   EXPECT_TRUE(map1.exists("two"));
00203   EXPECT_FALSE(map1.isInside(Position(3.0, 3.0)));
00204   EXPECT_DOUBLE_EQ(5.0, map1.getLength().x());
00205   EXPECT_DOUBLE_EQ(5.0, map1.getLength().y());
00206   EXPECT_DOUBLE_EQ(0.0, map1.getPosition().x());
00207   EXPECT_DOUBLE_EQ(0.0, map1.getPosition().y());
00208   EXPECT_DOUBLE_EQ(1.0, map1.atPosition("one", Position(2, 2)));
00209   EXPECT_FALSE(map1.isValid(index, "one"));
00210   EXPECT_DOUBLE_EQ(0.0, map1.atPosition("zero", Position(0.0, 0.0)));
00211 }
00212 
00213 TEST(ValueAtPosition, NearestNeighbor)
00214 {
00215   GridMap map( { "types" });
00216   map.setGeometry(Length(3.0, 3.0), 1.0, Position(0.0, 0.0));
00217 
00218   map.at("types", Index(0,0)) = 0.5;
00219   map.at("types", Index(0,1)) = 3.8;
00220   map.at("types", Index(0,2)) = 2.0;
00221   map.at("types", Index(1,0)) = 2.1;
00222   map.at("types", Index(1,1)) = 1.0;
00223   map.at("types", Index(1,2)) = 2.0;
00224   map.at("types", Index(2,0)) = 1.0;
00225   map.at("types", Index(2,1)) = 2.0;
00226   map.at("types", Index(2,2)) = 2.0;
00227 
00228   double value;
00229 
00230   value = map.atPosition("types", Position(1.35,-0.4));
00231   EXPECT_DOUBLE_EQ((float)3.8, value);
00232 
00233   value = map.atPosition("types", Position(-0.3,0.0));
00234   EXPECT_DOUBLE_EQ(1.0, value);
00235 }
00236 
00237 TEST(ValueAtPosition, LinearInterpolated)
00238 {
00239   GridMap map( { "types" });
00240   map.setGeometry(Length(3.0, 3.0), 1.0, Position(0.0, 0.0));
00241 
00242   map.at("types", Index(0,0)) = 0.5;
00243   map.at("types", Index(0,1)) = 3.8;
00244   map.at("types", Index(0,2)) = 2.0;
00245   map.at("types", Index(1,0)) = 2.1;
00246   map.at("types", Index(1,1)) = 1.0;
00247   map.at("types", Index(1,2)) = 2.0;
00248   map.at("types", Index(2,0)) = 1.0;
00249   map.at("types", Index(2,1)) = 2.0;
00250   map.at("types", Index(2,2)) = 2.0;
00251 
00252   double value;
00253 
00254   // Close to the border -> reverting to INTER_NEAREST.
00255   value = map.atPosition("types", Position(-0.5,-1.2), InterpolationMethods::INTER_LINEAR);
00256   EXPECT_DOUBLE_EQ(2.0, value);
00257   // In between 1.0 and 2.0 field.
00258   value = map.atPosition("types", Position(-0.5,0.0), InterpolationMethods::INTER_LINEAR);
00259   EXPECT_DOUBLE_EQ(1.5, value);
00260   // Calculated "by Hand".
00261   value = map.atPosition("types", Position(0.69,0.38), InterpolationMethods::INTER_LINEAR);
00262   EXPECT_NEAR(2.1963200, value, 0.0000001);
00263 }


grid_map_core
Author(s): Péter Fankhauser
autogenerated on Tue Jul 9 2019 05:06:13