main_window.cpp
Go to the documentation of this file.
00001 // g2o - General Graph Optimization
00002 // Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
00003 //
00004 // This file is part of g2o.
00005 // 
00006 // g2o is free software: you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation, either version 3 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // g2o is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with g2o.  If not, see <http://www.gnu.org/licenses/>.
00018 
00019 #include "main_window.h"
00020 //#include "moc_main_window.cpp"
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   // check for vertices to fix to remove DoF
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 }


re_vision
Author(s): Dorian Galvez-Lopez
autogenerated on Sun Jan 5 2014 11:31:44