g2o
Loading...
Searching...
No Matches
Classes | Typedefs | Functions
convertSegmentLine.cpp File Reference
#include <algorithm>
#include <cassert>
#include <csignal>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include "g2o/apps/g2o_simulator/simutils.h"
#include "g2o/core/batch_stats.h"
#include "g2o/core/estimate_propagator.h"
#include "g2o/core/factory.h"
#include "g2o/core/hyper_dijkstra.h"
#include "g2o/core/hyper_graph_action.h"
#include "g2o/core/optimization_algorithm.h"
#include "g2o/core/optimization_algorithm_factory.h"
#include "g2o/core/sparse_optimizer.h"
#include "g2o/core/sparse_optimizer_terminate_action.h"
#include "g2o/stuff/color_macros.h"
#include "g2o/stuff/command_args.h"
#include "g2o/stuff/filesys_tools.h"
#include "g2o/stuff/macros.h"
#include "g2o/stuff/string_tools.h"
#include "g2o/stuff/timeutil.h"
#include "g2o/types/slam2d_addons/types_slam2d_addons.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

◆ LineInfoMap

typedef std::map<int, LineInfo> LineInfoMap

Definition at line 71 of file convertSegmentLine.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 73 of file convertSegmentLine.cpp.

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

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(), LineInfo::p1, g2o::VertexLine2D::p1Id, LineInfo::p2, 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::BaseEdge< D, E >::setMeasurement(), g2o::EdgeSE2::setMeasurement(), g2o::EdgeSE2Segment2DPointLine::theta(), g2o::OptimizableGraph::vertex(), g2o::HyperGraph::Edge::vertices(), and g2o::HyperGraph::vertices().