Main MRPT website > C++ reference
MRPT logo
CHistogram.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CHISTOGRAM_H
29 #define CHISTOGRAM_H
30 
31 #include <cmath>
32 #include <mrpt/utils/utils_defs.h>
33 
34 /*---------------------------------------------------------------
35  Class
36  ---------------------------------------------------------------*/
37 namespace mrpt
38 {
39 namespace math
40 {
41  /** This class provides an easy way of computing histograms for unidimensional real valued variables.
42  * Call "getHistogram" or "getHistogramNormalized" to retrieve the full list of bin positions & hit counts.
43  *
44  * Example:
45  \code
46  CHistogram hist(0,100,10);
47  hist.add(86);
48  hist.add(7);
49  hist.add(45);
50 
51  std::cout << hist.getBinCount(0) << std::endl; // Result: "1"
52  std::cout << hist.getBinRatio(0) << std::endl; // Result: "0.33"
53  \endcode
54  * \ingroup mrpt_base_grp
55  */
57  {
58  private:
59  double m_min,m_max; //!< The histogram limits
60  double m_binSizeInv; //!< ((max-min)/nBins)^-1
61  std::vector<size_t> m_bins; //!< The bins counter
62  size_t m_count; //!< The total elements count
63 
64  public:
65  /** Constructor
66  * \exception std::exception On nBins<=0 or max<=min
67  */
68  CHistogram(const double min, const double max, const size_t nBins);
69 
70  /** Constructor with a fixed bin width.
71  * \exception std::exception On max<=min or width<=0
72  */
73  static inline CHistogram createWithFixedWidth(double min,double max,double binWidth) {
74  ASSERT_(max>min);
75  ASSERT_(binWidth>0);
76  return CHistogram(min,max,static_cast<size_t>(ceil((max-min)/binWidth)));
77  }
78 
79  /** Clear the histogram:
80  */
81  void clear();
82 
83  /** Add an element to the histogram. If element is out of [min,max] it is ignored. */
84  void add(const double x);
85 
86  /** Add all the elements from a MRPT container to the histogram. If an element is out of [min,max] it is ignored. */
87  template <typename Derived>
88  inline void add(const Eigen::MatrixBase<Derived> &x)
89  {
90  const size_t N = x.size();
91  for (size_t i=0;i<N;i++)
92  this->add(static_cast<const double>(x(i)));
93  }
94 
95  //! \overload
96  template <typename T>
97  inline void add(const std::vector<T> &x)
98  {
99  const size_t N = x.size();
100  for (size_t i=0;i<N;i++)
101  this->add(static_cast<const double>(x[i]));
102  }
103 
104  /** Retuns the elements count into the selected bin index, where first one is 0.
105  * \exception std::exception On invalid index
106  */
107  int getBinCount(const size_t index) const;
108 
109  /** Retuns the ratio in [0,1] range for the selected bin index, where first one is 0.
110  * It returns 0 if no elements have been added.
111  * \exception std::exception On invalid index.
112  */
113  double getBinRatio(const size_t index) const;
114 
115  /** Returns the list of bin centers & hit counts
116  * \sa getHistogramNormalized
117  */
118  void getHistogram( vector_double &x, vector_double &hits ) const;
119 
120  /** Returns the list of bin centers & hit counts, normalized such as the integral of the histogram, interpreted as a density PDF, amounts to 1.
121  * \sa getHistogram
122  */
123  void getHistogramNormalized( vector_double &x, vector_double &hits ) const;
124 
125 
126  }; // End of class def.
127 
128  } // End of namespace
129 } // End of namespace
130 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013