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