fast_correlative_scan_matcher_2d.h
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 
17 // This is an implementation of the algorithm described in "Real-Time
18 // Correlative Scan Matching" by Olson.
19 //
20 // It is similar to the RealTimeCorrelativeScanMatcher but has a different
21 // trade-off: Scan matching is faster because more effort is put into the
22 // precomputation done for a given map. However, this map is immutable after
23 // construction.
24 
25 #ifndef CARTOGRAPHER_MAPPING_INTERNAL_2D_SCAN_MATCHING_FAST_CORRELATIVE_SCAN_MATCHER_2D_H_
26 #define CARTOGRAPHER_MAPPING_INTERNAL_2D_SCAN_MATCHING_FAST_CORRELATIVE_SCAN_MATCHER_2D_H_
27 
28 #include <memory>
29 #include <vector>
30 
31 #include "Eigen/Core"
35 #include "cartographer/mapping/proto/scan_matching/fast_correlative_scan_matcher_options_2d.pb.h"
37 
38 namespace cartographer {
39 namespace mapping {
40 namespace scan_matching {
41 
42 proto::FastCorrelativeScanMatcherOptions2D
44  common::LuaParameterDictionary* parameter_dictionary);
45 
46 // A precomputed grid that contains in each cell (x0, y0) the maximum
47 // probability in the width x width area defined by x0 <= x < x0 + width and
48 // y0 <= y < y0.
50  public:
51  PrecomputationGrid2D(const Grid2D& grid, const CellLimits& limits, int width,
52  std::vector<float>* reusable_intermediate_grid);
53 
54  // Returns a value between 0 and 255 to represent probabilities between
55  // min_score and max_score.
56  int GetValue(const Eigen::Array2i& xy_index) const {
57  const Eigen::Array2i local_xy_index = xy_index - offset_;
58  // The static_cast<unsigned> is for performance to check with 2 comparisons
59  // xy_index.x() < offset_.x() || xy_index.y() < offset_.y() ||
60  // local_xy_index.x() >= wide_limits_.num_x_cells ||
61  // local_xy_index.y() >= wide_limits_.num_y_cells
62  // instead of using 4 comparisons.
63  if (static_cast<unsigned>(local_xy_index.x()) >=
64  static_cast<unsigned>(wide_limits_.num_x_cells) ||
65  static_cast<unsigned>(local_xy_index.y()) >=
66  static_cast<unsigned>(wide_limits_.num_y_cells)) {
67  return 0;
68  }
69  const int stride = wide_limits_.num_x_cells;
70  return cells_[local_xy_index.x() + local_xy_index.y() * stride];
71  }
72 
73  // Maps values from [0, 255] to [min_score, max_score].
74  float ToScore(float value) const {
75  return min_score_ + value * ((max_score_ - min_score_) / 255.f);
76  }
77 
78  private:
79  uint8 ComputeCellValue(float probability) const;
80 
81  // Offset of the precomputation grid in relation to the 'grid'
82  // including the additional 'width' - 1 cells.
83  const Eigen::Array2i offset_;
84 
85  // Size of the precomputation grid.
87 
88  const float min_score_;
89  const float max_score_;
90 
91  // Probabilites mapped to 0 to 255.
92  std::vector<uint8> cells_;
93 };
94 
96  public:
98  const Grid2D& grid,
99  const proto::FastCorrelativeScanMatcherOptions2D& options);
100 
101  const PrecomputationGrid2D& Get(int index) {
102  return precomputation_grids_[index];
103  }
104 
105  int max_depth() const { return precomputation_grids_.size() - 1; }
106 
107  private:
108  std::vector<PrecomputationGrid2D> precomputation_grids_;
109 };
110 
111 // An implementation of "Real-Time Correlative Scan Matching" by Olson.
113  public:
115  const Grid2D& grid,
116  const proto::FastCorrelativeScanMatcherOptions2D& options);
118 
121  delete;
122 
123  // Aligns 'point_cloud' within the 'grid' given an
124  // 'initial_pose_estimate'. If a score above 'min_score' (excluding equality)
125  // is possible, true is returned, and 'score' and 'pose_estimate' are updated
126  // with the result.
127  bool Match(const transform::Rigid2d& initial_pose_estimate,
128  const sensor::PointCloud& point_cloud, float min_score,
129  float* score, transform::Rigid2d* pose_estimate) const;
130 
131  // Aligns 'point_cloud' within the full 'grid', i.e., not
132  // restricted to the configured search window. If a score above 'min_score'
133  // (excluding equality) is possible, true is returned, and 'score' and
134  // 'pose_estimate' are updated with the result.
135  bool MatchFullSubmap(const sensor::PointCloud& point_cloud, float min_score,
136  float* score, transform::Rigid2d* pose_estimate) const;
137 
138  private:
139  // The actual implementation of the scan matcher, called by Match() and
140  // MatchFullSubmap() with appropriate 'initial_pose_estimate' and
141  // 'search_parameters'.
142  bool MatchWithSearchParameters(
143  SearchParameters search_parameters,
144  const transform::Rigid2d& initial_pose_estimate,
145  const sensor::PointCloud& point_cloud, float min_score, float* score,
146  transform::Rigid2d* pose_estimate) const;
147  std::vector<Candidate2D> ComputeLowestResolutionCandidates(
148  const std::vector<DiscreteScan2D>& discrete_scans,
149  const SearchParameters& search_parameters) const;
150  std::vector<Candidate2D> GenerateLowestResolutionCandidates(
151  const SearchParameters& search_parameters) const;
152  void ScoreCandidates(const PrecomputationGrid2D& precomputation_grid,
153  const std::vector<DiscreteScan2D>& discrete_scans,
154  const SearchParameters& search_parameters,
155  std::vector<Candidate2D>* const candidates) const;
156  Candidate2D BranchAndBound(const std::vector<DiscreteScan2D>& discrete_scans,
157  const SearchParameters& search_parameters,
158  const std::vector<Candidate2D>& candidates,
159  int candidate_depth, float min_score) const;
160 
161  const proto::FastCorrelativeScanMatcherOptions2D options_;
163  std::unique_ptr<PrecomputationGridStack2D> precomputation_grid_stack_;
164 };
165 
166 } // namespace scan_matching
167 } // namespace mapping
168 } // namespace cartographer
169 
170 #endif // CARTOGRAPHER_MAPPING_INTERNAL_2D_SCAN_MATCHING_FAST_CORRELATIVE_SCAN_MATCHER_2D_H_
proto::FastCorrelativeScanMatcherOptions2D CreateFastCorrelativeScanMatcherOptions2D(common::LuaParameterDictionary *const parameter_dictionary)
std::vector< Eigen::Vector3f > PointCloud
Definition: point_cloud.h:32
PrecomputationGrid2D(const Grid2D &grid, const CellLimits &limits, int width, std::vector< float > *reusable_intermediate_grid)
uint8_t uint8
Definition: port.h:34


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