Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00028
00029
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
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 ,
00082 3 >(
00083 new TSDFMatchCostFunction2D(scaling_factor, point_cloud, tsdf),
00084 point_cloud.size());
00085 }
00086
00087 }
00088 }
00089 }