Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
| 2 | // Copyright 2015 Google Inc. All rights reserved. |
| 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 | #include "ceres/problem.h" |
| 33 | |
| 34 | #include <vector> |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 35 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 36 | #include "ceres/crs_matrix.h" |
| 37 | #include "ceres/problem_impl.h" |
| 38 | |
| 39 | namespace ceres { |
| 40 | |
| 41 | using std::vector; |
| 42 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 43 | Problem::Problem() : impl_(new internal::ProblemImpl) {} |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 44 | Problem::Problem(const Problem::Options& options) |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 45 | : impl_(new internal::ProblemImpl(options)) {} |
| 46 | // Not inline defaulted in declaration due to use of std::unique_ptr. |
| 47 | Problem::Problem(Problem&&) = default; |
| 48 | Problem& Problem::operator=(Problem&&) = default; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 49 | Problem::~Problem() {} |
| 50 | |
| 51 | ResidualBlockId Problem::AddResidualBlock( |
| 52 | CostFunction* cost_function, |
| 53 | LossFunction* loss_function, |
| 54 | const vector<double*>& parameter_blocks) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 55 | return impl_->AddResidualBlock(cost_function, |
| 56 | loss_function, |
| 57 | parameter_blocks.data(), |
| 58 | static_cast<int>(parameter_blocks.size())); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 61 | ResidualBlockId Problem::AddResidualBlock(CostFunction* cost_function, |
| 62 | LossFunction* loss_function, |
| 63 | double* const* const parameter_blocks, |
| 64 | int num_parameter_blocks) { |
| 65 | return impl_->AddResidualBlock( |
| 66 | cost_function, loss_function, parameter_blocks, num_parameter_blocks); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void Problem::AddParameterBlock(double* values, int size) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 70 | impl_->AddParameterBlock(values, size); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void Problem::AddParameterBlock(double* values, |
| 74 | int size, |
| 75 | LocalParameterization* local_parameterization) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 76 | impl_->AddParameterBlock(values, size, local_parameterization); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void Problem::RemoveResidualBlock(ResidualBlockId residual_block) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 80 | impl_->RemoveResidualBlock(residual_block); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 83 | void Problem::RemoveParameterBlock(const double* values) { |
| 84 | impl_->RemoveParameterBlock(values); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 87 | void Problem::SetParameterBlockConstant(const double* values) { |
| 88 | impl_->SetParameterBlockConstant(values); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | void Problem::SetParameterBlockVariable(double* values) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 92 | impl_->SetParameterBlockVariable(values); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 95 | bool Problem::IsParameterBlockConstant(const double* values) const { |
| 96 | return impl_->IsParameterBlockConstant(values); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void Problem::SetParameterization( |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 100 | double* values, LocalParameterization* local_parameterization) { |
| 101 | impl_->SetParameterization(values, local_parameterization); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | const LocalParameterization* Problem::GetParameterization( |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 105 | const double* values) const { |
| 106 | return impl_->GetParameterization(values); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void Problem::SetParameterLowerBound(double* values, |
| 110 | int index, |
| 111 | double lower_bound) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 112 | impl_->SetParameterLowerBound(values, index, lower_bound); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void Problem::SetParameterUpperBound(double* values, |
| 116 | int index, |
| 117 | double upper_bound) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 118 | impl_->SetParameterUpperBound(values, index, upper_bound); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 121 | double Problem::GetParameterUpperBound(const double* values, int index) const { |
| 122 | return impl_->GetParameterUpperBound(values, index); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 125 | double Problem::GetParameterLowerBound(const double* values, int index) const { |
| 126 | return impl_->GetParameterLowerBound(values, index); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | bool Problem::Evaluate(const EvaluateOptions& evaluate_options, |
| 130 | double* cost, |
| 131 | vector<double>* residuals, |
| 132 | vector<double>* gradient, |
| 133 | CRSMatrix* jacobian) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 134 | return impl_->Evaluate(evaluate_options, cost, residuals, gradient, jacobian); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 137 | bool Problem::EvaluateResidualBlock(ResidualBlockId residual_block_id, |
| 138 | bool apply_loss_function, |
| 139 | double* cost, |
| 140 | double* residuals, |
| 141 | double** jacobians) const { |
| 142 | return impl_->EvaluateResidualBlock(residual_block_id, |
| 143 | apply_loss_function, |
| 144 | /* new_point = */ true, |
| 145 | cost, |
| 146 | residuals, |
| 147 | jacobians); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 150 | bool Problem::EvaluateResidualBlockAssumingParametersUnchanged( |
| 151 | ResidualBlockId residual_block_id, |
| 152 | bool apply_loss_function, |
| 153 | double* cost, |
| 154 | double* residuals, |
| 155 | double** jacobians) const { |
| 156 | return impl_->EvaluateResidualBlock(residual_block_id, |
| 157 | apply_loss_function, |
| 158 | /* new_point = */ false, |
| 159 | cost, |
| 160 | residuals, |
| 161 | jacobians); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 162 | } |
| 163 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 164 | int Problem::NumParameterBlocks() const { return impl_->NumParameterBlocks(); } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 165 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 166 | int Problem::NumParameters() const { return impl_->NumParameters(); } |
| 167 | |
| 168 | int Problem::NumResidualBlocks() const { return impl_->NumResidualBlocks(); } |
| 169 | |
| 170 | int Problem::NumResiduals() const { return impl_->NumResiduals(); } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 171 | |
| 172 | int Problem::ParameterBlockSize(const double* parameter_block) const { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 173 | return impl_->ParameterBlockSize(parameter_block); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | int Problem::ParameterBlockLocalSize(const double* parameter_block) const { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 177 | return impl_->ParameterBlockLocalSize(parameter_block); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | bool Problem::HasParameterBlock(const double* values) const { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 181 | return impl_->HasParameterBlock(values); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void Problem::GetParameterBlocks(vector<double*>* parameter_blocks) const { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 185 | impl_->GetParameterBlocks(parameter_blocks); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void Problem::GetResidualBlocks( |
| 189 | vector<ResidualBlockId>* residual_blocks) const { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 190 | impl_->GetResidualBlocks(residual_blocks); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void Problem::GetParameterBlocksForResidualBlock( |
| 194 | const ResidualBlockId residual_block, |
| 195 | vector<double*>* parameter_blocks) const { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 196 | impl_->GetParameterBlocksForResidualBlock(residual_block, parameter_blocks); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | const CostFunction* Problem::GetCostFunctionForResidualBlock( |
| 200 | const ResidualBlockId residual_block) const { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 201 | return impl_->GetCostFunctionForResidualBlock(residual_block); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | const LossFunction* Problem::GetLossFunctionForResidualBlock( |
| 205 | const ResidualBlockId residual_block) const { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 206 | return impl_->GetLossFunctionForResidualBlock(residual_block); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void Problem::GetResidualBlocksForParameterBlock( |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 210 | const double* values, vector<ResidualBlockId>* residual_blocks) const { |
| 211 | impl_->GetResidualBlocksForParameterBlock(values, residual_blocks); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | } // namespace ceres |