Home | History | Annotate | Download | only in ceres
      1 // 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 (at) 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/problem.h"
     49 #include "ceres/types.h"
     50 
     51 namespace ceres {
     52 
     53 class CostFunction;
     54 class LossFunction;
     55 class LocalParameterization;
     56 
     57 namespace internal {
     58 
     59 class Program;
     60 class ResidualBlock;
     61 
     62 class ProblemImpl {
     63  public:
     64   typedef map<double*, ParameterBlock*> ParameterMap;
     65 
     66   ProblemImpl();
     67   explicit ProblemImpl(const Problem::Options& options);
     68 
     69   ~ProblemImpl();
     70 
     71   // See the public problem.h file for description of these methods.
     72   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
     73                                    LossFunction* loss_function,
     74                                    const vector<double*>& parameter_blocks);
     75   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
     76                                    LossFunction* loss_function,
     77                                    double* x0);
     78   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
     79                                    LossFunction* loss_function,
     80                                    double* x0, double* x1);
     81   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
     82                                    LossFunction* loss_function,
     83                                    double* x0, double* x1, double* x2);
     84   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
     85                                    LossFunction* loss_function,
     86                                    double* x0, double* x1, double* x2,
     87                                    double* x3);
     88   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
     89                                    LossFunction* loss_function,
     90                                    double* x0, double* x1, double* x2,
     91                                    double* x3, double* x4);
     92   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
     93                                    LossFunction* loss_function,
     94                                    double* x0, double* x1, double* x2,
     95                                    double* x3, double* x4, double* x5);
     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                                    double* x6);
    101   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
    102                                    LossFunction* loss_function,
    103                                    double* x0, double* x1, double* x2,
    104                                    double* x3, double* x4, double* x5,
    105                                    double* x6, double* x7);
    106   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
    107                                    LossFunction* loss_function,
    108                                    double* x0, double* x1, double* x2,
    109                                    double* x3, double* x4, double* x5,
    110                                    double* x6, double* x7, double* x8);
    111   ResidualBlockId AddResidualBlock(CostFunction* cost_function,
    112                                    LossFunction* loss_function,
    113                                    double* x0, double* x1, double* x2,
    114                                    double* x3, double* x4, double* x5,
    115                                    double* x6, double* x7, double* x8,
    116                                    double* x9);
    117   void AddParameterBlock(double* values, int size);
    118   void AddParameterBlock(double* values,
    119                          int size,
    120                          LocalParameterization* local_parameterization);
    121   void SetParameterBlockConstant(double* values);
    122   void SetParameterBlockVariable(double* values);
    123   void SetParameterization(double* values,
    124                            LocalParameterization* local_parameterization);
    125   int NumParameterBlocks() const;
    126   int NumParameters() const;
    127   int NumResidualBlocks() const;
    128   int NumResiduals() const;
    129 
    130   const Program& program() const { return *program_; }
    131   Program* mutable_program() { return program_.get(); }
    132 
    133   const ParameterMap& parameter_map() const { return parameter_block_map_; }
    134 
    135  private:
    136   const Problem::Options options_;
    137 
    138   // The mapping from user pointers to parameter blocks.
    139   map<double*, ParameterBlock*> parameter_block_map_;
    140 
    141   internal::scoped_ptr<internal::Program> program_;
    142   CERES_DISALLOW_COPY_AND_ASSIGN(ProblemImpl);
    143 };
    144 
    145 }  // namespace internal
    146 }  // namespace ceres
    147 
    148 #endif  // CERES_PUBLIC_PROBLEM_IMPL_H_
    149