g2o
Toggle main menu visibility
Loading...
Searching...
No Matches
g2o
examples
g2o_hierarchical
star.cpp
Go to the documentation of this file.
1
// g2o - General Graph Optimization
2
// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, H. Strasdat, 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 "
star.h
"
28
29
#include "
g2o/core/optimization_algorithm_with_hessian.h
"
30
31
namespace
g2o
{
32
using namespace
std;
33
34
Star::Star
(
int
level
,
SparseOptimizer
*
optimizer
)
35
:
_level
(
level
),
_optimizer
(
optimizer
) {}
36
37
bool
Star::labelStarEdges
(
int
iterations,
EdgeLabeler
* labeler) {
38
// mark all vertices in the lowLevelEdges as floating
39
bool
ok =
true
;
40
std::set<OptimizableGraph::Vertex*> vset;
41
for
(HyperGraph::EdgeSet::iterator it =
_lowLevelEdges
.begin();
42
it !=
_lowLevelEdges
.end(); ++it) {
43
HyperGraph::Edge
* e = *it;
44
for
(
size_t
i = 0; i < e->
vertices
().size(); i++) {
45
OptimizableGraph::Vertex
* v = (
OptimizableGraph::Vertex
*)e->
vertices
()[i];
46
v->
setFixed
(
false
);
47
vset.insert(v);
48
}
49
}
50
for
(std::set<OptimizableGraph::Vertex*>::iterator it = vset.begin();
51
it != vset.end(); ++it) {
52
OptimizableGraph::Vertex
* v = *it;
53
v->
push
();
54
}
55
56
// fix all vertices in the gauge
57
// cerr << "fixing gauge: ";
58
for
(HyperGraph::VertexSet::iterator it =
_gauge
.begin(); it !=
_gauge
.end();
59
++it) {
60
OptimizableGraph::Vertex
* v = (
OptimizableGraph::Vertex
*)*it;
61
// cerr << v->id() << " ";
62
v->
setFixed
(
true
);
63
}
64
// cerr << endl;
65
if
(iterations > 0) {
66
_optimizer
->initializeOptimization(
_lowLevelEdges
);
67
_optimizer
->computeInitialGuess();
68
int
result =
_optimizer
->optimize(iterations);
69
if
(result < 1) {
70
cerr <<
"Vertices num: "
<<
_optimizer
->activeVertices().size()
71
<<
"ids: "
;
72
for
(
size_t
i = 0; i <
_optimizer
->indexMapping().size(); i++) {
73
cerr <<
_optimizer
->indexMapping()[i]->id() <<
" "
;
74
}
75
cerr << endl;
76
cerr <<
"!!! optimization failure"
<< endl;
77
cerr <<
"star size="
<<
_lowLevelEdges
.size() << endl;
78
cerr <<
"gauge: "
;
79
for
(HyperGraph::VertexSet::iterator it =
_gauge
.begin();
80
it !=
_gauge
.end(); ++it) {
81
OptimizableGraph::Vertex
* v = (
OptimizableGraph::Vertex
*)*it;
82
cerr <<
"["
<< v->
id
() <<
" "
<< v->
hessianIndex
() <<
"] "
;
83
}
84
cerr << endl;
85
ok =
false
;
86
}
87
}
else
{
88
optimizer
()->
initializeOptimization
(
_lowLevelEdges
);
89
// cerr << "guess" << endl;
90
// optimizer()->computeInitialGuess();
91
// cerr << "solver init" << endl;
92
optimizer
()->
solver
()->
init
();
93
// cerr << "structure" << endl;
94
OptimizationAlgorithmWithHessian
* solverWithHessian =
95
dynamic_cast<
OptimizationAlgorithmWithHessian
*
>
(
optimizer
()->
solver
());
96
if
(!solverWithHessian->
buildLinearStructure
())
97
cerr <<
"FATAL: failure while building linear structure"
<< endl;
98
// cerr << "errors" << endl;
99
optimizer
()->
computeActiveErrors
();
100
// cerr << "system" << endl;
101
solverWithHessian->
updateLinearSystem
();
102
}
103
104
std::set<OptimizableGraph::Edge*> star;
105
for
(HyperGraph::EdgeSet::iterator it =
_starEdges
.begin();
106
it !=
_starEdges
.end(); ++it) {
107
star.insert((
OptimizableGraph::Edge
*)*it);
108
}
109
if
(ok) {
110
int
result = labeler->
labelEdges
(star);
111
if
(result < 0) ok =
false
;
112
}
113
// release all vertices in the gauge
114
for
(std::set<OptimizableGraph::Vertex*>::iterator it = vset.begin();
115
it != vset.end(); ++it) {
116
OptimizableGraph::Vertex
* v = *it;
117
v->
pop
();
118
}
119
for
(HyperGraph::VertexSet::iterator it =
_gauge
.begin(); it !=
_gauge
.end();
120
++it) {
121
OptimizableGraph::Vertex
* v = (
OptimizableGraph::Vertex
*)*it;
122
v->
setFixed
(
false
);
123
}
124
125
return
ok;
126
}
127
128
}
// namespace g2o
g2o::HyperGraph::Edge
Definition
hyper_graph.h:172
g2o::HyperGraph::Edge::vertices
const VertexContainer & vertices() const
Definition
hyper_graph.h:186
g2o::HyperGraph::Vertex::id
int id() const
returns the id
Definition
hyper_graph.h:154
g2o::OptimizableGraph::Edge
Definition
optimizable_graph.h:417
g2o::OptimizableGraph::Vertex
A general case Vertex for optimization.
Definition
optimizable_graph.h:108
g2o::OptimizableGraph::Vertex::hessianIndex
int hessianIndex() const
Definition
optimizable_graph.h:331
g2o::OptimizableGraph::Vertex::pop
virtual void pop()=0
g2o::OptimizableGraph::Vertex::push
virtual void push()=0
backup the position of the vertex to a stack
g2o::OptimizableGraph::Vertex::setFixed
void setFixed(bool fixed)
true => this node should be considered fixed during the optimization
Definition
optimizable_graph.h:340
g2o::OptimizationAlgorithmWithHessian
Base for solvers operating on the approximated Hessian, e.g., Gauss-Newton, Levenberg.
Definition
optimization_algorithm_with_hessian.h:42
g2o::OptimizationAlgorithmWithHessian::updateLinearSystem
virtual void updateLinearSystem()
Definition
optimization_algorithm_with_hessian.cpp:80
g2o::OptimizationAlgorithmWithHessian::buildLinearStructure
virtual bool buildLinearStructure()
Definition
optimization_algorithm_with_hessian.cpp:76
g2o::OptimizationAlgorithm::init
virtual bool init(bool online=false)=0
g2o::SparseOptimizer
Definition
sparse_optimizer.h:42
g2o::SparseOptimizer::computeActiveErrors
void computeActiveErrors()
Definition
sparse_optimizer.cpp:65
g2o::SparseOptimizer::initializeOptimization
virtual bool initializeOptimization(HyperGraph::EdgeSet &eset)
Definition
sparse_optimizer.cpp:277
g2o::SparseOptimizer::solver
OptimizationAlgorithm * solver()
Definition
sparse_optimizer.h:215
g2o
Definition
dl_wrapper.cpp:55
optimization_algorithm_with_hessian.h
star.h
g2o::EdgeLabeler
Definition
edge_labeler.h:46
g2o::EdgeLabeler::labelEdges
int labelEdges(std::set< OptimizableGraph::Edge * > &edges)
Definition
edge_labeler.cpp:43
g2o::Star::_level
int _level
level of the star
Definition
star.h:83
g2o::Star::labelStarEdges
bool labelStarEdges(int iterations, EdgeLabeler *labeler)
Definition
star.cpp:37
g2o::Star::optimizer
SparseOptimizer * optimizer()
returns the optimizer
Definition
star.h:70
g2o::Star::_optimizer
SparseOptimizer * _optimizer
optimizer
Definition
star.h:85
g2o::Star::_gauge
HyperGraph::VertexSet _gauge
vertices that are fixed (center of the star)
Definition
star.h:93
g2o::Star::_starEdges
HyperGraph::EdgeSet _starEdges
edges in the star
Definition
star.h:89
g2o::Star::level
int level() const
returns the level of the lower edges in the star
Definition
star.h:68
g2o::Star::Star
Star(int level, SparseOptimizer *optimizer)
Definition
star.cpp:34
g2o::Star::_lowLevelEdges
HyperGraph::EdgeSet _lowLevelEdges
edges in the lower level
Definition
star.h:87
Generated on
for g2o by
1.17.0