g2o
block_solver.h
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 #ifndef G2O_BLOCK_SOLVER_H
28 #define G2O_BLOCK_SOLVER_H
29 #include <Eigen/Core>
30 #include "solver.h"
31 #include "linear_solver.h"
32 #include "sparse_block_matrix.h"
34 #include "openmp_mutex.h"
35 #include "g2o/config.h"
36 
37 namespace g2o {
38 
42  template <int _PoseDim, int _LandmarkDim>
44  {
45  static const int PoseDim = _PoseDim;
46  static const int LandmarkDim = _LandmarkDim;
47  typedef Eigen::Matrix<double, PoseDim, PoseDim, Eigen::ColMajor> PoseMatrixType;
48  typedef Eigen::Matrix<double, LandmarkDim, LandmarkDim, Eigen::ColMajor> LandmarkMatrixType;
49  typedef Eigen::Matrix<double, PoseDim, LandmarkDim, Eigen::ColMajor> PoseLandmarkMatrixType;
50  typedef Eigen::Matrix<double, PoseDim, 1, Eigen::ColMajor> PoseVectorType;
51  typedef Eigen::Matrix<double, LandmarkDim, 1, Eigen::ColMajor> LandmarkVectorType;
52 
57  };
58 
62  template <>
63  struct BlockSolverTraits<Eigen::Dynamic, Eigen::Dynamic>
64  {
65  static const int PoseDim = Eigen::Dynamic;
66  static const int LandmarkDim = Eigen::Dynamic;
72 
77  };
78 
82  class BlockSolverBase : public Solver
83  {
84  public:
85  virtual ~BlockSolverBase() {}
89  virtual void multiplyHessian(double* dest, const double* src) const = 0;
90  };
91 
95  template <typename Traits>
96  class BlockSolver: public BlockSolverBase {
97  public:
98 
99  static const int PoseDim = Traits::PoseDim;
100  static const int LandmarkDim = Traits::LandmarkDim;
101  typedef typename Traits::PoseMatrixType PoseMatrixType;
102  typedef typename Traits::LandmarkMatrixType LandmarkMatrixType;
103  typedef typename Traits::PoseLandmarkMatrixType PoseLandmarkMatrixType;
104  typedef typename Traits::PoseVectorType PoseVectorType;
105  typedef typename Traits::LandmarkVectorType LandmarkVectorType;
106 
107  typedef typename Traits::PoseHessianType PoseHessianType;
108  typedef typename Traits::LandmarkHessianType LandmarkHessianType;
109  typedef typename Traits::PoseLandmarkHessianType PoseLandmarkHessianType;
110  typedef typename Traits::LinearSolverType LinearSolverType;
111 
112  public:
113 
119  BlockSolver(LinearSolverType* linearSolver);
120  ~BlockSolver();
121 
122  virtual bool init(SparseOptimizer* optmizer, bool online = false);
123  virtual bool buildStructure(bool zeroBlocks = false);
124  virtual bool updateStructure(const std::vector<HyperGraph::Vertex*>& vset, const HyperGraph::EdgeSet& edges);
125  virtual bool buildSystem();
126  virtual bool solve();
127  virtual bool computeMarginals(SparseBlockMatrix<MatrixXD>& spinv, const std::vector<std::pair<int, int> >& blockIndices);
128  virtual bool setLambda(double lambda, bool backup = false);
129  virtual void restoreDiagonal();
130  virtual bool supportsSchur() {return true;}
131  virtual bool schur() { return _doSchur;}
132  virtual void setSchur(bool s) { _doSchur = s;}
133 
134  LinearSolver<PoseMatrixType>* linearSolver() const { return _linearSolver;}
135 
136  virtual void setWriteDebug(bool writeDebug);
137  virtual bool writeDebug() const {return _linearSolver->writeDebug();}
138 
139  virtual bool saveHessian(const std::string& fileName) const;
140 
141  virtual void multiplyHessian(double* dest, const double* src) const { _Hpp->multiplySymmetricUpperTriangle(dest, src);}
142 
143  protected:
144  void resize(int* blockPoseIndices, int numPoseBlocks,
145  int* blockLandmarkIndices, int numLandmarkBlocks, int totalDim);
146 
147  void deallocate();
148 
152 
155 
158 
160 
161  std::vector<PoseVectorType, Eigen::aligned_allocator<PoseVectorType> > _diagonalBackupPose;
162  std::vector<LandmarkVectorType, Eigen::aligned_allocator<LandmarkVectorType> > _diagonalBackupLandmark;
163 
164 # ifdef G2O_OPENMP
165  std::vector<OpenMPMutex> _coefficientsMutex;
166 # endif
167 
168  bool _doSchur;
169 
170  double* _coefficients;
171  double* _bschur;
172 
173  int _numPoses, _numLandmarks;
174  int _sizePoses, _sizeLandmarks;
175  };
176 
177 
178  //variable size solver
180  // solver for BA/3D SLAM
182  // solver fo BA with scale
184  // 2Dof landmarks 3Dof poses
186 
187 } // end namespace
188 
189 #include "block_solver.hpp"
190 
191 
192 #endif
SparseBlockMatrix< PoseMatrixType > * _Hschur
Definition: block_solver.h:153
Traits::PoseHessianType PoseHessianType
Definition: block_solver.h:107
std::vector< LandmarkVectorType, Eigen::aligned_allocator< LandmarkVectorType > > _diagonalBackupLandmark
Definition: block_solver.h:162
Traits::LandmarkMatrixType LandmarkMatrixType
Definition: block_solver.h:102
Eigen::Matrix< double, LandmarkDim, 1, Eigen::ColMajor > LandmarkVectorType
Definition: block_solver.h:51
BlockSolver< BlockSolverTraits< 6, 3 > > BlockSolver_6_3
Definition: block_solver.h:181
Traits::PoseVectorType PoseVectorType
Definition: block_solver.h:104
SparseBlockMatrix< PoseMatrixType > * _Hpp
Definition: block_solver.h:149
virtual bool schur()
should the solver perform the schur complement or not
Definition: block_solver.h:131
Traits::LandmarkHessianType LandmarkHessianType
Definition: block_solver.h:108
virtual void setSchur(bool s)
Definition: block_solver.h:132
static const int PoseDim
Definition: block_solver.h:45
static const int LandmarkDim
Definition: block_solver.h:46
SparseBlockMatrixDiagonal< LandmarkMatrixType > * _DInvSchur
Definition: block_solver.h:154
SparseBlockMatrixCCS< PoseLandmarkMatrixType > * _HplCCS
Definition: block_solver.h:156
traits to summarize the properties of the fixed size optimization problem
Definition: block_solver.h:43
Eigen::Matrix< double, LandmarkDim, LandmarkDim, Eigen::ColMajor > LandmarkMatrixType
Definition: block_solver.h:48
SparseBlockMatrix< LandmarkMatrixType > * _Hll
Definition: block_solver.h:150
SparseBlockMatrix< PoseLandmarkMatrixType > PoseLandmarkHessianType
Definition: block_solver.h:55
Traits::PoseMatrixType PoseMatrixType
Definition: block_solver.h:101
double * _coefficients
Definition: block_solver.h:170
Traits::LinearSolverType LinearSolverType
Definition: block_solver.h:110
SparseBlockMatrix< PoseMatrixType > PoseHessianType
Definition: block_solver.h:53
std::set< Edge * > EdgeSet
Definition: hyper_graph.h:135
BlockSolver< BlockSolverTraits< 3, 2 > > BlockSolver_3_2
Definition: block_solver.h:185
SparseBlockMatrixCCS< PoseMatrixType > * _HschurTransposedCCS
Definition: block_solver.h:157
Traits::PoseLandmarkHessianType PoseLandmarkHessianType
Definition: block_solver.h:109
LinearSolver< PoseMatrixType > * linearSolver() const
Definition: block_solver.h:134
std::vector< PoseVectorType, Eigen::aligned_allocator< PoseVectorType > > _diagonalBackupPose
Definition: block_solver.h:161
Traits::PoseLandmarkMatrixType PoseLandmarkMatrixType
Definition: block_solver.h:103
Generic interface for a sparse solver operating on a graph which solves one iteration of the lineariz...
Definition: solver.h:44
Traits::LandmarkVectorType LandmarkVectorType
Definition: block_solver.h:105
SparseBlockMatrix< PoseLandmarkMatrixType > PoseLandmarkHessianType
Definition: block_solver.h:75
Eigen::Matrix< double, PoseDim, 1, Eigen::ColMajor > PoseVectorType
Definition: block_solver.h:50
virtual bool supportsSchur()
Definition: block_solver.h:130
Eigen::Matrix< double, Eigen::Dynamic, 1, Eigen::ColMajor > VectorXD
Definition: eigen_types.h:48
SparseBlockMatrix< PoseLandmarkMatrixType > * _Hpl
Definition: block_solver.h:151
SparseBlockMatrix< PoseMatrixType > PoseHessianType
Definition: block_solver.h:73
virtual void multiplyHessian(double *dest, const double *src) const
Definition: block_solver.h:141
SparseBlockMatrix< LandmarkMatrixType > LandmarkHessianType
Definition: block_solver.h:54
BlockSolver< BlockSolverTraits< 7, 3 > > BlockSolver_7_3
Definition: block_solver.h:183
LinearSolver< PoseMatrixType > * _linearSolver
Definition: block_solver.h:159
Implementation of a solver operating on the blocks of the Hessian.
Definition: block_solver.h:96
Eigen::Matrix< double, PoseDim, LandmarkDim, Eigen::ColMajor > PoseLandmarkMatrixType
Definition: block_solver.h:49
LinearSolver< PoseMatrixType > LinearSolverType
Definition: block_solver.h:56
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor > MatrixXD
Definition: eigen_types.h:63
SparseBlockMatrix< LandmarkMatrixType > LandmarkHessianType
Definition: block_solver.h:74
Eigen::Matrix< double, PoseDim, PoseDim, Eigen::ColMajor > PoseMatrixType
Definition: block_solver.h:47
virtual ~BlockSolverBase()
Definition: block_solver.h:85
virtual bool writeDebug() const
Definition: block_solver.h:137
base for the block solvers with some basic function interfaces
Definition: block_solver.h:82
BlockSolver< BlockSolverTraits< Eigen::Dynamic, Eigen::Dynamic > > BlockSolverX
Definition: block_solver.h:179