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