ceres_scan_matcher_3d.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 <string>
20 #include <utility>
21 #include <vector>
22 
32 #include "ceres/ceres.h"
33 #include "glog/logging.h"
34 
35 namespace cartographer {
36 namespace mapping {
37 namespace scan_matching {
38 
39 proto::CeresScanMatcherOptions3D CreateCeresScanMatcherOptions3D(
40  common::LuaParameterDictionary* const parameter_dictionary) {
41  proto::CeresScanMatcherOptions3D options;
42  for (int i = 0;; ++i) {
43  const std::string lua_identifier =
44  "occupied_space_weight_" + std::to_string(i);
45  if (!parameter_dictionary->HasKey(lua_identifier)) {
46  break;
47  }
48  options.add_occupied_space_weight(
49  parameter_dictionary->GetDouble(lua_identifier));
50  }
51  options.set_translation_weight(
52  parameter_dictionary->GetDouble("translation_weight"));
53  options.set_rotation_weight(
54  parameter_dictionary->GetDouble("rotation_weight"));
55  options.set_only_optimize_yaw(
56  parameter_dictionary->GetBool("only_optimize_yaw"));
57  *options.mutable_ceres_solver_options() =
59  parameter_dictionary->GetDictionary("ceres_solver_options").get());
60  return options;
61 }
62 
64  const proto::CeresScanMatcherOptions3D& options)
65  : options_(options),
66  ceres_solver_options_(
67  common::CreateCeresSolverOptions(options.ceres_solver_options())) {
68  ceres_solver_options_.linear_solver_type = ceres::DENSE_QR;
69 }
70 
72  const Eigen::Vector3d& target_translation,
73  const transform::Rigid3d& initial_pose_estimate,
74  const std::vector<PointCloudAndHybridGridPointers>&
75  point_clouds_and_hybrid_grids,
76  transform::Rigid3d* const pose_estimate,
77  ceres::Solver::Summary* const summary) {
78  ceres::Problem problem;
79  optimization::CeresPose ceres_pose(
80  initial_pose_estimate, nullptr /* translation_parameterization */,
81  options_.only_optimize_yaw()
82  ? std::unique_ptr<ceres::LocalParameterization>(
83  common::make_unique<ceres::AutoDiffLocalParameterization<
84  YawOnlyQuaternionPlus, 4, 1>>())
85  : std::unique_ptr<ceres::LocalParameterization>(
86  common::make_unique<ceres::QuaternionParameterization>()),
87  &problem);
88 
89  CHECK_EQ(options_.occupied_space_weight_size(),
90  point_clouds_and_hybrid_grids.size());
91  for (size_t i = 0; i != point_clouds_and_hybrid_grids.size(); ++i) {
92  CHECK_GT(options_.occupied_space_weight(i), 0.);
93  const sensor::PointCloud& point_cloud =
94  *point_clouds_and_hybrid_grids[i].first;
95  const HybridGrid& hybrid_grid = *point_clouds_and_hybrid_grids[i].second;
96  problem.AddResidualBlock(
98  options_.occupied_space_weight(i) /
99  std::sqrt(static_cast<double>(point_cloud.size())),
100  point_cloud, hybrid_grid),
101  nullptr /* loss function */, ceres_pose.translation(),
102  ceres_pose.rotation());
103  }
104  CHECK_GT(options_.translation_weight(), 0.);
105  problem.AddResidualBlock(
107  options_.translation_weight(), target_translation),
108  nullptr /* loss function */, ceres_pose.translation());
109  CHECK_GT(options_.rotation_weight(), 0.);
110  problem.AddResidualBlock(
112  options_.rotation_weight(), initial_pose_estimate.rotation()),
113  nullptr /* loss function */, ceres_pose.rotation());
114 
115  ceres::Solve(ceres_solver_options_, &problem, summary);
116 
117  *pose_estimate = ceres_pose.ToRigid();
118 }
119 
120 } // namespace scan_matching
121 } // namespace mapping
122 } // namespace cartographer
_Unique_if< T >::_Single_object make_unique(Args &&... args)
Definition: make_unique.h:46
proto::CeresScanMatcherOptions3D CreateCeresScanMatcherOptions3D(common::LuaParameterDictionary *const parameter_dictionary)
static ceres::CostFunction * CreateAutoDiffCostFunction(const double scaling_factor, const Eigen::Vector3d &target_translation)
const Quaternion & rotation() const
static ceres::CostFunction * CreateAutoDiffCostFunction(const double scaling_factor, const sensor::PointCloud &point_cloud, const mapping::HybridGrid &hybrid_grid)
proto::ProbabilityGridRangeDataInserterOptions2D options_
CeresScanMatcher3D(const proto::CeresScanMatcherOptions3D &options)
void Match(const Eigen::Vector3d &target_translation, const transform::Rigid3d &initial_pose_estimate, const std::vector< PointCloudAndHybridGridPointers > &point_clouds_and_hybrid_grids, transform::Rigid3d *pose_estimate, ceres::Solver::Summary *summary)
ceres::Solver::Options CreateCeresSolverOptions(const proto::CeresSolverOptions &proto)
std::vector< Eigen::Vector3f > PointCloud
Definition: point_cloud.h:32
static ceres::CostFunction * CreateAutoDiffCostFunction(const double scaling_factor, const Eigen::Quaterniond &target_rotation)
std::unique_ptr< LuaParameterDictionary > GetDictionary(const std::string &key)
proto::CeresSolverOptions CreateCeresSolverOptionsProto(common::LuaParameterDictionary *parameter_dictionary)


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