00001 #ifndef _XY_RESPONSE_CURVE_H
00002 #define _XY_RESPONSE_CURVE_H
00003
00004 #include "AbstractFunction.h"
00005
00006 namespace luma
00007 {
00008 namespace numbers
00009 {
00017 template<class T, unsigned int n>
00018 class XYResponseCurve: public AbstractFunction<T>
00019 {
00020 public:
00030 XYResponseCurve(T inputSamples[n], T outputSamples[n]);
00031
00045 T operator()(const T input) const;
00046
00047 void makeInverse();
00048
00052 unsigned int findInputIndex(const T input) const;
00053
00054 private:
00055 T mInputSamples[n];
00056 T mOutputSamples[n];
00057
00058 };
00059
00060 template<class T, unsigned int n>
00061 XYResponseCurve<T, n>::XYResponseCurve(T inputSamples[n], T outputSamples[n])
00062 {
00063 for(unsigned i = 0; i < n; i++)
00064 {
00065 mInputSamples[i] = inputSamples[i];
00066 mOutputSamples[i] = outputSamples[i];
00067 }
00068 }
00069
00070 template<class T, unsigned int n>
00071 T XYResponseCurve<T, n>::operator()(const T input) const
00072 {
00073 if (input <= mInputSamples[0])
00074 {
00075 return mOutputSamples[0];
00076 }
00077
00078 if (input >= mInputSamples[n - 1])
00079 {
00080 return mOutputSamples[n - 1];
00081 }
00082
00083 unsigned int index = findInputIndex(input);
00084
00085
00086 T x1 = mInputSamples[index + 1];
00087 T x0 = mInputSamples[index];
00088
00089 T tau = (input - x0) / (x1 - x0);
00090 T y1 = mOutputSamples[index + 1];
00091 T y0 = mOutputSamples[index];
00092 return (y1 - y0) * tau + y0;
00093 }
00094
00095 template<class T, unsigned int n>
00096 unsigned int XYResponseCurve<T, n>::findInputIndex(const T input) const
00097 {
00098 unsigned int min = 0;
00099 unsigned int max = n;
00100 unsigned int mid;
00101
00102 while (max > min + 1)
00103 {
00104 mid = (max + min) / 2 ;
00105
00106
00107 if(input < mInputSamples[mid])
00108 {
00109 max = mid;
00110 }
00111 else
00112 {
00113 min = mid;
00114 }
00115 }
00116
00117 return min;
00118 }
00119 template<class T, unsigned int n>
00120 void XYResponseCurve<T, n>::makeInverse()
00121 {
00122 T tmp;
00123
00124 for (unsigned i = 0; i < n; i++)
00125 {
00126 tmp = mInputSamples[i];
00127 mInputSamples[i] = mOutputSamples[i];
00128 mOutputSamples[i] = tmp;
00129 }
00130 }
00131
00132
00133 }
00134 }
00135
00136 #endif //_XYRESPONSE_CURVE_H