4#include "linear_solver.h"
5#include "sparse_block_matrix.h"
6#include "sparse_block_matrix_diagonal.h"
7#include "openmp_mutex.h"
19 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
21 using namespace Eigen;
26 template <
class t_solver,
int _PoseDim,
int _LandmarkDim>
29 static const int PoseDim = _PoseDim;
30 static const int LandmarkDim = _LandmarkDim;
31 typedef Eigen::Matrix<g_type, PoseDim, PoseDim> PoseMatrixType;
32 typedef Eigen::Matrix<g_type, LandmarkDim, LandmarkDim> LandmarkMatrixType;
33 typedef Eigen::Matrix<g_type, PoseDim, LandmarkDim> PoseLandmarkMatrixType;
34 typedef Eigen::Matrix<g_type, PoseDim, 1> PoseVectorType;
35 typedef Eigen::Matrix<g_type, LandmarkDim, 1> LandmarkVectorType;
40 typedef t_solver LinearSolverType;
46 template <
class t_type>
49 static const int PoseDim = Eigen::Dynamic;
50 static const int LandmarkDim = Eigen::Dynamic;
51 typedef MatrixX<g_type> PoseMatrixType;
52 typedef MatrixX<g_type> LandmarkMatrixType;
53 typedef MatrixX<g_type> PoseLandmarkMatrixType;
54 typedef VectorX<g_type> PoseVectorType;
55 typedef VectorX<g_type> LandmarkVectorType;
60 typedef t_type LinearSolverType;
65 template <
typename Traits>
70 static const int PoseDim = Traits::PoseDim;
71 static const int LandmarkDim = Traits::LandmarkDim;
72 typedef typename Traits::PoseMatrixType PoseMatrixType;
73 typedef typename Traits::LandmarkMatrixType LandmarkMatrixType;
74 typedef typename Traits::PoseLandmarkMatrixType PoseLandmarkMatrixType;
75 typedef typename Traits::PoseVectorType PoseVectorType;
76 typedef typename Traits::LandmarkVectorType LandmarkVectorType;
78 typedef typename Traits::PoseHessianType PoseHessianType;
79 typedef typename Traits::LandmarkHessianType LandmarkHessianType;
80 typedef typename Traits::PoseLandmarkHessianType PoseLandmarkHessianType;
81 typedef typename Traits::LinearSolverType LinearSolverType;
111 bool init(
bool online =
false);
128 v->clearQuadraticForm();
141 for (
uint04 i = 0; i < size; ++i)
148 for (
uint04 i = 0; i <
solver.optimizer.indexMapping().size(); ++i)
154 lib_assert(
_b.size() > iBase,
"Bad size");
175 for (
uint04 idx = 0; idx <
_Hll.blockCols().size(); ++idx)
177 auto& marginalizeColumn =
_Hll.blockCols()[idx];
179 assert(marginalizeColumn.size() == 1 &&
"more than one block in _Hll column");
182 const LandmarkMatrixType& D = marginalizeColumn.begin()->second;
185 assert(D.rows() == D.cols() &&
"Error in landmark matrix");
186 LandmarkMatrixType& Dinv =
_DInvSchur.diagonal()[idx];
190 LandmarkVectorType db(D.rows());
191 for (
uint04 j = 0; j < D.rows(); ++j)
192 db[j] =
_b[
_Hll.rowBaseOfBlock(idx) + _sizePoses + j];
195 const auto& landmarkColumn =
_HplCCS.columns()[idx];
197 for (
auto it_outer = landmarkColumn.begin(); it_outer != landmarkColumn.end(); ++it_outer)
199 int i1 = it_outer->row;
201 const PoseLandmarkMatrixType& Bi = it_outer->block;
203 PoseLandmarkMatrixType BDinv = Bi * Dinv;
204 assert(
_HplCCS.rowBaseOfBlock(i1) <
cast<int>(_sizePoses) &&
"Index out of bounds");
209 Bb.noalias() += Bi * db;
212 assert(i1 >= 0 && i1 <
static_cast<int>(
_HschurTransposedCCS.columns().size()) &&
"Index out of bounds");
216 auto it_inner = std::lower_bound(landmarkColumn.begin(), landmarkColumn.end(), aux);
217 for (; it_inner != landmarkColumn.end(); ++it_inner)
219 int i2 = it_inner->row;
220 const PoseLandmarkMatrixType& Bj = it_inner->block;
221 while (targetColumnIt->row < i2 )
223 assert(targetColumnIt !=
_HschurTransposedCCS.columns()[i1].end() && targetColumnIt->row == i2 &&
"invalid iterator, something wrong with the matrix structure");
224 PoseMatrixType& Hi1i2 = targetColumnIt->block;
225 Hi1i2.noalias() -= BDinv * Bj.transpose();
232 memcpy(
_bschur,
_b, _sizePoses *
sizeof(g_type));
236 for (
uint04 i = 0; i < _sizePoses; ++i)
248 g_type* xp =
_x.begin();
250 lib_assert(
_x.size() > _sizePoses,
"Bad size");
251 g_type* xl =
_x.begin(_sizePoses);
253 g_type* bl =
_b.begin(_sizePoses);
256 for (
uint04 i = 0; i < _sizePoses; ++i)
292 void multiplyHessian(g_type* dest,
const g_type* src)
const {
_Hpp.multiplySymmetricUpperTriangle(dest, src); }
321 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
326 template<
class t_type>
338#include "block_solver.hpp"
Convenience typedefs for commonly used BlockSolver configurations.
BlockSolver< BlockSolverTraits< t_type, 7, 3 > > BlockSolver_7_3
Solver for BA with scale (7-DoF poses, 3D landmarks).
BlockSolver< BlockSolverTraits< t_type, 3, 2 > > BlockSolver_3_2
Solver for 3-DoF poses and 2D landmarks.
BlockSolver< BlockSolverTraits< t_type, 6, 3 > > BlockSolver_6_3
Solver for BA / 3D SLAM (6-DoF poses, 3D landmarks).
BlockSolver< BlockSolverTraits< t_type, Eigen::Dynamic, Eigen::Dynamic > > BlockSolverX
Variable-size solver.
Implementation of a solver operating on the blocks of the Hessian.
const SparseOptimizer & optimizer() const
Returns a const reference to the underlying optimizer.
void resize(IndexScratch &scratch, int totalDim)
Resizes all internal matrices and vectors for the given problem size.
SparseBlockMatrixCCS< PoseLandmarkMatrixType > _HplCCS
Buffer< g_type > _coefficients
SparseBlockMatrix< PoseMatrixType > _Hpp
SparseBlockMatrix< LandmarkMatrixType > _Hll
void multiplyHessian(g_type *dest, const g_type *src) const
Multiplies the pose Hessian by a vector: dest = Hpp * src.
bool buildSystem()
Builds the linear system (Hessian blocks and gradient) from the current graph.
SparseBlockMatrixCCS< PoseMatrixType > _HschurTransposedCCS
void setSchur(bool s)
Enables or disables the Schur complement.
bool buildStructure(bool zeroBlocks, IndexScratch &scratch)
Builds the sparse block matrix structure from the graph.
void clear()
Clears all solver state, resetting sizes to zero.
bool schur()
Returns whether the Schur complement is currently enabled.
SparseBlockMatrix< PoseLandmarkMatrixType > _Hpl
bool updateStructure(const Buffer< HyperGraph::HGVertex * > &vset, const Set< HyperGraph::HGVertex * > &edges)
Updates the structure after vertices or edges have changed.
SparseBlockMatrix< PoseMatrixType > _Hschur
bool solve()
Solves the linear system, optionally using the Schur complement.
bool supportsSchur()
Returns true; this solver supports the Schur complement.
Buffer< PoseVectorType > _diagonalBackupPose
bool setLambda(g_type lambda, bool backup=false)
Adds a damping factor to the diagonal of the Hessian.
SparseOptimizer & optimizer()
Returns a mutable reference to the underlying optimizer.
void restoreDiagonal()
Restores the Hessian diagonal from the backup.
BlockSolver()
Default constructor.
bool init(bool online=false)
Initializes the block solver.
SparseBlockMatrixDiagonal< LandmarkMatrixType > _DInvSchur
Buffer< LandmarkVectorType > _diagonalBackupLandmark
The equivelent of std::vector but with a bit more control.
provide memory workspace for computing the Jacobians
Base edge class for the optimizable graph, adding error computation and robust kernels.
virtual void linearizeOplusAndConstructQuadraticForm(JacobianWorkspace &jacobianWorkspace)=0
Linearizes the constraint in the edge in the manifold space, and store the result in the given worksp...
A general case Vertex for optimization.
virtual sint04 copyB(g_type *b_) const =0
copies the b vector in the array b_
bool marginalized() const
true => this node is marginalized out during the optimization
int colInHessian() const
get the row of this vertex in the Hessian
Container that stores unique elements in no particular order, and which allow for fast retrieval or i...
Solver()
Default constructor.
Buffer< g_type > _b
The right-hand side vector.
Buffer< g_type > _x
The solution vector.
Sparse matrix which uses blocks.
Sparse matrix which uses blocks on the diagonal.
Sparse matrix which uses blocks.
Sparse optimizer that manages active vertices and edges for graph-based optimization.
The primary namespace for the NDEVR SDK.
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
traits to summarize the properties of the fixed size optimization problem
Scratch buffers for block index assignments during structure building.
Buffer< int > block_pose_indices
Block indices for pose vertices.
Buffer< int > block_landmark_indices
Block indices for landmark vertices.