g2o
Loading...
Searching...
No Matches
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
40namespace g2o {
41
42double G2O_STUFF_API sampleUniform(double min = 0, double max = 1,
43 std::mt19937* generator = 0);
44double G2O_STUFF_API sampleGaussian(std::mt19937* generator = 0);
45
46template <class SampleType, class CovarianceType>
48 public:
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 }
63 SampleType generateSample() {
64 SampleType s;
65 for (int i = 0; i < s.size(); i++) {
67 }
68 return _cholesky * s;
69 }
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
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 }
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
std::unique_ptr< std::mt19937 > _generator
Definition sampler.h:80
GaussianSampler(GaussianSampler const &)=delete
CovarianceType _cholesky
Definition sampler.h:79
GaussianSampler & operator=(const GaussianSampler &)=delete
SampleType generateSample()
return a sample of the Gaussian distribution
Definition sampler.h:63
bool seed(int s)
Definition sampler.h:72
void setDistribution(const CovarianceType &cov)
Definition sampler.h:53
GaussianSampler(bool hasGenerator=true)
Definition sampler.h:51
static void seedRand(unsigned int seed)
Definition sampler.h:114
static void seedRand()
Definition sampler.h:109
static double gaussRand(double mean, double sigma)
Definition sampler.h:89
static double uniformRand(double lowerBndr, double upperBndr)
Definition sampler.h:102
#define G2O_STUFF_API
double sampleUniform(double min, double max, std::mt19937 *generator)
Definition sampler.cpp:35
double sampleGaussian(std::mt19937 *generator)
Definition sampler.cpp:40
Definition jet.h:876