|
| 1 | +/* |
| 2 | + * Copyright 2019 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 | +#ifndef CARTOGRAPHER_MAPPING_INTERNAL_3D_SCAN_MATCHING_INTENSITY_COST_FUNCTION_3D_H_ |
| 18 | +#define CARTOGRAPHER_MAPPING_INTERNAL_3D_SCAN_MATCHING_INTENSITY_COST_FUNCTION_3D_H_ |
| 19 | + |
| 20 | +#include "Eigen/Core" |
| 21 | +#include "cartographer/mapping/3d/hybrid_grid.h" |
| 22 | +#include "cartographer/mapping/internal/3d/scan_matching/interpolated_grid.h" |
| 23 | +#include "cartographer/sensor/point_cloud.h" |
| 24 | +#include "cartographer/transform/rigid_transform.h" |
| 25 | +#include "cartographer/transform/transform.h" |
| 26 | +#include "ceres/ceres.h" |
| 27 | + |
| 28 | +namespace cartographer { |
| 29 | +namespace mapping { |
| 30 | +namespace scan_matching { |
| 31 | + |
| 32 | +// Computes a cost for matching the 'point_cloud' to the 'hybrid_grid' with a |
| 33 | +// 'translation' and 'rotation'. The cost increases when points fall into space |
| 34 | +// for which different intensity has been observed, i.e. at voxels with different |
| 35 | +// values. Only points up to a certain threshold are evaluated which is intended |
| 36 | +// to ignore data from retroreflections. |
| 37 | +class IntensityCostFunction3D { |
| 38 | + public: |
| 39 | + static ceres::CostFunction* CreateAutoDiffCostFunction( |
| 40 | + const double scaling_factor, const float intensity_threshold, |
| 41 | + const sensor::PointCloud& point_cloud, |
| 42 | + const IntensityHybridGrid& hybrid_grid); |
| 43 | + |
| 44 | + template <typename T> |
| 45 | + bool operator()(const T* const translation, const T* const rotation, |
| 46 | + T* const residual) const { |
| 47 | + const transform::Rigid3<T> transform( |
| 48 | + Eigen::Map<const Eigen::Matrix<T, 3, 1>>(translation), |
| 49 | + Eigen::Quaternion<T>(rotation[0], rotation[1], rotation[2], |
| 50 | + rotation[3])); |
| 51 | + return Evaluate(transform, residual); |
| 52 | + } |
| 53 | + |
| 54 | + private: |
| 55 | + IntensityCostFunction3D(const double scaling_factor, |
| 56 | + const float intensity_threshold, |
| 57 | + const sensor::PointCloud& point_cloud, |
| 58 | + const IntensityHybridGrid& hybrid_grid) |
| 59 | + : scaling_factor_(scaling_factor), |
| 60 | + intensity_threshold_(intensity_threshold), |
| 61 | + point_cloud_(point_cloud), |
| 62 | + interpolated_grid_(hybrid_grid) {} |
| 63 | + |
| 64 | + IntensityCostFunction3D(const IntensityCostFunction3D&) = delete; |
| 65 | + IntensityCostFunction3D& operator=(const IntensityCostFunction3D&) = delete; |
| 66 | + |
| 67 | + template <typename T> |
| 68 | + bool Evaluate(const transform::Rigid3<T>& transform, |
| 69 | + T* const residual) const { |
| 70 | + for (size_t i = 0; i < point_cloud_.size(); ++i) { |
| 71 | + if (point_cloud_.intensities()[i] > intensity_threshold_) { |
| 72 | + residual[i] = T(0.f); |
| 73 | + } else { |
| 74 | + const Eigen::Matrix<T, 3, 1> point = point_cloud_[i].position.cast<T>(); |
| 75 | + const T intensity = T(point_cloud_.intensities()[i]); |
| 76 | + |
| 77 | + const Eigen::Matrix<T, 3, 1> world = transform * point; |
| 78 | + const T interpolated_intensity = |
| 79 | + interpolated_grid_.GetInterpolatedValue(world[0], world[1], |
| 80 | + world[2]); |
| 81 | + residual[i] = scaling_factor_ * (interpolated_intensity - intensity); |
| 82 | + } |
| 83 | + } |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + const double scaling_factor_; |
| 88 | + // We will ignore returns with intensity above this threshold. |
| 89 | + const float intensity_threshold_; |
| 90 | + const sensor::PointCloud& point_cloud_; |
| 91 | + const InterpolatedIntensityGrid interpolated_grid_; |
| 92 | +}; |
| 93 | + |
| 94 | +} // namespace scan_matching |
| 95 | +} // namespace mapping |
| 96 | +} // namespace cartographer |
| 97 | + |
| 98 | +#endif // CARTOGRAPHER_MAPPING_INTERNAL_3D_SCAN_MATCHING_INTENSITY_COST_FUNCTION_3D_H_ |
0 commit comments