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


orb_slam2_with_maps_odom
Author(s): teng zhang
autogenerated on Fri Sep 25 2020 03:24:47