f42bde6c7936435f4ab3f4191b94a2ccc301220e
[blender.git] / extern / ceres / internal / ceres / problem_impl.h
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: 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"
48 #include "ceres/collections_port.h"
49 #include "ceres/problem.h"
50 #include "ceres/types.h"
51
52 namespace ceres {
53
54 class CostFunction;
55 class LossFunction;
56 class LocalParameterization;
57 struct CRSMatrix;
58
59 namespace internal {
60
61 class Program;
62 class ResidualBlock;
63
64 class ProblemImpl {
65  public:
66   typedef std::map<double*, ParameterBlock*> ParameterMap;
67   typedef HashSet<ResidualBlock*> ResidualBlockSet;
68
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(
76       CostFunction* cost_function,
77       LossFunction* loss_function,
78       const std::vector<double*>& parameter_blocks);
79   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
80                                    LossFunction* loss_function,
81                                    double* x0);
82   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
83                                    LossFunction* loss_function,
84                                    double* x0, double* x1);
85   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
86                                    LossFunction* loss_function,
87                                    double* x0, double* x1, double* x2);
88   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
89                                    LossFunction* loss_function,
90                                    double* x0, double* x1, double* x2,
91                                    double* x3);
92   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
93                                    LossFunction* loss_function,
94                                    double* x0, double* x1, double* x2,
95                                    double* x3, double* x4);
96   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
97                                    LossFunction* loss_function,
98                                    double* x0, double* x1, double* x2,
99                                    double* x3, double* x4, double* x5);
100   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
101                                    LossFunction* loss_function,
102                                    double* x0, double* x1, double* x2,
103                                    double* x3, double* x4, double* x5,
104                                    double* x6);
105   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
106                                    LossFunction* loss_function,
107                                    double* x0, double* x1, double* x2,
108                                    double* x3, double* x4, double* x5,
109                                    double* x6, double* x7);
110   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
111                                    LossFunction* loss_function,
112                                    double* x0, double* x1, double* x2,
113                                    double* x3, double* x4, double* x5,
114                                    double* x6, double* x7, double* x8);
115   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
116                                    LossFunction* loss_function,
117                                    double* x0, double* x1, double* x2,
118                                    double* x3, double* x4, double* x5,
119                                    double* x6, double* x7, double* x8,
120                                    double* x9);
121   void AddParameterBlock(double* values, int size);
122   void AddParameterBlock(double* values,
123                          int size,
124                          LocalParameterization* local_parameterization);
125
126   void RemoveResidualBlock(ResidualBlock* residual_block);
127   void RemoveParameterBlock(double* values);
128
129   void SetParameterBlockConstant(double* values);
130   void SetParameterBlockVariable(double* values);
131   void SetParameterization(double* values,
132                            LocalParameterization* local_parameterization);
133   const LocalParameterization* GetParameterization(double* values) const;
134
135   void SetParameterLowerBound(double* values, int index, double lower_bound);
136   void SetParameterUpperBound(double* values, int index, double upper_bound);
137
138   bool Evaluate(const Problem::EvaluateOptions& options,
139                 double* cost,
140                 std::vector<double>* residuals,
141                 std::vector<double>* gradient,
142                 CRSMatrix* jacobian);
143
144   int NumParameterBlocks() const;
145   int NumParameters() const;
146   int NumResidualBlocks() const;
147   int NumResiduals() const;
148
149   int ParameterBlockSize(const double* parameter_block) const;
150   int ParameterBlockLocalSize(const double* parameter_block) const;
151
152   bool HasParameterBlock(const double* parameter_block) const;
153
154   void GetParameterBlocks(std::vector<double*>* parameter_blocks) const;
155   void GetResidualBlocks(std::vector<ResidualBlockId>* residual_blocks) const;
156
157   void GetParameterBlocksForResidualBlock(
158       const ResidualBlockId residual_block,
159       std::vector<double*>* parameter_blocks) const;
160
161   const CostFunction* GetCostFunctionForResidualBlock(
162       const ResidualBlockId residual_block) const;
163   const LossFunction* GetLossFunctionForResidualBlock(
164       const ResidualBlockId residual_block) const;
165
166   void GetResidualBlocksForParameterBlock(
167       const double* values,
168       std::vector<ResidualBlockId>* residual_blocks) const;
169
170   const Program& program() const { return *program_; }
171   Program* mutable_program() { return program_.get(); }
172
173   const ParameterMap& parameter_map() const { return parameter_block_map_; }
174   const ResidualBlockSet& residual_block_set() const {
175     CHECK(options_.enable_fast_removal)
176         << "Fast removal not enabled, residual_block_set is not maintained.";
177     return residual_block_set_;
178   }
179
180  private:
181   ParameterBlock* InternalAddParameterBlock(double* values, int size);
182   void InternalRemoveResidualBlock(ResidualBlock* residual_block);
183
184   bool InternalEvaluate(Program* program,
185                         double* cost,
186                         std::vector<double>* residuals,
187                         std::vector<double>* gradient,
188                         CRSMatrix* jacobian);
189
190   // Delete the arguments in question. These differ from the Remove* functions
191   // in that they do not clean up references to the block to delete; they
192   // merely delete them.
193   template<typename Block>
194   void DeleteBlockInVector(std::vector<Block*>* mutable_blocks,
195                            Block* block_to_remove);
196   void DeleteBlock(ResidualBlock* residual_block);
197   void DeleteBlock(ParameterBlock* parameter_block);
198
199   const Problem::Options options_;
200
201   // The mapping from user pointers to parameter blocks.
202   std::map<double*, ParameterBlock*> parameter_block_map_;
203
204   // Iff enable_fast_removal is enabled, contains the current residual blocks.
205   ResidualBlockSet residual_block_set_;
206
207   // The actual parameter and residual blocks.
208   internal::scoped_ptr<internal::Program> program_;
209
210   // When removing residual and parameter blocks, cost/loss functions and
211   // parameterizations have ambiguous ownership. Instead of scanning the entire
212   // problem to see if the cost/loss/parameterization is shared with other
213   // residual or parameter blocks, buffer them until destruction.
214   //
215   // TODO(keir): See if it makes sense to use sets instead.
216   std::vector<CostFunction*> cost_functions_to_delete_;
217   std::vector<LossFunction*> loss_functions_to_delete_;
218   std::vector<LocalParameterization*> local_parameterizations_to_delete_;
219
220   CERES_DISALLOW_COPY_AND_ASSIGN(ProblemImpl);
221 };
222
223 }  // namespace internal
224 }  // namespace ceres
225
226 #endif  // CERES_PUBLIC_PROBLEM_IMPL_H_