Main MRPT website > C++ reference for MRPT 1.9.9
levmarq_impl.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #ifndef GRAPH_SLAM_LEVMARQ_IMPL_H
10 #define GRAPH_SLAM_LEVMARQ_IMPL_H
11 
12 namespace mrpt
13 {
14 namespace graphslam
15 {
16 /// Internal auxiliary classes
17 namespace detail
18 {
19 using namespace mrpt;
20 using namespace mrpt::poses;
21 using namespace mrpt::graphslam;
22 using namespace mrpt::math;
23 using namespace std;
24 
25 // An auxiliary struct to compute the pseudo-ln of a pose error, possibly
26 // modified with an information matrix.
27 // Specializations are below.
28 template <class EDGE, class gst>
29 struct AuxErrorEval;
30 
31 // For graphs of 2D constraints (no information matrix)
32 template <class gst>
33 struct AuxErrorEval<CPose2D, gst>
34 {
35  template <class POSE, class VEC, class EDGE_ITERATOR>
36  static inline void computePseudoLnError(
37  const POSE& P1DP2inv, VEC& err, const EDGE_ITERATOR& edge)
38  {
39  MRPT_UNUSED_PARAM(edge);
40  gst::SE_TYPE::pseudo_ln(P1DP2inv, err);
41  }
42 
43  template <class MAT, class EDGE_ITERATOR>
44  static inline void multiplyJtLambdaJ(
45  const MAT& J1, MAT& JtJ, const EDGE_ITERATOR& edge)
46  {
47  MRPT_UNUSED_PARAM(edge);
48  JtJ.multiply_AtA(J1);
49  }
50 
51  template <class MAT, class EDGE_ITERATOR>
52  static inline void multiplyJ1tLambdaJ2(
53  const MAT& J1, const MAT& J2, MAT& JtJ, const EDGE_ITERATOR& edge)
54  {
55  MRPT_UNUSED_PARAM(edge);
56  JtJ.multiply_AtB(J1, J2);
57  }
58 
59  template <class JAC, class EDGE_ITERATOR, class VEC1, class VEC2>
60  static inline void multiply_Jt_W_err(
61  const JAC& J, const EDGE_ITERATOR& edge, const VEC1& ERR, VEC2& OUT)
62  {
63  MRPT_UNUSED_PARAM(edge);
64  J.multiply_Atb(ERR, OUT, true /* accumulate in output */);
65  }
66 };
67 
68 // For graphs of 3D constraints (no information matrix)
69 template <class gst>
70 struct AuxErrorEval<CPose3D, gst>
71 {
72  template <class POSE, class VEC, class EDGE_ITERATOR>
73  static inline void computePseudoLnError(
74  const POSE& P1DP2inv, VEC& err, const EDGE_ITERATOR& edge)
75  {
76  MRPT_UNUSED_PARAM(edge);
77  gst::SE_TYPE::pseudo_ln(P1DP2inv, err);
78  }
79 
80  template <class MAT, class EDGE_ITERATOR>
81  static inline void multiplyJtLambdaJ(
82  const MAT& J1, MAT& JtJ, const EDGE_ITERATOR& edge)
83  {
84  MRPT_UNUSED_PARAM(edge);
85  JtJ.multiply_AtA(J1);
86  }
87 
88  template <class MAT, class EDGE_ITERATOR>
89  static inline void multiplyJ1tLambdaJ2(
90  const MAT& J1, const MAT& J2, MAT& JtJ, const EDGE_ITERATOR& edge)
91  {
92  MRPT_UNUSED_PARAM(edge);
93  JtJ.multiply_AtB(J1, J2);
94  }
95 
96  template <class JAC, class EDGE_ITERATOR, class VEC1, class VEC2>
97  static inline void multiply_Jt_W_err(
98  const JAC& J, const EDGE_ITERATOR& edge, const VEC1& ERR, VEC2& OUT)
99  {
100  MRPT_UNUSED_PARAM(edge);
101  J.multiply_Atb(ERR, OUT, true /* accumulate in output */);
102  }
103 };
104 
105 // For graphs of 2D constraints (with information matrix)
106 template <class gst>
108 {
109  template <class POSE, class VEC, class EDGE_ITERATOR>
110  static inline void computePseudoLnError(
111  const POSE& P1DP2inv, VEC& err, const EDGE_ITERATOR& edge)
112  {
113  gst::SE_TYPE::pseudo_ln(P1DP2inv, err);
114  }
115 
116  template <class MAT, class EDGE_ITERATOR>
117  static inline void multiplyJtLambdaJ(
118  const MAT& J1, MAT& JtJ, const EDGE_ITERATOR& edge)
119  {
120  JtJ.multiply_AtBC(J1, edge->second.cov_inv, J1);
121  }
122  template <class MAT, class EDGE_ITERATOR>
123  static inline void multiplyJ1tLambdaJ2(
124  const MAT& J1, const MAT& J2, MAT& JtJ, const EDGE_ITERATOR& edge)
125  {
126  JtJ.multiply_AtBC(J1, edge->second.cov_inv, J2);
127  }
128 
129  template <class JAC, class EDGE_ITERATOR, class VEC1, class VEC2>
130  static inline void multiply_Jt_W_err(
131  const JAC& J, const EDGE_ITERATOR& edge, const VEC1& ERR, VEC2& OUT)
132  {
134  JtInf.multiply_AtB(J, edge->second.cov_inv);
135  JtInf.multiply_Ab(ERR, OUT, true /* accumulate in output */);
136  }
137 };
138 
139 // For graphs of 3D constraints (with information matrix)
140 template <class gst>
142 {
143  template <class POSE, class VEC, class EDGE_ITERATOR>
144  static inline void computePseudoLnError(
145  const POSE& P1DP2inv, VEC& err, const EDGE_ITERATOR& edge)
146  {
147  MRPT_UNUSED_PARAM(edge);
148  gst::SE_TYPE::pseudo_ln(P1DP2inv, err);
149  }
150 
151  template <class MAT, class EDGE_ITERATOR>
152  static inline void multiplyJtLambdaJ(
153  const MAT& J1, MAT& JtJ, const EDGE_ITERATOR& edge)
154  {
155  JtJ.multiply_AtBC(J1, edge->second.cov_inv, J1);
156  }
157 
158  template <class MAT, class EDGE_ITERATOR>
159  static inline void multiplyJ1tLambdaJ2(
160  const MAT& J1, const MAT& J2, MAT& JtJ, const EDGE_ITERATOR& edge)
161  {
162  JtJ.multiply_AtBC(J1, edge->second.cov_inv, J2);
163  }
164 
165  template <class JAC, class EDGE_ITERATOR, class VEC1, class VEC2>
166  static inline void multiply_Jt_W_err(
167  const JAC& J, const EDGE_ITERATOR& edge, const VEC1& ERR, VEC2& OUT)
168  {
170  JtInf.multiply_AtB(J, edge->second.cov_inv);
171  JtInf.multiply_Ab(ERR, OUT, true /* accumulate in output */);
172  }
173 };
174 
175 } // end NS detail
176 
177 // Compute, at once, jacobians and the error vectors for each constraint in
178 // "lstObservationData", returns the overall squared error.
179 template <class GRAPH_T>
181  const GRAPH_T& graph,
182  const std::vector<typename graphslam_traits<GRAPH_T>::observation_info_t>&
183  lstObservationData,
186 {
187  MRPT_UNUSED_PARAM(graph);
188  using gst = graphslam_traits<GRAPH_T>;
189 
190  lstJacobians.clear();
191  errs.clear();
192 
193  const size_t nObservations = lstObservationData.size();
194 
195  for (size_t i = 0; i < nObservations; i++)
196  {
197  const typename gst::observation_info_t& obs = lstObservationData[i];
198  typename gst::edge_const_iterator it = obs.edge;
199  const typename gst::graph_t::constraint_t::type_value* EDGE_POSE =
200  obs.edge_mean;
201  typename gst::graph_t::constraint_t::type_value* P1 = obs.P1;
202  typename gst::graph_t::constraint_t::type_value* P2 = obs.P2;
203 
204  const auto& ids = it->first;
205  const auto& edge = it->second;
206 
207  // Compute the residual pose error of these pair of nodes + its
208  // constraint,
209  // that is: P1DP2inv = P1 * EDGE * inv(P2)
210  typename gst::graph_t::constraint_t::type_value P1DP2inv(
212  {
213  typename gst::graph_t::constraint_t::type_value P1D(
215  P1D.composeFrom(*P1, *EDGE_POSE);
216  const typename gst::graph_t::constraint_t::type_value P2inv =
217  -(*P2); // Pose inverse (NOT just switching signs!)
218  P1DP2inv.composeFrom(P1D, P2inv);
219  }
220 
221  // Add to vector of errors:
222  errs.resize(errs.size() + 1);
224  P1DP2inv, errs.back(), edge);
225 
226  // Compute the jacobians:
227  alignas(MRPT_MAX_ALIGN_BYTES)
228  std::pair<mrpt::graphs::TPairNodeIDs, typename gst::TPairJacobs>
229  newMapEntry;
230  newMapEntry.first = ids;
231  gst::SE_TYPE::jacobian_dP1DP2inv_depsilon(
232  P1DP2inv, &newMapEntry.second.first, &newMapEntry.second.second);
233 
234  // And insert into map of jacobians:
235  lstJacobians.insert(lstJacobians.end(), newMapEntry);
236  }
237 
238  // return overall square error: (Was:
239  // std::accumulate(...,mrpt::squareNorm_accum<>), but led to GCC
240  // errors when enabling parallelization)
241  double ret_err = 0.0;
242  for (size_t i = 0; i < errs.size(); i++) ret_err += errs[i].squaredNorm();
243  return ret_err;
244 }
245 
246 } // end of NS
247 } // end of NS
248 
249 #endif
mrpt::graphslam::detail::AuxErrorEval< CPose3D, gst >::multiply_Jt_W_err
static void multiply_Jt_W_err(const JAC &J, const EDGE_ITERATOR &edge, const VEC1 &ERR, VEC2 &OUT)
Definition: levmarq_impl.h:97
mrpt::graphslam::graphslam_traits::Array_O
typename SE_TYPE::array_t Array_O
Definition: graphslam/include/mrpt/graphslam/types.h:42
mrpt::graphslam::detail::AuxErrorEval< CPose2D, gst >::multiplyJtLambdaJ
static void multiplyJtLambdaJ(const MAT &J1, MAT &JtJ, const EDGE_ITERATOR &edge)
Definition: levmarq_impl.h:44
ids
GLuint * ids
Definition: glext.h:3906
mrpt::graphslam::graphslam_traits::map_pairIDs_pairJacobs_t
mrpt::aligned_std_multimap< mrpt::graphs::TPairNodeIDs, TPairJacobs > map_pairIDs_pairJacobs_t
Definition: graphslam/include/mrpt/graphslam/types.h:47
mrpt::graphslam::detail::AuxErrorEval< CPosePDFGaussianInf, gst >::multiplyJ1tLambdaJ2
static void multiplyJ1tLambdaJ2(const MAT &J1, const MAT &J2, MAT &JtJ, const EDGE_ITERATOR &edge)
Definition: levmarq_impl.h:123
MRPT_UNUSED_PARAM
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
mrpt::graphslam::graphslam_traits::observation_info_t
Auxiliary struct used in graph-slam implementation: It holds the relevant information for each of the...
Definition: graphslam/include/mrpt/graphslam/types.h:52
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::graphslam::detail::AuxErrorEval< CPose3D, gst >::computePseudoLnError
static void computePseudoLnError(const POSE &P1DP2inv, VEC &err, const EDGE_ITERATOR &edge)
Definition: levmarq_impl.h:73
mrpt::graphslam::detail::AuxErrorEval< CPose2D, gst >::multiplyJ1tLambdaJ2
static void multiplyJ1tLambdaJ2(const MAT &J1, const MAT &J2, MAT &JtJ, const EDGE_ITERATOR &edge)
Definition: levmarq_impl.h:52
mrpt::poses
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CHierarchicalMapMHPartition.h:25
mrpt::graphslam
SLAM methods related to graphs of pose constraints.
Definition: TUserOptionsChecker.h:33
mrpt::aligned_std_vector
std::vector< T, mrpt::aligned_allocator_cpp11< T > > aligned_std_vector
Definition: aligned_std_vector.h:15
mrpt::graphslam::detail::AuxErrorEval
Definition: levmarq_impl.h:29
mrpt::graphslam::detail::AuxErrorEval< CPosePDFGaussianInf, gst >::multiplyJtLambdaJ
static void multiplyJtLambdaJ(const MAT &J1, MAT &JtJ, const EDGE_ITERATOR &edge)
Definition: levmarq_impl.h:117
mrpt::poses::CPose2D
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle.
Definition: CPose2D.h:40
mrpt::poses::CPose3D
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
mrpt::graphslam::detail::AuxErrorEval< CPose3DPDFGaussianInf, gst >::computePseudoLnError
static void computePseudoLnError(const POSE &P1DP2inv, VEC &err, const EDGE_ITERATOR &edge)
Definition: levmarq_impl.h:144
mrpt::poses::CPose3DPDFGaussianInf
Declares a class that represents a Probability Density function (PDF) of a 3D pose as a Gaussian des...
Definition: CPose3DPDFGaussianInf.h:42
mrpt::poses::CPosePDFGaussianInf
A Probability Density function (PDF) of a 2D pose as a Gaussian with a mean and the inverse of the c...
Definition: CPosePDFGaussianInf.h:36
mrpt::graphslam::detail::AuxErrorEval< CPose3D, gst >::multiplyJ1tLambdaJ2
static void multiplyJ1tLambdaJ2(const MAT &J1, const MAT &J2, MAT &JtJ, const EDGE_ITERATOR &edge)
Definition: levmarq_impl.h:89
mrpt::graphslam::detail::AuxErrorEval< CPose2D, gst >::multiply_Jt_W_err
static void multiply_Jt_W_err(const JAC &J, const EDGE_ITERATOR &edge, const VEC1 &ERR, VEC2 &OUT)
Definition: levmarq_impl.h:60
mrpt::graphslam::detail::AuxErrorEval< CPose3DPDFGaussianInf, gst >::multiplyJ1tLambdaJ2
static void multiplyJ1tLambdaJ2(const MAT &J1, const MAT &J2, MAT &JtJ, const EDGE_ITERATOR &edge)
Definition: levmarq_impl.h:159
mrpt::math::CMatrixFixedNumeric< double, 3, 3 >
mrpt::math::UNINITIALIZED_MATRIX
@ UNINITIALIZED_MATRIX
Definition: math_frwds.h:75
mrpt::graphslam::detail::AuxErrorEval< CPose2D, gst >::computePseudoLnError
static void computePseudoLnError(const POSE &P1DP2inv, VEC &err, const EDGE_ITERATOR &edge)
Definition: levmarq_impl.h:36
mrpt::graphslam::graphslam_traits
Auxiliary traits template for use among graph-slam problems to make life easier with these complicate...
Definition: graphslam/include/mrpt/graphslam/types.h:32
mrpt::graphslam::detail::AuxErrorEval< CPose3D, gst >::multiplyJtLambdaJ
static void multiplyJtLambdaJ(const MAT &J1, MAT &JtJ, const EDGE_ITERATOR &edge)
Definition: levmarq_impl.h:81
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:13
mrpt::graphslam::detail::AuxErrorEval< CPosePDFGaussianInf, gst >::computePseudoLnError
static void computePseudoLnError(const POSE &P1DP2inv, VEC &err, const EDGE_ITERATOR &edge)
Definition: levmarq_impl.h:110
mrpt::graphslam::computeJacobiansAndErrors
double computeJacobiansAndErrors(const GRAPH_T &graph, const std::vector< typename graphslam_traits< GRAPH_T >::observation_info_t > &lstObservationData, typename graphslam_traits< GRAPH_T >::map_pairIDs_pairJacobs_t &lstJacobians, mrpt::aligned_std_vector< typename graphslam_traits< GRAPH_T >::Array_O > &errs)
Definition: levmarq_impl.h:180
mrpt::poses::UNINITIALIZED_POSE
@ UNINITIALIZED_POSE
Definition: CPoseOrPoint.h:36
mrpt::graphslam::detail::AuxErrorEval< CPosePDFGaussianInf, gst >::multiply_Jt_W_err
static void multiply_Jt_W_err(const JAC &J, const EDGE_ITERATOR &edge, const VEC1 &ERR, VEC2 &OUT)
Definition: levmarq_impl.h:130
mrpt::graphslam::detail::AuxErrorEval< CPose3DPDFGaussianInf, gst >::multiply_Jt_W_err
static void multiply_Jt_W_err(const JAC &J, const EDGE_ITERATOR &edge, const VEC1 &ERR, VEC2 &OUT)
Definition: levmarq_impl.h:166
MRPT_MAX_ALIGN_BYTES
#define MRPT_MAX_ALIGN_BYTES
Definition: aligned_allocator.h:21
mrpt::graphslam::detail::AuxErrorEval< CPose3DPDFGaussianInf, gst >::multiplyJtLambdaJ
static void multiplyJtLambdaJ(const MAT &J1, MAT &JtJ, const EDGE_ITERATOR &edge)
Definition: levmarq_impl.h:152



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST