vcglib/vcg/math/histogram.h

305 lines
7.3 KiB
C
Raw Normal View History

2004-06-24 11:12:28 +02:00
/****************************************************************************
* 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 $
Revision 1.15 2006/11/28 09:47:42 corsini
add documentation
fix typo
2006-11-28 10:47:42 +01:00
Revision 1.14 2006/05/04 00:09:53 cignoni
minor change: removed unused vars
2006-05-04 02:09:53 +02:00
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
Revision 1.12 2006/01/12 13:12:54 callieri
in FileWrite, added file closing after finishing
Revision 1.11 2005/09/16 11:51:23 cignoni
removed signed/unsigned warning
2005-09-16 13:51:23 +02:00
Revision 1.10 2005/06/17 00:54:55 cignoni
Corrected small bug in SetRange (H was resized to n instead of n+1)
Revision 1.9 2005/06/14 14:27:00 ganovelli
added include of algorithm
2005-06-14 16:27:00 +02:00
Revision 1.8 2005/06/10 14:59:39 cignoni
Added include assert.h and cast to ScalarType for a pow in SetRange() function.
Revision 1.7 2005/06/09 14:19:55 cignoni
Added typedef Histogramf and Histogramd
Revision 1.6 2005/06/07 09:37:33 ponchio
Added fabs() to variance, which can sometime be negative in case
of rounding errors (and sqrt chokes on it).
Revision 1.5 2005/06/07 07:44:08 cignoni
Added Percentile and removed small bug in Add
Revision 1.4 2005/04/04 10:48:35 cignoni
Added missing functions Avg, rms etc, now fully (almost) functional
Revision 1.3 2005/03/14 09:23:40 cignoni
Added missing include<vector>
2005-03-14 10:23:40 +01:00
Revision 1.2 2004/08/25 15:15:26 ganovelli
minor changes to comply gcc compiler (typename's and stuff)
Revision 1.1 2004/06/24 09:12:28 cignoni
Initial Release
2004-06-24 11:12:28 +02:00
****************************************************************************/
#ifndef __VCG_HISTOGRAM
#define __VCG_HISTOGRAM
2005-03-14 10:23:40 +01:00
#include <vector>
2005-06-14 16:27:00 +02:00
#include <algorithm>
#include <assert.h>
#include <string>
2006-11-28 10:47:42 +01:00
2004-06-24 11:12:28 +02:00
namespace vcg {
/**
2006-11-28 10:47:42 +01:00
* Histogram.
*
* This class implements a single-value histogram.
*/
2004-06-24 11:12:28 +02:00
template <class ScalarType>
2006-11-28 10:47:42 +01:00
class Histogram
{
// public data members
2004-06-24 11:12:28 +02:00
public:
2006-11-28 10:47:42 +01:00
//! Counters for bins.
std::vector <int> H;
//! Range for bins.
std::vector <ScalarType> R;
//! Minimum value.
2004-06-24 11:12:28 +02:00
ScalarType minv;
2006-11-28 10:47:42 +01:00
//! Maximum value.
2004-06-24 11:12:28 +02:00
ScalarType maxv;
2006-11-28 10:47:42 +01:00
//! Number of intervals.
int n;
//! Number of accumulated samples.
int cnt;
//! Average.
2004-06-24 11:12:28 +02:00
ScalarType avg;
2006-11-28 10:47:42 +01:00
//! Root mean square.
2004-06-24 11:12:28 +02:00
ScalarType rms;
2006-11-28 10:47:42 +01:00
// public methods
public:
/**
* Set the histogram values.
*
* This method is used to correctly initialize the bins of the histogram.
*/
2004-06-24 11:12:28 +02:00
void SetRange(ScalarType _minv, ScalarType _maxv, int _n);
2006-11-28 10:47:42 +01:00
/**
* 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.
*/
2004-06-24 11:12:28 +02:00
void SetRange(ScalarType _minv, ScalarType _maxv, int _n, ScalarType gamma);
2006-11-28 10:47:42 +01:00
/**
* Returns the index of the bin which contains a given value.
*/
2004-06-24 11:12:28 +02:00
int Interize(ScalarType val);
2006-11-28 10:47:42 +01:00
/**
* Add a new value to the histogram.
*
* The statistics related to the histogram data (average, RMS, etc.) are
* also updated.
*/
2004-06-24 11:12:28 +02:00
void Add(ScalarType v);
2006-11-28 10:47:42 +01:00
/**
* Returns the value corresponding to a given percentile of the data.
*
* The percentile range between 0 and 1.
*/
2004-06-24 11:12:28 +02:00
ScalarType Percentile(ScalarType frac) const;
2006-11-28 10:47:42 +01:00
//! 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());}
2006-11-28 10:47:42 +01:00
//! Dump the histogram to a file.
void FileWrite(const std::string &filename);
2006-11-28 10:47:42 +01:00
//! Reset histogram data.
2004-06-24 11:12:28 +02:00
void Clear();
};
template <class ScalarType>
2006-11-28 10:47:42 +01:00
void Histogram<ScalarType>::Clear()
2004-06-24 11:12:28 +02:00
{
H.clear();
R.clear();
cnt=0;
avg=0;
rms=0;
n=0;
minv=0;
maxv=1;
}
2006-11-28 10:47:42 +01:00
2004-06-24 11:12:28 +02:00
template <class ScalarType>
void Histogram<ScalarType>::SetRange(ScalarType _minv, ScalarType _maxv, int _n)
{
2006-11-28 10:47:42 +01:00
// reset data
2004-06-24 11:12:28 +02:00
Clear();
2006-11-28 10:47:42 +01:00
// set bins
2004-06-24 11:12:28 +02:00
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;
2006-11-28 10:47:42 +01:00
for(int i=0; i<n+1; ++i)
2004-06-24 11:12:28 +02:00
R[i]=minv+dlt*i;
}
2006-11-28 10:47:42 +01:00
2004-06-24 11:12:28 +02:00
template <class ScalarType>
void Histogram<ScalarType>::SetRange(ScalarType _minv, ScalarType _maxv, int _n, ScalarType gamma)
{
2006-11-28 10:47:42 +01:00
// reset data
2004-06-24 11:12:28 +02:00
Clear();
2006-11-28 10:47:42 +01:00
2004-06-24 11:12:28 +02:00
minv=_minv;maxv=_maxv;n=_n;
H.resize(n+1);
2004-06-24 11:12:28 +02:00
fill(H.begin(),H.end(),0);
R.resize(n+1);
2006-11-28 10:47:42 +01:00
2004-06-24 11:12:28 +02:00
double dlt=(maxv-minv);
for(int i=0;i<n+1;++i)
R[i]=minv+dlt*pow(ScalarType(i)/n,gamma);
2004-06-24 11:12:28 +02:00
}
2006-11-28 10:47:42 +01:00
2004-06-24 11:12:28 +02:00
template <class ScalarType>
int Histogram<ScalarType>::Interize(ScalarType val)
{
int pos = lower_bound(R.begin(),R.end(),val) - R.begin() - 1;
2006-11-28 10:47:42 +01:00
if (pos>n) pos=n;
2004-06-24 11:12:28 +02:00
return pos;
}
2006-11-28 10:47:42 +01:00
2004-06-24 11:12:28 +02:00
template <class ScalarType>
2006-11-28 10:47:42 +01:00
void Histogram<ScalarType>::Add(ScalarType v)
{
2004-06-24 11:12:28 +02:00
int pos= lower_bound(R.begin(),R.end(),v)-R.begin()-1;
2006-11-28 10:47:42 +01:00
if(pos>=0 && pos<=n)
{
2004-06-24 11:12:28 +02:00
++H[pos];
++cnt;
avg+=v;
rms += v*v;
}
}
template <class ScalarType>
void Histogram<ScalarType>::FileWrite(const std::string &filename)
{
FILE *fp;
fp=fopen(filename.c_str(),"w");
2006-11-28 10:47:42 +01:00
for(unsigned int i=0; i<H.size(); i++)
fprintf (fp,"%12.8lf , %12.8lf \n",R[i],double(H[i])/cnt);
2006-11-28 10:47:42 +01:00
fclose(fp);
}
2006-11-28 10:47:42 +01:00
template <class ScalarType>
ScalarType Histogram<ScalarType>::Percentile(ScalarType frac) const
{
2006-11-28 10:47:42 +01:00
if(H.size()==0 && R.size()==0)
return 0;
// check percentile range
assert(frac >= 0 && frac <= 1);
ScalarType sum=0,partsum=0;
2006-05-04 02:09:53 +02:00
int isum=0;
2006-11-28 10:47:42 +01:00
int i;
for(i=0;i<n+1;i++)
{
sum+=H[i];
isum+=H[i];
}
2006-11-28 10:47:42 +01:00
// check
assert(isum==cnt);
assert(sum==cnt);
sum*=frac;
2006-11-28 10:47:42 +01:00
for(i=0; i<n; i++)
{
partsum+=H[i];
if(partsum>=sum) break;
}
return R[i+1];
}
typedef Histogram<double> Histogramd ;
typedef Histogram<float> Histogramf ;
2006-11-28 10:47:42 +01:00
} // end namespace (vcg)
2006-11-28 10:47:42 +01:00
#endif /* __VCG_HISTOGRAM */