g2o
star.cpp
Go to the documentation of this file.
1 #include "star.h"
3 
4 namespace g2o {
5  using namespace std;
6 
7  Star::Star(int level, SparseOptimizer* optimizer): _level(level), _optimizer(optimizer) {}
8 
9  bool Star::labelStarEdges(int iterations, EdgeLabeler* labeler){
10  // mark all vertices in the lowLevelEdges as floating
11  bool ok=true;
12  std::set<OptimizableGraph::Vertex*> vset;
13  for (HyperGraph::EdgeSet::iterator it=_lowLevelEdges.begin(); it!=_lowLevelEdges.end(); it++){
14  HyperGraph::Edge* e=*it;
15  for (size_t i=0; i<e->vertices().size(); i++){
17  v->setFixed(false);
18  vset.insert(v);
19  }
20  }
21  for (std::set<OptimizableGraph::Vertex*>::iterator it=vset.begin(); it!=vset.end(); it++){
22  OptimizableGraph::Vertex* v = *it;
23  v->push();
24  }
25 
26  // fix all vertices in the gauge
27  //cerr << "fixing gauge: ";
28  for (HyperGraph::VertexSet::iterator it = _gauge.begin(); it!=_gauge.end(); it++){
30  //cerr << v->id() << " ";
31  v->setFixed(true);
32  }
33  //cerr << endl;
34  if (iterations>0){
37  int result=_optimizer->optimize(iterations);
38  if (result<1){
39  cerr << "Vertices num: " << _optimizer->activeVertices().size() << "ids: ";
40  for (size_t i=0; i<_optimizer->indexMapping().size(); i++){
41  cerr << _optimizer->indexMapping()[i]->id() << " " ;
42  }
43  cerr << endl;
44  cerr << "!!! optimization failure" << endl;
45  cerr << "star size=" << _lowLevelEdges.size() << endl;
46  cerr << "gauge: ";
47  for (HyperGraph::VertexSet::iterator it=_gauge.begin(); it!=_gauge.end(); it++){
49  cerr << "[" << v->id() << " " << v->hessianIndex() << "] ";
50  }
51  cerr << endl;
52  ok=false;
53  }
54  } else {
56  // cerr << "guess" << endl;
57  //optimizer()->computeInitialGuess();
58  // cerr << "solver init" << endl;
59  optimizer()->solver()->init();
60  // cerr << "structure" << endl;
61  OptimizationAlgorithmWithHessian* solverWithHessian = dynamic_cast<OptimizationAlgorithmWithHessian*> (optimizer()->solver());
62  if (!solverWithHessian->buildLinearStructure())
63  cerr << "FATAL: failure while building linear structure" << endl;
64  // cerr << "errors" << endl;
66  // cerr << "system" << endl;
67  solverWithHessian->updateLinearSystem();
68  }
69 
70  std::set<OptimizableGraph::Edge*> star;
71  for(HyperGraph::EdgeSet::iterator it=_starEdges.begin(); it!=_starEdges.end(); it++){
72  star.insert((OptimizableGraph::Edge*)*it);
73  }
74  if (ok) {
75  int result = labeler->labelEdges(star);
76  if (result < 0)
77  ok=false;
78  }
79  // release all vertices in the gauge
80  for (std::set<OptimizableGraph::Vertex*>::iterator it=vset.begin(); it!=vset.end(); it++){
81  OptimizableGraph::Vertex* v = *it;
82  v->pop();
83  }
84  for (HyperGraph::VertexSet::iterator it=_gauge.begin(); it!=_gauge.end(); it++){
86  v->setFixed(false);
87  }
88 
89  return ok;
90  }
91 
92 }
virtual void computeInitialGuess()
int labelEdges(std::set< OptimizableGraph::Edge * > &edges)
int id() const
returns the id
Definition: hyper_graph.h:148
SparseOptimizer * optimizer()
returns the optimizer
Definition: star.h:41
HyperGraph::EdgeSet _lowLevelEdges
edges in the lower level
Definition: star.h:58
int optimize(int iterations, bool online=false)
virtual bool init(bool online=false)=0
OptimizationAlgorithm * solver()
int hessianIndex() const
temporary index of this node in the parameter vector obtained from linearization
Base for solvers operating on the approximated Hessian, e.g., Gauss-Newton, Levenberg.
Star(int level, SparseOptimizer *optimizer)
Definition: star.cpp:7
bool labelStarEdges(int iterations, EdgeLabeler *labeler)
Definition: star.cpp:9
HyperGraph::EdgeSet _starEdges
edges in the star
Definition: star.h:60
const VertexContainer & vertices() const
Definition: hyper_graph.h:178
const VertexContainer & indexMapping() const
the index mapping of the vertices
virtual void push()=0
backup the position of the vertex to a stack
virtual void pop()=0
restore the position of the vertex by retrieving the position from the stack
HyperGraph::VertexSet _gauge
vertices that are fixed (center of the star)
Definition: star.h:64
A general case Vertex for optimization.
const VertexContainer & activeVertices() const
the vertices active in the current optimization
SparseOptimizer * _optimizer
optimizer
Definition: star.h:56
void setFixed(bool fixed)
true => this node should be considered fixed during the optimization
virtual bool initializeOptimization(HyperGraph::EdgeSet &eset)