Main MRPT website > C++ reference for MRPT 1.9.9
test.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <mrpt/system/CTicTac.h>
11 #include <mrpt/bayes.h>
12 #include <mrpt/poses/CPose2D.h>
13 #include <mrpt/poses/CPoint2D.h>
14 #include <mrpt/random.h>
15 #include <mrpt/system/os.h>
16 #include <iostream>
17 
18 using namespace mrpt;
19 using namespace mrpt::bayes;
20 using namespace mrpt::poses;
21 using namespace mrpt::random;
22 using namespace mrpt::system;
23 
24 double SIGMA = 0.05;
25 
26 // The custom class:
27 class CMyRejectionSampling : public CRejectionSamplingCapable<CPose2D>
28 {
29  protected:
30  void RS_drawFromProposal(CPose2D& outSample)
31  {
32  double ang = getRandomGenerator().drawUniform(-M_PI, M_PI);
33  double R = getRandomGenerator().drawGaussian1D(1.0, SIGMA);
34  outSample.x(1.0f - cos(ang) * R);
35  outSample.y(sin(ang) * R);
36  outSample.phi(getRandomGenerator().drawUniform(-M_PI, M_PI));
37  }
38 
39  /** Returns the NORMALIZED observation likelihood at a given point of the
40  * state space (values in the range [0,1]).
41  */
42  double RS_observationLikelihood(const CPose2D& x)
43  {
44  return exp(
45  -0.5 * square((x.distanceTo(CPoint2D(0, 0)) - 1.0f) / SIGMA));
46  }
47 };
48 
49 // ------------------------------------------------------
50 // TestRS
51 // ------------------------------------------------------
52 void TestRS()
53 {
55  std::vector<CMyRejectionSampling::TParticle> samples;
56  CTicTac tictac;
57 
58  tictac.Tic();
59  printf("Computing...");
60 
61  RS.rejectionSampling(1000, samples);
62 
63  printf("Ok! %fms\n", 1000 * tictac.Tac());
64 
65  FILE* f = os::fopen("_out_samples.txt", "wt");
67  for (it = samples.begin(); it != samples.end(); it++)
69  f, "%f %f %f %e\n", it->d->x(), it->d->y(), it->d->phi(),
70  it->log_w);
71 
72  os::fclose(f);
73 }
74 
75 // ------------------------------------------------------
76 // MAIN
77 // ------------------------------------------------------
78 int main()
79 {
80  try
81  {
82  TestRS();
83 
84  return 0;
85  }
86  catch (std::exception& e)
87  {
88  std::cout << "EXCEPCTION: " << e.what() << std::endl;
89  return -1;
90  }
91  catch (...)
92  {
93  printf("Untyped excepcion!!");
94  return -1;
95  }
96 }
os.h
CPoint2D.h
samples
GLsizei samples
Definition: glext.h:8068
mrpt::system::os::fclose
int void fclose(FILE *f)
An OS-independent version of fclose.
Definition: os.cpp:273
mrpt::system::CTicTac
A high-performance stopwatch, with typical resolution of nanoseconds.
Definition: system/CTicTac.h:19
CMyRejectionSampling
Definition: vision_stereo_rectify/test.cpp:27
mrpt::bayes::CRejectionSamplingCapable
A base class for implementing rejection sampling in a generic state space.
Definition: CRejectionSamplingCapable.h:30
mrpt::poses::CPose2D::phi
const double & phi() const
Get the phi angle of the 2D pose (in radians)
Definition: CPose2D.h:82
SIGMA
double SIGMA
Definition: vision_stereo_rectify/test.cpp:24
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
R
const float R
Definition: CKinematicChain.cpp:138
mrpt::square
T square(const T x)
Inline function for the square of a number.
Definition: core/include/mrpt/core/bits_math.h:18
mrpt::poses
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CHierarchicalMapMHPartition.h:25
CPose2D.h
TestRS
void TestRS()
Definition: vision_stereo_rectify/test.cpp:52
random.h
mrpt::system::CTicTac::Tac
double Tac() noexcept
Stops the stopwatch.
Definition: CTicTac.cpp:90
main
int main()
Definition: vision_stereo_rectify/test.cpp:78
M_PI
#define M_PI
Definition: core/include/mrpt/core/bits_math.h:38
mrpt::poses::CPoseOrPoint::y
double y() const
Definition: CPoseOrPoint.h:144
mrpt::random::CRandomGenerator::drawUniform
double drawUniform(const double Min, const double Max)
Generate a uniformly distributed pseudo-random number using the MT19937 algorithm,...
Definition: RandomGenerators.h:111
mrpt::poses::CPose2D
A class used to store a 2D pose, including the 2D coordinate point and a heading (phi) angle.
Definition: CPose2D.h:40
mrpt::poses::CPoseOrPoint::x
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:140
mrpt::system::CTicTac::Tic
void Tic() noexcept
Starts the stopwatch.
Definition: CTicTac.cpp:79
mrpt::system::os::fprintf
int fprintf(FILE *fil, const char *format,...) noexcept MRPT_printf_format_check(2
An OS-independent version of fprintf.
Definition: os.cpp:406
mrpt::random::getRandomGenerator
CRandomGenerator & getRandomGenerator()
A static instance of a CRandomGenerator class, for use in single-thread applications.
Definition: RandomGenerator.cpp:19
CTicTac.h
mrpt::bayes
The namespace for Bayesian filtering algorithm: different particle filters and Kalman filter algorith...
Definition: CKalmanFilterCapable.h:32
mrpt::bayes::CRejectionSamplingCapable::rejectionSampling
void rejectionSampling(size_t desiredSamples, std::vector< TParticle > &outSamples, size_t timeoutTrials=1000)
Generates a set of N independent samples via rejection sampling.
Definition: CRejectionSamplingCapable.h:49
mrpt::system::os::fopen
FILE * fopen(const char *fileName, const char *mode) noexcept
An OS-independent version of fopen.
Definition: os.cpp:255
mrpt::random
A namespace of pseudo-random numbers generators of diferent distributions.
Definition: random_shuffle.h:17
bayes.h
iterator
Scalar * iterator
Definition: eigen_plugins.h:26
mrpt::random::CRandomGenerator::drawGaussian1D
double drawGaussian1D(const double mean, const double std)
Generate a normally distributed pseudo-random number.
Definition: RandomGenerators.h:162
mrpt::poses::CPoint2D
A class used to store a 2D point.
Definition: CPoint2D.h:35
x
GLenum GLint x
Definition: glext.h:3538
mrpt::system
This namespace provides a OS-independent interface to many useful functions: filenames manipulation,...
Definition: math_frwds.h:25



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST