00001 /* +---------------------------------------------------------------------------+ 00002 | The Mobile Robot Programming Toolkit (MRPT) C++ library | 00003 | | 00004 | http://mrpt.sourceforge.net/ | 00005 | | 00006 | Copyright (C) 2005-2010 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 VECTOR_OR_MAT> 00087 inline RET_VOID_ASSERT_MRPTCONTAINER(VECTOR_OR_MAT) 00088 add(const VECTOR_OR_MAT &x) 00089 { 00090 for (typename VECTOR_OR_MAT::const_iterator it=x.begin();it!=x.end();++it) 00091 this->add(static_cast<const double>(*it)); 00092 } 00093 00094 /** Retuns the elements count into the selected bin index, where first one is 0. 00095 * \exception std::exception On invalid index 00096 */ 00097 int getBinCount(const size_t index) const; 00098 00099 /** Retuns the ratio in [0,1] range for the selected bin index, where first one is 0. 00100 * It returns 0 if no elements have been added. 00101 * \exception std::exception On invalid index. 00102 */ 00103 double getBinRatio(const size_t index) const; 00104 00105 /** Returns the list of bin centers & hit counts 00106 * \sa getHistogramNormalized 00107 */ 00108 void getHistogram( std::vector<double> &x, std::vector<double> &hits ) const; 00109 00110 /** Returns the list of bin centers & hit counts, normalized such as the integral of the histogram, interpreted as a density PDF, amounts to 1. 00111 * \sa getHistogram 00112 */ 00113 void getHistogramNormalized( std::vector<double> &x, std::vector<double> &hits ) const; 00114 00115 00116 }; // End of class def. 00117 00118 } // End of namespace 00119 } // End of namespace 00120 #endif
| Page generated by Doxygen 1.6.1 for MRPT 0.9.0 SVN: at Mon Jun 7 06:47:58 UTC 2010 |
