g2o
Toggle main menu visibility
Loading...
Searching...
No Matches
g2o
examples
g2o_hierarchical
g2o_hierarchical_test_functions.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 <signal.h>
28
29
#include <Eigen/Core>
30
#include <Eigen/Dense>
31
#include <algorithm>
32
#include <cassert>
33
#include <fstream>
34
#include <iomanip>
35
#include <iostream>
36
#include <sstream>
37
#include <string>
38
39
#include "
edge_creator.h
"
40
#include "
edge_labeler.h
"
41
#include "
g2o/apps/g2o_cli/dl_wrapper.h
"
42
#include "
g2o/apps/g2o_cli/g2o_common.h
"
43
#include "
g2o/apps/g2o_cli/output_helper.h
"
44
#include "
g2o/core/estimate_propagator.h
"
45
#include "
g2o/core/factory.h
"
46
#include "
g2o/core/hyper_dijkstra.h
"
47
#include "
g2o/core/optimization_algorithm_factory.h
"
48
#include "
g2o/core/sparse_optimizer.h
"
49
#include "
g2o/stuff/color_macros.h
"
50
#include "
g2o/stuff/command_args.h
"
51
#include "
g2o/stuff/filesys_tools.h
"
52
#include "
g2o/stuff/macros.h
"
53
#include "
g2o/stuff/string_tools.h
"
54
#include "
g2o/stuff/timeutil.h
"
55
#include "
g2o/stuff/unscented.h
"
56
#include "
star.h
"
57
58
using namespace
std
;
59
using namespace
g2o
;
60
using namespace
Eigen
;
61
62
typedef
SigmaPoint<VectorXd>
MySigmaPoint
;
63
64
void
testMarginals
(
SparseOptimizer
& optimizer) {
65
cerr <<
"Projecting marginals"
<< endl;
66
std::vector<std::pair<int, int> > blockIndices;
67
for
(
size_t
i = 0; i < optimizer.
activeVertices
().size(); i++) {
68
OptimizableGraph::Vertex
* v = optimizer.
activeVertices
()[i];
69
if
(v->
hessianIndex
() >= 0) {
70
blockIndices.push_back(make_pair(v->
hessianIndex
(), v->
hessianIndex
()));
71
}
72
// if (v->hessianIndex()>0){
73
// blockIndices.push_back(make_pair(v->hessianIndex()-1,
74
// v->hessianIndex()));
75
// }
76
}
77
SparseBlockMatrix<MatrixXd>
spinv;
78
if
(optimizer.
computeMarginals
(spinv, blockIndices)) {
79
for
(
size_t
i = 0; i < optimizer.
activeVertices
().size(); i++) {
80
OptimizableGraph::Vertex
* v = optimizer.
activeVertices
()[i];
81
cerr <<
"Vertex id:"
<< v->
id
() << endl;
82
if
(v->
hessianIndex
() >= 0) {
83
cerr <<
"increments block :"
<< v->
hessianIndex
() <<
", "
84
<< v->
hessianIndex
() <<
" covariance:"
<< endl;
85
VectorXd mean(
86
v->
minimalEstimateDimension
());
// HACK: need to set identity
87
mean.fill(0);
88
VectorXd oldMean(
89
v->
minimalEstimateDimension
());
// HACK: need to set identity
90
v->
getMinimalEstimateData
(&oldMean[0]);
91
MatrixXd& cov = *(spinv.
block
(v->
hessianIndex
(), v->
hessianIndex
()));
92
std::vector<MySigmaPoint> spts;
93
cerr << cov << endl;
94
if
(!
sampleUnscented
(spts, mean, cov))
continue
;
95
96
// now apply the oplus operator to the sigma points,
97
// and get the points in the global space
98
std::vector<MySigmaPoint> tspts = spts;
99
100
for
(
size_t
j = 0; j < spts.size(); j++) {
101
v->
push
();
102
// cerr << "v_before [" << j << "]" << endl;
103
v->
getMinimalEstimateData
(&mean[0]);
104
// cerr << mean << endl;
105
// cerr << "sigma [" << j << "]" << endl;
106
// cerr << spts[j]._sample << endl;
107
v->
oplus
(&(spts[j]._sample[0]));
108
v->
getMinimalEstimateData
(&mean[0]);
109
tspts[j]._sample = mean;
110
// cerr << "oplus [" << j << "]" << endl;
111
// cerr << tspts[j]._sample << endl;
112
v->
pop
();
113
}
114
MatrixXd cov2 = cov;
115
reconstructGaussian
(mean, cov2, tspts);
116
cerr <<
"global block :"
<< v->
hessianIndex
() <<
", "
117
<< v->
hessianIndex
() << endl;
118
cerr <<
"mean: "
<< endl;
119
cerr << mean << endl;
120
cerr <<
"oldMean: "
<< endl;
121
cerr << oldMean << endl;
122
cerr <<
"cov: "
<< endl;
123
cerr << cov2 << endl;
124
}
125
// if (v->hessianIndex()>0){
126
// cerr << "inv block :" << v->hessianIndex()-1 << ", " <<
127
// v->hessianIndex()<< endl; cerr << *(spinv.block(v->hessianIndex()-1,
128
// v->hessianIndex())); cerr << endl;
129
// }
130
}
131
}
132
}
133
134
int
unscentedTest
() {
135
MatrixXd m = MatrixXd(6, 6);
136
for
(
int
i = 0; i < 6; i++) {
137
for
(
int
j = i; j < 6; j++) {
138
m(i, j) = m(j, i) = i * j + 1;
139
}
140
}
141
m += MatrixXd::Identity(6, 6);
142
cerr << m;
143
VectorXd mean(6);
144
mean.fill(1);
145
146
std::vector<MySigmaPoint> spts;
147
sampleUnscented
(spts, mean, m);
148
for
(
size_t
i = 0; i < spts.size(); i++) {
149
cerr <<
"Point "
<< i <<
" "
<< endl
150
<<
"wi="
<< spts[i]._wi <<
" wp="
<< spts[i]._wp <<
" "
<< endl;
151
cerr << spts[i]._sample << endl;
152
}
153
154
VectorXd recMean(6);
155
MatrixXd recCov(6, 6);
156
157
reconstructGaussian
(recMean, recCov, spts);
158
159
cerr <<
"recMean"
<< endl;
160
cerr << recMean << endl;
161
162
cerr <<
"recCov"
<< endl;
163
cerr << recCov << endl;
164
165
return
0;
166
}
g2o::HyperGraph::Vertex::id
int id() const
returns the id
Definition
hyper_graph.h:154
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::getMinimalEstimateData
virtual bool getMinimalEstimateData(double *estimate) const
Definition
optimizable_graph.cpp:99
g2o::OptimizableGraph::Vertex::minimalEstimateDimension
virtual int minimalEstimateDimension() const
Definition
optimizable_graph.cpp:103
g2o::OptimizableGraph::Vertex::oplus
void oplus(const double *v)
Definition
optimizable_graph.h:324
g2o::SparseBlockMatrix
Sparse matrix which uses blocks.
Definition
sparse_block_matrix.h:64
g2o::SparseBlockMatrix::block
SparseMatrixBlock * block(int r, int c, bool alloc=false)
Definition
sparse_block_matrix.hpp:70
g2o::SparseOptimizer
Definition
sparse_optimizer.h:42
g2o::SparseOptimizer::activeVertices
const VertexContainer & activeVertices() const
the vertices active in the current optimization
Definition
sparse_optimizer.h:188
g2o::SparseOptimizer::computeMarginals
bool computeMarginals(SparseBlockMatrix< MatrixX > &spinv, const std::vector< std::pair< int, int > > &blockIndices)
Definition
sparse_optimizer.cpp:577
color_macros.h
command_args.h
dl_wrapper.h
edge_creator.h
edge_labeler.h
estimate_propagator.h
factory.h
filesys_tools.h
g2o_common.h
testMarginals
void testMarginals(SparseOptimizer &optimizer)
Definition
g2o_hierarchical_test_functions.cpp:64
unscentedTest
int unscentedTest()
Definition
g2o_hierarchical_test_functions.cpp:134
hyper_dijkstra.h
macros.h
Eigen
Definition
jet.h:938
g2o
Definition
dl_wrapper.cpp:55
g2o::reconstructGaussian
void reconstructGaussian(SampleType &mean, CovarianceType &covariance, const std::vector< SigmaPoint< SampleType > > &sigmaPoints)
Definition
unscented.h:77
g2o::sampleUnscented
bool sampleUnscented(std::vector< SigmaPoint< SampleType > > &sigmaPoints, const SampleType &mean, const CovarianceType &covariance)
Definition
unscented.h:48
g2o::MySigmaPoint
SigmaPoint< VectorXd > MySigmaPoint
Definition
edge_labeler.cpp:39
std
Definition
jet.h:876
optimization_algorithm_factory.h
output_helper.h
sparse_optimizer.h
star.h
string_tools.h
g2o::SigmaPoint
Definition
unscented.h:37
timeutil.h
utility functions for handling time related stuff
unscented.h
Generated on
for g2o by
1.17.0