range_data_inserter_2d_test.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2016 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #include <memory>
20 
26 #include "gmock/gmock.h"
27 
28 namespace cartographer {
29 namespace mapping {
30 namespace {
31 
32 class RangeDataInserterTest2D : public ::testing::Test {
33  protected:
34  RangeDataInserterTest2D()
36  MapLimits(1., Eigen::Vector2d(1., 5.), CellLimits(5, 5))) {
37  auto parameter_dictionary = common::MakeDictionary(
38  "return { "
39  "insert_free_space = true, "
40  "hit_probability = 0.7, "
41  "miss_probability = 0.4, "
42  "}");
44  parameter_dictionary.get());
46  common::make_unique<ProbabilityGridRangeDataInserter2D>(options_);
47  }
48 
49  void InsertPointCloud() {
50  sensor::RangeData range_data;
51  range_data.returns.emplace_back(-3.5f, 0.5f, 0.f);
52  range_data.returns.emplace_back(-2.5f, 1.5f, 0.f);
53  range_data.returns.emplace_back(-1.5f, 2.5f, 0.f);
54  range_data.returns.emplace_back(-0.5f, 3.5f, 0.f);
55  range_data.origin.x() = -0.5f;
56  range_data.origin.y() = 0.5f;
57  range_data_inserter_->Insert(range_data, &probability_grid_);
58  probability_grid_.FinishUpdate();
59  }
60 
61  ProbabilityGrid probability_grid_;
62  std::unique_ptr<ProbabilityGridRangeDataInserter2D> range_data_inserter_;
63  proto::ProbabilityGridRangeDataInserterOptions2D options_;
64 };
65 
66 TEST_F(RangeDataInserterTest2D, InsertPointCloud) {
67  InsertPointCloud();
68 
69  EXPECT_NEAR(1., probability_grid_.limits().max().x(), 1e-9);
70  EXPECT_NEAR(5., probability_grid_.limits().max().y(), 1e-9);
71 
72  const CellLimits& cell_limits = probability_grid_.limits().cell_limits();
73  EXPECT_EQ(5, cell_limits.num_x_cells);
74  EXPECT_EQ(5, cell_limits.num_y_cells);
75 
76  enum class State { UNKNOWN, MISS, HIT };
77  State expected_states[5][5] = {
78  {State::UNKNOWN, State::UNKNOWN, State::UNKNOWN, State::UNKNOWN,
79  State::UNKNOWN},
80  {State::UNKNOWN, State::HIT, State::MISS, State::MISS, State::MISS},
81  {State::UNKNOWN, State::UNKNOWN, State::HIT, State::MISS, State::MISS},
82  {State::UNKNOWN, State::UNKNOWN, State::UNKNOWN, State::HIT, State::MISS},
83  {State::UNKNOWN, State::UNKNOWN, State::UNKNOWN, State::UNKNOWN,
84  State::HIT}};
85  for (int row = 0; row != 5; ++row) {
86  for (int column = 0; column != 5; ++column) {
87  Eigen::Array2i cell_index(row, column);
88  EXPECT_TRUE(probability_grid_.limits().Contains(cell_index));
89  switch (expected_states[column][row]) {
90  case State::UNKNOWN:
91  EXPECT_FALSE(probability_grid_.IsKnown(cell_index));
92  break;
93  case State::MISS:
94  EXPECT_NEAR(options_.miss_probability(),
95  probability_grid_.GetProbability(cell_index), 1e-4);
96  break;
97  case State::HIT:
98  EXPECT_NEAR(options_.hit_probability(),
99  probability_grid_.GetProbability(cell_index), 1e-4);
100  break;
101  }
102  }
103  }
104 }
105 
106 TEST_F(RangeDataInserterTest2D, ProbabilityProgression) {
107  InsertPointCloud();
108  EXPECT_NEAR(
109  options_.hit_probability(),
110  probability_grid_.GetProbability(probability_grid_.limits().GetCellIndex(
111  Eigen::Vector2f(-3.5f, 0.5f))),
112  1e-4);
113  EXPECT_NEAR(
114  options_.miss_probability(),
115  probability_grid_.GetProbability(probability_grid_.limits().GetCellIndex(
116  Eigen::Vector2f(-2.5f, 0.5f))),
117  1e-4);
118 
119  for (int i = 0; i < 1000; ++i) {
120  InsertPointCloud();
121  }
122  EXPECT_NEAR(
124  probability_grid_.GetProbability(probability_grid_.limits().GetCellIndex(
125  Eigen::Vector2f(-3.5f, 0.5f))),
126  1e-3);
127  EXPECT_NEAR(
129  probability_grid_.GetProbability(probability_grid_.limits().GetCellIndex(
130  Eigen::Vector2f(-2.5f, 0.5f))),
131  1e-3);
132 }
133 
134 } // namespace
135 } // namespace mapping
136 } // namespace cartographer
ProbabilityGrid probability_grid_
proto::ProbabilityGridRangeDataInserterOptions2D CreateProbabilityGridRangeDataInserterOptions2D(common::LuaParameterDictionary *parameter_dictionary)
std::unique_ptr< LuaParameterDictionary > MakeDictionary(const std::string &code)
constexpr float kMinProbability
constexpr float kMaxProbability
proto::ProbabilityGridRangeDataInserterOptions2D options_
std::unique_ptr< ProbabilityGridRangeDataInserter2D > range_data_inserter_


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58