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 $
|
2008-03-06 11:45:39 +01:00
|
|
|
Revision 1.18 2008/03/05 11:21:49 cignoni
|
|
|
|
Heavily refactored the whole structure.
|
|
|
|
Some interfaces have been changed. Be careful.
|
|
|
|
|
2008-03-05 12:21:49 +01:00
|
|
|
Revision 1.17 2008/02/29 12:15:06 cignoni
|
|
|
|
added maxcount
|
|
|
|
|
2008-02-29 13:15:06 +01:00
|
|
|
Revision 1.16 2006/11/28 21:29:21 cignoni
|
|
|
|
Re added typedef Histogramf and Histogramd
|
|
|
|
|
2006-11-28 22:29:21 +01:00
|
|
|
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
|
|
|
|
|
2006-03-29 11:25:49 +02:00
|
|
|
Revision 1.12 2006/01/12 13:12:54 callieri
|
|
|
|
in FileWrite, added file closing after finishing
|
|
|
|
|
2006-01-12 14:12:54 +01:00
|
|
|
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)
|
|
|
|
|
2005-06-17 02:54:55 +02:00
|
|
|
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.
|
|
|
|
|
2005-06-10 16:59:39 +02:00
|
|
|
Revision 1.7 2005/06/09 14:19:55 cignoni
|
|
|
|
Added typedef Histogramf and Histogramd
|
|
|
|
|
2005-06-09 16:19:55 +02:00
|
|
|
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).
|
|
|
|
|
2005-06-07 11:37:33 +02:00
|
|
|
Revision 1.5 2005/06/07 07:44:08 cignoni
|
|
|
|
Added Percentile and removed small bug in Add
|
|
|
|
|
2005-06-07 09:44:08 +02:00
|
|
|
Revision 1.4 2005/04/04 10:48:35 cignoni
|
|
|
|
Added missing functions Avg, rms etc, now fully (almost) functional
|
|
|
|
|
2005-04-04 12:48:35 +02:00
|
|
|
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)
|
|
|
|
|
2004-08-25 17:15:27 +02:00
|
|
|
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>
|
2005-06-10 16:59:39 +02:00
|
|
|
#include <assert.h>
|
2006-03-29 11:25:49 +02:00
|
|
|
#include <string>
|
2008-03-06 11:45:39 +01:00
|
|
|
#include <limits>
|
2009-01-19 00:56:28 +01:00
|
|
|
#include <vcg/math/base.h>
|
2009-07-17 19:42:51 +02:00
|
|
|
#include <stdio.h>
|
2006-11-28 10:47:42 +01:00
|
|
|
|
2004-06-24 11:12:28 +02:00
|
|
|
namespace vcg {
|
|
|
|
|
2009-01-16 00:54:00 +01:00
|
|
|
template <class ScalarType>
|
|
|
|
class Distribution
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::vector<ScalarType> vec;
|
2010-01-11 23:57:32 +01:00
|
|
|
bool dirty;
|
|
|
|
double valSum;
|
|
|
|
double sqrdValSum;
|
2009-01-16 00:54:00 +01:00
|
|
|
double avg;
|
2010-01-11 23:57:32 +01:00
|
|
|
double sqrdAvg;
|
2009-01-16 00:54:00 +01:00
|
|
|
double rms;
|
|
|
|
double min_v;
|
|
|
|
double max_v;
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Distribution() { Clear(); }
|
|
|
|
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
vec.clear();
|
|
|
|
dirty=true;
|
|
|
|
min_v = std::numeric_limits<float>::max();
|
|
|
|
max_v = -std::numeric_limits<float>::max();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Add(const ScalarType v)
|
|
|
|
{
|
|
|
|
vec.push_back(v);
|
|
|
|
dirty=true;
|
|
|
|
if(v<min_v) min_v=v;
|
|
|
|
if(v>max_v) max_v=v;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScalarType Min() { return min_v; }
|
|
|
|
ScalarType Max() { return max_v; }
|
|
|
|
|
|
|
|
ScalarType Avg(){ DirtyCheck(); return avg;}
|
|
|
|
//! Returns the Root Mean Square of the data.
|
|
|
|
ScalarType RMS(){ DirtyCheck(); return rms;}
|
|
|
|
|
|
|
|
//! Returns the variance of the data.
|
2010-01-11 23:57:32 +01:00
|
|
|
// the average of the squares less the square of the average.
|
|
|
|
ScalarType Variance(){ DirtyCheck(); return sqrdAvg - avg*avg ;}
|
2009-01-16 00:54:00 +01:00
|
|
|
|
|
|
|
//! Returns the standard deviation of the data.
|
2010-01-11 23:57:32 +01:00
|
|
|
ScalarType StandardDeviation(){ DirtyCheck(); return sqrt( Variance() );}
|
2009-01-16 00:54:00 +01:00
|
|
|
void DirtyCheck()
|
|
|
|
{
|
|
|
|
if(!dirty) return;
|
|
|
|
std::sort(vec.begin(),vec.end());
|
2010-01-11 23:57:32 +01:00
|
|
|
valSum=0;
|
|
|
|
sqrdValSum=0;
|
2009-01-16 00:54:00 +01:00
|
|
|
typename std::vector<ScalarType>::iterator vi;
|
|
|
|
for(vi=vec.begin();vi!=vec.end();++vi)
|
|
|
|
{
|
2010-01-11 23:57:32 +01:00
|
|
|
valSum += double(*vi);
|
|
|
|
sqrdValSum += double(*vi)*double(*vi);
|
2009-01-16 00:54:00 +01:00
|
|
|
}
|
2010-01-11 23:57:32 +01:00
|
|
|
avg = valSum/double(vec.size());
|
|
|
|
sqrdAvg = sqrdValSum/double(vec.size());
|
|
|
|
rms = math::Sqrt(sqrdAvg);
|
2009-01-16 00:54:00 +01:00
|
|
|
dirty=false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScalarType Percentile(ScalarType perc)
|
|
|
|
{
|
|
|
|
assert(perc>=0 && perc<=1);
|
|
|
|
DirtyCheck();
|
|
|
|
int index = vec.size() *perc -1;
|
|
|
|
|
|
|
|
if(index< 0 ) index = 0;
|
|
|
|
|
|
|
|
return vec[index];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-06-24 11:12:28 +02:00
|
|
|
/**
|
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
|
2010-09-13 11:42:25 +02:00
|
|
|
protected:
|
2006-11-28 10:47:42 +01:00
|
|
|
|
2010-09-22 00:33:37 +02:00
|
|
|
std::vector <ScalarType> H; //! Counters for bins.
|
2008-03-05 12:21:49 +01:00
|
|
|
std::vector <ScalarType> R; //! Range for bins.
|
|
|
|
ScalarType minv; //! Minimum value.
|
|
|
|
ScalarType maxv; //! Maximum value.
|
2010-10-01 10:59:25 +02:00
|
|
|
ScalarType minElem; //! Minimum value.
|
|
|
|
ScalarType maxElem; //! Maximum value.
|
|
|
|
int n; //! Number of vaild intervals stored between minv and maxv.
|
2006-11-28 10:47:42 +01:00
|
|
|
|
|
|
|
|
2008-03-05 12:21:49 +01:00
|
|
|
/// incrementally updated values
|
2010-09-22 00:33:37 +02:00
|
|
|
ScalarType cnt; //! Number of accumulated samples.
|
2008-03-05 12:21:49 +01:00
|
|
|
ScalarType avg; //! Average.
|
|
|
|
ScalarType rms; //! Root mean square.
|
2006-11-28 10:47:42 +01:00
|
|
|
|
2008-03-05 12:21:49 +01:00
|
|
|
/**
|
|
|
|
* Returns the index of the bin which contains a given value.
|
|
|
|
*/
|
|
|
|
int BinIndex(ScalarType val) ;
|
2004-06-24 11:12:28 +02:00
|
|
|
|
2006-11-28 10:47:42 +01:00
|
|
|
// public methods
|
|
|
|
public:
|
2008-03-05 12:21:49 +01:00
|
|
|
|
2006-11-28 10:47:42 +01:00
|
|
|
/**
|
|
|
|
* Set the histogram values.
|
|
|
|
*
|
|
|
|
* This method is used to correctly initialize the bins of the histogram.
|
2008-03-05 12:21:49 +01:00
|
|
|
* n is the number of valid intervals between minv and maxv.
|
|
|
|
* for a more robust working, the Histogram class stores also the two out of range intervals (-inf, minv] and [maxv, +inf)
|
|
|
|
* Each bin is left closed (eg it contains the value
|
|
|
|
* The \a gamma parameter is applied to modify the distribution of the ranges of the bins. Default uniform distibution.
|
|
|
|
*
|
2006-11-28 10:47:42 +01:00
|
|
|
*/
|
2008-03-05 12:21:49 +01:00
|
|
|
void SetRange(ScalarType _minv, ScalarType _maxv, int _n,ScalarType gamma=1.0 );
|
2006-11-28 10:47:42 +01:00
|
|
|
|
2008-03-05 12:21:49 +01:00
|
|
|
ScalarType MinV() {return minv;}; //! Minimum value.
|
2010-10-01 10:59:25 +02:00
|
|
|
ScalarType MaxV() {return maxv;}; //! Maximum value.
|
2006-11-28 10:47:42 +01:00
|
|
|
|
2008-03-05 12:21:49 +01:00
|
|
|
|
2010-10-01 10:59:25 +02:00
|
|
|
ScalarType MinElem() {return minElem;}; //! Minimum element added to the histogram. It could be < or > than MinV;.
|
|
|
|
ScalarType MaxElem() {return maxElem;}; //! Maximum element added to the histogram. It could be < or > than MinV;..
|
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.
|
|
|
|
*/
|
2010-09-22 00:33:37 +02:00
|
|
|
void Add(ScalarType v, ScalarType increment=ScalarType(1.0));
|
2006-11-28 10:47:42 +01:00
|
|
|
|
2010-10-01 10:59:25 +02:00
|
|
|
ScalarType MaxCount() const;
|
2010-09-13 11:42:25 +02:00
|
|
|
int BinNum() const {return n;};
|
2010-10-01 10:59:25 +02:00
|
|
|
ScalarType BinCount(ScalarType v);
|
|
|
|
ScalarType BinCountInd(int index) {return H[index];}
|
|
|
|
ScalarType BinCount(ScalarType v, ScalarType width);
|
2010-01-11 23:57:32 +01:00
|
|
|
ScalarType BinLowerBound(int index) {return R[index];}
|
|
|
|
ScalarType BinUpperBound(int index) {return R[index+1];};
|
2010-10-01 10:59:25 +02:00
|
|
|
ScalarType RangeCount(ScalarType rangeMin, ScalarType rangeMax);
|
2008-03-05 12:21:49 +01:00
|
|
|
ScalarType BinWidth(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());}
|
2005-04-04 12:48:35 +02:00
|
|
|
|
2006-11-28 10:47:42 +01:00
|
|
|
//! Dump the histogram to a file.
|
|
|
|
void FileWrite(const std::string &filename);
|
2005-04-04 12:48:35 +02:00
|
|
|
|
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;
|
2010-10-01 10:59:25 +02:00
|
|
|
minElem = std::numeric_limits<ScalarType>::max();
|
|
|
|
maxElem = -std::numeric_limits<ScalarType>::max();
|
2004-06-24 11:12:28 +02:00
|
|
|
}
|
2006-11-28 10:47:42 +01:00
|
|
|
|
2008-03-05 12:21:49 +01:00
|
|
|
/*
|
|
|
|
Note that the histogram holds <n> valid bins plus two semi-infinite bins.
|
2006-11-28 10:47:42 +01:00
|
|
|
|
2008-03-05 12:21:49 +01:00
|
|
|
R[0] = -inf
|
|
|
|
R[1] = minv
|
|
|
|
R[n+1] = maxv
|
|
|
|
R[n+2] = +inf
|
2006-11-28 10:47:42 +01:00
|
|
|
|
2004-06-24 11:12:28 +02:00
|
|
|
|
2008-03-05 12:21:49 +01:00
|
|
|
Eg. SetRange(0, 10, 5) asks for 5 intervals covering the 0..10 range
|
|
|
|
|
|
|
|
H[0] H[1] H[2] H[3] H[4] H[5] H[6]
|
|
|
|
-inf 0 2 4 6 8 10 +inf
|
|
|
|
R[0] R[1] R[2] R[3] R[4] R[5] R[6] R[7]
|
|
|
|
|
|
|
|
*/
|
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;
|
2008-03-05 12:21:49 +01:00
|
|
|
H.resize(n+2);
|
2004-06-24 11:12:28 +02:00
|
|
|
fill(H.begin(),H.end(),0);
|
2008-03-05 12:21:49 +01:00
|
|
|
R.resize(n+3);
|
2006-11-28 10:47:42 +01:00
|
|
|
|
2008-03-05 12:21:49 +01:00
|
|
|
R[0] = - std::numeric_limits< ScalarType >::max();
|
|
|
|
R[n+2] = std::numeric_limits< ScalarType >::max();
|
|
|
|
|
|
|
|
double delta=(maxv-minv);
|
|
|
|
if(gamma==1)
|
|
|
|
{
|
|
|
|
for(int i=0; i<=n; ++i)
|
|
|
|
R[i+1] = minv + delta*ScalarType(i)/n;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for(int i=0; i<=n; ++i)
|
|
|
|
R[i+1] = minv + delta*pow(ScalarType(i)/n,gamma);
|
|
|
|
}
|
2004-06-24 11:12:28 +02:00
|
|
|
}
|
2006-11-28 10:47:42 +01:00
|
|
|
|
|
|
|
|
2008-03-05 12:21:49 +01:00
|
|
|
|
2004-06-24 11:12:28 +02:00
|
|
|
template <class ScalarType>
|
2008-03-05 12:21:49 +01:00
|
|
|
int Histogram<ScalarType>::BinIndex(ScalarType val)
|
2004-06-24 11:12:28 +02:00
|
|
|
{
|
2008-03-05 12:21:49 +01:00
|
|
|
// lower_bound returns the furthermost iterator i in [first, last) such that, for every iterator j in [first, i), *j < value.
|
|
|
|
// E.g. An iterator pointing to the first element "not less than" val, or end() if every element is less than val.
|
|
|
|
typename std::vector<ScalarType>::iterator it = lower_bound(R.begin(),R.end(),val);
|
|
|
|
|
|
|
|
assert(it!=R.begin());
|
|
|
|
assert(it!=R.end());
|
|
|
|
assert((*it)>=val);
|
|
|
|
|
|
|
|
int pos = it-R.begin();
|
|
|
|
assert(pos >=1);
|
|
|
|
pos -= 1;
|
|
|
|
assert (R[pos] < val);
|
|
|
|
assert ( val <= R[pos+1] );
|
2004-06-24 11:12:28 +02:00
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2008-03-05 12:21:49 +01:00
|
|
|
/*
|
|
|
|
H[0] H[1] H[2] H[3] H[4] H[5] H[6]
|
|
|
|
-inf 0 2 4 6 8 10 +inf
|
|
|
|
R[0] R[1] R[2] R[3] R[4] R[5] R[6] R[7]
|
|
|
|
|
|
|
|
asking for 3.14 lower bound will return an iterator pointing to R[3]==4; and will increase H[2]
|
|
|
|
asking for 4 lower bound will return an iterator pointing to R[3]==4; and will increase H[2]
|
2006-11-28 10:47:42 +01:00
|
|
|
|
2008-03-05 12:21:49 +01:00
|
|
|
*/
|
2004-06-24 11:12:28 +02:00
|
|
|
template <class ScalarType>
|
2010-09-22 00:33:37 +02:00
|
|
|
void Histogram<ScalarType>::Add(ScalarType v, ScalarType increment)
|
2006-11-28 10:47:42 +01:00
|
|
|
{
|
2008-03-05 12:21:49 +01:00
|
|
|
int pos=BinIndex(v);
|
2010-10-01 10:59:25 +02:00
|
|
|
if(v<minElem) minElem=v;
|
|
|
|
if(v>maxElem) maxElem=v;
|
2008-03-05 12:21:49 +01:00
|
|
|
if(pos>=0 && pos<=n)
|
2006-11-28 10:47:42 +01:00
|
|
|
{
|
2010-10-01 10:59:25 +02:00
|
|
|
|
2010-09-22 00:33:37 +02:00
|
|
|
H[pos]+=increment;
|
|
|
|
cnt+=increment;
|
|
|
|
avg+=v*increment;
|
|
|
|
rms += (v*v)*increment;
|
2004-06-24 11:12:28 +02:00
|
|
|
}
|
|
|
|
}
|
2005-04-04 12:48:35 +02:00
|
|
|
|
2008-03-05 12:21:49 +01:00
|
|
|
template <class ScalarType>
|
2010-10-01 10:59:25 +02:00
|
|
|
ScalarType Histogram<ScalarType>::BinCount(ScalarType v)
|
2008-03-05 12:21:49 +01:00
|
|
|
{
|
|
|
|
return H[BinIndex(v)];
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ScalarType>
|
2010-10-01 10:59:25 +02:00
|
|
|
ScalarType Histogram<ScalarType>::BinCount(ScalarType v, ScalarType width)
|
2008-03-05 12:21:49 +01:00
|
|
|
{
|
|
|
|
return RangeCount(v-width/2.0,v+width/2.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ScalarType>
|
2010-10-01 10:59:25 +02:00
|
|
|
ScalarType Histogram<ScalarType>::RangeCount(ScalarType rangeMin, ScalarType rangeMax)
|
2008-03-05 12:21:49 +01:00
|
|
|
{
|
|
|
|
int firstBin=BinIndex(rangeMin);
|
|
|
|
int lastBin=BinIndex (rangeMax);
|
2010-10-01 10:59:25 +02:00
|
|
|
ScalarType sum=0;
|
2008-03-05 12:21:49 +01:00
|
|
|
for(int i=firstBin; i<=lastBin;++i)
|
|
|
|
sum+=H[i];
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ScalarType>
|
|
|
|
ScalarType Histogram<ScalarType>::BinWidth(ScalarType v)
|
|
|
|
{
|
|
|
|
int pos=BinIndex(v);
|
|
|
|
return R[pos+1]-R[pos];
|
|
|
|
}
|
2005-04-04 12:48:35 +02:00
|
|
|
|
|
|
|
template <class ScalarType>
|
2006-01-12 14:12:54 +01:00
|
|
|
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++)
|
2006-01-12 14:12:54 +01:00
|
|
|
fprintf (fp,"%12.8lf , %12.8lf \n",R[i],double(H[i])/cnt);
|
2006-11-28 10:47:42 +01:00
|
|
|
|
2006-01-12 14:12:54 +01:00
|
|
|
fclose(fp);
|
2005-04-04 12:48:35 +02:00
|
|
|
}
|
2005-06-07 09:44:08 +02:00
|
|
|
|
2006-11-28 10:47:42 +01:00
|
|
|
|
2008-02-29 13:15:06 +01:00
|
|
|
template <class ScalarType>
|
2010-10-01 10:59:25 +02:00
|
|
|
ScalarType Histogram<ScalarType>::MaxCount() const
|
2008-02-29 13:15:06 +01:00
|
|
|
{
|
|
|
|
return *(std::max_element(H.begin(),H.end()));
|
|
|
|
}
|
|
|
|
|
2008-03-05 12:21:49 +01:00
|
|
|
// Return the scalar value <r> such that there are <frac> samples <= <r>.
|
|
|
|
// E.g. Percentile(0.0) will return R[1] e.g. min value
|
|
|
|
// E.g. Percentile(1.0) will return R[n+1] e.g max value
|
2008-02-29 13:15:06 +01:00
|
|
|
|
2005-06-07 09:44:08 +02: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);
|
|
|
|
|
2005-06-07 09:44:08 +02:00
|
|
|
ScalarType sum=0,partsum=0;
|
2009-06-12 01:55:06 +02:00
|
|
|
size_t i;
|
2006-11-28 10:47:42 +01:00
|
|
|
|
2008-03-05 12:21:49 +01:00
|
|
|
// useless summation just to be sure
|
|
|
|
for(i=0;i<H.size();i++) sum+=H[i];
|
2005-06-07 09:44:08 +02:00
|
|
|
assert(sum==cnt);
|
|
|
|
|
|
|
|
sum*=frac;
|
2008-03-05 12:21:49 +01:00
|
|
|
for(i=0; i<H.size(); i++)
|
2006-11-28 10:47:42 +01:00
|
|
|
{
|
|
|
|
partsum+=H[i];
|
|
|
|
if(partsum>=sum) break;
|
|
|
|
}
|
|
|
|
|
2008-03-05 12:21:49 +01:00
|
|
|
assert(i<H.size());
|
|
|
|
|
2005-06-07 09:44:08 +02:00
|
|
|
return R[i+1];
|
|
|
|
}
|
|
|
|
|
2006-11-28 22:29:21 +01:00
|
|
|
typedef Histogram<double> Histogramd ;
|
|
|
|
typedef Histogram<float> Histogramf ;
|
|
|
|
|
2006-11-28 10:47:42 +01:00
|
|
|
} // end namespace (vcg)
|
2005-06-07 09:44:08 +02:00
|
|
|
|
2006-11-28 10:47:42 +01:00
|
|
|
#endif /* __VCG_HISTOGRAM */
|