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, Autonomous Systems Lab
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(AddDataFrom, ExtendMapAligned)
00088 {
00089   GridMap map1, map2;
00090   map1.setGeometry(Length(5.1, 5.1), 1.0, Position(0.0, 0.0)); // bufferSize(5, 5)
00091   map1.add("zero", 0.0);
00092   map1.add("one", 1.0);
00093   map1.setBasicLayers(map1.getLayers());
00094 
00095   map2.setGeometry(Length(3.1, 3.1), 1.0, Position(2.0, 2.0));
00096   map2.add("one", 1.1);
00097   map2.add("two", 2.0);
00098   map2.setBasicLayers(map1.getLayers());
00099 
00100   map1.addDataFrom(map2, true, true, true);
00101 
00102   EXPECT_TRUE(map1.exists("two"));
00103   EXPECT_TRUE(map1.isInside(Position(3.0, 3.0)));
00104   EXPECT_DOUBLE_EQ(6.0, map1.getLength().x());
00105   EXPECT_DOUBLE_EQ(6.0, map1.getLength().y());
00106   EXPECT_DOUBLE_EQ(0.5, map1.getPosition().x());
00107   EXPECT_DOUBLE_EQ(0.5, map1.getPosition().y());
00108   EXPECT_NEAR(1.1, map1.atPosition("one", Position(2, 2)), 1e-4);
00109   EXPECT_DOUBLE_EQ(1.0, map1.atPosition("one", Position(-2, -2)));
00110   EXPECT_DOUBLE_EQ(0.0, map1.atPosition("zero", Position(0.0, 0.0)));
00111 }
00112 
00113 TEST(AddDataFrom, ExtendMapNotAligned)
00114 {
00115   GridMap map1, map2;
00116   map1.setGeometry(Length(6.1, 6.1), 1.0, Position(0.0, 0.0)); // bufferSize(6, 6)
00117   map1.add("nan");
00118   map1.add("one", 1.0);
00119   map1.add("zero", 0.0);
00120   map1.setBasicLayers(map1.getLayers());
00121 
00122   map2.setGeometry(Length(3.1, 3.1), 1.0, Position(3.2, 3.2));
00123   map2.add("nan", 1.0);
00124   map2.add("one", 1.1);
00125   map2.add("two", 2.0);
00126   map2.setBasicLayers(map1.getLayers());
00127 
00128   std::vector<std::string> stringVector;
00129   stringVector.push_back("nan");
00130   map1.addDataFrom(map2, true, false, false, stringVector);
00131   Index index;
00132   map1.getIndex(Position(-2, -2), index);
00133 
00134   EXPECT_FALSE(map1.exists("two"));
00135   EXPECT_TRUE(map1.isInside(Position(4.0, 4.0)));
00136   EXPECT_DOUBLE_EQ(8.0, map1.getLength().x());
00137   EXPECT_DOUBLE_EQ(8.0, map1.getLength().y());
00138   EXPECT_DOUBLE_EQ(1.0, map1.getPosition().x());
00139   EXPECT_DOUBLE_EQ(1.0, map1.getPosition().y());
00140   EXPECT_FALSE(map1.isValid(index, "nan"));
00141   EXPECT_DOUBLE_EQ(1.0, map1.atPosition("one", Position(0.0, 0.0)));
00142   EXPECT_DOUBLE_EQ(1.0, map1.atPosition("nan", Position(3.0, 3.0)));
00143 }
00144 
00145 TEST(AddDataFrom, CopyData)
00146 {
00147   GridMap map1, map2;
00148   map1.setGeometry(Length(5.1, 5.1), 1.0, Position(0.0, 0.0)); // bufferSize(5, 5)
00149   map1.add("zero", 0.0);
00150   map1.add("one");
00151   map1.setBasicLayers(map1.getLayers());
00152 
00153   map2.setGeometry(Length(3.1, 3.1), 1.0, Position(2.0, 2.0));
00154   map2.add("one", 1.0);
00155   map2.add("two", 2.0);
00156   map2.setBasicLayers(map1.getLayers());
00157 
00158   map1.addDataFrom(map2, false, false, true);
00159   Index index;
00160   map1.getIndex(Position(-2, -2), index);
00161 
00162   EXPECT_TRUE(map1.exists("two"));
00163   EXPECT_FALSE(map1.isInside(Position(3.0, 3.0)));
00164   EXPECT_DOUBLE_EQ(5.0, map1.getLength().x());
00165   EXPECT_DOUBLE_EQ(5.0, map1.getLength().y());
00166   EXPECT_DOUBLE_EQ(0.0, map1.getPosition().x());
00167   EXPECT_DOUBLE_EQ(0.0, map1.getPosition().y());
00168   EXPECT_DOUBLE_EQ(1.0, map1.atPosition("one", Position(2, 2)));
00169   EXPECT_FALSE(map1.isValid(index, "one"));
00170   EXPECT_DOUBLE_EQ(0.0, map1.atPosition("zero", Position(0.0, 0.0)));
00171 }
00172 
00173 TEST(ValueAtPosition, NearestNeighbor)
00174 {
00175   GridMap map( { "types" });
00176   map.setGeometry(Length(3.0, 3.0), 1.0, Position(0.0, 0.0));
00177 
00178   map.at("types", Index(0,0)) = 0.5;
00179   map.at("types", Index(0,1)) = 3.8;
00180   map.at("types", Index(0,2)) = 2.0;
00181   map.at("types", Index(1,0)) = 2.1;
00182   map.at("types", Index(1,1)) = 1.0;
00183   map.at("types", Index(1,2)) = 2.0;
00184   map.at("types", Index(2,0)) = 1.0;
00185   map.at("types", Index(2,1)) = 2.0;
00186   map.at("types", Index(2,2)) = 2.0;
00187 
00188   double value;
00189 
00190   value = map.atPosition("types", Position(1.35,-0.4));
00191   EXPECT_DOUBLE_EQ((float)3.8, value);
00192 
00193   value = map.atPosition("types", Position(-0.3,0.0));
00194   EXPECT_DOUBLE_EQ(1.0, value);
00195 }
00196 
00197 TEST(ValueAtPosition, LinearInterpolated)
00198 {
00199   GridMap map( { "types" });
00200   map.setGeometry(Length(3.0, 3.0), 1.0, Position(0.0, 0.0));
00201 
00202   map.at("types", Index(0,0)) = 0.5;
00203   map.at("types", Index(0,1)) = 3.8;
00204   map.at("types", Index(0,2)) = 2.0;
00205   map.at("types", Index(1,0)) = 2.1;
00206   map.at("types", Index(1,1)) = 1.0;
00207   map.at("types", Index(1,2)) = 2.0;
00208   map.at("types", Index(2,0)) = 1.0;
00209   map.at("types", Index(2,1)) = 2.0;
00210   map.at("types", Index(2,2)) = 2.0;
00211 
00212   double value;
00213 
00214   // Close to the border -> reverting to INTER_NEAREST.
00215   value = map.atPosition("types", Position(-0.5,-1.2), InterpolationMethods::INTER_LINEAR);
00216   EXPECT_DOUBLE_EQ(2.0, value);
00217   // In between 1.0 and 2.0 field.
00218   value = map.atPosition("types", Position(-0.5,0.0), InterpolationMethods::INTER_LINEAR);
00219   EXPECT_DOUBLE_EQ(1.5, value);
00220   // Calculated "by Hand".
00221   value = map.atPosition("types", Position(0.69,0.38), InterpolationMethods::INTER_LINEAR);
00222   EXPECT_NEAR(2.1963200, value, 0.0000001);
00223 }


grid_map_core
Author(s): Péter Fankhauser
autogenerated on Mon Oct 9 2017 03:09:16