g2o
Toggle main menu visibility
Loading...
Searching...
No Matches
g2o
examples
target
static_target.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
// This example consists of a single static target which sits in one
28
// place and does not move; in effect it has a "GPS" which measures
29
// its position
30
31
#include <
g2o/core/block_solver.h
>
32
#include <
g2o/core/optimization_algorithm_gauss_newton.h
>
33
#include <
g2o/core/solver.h
>
34
#include <
g2o/core/sparse_optimizer.h
>
35
#include <
g2o/solvers/eigen/linear_solver_eigen.h
>
36
#include <
g2o/stuff/sampler.h
>
37
38
#include <iostream>
39
40
#include "
targetTypes3D.hpp
"
41
42
using namespace
Eigen
;
43
using namespace
std
;
44
using namespace
g2o
;
45
46
int
main
() {
47
// Set up the optimiser
48
SparseOptimizer
optimizer;
49
optimizer.
setVerbose
(
false
);
50
51
// Create the block solver - the dimensions are specified because
52
// 3D observations marginalise to a 3D estimate
53
typedef
BlockSolver<BlockSolverTraits<3, 3>
> BlockSolver_3_3;
54
OptimizationAlgorithmGaussNewton
* solver =
55
new
OptimizationAlgorithmGaussNewton
(std::make_unique<BlockSolver_3_3>(
56
std::make_unique<
57
LinearSolverEigen<BlockSolver_3_3::PoseMatrixType>
>()));
58
59
optimizer.
setAlgorithm
(solver);
60
61
// Sample the actual location of the target
62
Vector3d truePoint(
sampleUniform
(-500, 500),
sampleUniform
(-500, 500),
63
sampleUniform
(-500, 500));
64
65
// Construct vertex which corresponds to the actual point of the target
66
VertexPosition3D
* position =
new
VertexPosition3D
();
67
position->
setId
(0);
68
optimizer.
addVertex
(position);
69
70
// Now generate some noise corrupted measurements; for simplicity
71
// these are uniformly distributed about the true target. These are
72
// modelled as a unary edge because they do not like to, say,
73
// another node in the map.
74
int
numMeasurements = 10;
75
double
noiseLimit = sqrt(12.);
76
double
noiseSigma = noiseLimit * noiseLimit / 12.0;
77
78
for
(
int
i = 0; i < numMeasurements; i++) {
79
Vector3d measurement =
80
truePoint + Vector3d(
sampleUniform
(-0.5, 0.5) * noiseLimit,
81
sampleUniform
(-0.5, 0.5) * noiseLimit,
82
sampleUniform
(-0.5, 0.5) * noiseLimit);
83
GPSObservationPosition3DEdge
* goe =
new
GPSObservationPosition3DEdge
();
84
goe->
setVertex
(0, position);
85
goe->
setMeasurement
(measurement);
86
goe->
setInformation
(Matrix3d::Identity() / noiseSigma);
87
optimizer.
addEdge
(goe);
88
}
89
90
// Configure and set things going
91
optimizer.
initializeOptimization
();
92
optimizer.
setVerbose
(
true
);
93
optimizer.
optimize
(5);
94
95
cout <<
"truePoint=\n"
<< truePoint << endl;
96
cerr <<
"computed estimate=\n"
97
<<
dynamic_cast<
VertexPosition3D
*
>
(optimizer.
vertices
().find(0)->second)
98
->
estimate
()
99
<< endl;
100
101
// position->setMarginalized(true);
102
103
SparseBlockMatrix<MatrixXd>
spinv;
104
bool
state = optimizer.
computeMarginals
(spinv, position);
105
if
(state) {
106
cout <<
"covariance\n"
<< spinv << endl;
107
cout << spinv.
block
(0, 0) << endl;
108
}
109
110
return
0;
111
}
block_solver.h
GPSObservationPosition3DEdge
Definition
targetTypes3D.hpp:51
VertexPosition3D
Definition
targetTypes3D.hpp:12
g2o::BaseEdge::setMeasurement
virtual void setMeasurement(const Measurement &m)
Definition
base_edge.h:122
g2o::BaseEdge::setInformation
void setInformation(const InformationType &information)
Definition
base_edge.h:111
g2o::BaseVertex::estimate
const EstimateType & estimate() const
return the current estimate of the vertex
Definition
base_vertex.h:127
g2o::BlockSolver
Implementation of a solver operating on the blocks of the Hessian.
Definition
block_solver.h:103
g2o::HyperGraph::Edge::setVertex
void setVertex(size_t i, Vertex *v)
Definition
hyper_graph.h:209
g2o::HyperGraph::vertices
const VertexIDMap & vertices() const
Definition
hyper_graph.h:248
g2o::LinearSolverEigen
linear solver which uses the sparse Cholesky solver from Eigen
Definition
linear_solver_eigen.h:51
g2o::OptimizableGraph::Vertex::setId
virtual void setId(int id)
Definition
optimizable_graph.h:352
g2o::OptimizationAlgorithmGaussNewton
Implementation of the Gauss Newton Algorithm.
Definition
optimization_algorithm_gauss_newton.h:41
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::optimize
int optimize(int iterations, bool online=false)
Definition
sparse_optimizer.cpp:381
g2o::SparseOptimizer::setVerbose
void setVerbose(bool verbose)
Definition
sparse_optimizer.cpp:562
g2o::SparseOptimizer::initializeOptimization
virtual bool initializeOptimization(HyperGraph::EdgeSet &eset)
Definition
sparse_optimizer.cpp:277
g2o::SparseOptimizer::setAlgorithm
void setAlgorithm(OptimizationAlgorithm *algorithm)
Definition
sparse_optimizer.cpp:564
g2o::SparseOptimizer::computeMarginals
bool computeMarginals(SparseBlockMatrix< MatrixX > &spinv, const std::vector< std::pair< int, int > > &blockIndices)
Definition
sparse_optimizer.cpp:577
linear_solver_eigen.h
Eigen
Definition
jet.h:938
g2o
Definition
dl_wrapper.cpp:55
g2o::sampleUniform
double sampleUniform(double min, double max, std::mt19937 *generator)
Definition
sampler.cpp:35
std
Definition
jet.h:876
optimization_algorithm_gauss_newton.h
sampler.h
solver.h
sparse_optimizer.h
main
int main()
Definition
static_target.cpp:46
g2o::OptimizableGraph::addEdge
virtual bool addEdge(HyperGraph::Edge *e)
Definition
optimizable_graph.cpp:252
g2o::OptimizableGraph::addVertex
virtual bool addVertex(HyperGraph::Vertex *v, Data *userData)
Definition
optimizable_graph.cpp:212
targetTypes3D.hpp
Generated on
for g2o by
1.17.0