Main MRPT website > C++ reference for MRPT 1.4.0
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-2016, 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  int 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
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,...
mrpt::math::CHistogram
This class provides an easy way of computing histograms for unidimensional real valued variables.
Definition: CHistogram.h:36
mrpt::math::CHistogram::add
void add(const double x)
Add an element to the histogram.
mrpt::math::CHistogram::getBinCount
int getBinCount(const size_t index) const
Retuns the elements count into the selected bin index, where first one is 0.
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CParticleFilter.h:17
mrpt::math::CHistogram::m_count
size_t m_count
The total elements count.
Definition: CHistogram.h:41
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.
core_defs.h
mrpt::math::CHistogram::clear
void clear()
Clear the histogram:
mrpt::math::CHistogram::createWithFixedWidth
static CHistogram createWithFixedWidth(double min, double max, double binWidth)
Constructor with a fixed bin width.
Definition: CHistogram.h:52
mrpt::math::CHistogram::getHistogram
void getHistogram(std::vector< double > &x, std::vector< double > &hits) const
Returns the list of bin centers & hit counts.
mrpt::math::CHistogram::CHistogram
CHistogram(const double min, const double max, const size_t nBins)
Constructor.
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:67
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:76
mrpt::math::CHistogram::m_min
double m_min
Definition: CHistogram.h:38
ASSERT_
#define ASSERT_(f)
Definition: mrpt_macros.h:261
mrpt::math::CHistogram::m_binSizeInv
double m_binSizeInv
((max-min)/nBins)^-1
Definition: CHistogram.h:39
types_math.h
mrpt::math::CHistogram::m_bins
std::vector< size_t > m_bins
The bins counter.
Definition: CHistogram.h:40



Page generated by Doxygen 1.8.20 for MRPT 1.4.0 SVN: at Thu Aug 27 02:40:23 UTC 2020