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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020