submap_2d.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 <cinttypes>
20 #include <cmath>
21 #include <cstdlib>
22 #include <fstream>
23 #include <limits>
24 
25 #include "Eigen/Geometry"
30 #include "glog/logging.h"
31 
32 namespace cartographer {
33 namespace mapping {
34 
35 proto::SubmapsOptions2D CreateSubmapsOptions2D(
36  common::LuaParameterDictionary* const parameter_dictionary) {
37  proto::SubmapsOptions2D options;
38  options.set_num_range_data(
39  parameter_dictionary->GetNonNegativeInt("num_range_data"));
40  *options.mutable_grid_options_2d() = CreateGridOptions2D(
41  parameter_dictionary->GetDictionary("grid_options_2d").get());
42  *options.mutable_range_data_inserter_options() =
44  parameter_dictionary->GetDictionary("range_data_inserter").get());
45 
46  bool valid_range_data_inserter_grid_combination = false;
47  const proto::GridOptions2D_GridType& grid_type =
48  options.grid_options_2d().grid_type();
49  const proto::RangeDataInserterOptions_RangeDataInserterType&
50  range_data_inserter_type =
51  options.range_data_inserter_options().range_data_inserter_type();
52  if (grid_type == proto::GridOptions2D::PROBABILITY_GRID &&
53  range_data_inserter_type ==
54  proto::RangeDataInserterOptions::PROBABILITY_GRID_INSERTER_2D) {
55  valid_range_data_inserter_grid_combination = true;
56  }
57  CHECK(valid_range_data_inserter_grid_combination)
58  << "Invalid combination grid_type " << grid_type
59  << " with range_data_inserter_type " << range_data_inserter_type;
60  CHECK_GT(options.num_range_data(), 0);
61  return options;
62 }
63 
64 Submap2D::Submap2D(const Eigen::Vector2f& origin, std::unique_ptr<Grid2D> grid)
65  : Submap(transform::Rigid3d::Translation(
66  Eigen::Vector3d(origin.x(), origin.y(), 0.))) {
67  grid_ = std::move(grid);
68 }
69 
70 Submap2D::Submap2D(const proto::Submap2D& proto)
71  : Submap(transform::ToRigid3(proto.local_pose())) {
72  if (proto.has_grid()) {
73  CHECK(proto.grid().has_probability_grid_2d());
74  grid_ = common::make_unique<ProbabilityGrid>(proto.grid());
75  }
76  set_num_range_data(proto.num_range_data());
77  set_finished(proto.finished());
78 }
79 
80 void Submap2D::ToProto(proto::Submap* const proto,
81  bool include_probability_grid_data) const {
82  auto* const submap_2d = proto->mutable_submap_2d();
83  *submap_2d->mutable_local_pose() = transform::ToProto(local_pose());
84  submap_2d->set_num_range_data(num_range_data());
85  submap_2d->set_finished(finished());
86  if (include_probability_grid_data) {
87  CHECK(grid_);
88  *submap_2d->mutable_grid() = grid_->ToProto();
89  }
90 }
91 
92 void Submap2D::UpdateFromProto(const proto::Submap& proto) {
93  CHECK(proto.has_submap_2d());
94  const auto& submap_2d = proto.submap_2d();
95  set_num_range_data(submap_2d.num_range_data());
96  set_finished(submap_2d.finished());
97  if (proto.submap_2d().has_grid()) {
98  CHECK(proto.submap_2d().grid().has_probability_grid_2d());
99  grid_ = common::make_unique<ProbabilityGrid>(submap_2d.grid());
100  }
101 }
102 
104  const transform::Rigid3d&,
105  proto::SubmapQuery::Response* const response) const {
106  if (!grid_) return;
107  response->set_submap_version(num_range_data());
108  proto::SubmapQuery::Response::SubmapTexture* const texture =
109  response->add_textures();
110  grid()->DrawToSubmapTexture(texture, local_pose());
111 }
112 
114  const sensor::RangeData& range_data,
115  const RangeDataInserterInterface* range_data_inserter) {
116  CHECK(grid_);
117  CHECK(!finished());
118  range_data_inserter->Insert(range_data, grid_.get());
120 }
121 
123  CHECK(grid_);
124  CHECK(!finished());
125  grid_ = grid_->ComputeCroppedGrid();
126  set_finished(true);
127 }
128 
129 ActiveSubmaps2D::ActiveSubmaps2D(const proto::SubmapsOptions2D& options)
130  : options_(options),
131  range_data_inserter_(std::move(CreateRangeDataInserter())) {
132  // We always want to have at least one likelihood field which we can return,
133  // and will create it at the origin in absence of a better choice.
134  AddSubmap(Eigen::Vector2f::Zero());
135 }
136 
137 std::vector<std::shared_ptr<Submap2D>> ActiveSubmaps2D::submaps() const {
138  return submaps_;
139 }
140 
142 
144  for (auto& submap : submaps_) {
145  submap->InsertRangeData(range_data, range_data_inserter_.get());
146  }
147  if (submaps_.back()->num_range_data() == options_.num_range_data()) {
148  AddSubmap(range_data.origin.head<2>());
149  }
150 }
151 
152 std::unique_ptr<RangeDataInserterInterface>
154  return common::make_unique<ProbabilityGridRangeDataInserter2D>(
155  options_.range_data_inserter_options()
156  .probability_grid_range_data_inserter_options_2d());
157 }
158 
159 std::unique_ptr<GridInterface> ActiveSubmaps2D::CreateGrid(
160  const Eigen::Vector2f& origin) {
161  constexpr int kInitialSubmapSize = 100;
162  float resolution = options_.grid_options_2d().resolution();
163  return common::make_unique<ProbabilityGrid>(
164  MapLimits(resolution,
165  origin.cast<double>() + 0.5 * kInitialSubmapSize * resolution *
166  Eigen::Vector2d::Ones(),
167  CellLimits(kInitialSubmapSize, kInitialSubmapSize)));
168 }
169 
171  Submap2D* submap = submaps_.front().get();
172  submap->Finish();
174  submaps_.erase(submaps_.begin());
175 }
176 
177 void ActiveSubmaps2D::AddSubmap(const Eigen::Vector2f& origin) {
178  if (submaps_.size() > 1) {
179  // This will crop the finished Submap before inserting a new Submap to
180  // reduce peak memory usage a bit.
181  FinishSubmap();
182  }
183 
184  submaps_.push_back(common::make_unique<Submap2D>(
185  origin, std::unique_ptr<Grid2D>(
186  static_cast<Grid2D*>(CreateGrid(origin).release()))));
187  LOG(INFO) << "Added submap " << matching_submap_index_ + submaps_.size();
188 }
189 
190 } // namespace mapping
191 } // namespace cartographer
void InsertRangeData(const sensor::RangeData &range_data, const RangeDataInserterInterface *range_data_inserter)
Definition: submap_2d.cc:113
void ToProto(proto::Submap *proto, bool include_probability_grid_data) const override
Definition: submap_2d.cc:80
Rigid3< double > Rigid3d
std::unique_ptr< RangeDataInserterInterface > range_data_inserter_
Definition: submap_2d.h:100
proto::SubmapsOptions2D CreateSubmapsOptions2D(common::LuaParameterDictionary *const parameter_dictionary)
Definition: submap_2d.cc:35
std::vector< std::shared_ptr< Submap2D > > submaps() const
Definition: submap_2d.cc:137
void set_num_range_data(const int num_range_data)
Definition: submaps.h:79
void InsertRangeData(const sensor::RangeData &range_data)
Definition: submap_2d.cc:143
void set_finished(bool finished)
Definition: submaps.h:85
void ToResponseProto(const transform::Rigid3d &global_submap_pose, proto::SubmapQuery::Response *response) const override
Definition: submap_2d.cc:103
std::unique_ptr< GridInterface > CreateGrid(const Eigen::Vector2f &origin)
Definition: submap_2d.cc:159
const proto::SubmapsOptions2D options_
Definition: submap_2d.h:97
std::unique_ptr< RangeDataInserterInterface > CreateRangeDataInserter()
Definition: submap_2d.cc:153
transform::Rigid3d ToRigid3(const proto::Rigid3d &rigid)
Definition: transform.cc:71
std::vector< std::shared_ptr< Submap2D > > submaps_
Definition: submap_2d.h:99
Submap2D(const Eigen::Vector2f &origin, std::unique_ptr< Grid2D > grid)
Definition: submap_2d.cc:64
virtual bool DrawToSubmapTexture(proto::SubmapQuery::Response::SubmapTexture *const texture, transform::Rigid3d local_pose) const =0
proto::Rigid2d ToProto(const transform::Rigid2d &transform)
Definition: transform.cc:48
proto::ProbabilityGridRangeDataInserterOptions2D options_
std::unique_ptr< ProbabilityGridRangeDataInserter2D > range_data_inserter_
virtual void Insert(const sensor::RangeData &range_data, GridInterface *grid) const =0
const Grid2D * grid() const
Definition: submap_2d.h:54
transform::Rigid3d local_pose() const
Definition: submaps.h:75
string release
Definition: conf.py:73
ActiveSubmaps2D(const proto::SubmapsOptions2D &options)
Definition: submap_2d.cc:129
std::unique_ptr< LuaParameterDictionary > GetDictionary(const std::string &key)
int num_range_data() const
Definition: submaps.h:78
std::unique_ptr< Grid2D > grid_
Definition: submap_2d.h:63
proto::RangeDataInserterOptions CreateRangeDataInserterOptions(common::LuaParameterDictionary *const parameter_dictionary)
void UpdateFromProto(const proto::Submap &proto) override
Definition: submap_2d.cc:92
void AddSubmap(const Eigen::Vector2f &origin)
Definition: submap_2d.cc:177
proto::GridOptions2D CreateGridOptions2D(common::LuaParameterDictionary *const parameter_dictionary)
Definition: grid_2d.cc:23


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