g2o
Toggle main menu visibility
Loading...
Searching...
No Matches
g2o
stuff
sampler.h
Go to the documentation of this file.
1
// g2o - General Graph Optimization
2
// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, 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
#ifndef G2O_GAUSSIAN_SAMPLER_
28
#define G2O_GAUSSIAN_SAMPLER_
29
30
#include <Eigen/Core>
31
#include <cassert>
32
#include <cmath>
33
#include <cstdlib>
34
#include <ctime>
35
#include <memory>
36
#include <random>
37
38
#include "
g2o_stuff_api.h
"
39
40
namespace
g2o
{
41
42
double
G2O_STUFF_API
sampleUniform
(
double
min = 0,
double
max = 1,
43
std::mt19937* generator = 0);
44
double
G2O_STUFF_API
sampleGaussian
(std::mt19937* generator = 0);
45
46
template
<
class
SampleType,
class
CovarianceType>
47
class
GaussianSampler
{
48
public
:
49
GaussianSampler
(
GaussianSampler
const
&) =
delete
;
50
GaussianSampler
&
operator=
(
const
GaussianSampler
&) =
delete
;
51
GaussianSampler
(
bool
hasGenerator =
true
)
52
:
_generator
(hasGenerator ? new
std
::mt19937 : nullptr) {}
53
void
setDistribution
(
const
CovarianceType& cov) {
54
Eigen::LLT<CovarianceType> cholDecomp;
55
cholDecomp.compute(cov);
56
if
(cholDecomp.info() == Eigen::NumericalIssue) {
57
assert(
false
&&
"Cholesky decomposition on the covariance matrix failed"
);
58
return
;
59
}
60
_cholesky
= cholDecomp.matrixL();
61
}
62
63
SampleType
generateSample
() {
64
SampleType s;
65
for
(
int
i = 0; i < s.size(); i++) {
66
s(i) = (
_generator
) ?
sampleGaussian
(
_generator
.get()) :
sampleGaussian
();
67
}
68
return
_cholesky
* s;
69
}
70
72
bool
seed
(
int
s) {
73
if
(!
_generator
)
return
false
;
74
_generator
->seed(s);
75
return
true
;
76
}
77
78
protected
:
79
CovarianceType
_cholesky
;
80
std::unique_ptr<std::mt19937>
_generator
;
81
};
82
83
class
G2O_STUFF_API
Sampler
{
84
public
:
89
static
double
gaussRand
(
double
mean,
double
sigma) {
90
double
y, r2;
91
do
{
92
double
x = -1.0 + 2.0 *
uniformRand
(0.0, 1.0);
93
y = -1.0 + 2.0 *
uniformRand
(0.0, 1.0);
94
r2 = x * x + y * y;
95
}
while
(r2 > 1.0 || r2 == 0.0);
96
return
mean + sigma * y * std::sqrt(-2.0 * log(r2) / r2);
97
}
98
102
static
double
uniformRand
(
double
lowerBndr,
double
upperBndr) {
103
return
lowerBndr +
104
((double)std::rand() / (RAND_MAX + 1.0)) * (upperBndr - lowerBndr);
105
}
106
109
static
void
seedRand
() {
110
seedRand
(
static_cast<
unsigned
int
>
(std::time(NULL)));
111
}
112
114
static
void
seedRand
(
unsigned
int
seed) { std::srand(seed); }
115
};
116
117
}
// namespace g2o
118
119
#endif
g2o::GaussianSampler::_generator
std::unique_ptr< std::mt19937 > _generator
Definition
sampler.h:80
g2o::GaussianSampler::GaussianSampler
GaussianSampler(GaussianSampler const &)=delete
g2o::GaussianSampler::_cholesky
CovarianceType _cholesky
Definition
sampler.h:79
g2o::GaussianSampler::operator=
GaussianSampler & operator=(const GaussianSampler &)=delete
g2o::GaussianSampler::generateSample
SampleType generateSample()
return a sample of the Gaussian distribution
Definition
sampler.h:63
g2o::GaussianSampler::seed
bool seed(int s)
Definition
sampler.h:72
g2o::GaussianSampler::setDistribution
void setDistribution(const CovarianceType &cov)
Definition
sampler.h:53
g2o::GaussianSampler::GaussianSampler
GaussianSampler(bool hasGenerator=true)
Definition
sampler.h:51
g2o::Sampler
Definition
sampler.h:83
g2o::Sampler::seedRand
static void seedRand(unsigned int seed)
Definition
sampler.h:114
g2o::Sampler::seedRand
static void seedRand()
Definition
sampler.h:109
g2o::Sampler::gaussRand
static double gaussRand(double mean, double sigma)
Definition
sampler.h:89
g2o::Sampler::uniformRand
static double uniformRand(double lowerBndr, double upperBndr)
Definition
sampler.h:102
g2o_stuff_api.h
G2O_STUFF_API
#define G2O_STUFF_API
Definition
g2o_stuff_api.h:56
g2o
Definition
dl_wrapper.cpp:55
g2o::sampleUniform
double sampleUniform(double min, double max, std::mt19937 *generator)
Definition
sampler.cpp:35
g2o::sampleGaussian
double sampleGaussian(std::mt19937 *generator)
Definition
sampler.cpp:40
std
Definition
jet.h:876
Generated on
for g2o by
1.17.0