NDEVR
API Documentation
linear_solver_dense.h
1// g2o - General Graph Optimization
2// Copyright (C) 2011 H. Strasdat
3// Copyright (C) 2012 R. Kümmerle
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10// * Redistributions of source code must retain the above copyright notice,
11// this list of conditions and the following disclaimer.
12// * Redistributions in binary form must reproduce the above copyright
13// notice, this list of conditions and the following disclaimer in the
14// documentation and/or other materials provided with the distribution.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
17// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
19// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef G2O_LINEAR_SOLVER_DENSE_H
29#define G2O_LINEAR_SOLVER_DENSE_H
30
31#include "linear_solver.h"
32
33#include <Eigen/Core>
34#include <Eigen/Cholesky>
35
36
37namespace NDEVR {
38
42 template <typename MatrixType>
44 {
45 public:
49 : _reset(true)
50 {
51 }
52
54 {
55 }
56
59 bool init()
60 {
61 _reset = true;
62 return true;
63 }
64
70 bool solve(const SparseBlockMatrix<MatrixType>& A, g_type* x, g_type* b)
71 {
72 int n = A.cols();
73 int m = A.cols();
74 //if (_H.array().isNaN().any())
75 // throw "bad H";
76 Eigen::MatrixX<g_type>& H = _H;
77 if (H.cols() != n) {
78 H.resize(n, m);
79 _reset = true;
80 }
81 if (_reset) {
82 _reset = false;
83 H.setZero();
84 }
85
86 // copy the sparse block matrix into a dense matrix
87 int c_idx = 0;
88 for (uint04 i = 0; i < A.blockCols().size(); ++i)
89 {
90 int c_size = A.colsOfBlock(i);
91 assert(c_idx == A.colBaseOfBlock(i) && "mismatch in block indices");
92
93 const typename SparseBlockMatrix<MatrixType>::IntBlockMap& col = A.blockCols()[i];
94 if (col.size() > 0) {
95 typename SparseBlockMatrix<MatrixType>::IntBlockMap::const_iterator it;
96 for (it = col.begin(); it != col.end(); ++it) {
97 int r_idx = A.rowBaseOfBlock(it->first);
98 // only the upper triangular block is processed
99 if (it->first <= (int)i) {
100 int r_size = A.rowsOfBlock(it->first);
101 //if (it->second.array().isNaN().any())
102 // throw "bad a";
103 H.block(r_idx, c_idx, r_size, c_size) = it->second;
104 if (r_idx != c_idx) // write the lower triangular block
105 {
106 // if (it->second.transpose().array().isNaN().any())
107 // throw "bad a";
108 H.block(c_idx, r_idx, c_size, r_size) = it->second.transpose();
109 }
110 }
111 }
112 }
113
114 c_idx += c_size;
115 }
116
117 // solving via Cholesky decomposition
118 Eigen::VectorX<g_type>::MapType xvec(x, m);
119 Eigen::VectorX<g_type>::ConstMapType bvec(b, n);
120 //if (H.array().isNaN().any())
121 // throw "bad a";
122 _cholesky.compute(H);
123 //if (H.array().isNaN().any())
124 // throw "bad H";
125 if (_cholesky.isPositive()) {
126 //if (bvec.array().isNaN().any())
127 // throw "bad B";
128 xvec = _cholesky.solve(bvec);
129 // if (xvec.array().isNaN().any())
130 // throw "bad xvec";
131 return true;
132 }
133 return false;
134 }
135
136 protected:
137 bool _reset;
138 Eigen::MatrixX<g_type> _H;
139 Eigen::LDLT<Eigen::MatrixX<g_type>> _cholesky;
140 public:
141 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
142
143 };
144
145
146}// end namespace
147
148#endif
linear solver using dense cholesky decomposition
bool _reset
Whether the dense matrix needs to be re-zeroed.
bool init()
Resets the solver state for a new factorization.
Eigen::MatrixX< g_type > _H
Dense representation of the system matrix.
bool solve(const SparseBlockMatrix< MatrixType > &A, g_type *x, g_type *b)
Solves the linear system Ax = b using dense Cholesky decomposition.
LinearSolverDense()
Default constructor.
Eigen::LDLT< Eigen::MatrixX< g_type > > _cholesky
LDLT Cholesky decomposition.
SparseOptimizer optimizer
The sparse optimizer instance.
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...