g2o
Loading...
Searching...
No Matches
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
21#include <QFileDialog>
22#include <fstream>
23#include <iostream>
24
30using namespace std;
31
32namespace {
33using SlamBlockSolver = g2o::BlockSolver<g2o::BlockSolverTraits<-1, -1> >;
34using SlamLinearSolver =
36
37// Gauss Newton
38g2o::OptimizationAlgorithm* createGauss() {
39 auto linearSolverGN = std::make_unique<SlamLinearSolver>();
40 linearSolverGN->setBlockOrdering(false);
42 std::make_unique<SlamBlockSolver>(std::move(linearSolverGN)));
43}
44
45// Levenberg
46g2o::OptimizationAlgorithm* createLevenberg() {
47 auto linearSolverLM = std::make_unique<SlamLinearSolver>();
48 linearSolverLM->setBlockOrdering(false);
50 std::make_unique<SlamBlockSolver>(std::move(linearSolverLM)));
51}
52
53} // namespace
54
55MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent) { setupUi(this); }
56
58
60 viewer->graph->clear();
61 QString filename = QFileDialog::getOpenFileName(
62 this, "Load g2o file", "", "g2o files (*.g2o);;All Files (*)");
63 if (!filename.isNull()) {
64 ifstream ifs(filename.toStdString().c_str());
65 viewer->graph->load(ifs);
66 cerr << "Graph loaded with " << viewer->graph->vertices().size()
67 << " vertices and " << viewer->graph->edges().size() << " Measurements"
68 << endl;
69 }
70 viewer->update();
71 fixGraph();
72}
73
75 QString filename = QFileDialog::getSaveFileName(this, "Save g2o file", "",
76 "g2o files (*.g2o)");
77 if (!filename.isNull()) {
78 ofstream fout(filename.toStdString().c_str());
79 viewer->graph->save(fout);
80 if (fout.good())
81 cerr << "Saved " << filename.toStdString() << endl;
82 else
83 cerr << "Error while saving file" << endl;
84 }
85}
86
87void MainWindow::on_actionQuit_triggered(bool) { close(); }
88
90 if (viewer->graph->vertices().size() == 0 ||
91 viewer->graph->edges().size() == 0) {
92 cerr << "Graph has no vertices / edges" << endl;
93 return;
94 }
95
96 viewer->graph->initializeOptimization();
97
98 if (rbGauss->isChecked())
99 viewer->graph->setAlgorithm(createGauss());
100 else if (rbLevenberg->isChecked())
101 viewer->graph->setAlgorithm(createLevenberg());
102 else
103 viewer->graph->setAlgorithm(createGauss());
104
105 int maxIterations = spIterations->value();
106 int iter = viewer->graph->optimize(maxIterations);
107 if (maxIterations > 0 && !iter) {
108 cerr << "Optimization failed, result might be invalid" << endl;
109 }
110
111 if (cbCovariances->isChecked()) {
112 std::vector<std::pair<int, int> > cov_vertices;
113 for (const auto& vertex_index : viewer->graph->vertices()) {
114 auto* vertex =
115 static_cast<g2o::OptimizableGraph::Vertex*>(vertex_index.second);
116 if (!vertex->fixed())
117 cov_vertices.emplace_back(vertex->hessianIndex(),
118 vertex->hessianIndex());
119 }
120 viewer->covariances.clear(true);
121 std::cerr << "Compute covariance matrices" << std::endl;
122 bool cov_result =
123 viewer->graph->computeMarginals(viewer->covariances, cov_vertices);
124 viewer->drawCovariance = cov_result;
125 std::cerr << (cov_result ? "Done." : "Failed") << std::endl;
126 } else {
127 viewer->drawCovariance = false;
128 }
129 viewer->update();
130}
131
133 viewer->graph->computeInitialGuess();
134 viewer->drawCovariance = false;
135 viewer->update();
136}
137
139 if (viewer->graph->vertices().size() == 0 ||
140 viewer->graph->edges().size() == 0) {
141 return;
142 }
143
144 // check for vertices to fix to remove DoF
145 bool gaugeFreedom = viewer->graph->gaugeFreedom();
146 g2o::OptimizableGraph::Vertex* gauge = viewer->graph->findGauge();
147 if (gaugeFreedom) {
148 if (!gauge) {
149 cerr << "cannot find a vertex to fix in this thing" << endl;
150 return;
151 } else {
152 cerr << "graph is fixed by node " << gauge->id() << endl;
153 gauge->setFixed(true);
154 }
155 } else {
156 cerr << "graph is fixed by priors" << endl;
157 }
158
159 viewer->graph->setVerbose(true);
160 viewer->graph->computeActiveErrors();
161}
void fixGraph()
void on_btnOptimize_clicked()
void on_actionSave_triggered(bool)
void on_btnInitialGuess_clicked()
void on_actionLoad_triggered(bool)
MainWindow(QWidget *parent=0)
void on_actionQuit_triggered(bool)
Implementation of a solver operating on the blocks of the Hessian.
int id() const
returns the id
linear solver which uses the sparse Cholesky solver from Eigen
A general case Vertex for optimization.
const OptimizableGraph * graph() const
void setFixed(bool fixed)
true => this node should be considered fixed during the optimization
Implementation of the Gauss Newton Algorithm.
Implementation of the Levenberg Algorithm.
Generic interface for a non-linear solver operating on a graph.
Definition jet.h:876
traits to summarize the properties of the fixed size optimization problem