g2o
sparse_helper.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 "sparse_helper.h"
28 
29 #include <string>
30 #include <fstream>
31 #include <iomanip>
32 #include <vector>
33 #include <algorithm>
34 
35 using namespace std;
36 
37 namespace g2o {
38 
39  namespace {
40  struct TripletEntry
41  {
42  int r, c;
43  double x;
44  TripletEntry(int r_, int c_, double x_) : r(r_), c(c_), x(x_) {}
45  };
46  struct TripletColSort
47  {
48  bool operator()(const TripletEntry& e1, const TripletEntry& e2) const
49  {
50  return e1.c < e2.c || (e1.c == e2.c && e1.r < e2.r);
51  }
52  };
53  }
54 
55  bool writeVector(const string& filename, const double*v, int n)
56  {
57  ofstream os(filename.c_str());
58  os << fixed;
59  for (int i=0; i<n; i++)
60  os << *v++ << endl;
61  return os.good();
62  }
63 
64  bool writeCCSMatrix(const string& filename, int rows, int cols, const int* Ap, const int* Ai, const double* Ax, bool upperTriangleSymmetric)
65  {
66  vector<TripletEntry> entries;
67  entries.reserve((size_t)Ap[cols]);
68  for (int i=0; i < cols; i++) {
69  const int& rbeg = Ap[i];
70  const int& rend = Ap[i+1];
71  for (int j = rbeg; j < rend; j++) {
72  entries.push_back(TripletEntry(Ai[j], i, Ax[j]));
73  if (upperTriangleSymmetric && Ai[j] != i)
74  entries.push_back(TripletEntry(i, Ai[j], Ax[j]));
75  }
76  }
77  sort(entries.begin(), entries.end(), TripletColSort());
78 
79  string name = filename;
80  std::string::size_type lastDot = name.find_last_of('.');
81  if (lastDot != std::string::npos)
82  name = name.substr(0, lastDot);
83 
84  std::ofstream fout(filename.c_str());
85  fout << "# name: " << name << std::endl;
86  fout << "# type: sparse matrix" << std::endl;
87  fout << "# nnz: " << entries.size() << std::endl;
88  fout << "# rows: " << rows << std::endl;
89  fout << "# columns: " << cols << std::endl;
90  //fout << fixed;
91  fout << setprecision(9) << endl;
92  for (vector<TripletEntry>::const_iterator it = entries.begin(); it != entries.end(); ++it) {
93  const TripletEntry& entry = *it;
94  fout << entry.r+1 << " " << entry.c+1 << " " << entry.x << std::endl;
95  }
96  return fout.good();
97  }
98 
99 } // end namespace
bool writeVector(const string &filename, const double *v, int n)
bool writeCCSMatrix(const string &filename, int rows, int cols, const int *Ap, const int *Ai, const double *Ax, bool upperTriangleSymmetric)