1// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9//   this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11//   this list of conditions and the following disclaimer in the documentation
12//   and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14//   used to endorse or promote products derived from this software without
15//   specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: sameeragarwal@google.com (Sameer Agarwal)
30//
31// Class definition for the object that is responsible for applying a
32// second order correction to the Gauss-Newton based on the ideas in
33// BANS by Triggs et al.
34
35#ifndef CERES_INTERNAL_CORRECTOR_H_
36#define CERES_INTERNAL_CORRECTOR_H_
37
38namespace ceres {
39namespace internal {
40
41// Corrector is responsible for applying the second order correction
42// to the residual and jacobian of a least squares problem based on a
43// radial robust loss.
44//
45// The key idea here is to look at the expressions for the robustified
46// gauss newton approximation and then take its squareroot to get the
47// corresponding corrections to the residual and jacobian.  For the
48// full expressions see Eq. 10 and 11 in BANS by Triggs et al.
49class Corrector {
50 public:
51  // The constructor takes the squared norm, the value, the first and
52  // second derivatives of the LossFunction. It precalculates some of
53  // the constants that are needed to apply the correction. The
54  // correction constant alpha is constrained to be smaller than 1, if
55  // it becomes larger than 1, then it will reverse the sign of the
56  // residual and the correction. If alpha is equal to 1 will result
57  // in a divide by zero error. Thus we constrain alpha to be upper
58  // bounded by 1 - epsilon_.
59  //
60  // rho[1] needs to be positive. The constructor will crash if this
61  // condition is not met.
62  //
63  // In practical use CorrectJacobian should always be called before
64  // CorrectResidual, because the jacobian correction depends on the
65  // value of the uncorrected residual values.
66  explicit Corrector(double sq_norm, const double rho[3]);
67
68  // residuals *= sqrt(rho[1]) / (1 - alpha)
69  void CorrectResiduals(int num_rows, double* residuals);
70
71  // jacobian = sqrt(rho[1]) * jacobian -
72  // sqrt(rho[1]) * alpha / sq_norm * residuals residuals' * jacobian.
73  //
74  // The method assumes that the jacobian has row-major storage. It is
75  // the caller's responsibility to ensure that the pointer to
76  // jacobian is not null.
77  void CorrectJacobian(int num_rows,
78                       int num_cols,
79                       double* residuals,
80                       double* jacobian);
81
82 private:
83  double sqrt_rho1_;
84  double residual_scaling_;
85  double alpha_sq_norm_;
86};
87}  // namespace internal
88}  // namespace ceres
89
90#endif  // CERES_INTERNAL_CORRECTOR_H_
91