blob: 7b5547bd95af88e576322c54c22c0b6db2d4303a [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// 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: keir@google.com (Keir Mierle)
30//
31// This is the implementation of the public Problem API. The pointer to
32// implementation (PIMPL) idiom makes it possible for Ceres internal code to
33// refer to the private data members without needing to exposing it to the
34// world. An alternative to PIMPL is to have a factory which returns instances
35// of a virtual base class; while that approach would work, it requires clients
36// to always put a Problem object into a scoped pointer; this needlessly muddies
37// client code for little benefit. Therefore, the PIMPL comprise was chosen.
38
39#ifndef CERES_PUBLIC_PROBLEM_IMPL_H_
40#define CERES_PUBLIC_PROBLEM_IMPL_H_
41
42#include <map>
43#include <vector>
44
45#include "ceres/internal/macros.h"
46#include "ceres/internal/port.h"
47#include "ceres/internal/scoped_ptr.h"
Alex Stewart195e4932014-03-26 11:36:11 +000048#include "ceres/collections_port.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070049#include "ceres/problem.h"
50#include "ceres/types.h"
51
52namespace ceres {
53
54class CostFunction;
55class LossFunction;
56class LocalParameterization;
Sameer Agarwal509f68c2013-02-20 01:39:03 -080057struct CRSMatrix;
Keir Mierle8ebb0732012-04-30 23:09:08 -070058
59namespace internal {
60
61class Program;
62class ResidualBlock;
63
64class ProblemImpl {
65 public:
66 typedef map<double*, ParameterBlock*> ParameterMap;
Alex Stewart195e4932014-03-26 11:36:11 +000067 typedef HashSet<ResidualBlock*> ResidualBlockSet;
Keir Mierle8ebb0732012-04-30 23:09:08 -070068
69 ProblemImpl();
70 explicit ProblemImpl(const Problem::Options& options);
71
72 ~ProblemImpl();
73
74 // See the public problem.h file for description of these methods.
75 ResidualBlockId AddResidualBlock(CostFunction* cost_function,
76 LossFunction* loss_function,
77 const vector<double*>& parameter_blocks);
78 ResidualBlockId AddResidualBlock(CostFunction* cost_function,
79 LossFunction* loss_function,
80 double* x0);
81 ResidualBlockId AddResidualBlock(CostFunction* cost_function,
82 LossFunction* loss_function,
83 double* x0, double* x1);
84 ResidualBlockId AddResidualBlock(CostFunction* cost_function,
85 LossFunction* loss_function,
86 double* x0, double* x1, double* x2);
87 ResidualBlockId AddResidualBlock(CostFunction* cost_function,
88 LossFunction* loss_function,
89 double* x0, double* x1, double* x2,
90 double* x3);
91 ResidualBlockId AddResidualBlock(CostFunction* cost_function,
92 LossFunction* loss_function,
93 double* x0, double* x1, double* x2,
94 double* x3, double* x4);
95 ResidualBlockId AddResidualBlock(CostFunction* cost_function,
96 LossFunction* loss_function,
97 double* x0, double* x1, double* x2,
98 double* x3, double* x4, double* x5);
Fisher12626e82012-10-21 14:12:04 -040099 ResidualBlockId AddResidualBlock(CostFunction* cost_function,
100 LossFunction* loss_function,
101 double* x0, double* x1, double* x2,
102 double* x3, double* x4, double* x5,
103 double* x6);
104 ResidualBlockId AddResidualBlock(CostFunction* cost_function,
105 LossFunction* loss_function,
106 double* x0, double* x1, double* x2,
107 double* x3, double* x4, double* x5,
108 double* x6, double* x7);
109 ResidualBlockId AddResidualBlock(CostFunction* cost_function,
110 LossFunction* loss_function,
111 double* x0, double* x1, double* x2,
112 double* x3, double* x4, double* x5,
113 double* x6, double* x7, double* x8);
114 ResidualBlockId AddResidualBlock(CostFunction* cost_function,
115 LossFunction* loss_function,
116 double* x0, double* x1, double* x2,
117 double* x3, double* x4, double* x5,
118 double* x6, double* x7, double* x8,
119 double* x9);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700120 void AddParameterBlock(double* values, int size);
121 void AddParameterBlock(double* values,
122 int size,
123 LocalParameterization* local_parameterization);
Keir Mierle04938ef2013-02-17 12:37:55 -0800124
125 void RemoveResidualBlock(ResidualBlock* residual_block);
126 void RemoveParameterBlock(double* values);
127
Keir Mierle8ebb0732012-04-30 23:09:08 -0700128 void SetParameterBlockConstant(double* values);
129 void SetParameterBlockVariable(double* values);
130 void SetParameterization(double* values,
131 LocalParameterization* local_parameterization);
Sameer Agarwalf949bab2014-02-18 10:11:02 -0800132 const LocalParameterization* GetParameterization(double* values) const;
Sameer Agarwal509f68c2013-02-20 01:39:03 -0800133
Sameer Agarwala482ab82014-02-18 22:24:03 -0800134 void SetParameterLowerBound(double* values, int index, double lower_bound);
135 void SetParameterUpperBound(double* values, int index, double upper_bound);
136
Sameer Agarwal509f68c2013-02-20 01:39:03 -0800137 bool Evaluate(const Problem::EvaluateOptions& options,
138 double* cost,
139 vector<double>* residuals,
140 vector<double>* gradient,
141 CRSMatrix* jacobian);
142
Keir Mierle8ebb0732012-04-30 23:09:08 -0700143 int NumParameterBlocks() const;
144 int NumParameters() const;
145 int NumResidualBlocks() const;
146 int NumResiduals() const;
147
Sameer Agarwal02706c12013-05-12 22:07:55 -0700148 int ParameterBlockSize(const double* parameter_block) const;
149 int ParameterBlockLocalSize(const double* parameter_block) const;
Sameer Agarwal5ecb1c32014-04-01 09:20:35 -0700150
151 bool HasParameterBlock(const double* parameter_block) const;
152
Sameer Agarwal3d954692013-04-18 14:54:55 -0700153 void GetParameterBlocks(vector<double*>* parameter_blocks) const;
Keir Mierlefda69b52013-10-10 00:25:24 -0700154 void GetResidualBlocks(vector<ResidualBlockId>* residual_blocks) const;
155
156 void GetParameterBlocksForResidualBlock(
157 const ResidualBlockId residual_block,
158 vector<double*>* parameter_blocks) const;
159
160 void GetResidualBlocksForParameterBlock(
161 const double* values,
162 vector<ResidualBlockId>* residual_blocks) const;
Sameer Agarwal3d954692013-04-18 14:54:55 -0700163
Keir Mierle8ebb0732012-04-30 23:09:08 -0700164 const Program& program() const { return *program_; }
165 Program* mutable_program() { return program_.get(); }
166
167 const ParameterMap& parameter_map() const { return parameter_block_map_; }
Alex Stewart195e4932014-03-26 11:36:11 +0000168 const ResidualBlockSet& residual_block_set() const {
169 CHECK(options_.enable_fast_removal)
170 << "Fast removal not enabled, residual_block_set is not maintained.";
171 return residual_block_set_;
172 }
Keir Mierle8ebb0732012-04-30 23:09:08 -0700173
174 private:
Sameer Agarwal8e1f83c2013-02-15 08:35:40 -0800175 ParameterBlock* InternalAddParameterBlock(double* values, int size);
Alex Stewart195e4932014-03-26 11:36:11 +0000176 void InternalRemoveResidualBlock(ResidualBlock* residual_block);
Sameer Agarwal8e1f83c2013-02-15 08:35:40 -0800177
Sameer Agarwal509f68c2013-02-20 01:39:03 -0800178 bool InternalEvaluate(Program* program,
179 double* cost,
180 vector<double>* residuals,
181 vector<double>* gradient,
182 CRSMatrix* jacobian);
183
Keir Mierle04938ef2013-02-17 12:37:55 -0800184 // Delete the arguments in question. These differ from the Remove* functions
185 // in that they do not clean up references to the block to delete; they
186 // merely delete them.
187 template<typename Block>
188 void DeleteBlockInVector(vector<Block*>* mutable_blocks,
189 Block* block_to_remove);
190 void DeleteBlock(ResidualBlock* residual_block);
191 void DeleteBlock(ParameterBlock* parameter_block);
192
Keir Mierle8ebb0732012-04-30 23:09:08 -0700193 const Problem::Options options_;
194
195 // The mapping from user pointers to parameter blocks.
196 map<double*, ParameterBlock*> parameter_block_map_;
197
Alex Stewart195e4932014-03-26 11:36:11 +0000198 // Iff enable_fast_removal is enabled, contains the current residual blocks.
199 ResidualBlockSet residual_block_set_;
200
Keir Mierle04938ef2013-02-17 12:37:55 -0800201 // The actual parameter and residual blocks.
Keir Mierle8ebb0732012-04-30 23:09:08 -0700202 internal::scoped_ptr<internal::Program> program_;
Keir Mierle04938ef2013-02-17 12:37:55 -0800203
204 // When removing residual and parameter blocks, cost/loss functions and
205 // parameterizations have ambiguous ownership. Instead of scanning the entire
206 // problem to see if the cost/loss/parameterization is shared with other
207 // residual or parameter blocks, buffer them until destruction.
208 //
209 // TODO(keir): See if it makes sense to use sets instead.
210 vector<CostFunction*> cost_functions_to_delete_;
211 vector<LossFunction*> loss_functions_to_delete_;
212 vector<LocalParameterization*> local_parameterizations_to_delete_;
213
Sameer Agarwal237d6592012-05-30 20:34:49 -0700214 CERES_DISALLOW_COPY_AND_ASSIGN(ProblemImpl);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700215};
216
217} // namespace internal
218} // namespace ceres
219
220#endif // CERES_PUBLIC_PROBLEM_IMPL_H_