evaluation_callback.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2019 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: mierle@gmail.com (Keir Mierle)
  30. #ifndef CERES_PUBLIC_EVALUATION_CALLBACK_H_
  31. #define CERES_PUBLIC_EVALUATION_CALLBACK_H_
  32. #include "ceres/internal/export.h"
  33. namespace ceres {
  34. // Using this callback interface, Ceres can notify you when it is
  35. // about to evaluate the residuals or jacobians. With the callback,
  36. // you can share computation between residual blocks by doing the
  37. // shared computation in PrepareForEvaluation() before Ceres calls
  38. // CostFunction::Evaluate(). It also enables caching results between a
  39. // pure residual evaluation and a residual & jacobian evaluation, via
  40. // the new_evaluation_point argument.
  41. //
  42. // One use case for this callback is if the cost function compute is
  43. // moved to the GPU. In that case, the prepare call does the actual
  44. // cost function evaluation, and subsequent calls from Ceres to the
  45. // actual cost functions merely copy the results from the GPU onto the
  46. // corresponding blocks for Ceres to plug into the solver.
  47. //
  48. // NOTE: Ceres provides no mechanism to share data other than the
  49. // notification from the callback. Users must provide access to
  50. // pre-computed shared data to their cost functions behind the scenes;
  51. // this all happens without Ceres knowing.
  52. //
  53. // One approach is to put a pointer to the shared data in each cost
  54. // function (recommended) or to use a global shared variable
  55. // (discouraged; bug-prone). As far as Ceres is concerned, it is
  56. // evaluating cost functions like any other; it just so happens that
  57. // behind the scenes the cost functions reuse pre-computed data to
  58. // execute faster.
  59. class CERES_EXPORT EvaluationCallback {
  60. public:
  61. virtual ~EvaluationCallback();
  62. // Called before Ceres requests residuals or jacobians for a given setting of
  63. // the parameters. User parameters (the double* values provided to the cost
  64. // functions) are fixed until the next call to PrepareForEvaluation(). If
  65. // new_evaluation_point == true, then this is a new point that is different
  66. // from the last evaluated point. Otherwise, it is the same point that was
  67. // evaluated previously (either jacobian or residual) and the user can use
  68. // cached results from previous evaluations.
  69. virtual void PrepareForEvaluation(bool evaluate_jacobians,
  70. bool new_evaluation_point) = 0;
  71. };
  72. } // namespace ceres
  73. #endif // CERES_PUBLIC_EVALUATION_CALLBACK_H_