g2o
Loading...
Searching...
No Matches
output_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 "output_helper.h"
28
29#include <Eigen/Core>
30#include <fstream>
31#include <iostream>
32
37using namespace std;
38
39namespace g2o {
40
42 for (size_t i = 0; i < e->vertices().size(); ++i) {
44 static_cast<OptimizableGraph::Vertex*>(e->vertices()[i]);
45 if (v->dimension() != dim) return false;
46 }
47 return true;
48}
49
50bool saveGnuplot(const std::string& gnudump,
51 const OptimizableGraph& optimizer) {
53 for (HyperGraph::VertexIDMap::const_iterator it =
54 optimizer.vertices().begin();
55 it != optimizer.vertices().end(); ++it) {
56 vset.insert(it->second);
57 }
58 return saveGnuplot(gnudump, vset, optimizer.edges());
59}
60
61bool saveGnuplot(const std::string& gnudump,
62 const HyperGraph::VertexSet& vertices,
63 const HyperGraph::EdgeSet& edges) {
64 // seek for an action whose name is writeGnuplot in the library
67 if (!saveGnuplot) {
68 cerr << __PRETTY_FUNCTION__ << ": no action \"writeGnuplot\" registered"
69 << endl;
70 return false;
71 }
73
74 int maxDim = -1;
75 int minDim = numeric_limits<int>::max();
76 for (HyperGraph::VertexSet::const_iterator it = vertices.begin();
77 it != vertices.end(); ++it) {
79 int vdim = v->dimension();
80 maxDim = (std::max)(vdim, maxDim);
81 minDim = (std::min)(vdim, minDim);
82 }
83
84 string extension = getFileExtension(gnudump);
85 if (extension.size() == 0) extension = "dat";
86 string baseFilename = getPureFilename(gnudump);
87
88 // check for odometry edges
89 bool hasOdomEdge = false;
90 bool hasLandmarkEdge = false;
91 for (HyperGraph::EdgeSet::const_iterator it = edges.begin();
92 it != edges.end(); ++it) {
93 OptimizableGraph::Edge* e = static_cast<OptimizableGraph::Edge*>(*it);
94 if (e->vertices().size() == 2) {
95 if (edgeAllVertsSameDim(e, maxDim))
96 hasOdomEdge = true;
97 else
98 hasLandmarkEdge = true;
99 }
100 if (hasOdomEdge && hasLandmarkEdge) break;
101 }
102
103 bool fileStatus = true;
104 if (hasOdomEdge) {
105 string odomFilename = baseFilename + "_odom_edges." + extension;
106 cerr << "# saving " << odomFilename << " ... ";
107 ofstream fout(odomFilename.c_str());
108 if (!fout) {
109 cerr << "Unable to open file" << endl;
110 return false;
111 }
112 params.os = &fout;
113
114 // writing odometry edges
115 for (HyperGraph::EdgeSet::const_iterator it = edges.begin();
116 it != edges.end(); ++it) {
117 OptimizableGraph::Edge* e = static_cast<OptimizableGraph::Edge*>(*it);
118 if (e->vertices().size() != 2 || !edgeAllVertsSameDim(e, maxDim))
119 continue;
120 (*saveGnuplot)(e, &params);
121 }
122 cerr << "done." << endl;
123 }
124
125 if (hasLandmarkEdge) {
126 string filename = baseFilename + "_landmarks_edges." + extension;
127 cerr << "# saving " << filename << " ... ";
128 ofstream fout(filename.c_str());
129 if (!fout) {
130 cerr << "Unable to open file" << endl;
131 return false;
132 }
133 params.os = &fout;
134
135 // writing landmark edges
136 for (HyperGraph::EdgeSet::const_iterator it = edges.begin();
137 it != edges.end(); ++it) {
138 OptimizableGraph::Edge* e = static_cast<OptimizableGraph::Edge*>(*it);
139 if (e->vertices().size() != 2 || edgeAllVertsSameDim(e, maxDim)) continue;
140 (*saveGnuplot)(e, &params);
141 }
142 cerr << "done." << endl;
143 }
144
145 if (1) {
146 string filename = baseFilename + "_edges." + extension;
147 cerr << "# saving " << filename << " ... ";
148 ofstream fout(filename.c_str());
149 if (!fout) {
150 cerr << "Unable to open file" << endl;
151 return false;
152 }
153 params.os = &fout;
154
155 // writing all edges
156 for (HyperGraph::EdgeSet::const_iterator it = edges.begin();
157 it != edges.end(); ++it) {
158 OptimizableGraph::Edge* e = static_cast<OptimizableGraph::Edge*>(*it);
159 (*saveGnuplot)(e, &params);
160 }
161 cerr << "done." << endl;
162 }
163
164 if (1) {
165 string filename = baseFilename + "_vertices." + extension;
166 cerr << "# saving " << filename << " ... ";
167 ofstream fout(filename.c_str());
168 if (!fout) {
169 cerr << "Unable to open file" << endl;
170 return false;
171 }
172 params.os = &fout;
173
174 for (HyperGraph::VertexSet::const_iterator it = vertices.begin();
175 it != vertices.end(); ++it) {
177 (*saveGnuplot)(v, &params);
178 }
179 cerr << "done." << endl;
180 }
181
182 return fileStatus;
183}
184
185bool dumpEdges(std::ostream& os, const OptimizableGraph& optimizer) {
186 // seek for an action whose name is writeGnuplot in the library
189 if (!saveGnuplot) {
190 cerr << __PRETTY_FUNCTION__ << ": no action \"writeGnuplot\" registered"
191 << endl;
192 return false;
193 }
195 params.os = &os;
196
197 // writing all edges
198 os << "set terminal x11 noraise" << endl;
199 os << "set size ratio -1" << endl;
200 os << "plot \"-\" w l" << endl;
201 for (HyperGraph::EdgeSet::const_iterator it = optimizer.edges().begin();
202 it != optimizer.edges().end(); ++it) {
203 OptimizableGraph::Edge* e = static_cast<OptimizableGraph::Edge*>(*it);
204 (*saveGnuplot)(e, &params);
205 }
206 os << "e" << endl;
207
208 return true;
209}
210
211} // namespace g2o
static HyperGraphActionLibrary * instance()
return the single instance of the HyperGraphActionLibrary
HyperGraphElementAction * actionByName(const std::string &name)
Abstract action that operates on a graph entity.
const VertexContainer & vertices() const
std::set< Edge * > EdgeSet
std::set< Vertex * > VertexSet
const EdgeSet & edges() const
const VertexIDMap & vertices() const
A general case Vertex for optimization.
int dimension() const
dimension of the estimated state belonging to this node
#define __PRETTY_FUNCTION__
Definition macros.h:90
bool edgeAllVertsSameDim(OptimizableGraph::Edge *e, int dim)
bool saveGnuplot(const std::string &gnudump, const OptimizableGraph &optimizer)
bool dumpEdges(std::ostream &os, const OptimizableGraph &optimizer)
std::string getFileExtension(const std::string &filename)
std::string getPureFilename(const std::string &filename)
Definition jet.h:876