1 // Ceres Solver - A fast non-linear least squares minimizer 2 // Copyright 2010, 2011, 2012 Google Inc. All rights reserved. 3 // http://code.google.com/p/ceres-solver/ 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 // keir@google.com (Keir Mierle) 31 // 32 // Purpose : Class and struct definitions for parameter and residual blocks. 33 34 #ifndef CERES_INTERNAL_RESIDUAL_BLOCK_H_ 35 #define CERES_INTERNAL_RESIDUAL_BLOCK_H_ 36 37 #include <vector> 38 39 #include "ceres/cost_function.h" 40 #include "ceres/internal/port.h" 41 #include "ceres/internal/scoped_ptr.h" 42 #include "ceres/types.h" 43 44 namespace ceres { 45 46 class LossFunction; 47 48 namespace internal { 49 50 class ParameterBlock; 51 52 // A term in the least squares problem. The mathematical form of each term in 53 // the overall least-squares cost function is: 54 // 55 // 1 56 // --- loss_function( || cost_function(block1, block2, ...) ||^2 ), 57 // 2 58 // 59 // Storing the cost function and the loss function separately permits optimizing 60 // the problem with standard non-linear least techniques, without requiring a 61 // more general non-linear solver. 62 // 63 // The residual block stores pointers to but does not own the cost functions, 64 // loss functions, and parameter blocks. 65 class ResidualBlock { 66 public: 67 ResidualBlock(const CostFunction* cost_function, 68 const LossFunction* loss_function, 69 const vector<ParameterBlock*>& parameter_blocks); 70 71 // Evaluates the residual term, storing the scalar cost in *cost, the residual 72 // components in *residuals, and the jacobians between the parameters and 73 // residuals in jacobians[i], in row-major order. If residuals is NULL, the 74 // residuals are not computed. If jacobians is NULL, no jacobians are 75 // computed. If jacobians[i] is NULL, then the jacobian for that parameter is 76 // not computed. 77 // 78 // Evaluate needs scratch space which must be supplied by the caller via 79 // scratch. The array should have at least NumScratchDoublesForEvaluate() 80 // space available. 81 // 82 // The return value indicates the success or failure. If the function returns 83 // false, the caller should expect the the output memory locations to have 84 // been modified. 85 // 86 // The returned cost and jacobians have had robustification and local 87 // parameterizations applied already; for example, the jacobian for a 88 // 4-dimensional quaternion parameter using the "QuaternionParameterization" 89 // is num_residuals by 3 instead of num_residuals by 4. 90 bool Evaluate(double* cost, 91 double* residuals, 92 double** jacobians, 93 double* scratch) const; 94 cost_function()95 const CostFunction* cost_function() const { return cost_function_; } loss_function()96 const LossFunction* loss_function() const { return loss_function_; } 97 98 // Access the parameter blocks for this residual. The array has size 99 // NumParameterBlocks(). parameter_blocks()100 ParameterBlock* const* parameter_blocks() const { 101 return parameter_blocks_.get(); 102 } 103 104 // Number of variable blocks that this residual term depends on. NumParameterBlocks()105 int NumParameterBlocks() const { 106 return cost_function_->parameter_block_sizes().size(); 107 } 108 109 // The size of the residual vector returned by this residual function. NumResiduals()110 int NumResiduals() const { return cost_function_->num_residuals(); } 111 112 // The minimum amount of scratch space needed to pass to Evaluate(). 113 int NumScratchDoublesForEvaluate() const; 114 115 private: 116 const CostFunction* cost_function_; 117 const LossFunction* loss_function_; 118 scoped_array<ParameterBlock*> parameter_blocks_; 119 }; 120 121 } // namespace internal 122 } // namespace ceres 123 124 #endif // CERES_INTERNAL_RESIDUAL_BLOCK_H_ 125