blob: 4abea8b33ee07061e6b22c86342483ee5266c178 [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
Keir Mierle7492b0d2015-03-17 22:30:16 -07002// Copyright 2015 Google Inc. All rights reserved.
3// http://ceres-solver.org/
Keir Mierle8ebb0732012-04-30 23:09:08 -07004//
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)
Keir Mierlefda69b52013-10-10 00:25:24 -070030// mierle@gmail.com (Keir Mierle)
Keir Mierle8ebb0732012-04-30 23:09:08 -070031
32#include "ceres/problem_impl.h"
33
34#include <algorithm>
35#include <cstddef>
Sergey Sharybin16636ef2013-03-21 15:12:01 +060036#include <iterator>
Keir Mierle8ebb0732012-04-30 23:09:08 -070037#include <set>
38#include <string>
39#include <utility>
40#include <vector>
Sameer Agarwal509f68c2013-02-20 01:39:03 -080041#include "ceres/casts.h"
42#include "ceres/compressed_row_sparse_matrix.h"
Sameer Agarwal0beab862012-08-13 15:12:01 -070043#include "ceres/cost_function.h"
Sameer Agarwal509f68c2013-02-20 01:39:03 -080044#include "ceres/crs_matrix.h"
45#include "ceres/evaluator.h"
Sameer Agarwal0beab862012-08-13 15:12:01 -070046#include "ceres/loss_function.h"
47#include "ceres/map_util.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070048#include "ceres/parameter_block.h"
49#include "ceres/program.h"
50#include "ceres/residual_block.h"
51#include "ceres/stl_util.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070052#include "ceres/stringprintf.h"
Sameer Agarwal0beab862012-08-13 15:12:01 -070053#include "glog/logging.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070054
55namespace ceres {
56namespace internal {
57
Sameer Agarwalbcc865f2014-12-17 07:35:09 -080058using std::map;
Sameer Agarwal05a07ec2015-01-07 15:10:46 -080059using std::string;
60using std::vector;
Sameer Agarwalbcc865f2014-12-17 07:35:09 -080061typedef std::map<double*, internal::ParameterBlock*> ParameterMap;
Keir Mierle8ebb0732012-04-30 23:09:08 -070062
Sergey Sharybinf46de9e2013-03-21 15:31:35 +060063namespace {
Keir Mierle8ebb0732012-04-30 23:09:08 -070064// Returns true if two regions of memory, a and b, with sizes size_a and size_b
65// respectively, overlap.
Sergey Sharybinf46de9e2013-03-21 15:31:35 +060066bool RegionsAlias(const double* a, int size_a,
67 const double* b, int size_b) {
Keir Mierle8ebb0732012-04-30 23:09:08 -070068 return (a < b) ? b < (a + size_a)
69 : a < (b + size_b);
70}
71
Sergey Sharybinf46de9e2013-03-21 15:31:35 +060072void CheckForNoAliasing(double* existing_block,
73 int existing_block_size,
74 double* new_block,
75 int new_block_size) {
Keir Mierle8ebb0732012-04-30 23:09:08 -070076 CHECK(!RegionsAlias(existing_block, existing_block_size,
77 new_block, new_block_size))
78 << "Aliasing detected between existing parameter block at memory "
79 << "location " << existing_block
80 << " and has size " << existing_block_size << " with new parameter "
Sameer Agarwalc290df82013-04-07 09:17:47 -070081 << "block that has memory address " << new_block << " and would have "
Keir Mierle8ebb0732012-04-30 23:09:08 -070082 << "size " << new_block_size << ".";
83}
84
Sergey Sharybinf46de9e2013-03-21 15:31:35 +060085} // namespace
86
Sameer Agarwal8e1f83c2013-02-15 08:35:40 -080087ParameterBlock* ProblemImpl::InternalAddParameterBlock(double* values,
88 int size) {
89 CHECK(values != NULL) << "Null pointer passed to AddParameterBlock "
90 << "for a parameter with size " << size;
Keir Mierle8ebb0732012-04-30 23:09:08 -070091
92 // Ignore the request if there is a block for the given pointer already.
Sameer Agarwal8e1f83c2013-02-15 08:35:40 -080093 ParameterMap::iterator it = parameter_block_map_.find(values);
94 if (it != parameter_block_map_.end()) {
95 if (!options_.disable_all_safety_checks) {
96 int existing_size = it->second->Size();
97 CHECK(size == existing_size)
98 << "Tried adding a parameter block with the same double pointer, "
99 << values << ", twice, but with different block sizes. Original "
100 << "size was " << existing_size << " but new size is "
101 << size;
102 }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700103 return it->second;
104 }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700105
Sameer Agarwal8e1f83c2013-02-15 08:35:40 -0800106 if (!options_.disable_all_safety_checks) {
107 // Before adding the parameter block, also check that it doesn't alias any
108 // other parameter blocks.
109 if (!parameter_block_map_.empty()) {
110 ParameterMap::iterator lb = parameter_block_map_.lower_bound(values);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700111
Sameer Agarwal8e1f83c2013-02-15 08:35:40 -0800112 // If lb is not the first block, check the previous block for aliasing.
113 if (lb != parameter_block_map_.begin()) {
114 ParameterMap::iterator previous = lb;
115 --previous;
116 CheckForNoAliasing(previous->first,
117 previous->second->Size(),
118 values,
119 size);
120 }
121
122 // If lb is not off the end, check lb for aliasing.
123 if (lb != parameter_block_map_.end()) {
124 CheckForNoAliasing(lb->first,
125 lb->second->Size(),
126 values,
127 size);
128 }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700129 }
130 }
Sameer Agarwal8e1f83c2013-02-15 08:35:40 -0800131
Keir Mierle04938ef2013-02-17 12:37:55 -0800132 // Pass the index of the new parameter block as well to keep the index in
133 // sync with the position of the parameter in the program's parameter vector.
134 ParameterBlock* new_parameter_block =
135 new ParameterBlock(values, size, program_->parameter_blocks_.size());
136
137 // For dynamic problems, add the list of dependent residual blocks, which is
138 // empty to start.
Alex Stewart195e4932014-03-26 11:36:11 +0000139 if (options_.enable_fast_removal) {
Keir Mierle04938ef2013-02-17 12:37:55 -0800140 new_parameter_block->EnableResidualBlockDependencies();
141 }
Sameer Agarwal8e1f83c2013-02-15 08:35:40 -0800142 parameter_block_map_[values] = new_parameter_block;
143 program_->parameter_blocks_.push_back(new_parameter_block);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700144 return new_parameter_block;
145}
146
Alex Stewart195e4932014-03-26 11:36:11 +0000147void ProblemImpl::InternalRemoveResidualBlock(ResidualBlock* residual_block) {
148 CHECK_NOTNULL(residual_block);
149 // Perform no check on the validity of residual_block, that is handled in
150 // the public method: RemoveResidualBlock().
151
152 // If needed, remove the parameter dependencies on this residual block.
153 if (options_.enable_fast_removal) {
154 const int num_parameter_blocks_for_residual =
155 residual_block->NumParameterBlocks();
156 for (int i = 0; i < num_parameter_blocks_for_residual; ++i) {
157 residual_block->parameter_blocks()[i]
158 ->RemoveResidualBlock(residual_block);
159 }
160
161 ResidualBlockSet::iterator it = residual_block_set_.find(residual_block);
162 residual_block_set_.erase(it);
163 }
164 DeleteBlockInVector(program_->mutable_residual_blocks(), residual_block);
165}
166
Keir Mierle04938ef2013-02-17 12:37:55 -0800167// Deletes the residual block in question, assuming there are no other
168// references to it inside the problem (e.g. by another parameter). Referenced
169// cost and loss functions are tucked away for future deletion, since it is not
170// possible to know whether other parts of the problem depend on them without
171// doing a full scan.
172void ProblemImpl::DeleteBlock(ResidualBlock* residual_block) {
173 // The const casts here are legit, since ResidualBlock holds these
174 // pointers as const pointers but we have ownership of them and
175 // have the right to destroy them when the destructor is called.
176 if (options_.cost_function_ownership == TAKE_OWNERSHIP &&
177 residual_block->cost_function() != NULL) {
178 cost_functions_to_delete_.push_back(
179 const_cast<CostFunction*>(residual_block->cost_function()));
180 }
181 if (options_.loss_function_ownership == TAKE_OWNERSHIP &&
182 residual_block->loss_function() != NULL) {
183 loss_functions_to_delete_.push_back(
184 const_cast<LossFunction*>(residual_block->loss_function()));
185 }
186 delete residual_block;
187}
188
189// Deletes the parameter block in question, assuming there are no other
190// references to it inside the problem (e.g. by any residual blocks).
191// Referenced parameterizations are tucked away for future deletion, since it
192// is not possible to know whether other parts of the problem depend on them
193// without doing a full scan.
194void ProblemImpl::DeleteBlock(ParameterBlock* parameter_block) {
195 if (options_.local_parameterization_ownership == TAKE_OWNERSHIP &&
196 parameter_block->local_parameterization() != NULL) {
197 local_parameterizations_to_delete_.push_back(
198 parameter_block->mutable_local_parameterization());
199 }
200 parameter_block_map_.erase(parameter_block->mutable_user_state());
201 delete parameter_block;
202}
203
Keir Mierle8ebb0732012-04-30 23:09:08 -0700204ProblemImpl::ProblemImpl() : program_(new internal::Program) {}
205ProblemImpl::ProblemImpl(const Problem::Options& options)
206 : options_(options),
207 program_(new internal::Program) {}
208
209ProblemImpl::~ProblemImpl() {
210 // Collect the unique cost/loss functions and delete the residuals.
Sameer Agarwalbeb45052013-02-22 13:37:01 -0800211 const int num_residual_blocks = program_->residual_blocks_.size();
Keir Mierle04938ef2013-02-17 12:37:55 -0800212 cost_functions_to_delete_.reserve(num_residual_blocks);
213 loss_functions_to_delete_.reserve(num_residual_blocks);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700214 for (int i = 0; i < program_->residual_blocks_.size(); ++i) {
Keir Mierle04938ef2013-02-17 12:37:55 -0800215 DeleteBlock(program_->residual_blocks_[i]);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700216 }
217
218 // Collect the unique parameterizations and delete the parameters.
Keir Mierle8ebb0732012-04-30 23:09:08 -0700219 for (int i = 0; i < program_->parameter_blocks_.size(); ++i) {
Keir Mierle04938ef2013-02-17 12:37:55 -0800220 DeleteBlock(program_->parameter_blocks_[i]);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700221 }
222
223 // Delete the owned cost/loss functions and parameterizations.
Keir Mierle04938ef2013-02-17 12:37:55 -0800224 STLDeleteUniqueContainerPointers(local_parameterizations_to_delete_.begin(),
225 local_parameterizations_to_delete_.end());
226 STLDeleteUniqueContainerPointers(cost_functions_to_delete_.begin(),
227 cost_functions_to_delete_.end());
228 STLDeleteUniqueContainerPointers(loss_functions_to_delete_.begin(),
229 loss_functions_to_delete_.end());
Keir Mierle8ebb0732012-04-30 23:09:08 -0700230}
231
Keir Mierle04938ef2013-02-17 12:37:55 -0800232ResidualBlock* ProblemImpl::AddResidualBlock(
Keir Mierle8ebb0732012-04-30 23:09:08 -0700233 CostFunction* cost_function,
234 LossFunction* loss_function,
235 const vector<double*>& parameter_blocks) {
236 CHECK_NOTNULL(cost_function);
237 CHECK_EQ(parameter_blocks.size(),
238 cost_function->parameter_block_sizes().size());
239
240 // Check the sizes match.
Sameer Agarwal85561ee2014-01-07 22:22:14 -0800241 const vector<int32>& parameter_block_sizes =
Keir Mierle8ebb0732012-04-30 23:09:08 -0700242 cost_function->parameter_block_sizes();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700243
Sameer Agarwal8e1f83c2013-02-15 08:35:40 -0800244 if (!options_.disable_all_safety_checks) {
245 CHECK_EQ(parameter_block_sizes.size(), parameter_blocks.size())
246 << "Number of blocks input is different than the number of blocks "
247 << "that the cost function expects.";
248
249 // Check for duplicate parameter blocks.
250 vector<double*> sorted_parameter_blocks(parameter_blocks);
251 sort(sorted_parameter_blocks.begin(), sorted_parameter_blocks.end());
Sameer Agarwala1019f62015-12-12 15:02:15 -0800252 const bool has_duplicate_items =
253 (std::adjacent_find(sorted_parameter_blocks.begin(),
254 sorted_parameter_blocks.end())
255 != sorted_parameter_blocks.end());
Sameer Agarwal8ecfb2d2015-12-17 13:55:02 -0800256 if (has_duplicate_items) {
Sameer Agarwal8e1f83c2013-02-15 08:35:40 -0800257 string blocks;
258 for (int i = 0; i < parameter_blocks.size(); ++i) {
Sameer Agarwal509f68c2013-02-20 01:39:03 -0800259 blocks += StringPrintf(" %p ", parameter_blocks[i]);
Sameer Agarwal8e1f83c2013-02-15 08:35:40 -0800260 }
261
262 LOG(FATAL) << "Duplicate parameter blocks in a residual parameter "
263 << "are not allowed. Parameter block pointers: ["
264 << blocks << "]";
Keir Mierle8ebb0732012-04-30 23:09:08 -0700265 }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700266 }
267
268 // Add parameter blocks and convert the double*'s to parameter blocks.
269 vector<ParameterBlock*> parameter_block_ptrs(parameter_blocks.size());
270 for (int i = 0; i < parameter_blocks.size(); ++i) {
271 parameter_block_ptrs[i] =
272 InternalAddParameterBlock(parameter_blocks[i],
Sameer Agarwal8e1f83c2013-02-15 08:35:40 -0800273 parameter_block_sizes[i]);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700274 }
275
Sameer Agarwal8e1f83c2013-02-15 08:35:40 -0800276 if (!options_.disable_all_safety_checks) {
277 // Check that the block sizes match the block sizes expected by the
278 // cost_function.
279 for (int i = 0; i < parameter_block_ptrs.size(); ++i) {
280 CHECK_EQ(cost_function->parameter_block_sizes()[i],
281 parameter_block_ptrs[i]->Size())
282 << "The cost function expects parameter block " << i
283 << " of size " << cost_function->parameter_block_sizes()[i]
284 << " but was given a block of size "
285 << parameter_block_ptrs[i]->Size();
286 }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700287 }
288
289 ResidualBlock* new_residual_block =
290 new ResidualBlock(cost_function,
291 loss_function,
Keir Mierle04938ef2013-02-17 12:37:55 -0800292 parameter_block_ptrs,
293 program_->residual_blocks_.size());
294
295 // Add dependencies on the residual to the parameter blocks.
Alex Stewart195e4932014-03-26 11:36:11 +0000296 if (options_.enable_fast_removal) {
Keir Mierle04938ef2013-02-17 12:37:55 -0800297 for (int i = 0; i < parameter_blocks.size(); ++i) {
298 parameter_block_ptrs[i]->AddResidualBlock(new_residual_block);
299 }
300 }
301
Keir Mierle8ebb0732012-04-30 23:09:08 -0700302 program_->residual_blocks_.push_back(new_residual_block);
Alex Stewart195e4932014-03-26 11:36:11 +0000303
304 if (options_.enable_fast_removal) {
305 residual_block_set_.insert(new_residual_block);
306 }
307
Keir Mierle8ebb0732012-04-30 23:09:08 -0700308 return new_residual_block;
309}
310
311// Unfortunately, macros don't help much to reduce this code, and var args don't
312// work because of the ambiguous case that there is no loss function.
Keir Mierle04938ef2013-02-17 12:37:55 -0800313ResidualBlock* ProblemImpl::AddResidualBlock(
Keir Mierle8ebb0732012-04-30 23:09:08 -0700314 CostFunction* cost_function,
315 LossFunction* loss_function,
316 double* x0) {
317 vector<double*> residual_parameters;
318 residual_parameters.push_back(x0);
319 return AddResidualBlock(cost_function, loss_function, residual_parameters);
320}
321
Keir Mierle04938ef2013-02-17 12:37:55 -0800322ResidualBlock* ProblemImpl::AddResidualBlock(
Keir Mierle8ebb0732012-04-30 23:09:08 -0700323 CostFunction* cost_function,
324 LossFunction* loss_function,
325 double* x0, double* x1) {
326 vector<double*> residual_parameters;
327 residual_parameters.push_back(x0);
328 residual_parameters.push_back(x1);
329 return AddResidualBlock(cost_function, loss_function, residual_parameters);
330}
331
Keir Mierle04938ef2013-02-17 12:37:55 -0800332ResidualBlock* ProblemImpl::AddResidualBlock(
Keir Mierle8ebb0732012-04-30 23:09:08 -0700333 CostFunction* cost_function,
334 LossFunction* loss_function,
335 double* x0, double* x1, double* x2) {
336 vector<double*> residual_parameters;
337 residual_parameters.push_back(x0);
338 residual_parameters.push_back(x1);
339 residual_parameters.push_back(x2);
340 return AddResidualBlock(cost_function, loss_function, residual_parameters);
341}
342
Keir Mierle04938ef2013-02-17 12:37:55 -0800343ResidualBlock* ProblemImpl::AddResidualBlock(
Keir Mierle8ebb0732012-04-30 23:09:08 -0700344 CostFunction* cost_function,
345 LossFunction* loss_function,
346 double* x0, double* x1, double* x2, double* x3) {
347 vector<double*> residual_parameters;
348 residual_parameters.push_back(x0);
349 residual_parameters.push_back(x1);
350 residual_parameters.push_back(x2);
351 residual_parameters.push_back(x3);
352 return AddResidualBlock(cost_function, loss_function, residual_parameters);
353}
354
Keir Mierle04938ef2013-02-17 12:37:55 -0800355ResidualBlock* ProblemImpl::AddResidualBlock(
Keir Mierle8ebb0732012-04-30 23:09:08 -0700356 CostFunction* cost_function,
357 LossFunction* loss_function,
358 double* x0, double* x1, double* x2, double* x3, double* x4) {
359 vector<double*> residual_parameters;
360 residual_parameters.push_back(x0);
361 residual_parameters.push_back(x1);
362 residual_parameters.push_back(x2);
363 residual_parameters.push_back(x3);
364 residual_parameters.push_back(x4);
365 return AddResidualBlock(cost_function, loss_function, residual_parameters);
366}
367
Keir Mierle04938ef2013-02-17 12:37:55 -0800368ResidualBlock* ProblemImpl::AddResidualBlock(
Keir Mierle8ebb0732012-04-30 23:09:08 -0700369 CostFunction* cost_function,
370 LossFunction* loss_function,
371 double* x0, double* x1, double* x2, double* x3, double* x4, double* x5) {
372 vector<double*> residual_parameters;
373 residual_parameters.push_back(x0);
374 residual_parameters.push_back(x1);
375 residual_parameters.push_back(x2);
376 residual_parameters.push_back(x3);
377 residual_parameters.push_back(x4);
378 residual_parameters.push_back(x5);
379 return AddResidualBlock(cost_function, loss_function, residual_parameters);
380}
381
Keir Mierle04938ef2013-02-17 12:37:55 -0800382ResidualBlock* ProblemImpl::AddResidualBlock(
Fisher12626e82012-10-21 14:12:04 -0400383 CostFunction* cost_function,
384 LossFunction* loss_function,
385 double* x0, double* x1, double* x2, double* x3, double* x4, double* x5,
386 double* x6) {
387 vector<double*> residual_parameters;
388 residual_parameters.push_back(x0);
389 residual_parameters.push_back(x1);
390 residual_parameters.push_back(x2);
391 residual_parameters.push_back(x3);
392 residual_parameters.push_back(x4);
393 residual_parameters.push_back(x5);
394 residual_parameters.push_back(x6);
395 return AddResidualBlock(cost_function, loss_function, residual_parameters);
396}
397
Keir Mierle04938ef2013-02-17 12:37:55 -0800398ResidualBlock* ProblemImpl::AddResidualBlock(
Fisher12626e82012-10-21 14:12:04 -0400399 CostFunction* cost_function,
400 LossFunction* loss_function,
401 double* x0, double* x1, double* x2, double* x3, double* x4, double* x5,
402 double* x6, double* x7) {
403 vector<double*> residual_parameters;
404 residual_parameters.push_back(x0);
405 residual_parameters.push_back(x1);
406 residual_parameters.push_back(x2);
407 residual_parameters.push_back(x3);
408 residual_parameters.push_back(x4);
409 residual_parameters.push_back(x5);
410 residual_parameters.push_back(x6);
411 residual_parameters.push_back(x7);
412 return AddResidualBlock(cost_function, loss_function, residual_parameters);
413}
414
Keir Mierle04938ef2013-02-17 12:37:55 -0800415ResidualBlock* ProblemImpl::AddResidualBlock(
Fisher12626e82012-10-21 14:12:04 -0400416 CostFunction* cost_function,
417 LossFunction* loss_function,
418 double* x0, double* x1, double* x2, double* x3, double* x4, double* x5,
419 double* x6, double* x7, double* x8) {
420 vector<double*> residual_parameters;
421 residual_parameters.push_back(x0);
422 residual_parameters.push_back(x1);
423 residual_parameters.push_back(x2);
424 residual_parameters.push_back(x3);
425 residual_parameters.push_back(x4);
426 residual_parameters.push_back(x5);
427 residual_parameters.push_back(x6);
428 residual_parameters.push_back(x7);
429 residual_parameters.push_back(x8);
430 return AddResidualBlock(cost_function, loss_function, residual_parameters);
431}
432
Keir Mierle04938ef2013-02-17 12:37:55 -0800433ResidualBlock* ProblemImpl::AddResidualBlock(
Fisher12626e82012-10-21 14:12:04 -0400434 CostFunction* cost_function,
435 LossFunction* loss_function,
436 double* x0, double* x1, double* x2, double* x3, double* x4, double* x5,
437 double* x6, double* x7, double* x8, double* x9) {
438 vector<double*> residual_parameters;
439 residual_parameters.push_back(x0);
440 residual_parameters.push_back(x1);
441 residual_parameters.push_back(x2);
442 residual_parameters.push_back(x3);
443 residual_parameters.push_back(x4);
444 residual_parameters.push_back(x5);
445 residual_parameters.push_back(x6);
446 residual_parameters.push_back(x7);
447 residual_parameters.push_back(x8);
448 residual_parameters.push_back(x9);
449 return AddResidualBlock(cost_function, loss_function, residual_parameters);
450}
Keir Mierle8ebb0732012-04-30 23:09:08 -0700451
452void ProblemImpl::AddParameterBlock(double* values, int size) {
Sameer Agarwal8e1f83c2013-02-15 08:35:40 -0800453 InternalAddParameterBlock(values, size);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700454}
455
456void ProblemImpl::AddParameterBlock(
457 double* values,
458 int size,
459 LocalParameterization* local_parameterization) {
460 ParameterBlock* parameter_block =
Sameer Agarwal8e1f83c2013-02-15 08:35:40 -0800461 InternalAddParameterBlock(values, size);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700462 if (local_parameterization != NULL) {
463 parameter_block->SetParameterization(local_parameterization);
464 }
465}
466
Keir Mierle04938ef2013-02-17 12:37:55 -0800467// Delete a block from a vector of blocks, maintaining the indexing invariant.
468// This is done in constant time by moving an element from the end of the
469// vector over the element to remove, then popping the last element. It
470// destroys the ordering in the interest of speed.
471template<typename Block>
472void ProblemImpl::DeleteBlockInVector(vector<Block*>* mutable_blocks,
473 Block* block_to_remove) {
474 CHECK_EQ((*mutable_blocks)[block_to_remove->index()], block_to_remove)
Sameer Agarwal8af13222013-09-23 22:10:24 -0700475 << "You found a Ceres bug! \n"
476 << "Block requested: "
477 << block_to_remove->ToString() << "\n"
478 << "Block present: "
479 << (*mutable_blocks)[block_to_remove->index()]->ToString();
Keir Mierle04938ef2013-02-17 12:37:55 -0800480
481 // Prepare the to-be-moved block for the new, lower-in-index position by
482 // setting the index to the blocks final location.
483 Block* tmp = mutable_blocks->back();
484 tmp->set_index(block_to_remove->index());
485
486 // Overwrite the to-be-deleted residual block with the one at the end.
487 (*mutable_blocks)[block_to_remove->index()] = tmp;
488
489 DeleteBlock(block_to_remove);
490
491 // The block is gone so shrink the vector of blocks accordingly.
492 mutable_blocks->pop_back();
493}
494
495void ProblemImpl::RemoveResidualBlock(ResidualBlock* residual_block) {
496 CHECK_NOTNULL(residual_block);
497
Alex Stewart195e4932014-03-26 11:36:11 +0000498 // Verify that residual_block identifies a residual in the current problem.
499 const string residual_not_found_message =
500 StringPrintf("Residual block to remove: %p not found. This usually means "
501 "one of three things have happened:\n"
502 " 1) residual_block is uninitialised and points to a random "
503 "area in memory.\n"
504 " 2) residual_block represented a residual that was added to"
505 " the problem, but referred to a parameter block which has "
506 "since been removed, which removes all residuals which "
507 "depend on that parameter block, and was thus removed.\n"
508 " 3) residual_block referred to a residual that has already "
509 "been removed from the problem (by the user).",
510 residual_block);
511 if (options_.enable_fast_removal) {
512 CHECK(residual_block_set_.find(residual_block) !=
513 residual_block_set_.end())
514 << residual_not_found_message;
515 } else {
516 // Perform a full search over all current residuals.
517 CHECK(std::find(program_->residual_blocks().begin(),
518 program_->residual_blocks().end(),
519 residual_block) != program_->residual_blocks().end())
520 << residual_not_found_message;
Keir Mierle04938ef2013-02-17 12:37:55 -0800521 }
Alex Stewart195e4932014-03-26 11:36:11 +0000522
523 InternalRemoveResidualBlock(residual_block);
Keir Mierle04938ef2013-02-17 12:37:55 -0800524}
525
526void ProblemImpl::RemoveParameterBlock(double* values) {
Sameer Agarwal020d8e12013-03-06 16:19:26 -0800527 ParameterBlock* parameter_block =
Sameer Agarwaldb1a76d2015-01-16 11:47:07 -0800528 FindWithDefault(parameter_block_map_, values, NULL);
529 if (parameter_block == NULL) {
530 LOG(FATAL) << "Parameter block not found: " << values
531 << ". You must add the parameter block to the problem before "
532 << "it can be removed.";
533 }
Keir Mierle04938ef2013-02-17 12:37:55 -0800534
Alex Stewart195e4932014-03-26 11:36:11 +0000535 if (options_.enable_fast_removal) {
Keir Mierle04938ef2013-02-17 12:37:55 -0800536 // Copy the dependent residuals from the parameter block because the set of
537 // dependents will change after each call to RemoveResidualBlock().
538 vector<ResidualBlock*> residual_blocks_to_remove(
539 parameter_block->mutable_residual_blocks()->begin(),
540 parameter_block->mutable_residual_blocks()->end());
541 for (int i = 0; i < residual_blocks_to_remove.size(); ++i) {
Alex Stewart195e4932014-03-26 11:36:11 +0000542 InternalRemoveResidualBlock(residual_blocks_to_remove[i]);
Keir Mierle04938ef2013-02-17 12:37:55 -0800543 }
544 } else {
545 // Scan all the residual blocks to remove ones that depend on the parameter
546 // block. Do the scan backwards since the vector changes while iterating.
547 const int num_residual_blocks = NumResidualBlocks();
548 for (int i = num_residual_blocks - 1; i >= 0; --i) {
549 ResidualBlock* residual_block =
550 (*(program_->mutable_residual_blocks()))[i];
551 const int num_parameter_blocks = residual_block->NumParameterBlocks();
Sameer Agarwalbeb45052013-02-22 13:37:01 -0800552 for (int j = 0; j < num_parameter_blocks; ++j) {
553 if (residual_block->parameter_blocks()[j] == parameter_block) {
Alex Stewart195e4932014-03-26 11:36:11 +0000554 InternalRemoveResidualBlock(residual_block);
Keir Mierle04938ef2013-02-17 12:37:55 -0800555 // The parameter blocks are guaranteed unique.
556 break;
557 }
558 }
559 }
560 }
561 DeleteBlockInVector(program_->mutable_parameter_blocks(), parameter_block);
562}
563
Keir Mierle8ebb0732012-04-30 23:09:08 -0700564void ProblemImpl::SetParameterBlockConstant(double* values) {
Sameer Agarwaldb1a76d2015-01-16 11:47:07 -0800565 ParameterBlock* parameter_block =
566 FindWithDefault(parameter_block_map_, values, NULL);
567 if (parameter_block == NULL) {
568 LOG(FATAL) << "Parameter block not found: " << values
569 << ". You must add the parameter block to the problem before "
570 << "it can be set constant.";
571 }
572
573 parameter_block->SetConstant();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700574}
575
Thomas Schneiderd61e94d2016-04-06 10:40:29 +0200576bool ProblemImpl::IsParameterBlockConstant(double* values) const {
577 const ParameterBlock* parameter_block =
578 FindWithDefault(parameter_block_map_, values, NULL);
579 CHECK(parameter_block != NULL)
580 << "Parameter block not found: " << values << ". You must add the "
581 << "parameter block to the problem before it can be queried.";
582
583 return parameter_block->IsConstant();
584}
585
Keir Mierle8ebb0732012-04-30 23:09:08 -0700586void ProblemImpl::SetParameterBlockVariable(double* values) {
Sameer Agarwaldb1a76d2015-01-16 11:47:07 -0800587 ParameterBlock* parameter_block =
588 FindWithDefault(parameter_block_map_, values, NULL);
589 if (parameter_block == NULL) {
590 LOG(FATAL) << "Parameter block not found: " << values
591 << ". You must add the parameter block to the problem before "
592 << "it can be set varying.";
593 }
594
595 parameter_block->SetVarying();
Keir Mierle8ebb0732012-04-30 23:09:08 -0700596}
597
598void ProblemImpl::SetParameterization(
599 double* values,
600 LocalParameterization* local_parameterization) {
Sameer Agarwaldb1a76d2015-01-16 11:47:07 -0800601 ParameterBlock* parameter_block =
602 FindWithDefault(parameter_block_map_, values, NULL);
603 if (parameter_block == NULL) {
604 LOG(FATAL) << "Parameter block not found: " << values
605 << ". You must add the parameter block to the problem before "
606 << "you can set its local parameterization.";
607 }
608
609 parameter_block->SetParameterization(local_parameterization);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700610}
611
Sameer Agarwalf949bab2014-02-18 10:11:02 -0800612const LocalParameterization* ProblemImpl::GetParameterization(
613 double* values) const {
Sameer Agarwaldb1a76d2015-01-16 11:47:07 -0800614 ParameterBlock* parameter_block =
615 FindWithDefault(parameter_block_map_, values, NULL);
616 if (parameter_block == NULL) {
617 LOG(FATAL) << "Parameter block not found: " << values
618 << ". You must add the parameter block to the problem before "
619 << "you can get its local parameterization.";
620 }
621
622 return parameter_block->local_parameterization();
Sameer Agarwalf949bab2014-02-18 10:11:02 -0800623}
624
Sameer Agarwala482ab82014-02-18 22:24:03 -0800625void ProblemImpl::SetParameterLowerBound(double* values,
626 int index,
627 double lower_bound) {
Sameer Agarwaldb1a76d2015-01-16 11:47:07 -0800628 ParameterBlock* parameter_block =
629 FindWithDefault(parameter_block_map_, values, NULL);
630 if (parameter_block == NULL) {
631 LOG(FATAL) << "Parameter block not found: " << values
632 << ". You must add the parameter block to the problem before "
633 << "you can set a lower bound on one of its components.";
634 }
635
636 parameter_block->SetLowerBound(index, lower_bound);
Sameer Agarwala482ab82014-02-18 22:24:03 -0800637}
638
639void ProblemImpl::SetParameterUpperBound(double* values,
640 int index,
641 double upper_bound) {
Sameer Agarwaldb1a76d2015-01-16 11:47:07 -0800642 ParameterBlock* parameter_block =
643 FindWithDefault(parameter_block_map_, values, NULL);
644 if (parameter_block == NULL) {
645 LOG(FATAL) << "Parameter block not found: " << values
646 << ". You must add the parameter block to the problem before "
647 << "you can set an upper bound on one of its components.";
648 }
649 parameter_block->SetUpperBound(index, upper_bound);
Sameer Agarwala482ab82014-02-18 22:24:03 -0800650}
651
Sameer Agarwal509f68c2013-02-20 01:39:03 -0800652bool ProblemImpl::Evaluate(const Problem::EvaluateOptions& evaluate_options,
653 double* cost,
654 vector<double>* residuals,
655 vector<double>* gradient,
656 CRSMatrix* jacobian) {
657 if (cost == NULL &&
658 residuals == NULL &&
659 gradient == NULL &&
660 jacobian == NULL) {
661 LOG(INFO) << "Nothing to do.";
662 return true;
663 }
664
665 // If the user supplied residual blocks, then use them, otherwise
666 // take the residual blocks from the underlying program.
667 Program program;
668 *program.mutable_residual_blocks() =
669 ((evaluate_options.residual_blocks.size() > 0)
670 ? evaluate_options.residual_blocks : program_->residual_blocks());
671
672 const vector<double*>& parameter_block_ptrs =
673 evaluate_options.parameter_blocks;
674
675 vector<ParameterBlock*> variable_parameter_blocks;
676 vector<ParameterBlock*>& parameter_blocks =
677 *program.mutable_parameter_blocks();
678
679 if (parameter_block_ptrs.size() == 0) {
680 // The user did not provide any parameter blocks, so default to
681 // using all the parameter blocks in the order that they are in
682 // the underlying program object.
683 parameter_blocks = program_->parameter_blocks();
684 } else {
685 // The user supplied a vector of parameter blocks. Using this list
686 // requires a number of steps.
687
688 // 1. Convert double* into ParameterBlock*
689 parameter_blocks.resize(parameter_block_ptrs.size());
690 for (int i = 0; i < parameter_block_ptrs.size(); ++i) {
Sameer Agarwaldb1a76d2015-01-16 11:47:07 -0800691 parameter_blocks[i] = FindWithDefault(parameter_block_map_,
692 parameter_block_ptrs[i],
693 NULL);
694 if (parameter_blocks[i] == NULL) {
695 LOG(FATAL) << "No known parameter block for "
696 << "Problem::Evaluate::Options.parameter_blocks[" << i << "]"
697 << " = " << parameter_block_ptrs[i];
698 }
Sameer Agarwal509f68c2013-02-20 01:39:03 -0800699 }
700
701 // 2. The user may have only supplied a subset of parameter
702 // blocks, so identify the ones that are not supplied by the user
703 // and are NOT constant. These parameter blocks are stored in
704 // variable_parameter_blocks.
705 //
706 // To ensure that the parameter blocks are not included in the
707 // columns of the jacobian, we need to make sure that they are
708 // constant during evaluation and then make them variable again
709 // after we are done.
710 vector<ParameterBlock*> all_parameter_blocks(program_->parameter_blocks());
711 vector<ParameterBlock*> included_parameter_blocks(
712 program.parameter_blocks());
713
714 vector<ParameterBlock*> excluded_parameter_blocks;
715 sort(all_parameter_blocks.begin(), all_parameter_blocks.end());
716 sort(included_parameter_blocks.begin(), included_parameter_blocks.end());
717 set_difference(all_parameter_blocks.begin(),
718 all_parameter_blocks.end(),
719 included_parameter_blocks.begin(),
720 included_parameter_blocks.end(),
721 back_inserter(excluded_parameter_blocks));
722
723 variable_parameter_blocks.reserve(excluded_parameter_blocks.size());
724 for (int i = 0; i < excluded_parameter_blocks.size(); ++i) {
725 ParameterBlock* parameter_block = excluded_parameter_blocks[i];
726 if (!parameter_block->IsConstant()) {
727 variable_parameter_blocks.push_back(parameter_block);
728 parameter_block->SetConstant();
729 }
730 }
731 }
732
733 // Setup the Parameter indices and offsets before an evaluator can
734 // be constructed and used.
735 program.SetParameterOffsetsAndIndex();
736
737 Evaluator::Options evaluator_options;
738
739 // Even though using SPARSE_NORMAL_CHOLESKY requires SuiteSparse or
740 // CXSparse, here it just being used for telling the evaluator to
741 // use a SparseRowCompressedMatrix for the jacobian. This is because
742 // the Evaluator decides the storage for the Jacobian based on the
743 // type of linear solver being used.
744 evaluator_options.linear_solver_type = SPARSE_NORMAL_CHOLESKY;
Alex Stewartffa32472015-03-15 18:11:17 +0000745#ifndef CERES_USE_OPENMP
746 LOG_IF(WARNING, evaluate_options.num_threads > 1)
747 << "OpenMP support is not compiled into this binary; "
748 << "only evaluate_options.num_threads = 1 is supported. Switching "
749 << "to single threaded mode.";
750 evaluator_options.num_threads = 1;
751#else
Sameer Agarwal509f68c2013-02-20 01:39:03 -0800752 evaluator_options.num_threads = evaluate_options.num_threads;
Alex Stewartffa32472015-03-15 18:11:17 +0000753#endif // CERES_USE_OPENMP
Sameer Agarwal509f68c2013-02-20 01:39:03 -0800754
755 string error;
756 scoped_ptr<Evaluator> evaluator(
757 Evaluator::Create(evaluator_options, &program, &error));
758 if (evaluator.get() == NULL) {
759 LOG(ERROR) << "Unable to create an Evaluator object. "
760 << "Error: " << error
761 << "This is a Ceres bug; please contact the developers!";
762
763 // Make the parameter blocks that were temporarily marked
764 // constant, variable again.
765 for (int i = 0; i < variable_parameter_blocks.size(); ++i) {
766 variable_parameter_blocks[i]->SetVarying();
767 }
Sameer Agarwal324eccb2013-12-03 09:28:14 -0800768
769 program_->SetParameterBlockStatePtrsToUserStatePtrs();
770 program_->SetParameterOffsetsAndIndex();
Sameer Agarwal509f68c2013-02-20 01:39:03 -0800771 return false;
772 }
773
774 if (residuals !=NULL) {
775 residuals->resize(evaluator->NumResiduals());
776 }
777
778 if (gradient != NULL) {
779 gradient->resize(evaluator->NumEffectiveParameters());
780 }
781
782 scoped_ptr<CompressedRowSparseMatrix> tmp_jacobian;
783 if (jacobian != NULL) {
784 tmp_jacobian.reset(
785 down_cast<CompressedRowSparseMatrix*>(evaluator->CreateJacobian()));
786 }
787
788 // Point the state pointers to the user state pointers. This is
789 // needed so that we can extract a parameter vector which is then
790 // passed to Evaluator::Evaluate.
791 program.SetParameterBlockStatePtrsToUserStatePtrs();
792
793 // Copy the value of the parameter blocks into a vector, since the
794 // Evaluate::Evaluate method needs its input as such. The previous
795 // call to SetParameterBlockStatePtrsToUserStatePtrs ensures that
796 // these values are the ones corresponding to the actual state of
797 // the parameter blocks, rather than the temporary state pointer
798 // used for evaluation.
799 Vector parameters(program.NumParameters());
800 program.ParameterBlocksToStateVector(parameters.data());
801
802 double tmp_cost = 0;
Sameer Agarwal039ff072013-02-26 09:15:39 -0800803
804 Evaluator::EvaluateOptions evaluator_evaluate_options;
805 evaluator_evaluate_options.apply_loss_function =
806 evaluate_options.apply_loss_function;
807 bool status = evaluator->Evaluate(evaluator_evaluate_options,
808 parameters.data(),
Sameer Agarwal509f68c2013-02-20 01:39:03 -0800809 &tmp_cost,
810 residuals != NULL ? &(*residuals)[0] : NULL,
811 gradient != NULL ? &(*gradient)[0] : NULL,
812 tmp_jacobian.get());
813
Sameer Agarwal931c3092013-02-25 09:46:21 -0800814 // Make the parameter blocks that were temporarily marked constant,
815 // variable again.
Sameer Agarwal509f68c2013-02-20 01:39:03 -0800816 for (int i = 0; i < variable_parameter_blocks.size(); ++i) {
817 variable_parameter_blocks[i]->SetVarying();
818 }
819
820 if (status) {
821 if (cost != NULL) {
822 *cost = tmp_cost;
823 }
824 if (jacobian != NULL) {
825 tmp_jacobian->ToCRSMatrix(jacobian);
826 }
827 }
828
Sameer Agarwal324eccb2013-12-03 09:28:14 -0800829 program_->SetParameterBlockStatePtrsToUserStatePtrs();
830 program_->SetParameterOffsetsAndIndex();
Sameer Agarwal509f68c2013-02-20 01:39:03 -0800831 return status;
832}
833
Keir Mierle8ebb0732012-04-30 23:09:08 -0700834int ProblemImpl::NumParameterBlocks() const {
835 return program_->NumParameterBlocks();
836}
837
838int ProblemImpl::NumParameters() const {
839 return program_->NumParameters();
840}
841
842int ProblemImpl::NumResidualBlocks() const {
843 return program_->NumResidualBlocks();
844}
845
846int ProblemImpl::NumResiduals() const {
847 return program_->NumResiduals();
848}
849
Sameer Agarwaldb1a76d2015-01-16 11:47:07 -0800850int ProblemImpl::ParameterBlockSize(const double* values) const {
851 ParameterBlock* parameter_block =
852 FindWithDefault(parameter_block_map_, const_cast<double*>(values), NULL);
853 if (parameter_block == NULL) {
854 LOG(FATAL) << "Parameter block not found: " << values
855 << ". You must add the parameter block to the problem before "
856 << "you can get its size.";
857 }
858
859 return parameter_block->Size();
Sameer Agarwalbcc865f2014-12-17 07:35:09 -0800860}
Sameer Agarwal3d954692013-04-18 14:54:55 -0700861
Sameer Agarwaldb1a76d2015-01-16 11:47:07 -0800862int ProblemImpl::ParameterBlockLocalSize(const double* values) const {
863 ParameterBlock* parameter_block =
864 FindWithDefault(parameter_block_map_, const_cast<double*>(values), NULL);
865 if (parameter_block == NULL) {
866 LOG(FATAL) << "Parameter block not found: " << values
867 << ". You must add the parameter block to the problem before "
868 << "you can get its local size.";
869 }
870
871 return parameter_block->LocalSize();
Sameer Agarwalbcc865f2014-12-17 07:35:09 -0800872}
Sameer Agarwal3d954692013-04-18 14:54:55 -0700873
Sameer Agarwal5ecb1c32014-04-01 09:20:35 -0700874bool ProblemImpl::HasParameterBlock(const double* parameter_block) const {
875 return (parameter_block_map_.find(const_cast<double*>(parameter_block)) !=
876 parameter_block_map_.end());
877}
878
Sameer Agarwal3d954692013-04-18 14:54:55 -0700879void ProblemImpl::GetParameterBlocks(vector<double*>* parameter_blocks) const {
880 CHECK_NOTNULL(parameter_blocks);
881 parameter_blocks->resize(0);
882 for (ParameterMap::const_iterator it = parameter_block_map_.begin();
883 it != parameter_block_map_.end();
884 ++it) {
885 parameter_blocks->push_back(it->first);
886 }
887}
888
Keir Mierlefda69b52013-10-10 00:25:24 -0700889void ProblemImpl::GetResidualBlocks(
890 vector<ResidualBlockId>* residual_blocks) const {
891 CHECK_NOTNULL(residual_blocks);
892 *residual_blocks = program().residual_blocks();
893}
894
895void ProblemImpl::GetParameterBlocksForResidualBlock(
896 const ResidualBlockId residual_block,
897 vector<double*>* parameter_blocks) const {
898 int num_parameter_blocks = residual_block->NumParameterBlocks();
899 CHECK_NOTNULL(parameter_blocks)->resize(num_parameter_blocks);
900 for (int i = 0; i < num_parameter_blocks; ++i) {
901 (*parameter_blocks)[i] =
902 residual_block->parameter_blocks()[i]->mutable_user_state();
903 }
904}
905
Sameer Agarwal6c45d6b2014-09-11 07:48:30 -0700906const CostFunction* ProblemImpl::GetCostFunctionForResidualBlock(
907 const ResidualBlockId residual_block) const {
908 return residual_block->cost_function();
909}
910
911const LossFunction* ProblemImpl::GetLossFunctionForResidualBlock(
912 const ResidualBlockId residual_block) const {
913 return residual_block->loss_function();
914}
915
Keir Mierlefda69b52013-10-10 00:25:24 -0700916void ProblemImpl::GetResidualBlocksForParameterBlock(
917 const double* values,
918 vector<ResidualBlockId>* residual_blocks) const {
919 ParameterBlock* parameter_block =
Sameer Agarwaldb1a76d2015-01-16 11:47:07 -0800920 FindWithDefault(parameter_block_map_, const_cast<double*>(values), NULL);
921 if (parameter_block == NULL) {
922 LOG(FATAL) << "Parameter block not found: " << values
923 << ". You must add the parameter block to the problem before "
924 << "you can get the residual blocks that depend on it.";
925 }
Keir Mierlefda69b52013-10-10 00:25:24 -0700926
Alex Stewart195e4932014-03-26 11:36:11 +0000927 if (options_.enable_fast_removal) {
Keir Mierlefda69b52013-10-10 00:25:24 -0700928 // In this case the residual blocks that depend on the parameter block are
929 // stored in the parameter block already, so just copy them out.
Sameer Agarwal89a592f2013-11-26 11:35:49 -0800930 CHECK_NOTNULL(residual_blocks)->resize(
931 parameter_block->mutable_residual_blocks()->size());
Keir Mierlefda69b52013-10-10 00:25:24 -0700932 std::copy(parameter_block->mutable_residual_blocks()->begin(),
933 parameter_block->mutable_residual_blocks()->end(),
934 residual_blocks->begin());
935 return;
936 }
937
938 // Find residual blocks that depend on the parameter block.
939 CHECK_NOTNULL(residual_blocks)->clear();
940 const int num_residual_blocks = NumResidualBlocks();
941 for (int i = 0; i < num_residual_blocks; ++i) {
942 ResidualBlock* residual_block =
943 (*(program_->mutable_residual_blocks()))[i];
944 const int num_parameter_blocks = residual_block->NumParameterBlocks();
945 for (int j = 0; j < num_parameter_blocks; ++j) {
946 if (residual_block->parameter_blocks()[j] == parameter_block) {
947 residual_blocks->push_back(residual_block);
948 // The parameter blocks are guaranteed unique.
949 break;
950 }
951 }
952 }
953}
Sameer Agarwal3d954692013-04-18 14:54:55 -0700954
Keir Mierle8ebb0732012-04-30 23:09:08 -0700955} // namespace internal
956} // namespace ceres