g2o
Typedefs | Functions
g2o_hierarchical_test_functions.cpp File Reference
#include <signal.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <cassert>
#include "g2o/apps/g2o_cli/dl_wrapper.h"
#include "g2o/apps/g2o_cli/output_helper.h"
#include "g2o/apps/g2o_cli/g2o_common.h"
#include "g2o/core/estimate_propagator.h"
#include "g2o/core/sparse_optimizer.h"
#include "g2o/core/factory.h"
#include "g2o/core/optimization_algorithm_factory.h"
#include "g2o/core/hyper_dijkstra.h"
#include "g2o/stuff/macros.h"
#include "g2o/stuff/color_macros.h"
#include "g2o/stuff/command_args.h"
#include "g2o/stuff/filesys_tools.h"
#include "g2o/stuff/string_tools.h"
#include "g2o/stuff/timeutil.h"
#include "edge_labeler.h"
#include "edge_creator.h"
#include "star.h"
#include "g2o/stuff/unscented.h"
#include <Eigen/Core>
#include <Eigen/Dense>
Include dependency graph for g2o_hierarchical_test_functions.cpp:

Go to the source code of this file.

Typedefs

typedef SigmaPoint< VectorXd > MySigmaPoint
 

Functions

void testMarginals (SparseOptimizer &optimizer)
 
int unscentedTest ()
 

Typedef Documentation

typedef SigmaPoint<VectorXd> MySigmaPoint

Definition at line 40 of file g2o_hierarchical_test_functions.cpp.

Function Documentation

void testMarginals ( SparseOptimizer optimizer)

Definition at line 42 of file g2o_hierarchical_test_functions.cpp.

References g2o::SparseOptimizer::activeVertices(), g2o::SparseBlockMatrix< MatrixType >::block(), g2o::SparseOptimizer::computeMarginals(), g2o::OptimizableGraph::Vertex::getMinimalEstimateData(), g2o::OptimizableGraph::Vertex::hessianIndex(), g2o::HyperGraph::Vertex::id(), g2o::OptimizableGraph::Vertex::minimalEstimateDimension(), g2o::OptimizableGraph::Vertex::oplus(), g2o::OptimizableGraph::Vertex::pop(), g2o::OptimizableGraph::Vertex::push(), g2o::reconstructGaussian(), and g2o::sampleUnscented().

