blob: e582dc840a47a90f4f075425f7f9553e9d203e7a [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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 Schuh3de38b02024-06-25 18:25:10 -070034#include "ceres/internal/export.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080035
36namespace ceres {
37
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080038// 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 Schuh70cc9552019-01-21 19:46:48 -080045//
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080046// 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 Schuh70cc9552019-01-21 19:46:48 -080051//
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080052// 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 Schuh70cc9552019-01-21 19:46:48 -080063class CERES_EXPORT EvaluationCallback {
64 public:
Austin Schuh3de38b02024-06-25 18:25:10 -070065 virtual ~EvaluationCallback();
Austin Schuh70cc9552019-01-21 19:46:48 -080066
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 Schuh3de38b02024-06-25 18:25:10 -070069 // 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 Schuh70cc9552019-01-21 19:46:48 -080075 // 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_