/********************************************************************* * * * * file: AlgInterpolation.h * * * * copyright: Shanghai Baosight Software Co., Ltd. * * * * author: zoufuzhou * *********************************************************************/ #ifndef _ALG_INTERPOLATION_H #define _ALG_INTERPOLATION_H #include #include #include using namespace std; template 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& 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& iodata,int outsize) { interpolation(iodata,outsize,"neighbor"); } private: static void interpolation(vector& iodata,int outlength,const string& mode) { vector vtemp; vector vpos; double range = (double)outlength/(iodata.size()-1); for(int i=0;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