g2o
Loading...
Searching...
No Matches
matrix_structure.cpp
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#include "matrix_structure.h"
28
29#include <algorithm>
30#include <fstream>
31#include <string>
32#include <string_view>
33#include <vector>
34using namespace std;
35
36namespace g2o {
37
38struct ColSort {
39 bool operator()(const pair<int, int>& e1, const pair<int, int>& e2) const {
40 return e1.second < e2.second ||
41 (e1.second == e2.second && e1.first < e2.first);
42 }
43};
44
46 : n(0), m(0), Ap(0), Aii(0), maxN(0), maxNz(0) {}
47
49
50void MatrixStructure::alloc(int n_, int nz) {
51 if (n == 0) {
52 maxN = n = n_;
53 maxNz = nz;
54 Ap = new int[maxN + 1];
55 Aii = new int[maxNz];
56 } else {
57 n = n_;
58 if (maxNz < nz) {
59 maxNz = 2 * nz;
60 delete[] Aii;
61 Aii = new int[maxNz];
62 }
63 if (maxN < n) {
64 maxN = 2 * n;
65 delete[] Ap;
66 Ap = new int[maxN + 1];
67 }
68 }
69}
70
72 n = 0;
73 m = 0;
74 maxN = 0;
75 maxNz = 0;
76 delete[] Aii;
77 Aii = 0;
78 delete[] Ap;
79 Ap = 0;
80}
81
82bool MatrixStructure::write(std::string_view filename) const {
83 const int& cols = n;
84 const int& rows = m;
85
86 const string_view name = [&filename]() {
87 const std::string::size_type lastDot = filename.find_last_of('.');
88 if (lastDot != std::string_view::npos) return filename.substr(0, lastDot);
89 return filename;
90 }();
91
92 vector<pair<int, int> > entries;
93 for (int i = 0; i < cols; ++i) {
94 const int& rbeg = Ap[i];
95 const int& rend = Ap[i + 1];
96 for (int j = rbeg; j < rend; ++j) {
97 entries.push_back(make_pair(Aii[j], i));
98 if (Aii[j] != i) entries.push_back(make_pair(i, Aii[j]));
99 }
100 }
101
102 sort(entries.begin(), entries.end(), ColSort());
103
104 const string output_filename(filename);
105 std::ofstream fout(output_filename);
106 fout << "# name: " << name << std::endl;
107 fout << "# type: sparse matrix" << std::endl;
108 fout << "# nnz: " << entries.size() << std::endl;
109 fout << "# rows: " << rows << std::endl;
110 fout << "# columns: " << cols << std::endl;
111 for (vector<pair<int, int> >::const_iterator it = entries.begin();
112 it != entries.end(); ++it) {
113 const pair<int, int>& entry = *it;
114 fout << entry.first << " " << entry.second << " 0"
115 << std::endl; // write a constant value of 0
116 }
117
118 return fout.good();
119}
120
121} // namespace g2o
int maxNz
size of the allocated memory
int maxN
size of the allocated memory
bool write(std::string_view filename) const
int * Aii
row indices of A, of size nz = Ap [n]
int m
A is m-by-n. m must be >= 0.
int n
A is m-by-n. n must be >= 0.
void alloc(int n_, int nz)
int * Ap
column pointers for A, of size n+1
Definition jet.h:876
bool operator()(const pair< int, int > &e1, const pair< int, int > &e2) const