g2o
g2o_hierarchical_test_functions.cpp
Go to the documentation of this file.
1 #include <signal.h>
2 #include <iostream>
3 #include <iomanip>
4 #include <string>
5 #include <fstream>
6 #include <sstream>
7 #include <algorithm>
8 #include <cassert>
9 #include <sstream>
13 
16 #include "g2o/core/factory.h"
19 
20 #include "g2o/stuff/macros.h"
21 #include "g2o/stuff/color_macros.h"
22 #include "g2o/stuff/command_args.h"
24 #include "g2o/stuff/string_tools.h"
25 #include "g2o/stuff/timeutil.h"
26 
27 #include "edge_labeler.h"
28 #include "edge_creator.h"
29 #include "star.h"
30 
31 #include "g2o/stuff/unscented.h"
32 #include <Eigen/Core>
33 #include <Eigen/Dense>
34 
35 
36 using namespace std;
37 using namespace g2o;
38 using namespace Eigen;
39 
41 
42 void testMarginals(SparseOptimizer& optimizer){
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 }
108 
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 }
int id() const
returns the id
Definition: hyper_graph.h:148
void testMarginals(SparseOptimizer &optimizer)
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
utility functions for handling time related stuff
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.
SigmaPoint< VectorXd > MySigmaPoint