TabFunct.hxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __TABFUNCT__
00022 #define __TABFUNCT__
00023
00024 #include "DataTypes.hxx"
00025 #include "Assert.hxx"
00026 #include <vector>
00027
00028 using std::vector;
00029
00030 namespace CLAM
00031 {
00043 template <class OriginalFunction> class TabFunct : public OriginalFunction
00044 {
00045 private:
00046 TabFunct() {};
00047 public:
00048 TData operator() (const TData arg) {
00049
00050 CLAM_DEBUG_ASSERT(arg>=mLowerBound && arg<=mUpperBound, "Tablulated functor argument out of bound");
00051
00052 int index = int((arg-mLowerBound) / mIncr);
00053
00054 CLAM_DEBUG_ASSERT(index<=mTableSize-2, "Bad index calculation");
00055
00056 TData x1 = mLowerBound+mIncr*index;
00057 TData yIncr = mTable[index+1]-mTable[index];
00058
00059 return mTable[index] + ((arg-x1) * yIncr) * mInvIncr;
00060 }
00061
00062
00063 TabFunct(const unsigned tableSize, const TData lowerBound, const TData upperBound) :
00064 mLowerBound(lowerBound),
00065 mUpperBound(upperBound),
00066 mTableSize(tableSize)
00067
00068 {
00069 CLAM_ASSERT(tableSize>=2, "Tabulating a function with less than 2 points.");
00070 CLAM_ASSERT(lowerBound < upperBound, "No interval left to calculate values");
00071
00072 CalculateTable();
00073 }
00074
00075 private:
00076
00077 void CalculateTable()
00078 {
00079 mTable.reserve(mTableSize);
00080
00081 mIncr = (mUpperBound - mLowerBound) / (mTableSize-1);
00082 mInvIncr = 1/mIncr;
00083
00084 TData arg = mLowerBound;
00085
00086 for (int i=0; i<=mTableSize-1; arg+=mIncr, i++)
00087 mTable[i] = OriginalFunction::operator() (arg);
00088
00089 }
00090
00091 vector<TData> mTable;
00092 TData mLowerBound;
00093 TData mUpperBound;
00094 unsigned mTableSize;
00095 TData mIncr, mInvIncr;
00096
00097 };
00098
00099 }
00100
00101 #endif // TabFunct.hxx
00102