g2o
Loading...
Searching...
No Matches
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
33#include <g2o/core/solver.h>
36#include <g2o/stuff/sampler.h>
37
38#include <iostream>
39
40#include "targetTypes3D.hpp"
41
42using namespace Eigen;
43using namespace std;
44using namespace g2o;
45
46int 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;
55 new OptimizationAlgorithmGaussNewton(std::make_unique<BlockSolver_3_3>(
56 std::make_unique<
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);
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
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}
virtual void setMeasurement(const Measurement &m)
Definition base_edge.h:122
void setInformation(const InformationType &information)
Definition base_edge.h:111
Implementation of a solver operating on the blocks of the Hessian.
void setVertex(size_t i, Vertex *v)
const VertexIDMap & vertices() const
linear solver which uses the sparse Cholesky solver from Eigen
Implementation of the Gauss Newton Algorithm.
Sparse matrix which uses blocks.
SparseMatrixBlock * block(int r, int c, bool alloc=false)
int optimize(int iterations, bool online=false)
void setVerbose(bool verbose)
virtual bool initializeOptimization(HyperGraph::EdgeSet &eset)
void setAlgorithm(OptimizationAlgorithm *algorithm)
bool computeMarginals(SparseBlockMatrix< MatrixX > &spinv, const std::vector< std::pair< int, int > > &blockIndices)
Definition jet.h:938
double sampleUniform(double min, double max, std::mt19937 *generator)
Definition sampler.cpp:35
Definition jet.h:876
int main()
virtual bool addEdge(HyperGraph::Edge *e)
virtual bool addVertex(HyperGraph::Vertex *v, Data *userData)