eis/inc/zlib/AlgInterpolation.hpp

121 lines
2.6 KiB
C++

/*********************************************************************
* *
* * file: AlgInterpolation.h
* *
* * copyright: Shanghai Baosight Software Co., Ltd.
* *
* * author: zoufuzhou
* *********************************************************************/
#ifndef _ALG_INTERPOLATION_H
#define _ALG_INTERPOLATION_H
#include <string>
#include <iostream>
#include <vector>
using namespace std;
template <typename T> class AlgorithmInterpolation
{
public:
/*
* Insert linear data between two points
* percentage: There is a partial proportion between two points
*/
static T linear(T start,T end,double percentage)
{
T ret;
ret = (start*(1-percentage)+end*percentage);
ret = (T)((long)(ret*1000 +0.5))/1000;
return ret;
}
/*
* Insert linear data proportionally into an array
* outsize: Output the number of data in the array
*/
static void linear(vector<T>& iodata,int outsize)
{
interpolation(iodata,outsize,"linear");
}
/*
* Insert neighbor data between two points
* percentage: There is a partial proportion between two points
*/
static T neighbor(T start,T end,double percentage)
{
if(percentage<0.5)return start;
else return end;
}
/*
* Insert neighbor data proportionally into an array
* outsize: Output the number of data in the array
*/
static void neighbor(vector<T>& iodata,int outsize)
{
interpolation(iodata,outsize,"neighbor");
}
private:
static void interpolation(vector<T>& iodata,int outlength,const string& mode)
{
vector<T> vtemp;
vector<int> vpos;
double range = (double)outlength/(iodata.size()-1);
for(int i=0;i<iodata.size();i++)
{
/*if(i == 0)
{
vpos.push_back(0);
}else*/
{
vpos.push_back(i*range);
}
}
for(int i=0,pos=0; i<outlength;i++)
{
if( i == vpos[pos])
{
vtemp.push_back(iodata[pos]);
if(pos < vpos.size()-1)
{
pos++;
}
continue;
}
else if(i > vpos[pos] && pos < vpos.size()-1 )
{
pos++;
--i;
continue;
}
else
{
double percentage = (double)(i-vpos[pos-1])/(vpos[pos] - vpos[pos-1]);
if(mode == "neighbor")
{
vtemp.push_back(neighbor(iodata[pos-1],iodata[pos],percentage));
}
else
{
vtemp.push_back(linear(iodata[pos-1],iodata[pos],percentage));
}
}
}
iodata.clear();
iodata = vtemp;
vtemp.clear();
vpos.clear();
}
};
#endif