g2o
Loading...
Searching...
No Matches
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
30
31namespace g2o {
32using namespace std;
33
34Star::Star(int level, SparseOptimizer* optimizer)
35 : _level(level), _optimizer(optimizer) {}
36
37bool 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++) {
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) {
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) {
61 // cerr << v->id() << " ";
62 v->setFixed(true);
63 }
64 // cerr << endl;
65 if (iterations > 0) {
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) {
82 cerr << "[" << v->id() << " " << v->hessianIndex() << "] ";
83 }
84 cerr << endl;
85 ok = false;
86 }
87 } else {
89 // cerr << "guess" << endl;
90 // optimizer()->computeInitialGuess();
91 // cerr << "solver init" << endl;
92 optimizer()->solver()->init();
93 // cerr << "structure" << endl;
94 OptimizationAlgorithmWithHessian* solverWithHessian =
96 if (!solverWithHessian->buildLinearStructure())
97 cerr << "FATAL: failure while building linear structure" << endl;
98 // cerr << "errors" << endl;
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) {
117 v->pop();
118 }
119 for (HyperGraph::VertexSet::iterator it = _gauge.begin(); it != _gauge.end();
120 ++it) {
122 v->setFixed(false);
123 }
124
125 return ok;
126}
127
128} // namespace g2o
const VertexContainer & vertices() const
int id() const
returns the id
A general case Vertex for optimization.
virtual void push()=0
backup the position of the vertex to a stack
void setFixed(bool fixed)
true => this node should be considered fixed during the optimization
Base for solvers operating on the approximated Hessian, e.g., Gauss-Newton, Levenberg.
virtual bool init(bool online=false)=0
int optimize(int iterations, bool online=false)
virtual bool initializeOptimization(HyperGraph::EdgeSet &eset)
virtual void computeInitialGuess()
const VertexContainer & indexMapping() const
the index mapping of the vertices
const VertexContainer & activeVertices() const
the vertices active in the current optimization
OptimizationAlgorithm * solver()
Definition jet.h:876
int labelEdges(std::set< OptimizableGraph::Edge * > &edges)
bool labelStarEdges(int iterations, EdgeLabeler *labeler)
Definition star.cpp:37
SparseOptimizer * optimizer()
returns the optimizer
Definition star.h:70
SparseOptimizer * _optimizer
optimizer
Definition star.h:85
HyperGraph::VertexSet _gauge
vertices that are fixed (center of the star)
Definition star.h:93
HyperGraph::EdgeSet _starEdges
edges in the star
Definition star.h:89
Star(int level, SparseOptimizer *optimizer)
Definition star.cpp:34
HyperGraph::EdgeSet _lowLevelEdges
edges in the lower level
Definition star.h:87