00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <memory>
00018
00019 #include "absl/memory/memory.h"
00020 #include "cartographer/common/lua_parameter_dictionary.h"
00021 #include "cartographer/common/lua_parameter_dictionary_test_helpers.h"
00022 #include "cartographer/mapping/2d/probability_grid.h"
00023 #include "cartographer/mapping/2d/probability_grid_range_data_inserter_2d.h"
00024 #include "cartographer/mapping/probability_values.h"
00025 #include "gmock/gmock.h"
00026
00027 namespace cartographer {
00028 namespace mapping {
00029 namespace {
00030
00031 class RangeDataInserterTest2D : public ::testing::Test {
00032 protected:
00033 RangeDataInserterTest2D()
00034 : probability_grid_(
00035 MapLimits(1., Eigen::Vector2d(1., 5.), CellLimits(5, 5)),
00036 &conversion_tables_) {
00037 auto parameter_dictionary = common::MakeDictionary(
00038 "return { "
00039 "insert_free_space = true, "
00040 "hit_probability = 0.7, "
00041 "miss_probability = 0.4, "
00042 "}");
00043 options_ = CreateProbabilityGridRangeDataInserterOptions2D(
00044 parameter_dictionary.get());
00045 range_data_inserter_ =
00046 absl::make_unique<ProbabilityGridRangeDataInserter2D>(options_);
00047 }
00048
00049 void InsertPointCloud() {
00050 sensor::RangeData range_data;
00051 range_data.returns.push_back({Eigen::Vector3f{-3.5f, 0.5f, 0.f}});
00052 range_data.returns.push_back({Eigen::Vector3f{-2.5f, 1.5f, 0.f}});
00053 range_data.returns.push_back({Eigen::Vector3f{-1.5f, 2.5f, 0.f}});
00054 range_data.returns.push_back({Eigen::Vector3f{-0.5f, 3.5f, 0.f}});
00055 range_data.origin.x() = -0.5f;
00056 range_data.origin.y() = 0.5f;
00057 range_data_inserter_->Insert(range_data, &probability_grid_);
00058 probability_grid_.FinishUpdate();
00059 }
00060
00061 ValueConversionTables conversion_tables_;
00062 ProbabilityGrid probability_grid_;
00063 std::unique_ptr<ProbabilityGridRangeDataInserter2D> range_data_inserter_;
00064 proto::ProbabilityGridRangeDataInserterOptions2D options_;
00065 };
00066
00067 TEST_F(RangeDataInserterTest2D, InsertPointCloud) {
00068 InsertPointCloud();
00069
00070 EXPECT_NEAR(1., probability_grid_.limits().max().x(), 1e-9);
00071 EXPECT_NEAR(5., probability_grid_.limits().max().y(), 1e-9);
00072
00073 const CellLimits& cell_limits = probability_grid_.limits().cell_limits();
00074 EXPECT_EQ(5, cell_limits.num_x_cells);
00075 EXPECT_EQ(5, cell_limits.num_y_cells);
00076
00077 enum class State { UNKNOWN, MISS, HIT };
00078 State expected_states[5][5] = {
00079 {State::UNKNOWN, State::UNKNOWN, State::UNKNOWN, State::UNKNOWN,
00080 State::UNKNOWN},
00081 {State::UNKNOWN, State::HIT, State::MISS, State::MISS, State::MISS},
00082 {State::UNKNOWN, State::UNKNOWN, State::HIT, State::MISS, State::MISS},
00083 {State::UNKNOWN, State::UNKNOWN, State::UNKNOWN, State::HIT, State::MISS},
00084 {State::UNKNOWN, State::UNKNOWN, State::UNKNOWN, State::UNKNOWN,
00085 State::HIT}};
00086 for (int row = 0; row != 5; ++row) {
00087 for (int column = 0; column != 5; ++column) {
00088 Eigen::Array2i cell_index(row, column);
00089 EXPECT_TRUE(probability_grid_.limits().Contains(cell_index));
00090 switch (expected_states[column][row]) {
00091 case State::UNKNOWN:
00092 EXPECT_FALSE(probability_grid_.IsKnown(cell_index));
00093 break;
00094 case State::MISS:
00095 EXPECT_NEAR(options_.miss_probability(),
00096 probability_grid_.GetProbability(cell_index), 1e-4);
00097 break;
00098 case State::HIT:
00099 EXPECT_NEAR(options_.hit_probability(),
00100 probability_grid_.GetProbability(cell_index), 1e-4);
00101 break;
00102 }
00103 }
00104 }
00105 }
00106
00107 TEST_F(RangeDataInserterTest2D, ProbabilityProgression) {
00108 InsertPointCloud();
00109 EXPECT_NEAR(
00110 options_.hit_probability(),
00111 probability_grid_.GetProbability(probability_grid_.limits().GetCellIndex(
00112 Eigen::Vector2f(-3.5f, 0.5f))),
00113 1e-4);
00114 EXPECT_NEAR(
00115 options_.miss_probability(),
00116 probability_grid_.GetProbability(probability_grid_.limits().GetCellIndex(
00117 Eigen::Vector2f(-2.5f, 0.5f))),
00118 1e-4);
00119
00120 for (int i = 0; i < 1000; ++i) {
00121 InsertPointCloud();
00122 }
00123 EXPECT_NEAR(
00124 kMaxProbability,
00125 probability_grid_.GetProbability(probability_grid_.limits().GetCellIndex(
00126 Eigen::Vector2f(-3.5f, 0.5f))),
00127 1e-3);
00128 EXPECT_NEAR(
00129 kMinProbability,
00130 probability_grid_.GetProbability(probability_grid_.limits().GetCellIndex(
00131 Eigen::Vector2f(-2.5f, 0.5f))),
00132 1e-3);
00133 }
00134
00135 }
00136 }
00137 }