small_gicp
robust_kernel.hpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright 2024 Kenji Koide
2 // SPDX-License-Identifier: MIT
3 #pragma once
4 
5 #include <Eigen/Core>
6 #include <Eigen/Geometry>
7 
8 namespace small_gicp {
9 
11 struct Huber {
12 public:
14  struct Setting {
15  double c = 1.0;
16  };
17 
19  Huber(const Setting& setting) : c(setting.c) {}
20 
24  double weight(double e) const {
25  const double e_abs = std::abs(e);
26  return e_abs < c ? 1.0 : c / e_abs;
27  }
28 
29 public:
30  const double c;
31 };
32 
34 struct Cauchy {
35 public:
37  struct Setting {
38  double c = 1.0;
39  };
40 
42  Cauchy(const Setting& setting) : c(setting.c) {}
43 
47  double weight(double e) const { return c / (c + e * e); }
48 
49 public:
50  const double c;
51 };
52 
56 template <typename Kernel, typename Factor>
57 struct RobustFactor {
58 public:
60  struct Setting {
61  typename Kernel::Setting robust_kernel;
62  typename Factor::Setting factor;
63  };
64 
66  RobustFactor(const Setting& setting = Setting()) : robust_kernel(setting.robust_kernel), factor(setting.factor) {}
67 
69  template <typename TargetPointCloud, typename SourcePointCloud, typename TargetTree, typename CorrespondenceRejector>
70  bool linearize(
71  const TargetPointCloud& target,
72  const SourcePointCloud& source,
73  const TargetTree& target_tree,
74  const Eigen::Isometry3d& T,
75  size_t source_index,
76  const CorrespondenceRejector& rejector,
77  Eigen::Matrix<double, 6, 6>* H,
78  Eigen::Matrix<double, 6, 1>* b,
79  double* e) {
80  if (!factor.linearize(target, source, target_tree, T, source_index, rejector, H, b, e)) {
81  return false;
82  }
83 
84  // Robustify the linearized factor
85  const double w = robust_kernel.weight(std::sqrt(*e));
86  *H *= w;
87  *b *= w;
88  *e *= w;
89 
90  return true;
91  }
92 
94  template <typename TargetPointCloud, typename SourcePointCloud>
95  double error(const TargetPointCloud& target, const SourcePointCloud& source, const Eigen::Isometry3d& T) const {
96  const double e = factor.error(target, source, T);
97  return robust_kernel.weight(std::sqrt(e)) * e;
98  }
99 
101  bool inlier() const { return factor.inlier(); }
102 
103 public:
104  Kernel robust_kernel;
105  Factor factor;
106 };
107 
108 } // namespace small_gicp
Definition: flat_container.hpp:12
Huber robust kernel setting.
Definition: robust_kernel.hpp:37
double c
Kernel width.
Definition: robust_kernel.hpp:38
Cauchy robust kernel.
Definition: robust_kernel.hpp:34
const double c
Kernel width.
Definition: robust_kernel.hpp:50
double weight(double e) const
Compute the weight for an error.
Definition: robust_kernel.hpp:47
Cauchy(const Setting &setting)
Constructor.
Definition: robust_kernel.hpp:42
Huber robust kernel setting.
Definition: robust_kernel.hpp:14
double c
Kernel width.
Definition: robust_kernel.hpp:15
Huber robust kernel.
Definition: robust_kernel.hpp:11
Huber(const Setting &setting)
Constructor.
Definition: robust_kernel.hpp:19
double weight(double e) const
Compute the weight for an error.
Definition: robust_kernel.hpp:24
const double c
Kernel width.
Definition: robust_kernel.hpp:30
Robust factor setting.
Definition: robust_kernel.hpp:60
Kernel::Setting robust_kernel
Robust kernel setting.
Definition: robust_kernel.hpp:61
Factor::Setting factor
Factor setting.
Definition: robust_kernel.hpp:62
Robustify a factor with a robust kernel.
Definition: robust_kernel.hpp:57
bool inlier() const
Check if the factor is inlier.
Definition: robust_kernel.hpp:101
bool linearize(const TargetPointCloud &target, const SourcePointCloud &source, const TargetTree &target_tree, const Eigen::Isometry3d &T, size_t source_index, const CorrespondenceRejector &rejector, Eigen::Matrix< double, 6, 6 > *H, Eigen::Matrix< double, 6, 1 > *b, double *e)
Linearize the factor.
Definition: robust_kernel.hpp:70
double error(const TargetPointCloud &target, const SourcePointCloud &source, const Eigen::Isometry3d &T) const
Evaluate error.
Definition: robust_kernel.hpp:95
Factor factor
Factor.
Definition: robust_kernel.hpp:105
RobustFactor(const Setting &setting=Setting())
Constructor.
Definition: robust_kernel.hpp:66
Kernel robust_kernel
Robust kernel.
Definition: robust_kernel.hpp:104