g2o
Loading...
Searching...
No Matches
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
30#include <Eigen/Core>
31#include <memory>
32
34#include "g2o/config.h"
35#include "linear_solver.h"
36#include "openmp_mutex.h"
37#include "solver.h"
38#include "sparse_block_matrix.h"
40
41namespace g2o {
42
47template <int _PoseDim, int _LandmarkDim>
49 static const int PoseDim = _PoseDim;
50 static const int LandmarkDim = _LandmarkDim;
51 typedef Eigen::Matrix<double, PoseDim, PoseDim, Eigen::ColMajor>
53 typedef Eigen::Matrix<double, LandmarkDim, LandmarkDim, Eigen::ColMajor>
55 typedef Eigen::Matrix<double, PoseDim, LandmarkDim, Eigen::ColMajor>
57 typedef Eigen::Matrix<double, PoseDim, 1, Eigen::ColMajor> PoseVectorType;
58 typedef Eigen::Matrix<double, LandmarkDim, 1, Eigen::ColMajor>
60
65};
66
71template <>
86
90class BlockSolverBase : public Solver {
91 public:
92 virtual ~BlockSolverBase() {}
96 virtual void multiplyHessian(double* dest, const double* src) const = 0;
97};
98
102template <typename Traits>
104 public:
105 static const int PoseDim = Traits::PoseDim;
106 static const int LandmarkDim = Traits::LandmarkDim;
107 typedef typename Traits::PoseMatrixType PoseMatrixType;
108 typedef typename Traits::LandmarkMatrixType LandmarkMatrixType;
109 typedef typename Traits::PoseLandmarkMatrixType PoseLandmarkMatrixType;
110 typedef typename Traits::PoseVectorType PoseVectorType;
111 typedef typename Traits::LandmarkVectorType LandmarkVectorType;
112
113 typedef typename Traits::PoseHessianType PoseHessianType;
114 typedef typename Traits::LandmarkHessianType LandmarkHessianType;
115 typedef typename Traits::PoseLandmarkHessianType PoseLandmarkHessianType;
116 typedef typename Traits::LinearSolverType LinearSolverType;
117
118 public:
124 BlockSolver(std::unique_ptr<LinearSolverType> linearSolver);
125 ~BlockSolver();
126
127 virtual bool init(SparseOptimizer* optmizer, bool online = false);
128 virtual bool buildStructure(bool zeroBlocks = false);
129 virtual bool updateStructure(const std::vector<HyperGraph::Vertex*>& vset,
130 const HyperGraph::EdgeSet& edges);
131 virtual bool buildSystem();
132 virtual bool solve();
133 virtual bool computeMarginals(
135 const std::vector<std::pair<int, int>>& blockIndices);
136 virtual bool setLambda(double lambda, bool backup = false);
137 virtual void restoreDiagonal();
138 virtual bool supportsSchur() { return true; }
139 virtual bool schur() { return _doSchur; }
140 virtual void setSchur(bool s) { _doSchur = s; }
141
143
144 virtual void setWriteDebug(bool writeDebug);
145 virtual bool writeDebug() const { return _linearSolver->writeDebug(); }
146
147 virtual bool saveHessian(const std::string& fileName) const;
148
149 virtual void multiplyHessian(double* dest, const double* src) const {
150 _Hpp->multiplySymmetricUpperTriangle(dest, src);
151 }
152
153 protected:
154 void resize(int* blockPoseIndices, int numPoseBlocks,
155 int* blockLandmarkIndices, int numLandmarkBlocks, int totalDim);
156
157 void deallocate();
158
159 std::unique_ptr<SparseBlockMatrix<PoseMatrixType>> _Hpp;
160 std::unique_ptr<SparseBlockMatrix<LandmarkMatrixType>> _Hll;
161 std::unique_ptr<SparseBlockMatrix<PoseLandmarkMatrixType>> _Hpl;
162
163 std::unique_ptr<SparseBlockMatrix<PoseMatrixType>> _Hschur;
164 std::unique_ptr<SparseBlockMatrixDiagonal<LandmarkMatrixType>> _DInvSchur;
165
166 std::unique_ptr<SparseBlockMatrixCCS<PoseLandmarkMatrixType>> _HplCCS;
167 std::unique_ptr<SparseBlockMatrixCCS<PoseMatrixType>> _HschurTransposedCCS;
168
169 std::unique_ptr<LinearSolverType> _linearSolver;
170
171 std::vector<PoseVectorType> _diagonalBackupPose;
172 std::vector<LandmarkVectorType> _diagonalBackupLandmark;
173
174#ifdef G2O_OPENMP
175 std::vector<OpenMPMutex> _coefficientsMutex;
176#endif
177
179
180 std::unique_ptr<double[], aligned_deleter<double>> _coefficients;
181 std::unique_ptr<double[], aligned_deleter<double>> _bschur;
182
185};
186
187template <int p, int l>
189
190// variable size solver
192
193// solver for BA/3D SLAM
195
196// solver fo BA with scale
198
199// 2Dof landmarks 3Dof poses
201
202} // namespace g2o
203
204#include "block_solver.hpp"
205
206#endif
base for the block solvers with some basic function interfaces
virtual void multiplyHessian(double *dest, const double *src) const =0
virtual ~BlockSolverBase()
Implementation of a solver operating on the blocks of the Hessian.
void resize(int *blockPoseIndices, int numPoseBlocks, int *blockLandmarkIndices, int numLandmarkBlocks, int totalDim)
std::unique_ptr< SparseBlockMatrix< PoseMatrixType > > _Hschur
virtual bool computeMarginals(SparseBlockMatrix< MatrixX > &spinv, const std::vector< std::pair< int, int > > &blockIndices)
Traits::PoseHessianType PoseHessianType
virtual bool buildStructure(bool zeroBlocks=false)
Traits::LandmarkVectorType LandmarkVectorType
virtual void setWriteDebug(bool writeDebug)
virtual void restoreDiagonal()
std::unique_ptr< SparseBlockMatrixCCS< PoseLandmarkMatrixType > > _HplCCS
std::unique_ptr< LinearSolverType > _linearSolver
virtual bool buildSystem()
static const int LandmarkDim
virtual bool schur()
should the solver perform the schur complement or not
Traits::LandmarkHessianType LandmarkHessianType
virtual bool saveHessian(const std::string &fileName) const
write the hessian to disk using the specified file name
virtual bool solve()
Traits::PoseVectorType PoseVectorType
virtual bool supportsSchur()
virtual void setSchur(bool s)
virtual void multiplyHessian(double *dest, const double *src) const
Traits::LinearSolverType LinearSolverType
std::unique_ptr< SparseBlockMatrix< LandmarkMatrixType > > _Hll
std::unique_ptr< SparseBlockMatrix< PoseMatrixType > > _Hpp
virtual bool init(SparseOptimizer *optmizer, bool online=false)
Traits::PoseMatrixType PoseMatrixType
Traits::PoseLandmarkMatrixType PoseLandmarkMatrixType
static const int PoseDim
LinearSolver< PoseMatrixType > & linearSolver() const
virtual bool writeDebug() const
std::unique_ptr< SparseBlockMatrixDiagonal< LandmarkMatrixType > > _DInvSchur
std::unique_ptr< SparseBlockMatrixCCS< PoseMatrixType > > _HschurTransposedCCS
std::unique_ptr< double[], aligned_deleter< double > > _bschur
std::vector< LandmarkVectorType > _diagonalBackupLandmark
virtual bool updateStructure(const std::vector< HyperGraph::Vertex * > &vset, const HyperGraph::EdgeSet &edges)
virtual bool setLambda(double lambda, bool backup=false)
std::vector< PoseVectorType > _diagonalBackupPose
std::unique_ptr< SparseBlockMatrix< PoseLandmarkMatrixType > > _Hpl
std::unique_ptr< double[], aligned_deleter< double > > _coefficients
Traits::PoseLandmarkHessianType PoseLandmarkHessianType
Traits::LandmarkMatrixType LandmarkMatrixType
std::set< Edge * > EdgeSet
basic solver for Ax = b
Generic interface for a sparse solver operating on a graph which solves one iteration of the lineariz...
Definition solver.h:44
Sparse matrix which uses blocks.
Definition jet.h:938
BlockSolverPL< 7, 3 > BlockSolver_7_3
MatrixN< Eigen::Dynamic > MatrixX
Definition eigen_types.h:74
BlockSolverPL< 6, 3 > BlockSolver_6_3
BlockSolverPL< Eigen::Dynamic, Eigen::Dynamic > BlockSolverX
VectorN< Eigen::Dynamic > VectorX
Definition eigen_types.h:55
BlockSolverPL< 3, 2 > BlockSolver_3_2
BlockSolver< BlockSolverTraits< p, l > > BlockSolverPL
SparseBlockMatrix< PoseMatrixType > PoseHessianType
SparseBlockMatrix< LandmarkMatrixType > LandmarkHessianType
SparseBlockMatrix< PoseLandmarkMatrixType > PoseLandmarkHessianType
traits to summarize the properties of the fixed size optimization problem
SparseBlockMatrix< PoseMatrixType > PoseHessianType
Eigen::Matrix< double, LandmarkDim, LandmarkDim, Eigen::ColMajor > LandmarkMatrixType
Eigen::Matrix< double, PoseDim, PoseDim, Eigen::ColMajor > PoseMatrixType
Eigen::Matrix< double, PoseDim, LandmarkDim, Eigen::ColMajor > PoseLandmarkMatrixType
static const int LandmarkDim
static const int PoseDim
LinearSolver< PoseMatrixType > LinearSolverType
Eigen::Matrix< double, LandmarkDim, 1, Eigen::ColMajor > LandmarkVectorType
SparseBlockMatrix< LandmarkMatrixType > LandmarkHessianType
SparseBlockMatrix< PoseLandmarkMatrixType > PoseLandmarkHessianType
Eigen::Matrix< double, PoseDim, 1, Eigen::ColMajor > PoseVectorType