Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "main_window.h"
00020
00021
00022 #include "g2o/core/graph_optimizer_sparse.h"
00023 #include "g2o/core/estimate_propagator.h"
00024
00025 #include <QFileDialog>
00026
00027 #include <fstream>
00028 #include <iostream>
00029 using namespace std;
00030
00031 MainWindow::MainWindow(QWidget * parent, Qt::WindowFlags flags) :
00032 QMainWindow(parent, flags)
00033 {
00034 setupUi(this);
00035 }
00036
00037 MainWindow::~MainWindow()
00038 {
00039 }
00040
00041 void MainWindow::on_actionLoad_triggered(bool)
00042 {
00043 viewer->graph->clear();
00044 QString filename = QFileDialog::getOpenFileName(this, "Load g2o file", "", "g2o files (*.g2o);;All Files (*)");
00045 if (! filename.isNull()) {
00046 ifstream ifs(filename.toStdString().c_str());
00047 viewer->graph->load(ifs);
00048 cerr << "Graph loaded with " << viewer->graph->vertices().size() << " vertices and "
00049 << viewer->graph->edges().size() << " measurments" << endl;
00050 }
00051 viewer->updateGL();
00052 fixGraph();
00053 }
00054
00055 void MainWindow::on_actionSave_triggered(bool)
00056 {
00057 QString filename = QFileDialog::getSaveFileName(this, "Save g2o file", "", "g2o files (*.g2o)");
00058 if (! filename.isNull()) {
00059 ofstream fout(filename.toStdString().c_str());
00060 viewer->graph->save(fout);
00061 if (fout.good())
00062 cerr << "Saved " << filename.toStdString() << endl;
00063 else
00064 cerr << "Error while saving file" << endl;
00065 }
00066 }
00067
00068 void MainWindow::on_actionQuit_triggered(bool)
00069 {
00070 close();
00071 }
00072
00073 void MainWindow::on_btnOptimize_clicked()
00074 {
00075 if (viewer->graph->vertices().size() == 0 || viewer->graph->edges().size() == 0) {
00076 cerr << "Graph has no vertices / egdes" << endl;
00077 return;
00078 }
00079
00080 viewer->graph->initializeOptimization();
00081
00082 if (rbGauss->isChecked())
00083 viewer->graph->setMethod(g2o::SparseOptimizer::GaussNewton);
00084 else if (rbLevenberg->isChecked())
00085 viewer->graph->setMethod(g2o::SparseOptimizer::LevenbergMarquardt);
00086 else
00087 viewer->graph->setMethod(g2o::SparseOptimizer::GaussNewton);
00088
00089 int maxIterations = spIterations->value();
00090 int iter = viewer->graph->optimize(maxIterations);
00091 if (maxIterations > 0 && !iter){
00092 cerr << "Optimization failed, result might be invalid" << endl;
00093 }
00094
00095 if (cbCovariances->isChecked()) {
00096 viewer->graph->solver()->computeMarginals();
00097 }
00098 viewer->drawCovariance = cbCovariances->isChecked();
00099
00100 viewer->updateGL();
00101 }
00102
00103 void MainWindow::on_btnInitialGuess_clicked()
00104 {
00105 viewer->graph->computeInitialGuess();
00106 viewer->drawCovariance = false;
00107 viewer->updateGL();
00108 }
00109
00110 void MainWindow::fixGraph()
00111 {
00112 if (viewer->graph->vertices().size() == 0 || viewer->graph->edges().size() == 0) {
00113 return;
00114 }
00115
00116
00117 bool gaugeFreedom = viewer->graph->gaugeFreedom();
00118 g2o::OptimizableGraph::Vertex* gauge = viewer->graph->findGauge();
00119 if (gaugeFreedom) {
00120 if (! gauge) {
00121 cerr << "cannot find a vertex to fix in this thing" << endl;
00122 return;
00123 } else {
00124 cerr << "graph is fixed by node " << gauge->id() << endl;
00125 gauge->setFixed(true);
00126 }
00127 } else {
00128 cerr << "graph is fixed by priors" << endl;
00129 }
00130
00131 viewer->graph->setVerbose(true);
00132 viewer->graph->computeActiveErrors();
00133 }