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: 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 <cstdint> |
| 38 | #include <memory> |
| 39 | #include <string> |
| 40 | #include <vector> |
| 41 | |
| 42 | #include "ceres/cost_function.h" |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 43 | #include "ceres/internal/disable_warnings.h" |
| 44 | #include "ceres/internal/export.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 45 | #include "ceres/stringprintf.h" |
| 46 | #include "ceres/types.h" |
| 47 | |
| 48 | namespace ceres { |
| 49 | |
| 50 | class LossFunction; |
| 51 | |
| 52 | namespace internal { |
| 53 | |
| 54 | class ParameterBlock; |
| 55 | |
| 56 | // A term in the least squares problem. The mathematical form of each term in |
| 57 | // the overall least-squares cost function is: |
| 58 | // |
| 59 | // 1 |
| 60 | // --- loss_function( || cost_function(block1, block2, ...) ||^2 ), |
| 61 | // 2 |
| 62 | // |
| 63 | // Storing the cost function and the loss function separately permits optimizing |
| 64 | // the problem with standard non-linear least techniques, without requiring a |
| 65 | // more general non-linear solver. |
| 66 | // |
| 67 | // The residual block stores pointers to but does not own the cost functions, |
| 68 | // loss functions, and parameter blocks. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 69 | class CERES_NO_EXPORT ResidualBlock { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 70 | public: |
| 71 | // Construct the residual block with the given cost/loss functions. Loss may |
| 72 | // be null. The index is the index of the residual block in the Program's |
| 73 | // residual_blocks array. |
| 74 | ResidualBlock(const CostFunction* cost_function, |
| 75 | const LossFunction* loss_function, |
| 76 | const std::vector<ParameterBlock*>& parameter_blocks, |
| 77 | int index); |
| 78 | |
| 79 | // Evaluates the residual term, storing the scalar cost in *cost, the residual |
| 80 | // components in *residuals, and the jacobians between the parameters and |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 81 | // residuals in jacobians[i], in row-major order. If residuals is nullptr, the |
| 82 | // residuals are not computed. If jacobians is nullptr, no jacobians are |
| 83 | // computed. If jacobians[i] is nullptr, then the jacobian for that parameter |
| 84 | // is not computed. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 85 | // |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 86 | // cost must not be null. |
| 87 | // |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 88 | // Evaluate needs scratch space which must be supplied by the caller via |
| 89 | // scratch. The array should have at least NumScratchDoublesForEvaluate() |
| 90 | // space available. |
| 91 | // |
| 92 | // The return value indicates the success or failure. If the function returns |
| 93 | // false, the caller should expect the output memory locations to have |
| 94 | // been modified. |
| 95 | // |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 96 | // The returned cost and jacobians have had robustification and manifold |
| 97 | // projection applied already; for example, the jacobian for a 4-dimensional |
| 98 | // quaternion parameter using the "Quaternion" manifold is num_residuals by 3 |
| 99 | // instead of num_residuals by 4. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 100 | // |
| 101 | // apply_loss_function as the name implies allows the user to switch |
| 102 | // the application of the loss function on and off. |
| 103 | bool Evaluate(bool apply_loss_function, |
| 104 | double* cost, |
| 105 | double* residuals, |
| 106 | double** jacobians, |
| 107 | double* scratch) const; |
| 108 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 109 | const CostFunction* cost_function() const { return cost_function_; } |
| 110 | const LossFunction* loss_function() const { return loss_function_; } |
| 111 | |
| 112 | // Access the parameter blocks for this residual. The array has size |
| 113 | // NumParameterBlocks(). |
| 114 | ParameterBlock* const* parameter_blocks() const { |
| 115 | return parameter_blocks_.get(); |
| 116 | } |
| 117 | |
| 118 | // Number of variable blocks that this residual term depends on. |
| 119 | int NumParameterBlocks() const { |
| 120 | return cost_function_->parameter_block_sizes().size(); |
| 121 | } |
| 122 | |
| 123 | // The size of the residual vector returned by this residual function. |
| 124 | int NumResiduals() const { return cost_function_->num_residuals(); } |
| 125 | |
| 126 | // The minimum amount of scratch space needed to pass to Evaluate(). |
| 127 | int NumScratchDoublesForEvaluate() const; |
| 128 | |
| 129 | // This residual block's index in an array. |
| 130 | int index() const { return index_; } |
| 131 | void set_index(int index) { index_ = index; } |
| 132 | |
| 133 | std::string ToString() const { |
| 134 | return StringPrintf("{residual block; index=%d}", index_); |
| 135 | } |
| 136 | |
| 137 | private: |
| 138 | const CostFunction* cost_function_; |
| 139 | const LossFunction* loss_function_; |
| 140 | std::unique_ptr<ParameterBlock*[]> parameter_blocks_; |
| 141 | |
| 142 | // The index of the residual, typically in a Program. This is only to permit |
| 143 | // switching from a ResidualBlock* to an index in the Program's array, needed |
| 144 | // to do efficient removals. |
| 145 | int32_t index_; |
| 146 | }; |
| 147 | |
| 148 | } // namespace internal |
| 149 | } // namespace ceres |
| 150 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 151 | #include "ceres/internal/reenable_warnings.h" |
| 152 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 153 | #endif // CERES_INTERNAL_RESIDUAL_BLOCK_H_ |