00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://www.mrpt.org/ | 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 * \ingroup mrpt_base_grp 00055 */ 00056 class BASE_IMPEXP CHistogram 00057 { 00058 private: 00059 double m_min,m_max; //!< The histogram limits 00060 double m_binSizeInv; //!< ((max-min)/nBins)^-1 00061 std::vector<size_t> m_bins; //!< The bins counter 00062 size_t m_count; //!< The total elements count 00063 00064 public: 00065 /** Constructor 00066 * \exception std::exception On nBins<=0 or max<=min 00067 */ 00068 CHistogram(const double min, const double max, const size_t nBins); 00069 00070 /** Constructor with a fixed bin width. 00071 * \exception std::exception On max<=min or width<=0 00072 */ 00073 static inline CHistogram createWithFixedWidth(double min,double max,double binWidth) { 00074 ASSERT_(max>min); 00075 ASSERT_(binWidth>0); 00076 return CHistogram(min,max,static_cast<size_t>(ceil((max-min)/binWidth))); 00077 } 00078 00079 /** Clear the histogram: 00080 */ 00081 void clear(); 00082 00083 /** Add an element to the histogram. If element is out of [min,max] it is ignored. */ 00084 void add(const double x); 00085 00086 /** Add all the elements from a MRPT container to the histogram. If an element is out of [min,max] it is ignored. */ 00087 template <typename Derived> 00088 inline void add(const Eigen::MatrixBase<Derived> &x) 00089 { 00090 const size_t N = x.size(); 00091 for (size_t i=0;i<N;i++) 00092 this->add(static_cast<const double>(x(i))); 00093 } 00094 00095 //! \overload 00096 template <typename T> 00097 inline void add(const std::vector<T> &x) 00098 { 00099 const size_t N = x.size(); 00100 for (size_t i=0;i<N;i++) 00101 this->add(static_cast<const double>(x[i])); 00102 } 00103 00104 /** Retuns the elements count into the selected bin index, where first one is 0. 00105 * \exception std::exception On invalid index 00106 */ 00107 int getBinCount(const size_t index) const; 00108 00109 /** Retuns the ratio in [0,1] range for the selected bin index, where first one is 0. 00110 * It returns 0 if no elements have been added. 00111 * \exception std::exception On invalid index. 00112 */ 00113 double getBinRatio(const size_t index) const; 00114 00115 /** Returns the list of bin centers & hit counts 00116 * \sa getHistogramNormalized 00117 */ 00118 void getHistogram( vector_double &x, vector_double &hits ) const; 00119 00120 /** Returns the list of bin centers & hit counts, normalized such as the integral of the histogram, interpreted as a density PDF, amounts to 1. 00121 * \sa getHistogram 00122 */ 00123 void getHistogramNormalized( vector_double &x, vector_double &hits ) const; 00124 00125 00126 }; // End of class def. 00127 00128 } // End of namespace 00129 } // End of namespace 00130 #endif
| Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011 |