g2o
Loading...
Searching...
No Matches
convert_sba_slam3d.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 <cassert>
28#include <fstream>
29#include <iostream>
30
33#include "g2o/stuff/macros.h"
37
38using namespace std;
39using namespace g2o;
40
41int main(int argc, char** argv) {
42 string inputFilename;
43 string outputFilename;
44 // command line parsing
45 CommandArgs commandLineArguments;
46 commandLineArguments.paramLeftOver("gm2dl-input", inputFilename, "",
47 "gm2dl file which will be processed");
48 commandLineArguments.paramLeftOver("gm2dl-output", outputFilename, "",
49 "name of the output file");
50
51 commandLineArguments.parseArgs(argc, argv);
52
53 OptimizableGraph inputGraph;
54 bool loadStatus = inputGraph.load(inputFilename.c_str());
55 if (!loadStatus) {
56 cerr << "Error while loading input data" << endl;
57 return 1;
58 }
59
60 OptimizableGraph outputGraph;
61
62 // process all the vertices first
63 double fx = -1;
64 double baseline = -1;
65 bool firstCam = true;
66 for (OptimizableGraph::VertexIDMap::const_iterator it =
67 inputGraph.vertices().begin();
68 it != inputGraph.vertices().end(); ++it) {
69 if (dynamic_cast<VertexCam*>(it->second)) {
70 VertexCam* v = static_cast<VertexCam*>(it->second);
71 if (firstCam) {
72 firstCam = false;
74 camParams->setId(0);
75 const SBACam& c = v->estimate();
76 baseline = c.baseline;
77 fx = c.Kcam(0, 0);
78 camParams->setKcam(c.Kcam(0, 0), c.Kcam(1, 1), c.Kcam(0, 2),
79 c.Kcam(1, 2));
80 outputGraph.addParameter(camParams);
81 }
82
83 VertexSE3* ov = new VertexSE3;
84 ov->setId(v->id());
85 Eigen::Isometry3d p;
86 p = v->estimate().rotation();
87 p.translation() = v->estimate().translation();
88 ov->setEstimate(p);
89 if (!outputGraph.addVertex(ov)) {
90 assert(0 && "Failure adding camera vertex");
91 }
92 } else if (dynamic_cast<VertexPointXYZ*>(it->second)) {
93 VertexPointXYZ* v = static_cast<VertexPointXYZ*>(it->second);
94
96 ov->setId(v->id());
97 ov->setEstimate(v->estimate());
98 if (!outputGraph.addVertex(ov)) {
99 assert(0 && "Failure adding camera vertex");
100 }
101 }
102 }
103
104 for (OptimizableGraph::EdgeSet::const_iterator it =
105 inputGraph.edges().begin();
106 it != inputGraph.edges().end(); ++it) {
107 if (dynamic_cast<EdgeProjectP2SC*>(*it)) {
108 EdgeProjectP2SC* e = static_cast<EdgeProjectP2SC*>(*it);
109
111 oe->vertices()[0] = outputGraph.vertex(e->vertices()[1]->id());
112 oe->vertices()[1] = outputGraph.vertex(e->vertices()[0]->id());
113
114 double kx = e->measurement().x();
115 double ky = e->measurement().y();
116 double disparity = kx - e->measurement()(2);
117
118 oe->setMeasurement(Eigen::Vector3d(kx, ky, disparity / (fx * baseline)));
119 oe->setInformation(e->information()); // TODO convert information matrix
120 oe->setParameterId(0, 0);
121 if (!outputGraph.addEdge(oe)) {
122 assert(0 && "error adding edge");
123 }
124 }
125 }
126
127 cout << "Vertices in/out:\t" << inputGraph.vertices().size() << " "
128 << outputGraph.vertices().size() << endl;
129 cout << "Edges in/out:\t" << inputGraph.edges().size() << " "
130 << outputGraph.edges().size() << endl;
131
132 cout << "Writing output ... " << flush;
133 outputGraph.save(outputFilename.c_str());
134 cout << "done." << endl;
135 return 0;
136}
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)
edge from a track to a depth camera node using a disparity measurement
const VertexContainer & vertices() const
int id() const
returns the id
const EdgeSet & edges() const
const VertexIDMap & vertices() const
bool setParameterId(int argNum, int paramId)
parameters for a camera
void setKcam(double fx, double fy, double cx, double cy)
void setId(int id_)
Definition parameter.cpp:33
Matrix3 Kcam
Definition sbacam.h:51
double baseline
Definition sbacam.h:52
const Quaternion & rotation() const
Definition se3quat.h:93
const Vector3 & translation() const
Definition se3quat.h:89
SBACam Vertex, (x,y,z,qw,qx,qy,qz) the parameterization for the increments constructed is a 6d vector...
Definition vertex_cam.h:43
Vertex for a tracked point in space.
3D pose Vertex, represented as an Isometry3
Definition vertex_se3.h:50
int main()
Definition gicp_demo.cpp:44
Definition jet.h:876
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.
bool addParameter(Parameter *p)
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