g2o
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 <Eigen/Cholesky>
32 #include <Eigen/StdVector>
33 
34 #include <cstdlib>
35 #include <cmath>
36 #include <ctime>
37 
38 #include <random>
39 
40 #include "g2o_stuff_api.h"
41 
42 namespace g2o {
43 
44  double G2O_STUFF_API sampleUniform(double min=0, double max=1, std::mt19937* generator=0);
45  double G2O_STUFF_API sampleGaussian(std::mt19937* generator = 0);
46 
47  template <class SampleType, class CovarianceType>
49  public:
50  GaussianSampler(bool hasGenerator=true) :
51  _generator(0)
52  {
53  if (hasGenerator){
54  _generator = new std::mt19937;
55  }
56  }
58  delete _generator;
59  }
60  void setDistribution(const CovarianceType& cov){
61  Eigen::LLT<CovarianceType> cholDecomp;
62  cholDecomp.compute(cov);
63  if (cholDecomp.info()==Eigen::NumericalIssue)
64  return;
65  _cholesky=cholDecomp.matrixL();
66  }
67  SampleType generateSample() {
68  SampleType s;
69  for (int i=0; i<s.size(); i++){
71  }
72  return _cholesky*s;
73  }
74  protected:
75  CovarianceType _cholesky;
76  std::mt19937* _generator;
77  };
78 
80  {
81  public:
86  static double gaussRand(double mean, double sigma)
87  {
88  double x, y, r2;
89  do {
90  x = -1.0 + 2.0 * uniformRand(0.0, 1.0);
91  y = -1.0 + 2.0 * uniformRand(0.0, 1.0);
92  r2 = x * x + y * y;
93  } while (r2 > 1.0 || r2 == 0.0);
94  return mean + sigma * y * std::sqrt(-2.0 * log(r2) / r2);
95  }
96 
100  static double uniformRand(double lowerBndr, double upperBndr)
101  {
102  return lowerBndr + ((double) std::rand() / (RAND_MAX + 1.0)) * (upperBndr - lowerBndr);
103  }
107  static void seedRand()
108  {
109  seedRand(static_cast<unsigned int>(std::time(NULL)));
110  }
111 
113  static void seedRand(unsigned int seed)
114  {
115  std::srand(seed);
116  }
117  };
118 
119 }
120 
121 #endif
std::mt19937 * _generator
Definition: sampler.h:76
CovarianceType _cholesky
Definition: sampler.h:75
static double gaussRand(double mean, double sigma)
Definition: sampler.h:86
static void seedRand()
Definition: sampler.h:107
static void seedRand(unsigned int seed)
Definition: sampler.h:113
SampleType generateSample()
Definition: sampler.h:67
static double uniformRand(double lowerBndr, double upperBndr)
Definition: sampler.h:100
double sampleGaussian(std::mt19937 *generator)
Definition: sampler.cpp:41
GaussianSampler(bool hasGenerator=true)
Definition: sampler.h:50
#define G2O_STUFF_API
Definition: g2o_stuff_api.h:30
void setDistribution(const CovarianceType &cov)
Definition: sampler.h:60
double sampleUniform(double min, double max, std::mt19937 *generator)
Definition: sampler.cpp:35