g2o
Classes | Typedefs | Functions
convertSegmentLine.cpp File Reference
#include <signal.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <cassert>
#include "g2o/core/estimate_propagator.h"
#include "g2o/core/sparse_optimizer.h"
#include "g2o/core/factory.h"
#include "g2o/core/optimization_algorithm_factory.h"
#include "g2o/core/hyper_dijkstra.h"
#include "g2o/core/hyper_graph_action.h"
#include "g2o/core/batch_stats.h"
#include "g2o/core/optimization_algorithm.h"
#include "g2o/core/sparse_optimizer_terminate_action.h"
#include "g2o/stuff/macros.h"
#include "g2o/stuff/color_macros.h"
#include "g2o/stuff/command_args.h"
#include "g2o/stuff/filesys_tools.h"
#include "g2o/stuff/string_tools.h"
#include "g2o/stuff/timeutil.h"
#include "g2o/types/slam2d_addons/types_slam2d_addons.h"
#include "g2o/apps/g2o_simulator/simutils.h"
Include dependency graph for convertSegmentLine.cpp:

Go to the source code of this file.

Classes

struct  LineInfo
 

Typedefs

typedef std::map< int, LineInfoLineInfoMap
 

Functions

int main (int argc, char **argv)
 

Typedef Documentation

typedef std::map<int, LineInfo> LineInfoMap

Definition at line 72 of file convertSegmentLine.cpp.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 74 of file convertSegmentLine.cpp.

References g2o::OptimizableGraph::addEdge(), g2o::OptimizableGraph::addVertex(), g2o::computeLineParameters(), g2o::HyperGraph::edges(), g2o::BaseVertex< D, T >::estimate(), g2o::VertexSegment2D::estimateP1(), g2o::VertexSegment2D::estimateP2(), g2o::HyperGraph::Vertex::id(), g2o::BaseEdge< D, E >::information(), LineInfo::line, g2o::OptimizableGraph::load(), g2o::BaseEdge< D, E >::measurement(), g2o::EdgeSE2Segment2D::measurementP1(), g2o::EdgeSE2Segment2D::measurementP2(), g2o::VertexLine2D::p1Id, g2o::VertexLine2D::p2Id, g2o::CommandArgs::param(), g2o::CommandArgs::paramLeftOver(), g2o::CommandArgs::parseArgs(), g2o::EdgeSE2Segment2DPointLine::point(), g2o::EdgeSE2Segment2DPointLine::pointNum(), g2o::OptimizableGraph::save(), g2o::BaseVertex< D, T >::setEstimate(), g2o::OptimizableGraph::Vertex::setFixed(), g2o::OptimizableGraph::Vertex::setId(), g2o::BaseEdge< D, E >::setInformation(), g2o::EdgeSE2::setMeasurement(), g2o::BaseEdge< D, E >::setMeasurement(), g2o::EdgeSE2Segment2DPointLine::theta(), g2o::OptimizableGraph::vertex(), g2o::HyperGraph::Edge::vertices(), and g2o::HyperGraph::vertices().

