From d179389424b65ce297b6283c38305b6291dab903 Mon Sep 17 00:00:00 2001 From: maxcorsini Date: Tue, 28 Nov 2006 09:47:42 +0000 Subject: [PATCH] add documentation fix typo --- vcg/math/histogram.h | 159 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 127 insertions(+), 32 deletions(-) diff --git a/vcg/math/histogram.h b/vcg/math/histogram.h index 5392d182..e0995775 100644 --- a/vcg/math/histogram.h +++ b/vcg/math/histogram.h @@ -24,6 +24,9 @@ History $Log: not supported by cvs2svn $ +Revision 1.14 2006/05/04 00:09:53 cignoni +minor change: removed unused vars + Revision 1.13 2006/03/29 09:25:47 zifnab1974 extra includes necessary for compilation of meshlab on AMD 64 with gcc 3.4.5 @@ -74,40 +77,105 @@ Initial Release #include #include #include + + namespace vcg { /** - -*/ + * Histogram. + * + * This class implements a single-value histogram. + */ template -class Histogram { +class Histogram +{ + +// public data members public: - std::vector H; /// counters for bins - std::vector R; /// Range for bins ( + + //! Counters for bins. + std::vector H; + + //! Range for bins. + std::vector R; + + //! Minimum value. ScalarType minv; + + //! Maximum value. ScalarType maxv; - int n; // numero di intervalli - int cnt; // numero di campioni accumulati + + //! Number of intervals. + int n; + + //! Number of accumulated samples. + int cnt; + + //! Average. ScalarType avg; + + //! Root mean square. ScalarType rms; +// public methods +public: + + /** + * Set the histogram values. + * + * This method is used to correctly initialize the bins of the histogram. + */ void SetRange(ScalarType _minv, ScalarType _maxv, int _n); + + /** + * Set the histogram values. + * + * This method is used to correctly initialize the bins of the histogram. + * The \a gamma parameter is applied to modify the ranges of the bins. + */ void SetRange(ScalarType _minv, ScalarType _maxv, int _n, ScalarType gamma); + + /** + * Returns the index of the bin which contains a given value. + */ int Interize(ScalarType val); + + /** + * Add a new value to the histogram. + * + * The statistics related to the histogram data (average, RMS, etc.) are + * also updated. + */ void Add(ScalarType v); + + /** + * Returns the value corresponding to a given percentile of the data. + * + * The percentile range between 0 and 1. + */ ScalarType Percentile(ScalarType frac) const; - ScalarType Avg() { return avg/cnt; } - ScalarType RMS() { return sqrt(rms/double(cnt));} - ScalarType Variance() { return fabs(rms/cnt-Avg()*Avg()); } - ScalarType StandardDeviation() { return sqrt(Variance()); } + + //! Returns the average of the data. + ScalarType Avg(){ return avg/cnt;} + + //! Returns the Root Mean Square of the data. + ScalarType RMS(){ return sqrt(rms/double(cnt));} + + //! Returns the variance of the data. + ScalarType Variance(){ return fabs(rms/cnt-Avg()*Avg());} + + //! Returns the standard deviation of the data. + ScalarType StandardDeviation(){ return sqrt(Variance());} - void FileWrite(const std::string &filename); + //! Dump the histogram to a file. + void FileWrite(const std::string &filename); + //! Reset histogram data. void Clear(); }; template -void Histogram ::Clear() +void Histogram::Clear() { H.clear(); R.clear(); @@ -118,49 +186,63 @@ void Histogram ::Clear() minv=0; maxv=1; } + + template void Histogram::SetRange(ScalarType _minv, ScalarType _maxv, int _n) { + // reset data Clear(); + + // set bins minv=_minv;maxv=_maxv;n=_n; H.resize(n+1); fill(H.begin(),H.end(),0); R.resize(n+1); ScalarType dlt=(maxv-minv)/n; - for(int i=0;i void Histogram::SetRange(ScalarType _minv, ScalarType _maxv, int _n, ScalarType gamma) { + // reset data Clear(); + minv=_minv;maxv=_maxv;n=_n; H.resize(n+1); fill(H.begin(),H.end(),0); R.resize(n+1); + double dlt=(maxv-minv); for(int i=0;i int Histogram::Interize(ScalarType val) { int pos = lower_bound(R.begin(),R.end(),val) - R.begin() - 1; - if(pos>n) pos=n; + if (pos>n) pos=n; return pos; } + template -void Histogram::Add(ScalarType v){ +void Histogram::Add(ScalarType v) +{ int pos= lower_bound(R.begin(),R.end(),v)-R.begin()-1; - if(pos>=0 && pos<=n){ + + if(pos>=0 && pos<=n) + { ++H[pos]; ++cnt; avg+=v; rms += v*v; } - } @@ -169,34 +251,47 @@ void Histogram::FileWrite(const std::string &filename) { FILE *fp; fp=fopen(filename.c_str(),"w"); - for(unsigned int i=0;i ScalarType Histogram::Percentile(ScalarType frac) const { - if(H.size()==0 && R.size()==0) return 0; - assert(frac>=0 && frac<=1); + if(H.size()==0 && R.size()==0) + return 0; + + // check percentile range + assert(frac >= 0 && frac <= 1); + ScalarType sum=0,partsum=0; int isum=0; - int i; - for(i=0;i=sum) break; - } + for(i=0; i=sum) break; + } + return R[i+1]; } -typedef Histogram Histogramd ; -typedef Histogram Histogramf ; +} // end namespace (vcg) -}// end namespace -#endif +#endif /* __VCG_HISTOGRAM */