MRPT  1.9.9
CHistogram.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <mrpt/math/math_frwds.h>
12 #include <vector>
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 <
68  typename MAT_VECTOR_LIKE, typename = typename MAT_VECTOR_LIKE::Scalar>
69  inline void add(const MAT_VECTOR_LIKE& x)
70  {
71  const size_t N = x.size();
72  for (size_t i = 0; i < N; i++) this->add(static_cast<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++) this->add(static_cast<double>(x[i]));
81  }
82 
83  /** Retuns the elements count into the selected bin index, where first one
84  * is 0.
85  * \exception std::exception On invalid index
86  */
87  size_t getBinCount(const size_t index) const;
88 
89  /** Retuns the ratio in [0,1] range for the selected bin index, where first
90  * one is 0.
91  * It returns 0 if no elements have been added.
92  * \exception std::exception On invalid index.
93  */
94  double getBinRatio(const size_t index) const;
95 
96  /** Returns the list of bin centers & hit counts
97  * \sa getHistogramNormalized
98  */
99  void getHistogram(std::vector<double>& x, std::vector<double>& hits) const;
100 
101  /** Returns the list of bin centers & hit counts, normalized such as the
102  * integral of the histogram, interpreted as a density PDF, amounts to 1.
103  * \sa getHistogram
104  */
106  std::vector<double>& x, std::vector<double>& hits) const;
107 
108 }; // End of class def.
109 
110 } // namespace mrpt::math
This class provides an easy way of computing histograms for unidimensional real valued variables...
Definition: CHistogram.h:33
double Scalar
Definition: KmUtils.h:43
std::vector< size_t > m_bins
The bins counter.
Definition: CHistogram.h:41
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:4068
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:3542
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
void add(const MAT_VECTOR_LIKE &x)
Add all the elements from a MRPT container to the histogram.
Definition: CHistogram.h:69



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c1796881b Sat Nov 16 19:04:34 2019 +0100 at sáb nov 16 19:15:10 CET 2019