probability_grid_points_processor.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2017 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 "Eigen/Core"
24 #include "cartographer/io/image.h"
27 
28 namespace cartographer {
29 namespace io {
30 namespace {
31 
32 void DrawTrajectoriesIntoImage(
33  const mapping::ProbabilityGrid& probability_grid,
34  const Eigen::Array2i& offset,
35  const std::vector<mapping::proto::Trajectory>& trajectories,
36  cairo_surface_t* cairo_surface) {
37  for (size_t i = 0; i < trajectories.size(); ++i) {
38  DrawTrajectory(trajectories[i], GetColor(i),
39  [&probability_grid,
40  &offset](const transform::Rigid3d& pose) -> Eigen::Array2i {
41  return probability_grid.limits().GetCellIndex(
42  pose.cast<float>().translation().head<2>()) -
43  offset;
44  },
45  cairo_surface);
46  }
47 }
48 
49 uint8 ProbabilityToColor(float probability_from_grid) {
50  const float probability = 1.f - probability_from_grid;
52  255 * ((probability - mapping::kMinProbability) /
54 }
55 
56 } // namespace
57 
59  const double resolution,
60  const mapping::proto::ProbabilityGridRangeDataInserterOptions2D&
61  probability_grid_range_data_inserter_options,
62  const DrawTrajectories& draw_trajectories,
63  std::unique_ptr<FileWriter> file_writer,
64  const std::vector<mapping::proto::Trajectory>& trajectories,
65  PointsProcessor* const next)
66  : draw_trajectories_(draw_trajectories),
67  trajectories_(trajectories),
68  file_writer_(std::move(file_writer)),
69  next_(next),
70  range_data_inserter_(probability_grid_range_data_inserter_options),
72 
73 std::unique_ptr<ProbabilityGridPointsProcessor>
75  const std::vector<mapping::proto::Trajectory>& trajectories,
76  const FileWriterFactory& file_writer_factory,
77  common::LuaParameterDictionary* const dictionary,
78  PointsProcessor* const next) {
79  const auto draw_trajectories = (!dictionary->HasKey("draw_trajectories") ||
80  dictionary->GetBool("draw_trajectories"))
83  return common::make_unique<ProbabilityGridPointsProcessor>(
84  dictionary->GetDouble("resolution"),
86  dictionary->GetDictionary("range_data_inserter").get()),
87  draw_trajectories,
88  file_writer_factory(dictionary->GetString("filename") + ".png"),
89  trajectories, next);
90 }
91 
93  std::unique_ptr<PointsBatch> batch) {
94  range_data_inserter_.Insert({batch->origin, batch->points, {}},
96  next_->Process(std::move(batch));
97 }
98 
100  Eigen::Array2i offset;
101  std::unique_ptr<Image> image =
103  if (image != nullptr) {
104  if (draw_trajectories_ ==
106  DrawTrajectoriesIntoImage(probability_grid_, offset, trajectories_,
107  image->GetCairoSurface().get());
108  }
109  image->WritePng(file_writer_.get());
110  CHECK(file_writer_->Close());
111  }
112 
113  switch (next_->Flush()) {
115  LOG(FATAL) << "ProbabilityGrid generation must be configured to occur "
116  "after any stages that require multiple passes.";
117 
119  return FlushResult::kFinished;
120  }
121  LOG(FATAL);
122  // The following unreachable return statement is needed to avoid a GCC bug
123  // described at https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81508
124  return FlushResult::kFinished;
125 }
126 
127 std::unique_ptr<Image> DrawProbabilityGrid(
128  const mapping::ProbabilityGrid& probability_grid, Eigen::Array2i* offset) {
129  mapping::CellLimits cell_limits;
130  probability_grid.ComputeCroppedLimits(offset, &cell_limits);
131  if (cell_limits.num_x_cells == 0 || cell_limits.num_y_cells == 0) {
132  LOG(WARNING) << "Not writing output: empty probability grid";
133  return nullptr;
134  }
135  auto image = common::make_unique<Image>(cell_limits.num_x_cells,
136  cell_limits.num_y_cells);
137  for (const Eigen::Array2i& xy_index :
138  mapping::XYIndexRangeIterator(cell_limits)) {
139  const Eigen::Array2i index = xy_index + *offset;
140  constexpr uint8 kUnknownValue = 128;
141  const uint8 value =
142  probability_grid.IsKnown(index)
143  ? ProbabilityToColor(probability_grid.GetProbability(index))
144  : kUnknownValue;
145  image->SetPixel(xy_index.x(), xy_index.y(), {{value, value, value}});
146  }
147  return image;
148 }
149 
151  constexpr int kInitialProbabilityGridSize = 100;
152  Eigen::Vector2d max =
153  0.5 * kInitialProbabilityGridSize * resolution * Eigen::Vector2d::Ones();
155  mapping::MapLimits(resolution, max,
156  mapping::CellLimits(kInitialProbabilityGridSize,
157  kInitialProbabilityGridSize)));
158 }
159 
160 } // namespace io
161 } // namespace cartographer
virtual void Process(std::unique_ptr< PointsBatch > points_batch)=0
std::function< std::unique_ptr< FileWriter >(const std::string &filename)> FileWriterFactory
Definition: file_writer.h:66
const std::vector< mapping::proto::Trajectory > trajectories_
void Process(std::unique_ptr< PointsBatch > batch) override
Rigid3< double > Rigid3d
ProbabilityGrid probability_grid_
proto::ProbabilityGridRangeDataInserterOptions2D CreateProbabilityGridRangeDataInserterOptions2D(common::LuaParameterDictionary *parameter_dictionary)
int RoundToInt(const float x)
Definition: port.h:41
std::unique_ptr< Image > DrawProbabilityGrid(const mapping::ProbabilityGrid &probability_grid, Eigen::Array2i *offset)
constexpr float kMinProbability
static std::unique_ptr< ProbabilityGridPointsProcessor > FromDictionary(const std::vector< mapping::proto::Trajectory > &trajectories, const FileWriterFactory &file_writer_factory, common::LuaParameterDictionary *dictionary, PointsProcessor *next)
constexpr float kMaxProbability
mapping::ProbabilityGrid CreateProbabilityGrid(const double resolution)
FloatColor GetColor(int id)
Definition: color.cc:61
std::unique_ptr< ProbabilityGridRangeDataInserter2D > range_data_inserter_
float GetProbability(const Eigen::Array2i &cell_index) const
void ComputeCroppedLimits(Eigen::Array2i *const offset, CellLimits *const limits) const
Definition: grid_2d.cc:101
virtual FlushResult Flush()=0
mapping::ProbabilityGridRangeDataInserter2D range_data_inserter_
bool IsKnown(const Eigen::Array2i &cell_index) const
Definition: grid_2d.cc:93
std::unique_ptr< LuaParameterDictionary > GetDictionary(const std::string &key)
transform::Rigid3d pose
virtual void Insert(const sensor::RangeData &range_data, GridInterface *grid) const override
ProbabilityGridPointsProcessor(double resolution, const mapping::proto::ProbabilityGridRangeDataInserterOptions2D &probability_grid_range_data_inserter_options, const DrawTrajectories &draw_trajectories, std::unique_ptr< FileWriter > file_writer, const std::vector< mapping::proto::Trajectory > &trajectorios, PointsProcessor *next)
void DrawTrajectory(const mapping::proto::Trajectory &trajectory, const FloatColor &color, const PoseToPixelFunction &pose_to_pixel, cairo_surface_t *surface)
uint8_t uint8
Definition: port.h:34


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