Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

GXInteroplationTemplates.h

Go to the documentation of this file.
00001 //todo, test these
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 A    B
00030 
00031 C    D
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 //todo, either auto array, or better yet, not news
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 //todo, rederive this to make sure it is correct
00101 
00102 //quadratic interpolation
00103 //f=Input1*(1-fWeight)^2
00104 //fWeight=0, output=Input1
00105 //fweight=1, output=input2
00106 //fWeight=.5,, MiddleInput
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 //todo, redo this without millions of news
00118 //Todo, make this a bool
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         //generate a random float between [0,1]
00150         float fRand=(rand()%RAND_MAX)/(float)RAND_MAX;
00151 
00152         return fMin+(fRand*(fMax-fMin));
00153 }
00154 
00155 
00156 
00157 #endif

Generated on Thu May 23 17:51:00 2002 by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001