Main MRPT website > C++ reference for MRPT 1.5.7
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 real valued variables.
21  * Call "getHistogram" or "getHistogramNormalized" to retrieve the full list 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_base_grp
34  */
36  {
37  private:
38  double m_min,m_max; //!< The histogram limits
39  double m_binSizeInv; //!< ((max-min)/nBins)^-1
40  std::vector<size_t> m_bins; //!< The bins counter
41  size_t m_count; //!< The total elements count
42 
43  public:
44  /** Constructor
45  * \exception std::exception On nBins<=0 or max<=min
46  */
47  CHistogram(const double min, const double max, const size_t nBins);
48 
49  /** Constructor with a fixed bin width.
50  * \exception std::exception On max<=min or width<=0
51  */
52  static inline CHistogram createWithFixedWidth(double min,double max,double binWidth) {
53  ASSERT_(max>min);
54  ASSERT_(binWidth>0);
55  return CHistogram(min,max,static_cast<size_t>(ceil((max-min)/binWidth)));
56  }
57 
58  /** Clear the histogram:
59  */
60  void clear();
61 
62  /** Add an element to the histogram. If element is out of [min,max] it is ignored. */
63  void add(const double x);
64 
65  /** Add all the elements from a MRPT container to the histogram. If an element is out of [min,max] it is ignored. */
66  template <typename Derived>
67  inline void add(const Eigen::MatrixBase<Derived> &x)
68  {
69  const size_t N = x.size();
70  for (size_t i=0;i<N;i++)
71  this->add(static_cast<const double>(x(i)));
72  }
73 
74  //! \overload
75  template <typename T>
76  inline void add(const std::vector<T> &x)
77  {
78  const size_t N = x.size();
79  for (size_t i=0;i<N;i++)
80  this->add(static_cast<const double>(x[i]));
81  }
82 
83  /** Retuns the elements count into the selected bin index, where first one is 0.
84  * \exception std::exception On invalid index
85  */
86  size_t getBinCount(const size_t index) const;
87 
88  /** Retuns the ratio in [0,1] range for the selected bin index, where first one is 0.
89  * It returns 0 if no elements have been added.
90  * \exception std::exception On invalid index.
91  */
92  double getBinRatio(const size_t index) const;
93 
94  /** Returns the list of bin centers & hit counts
95  * \sa getHistogramNormalized
96  */
97  void getHistogram( std::vector<double> &x, std::vector<double> &hits ) const;
98 
99  /** Returns the list of bin centers & hit counts, normalized such as the integral of the histogram, interpreted as a density PDF, amounts to 1.
100  * \sa getHistogram
101  */
102  void getHistogramNormalized( std::vector<double> &x, std::vector<double> &hits ) const;
103 
104 
105  }; // End of class def.
106 
107  } // End of namespace
108 } // End of namespace
109 #endif
This class provides an easy way of computing histograms for unidimensional real valued variables...
Definition: CHistogram.h:35
#define min(a, b)
std::vector< size_t > m_bins
The bins counter.
Definition: CHistogram.h:40
void add(const Eigen::MatrixBase< Derived > &x)
Add all the elements from a MRPT container to the histogram.
Definition: CHistogram.h:67
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:113
void add(const std::vector< T > &x)
Definition: CHistogram.h:76
double m_binSizeInv
((max-min)/nBins)^-1
Definition: CHistogram.h:39
GLuint index
Definition: glext.h:3891
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
size_t m_count
The total elements count.
Definition: CHistogram.h:41
#define ASSERT_(f)
GLenum GLint x
Definition: glext.h:3516
static CHistogram createWithFixedWidth(double min, double max, double binWidth)
Constructor with a fixed bin width.
Definition: CHistogram.h:52



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019