Costmap2DConverterTest.cpp
Go to the documentation of this file.
1 /*
2  * Costmap2DConverterTest.cpp
3  *
4  * Created on: Nov 23, 2016
5  * Authors: Peter Fankhauser, Gabriel Hottiger
6  * Institute: ETH Zurich, ANYbotics
7  */
8 
9 // Grid map
12 
13 // Gtest
14 #include <gtest/gtest.h>
15 
16 // Eigen
17 #include <Eigen/Core>
18 
19 using namespace grid_map;
20 
21 template <typename ConversionTable>
22 class TestCostmap2DConversion : public testing::Test {
23  public:
24  using TestValue = std::tuple<Position, unsigned char, double, bool>;
25 
27  TestCostmap2DConversion() : costmap2d_(8, 5, 1.0, 2.0, 3.0) { gridMap_.setGeometry(Length(8.0, 5.0), 1.0, Position(6.0, 5.5)); }
28 
30  std::vector<TestValue> getTestValues();
31 
33  void assertMapGeometry(const GridMap& gridMap, const costmap_2d::Costmap2D& costMap) {
34  // Check map info.
35  // Different conventions: Costmap2d returns the *centerpoint* of the last cell in the map.
36  Length length = gridMap.getLength() - Length::Constant(0.5 * gridMap.getResolution());
37  Length position = gridMap.getPosition() - 0.5 * gridMap.getLength().matrix();
38  EXPECT_EQ(costMap.getSizeInMetersX(), length.x());
39  EXPECT_EQ(costMap.getSizeInMetersY(), length.y());
40  EXPECT_EQ(costMap.getSizeInCellsX(), gridMap.getSize()[0]);
41  EXPECT_EQ(costMap.getSizeInCellsY(), gridMap.getSize()[1]);
42  EXPECT_EQ(costMap.getResolution(), gridMap.getResolution());
43  EXPECT_EQ(costMap.getOriginX(), position.x());
44  EXPECT_EQ(costMap.getOriginY(), position.y());
45  }
46 
47  protected:
51 };
52 
57 
58 // Test data for special translation
59 template <>
60 std::vector<TestCostmap2DConversion<Costmap2DSpecialTranslationTable>::TestValue>
62  std::vector<TestValue> testValues;
63  testValues.emplace_back(TestValue(Position(3.2, 5.1), costmap_2d::FREE_SPACE, 0.f, true));
64  testValues.emplace_back(TestValue(Position(4.2, 4.1), costmap_2d::INSCRIBED_INFLATED_OBSTACLE, 1.f, true));
65  testValues.emplace_back(TestValue(Position(6.2, 3.1), costmap_2d::LETHAL_OBSTACLE, 2.f, true));
66  testValues.emplace_back(TestValue(Position(5.2, 7.8), costmap_2d::NO_INFORMATION, 3.f, true));
67  // Check for grid map to costmap only.
68  testValues.emplace_back(TestValue(Position(5.4, 6.8), costmap_2d::FREE_SPACE, -1.f, false));
69  testValues.emplace_back(TestValue(Position(8.5, 4.5), costmap_2d::LETHAL_OBSTACLE, 4.f, false));
70  return testValues;
71 }
72 
73 // Test data for special translation
74 template <>
75 std::vector<TestCostmap2DConversion<Costmap2DLargeIntervalsTranslationTable>::TestValue>
77  std::vector<TestValue> testValues;
78  testValues.emplace_back(TestValue(Position(3.2, 5.1), costmap_2d::FREE_SPACE, 100.f, true));
79  testValues.emplace_back(TestValue(Position(4.2, 4.1), costmap_2d::INSCRIBED_INFLATED_OBSTACLE, 400.f, true));
80  testValues.emplace_back(TestValue(Position(6.2, 3.1), costmap_2d::LETHAL_OBSTACLE, 500.f, true));
81  testValues.emplace_back(TestValue(Position(5.2, 7.8), costmap_2d::NO_INFORMATION, 5.f, true));
82  testValues.emplace_back(TestValue(Position(3.4, 3.8), 253/11, 100.f + 300.f/11.f, true));
83  // Check for grid map to costmap only.
84  testValues.emplace_back(TestValue(Position(5.4, 6.8), costmap_2d::FREE_SPACE, 83.f, false));
85  testValues.emplace_back(TestValue(Position(8.5, 4.5), costmap_2d::LETHAL_OBSTACLE, 504.f, false));
86  testValues.emplace_back(TestValue(Position(4.7, 5.8), costmap_2d::INSCRIBED_INFLATED_OBSTACLE, 444.f, false));
87 
88  return testValues;
89 }
90 
91 // Test data for direct translation
92 template <>
93 std::vector<TestCostmap2DConversion<Costmap2DDirectTranslationTable>::TestValue>
95  std::vector<TestValue> testValues;
96  testValues.emplace_back(TestValue(Position(3.2, 5.1), costmap_2d::FREE_SPACE, costmap_2d::FREE_SPACE, true));
97  testValues.emplace_back(
99  testValues.emplace_back(TestValue(Position(6.2, 3.1), costmap_2d::LETHAL_OBSTACLE, costmap_2d::LETHAL_OBSTACLE, true));
100  testValues.emplace_back(TestValue(Position(5.2, 7.8), costmap_2d::NO_INFORMATION, costmap_2d::NO_INFORMATION, true));
101  testValues.emplace_back(TestValue(Position(5.4, 6.8), 1, 1.0, true));
102  testValues.emplace_back(TestValue(Position(8.5, 4.5), 97, 97.0, true));
103  // Check for grid map to costmap only.
104  testValues.emplace_back(TestValue(Position(4.7, 5.8), costmap_2d::FREE_SPACE, -30.f, false));
105  testValues.emplace_back(TestValue(Position(3.4, 3.8), costmap_2d::LETHAL_OBSTACLE, 270.f, false));
106  return testValues;
107 }
108 
109 // Test data for century translation
110 template <>
111 std::vector<TestCostmap2DConversion<Costmap2DCenturyTranslationTable>::TestValue>
113  std::vector<TestValue> testValues;
114  testValues.emplace_back(TestValue(Position(3.2, 5.1), costmap_2d::FREE_SPACE, 0.f, true));
115  testValues.emplace_back(TestValue(Position(4.2, 4.1), costmap_2d::INSCRIBED_INFLATED_OBSTACLE, 99.f, true));
116  testValues.emplace_back(TestValue(Position(6.2, 3.1), costmap_2d::LETHAL_OBSTACLE, 100.f, true));
117  testValues.emplace_back(TestValue(Position(5.2, 7.8), costmap_2d::NO_INFORMATION, -1.f, true));
118  testValues.emplace_back(TestValue(Position(5.4, 6.8), 253/11, 99.f/11.f, true));
119  // Check for grid map to costmap only.
120  testValues.emplace_back(TestValue(Position(4.7, 5.8), costmap_2d::FREE_SPACE, -30.f, false));
121  testValues.emplace_back(TestValue(Position(3.4, 3.8), costmap_2d::LETHAL_OBSTACLE, 270.f, false));
122  return testValues;
123 }
124 
127 
129 
130 TYPED_TEST(TestCostmap2DConversion, initializeFromCostmap2d) {
131  // Convert to grid map.
132  GridMap gridMap;
133  this->costmap2dConverter_.initializeFromCostmap2D(this->costmap2d_, gridMap);
134  this->assertMapGeometry(gridMap, this->costmap2d_);
135 }
136 
137 TYPED_TEST(TestCostmap2DConversion, initializeFromGridMap) {
138  // Convert to Costmap2D.
139  costmap_2d::Costmap2D costmap2d;
140  this->costmap2dConverter_.initializeFromGridMap(this->gridMap_, costmap2d);
141  this->assertMapGeometry(this->gridMap_, costmap2d);
142 }
143 
144 TYPED_TEST(TestCostmap2DConversion, addLayerFromCostmap2d) {
145  // Create grid map.
146  const std::string layer("layer");
147  GridMap gridMap;
148  this->costmap2dConverter_.initializeFromCostmap2D(this->costmap2d_, gridMap);
149 
150  // Fill in test data to Costmap2D.
151  for (const auto& testValue : this->getTestValues()) {
152  if (std::get<3>(testValue)) {
153  unsigned int xIndex, yIndex;
154  ASSERT_TRUE(this->costmap2d_.worldToMap(std::get<0>(testValue).x(), std::get<0>(testValue).y(), xIndex, yIndex));
155  this->costmap2d_.getCharMap()[this->costmap2d_.getIndex(xIndex, yIndex)] = std::get<1>(testValue);
156  }
157  }
158 
159  // Copy data.
160  this->costmap2dConverter_.addLayerFromCostmap2D(this->costmap2d_, layer, gridMap);
161 
162  // Check data.
163  for (const auto& testValue : this->getTestValues()) {
164  if (std::get<3>(testValue)) {
165  EXPECT_EQ(std::get<2>(testValue), gridMap.atPosition(layer, std::get<0>(testValue)));
166  }
167  }
168 }
169 
170 TYPED_TEST(TestCostmap2DConversion, setCostmap2DFromGridMap) {
171  // Create costmap2d.
172  costmap_2d::Costmap2D costmap;
173  this->costmap2dConverter_.initializeFromGridMap(this->gridMap_, costmap);
174 
175  // Fill in test data to grid map.
176  const std::string layer("layer");
177  this->gridMap_.add(layer);
178  for (const auto& testValue : this->getTestValues()) {
179  Index index;
180  this->gridMap_.getIndex(std::get<0>(testValue), index);
181  this->gridMap_.get(layer)(index(0), index(1)) = std::get<2>(testValue);
182  }
183 
184  // Copy data.
185  this->costmap2dConverter_.setCostmap2DFromGridMap(this->gridMap_, layer, costmap);
186 
187  // Check data.
188  for (const auto& testValue : this->getTestValues()) {
189  unsigned int xIndex, yIndex;
190  ASSERT_TRUE(costmap.worldToMap(std::get<0>(testValue).x(), std::get<0>(testValue).y(), xIndex, yIndex));
191  costmap.getCharMap()[costmap.getIndex(xIndex, yIndex)] = std::get<1>(testValue);
192  }
193 }
const Length & getLength() const
Eigen::Array2i Index
float & atPosition(const std::string &layer, const Position &position)
std::vector< TestValue > getTestValues()
Getter of test data.
f
bool getPosition(const Index &index, Position &position) const
double getOriginX() const
double getSizeInMetersX() const
TYPED_TEST_CASE(TestCostmap2DConversion, TranslationTableTestTypes)
TFSIMD_FORCE_INLINE const tfScalar & y() const
static const unsigned char FREE_SPACE
double getSizeInMetersY() const
unsigned char * getCharMap() const
Costmap2DConverter< GridMap, ConversionTable > costmap2dConverter_
double getResolution() const
static const unsigned char INSCRIBED_INFLATED_OBSTACLE
Eigen::Vector2d Position
void assertMapGeometry(const GridMap &gridMap, const costmap_2d::Costmap2D &costMap)
Check that maps have same geometry.
Costmap2DTranslationTable< 3, 2, 1, 0 > Costmap2DSpecialTranslationTable
Map type that has only mappings for cost map special values.
double getOriginY() const
costmap_2d::Costmap2D costmap2d_
unsigned int getSizeInCellsY() const
Costmap2DTranslationTable< 5, 500, 400, 100 > Costmap2DLargeIntervalsTranslationTable
Map type that has larger intervals between special values.
unsigned int getSizeInCellsX() const
static const unsigned char LETHAL_OBSTACLE
::testing::Types< Costmap2DSpecialTranslationTable, Costmap2DLargeIntervalsTranslationTable, Costmap2DDirectTranslationTable, Costmap2DCenturyTranslationTable > TranslationTableTestTypes
static const unsigned char NO_INFORMATION
TYPED_TEST(TestCostmap2DConversion, initializeFromCostmap2d)
TFSIMD_FORCE_INLINE tfScalar length(const Quaternion &q)
std::tuple< Position, unsigned char, double, bool > TestValue
double getResolution() const
Costmap2DTranslationTable< costmap_2d::NO_INFORMATION, costmap_2d::LETHAL_OBSTACLE, costmap_2d::INSCRIBED_INFLATED_OBSTACLE, costmap_2d::FREE_SPACE > Costmap2DDirectTranslationTable
Direct cost translations. This maps the cost directly, simply casting between the underlying unsigned...
bool worldToMap(double wx, double wy, unsigned int &mx, unsigned int &my) const
Eigen::Array2d Length
unsigned int getIndex(unsigned int mx, unsigned int my) const
const Size & getSize() const


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