small_gicp
reduction_tbb.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 <tbb/tbb.h>
6 #include <Eigen/Core>
7 
8 namespace small_gicp {
9 
11 template <typename TargetPointCloud, typename SourcePointCloud, typename TargetTree, typename CorrespondenceRejector, typename Factor>
12 struct LinearizeSum {
14  const TargetPointCloud& target,
15  const SourcePointCloud& source,
16  const TargetTree& target_tree,
17  const CorrespondenceRejector& rejector,
18  const Eigen::Isometry3d& T,
19  std::vector<Factor>& factors)
20  : target(target),
21  source(source),
24  T(T),
26  H(Eigen::Matrix<double, 6, 6>::Zero()),
27  b(Eigen::Matrix<double, 6, 1>::Zero()),
28  e(0.0) {}
29 
30  LinearizeSum(LinearizeSum& x, tbb::split)
31  : target(x.target),
32  source(x.source),
34  rejector(x.rejector),
35  T(x.T),
36  factors(x.factors),
37  H(Eigen::Matrix<double, 6, 6>::Zero()),
38  b(Eigen::Matrix<double, 6, 1>::Zero()),
39  e(0.0) {}
40 
41  void operator()(const tbb::blocked_range<size_t>& r) {
42  Eigen::Matrix<double, 6, 6> Ht = H;
43  Eigen::Matrix<double, 6, 1> bt = b;
44  double et = e;
45 
46  for (size_t i = r.begin(); i != r.end(); i++) {
47  Eigen::Matrix<double, 6, 6> Hi;
48  Eigen::Matrix<double, 6, 1> bi;
49  double ei;
50 
51  if (!factors[i].linearize(target, source, target_tree, T, i, rejector, &Hi, &bi, &ei)) {
52  continue;
53  }
54 
55  Ht += Hi;
56  bt += bi;
57  et += ei;
58  }
59 
60  H = Ht;
61  b = bt;
62  e = et;
63  }
64 
65  void join(const LinearizeSum& y) {
66  H += y.H;
67  b += y.b;
68  e += y.e;
69  }
70 
71  const TargetPointCloud& target;
72  const SourcePointCloud& source;
73  const TargetTree& target_tree;
74  const CorrespondenceRejector& rejector;
75  const Eigen::Isometry3d& T;
76  std::vector<Factor>& factors;
77 
78  Eigen::Matrix<double, 6, 6> H;
79  Eigen::Matrix<double, 6, 1> b;
80  double e;
81 };
82 
84 template <typename TargetPointCloud, typename SourcePointCloud, typename Factor>
85 struct ErrorSum {
86  ErrorSum(const TargetPointCloud& target, const SourcePointCloud& source, const Eigen::Isometry3d& T, std::vector<Factor>& factors)
87  : target(target),
88  source(source),
89  T(T),
91  e(0.0) {}
92 
93  ErrorSum(ErrorSum& x, tbb::split) : target(x.target), source(x.source), T(x.T), factors(x.factors), e(0.0) {}
94 
95  void operator()(const tbb::blocked_range<size_t>& r) {
96  double et = e;
97  for (size_t i = r.begin(); i != r.end(); i++) {
98  et += factors[i].error(target, source, T);
99  }
100  e = et;
101  }
102 
103  void join(const ErrorSum& y) { e += y.e; }
104 
105  const TargetPointCloud& target;
106  const SourcePointCloud& source;
107  const Eigen::Isometry3d& T;
108  std::vector<Factor>& factors;
109 
110  double e;
111 };
112 
116 
117  template <typename TargetPointCloud, typename SourcePointCloud, typename TargetTree, typename CorrespondenceRejector, typename Factor>
118  std::tuple<Eigen::Matrix<double, 6, 6>, Eigen::Matrix<double, 6, 1>, double> linearize(
119  const TargetPointCloud& target,
120  const SourcePointCloud& source,
121  const TargetTree& target_tree,
122  const CorrespondenceRejector& rejector,
123  const Eigen::Isometry3d& T,
124  std::vector<Factor>& factors) const {
125  //
126  LinearizeSum<TargetPointCloud, SourcePointCloud, TargetTree, CorrespondenceRejector, Factor> sum(target, source, target_tree, rejector, T, factors);
127 
128  tbb::parallel_reduce(tbb::blocked_range<size_t>(0, factors.size(), 8), sum);
129 
130  return {sum.H, sum.b, sum.e};
131  }
132 
133  template <typename TargetPointCloud, typename SourcePointCloud, typename Factor>
134  double error(const TargetPointCloud& target, const SourcePointCloud& source, const Eigen::Isometry3d& T, std::vector<Factor>& factors) const {
135  ErrorSum<TargetPointCloud, SourcePointCloud, Factor> sum(target, source, T, factors);
136  tbb::parallel_reduce(tbb::blocked_range<size_t>(0, factors.size(), 16), sum);
137  return sum.e;
138  }
139 };
140 
141 } // namespace small_gicp
Definition: flat_container.hpp:12
Summation for evaluated errors.
Definition: reduction_tbb.hpp:85
const TargetPointCloud & target
Definition: reduction_tbb.hpp:105
void join(const ErrorSum &y)
Definition: reduction_tbb.hpp:103
void operator()(const tbb::blocked_range< size_t > &r)
Definition: reduction_tbb.hpp:95
const Eigen::Isometry3d & T
Definition: reduction_tbb.hpp:107
double e
Definition: reduction_tbb.hpp:110
ErrorSum(ErrorSum &x, tbb::split)
Definition: reduction_tbb.hpp:93
ErrorSum(const TargetPointCloud &target, const SourcePointCloud &source, const Eigen::Isometry3d &T, std::vector< Factor > &factors)
Definition: reduction_tbb.hpp:86
const SourcePointCloud & source
Definition: reduction_tbb.hpp:106
std::vector< Factor > & factors
Definition: reduction_tbb.hpp:108
Summation for linearized systems.
Definition: reduction_tbb.hpp:12
void join(const LinearizeSum &y)
Definition: reduction_tbb.hpp:65
LinearizeSum(LinearizeSum &x, tbb::split)
Definition: reduction_tbb.hpp:30
std::vector< Factor > & factors
Definition: reduction_tbb.hpp:76
const SourcePointCloud & source
Definition: reduction_tbb.hpp:72
const Eigen::Isometry3d & T
Definition: reduction_tbb.hpp:75
const CorrespondenceRejector & rejector
Definition: reduction_tbb.hpp:74
const TargetTree & target_tree
Definition: reduction_tbb.hpp:73
Eigen::Matrix< double, 6, 1 > b
Definition: reduction_tbb.hpp:79
const TargetPointCloud & target
Definition: reduction_tbb.hpp:71
double e
Definition: reduction_tbb.hpp:80
Eigen::Matrix< double, 6, 6 > H
Definition: reduction_tbb.hpp:78
void operator()(const tbb::blocked_range< size_t > &r)
Definition: reduction_tbb.hpp:41
LinearizeSum(const TargetPointCloud &target, const SourcePointCloud &source, const TargetTree &target_tree, const CorrespondenceRejector &rejector, const Eigen::Isometry3d &T, std::vector< Factor > &factors)
Definition: reduction_tbb.hpp:13
Parallel reduction with TBB backend.
Definition: reduction_tbb.hpp:114
double error(const TargetPointCloud &target, const SourcePointCloud &source, const Eigen::Isometry3d &T, std::vector< Factor > &factors) const
Definition: reduction_tbb.hpp:134
std::tuple< Eigen::Matrix< double, 6, 6 >, Eigen::Matrix< double, 6, 1 >, double > linearize(const TargetPointCloud &target, const SourcePointCloud &source, const TargetTree &target_tree, const CorrespondenceRejector &rejector, const Eigen::Isometry3d &T, std::vector< Factor > &factors) const
Definition: reduction_tbb.hpp:118
ParallelReductionTBB()
Definition: reduction_tbb.hpp:115