tsdf_match_cost_function_2d.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2018 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "Eigen/Core"
00018 #include "Eigen/Geometry"
00019 #include "cartographer/mapping/internal/2d/scan_matching/interpolated_tsdf_2d.h"
00020 #include "cartographer/sensor/point_cloud.h"
00021 #include "ceres/ceres.h"
00022 
00023 namespace cartographer {
00024 namespace mapping {
00025 namespace scan_matching {
00026 
00027 // Computes a cost for matching the 'point_cloud' in the 'grid' at
00028 // a 'pose'. The cost increases with the signed distance of the matched point
00029 // location in the 'grid'.
00030 class TSDFMatchCostFunction2D {
00031  public:
00032   TSDFMatchCostFunction2D(const double residual_scaling_factor,
00033                           const sensor::PointCloud& point_cloud,
00034                           const TSDF2D& grid)
00035       : residual_scaling_factor_(residual_scaling_factor),
00036         point_cloud_(point_cloud),
00037         interpolated_grid_(grid) {}
00038 
00039   template <typename T>
00040   bool operator()(const T* const pose, T* residual) const {
00041     const Eigen::Matrix<T, 2, 1> translation(pose[0], pose[1]);
00042     const Eigen::Rotation2D<T> rotation(pose[2]);
00043     const Eigen::Matrix<T, 2, 2> rotation_matrix = rotation.toRotationMatrix();
00044     Eigen::Matrix<T, 3, 3> transform;
00045     transform << rotation_matrix, translation, T(0.), T(0.), T(1.);
00046 
00047     T summed_weight = T(0);
00048     for (size_t i = 0; i < point_cloud_.size(); ++i) {
00049       // Note that this is a 2D point. The third component is a scaling factor.
00050       const Eigen::Matrix<T, 3, 1> point((T(point_cloud_[i].position.x())),
00051                                          (T(point_cloud_[i].position.y())),
00052                                          T(1.));
00053       const Eigen::Matrix<T, 3, 1> world = transform * point;
00054       const T point_weight = interpolated_grid_.GetWeight(world[0], world[1]);
00055       summed_weight += point_weight;
00056       residual[i] =
00057           T(point_cloud_.size()) * residual_scaling_factor_ *
00058           interpolated_grid_.GetCorrespondenceCost(world[0], world[1]) *
00059           point_weight;
00060     }
00061     if (summed_weight == T(0)) return false;
00062     for (size_t i = 0; i < point_cloud_.size(); ++i) {
00063       residual[i] /= summed_weight;
00064     }
00065     return true;
00066   }
00067 
00068  private:
00069   TSDFMatchCostFunction2D(const TSDFMatchCostFunction2D&) = delete;
00070   TSDFMatchCostFunction2D& operator=(const TSDFMatchCostFunction2D&) = delete;
00071 
00072   const double residual_scaling_factor_;
00073   const sensor::PointCloud& point_cloud_;
00074   const InterpolatedTSDF2D interpolated_grid_;
00075 };
00076 
00077 ceres::CostFunction* CreateTSDFMatchCostFunction2D(
00078     const double scaling_factor, const sensor::PointCloud& point_cloud,
00079     const TSDF2D& tsdf) {
00080   return new ceres::AutoDiffCostFunction<TSDFMatchCostFunction2D,
00081                                          ceres::DYNAMIC /* residuals */,
00082                                          3 /* pose variables */>(
00083       new TSDFMatchCostFunction2D(scaling_factor, point_cloud, tsdf),
00084       point_cloud.size());
00085 }
00086 
00087 }  // namespace scan_matching
00088 }  // namespace mapping
00089 }  // namespace cartographer


cartographer
Author(s): The Cartographer Authors
autogenerated on Thu May 9 2019 02:27:36