g2o
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 
31 
33 #include "g2o/stuff/string_tools.h"
34 
35 #include <Eigen/Core>
36 
37 #include <iostream>
38 #include <fstream>
39 using namespace std;
40 
41 namespace g2o {
42 
44 {
45  for (size_t i = 0; i < e->vertices().size(); ++i) {
47  if (v->dimension() != dim)
48  return false;
49  }
50  return true;
51 }
52 
53 bool saveGnuplot(const std::string& gnudump, const OptimizableGraph& optimizer)
54 {
56  for (HyperGraph::VertexIDMap::const_iterator it=optimizer.vertices().begin(); it!=optimizer.vertices().end(); it++){
57  vset.insert(it->second);
58  }
59  return saveGnuplot(gnudump, vset, optimizer.edges());
60 }
61 
62 bool saveGnuplot(const std::string& gnudump, const HyperGraph::VertexSet& vertices, const HyperGraph::EdgeSet& edges)
63 {
64  // seek for an action whose name is writeGnuplot in the library
65  HyperGraphElementAction* saveGnuplot = HyperGraphActionLibrary::instance()->actionByName("writeGnuplot");
66  if (! saveGnuplot ){
67  cerr << __PRETTY_FUNCTION__ << ": no action \"writeGnuplot\" registered" << endl;
68  return false;
69  }
71 
72  int maxDim = -1;
73  int minDim = numeric_limits<int>::max();
74  for (HyperGraph::VertexSet::const_iterator it = vertices.begin(); it != vertices.end(); ++it){
75  OptimizableGraph::Vertex* v = static_cast<OptimizableGraph::Vertex*>(*it);
76  int vdim = v->dimension();
77  maxDim = (std::max)(vdim, maxDim);
78  minDim = (std::min)(vdim, minDim);
79  }
80 
81  string extension = getFileExtension(gnudump);
82  if (extension.size() == 0)
83  extension = "dat";
84  string baseFilename = getPureFilename(gnudump);
85 
86  // check for odometry edges
87  bool hasOdomEdge = false;
88  bool hasLandmarkEdge = false;
89  for (HyperGraph::EdgeSet::const_iterator it = edges.begin(); it != edges.end(); ++it) {
90  OptimizableGraph::Edge* e = static_cast<OptimizableGraph::Edge*>(*it);
91  if (e->vertices().size() == 2) {
92  if (edgeAllVertsSameDim(e, maxDim))
93  hasOdomEdge = true;
94  else
95  hasLandmarkEdge = true;
96  }
97  if (hasOdomEdge && hasLandmarkEdge)
98  break;
99  }
100 
101  bool fileStatus = true;
102  if (hasOdomEdge) {
103  string odomFilename = baseFilename + "_odom_edges." + extension;
104  cerr << "# saving " << odomFilename << " ... ";
105  ofstream fout(odomFilename.c_str());
106  if (! fout) {
107  cerr << "Unable to open file" << endl;
108  return false;
109  }
110  params.os = &fout;
111 
112  // writing odometry edges
113  for (HyperGraph::EdgeSet::const_iterator it = edges.begin(); it != edges.end(); ++it) {
114  OptimizableGraph::Edge* e = static_cast<OptimizableGraph::Edge*>(*it);
115  if (e->vertices().size() != 2 || ! edgeAllVertsSameDim(e, maxDim))
116  continue;
117  (*saveGnuplot)(e, &params);
118  }
119  cerr << "done." << endl;
120  }
121 
122  if (hasLandmarkEdge) {
123  string filename = baseFilename + "_landmarks_edges." + extension;
124  cerr << "# saving " << filename << " ... ";
125  ofstream fout(filename.c_str());
126  if (! fout) {
127  cerr << "Unable to open file" << endl;
128  return false;
129  }
130  params.os = &fout;
131 
132  // writing landmark edges
133  for (HyperGraph::EdgeSet::const_iterator it = edges.begin(); it != edges.end(); ++it) {
134  OptimizableGraph::Edge* e = static_cast<OptimizableGraph::Edge*>(*it);
135  if (e->vertices().size() != 2 || edgeAllVertsSameDim(e, maxDim))
136  continue;
137  (*saveGnuplot)(e, &params);
138  }
139  cerr << "done." << endl;
140  }
141 
142  if (1) {
143  string filename = baseFilename + "_edges." + extension;
144  cerr << "# saving " << filename << " ... ";
145  ofstream fout(filename.c_str());
146  if (! fout) {
147  cerr << "Unable to open file" << endl;
148  return false;
149  }
150  params.os = &fout;
151 
152  // writing all edges
153  for (HyperGraph::EdgeSet::const_iterator it = edges.begin(); it != edges.end(); ++it) {
154  OptimizableGraph::Edge* e = static_cast<OptimizableGraph::Edge*>(*it);
155  (*saveGnuplot)(e, &params);
156  }
157  cerr << "done." << endl;
158  }
159 
160  if (1) {
161  string filename = baseFilename + "_vertices." + extension;
162  cerr << "# saving " << filename << " ... ";
163  ofstream fout(filename.c_str());
164  if (! fout) {
165  cerr << "Unable to open file" << endl;
166  return false;
167  }
168  params.os = &fout;
169 
170  for (HyperGraph::VertexSet::const_iterator it = vertices.begin(); it != vertices.end(); ++it){
171  OptimizableGraph::Vertex* v = static_cast<OptimizableGraph::Vertex*>(*it);
172  (*saveGnuplot)(v, &params);
173  }
174  cerr << "done." << endl;
175  }
176 
177  return fileStatus;
178 }
179 
180 bool dumpEdges(std::ostream& os, const OptimizableGraph& optimizer)
181 {
182  // seek for an action whose name is writeGnuplot in the library
183  HyperGraphElementAction* saveGnuplot = HyperGraphActionLibrary::instance()->actionByName("writeGnuplot");
184  if (! saveGnuplot ){
185  cerr << __PRETTY_FUNCTION__ << ": no action \"writeGnuplot\" registered" << endl;
186  return false;
187  }
189  params.os = &os;
190 
191  // writing all edges
192  os << "set terminal x11 noraise" << endl;
193  os << "set size ratio -1" << endl;
194  os << "plot \"-\" w l" << endl;
195  for (HyperGraph::EdgeSet::const_iterator it = optimizer.edges().begin(); it != optimizer.edges().end(); ++it) {
196  OptimizableGraph::Edge* e = static_cast<OptimizableGraph::Edge*>(*it);
197  (*saveGnuplot)(e, &params);
198  }
199  os << "e" << endl;
200 
201  return true;
202 }
203 
204 } // end namespace
std::string getFileExtension(const std::string &filename)
#define __PRETTY_FUNCTION__
Definition: macros.h:89
bool dumpEdges(std::ostream &os, const OptimizableGraph &optimizer)
Abstract action that operates on a graph entity.
std::set< Vertex * > VertexSet
Definition: hyper_graph.h:136
const VertexIDMap & vertices() const
Definition: hyper_graph.h:225
std::set< Edge * > EdgeSet
Definition: hyper_graph.h:135
int dimension() const
dimension of the estimated state belonging to this node
Protocol The SLAM executable accepts such as solving the and retrieving or vertices in the explicitly state the reprensentation poses are represented by poses by VERTEX_XYZRPY In the Quaternions and other representations could be but note that it is up to the SLAM algorithm to choose the internal representation of the angles The keyword is followed by a unique vertex ID and an optional initialization of the or edges in the explicitly state the type of the constraint pose constraints are given by pose constraints by EDGE_XYZRPY The keyword is followed by a unique edge the IDs of the referenced vertices
Definition: protocol.txt:7
const VertexContainer & vertices() const
Definition: hyper_graph.h:178
const EdgeSet & edges() const
Definition: hyper_graph.h:230
std::string getPureFilename(const std::string &filename)
A general case Vertex for optimization.
bool edgeAllVertsSameDim(OptimizableGraph::Edge *e, int dim)
bool saveGnuplot(const std::string &gnudump, const HyperGraph::VertexSet &vertices, const HyperGraph::EdgeSet &edges)