Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "output_helper.h"
00018
00019 #include "g2o/core/optimizable_graph.h"
00020 #include "g2o/core/hyper_graph_action.h"
00021
00022 #include "g2o/stuff/filesys_tools.h"
00023 #include "g2o/stuff/string_tools.h"
00024
00025 #include <Eigen/Core>
00026
00027 #include <iostream>
00028 #include <fstream>
00029 using namespace std;
00030
00031 namespace g2o {
00032
00033 bool edgeAllVertsSameDim(OptimizableGraph::Edge* e, int dim)
00034 {
00035 for (size_t i = 0; i < e->vertices().size(); ++i) {
00036 OptimizableGraph::Vertex* v = static_cast<OptimizableGraph::Vertex*>(e->vertices()[i]);
00037 if (v->dimension() != dim)
00038 return false;
00039 }
00040 return true;
00041 }
00042
00043 bool saveGnuplot(const std::string& gnudump, const OptimizableGraph& optimizer)
00044 {
00045 HyperGraph::VertexSet vset;
00046 for (HyperGraph::VertexIDMap::const_iterator it=optimizer.vertices().begin(); it!=optimizer.vertices().end(); it++){
00047 vset.insert(it->second);
00048 }
00049 return saveGnuplot(gnudump, vset, optimizer.edges());
00050 }
00051
00052 bool saveGnuplot(const std::string& gnudump, const HyperGraph::VertexSet& vertices, const HyperGraph::EdgeSet& edges)
00053 {
00054
00055 HyperGraphElementAction* saveGnuplot = HyperGraphActionLibrary::instance()->actionByName("writeGnuplot");
00056 if (! saveGnuplot ){
00057 cerr << __PRETTY_FUNCTION__ << ": no action \"writeGnuplot\" registered" << endl;
00058 return false;
00059 }
00060 WriteGnuplotAction::Parameters params;
00061
00062 int maxDim = -1;
00063 int minDim = numeric_limits<int>::max();
00064 for (HyperGraph::VertexSet::const_iterator it = vertices.begin(); it != vertices.end(); ++it){
00065 OptimizableGraph::Vertex* v = static_cast<OptimizableGraph::Vertex*>(*it);
00066 int vdim = v->dimension();
00067 maxDim = max(vdim, maxDim);
00068 minDim = min(vdim, minDim);
00069 }
00070
00071 string extension = getFileExtension(gnudump);
00072 if (extension.size() == 0)
00073 extension = "dat";
00074 string baseFilename = getPureFilename(gnudump);
00075
00076
00077 bool hasOdomEdge = false;
00078 bool hasLandmarkEdge = false;
00079 for (HyperGraph::EdgeSet::const_iterator it = edges.begin(); it != edges.end(); ++it) {
00080 OptimizableGraph::Edge* e = static_cast<OptimizableGraph::Edge*>(*it);
00081 if (e->vertices().size() == 2) {
00082 if (edgeAllVertsSameDim(e, maxDim))
00083 hasOdomEdge = true;
00084 else
00085 hasLandmarkEdge = true;
00086 }
00087 if (hasOdomEdge && hasLandmarkEdge)
00088 break;
00089 }
00090
00091 bool fileStatus = true;
00092 if (hasOdomEdge) {
00093 string odomFilename = baseFilename + "_odom_edges." + extension;
00094 cerr << "# saving " << odomFilename << " ... ";
00095 ofstream fout(odomFilename.c_str());
00096 if (! fout) {
00097 cerr << "Unable to open file" << endl;
00098 return false;
00099 }
00100 params.os = &fout;
00101
00102
00103 for (HyperGraph::EdgeSet::const_iterator it = edges.begin(); it != edges.end(); ++it) {
00104 OptimizableGraph::Edge* e = static_cast<OptimizableGraph::Edge*>(*it);
00105 if (e->vertices().size() != 2 || ! edgeAllVertsSameDim(e, maxDim))
00106 continue;
00107 (*saveGnuplot)(e, ¶ms);
00108 }
00109 cerr << "done." << endl;
00110 }
00111
00112 if (hasLandmarkEdge) {
00113 string filename = baseFilename + "_landmarks_edges." + extension;
00114 cerr << "# saving " << filename << " ... ";
00115 ofstream fout(filename.c_str());
00116 if (! fout) {
00117 cerr << "Unable to open file" << endl;
00118 return false;
00119 }
00120 params.os = &fout;
00121
00122
00123 for (HyperGraph::EdgeSet::const_iterator it = edges.begin(); it != edges.end(); ++it) {
00124 OptimizableGraph::Edge* e = static_cast<OptimizableGraph::Edge*>(*it);
00125 if (e->vertices().size() != 2 || edgeAllVertsSameDim(e, maxDim))
00126 continue;
00127 (*saveGnuplot)(e, ¶ms);
00128 }
00129 cerr << "done." << endl;
00130 }
00131
00132 if (1) {
00133 string filename = baseFilename + "_edges." + extension;
00134 cerr << "# saving " << filename << " ... ";
00135 ofstream fout(filename.c_str());
00136 if (! fout) {
00137 cerr << "Unable to open file" << endl;
00138 return false;
00139 }
00140 params.os = &fout;
00141
00142
00143 for (HyperGraph::EdgeSet::const_iterator it = edges.begin(); it != edges.end(); ++it) {
00144 OptimizableGraph::Edge* e = static_cast<OptimizableGraph::Edge*>(*it);
00145 (*saveGnuplot)(e, ¶ms);
00146 }
00147 cerr << "done." << endl;
00148 }
00149
00150 if (1) {
00151 string filename = baseFilename + "_vertices." + extension;
00152 cerr << "# saving " << filename << " ... ";
00153 ofstream fout(filename.c_str());
00154 if (! fout) {
00155 cerr << "Unable to open file" << endl;
00156 return false;
00157 }
00158 params.os = &fout;
00159
00160 for (HyperGraph::VertexSet::const_iterator it = vertices.begin(); it != vertices.end(); ++it){
00161 OptimizableGraph::Vertex* v = static_cast<OptimizableGraph::Vertex*>(*it);
00162 (*saveGnuplot)(v, ¶ms);
00163 }
00164 cerr << "done." << endl;
00165 }
00166
00167 return fileStatus;
00168 }
00169
00170 bool dumpEdges(std::ostream& os, const OptimizableGraph& optimizer)
00171 {
00172
00173 HyperGraphElementAction* saveGnuplot = HyperGraphActionLibrary::instance()->actionByName("writeGnuplot");
00174 if (! saveGnuplot ){
00175 cerr << __PRETTY_FUNCTION__ << ": no action \"writeGnuplot\" registered" << endl;
00176 return false;
00177 }
00178 WriteGnuplotAction::Parameters params;
00179 params.os = &os;
00180
00181
00182 os << "set terminal x11 noraise" << endl;
00183 os << "set size ratio -1" << endl;
00184 os << "plot \"-\" w l" << endl;
00185 for (HyperGraph::EdgeSet::const_iterator it = optimizer.edges().begin(); it != optimizer.edges().end(); ++it) {
00186 OptimizableGraph::Edge* e = static_cast<OptimizableGraph::Edge*>(*it);
00187 (*saveGnuplot)(e, ¶ms);
00188 }
00189 os << "e" << endl;
00190
00191 return true;
00192 }
00193
00194 }