corrector.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  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. // BAMS by Triggs et al.
  34. #ifndef CERES_INTERNAL_CORRECTOR_H_
  35. #define CERES_INTERNAL_CORRECTOR_H_
  36. #include "ceres/internal/disable_warnings.h"
  37. #include "ceres/internal/export.h"
  38. namespace ceres::internal {
  39. // Corrector is responsible for applying the second order correction
  40. // to the residual and jacobian of a least squares problem based on a
  41. // radial robust loss.
  42. //
  43. // The key idea here is to look at the expressions for the robustified
  44. // gauss newton approximation and then take its square root to get the
  45. // corresponding corrections to the residual and jacobian. For the
  46. // full expressions see Eq. 10 and 11 in BAMS by Triggs et al.
  47. class CERES_NO_EXPORT Corrector {
  48. public:
  49. // The constructor takes the squared norm, the value, the first and
  50. // second derivatives of the LossFunction. It precalculates some of
  51. // the constants that are needed to apply the correction. The
  52. // correction constant alpha is constrained to be smaller than 1, if
  53. // it becomes larger than 1, then it will reverse the sign of the
  54. // residual and the correction. If alpha is equal to 1 will result
  55. // in a divide by zero error. Thus we constrain alpha to be upper
  56. // bounded by 1 - epsilon_.
  57. //
  58. // rho[1] needs to be positive. The constructor will crash if this
  59. // condition is not met.
  60. //
  61. // In practical use CorrectJacobian should always be called before
  62. // CorrectResidual, because the jacobian correction depends on the
  63. // value of the uncorrected residual values.
  64. explicit Corrector(double sq_norm, const double rho[3]);
  65. // residuals *= sqrt(rho[1]) / (1 - alpha)
  66. void CorrectResiduals(int num_rows, double* residuals);
  67. // jacobian = sqrt(rho[1]) * jacobian -
  68. // sqrt(rho[1]) * alpha / sq_norm * residuals residuals' * jacobian.
  69. //
  70. // The method assumes that the jacobian has row-major storage. It is
  71. // the caller's responsibility to ensure that the pointer to
  72. // jacobian is not null.
  73. void CorrectJacobian(int num_rows,
  74. int num_cols,
  75. double* residuals,
  76. double* jacobian);
  77. private:
  78. double sqrt_rho1_;
  79. double residual_scaling_;
  80. double alpha_sq_norm_;
  81. };
  82. } // namespace ceres::internal
  83. #include "ceres/internal/reenable_warnings.h"
  84. #endif // CERES_INTERNAL_CORRECTOR_H_