g2o
Loading...
Searching...
No Matches
sparse_block_matrix.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_SPARSE_BLOCK_MATRIX_
28#define G2O_SPARSE_BLOCK_MATRIX_
29
30#include <Eigen/Core>
31#include <cassert>
32#include <fstream>
33#include <iomanip>
34#include <iostream>
35#include <map>
36#include <memory>
37#include <vector>
38
39#include "g2o/config.h"
40#include "g2o/stuff/misc.h"
42#include "matrix_operations.h"
43#include "matrix_structure.h"
45
46namespace g2o {
63template <class MatrixType = MatrixX>
65 public:
67 typedef MatrixType SparseMatrixBlock;
68
70 inline int cols() const {
71 return _colBlockIndices.size() ? _colBlockIndices.back() : 0;
72 }
74 inline int rows() const {
75 return _rowBlockIndices.size() ? _rowBlockIndices.back() : 0;
76 }
77
78 typedef std::map<int, SparseMatrixBlock*> IntBlockMap;
79
94 SparseBlockMatrix(const int* rbi, const int* cbi, int rb, int cb,
95 bool hasStorage = true);
96
98
100
103 void clear(bool dealloc = false);
104
107 SparseMatrixBlock* block(int r, int c, bool alloc = false);
109 const SparseMatrixBlock* block(int r, int c) const;
110
112 inline int rowsOfBlock(int r) const {
113 return r ? _rowBlockIndices[r] - _rowBlockIndices[r - 1]
114 : _rowBlockIndices[0];
115 }
116
118 inline int colsOfBlock(int c) const {
119 return c ? _colBlockIndices[c] - _colBlockIndices[c - 1]
120 : _colBlockIndices[0];
121 }
122
124 inline int rowBaseOfBlock(int r) const {
125 return r ? _rowBlockIndices[r - 1] : 0;
126 }
127
129 inline int colBaseOfBlock(int c) const {
130 return c ? _colBlockIndices[c - 1] : 0;
131 }
132
134 size_t nonZeros() const;
136 size_t nonZeroBlocks() const;
137
139 SparseBlockMatrix* clone() const;
140
149 SparseBlockMatrix* slice(int rmin, int rmax, int cmin, int cmax,
150 bool alloc = true) const;
151
154 template <class MatrixTransposedType>
156
157 template <class MatrixTransposedType>
158 std::unique_ptr<SparseBlockMatrix<MatrixTransposedType>> transposed() const;
159
161 bool add(SparseBlockMatrix<MatrixType>& dest) const;
162 std::unique_ptr<SparseBlockMatrix<MatrixType>> added() const;
163
165 template <class MatrixResultType, class MatrixFactorType>
168
170 void multiply(double*& dest, const double* src) const;
171
177 void multiplySymmetricUpperTriangle(double*& dest, const double* src) const;
178
180 void rightMultiply(double*& dest, const double* src) const;
181
183 void scale(double a);
184
189 bool symmPermutation(SparseBlockMatrix<MatrixType>*& dest, const int* pinv,
190 bool onlyUpper = false) const;
191
195 int fillCCS(int* Cp, int* Ci, double* Cx, bool upperTriangle = false) const;
196
202 int fillCCS(double* Cx, bool upperTriangle = false) const;
203
205 void fillBlockStructure(MatrixStructure& ms) const;
206
208 void fillBlockStructure(int* Cp, int* Ci) const;
209
211 const std::vector<IntBlockMap>& blockCols() const { return _blockCols; }
212 std::vector<IntBlockMap>& blockCols() { return _blockCols; }
213
215 const std::vector<int>& rowBlockIndices() const { return _rowBlockIndices; }
216 std::vector<int>& rowBlockIndices() { return _rowBlockIndices; }
217
219 const std::vector<int>& colBlockIndices() const { return _colBlockIndices; }
220 std::vector<int>& colBlockIndices() { return _colBlockIndices; }
221
227 bool writeOctave(const char* filename, bool upperTriangle = true) const;
228
234 SparseBlockMatrixCCS<MatrixType>& blockCCS) const;
235
241 SparseBlockMatrixCCS<MatrixType>& blockCCS) const;
242
248
249 protected:
250 std::vector<int> _rowBlockIndices;
252 std::vector<int>
257 std::vector<IntBlockMap> _blockCols;
259
260 private:
261 template <class MatrixTransposedType>
263
265};
266
267template <class MatrixType>
268std::ostream& operator<<(std::ostream&, const SparseBlockMatrix<MatrixType>& m);
269
271
272} // namespace g2o
273
275
276#endif
representing the structure of a matrix in column compressed structure (only the upper triangular part...
Sparse matrix which uses blocks.
Sparse matrix which uses blocks based on hash structures.
Sparse matrix which uses blocks.
size_t nonZeros() const
number of non-zero elements
void takePatternFromHash(SparseBlockMatrixHashMap< MatrixType > &hashMatrix)
bool symmPermutation(SparseBlockMatrix< MatrixType > *&dest, const int *pinv, bool onlyUpper=false) const
bool add(SparseBlockMatrix< MatrixType > &dest) const
adds the current matrix to the destination
bool transpose(SparseBlockMatrix< MatrixTransposedType > &dest) const
bool writeOctave(const char *filename, bool upperTriangle=true) const
std::vector< IntBlockMap > & blockCols()
int fillSparseBlockMatrixCCSTransposed(SparseBlockMatrixCCS< MatrixType > &blockCCS) const
size_t nonZeroBlocks() const
number of allocated blocks
int colBaseOfBlock(int c) const
where does the col at block-col r starts?
int fillCCS(int *Cp, int *Ci, double *Cx, bool upperTriangle=false) const
int rowsOfBlock(int r) const
how many rows does the block at block-row r has?
const std::vector< IntBlockMap > & blockCols() const
the block matrices per block-column
int fillSparseBlockMatrixCCS(SparseBlockMatrixCCS< MatrixType > &blockCCS) const
std::unique_ptr< SparseBlockMatrix< MatrixType > > added() const
SparseBlockMatrix * slice(int rmin, int rmax, int cmin, int cmax, bool alloc=true) const
void rightMultiply(double *&dest, const double *src) const
dest = M * (*this)
void multiplySymmetricUpperTriangle(double *&dest, const double *src) const
const std::vector< int > & colBlockIndices() const
indices of the column blocks
std::unique_ptr< SparseBlockMatrix< MatrixTransposedType > > transposed() const
std::map< int, SparseMatrixBlock * > IntBlockMap
int rows() const
rows of the matrix
SparseMatrixBlock * block(int r, int c, bool alloc=false)
SparseBlockMatrix * clone() const
deep copy of a sparse-block-matrix;
std::vector< int > _rowBlockIndices
MatrixType SparseMatrixBlock
this is the type of the elementary block, it is an Eigen::Matrix.
bool multiply(SparseBlockMatrix< MatrixResultType > *&dest, const SparseBlockMatrix< MatrixFactorType > *M) const
dest = (*this) * M
int cols() const
columns of the matrix
std::vector< int > _colBlockIndices
std::vector< int > & colBlockIndices()
void add_internal(SparseBlockMatrix< MatrixType > &dest) const
std::vector< int > & rowBlockIndices()
void fillBlockStructure(MatrixStructure &ms) const
exports the non zero blocks in the structure matrix ms
std::vector< IntBlockMap > _blockCols
const std::vector< int > & rowBlockIndices() const
indices of the row blocks
void clear(bool dealloc=false)
void transpose_internal(SparseBlockMatrix< MatrixTransposedType > &dest) const
int rowBaseOfBlock(int r) const
where does the row at block-row r starts?
void scale(double a)
*this *= a
int colsOfBlock(int c) const
how many cols does the block at block-col c has?
some general case utility functions
SparseBlockMatrix< MatrixX > SparseBlockMatrixX
std::ostream & operator<<(std::ostream &os, const G2OBatchStatistics &st)