g2o
main_window.cpp
Go to the documentation of this file.
1 // g2o - General Graph Optimization
2 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
3 //
4 // This file is part of g2o.
5 //
6 // g2o is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // g2o is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with g2o. If not, see <http://www.gnu.org/licenses/>.
18 
19 #include "main_window.h"
20 //#include "moc_main_window.cpp"
21 
24 
25 #include <QFileDialog>
26 
27 #include <fstream>
28 #include <iostream>
29 using namespace std;
30 
31 MainWindow::MainWindow(QWidget * parent, Qt::WindowFlags flags) :
32  QMainWindow(parent, flags)
33 {
34  setupUi(this);
35 }
36 
38 {
39 }
40 
42 {
43  viewer->graph->clear();
44  QString filename = QFileDialog::getOpenFileName(this, "Load g2o file", "", "g2o files (*.g2o);;All Files (*)");
45  if (! filename.isNull()) {
46  ifstream ifs(filename.toStdString().c_str());
47  viewer->graph->load(ifs);
48  cerr << "Graph loaded with " << viewer->graph->vertices().size() << " vertices and "
49  << viewer->graph->edges().size() << " measurments" << endl;
50  }
51  viewer->updateGL();
52  fixGraph();
53 }
54 
56 {
57  QString filename = QFileDialog::getSaveFileName(this, "Save g2o file", "", "g2o files (*.g2o)");
58  if (! filename.isNull()) {
59  ofstream fout(filename.toStdString().c_str());
60  viewer->graph->save(fout);
61  if (fout.good())
62  cerr << "Saved " << filename.toStdString() << endl;
63  else
64  cerr << "Error while saving file" << endl;
65  }
66 }
67 
69 {
70  close();
71 }
72 
74 {
75  if (viewer->graph->vertices().size() == 0 || viewer->graph->edges().size() == 0) {
76  cerr << "Graph has no vertices / egdes" << endl;
77  return;
78  }
79 
80  viewer->graph->initializeOptimization();
81 
82  if (rbGauss->isChecked())
83  viewer->graph->setAlgorithm(solverGaussNewton);
84  else if (rbLevenberg->isChecked())
85  viewer->graph->setAlgorithm(solverLevenberg);
86  else
87  viewer->graph->setAlgorithm(solverGaussNewton);
88 
89  int maxIterations = spIterations->value();
90  int iter = viewer->graph->optimize(maxIterations);
91  if (maxIterations > 0 && !iter){
92  cerr << "Optimization failed, result might be invalid" << endl;
93  }
94 
95  if (cbCovariances->isChecked()) {
96  // TODO
97  //viewer->graph->solver()->computeMarginals();
98  }
99  viewer->drawCovariance = cbCovariances->isChecked();
100 
101  viewer->updateGL();
102 }
103 
105 {
106  viewer->graph->computeInitialGuess();
107  viewer->drawCovariance = false;
108  viewer->updateGL();
109 }
110 
112 {
113  if (viewer->graph->vertices().size() == 0 || viewer->graph->edges().size() == 0) {
114  return;
115  }
116 
117  // check for vertices to fix to remove DoF
118  bool gaugeFreedom = viewer->graph->gaugeFreedom();
119  g2o::OptimizableGraph::Vertex* gauge = viewer->graph->findGauge();
120  if (gaugeFreedom) {
121  if (! gauge) {
122  cerr << "cannot find a vertex to fix in this thing" << endl;
123  return;
124  } else {
125  cerr << "graph is fixed by node " << gauge->id() << endl;
126  gauge->setFixed(true);
127  }
128  } else {
129  cerr << "graph is fixed by priors" << endl;
130  }
131 
132  viewer->graph->setVerbose(true);
133  viewer->graph->computeActiveErrors();
134 }
g2o::OptimizationAlgorithm * solverGaussNewton
Definition: main_window.h:35
int id() const
returns the id
Definition: hyper_graph.h:148
const OptimizableGraph * graph() const
void on_btnOptimize_clicked()
Definition: main_window.cpp:77
void fixGraph()
A general case Vertex for optimization.
g2o::OptimizationAlgorithm * solverLevenberg
Definition: main_window.h:36
void on_btnInitialGuess_clicked()
void on_actionLoad_triggered(bool)
Definition: main_window.cpp:56
void setFixed(bool fixed)
true => this node should be considered fixed during the optimization
void on_actionSave_triggered(bool)
Definition: main_window.cpp:64
MainWindow(QWidget *parent=0, Qt::WindowFlags flags=0)
Definition: main_window.cpp:41
void on_actionQuit_triggered(bool)