Main MRPT website > C++ reference
MRPT logo

CHistogram.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef  CHISTOGRAM_H
00029 #define  CHISTOGRAM_H
00030 
00031 #include <cmath>
00032 #include <mrpt/utils/utils_defs.h>
00033 
00034 /*---------------------------------------------------------------
00035         Class
00036   ---------------------------------------------------------------*/
00037 namespace mrpt
00038 {
00039 namespace math
00040 {
00041         /** This class provides an easy way of computing histograms for unidimensional real valued variables.
00042          *   Call "getHistogram" or "getHistogramNormalized" to retrieve the full list of bin positions & hit counts.
00043          *
00044          *  Example:
00045         \code
00046         CHistogram              hist(0,100,10);
00047         hist.add(86);
00048         hist.add(7);
00049         hist.add(45);
00050 
00051         std::cout << hist.getBinCount(0) << std::endl;          // Result: "1"
00052         std::cout << hist.getBinRatio(0) << std::endl;          // Result: "0.33"
00053         \endcode
00054          */
00055         class BASE_IMPEXP CHistogram
00056         {
00057         private:
00058                 double  m_min,m_max;            //!< The histogram limits
00059                 double  m_binSizeInv;           //!< ((max-min)/nBins)^-1
00060                 std::vector<size_t>     m_bins; //!< The bins counter
00061                 size_t m_count;                         //!< The total elements count
00062 
00063         public:
00064                 /** Constructor
00065                  * \exception std::exception On nBins<=0 or max<=min
00066                  */
00067                 CHistogram(const double min, const double max, const size_t nBins);
00068 
00069                 /** Constructor with a fixed bin width.
00070                  * \exception std::exception On max<=min or width<=0
00071                  */
00072                 static inline CHistogram createWithFixedWidth(double min,double max,double binWidth)    {
00073                         ASSERT_(max>min);
00074                         ASSERT_(binWidth>0);
00075                         return CHistogram(min,max,static_cast<size_t>(ceil((max-min)/binWidth)));
00076                 }
00077 
00078                 /** Clear the histogram:
00079                  */
00080                 void    clear();
00081 
00082                 /**     Add an element to the histogram. If element is out of [min,max] it is ignored. */
00083                 void    add(const double x);
00084 
00085                 /**     Add all the elements from a MRPT container to the histogram. If an element is out of [min,max] it is ignored. */
00086                 template <typename Derived>
00087                 inline void add(const Eigen::MatrixBase<Derived> &x)
00088                 {
00089                         const size_t N = x.size();
00090                         for (size_t i=0;i<N;i++)
00091                                 this->add(static_cast<const double>(x[i]));
00092                 }
00093 
00094                 //! \overload
00095                 template <typename T>
00096                 inline void add(const std::vector<T> &x)
00097                 {
00098                         const size_t N = x.size();
00099                         for (size_t i=0;i<N;i++)
00100                                 this->add(static_cast<const double>(x[i]));
00101                 }
00102 
00103                 /** Retuns the elements count into the selected bin index, where first one is 0.
00104                  * \exception std::exception On invalid index
00105                  */
00106                 int             getBinCount(const size_t index) const;
00107 
00108                 /** Retuns the ratio in [0,1] range for the selected bin index, where first one is 0.
00109                  *  It returns 0 if no elements have been added.
00110                  * \exception std::exception On invalid index.
00111                  */
00112                 double  getBinRatio(const size_t index) const;
00113 
00114                 /** Returns the list of bin centers & hit counts
00115                   * \sa getHistogramNormalized
00116                   */
00117                 void getHistogram( vector_double &x, vector_double &hits ) const;
00118 
00119                 /** Returns the list of bin centers & hit counts, normalized such as the integral of the histogram, interpreted as a density PDF, amounts to 1.
00120                   * \sa getHistogram
00121                   */
00122                 void getHistogramNormalized( vector_double &x, vector_double &hits ) const;
00123 
00124 
00125         }; // End of class def.
00126 
00127         } // End of namespace
00128 } // End of namespace
00129 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011