Main MRPT website > C++ reference for MRPT 1.9.9
CHistogram.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, 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 #ifndef CHISTOGRAM_H
10 #define CHISTOGRAM_H
11 
12 #include <mrpt/utils/core_defs.h>
13 #include <mrpt/utils/types_math.h>
14 #include <vector>
15 
16 namespace mrpt
17 {
18 namespace math
19 {
20 /** This class provides an easy way of computing histograms for unidimensional
21 real valued variables.
22  * Call "getHistogram" or "getHistogramNormalized" to retrieve the full list
23 of bin positions & hit counts.
24  *
25  * Example:
26 \code
27 CHistogram hist(0,100,10);
28 hist.add(86);
29 hist.add(7);
30 hist.add(45);
31 
32 std::cout << hist.getBinCount(0) << std::endl; // Result: "1"
33 std::cout << hist.getBinRatio(0) << std::endl; // Result: "0.33"
34 \endcode
35  * \ingroup mrpt_base_grp
36  */
38 {
39  private:
40  /** The histogram limits */
41  double m_min, m_max;
42  /** ((max-min)/nBins)^-1 */
43  double m_binSizeInv;
44  /** The bins counter */
45  std::vector<size_t> m_bins;
46  /** The total elements count */
47  size_t m_count;
48 
49  public:
50  /** Constructor
51  * \exception std::exception On nBins<=0 or max<=min
52  */
53  CHistogram(const double min, const double max, const size_t nBins);
54 
55  /** Constructor with a fixed bin width.
56  * \exception std::exception On max<=min or width<=0
57  */
59  double min, double max, double binWidth)
60  {
61  ASSERT_(max > min);
62  ASSERT_(binWidth > 0);
63  return CHistogram(
64  min, max, static_cast<size_t>(ceil((max - min) / binWidth)));
65  }
66 
67  /** Clear the histogram:
68  */
69  void clear();
70 
71  /** Add an element to the histogram. If element is out of [min,max] it is
72  * ignored. */
73  void add(const double x);
74 
75  /** Add all the elements from a MRPT container to the histogram. If an
76  * element is out of [min,max] it is ignored. */
77  template <typename Derived>
78  inline void add(const Eigen::MatrixBase<Derived>& x)
79  {
80  const size_t N = x.size();
81  for (size_t i = 0; i < N; i++)
82  this->add(static_cast<const double>(x(i)));
83  }
84 
85  //! \overload
86  template <typename T>
87  inline void add(const std::vector<T>& x)
88  {
89  const size_t N = x.size();
90  for (size_t i = 0; i < N; i++)
91  this->add(static_cast<const double>(x[i]));
92  }
93 
94  /** Retuns the elements count into the selected bin index, where first one
95  * is 0.
96  * \exception std::exception On invalid index
97  */
98  size_t getBinCount(const size_t index) const;
99 
100  /** Retuns the ratio in [0,1] range for the selected bin index, where first
101  * one is 0.
102  * It returns 0 if no elements have been added.
103  * \exception std::exception On invalid index.
104  */
105  double getBinRatio(const size_t index) const;
106 
107  /** Returns the list of bin centers & hit counts
108  * \sa getHistogramNormalized
109  */
110  void getHistogram(std::vector<double>& x, std::vector<double>& hits) const;
111 
112  /** Returns the list of bin centers & hit counts, normalized such as the
113  * integral of the histogram, interpreted as a density PDF, amounts to 1.
114  * \sa getHistogram
115  */
117  std::vector<double>& x, std::vector<double>& hits) const;
118 
119 }; // End of class def.
120 
121 } // End of namespace
122 } // End of namespace
123 #endif
This class provides an easy way of computing histograms for unidimensional real valued variables...
Definition: CHistogram.h:37
#define min(a, b)
std::vector< size_t > m_bins
The bins counter.
Definition: CHistogram.h:45
void add(const Eigen::MatrixBase< Derived > &x)
Add all the elements from a MRPT container to the histogram.
Definition: CHistogram.h:78
CHistogram(const double min, const double max, const size_t nBins)
Constructor.
Definition: CHistogram.cpp:22
void add(const std::vector< T > &x)
Definition: CHistogram.h:87
double m_binSizeInv
((max-min)/nBins)^-1
Definition: CHistogram.h:43
GLuint index
Definition: glext.h:4054
void add(const double x)
Add an element to the histogram.
Definition: CHistogram.cpp:42
double m_min
The histogram limits.
Definition: CHistogram.h:41
void getHistogram(std::vector< double > &x, std::vector< double > &hits) const
Returns the list of bin centers & hit counts.
Definition: CHistogram.cpp:78
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
size_t m_count
The total elements count.
Definition: CHistogram.h:47
size_t getBinCount(const size_t index) const
Retuns the elements count into the selected bin index, where first one is 0.
Definition: CHistogram.cpp:55
#define ASSERT_(f)
double getBinRatio(const size_t index) const
Retuns the ratio in [0,1] range for the selected bin index, where first one is 0. ...
Definition: CHistogram.cpp:65
void clear()
Clear the histogram:
Definition: CHistogram.cpp:33
GLenum GLint x
Definition: glext.h:3538
static CHistogram createWithFixedWidth(double min, double max, double binWidth)
Constructor with a fixed bin width.
Definition: CHistogram.h:58
void getHistogramNormalized(std::vector< double > &x, std::vector< double > &hits) const
Returns the list of bin centers & hit counts, normalized such as the integral of the histogram...
Definition: CHistogram.cpp:93



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019