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



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