probability_grid.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  */
17 
18 #include <limits>
19 
23 
24 namespace cartographer {
25 namespace mapping {
26 
29 
30 ProbabilityGrid::ProbabilityGrid(const proto::Grid2D& proto) : Grid2D(proto) {
31  CHECK(proto.has_probability_grid_2d());
32 }
33 
34 // Sets the probability of the cell at 'cell_index' to the given
35 // 'probability'. Only allowed if the cell was unknown before.
36 void ProbabilityGrid::SetProbability(const Eigen::Array2i& cell_index,
37  const float probability) {
38  uint16& cell =
40  CHECK_EQ(cell, kUnknownProbabilityValue);
41  cell =
43  mutable_known_cells_box()->extend(cell_index.matrix());
44 }
45 
46 // Applies the 'odds' specified when calling ComputeLookupTableToApplyOdds()
47 // to the probability of the cell at 'cell_index' if the cell has not already
48 // been updated. Multiple updates of the same cell will be ignored until
49 // FinishUpdate() is called. Returns true if the cell was updated.
50 //
51 // If this is the first call to ApplyOdds() for the specified cell, its value
52 // will be set to probability corresponding to 'odds'.
53 bool ProbabilityGrid::ApplyLookupTable(const Eigen::Array2i& cell_index,
54  const std::vector<uint16>& table) {
55  DCHECK_EQ(table.size(), kUpdateMarker);
56  const int flat_index = ToFlatIndex(cell_index);
57  uint16* cell = &(*mutable_correspondence_cost_cells())[flat_index];
58  if (*cell >= kUpdateMarker) {
59  return false;
60  }
61  mutable_update_indices()->push_back(flat_index);
62  *cell = table[*cell];
63  DCHECK_GE(*cell, kUpdateMarker);
64  mutable_known_cells_box()->extend(cell_index.matrix());
65  return true;
66 }
67 
68 // Returns the probability of the cell with 'cell_index'.
69 float ProbabilityGrid::GetProbability(const Eigen::Array2i& cell_index) const {
70  if (!limits().Contains(cell_index)) return kMinProbability;
72  correspondence_cost_cells()[ToFlatIndex(cell_index)]));
73 }
74 
75 proto::Grid2D ProbabilityGrid::ToProto() const {
76  proto::Grid2D result;
77  result = Grid2D::ToProto();
78  result.mutable_probability_grid_2d();
79  return result;
80 }
81 
82 std::unique_ptr<Grid2D> ProbabilityGrid::ComputeCroppedGrid() const {
83  Eigen::Array2i offset;
84  CellLimits cell_limits;
85  ComputeCroppedLimits(&offset, &cell_limits);
86  const double resolution = limits().resolution();
87  const Eigen::Vector2d max =
88  limits().max() - resolution * Eigen::Vector2d(offset.y(), offset.x());
89  std::unique_ptr<ProbabilityGrid> cropped_grid =
90  common::make_unique<ProbabilityGrid>(
91  MapLimits(resolution, max, cell_limits));
92  for (const Eigen::Array2i& xy_index : XYIndexRangeIterator(cell_limits)) {
93  if (!IsKnown(xy_index + offset)) continue;
94  cropped_grid->SetProbability(xy_index, GetProbability(xy_index + offset));
95  }
96 
97  return std::unique_ptr<Grid2D>(cropped_grid.release());
98 }
99 
101  proto::SubmapQuery::Response::SubmapTexture* const texture,
102  transform::Rigid3d local_pose) const {
103  Eigen::Array2i offset;
104  CellLimits cell_limits;
105  ComputeCroppedLimits(&offset, &cell_limits);
106 
107  std::string cells;
108  for (const Eigen::Array2i& xy_index : XYIndexRangeIterator(cell_limits)) {
109  if (!IsKnown(xy_index + offset)) {
110  cells.push_back(0 /* unknown log odds value */);
111  cells.push_back(0 /* alpha */);
112  continue;
113  }
114  // We would like to add 'delta' but this is not possible using a value and
115  // alpha. We use premultiplied alpha, so when 'delta' is positive we can
116  // add it by setting 'alpha' to zero. If it is negative, we set 'value' to
117  // zero, and use 'alpha' to subtract. This is only correct when the pixel
118  // is currently white, so walls will look too gray. This should be hard to
119  // detect visually for the user, though.
120  const int delta =
121  128 - ProbabilityToLogOddsInteger(GetProbability(xy_index + offset));
122  const uint8 alpha = delta > 0 ? 0 : -delta;
123  const uint8 value = delta > 0 ? delta : 0;
124  cells.push_back(value);
125  cells.push_back((value || alpha) ? alpha : 1);
126  }
127 
128  common::FastGzipString(cells, texture->mutable_cells());
129  texture->set_width(cell_limits.num_x_cells);
130  texture->set_height(cell_limits.num_y_cells);
131  const double resolution = limits().resolution();
132  texture->set_resolution(resolution);
133  const double max_x = limits().max().x() - resolution * offset.y();
134  const double max_y = limits().max().y() - resolution * offset.x();
135  *texture->mutable_slice_pose() = transform::ToProto(
136  local_pose.inverse() *
137  transform::Rigid3d::Translation(Eigen::Vector3d(max_x, max_y, 0.)));
138 
139  return true;
140 }
141 
142 } // namespace mapping
143 } // namespace cartographer
virtual std::unique_ptr< Grid2D > ComputeCroppedGrid() const override
const Eigen::Vector2d & max() const
Definition: map_limits.h:61
void SetProbability(const Eigen::Array2i &cell_index, const float probability)
float ValueToCorrespondenceCost(const uint16 value)
constexpr float kMinProbability
constexpr float kMaxCorrespondenceCost
virtual bool DrawToSubmapTexture(proto::SubmapQuery::Response::SubmapTexture *const texture, transform::Rigid3d local_pose) const override
float CorrespondenceCostToProbability(const float correspondence_cost)
Eigen::AlignedBox2i * mutable_known_cells_box()
Definition: grid_2d.h:88
uint16_t uint16
Definition: port.h:35
virtual proto::Grid2D ToProto() const
Definition: grid_2d.cc:147
proto::Rigid2d ToProto(const transform::Rigid2d &transform)
Definition: transform.cc:48
uint8 ProbabilityToLogOddsInteger(const float probability)
Definition: submaps.h:46
ProbabilityGrid(const MapLimits &limits)
std::vector< uint16 > * mutable_correspondence_cost_cells()
Definition: grid_2d.h:84
const std::vector< uint16 > & correspondence_cost_cells() const
Definition: grid_2d.h:76
constexpr uint16 kUnknownProbabilityValue
float GetProbability(const Eigen::Array2i &cell_index) const
void ComputeCroppedLimits(Eigen::Array2i *const offset, CellLimits *const limits) const
Definition: grid_2d.cc:101
float ProbabilityToCorrespondenceCost(const float probability)
bool IsKnown(const Eigen::Array2i &cell_index) const
Definition: grid_2d.cc:93
virtual proto::Grid2D ToProto() const override
void FastGzipString(const std::string &uncompressed, std::string *compressed)
Definition: port.h:49
const MapLimits & limits() const
Definition: grid_2d.h:41
uint16 CorrespondenceCostToValue(const float correspondence_cost)
constexpr uint16 kUpdateMarker
int ToFlatIndex(const Eigen::Array2i &cell_index) const
Definition: grid_2d.cc:168
bool ApplyLookupTable(const Eigen::Array2i &cell_index, const std::vector< uint16 > &table)
std::vector< int > * mutable_update_indices()
Definition: grid_2d.h:87
static Rigid3 Translation(const Vector &vector)
constexpr float kMinCorrespondenceCost
uint8_t uint8
Definition: port.h:34


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