1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
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.
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.
29 // Author: sameeragarwal@google.com (Sameer Agarwal)
30 // keir@google.com (Keir Mierle)
32 // Purpose : Class and struct definitions for parameter and residual blocks.
34 #ifndef CERES_INTERNAL_RESIDUAL_BLOCK_H_
35 #define CERES_INTERNAL_RESIDUAL_BLOCK_H_
40 #include "ceres/cost_function.h"
41 #include "ceres/internal/port.h"
42 #include "ceres/internal/scoped_ptr.h"
43 #include "ceres/stringprintf.h"
44 #include "ceres/types.h"
54 // A term in the least squares problem. The mathematical form of each term in
55 // the overall least-squares cost function is:
58 // --- loss_function( || cost_function(block1, block2, ...) ||^2 ),
61 // Storing the cost function and the loss function separately permits optimizing
62 // the problem with standard non-linear least techniques, without requiring a
63 // more general non-linear solver.
65 // The residual block stores pointers to but does not own the cost functions,
66 // loss functions, and parameter blocks.
69 // Construct the residual block with the given cost/loss functions. Loss may
70 // be null. The index is the index of the residual block in the Program's
71 // residual_blocks array.
72 ResidualBlock(const CostFunction* cost_function,
73 const LossFunction* loss_function,
74 const std::vector<ParameterBlock*>& parameter_blocks,
77 // Evaluates the residual term, storing the scalar cost in *cost, the residual
78 // components in *residuals, and the jacobians between the parameters and
79 // residuals in jacobians[i], in row-major order. If residuals is NULL, the
80 // residuals are not computed. If jacobians is NULL, no jacobians are
81 // computed. If jacobians[i] is NULL, then the jacobian for that parameter is
84 // Evaluate needs scratch space which must be supplied by the caller via
85 // scratch. The array should have at least NumScratchDoublesForEvaluate()
88 // The return value indicates the success or failure. If the function returns
89 // false, the caller should expect the the output memory locations to have
92 // The returned cost and jacobians have had robustification and local
93 // parameterizations applied already; for example, the jacobian for a
94 // 4-dimensional quaternion parameter using the "QuaternionParameterization"
95 // is num_residuals by 3 instead of num_residuals by 4.
97 // apply_loss_function as the name implies allows the user to switch
98 // the application of the loss function on and off.
99 bool Evaluate(bool apply_loss_function,
103 double* scratch) const;
106 const CostFunction* cost_function() const { return cost_function_; }
107 const LossFunction* loss_function() const { return loss_function_; }
109 // Access the parameter blocks for this residual. The array has size
110 // NumParameterBlocks().
111 ParameterBlock* const* parameter_blocks() const {
112 return parameter_blocks_.get();
115 // Number of variable blocks that this residual term depends on.
116 int NumParameterBlocks() const {
117 return cost_function_->parameter_block_sizes().size();
120 // The size of the residual vector returned by this residual function.
121 int NumResiduals() const { return cost_function_->num_residuals(); }
123 // The minimum amount of scratch space needed to pass to Evaluate().
124 int NumScratchDoublesForEvaluate() const;
126 // This residual block's index in an array.
127 int index() const { return index_; }
128 void set_index(int index) { index_ = index; }
130 std::string ToString() const {
131 return StringPrintf("{residual block; index=%d}", index_);
135 const CostFunction* cost_function_;
136 const LossFunction* loss_function_;
137 scoped_array<ParameterBlock*> parameter_blocks_;
139 // The index of the residual, typically in a Program. This is only to permit
140 // switching from a ResidualBlock* to an index in the Program's array, needed
141 // to do efficient removals.
145 } // namespace internal
148 #endif // CERES_INTERNAL_RESIDUAL_BLOCK_H_