robust_kernel_impl.cpp
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #include "robust_kernel_impl.h"
28 #include "robust_kernel_factory.h"
29 
30 #include <cmath>
31 
32 namespace g2o {
33 
35  RobustKernel(delta),
36  _kernel(kernel)
37 {
38 }
39 
41  RobustKernel(delta)
42 {
43 }
44 
46 {
47  _kernel = ptr;
48 }
49 
50 void RobustKernelScaleDelta::robustify(double error, Eigen::Vector3d& rho) const
51 {
52  if (_kernel.get()) {
53  double dsqr = _delta * _delta;
54  double dsqrReci = 1. / dsqr;
55  _kernel->robustify(dsqrReci * error, rho);
56  rho[0] *= dsqr;
57  rho[2] *= dsqrReci;
58  } else { // no robustification
59  rho[0] = error;
60  rho[1] = 1.;
61  rho[2] = 0.;
62  }
63 }
64 
66 {
67  dsqr = delta*delta;
68  _delta = delta;
69 }
70 
71 
72 void RobustKernelHuber::setDeltaSqr(const double &delta, const double &deltaSqr)
73 {
74  dsqr = deltaSqr;
75  _delta = delta;
76 }
77 
78 void RobustKernelHuber::robustify(double e, Eigen::Vector3d& rho) const
79 {
80  //dsqr = _delta * _delta;
81  if (e <= dsqr) { // inlier
82  rho[0] = e;
83  rho[1] = 1.;
84  rho[2] = 0.;
85  } else { // outlier
86  double sqrte = sqrt(e); // absolut value of the error
87  rho[0] = 2*sqrte*_delta - dsqr; // rho(e) = 2 * delta * e^(1/2) - delta^2
88  rho[1] = _delta / sqrte; // rho'(e) = delta / sqrt(e)
89  rho[2] = - 0.5 * rho[1] / e; // rho''(e) = -1 / (2*e^(3/2)) = -1/2 * (delta/e) / e
90  }
91 }
92 
93 void RobustKernelTukey::setDeltaSqr(const double &deltaSqr, const double &inv)
94 {
95  _deltaSqr = deltaSqr;
96  _invDeltaSqr = inv;
97 
98 }
99 
100 void RobustKernelTukey::robustify(double e, Eigen::Vector3d& rho) const
101 {
102  if (e <= _deltaSqr) { // inlier
103  double factor = e*_invDeltaSqr;
104  double d = 1-factor;
105  double dd = d*d;
106  rho[0] = _deltaSqr*(1-dd*d);
107  rho[1] = 3*dd;
108  rho[2] = -6*_invDeltaSqr*d;
109  } else { // outlier
110  rho[0] = _deltaSqr; // rho(e) = delta^2
111  rho[1] = 0.;
112  rho[2] = 0.;
113  }
114 }
115 
116 void RobustKernelPseudoHuber::robustify(double e2, Eigen::Vector3d& rho) const
117 {
118  double dsqr = _delta * _delta;
119  double dsqrReci = 1. / dsqr;
120  double aux1 = dsqrReci * e2 + 1.0;
121  double aux2 = sqrt(aux1);
122  rho[0] = 2 * dsqr * (aux2 - 1);
123  rho[1] = 1. / aux2;
124  rho[2] = -0.5 * dsqrReci * rho[1] / aux1;
125 }
126 
127 void RobustKernelCauchy::robustify(double e2, Eigen::Vector3d& rho) const
128 {
129  double dsqr = _delta * _delta;
130  double dsqrReci = 1. / dsqr;
131  double aux = dsqrReci * e2 + 1.0;
132  rho[0] = dsqr * log(aux);
133  rho[1] = 1. / aux;
134  rho[2] = -dsqrReci * std::pow(rho[1], 2);
135 }
136 
137 void RobustKernelSaturated::robustify(double e2, Eigen::Vector3d& rho) const
138 {
139  double dsqr = _delta * _delta;
140  if (e2 <= dsqr) { // inlier
141  rho[0] = e2;
142  rho[1] = 1.;
143  rho[2] = 0.;
144  } else { // outlier
145  rho[0] = dsqr;
146  rho[1] = 0.;
147  rho[2] = 0.;
148  }
149 }
150 
151 //delta is used as $phi$
152 void RobustKernelDCS::robustify(double e2, Eigen::Vector3d& rho) const
153 {
154  const double& phi = _delta;
155  double scale = (2.0*phi)/(phi+e2);
156  if(scale>=1.0)
157  scale = 1.0;
158 
159  rho[0] = scale*e2*scale;
160  rho[1] = (scale*scale);
161  rho[2] = 0;
162 }
163 
164 
165 // register the kernel to their factory
172 
173 } // end namespace g2o
d
virtual void setDeltaSqr(const double &deltaSqr, const double &inv)
Dynamic covariance scaling - DCS.
RobustKernelScaleDelta(const RobustKernelPtr &kernel, double delta=1.)
Huber Cost Function.
#define G2O_REGISTER_ROBUST_KERNEL(name, classname)
Pseudo Huber Cost Function.
virtual void robustify(double e2, Eigen::Vector3d &rho) const
void robustify(double error, Eigen::Vector3d &rho) const
virtual void setDelta(double delta)
base for all robust cost functions
Definition: robust_kernel.h:51
Saturated cost function.
Tukey Cost Function.
double delta() const
Definition: robust_kernel.h:72
virtual void robustify(double e2, Eigen::Vector3d &rho) const
virtual void robustify(double e2, Eigen::Vector3d &rho) const
void setKernel(const RobustKernelPtr &ptr)
use another kernel for the underlying operation
std::tr1::shared_ptr< RobustKernel > RobustKernelPtr
Definition: robust_kernel.h:77
virtual void robustify(double e2, Eigen::Vector3d &rho) const
Cauchy cost function.
virtual void setDeltaSqr(const double &delta, const double &deltaSqr)
virtual void robustify(double e2, Eigen::Vector3d &rho) const
virtual void robustify(double e2, Eigen::Vector3d &rho) const


orb_slam2_ros
Author(s):
autogenerated on Wed Apr 21 2021 02:53:05