g2o
Toggle main menu visibility
Loading...
Searching...
No Matches
g2o
core
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>
34
using namespace
std
;
35
36
namespace
g2o
{
37
38
struct
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
45
MatrixStructure::MatrixStructure
()
46
:
n
(0),
m
(0),
Ap
(0),
Aii
(0),
maxN
(0),
maxNz
(0) {}
47
48
MatrixStructure::~MatrixStructure
() {
free
(); }
49
50
void
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
71
void
MatrixStructure::free
() {
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
82
bool
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
g2o::MatrixStructure::maxNz
int maxNz
size of the allocated memory
Definition
matrix_structure.h:69
g2o::MatrixStructure::maxN
int maxN
size of the allocated memory
Definition
matrix_structure.h:68
g2o::MatrixStructure::write
bool write(std::string_view filename) const
Definition
matrix_structure.cpp:82
g2o::MatrixStructure::free
void free()
Definition
matrix_structure.cpp:71
g2o::MatrixStructure::~MatrixStructure
~MatrixStructure()
Definition
matrix_structure.cpp:48
g2o::MatrixStructure::Aii
int * Aii
row indices of A, of size nz = Ap [n]
Definition
matrix_structure.h:62
g2o::MatrixStructure::m
int m
A is m-by-n. m must be >= 0.
Definition
matrix_structure.h:60
g2o::MatrixStructure::MatrixStructure
MatrixStructure()
Definition
matrix_structure.cpp:45
g2o::MatrixStructure::n
int n
A is m-by-n. n must be >= 0.
Definition
matrix_structure.h:59
g2o::MatrixStructure::alloc
void alloc(int n_, int nz)
Definition
matrix_structure.cpp:50
g2o::MatrixStructure::Ap
int * Ap
column pointers for A, of size n+1
Definition
matrix_structure.h:61
matrix_structure.h
g2o
Definition
dl_wrapper.cpp:55
std
Definition
jet.h:876
g2o::ColSort
Definition
matrix_structure.cpp:38
g2o::ColSort::operator()
bool operator()(const pair< int, int > &e1, const pair< int, int > &e2) const
Definition
matrix_structure.cpp:39
Generated on
for g2o by
1.17.0