75 {
76  string outputfilename;
77  string inputFilename;
78  CommandArgs arg;
79  arg.param("o", outputfilename, "", "output final version of the graph");
80  arg.paramLeftOver("graph-input", inputFilename, "", "graph file which will be processed", true);
81 
82 
83  arg.parseArgs(argc, argv);
84  OptimizableGraph inGraph;
85 
86  // registering all the types from the libraries
87 
88  if (inputFilename.size() == 0) {
89  cerr << "No input data specified" << endl;
90  return 0;
91  } else if (inputFilename == "-") {
92  cerr << "Read input from stdin" << endl;
93  if (!inGraph.load(cin)) {
94  cerr << "Error loading graph" << endl;
95  return 2;
96  }
97  } else {
98  cerr << "Read input from " << inputFilename << endl;
99  ifstream ifs(inputFilename.c_str());
100  if (!ifs) {
101  cerr << "Failed to open file" << endl;
102  return 1;
103  }
104  if (!inGraph.load(ifs)) {
105  cerr << "Error loading graph" << endl;
106  return 2;
107  }
108  }
109  cerr << "Loaded " << inGraph.vertices().size() << " vertices" << endl;
110  cerr << "Loaded " << inGraph.edges().size() << " edges" << endl;
111 
112  cerr << "filling in linfoMap and odoms" << endl;
113  LineInfoMap lmap;
114  OptimizableGraph outGraph;
115  // insert all lines in the infomap
116  int currentId = -1000;
117  bool firstVertexFound =false;
118  for (OptimizableGraph::VertexIDMap::iterator it=inGraph.vertices().begin(); it!=inGraph.vertices().end(); it++){
119  currentId = currentId > it->first ? currentId : it->first;
120 
121  VertexSE2 *pose=dynamic_cast<VertexSE2*> (it->second);
122  if (pose){
123  VertexSE2 *npose=new VertexSE2();
124  npose->setEstimate(pose->estimate());
125  npose->setId(pose->id());
126  outGraph.addVertex(npose);
127  if (! firstVertexFound) {
128  firstVertexFound = true;
129  npose->setFixed(true);
130  }
131  }
132 
133  VertexSegment2D* s=dynamic_cast<VertexSegment2D*> (it->second);
134  if (s){
135  LineInfo linfo(s);
136  outGraph.addVertex(linfo.line);
137  lmap.insert(make_pair(s->id(), linfo));
138  }
139  }
140 
141  currentId++;
142 
143  cerr << "filling in edges and odoms" << endl;
144  for (OptimizableGraph::EdgeSet::iterator it=inGraph.edges().begin(); it!=inGraph.edges().end(); it++){
145  EdgeSE2* ods=dynamic_cast<EdgeSE2*> (*it);
146  if (ods){
147  EdgeSE2* ods2=new EdgeSE2();
148  ods2->setMeasurement(ods->measurement());
149  ods2->setInformation(ods->information());
150  ods2->vertices()[0]=outGraph.vertex(ods->vertices()[0]->id());
151  ods2->vertices()[1]=outGraph.vertex(ods->vertices()[1]->id());
152  outGraph.addEdge(ods2);
153  }
154 
155  EdgeSE2Segment2D* es=dynamic_cast<EdgeSE2Segment2D*> (*it);
156  EdgeSE2Segment2DLine* el=dynamic_cast<EdgeSE2Segment2DLine*> (*it);
157  EdgeSE2Segment2DPointLine* espl=dynamic_cast<EdgeSE2Segment2DPointLine*> (*it);
158 
159  if (es || el || espl){
160  VertexSE2* pose = dynamic_cast<VertexSE2*>((*it)->vertices()[0]);
161  VertexSegment2D* segment = dynamic_cast<VertexSegment2D*>((*it)->vertices()[1]);
162  if (!pose)
163  continue;
164  pose=dynamic_cast<VertexSE2*>(outGraph.vertex(pose->id()));
165  LineInfoMap::iterator lit=lmap.find(segment->id());
166  assert (lit!=lmap.end());
167  LineInfo& linfo = lit->second;
168  VertexLine2D* line =linfo.line;
169  VertexPointXY* & p1=linfo.p1;
170  VertexPointXY* & p2=linfo.p2;
171 
172  EdgeSE2Line2D* el2=new EdgeSE2Line2D();
173  el2->vertices()[0]=pose;
174  el2->vertices()[1]=line;
175  if (el) {
176  el2->setMeasurement(el->measurement());
177  el2->setInformation(el->information());
178  outGraph.addEdge(el2);
179  }
180  if (es) {
182  Matrix2d el2info;
183  el2info << 10000, 0, 0, 1000;
184  el2->setInformation(el2info);
185  outGraph.addEdge(el2);
186  Matrix4d si=es->information();
187  if (!p1){
188  p1=new VertexPointXY();
189  p1->setEstimate(segment->estimateP1());
190  p1->setId(currentId++);
191  outGraph.addVertex(p1);
192  line->p1Id=p1->id();
193 
195  p1e->vertices()[0]=line;
196  p1e->vertices()[1]=p1;
197  p1e->setMeasurement(0);
198  Eigen::Matrix<double,1,1> p1i;
199  p1i(0,0)=1e6;
200  p1e->setInformation(p1i);
201  outGraph.addEdge(p1e);
202  }
203  if (!p2){
204  p2=new VertexPointXY();
205  p2->setEstimate(segment->estimateP2());
206  p2->setId(currentId++);
207  outGraph.addVertex(p2);
208  line->p2Id=p2->id();
209 
211  p2e->vertices()[0]=line;
212  p2e->vertices()[1]=p2;
213  p2e->setMeasurement(0);
214  Eigen::Matrix<double,1,1> p2i;
215  p2i(0,0)=1e6;
216  p2e->setInformation(p2i);
217  outGraph.addEdge(p2e);
218  }
219 
220  EdgeSE2PointXY* p1e = new EdgeSE2PointXY();
221  p1e->vertices()[0]=pose;
222  p1e->vertices()[1]=p1;
223  p1e->setMeasurement(es->measurementP1());
224  Matrix2d p1i=si.block<2,2>(0,0);
225  p1e->setInformation(p1i);
226  outGraph.addEdge(p1e);
227 
228  EdgeSE2PointXY* p2e = new EdgeSE2PointXY();
229  p2e->vertices()[0]=pose;
230  p2e->vertices()[1]=p2;
231  p2e->setMeasurement(es->measurementP2());
232  Matrix2d p2i=si.block<2,2>(2,2);
233  p2e->setInformation(p2i);
234  outGraph.addEdge(p2e);
235 
236  }
237 
238  if (espl) {
239  Matrix3d si=espl->information();
240  Vector2d lparams;
241  lparams[0]=espl->theta();
242  Vector2d n(cos(espl->theta()), sin(espl->theta()));
243  lparams[1]=n.dot(espl->point());
244  Matrix2d li;
245  li << si(2,2), 0, 0, 1000;
246  el2->setMeasurement(lparams);
247  el2->setInformation(li);
248  outGraph.addEdge(el2);
249 
250  VertexPointXY*& pX = (espl->pointNum()==0 )? p1:p2;
251  if (!pX){
252  cerr << "mkp: " << line->id() << endl;
253  pX=new VertexPointXY();
254  pX->setId(currentId++);
255  outGraph.addVertex(pX);
256 
257  Vector2d estPx;
258  if (espl->pointNum()){
259  estPx = segment->estimateP1();
260  line->p1Id=pX->id();
261  } else {
262  estPx = segment->estimateP2();
263  line->p2Id=pX->id();
264  }
265  pX->setEstimate(estPx);
266 
268  pXe->vertices()[0]=line;
269  pXe->vertices()[1]=pX;
270  pXe->setMeasurement(0);
271  Eigen::Matrix<double,1,1> pXi;
272  pXi(0,0)=1e6;
273  pXe->setInformation(pXi);
274  outGraph.addEdge(pXe);
275  }
276 
277  EdgeSE2PointXY* pXe = new EdgeSE2PointXY();
278  pXe->vertices()[0]=pose;
279  pXe->vertices()[1]=pX;
280  pXe->setMeasurement(espl->point());
281  Matrix2d pXi=si.block<2,2>(0,0);
282  pXe->setInformation(pXi);
283  outGraph.addEdge(pXe);
284  }
285  }
286  }
287 
288 
289  if (outputfilename.size() > 0) {
290  if (outputfilename == "-") {
291  cerr << "saving to stdout";
292  outGraph.save(cout);
293  } else {
294  cerr << "saving " << outputfilename << " ... ";
295  outGraph.save(outputfilename.c_str());
296  }
297  cerr << "done." << endl;
298  }
299 
300  // destroy all the singletons
301  //Factory::destroy();
302  //OptimizationAlgorithmFactory::destroy();
303  //HyperGraphActionLibrary::destroy();
304 
305  return 0;
306 }
Eigen::Vector2d computeLineParameters(const Eigen::Vector2d &p1, const Eigen::Vector2d &p2)
Definition: simutils.cpp:120
int id() const
returns the id
Definition: hyper_graph.h:148
Command line parsing of argc and argv.
Definition: command_args.h:46
virtual void setMeasurement(const SE2 &m)
Definition: edge_se2.h:56
2D edge between two Vertex2
Definition: edge_se2.h:40
virtual void setMeasurement(const Measurement &m)
Definition: base_edge.h:76
2D pose Vertex, (x,y,theta)
Definition: vertex_se2.h:41
const VertexIDMap & vertices() const
Definition: hyper_graph.h:225
Vertex * vertex(int id)
returns the vertex number id appropriately casted
virtual void setId(int id)
sets the id of the node in the graph be sure that the graph keeps consistent after changing the id ...
const VertexContainer & vertices() const
Definition: hyper_graph.h:178
bool parseArgs(int argc, char **argv, bool exitOnError=true)
virtual bool save(std::ostream &os, int level=0) const
save the graph to a stream. Again uses the Factory system.
const EdgeSet & edges() const
Definition: hyper_graph.h:230
void paramLeftOver(const std::string &name, std::string &p, const std::string &defValue, const std::string &desc, bool optional=false)
EIGEN_STRONG_INLINE void setInformation(const InformationType &information)
Definition: base_edge.h:69
void param(const std::string &name, bool &p, bool defValue, const std::string &desc)
G2O_TYPES_SLAM2D_ADDONS_API Vector2D measurementP1()
std::map< int, LineInfo > LineInfoMap
void setEstimate(const EstimateType &et)
set the estimate for the vertex also calls updateCache()
Definition: base_vertex.h:101
virtual bool load(std::istream &is, bool createEdges=true)
load the graph from a stream. Uses the Factory singleton for creating the vertices and edges...
EIGEN_STRONG_INLINE const InformationType & information() const
information matrix of the constraint
Definition: base_edge.h:67
Vector2D estimateP1() const
G2O_TYPES_SLAM2D_ADDONS_API Vector2D measurementP2()
const EstimateType & estimate() const
return the current estimate of the vertex
Definition: base_vertex.h:99
Vector2D estimateP2() const
void setFixed(bool fixed)
true => this node should be considered fixed during the optimization
virtual bool addEdge(HyperGraph::Edge *e)
virtual bool addVertex(HyperGraph::Vertex *v, Data *userData)
EIGEN_STRONG_INLINE const Measurement & measurement() const
accessor functions for the measurement represented by the edge
Definition: base_edge.h:75