diff --git a/vcg/math/histogram.h b/vcg/math/histogram.h new file mode 100644 index 00000000..323b2a18 --- /dev/null +++ b/vcg/math/histogram.h @@ -0,0 +1,118 @@ +/**************************************************************************** +* VCGLib o o * +* Visual and Computer Graphics Library o o * +* _ O _ * +* Copyright(C) 2004 \/)\/ * +* Visual Computing Lab /\/| * +* ISTI - Italian National Research Council | * +* \ * +* All rights reserved. * +* * +* This program is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +/**************************************************************************** + History + +$Log: not supported by cvs2svn $ + +****************************************************************************/ + +#ifndef __VCG_HISTOGRAM +#define __VCG_HISTOGRAM +//#include +namespace vcg { + +/** + +*/ +template +class Histogram { +public: + std::vector H; /// counters for bins + std::vector R; /// Range for bins ( + ScalarType minv; + ScalarType maxv; + int n; // numero di intervalli + int cnt; // numero di campioni accumulati + ScalarType avg; + ScalarType rms; + + void SetRange(ScalarType _minv, ScalarType _maxv, int _n); + void SetRange(ScalarType _minv, ScalarType _maxv, int _n, ScalarType gamma); + int Interize(ScalarType val); + void Add(ScalarType v); + ScalarType Percentile(ScalarType frac) const; + ScalarType Avg(); + ScalarType RMS(); + ScalarType Variance(); + ScalarType StandardDeviation(); + void Clear(); +}; + +template +void Histogram ::Clear() +{ + H.clear(); + R.clear(); + cnt=0; + avg=0; + rms=0; + n=0; + minv=0; + maxv=1; +} +template +void Histogram::SetRange(ScalarType _minv, ScalarType _maxv, int _n) +{ + Clear(); + 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) +{ + Clear(); + minv=_minv;maxv=_maxv;n=_n; + H.resize(n); + 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; + return pos; +} + +template +void Histogram::Add(ScalarType v){ + int pos= lower_bound(R.begin(),R.end(),v)-R.begin()-1; + if(pos<=n){ + ++H[pos]; + ++cnt; + avg+=v; + rms += v*v; + } +} +}// end namespace +#endif \ No newline at end of file