CachedFunction1D.h

00001 #ifndef CACHEDFUNCTION1D_H
00002 #define CACHEDFUNCTION1D_H
00003 
00004 #include <iostream>
00005 #include <cmath>
00006 
00007 namespace SctData{
00008 
00015     class CachedFunction1D{
00016     public:
00026     CachedFunction1D(double min, double max, double delta, double (*function)(double) ) throw() ;
00030     ~CachedFunction1D() throw() ;
00038     inline double eval(double x) const throw() ;
00039     private:
00041     CachedFunction1D() throw() ;
00043     CachedFunction1D& operator=(const CachedFunction1D&) throw() ;
00044     void makeTable() throw();    
00045     double min;                  
00046     double max;                  
00047     double delta;                
00048     double invdelta;             
00049     int nvalue;                  
00050     double * values;             
00051     double (*function) (double); 
00052     };
00053     
00054 // inline function
00055     double CachedFunction1D::eval(double x) const throw() {
00056     /*
00057     int ipos=(int)((x-min)*invdelta);
00058     if (ipos<0)            { return values[0];        }
00059     else if (ipos>=nvalue) { return values[nvalue-1]; }
00060     else                   { return values[ipos];     }
00061     */
00062     double pos = (x-min)*invdelta;
00063     int ipos=(int)(pos);
00064     if (ipos<0)            { return values[0];        }
00065     else if (ipos>=nvalue-1) { return values[nvalue-1]; }
00066     //interpolate
00067     double frac = pos - ipos;   
00068     double y1 = values[ipos];
00069     double y2 = values[ipos+1];
00070     //double x1 = ipos * delta + min;
00071     //double x2 = (ipos+1) * delta + min;
00072     //cout << x1 << "  "<< x2 << " "  << y1 << "  " << y2 << "  " << x << "  " << pos << "   " << ipos << "  " << frac << "  " << (x-x1)/(x2-x1) << endl;
00073     return y1 + (y2-y1) * frac;
00074     }
00075 
00076 }
00077 
00078 
00079 
00080 #endif // #ifndef CACHEDFUNCTION_H

Generated on Mon Feb 6 14:01:17 2006 for SCT DAQ/DCS Software - C++ by  doxygen 1.4.6