42  {
43  cerr << "Projecting marginals" << endl;
44  std::vector<std::pair<int, int> > blockIndices;
45  for (size_t i=0; i<optimizer.activeVertices().size(); i++) {
46  OptimizableGraph::Vertex* v=optimizer.activeVertices()[i];
47  if (v->hessianIndex()>=0){
48  blockIndices.push_back(make_pair(v->hessianIndex(), v->hessianIndex()));
49  }
50  // if (v->hessianIndex()>0){
51  // blockIndices.push_back(make_pair(v->hessianIndex()-1, v->hessianIndex()));
52  // }
53  }
55  if (optimizer.computeMarginals(spinv, blockIndices)) {
56  for (size_t i=0; i<optimizer.activeVertices().size(); i++) {
57  OptimizableGraph::Vertex* v=optimizer.activeVertices()[i];
58  cerr << "Vertex id:" << v->id() << endl;
59  if (v->hessianIndex()>=0){
60  cerr << "increments block :" << v->hessianIndex() << ", " << v->hessianIndex()<< " covariance:" << endl;
61  VectorXd mean(v->minimalEstimateDimension()); //HACK: need to set identity
62  mean.fill(0);
63  VectorXd oldMean(v->minimalEstimateDimension()); //HACK: need to set identity
64  v->getMinimalEstimateData(&oldMean[0]);
65  MatrixXd& cov= *(spinv.block(v->hessianIndex(), v->hessianIndex()));
66  std::vector<MySigmaPoint, Eigen::aligned_allocator<MySigmaPoint> > spts;
67  cerr << cov << endl;
68  if (! sampleUnscented(spts,mean,cov) )
69  continue;
70 
71  // now apply the oplus operator to the sigma points,
72  // and get the points in the global space
73  std::vector<MySigmaPoint, Eigen::aligned_allocator<MySigmaPoint> > tspts = spts;
74 
75  for (size_t j=0; j<spts.size(); j++) {
76  v->push();
77  // cerr << "v_before [" << j << "]" << endl;
78  v->getMinimalEstimateData(&mean[0]);
79  // cerr << mean << endl;
80  // cerr << "sigma [" << j << "]" << endl;
81  // cerr << spts[j]._sample << endl;
82  v->oplus(&(spts[j]._sample[0]));
83  v->getMinimalEstimateData(&mean[0]);
84  tspts[j]._sample=mean;
85  // cerr << "oplus [" << j << "]" << endl;
86  // cerr << tspts[j]._sample << endl;
87  v->pop();
88  }
89  MatrixXd cov2=cov;
90  reconstructGaussian(mean, cov2, tspts);
91  cerr << "global block :" << v->hessianIndex() << ", " << v->hessianIndex()<< endl;
92  cerr << "mean: " << endl;
93  cerr << mean << endl;
94  cerr << "oldMean: " << endl;
95  cerr << oldMean << endl;
96  cerr << "cov: " << endl;
97  cerr << cov2 << endl;
98 
99  }
100  // if (v->hessianIndex()>0){
101  // cerr << "inv block :" << v->hessianIndex()-1 << ", " << v->hessianIndex()<< endl;
102  // cerr << *(spinv.block(v->hessianIndex()-1, v->hessianIndex()));
103  // cerr << endl;
104  // }
105  }
106  }
107 }
int id() const
returns the id
Definition: hyper_graph.h:148
SparseMatrixBlock * block(int r, int c, bool alloc=false)
returns the block at location r,c. if alloc=true he block is created if it does not exist ...
void reconstructGaussian(SampleType &mean, CovarianceType &covariance, const std::vector< SigmaPoint< SampleType >, Eigen::aligned_allocator< SigmaPoint< SampleType > > > &sigmaPoints)
Definition: unscented.h:78
int hessianIndex() const
temporary index of this node in the parameter vector obtained from linearization
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
virtual int minimalEstimateDimension() const
virtual bool getMinimalEstimateData(double *estimate) const
bool sampleUnscented(std::vector< SigmaPoint< SampleType >, Eigen::aligned_allocator< SigmaPoint< SampleType > > > &sigmaPoints, const SampleType &mean, const CovarianceType &covariance)
Definition: unscented.h:49
A general case Vertex for optimization.
const VertexContainer & activeVertices() const
the vertices active in the current optimization
bool computeMarginals(SparseBlockMatrix< MatrixXD > &spinv, const std::vector< std::pair< int, int > > &blockIndices)
Sparse matrix which uses blocks.
int unscentedTest ( )

Definition at line 109 of file g2o_hierarchical_test_functions.cpp.

References g2o::reconstructGaussian(), and g2o::sampleUnscented().

109  {
110  MatrixXd m=MatrixXd(6,6);
111  for (int i=0; i<6; i++){
112  for (int j=i; j<6; j++){
113  m(i,j)=m(j,i)=i*j+1;
114  }
115  }
116  m+=MatrixXd::Identity(6,6);
117  cerr << m;
118  VectorXd mean(6);
119  mean.fill(1);
120 
121  std::vector<MySigmaPoint, Eigen::aligned_allocator<MySigmaPoint> > spts;
122  sampleUnscented(spts,mean,m);
123  for (size_t i =0; i<spts.size(); i++){
124  cerr << "Point " << i << " " << endl << "wi=" << spts[i]._wi << " wp=" << spts[i]._wp << " " << endl;
125  cerr << spts[i]._sample << endl;
126  }
127 
128  VectorXd recMean(6);
129  MatrixXd recCov(6,6);
130 
131  reconstructGaussian(recMean, recCov, spts);
132 
133  cerr << "recMean" << endl;
134  cerr << recMean << endl;
135 
136  cerr << "recCov" << endl;
137  cerr << recCov << endl;
138 
139  return 0;
140 }
void reconstructGaussian(SampleType &mean, CovarianceType &covariance, const std::vector< SigmaPoint< SampleType >, Eigen::aligned_allocator< SigmaPoint< SampleType > > > &sigmaPoints)
Definition: unscented.h:78
bool sampleUnscented(std::vector< SigmaPoint< SampleType >, Eigen::aligned_allocator< SigmaPoint< SampleType > > > &sigmaPoints, const SampleType &mean, const CovarianceType &covariance)
Definition: unscented.h:49