00001
00002
00003
00004 #ifndef __INTERPTEMPLATES
00005 #define __INTERPTEMPLATES
00006
00007
00008
00009 #include "GXStandardDefines.h"
00010
00011
00012
00013 template<typename type>
00014 inline void BlakeUtilLerp(float fWeight,
00015 const type* pInput1,
00016 const type* pInput2,
00017 type* pOut)
00018 {
00019 if(pOut)
00020 {
00021 *pOut=(type) (((*pInput1)*(1-fWeight))+(*pInput2)*(fWeight));
00022
00023 }
00024 }
00025
00026
00027
00028
00029
00030
00031
00032
00033 template<typename type>
00034 inline void BlakeUtil2DLerp(float fWeightX, float fWeightY, type& Input1, type& Input2, type& Input3, type& Input4, type* pOut)
00035 {
00036 float fOneMinusX=1-fWeightX;
00037 float fOneMinusY=1-fWeightY;
00038
00039
00040 if(pOut)
00041 {
00042 *pOut=(Input1*(fOneMinusX)*(fOneMinusY))+
00043 (Input2*(fWeightX)*(fOneMinusY))+
00044 (Input3*(fOneMinusX)*(fWeightY))+
00045 (Input4*(fWeightX)*(fWeightY));
00046 }
00047 }
00048
00049
00050 template<typename type>
00051 bool BlakeUtil2DSpline(float fWeightX, float fWeightY, type* pControlPoints, UINT uControlPointCount, type* pOut)
00052 {
00053 if(4==uControlPointCount)
00054 {
00055 BlakeUtil2DLerp(fWeightX, fWeightY,pControlPoints[0],pControlPoints[1],
00056 pControlPoints[2],pControlPoints[3],pOut);
00057 return (NULL!=pOut);
00058 }
00059
00060 if(uControlPointCount<4)
00061 {
00062 return false;
00063 }
00064
00065
00066 UINT uSideSize=sqrt(uControlPointCount);
00067
00068 type* pNewControlPoints=new type[(uSideSize-1)*(uSideSize-1)];
00069 if(!pNewControlPoints)
00070 {
00071 return false;
00072 }
00073
00074 for(UINT x=0;x<(uSideSize-1);x++)
00075 {
00076 for(UINT y=0;y<(uSideSize-1);y++)
00077 {
00078 type Input1, Input2, Input3, Input4;
00079 Input1=pControlPoints[x+(y*uSideSize)];
00080 Input2=pControlPoints[(x+1)+(y*uSideSize)];
00081 Input3=pControlPoints[x+((y+1)*uSideSize)];
00082 Input4=pControlPoints[(x+1)+((y+1)*uSideSize)];
00083
00084 BlakeUtil2DLerp(fWeightX, fWeightY,Input1,Input2,
00085 Input3,Input4,&(pNewControlPoints[x+((uSideSize-1)*y)]));
00086 }
00087 }
00088
00089 bool bRet=BlakeUtil2DSpline(fWeightX,fWeightY,pNewControlPoints,
00090 (uSideSize-1)*(uSideSize-1),pOut);
00091
00092 delete[] pNewControlPoints;
00093 return bRet;
00094 }
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 template<typename type>
00108 void BlakeUtilQuaderp(float fWeight, type* Input1, type* Input2, type* MiddleInput, type* pOut)
00109 {
00110 if(pOut)
00111 {
00112 *pOut=((*Input1)*pow((1-fWeight),2.0f))+((*Input2)*(pow(fWeight,2.0f))) + (*MiddleInput)*(2.0f*fWeight*(1-fWeight));
00113 }
00114 }
00115
00116
00117
00118
00119 template<class type>
00120 void BlakeUtilSpline(float fWeight, type* zControlPoints, DWORD dwControlPointCount, type* pOut)
00121 {
00122 if(1>=dwControlPointCount)
00123 {
00124 *pOut=zControlPoints[0];
00125 return;
00126 }
00127
00128 type *pNewPoints=new type[dwControlPointCount-1];
00129
00130 if(!pNewPoints)
00131 {
00132 return;
00133 }
00134
00135 for(UINT ctr=0;ctr<(dwControlPointCount-1);ctr++)
00136 {
00137 BlakeUtilLerp(fWeight,&(zControlPoints[ctr]),(&zControlPoints[ctr+1]),&(pNewPoints[ctr]));
00138 }
00139
00140 BlakeUtilSpline(fWeight,pNewPoints,dwControlPointCount-1,pOut);
00141
00142 delete[] pNewPoints;
00143 }
00144
00145
00146
00147 inline float BlakeUtilRandomFloat(float fMin, float fMax)
00148 {
00149
00150 float fRand=(rand()%RAND_MAX)/(float)RAND_MAX;
00151
00152 return fMin+(fRand*(fMax-fMin));
00153 }
00154
00155
00156
00157 #endif