00001 #ifndef __GXVECTOR_H 00002 #define __GXVECTOR_H 00003 00004 00005 00006 00007 00009 //vector 00011 template<unsigned int count> 00012 class CGXVector 00013 { 00014 public: 00015 float m_fPoints[count]; 00016 00017 inline void Normalize() 00018 { 00019 float fLen=GetLength(); 00020 00021 if(0==fLen) 00022 { 00023 return; 00024 } 00025 00026 operator/= (fLen); 00027 } 00028 00029 00030 inline float GetLength() 00031 { 00032 float fLen=0; 00033 00034 for(UINT uCtr=0;uCtr<count;uCtr++) 00035 { 00036 fLen+=(m_fPoints[uCtr]*m_fPoints[uCtr]); 00037 } 00038 00039 return (float)sqrt(fLen); 00040 } 00041 00042 inline float operator[] (UINT uIndex) 00043 { 00044 return m_fPoints[uIndex]; 00045 } 00046 00047 00048 inline void operator*= (float fMul) 00049 { 00050 for(UINT uCtr=0;uCtr<count;uCtr++) 00051 { 00052 m_fPoints[uCtr]*=fMul; 00053 } 00054 } 00055 00056 inline void operator+= (float fAdd) 00057 { 00058 for(UINT uCtr=0;uCtr<count;uCtr++) 00059 { 00060 m_fPoints[uCtr]+=fAdd; 00061 } 00062 } 00063 00064 00065 inline CGXVector<count> operator* (float fAmt) 00066 { 00067 CGXVector<count> vRet; 00068 memcpy(&vRet,this,sizeof(CGXVector<count>)); 00069 vRet*=fAmt; 00070 return vRet; 00071 } 00072 00073 inline CGXVector<count> operator+ (CGXVector<count>& vIn) 00074 { 00075 CGXVector<count> vRet; 00076 00077 for(UINT uCtr=0;uCtr<count;uCtr++) 00078 { 00079 vRet.m_fPoints[uCtr]=m_fPoints[uCtr]+vIn.m_fPoints[uCtr]; 00080 } 00081 00082 return vRet; 00083 } 00084 00085 inline CGXVector<count> operator* (CGXVector<count>& vIn) 00086 { 00087 CGXVector<count> vRet; 00088 00089 for(UINT uCtr=0;uCtr<count;uCtr++) 00090 { 00091 vRet.m_fPoints[uCtr]=m_fPoints[uCtr]*vIn.m_fPoints[uCtr]; 00092 } 00093 00094 return vRet; 00095 } 00096 00097 00098 inline CGXVector<count> operator/ (CGXVector<count>& vIn) 00099 { 00100 CGXVector<count> vRet; 00101 00102 for(UINT uCtr=0;uCtr<count;uCtr++) 00103 { 00104 vRet.m_fPoints[uCtr]=m_fPoints[uCtr]/vIn.m_fPoints[uCtr]; 00105 } 00106 00107 return vRet; 00108 } 00109 00110 00111 inline void operator/= (float fDiv) 00112 { 00113 for(UINT uCtr=0;uCtr<count;uCtr++) 00114 { 00115 m_fPoints[uCtr]/=fDiv; 00116 } 00117 } 00118 00119 inline void operator-= (CGXVector<count>& vSub) 00120 { 00121 for(UINT uCtr=0;uCtr<count;uCtr++) 00122 { 00123 m_fPoints[uCtr]-=vSub.m_fPoints[uCtr]; 00124 } 00125 } 00126 00127 inline void operator+= (CGXVector<count>& vAdd) 00128 { 00129 for(UINT uCtr=0;uCtr<count;uCtr++) 00130 { 00131 m_fPoints[uCtr]+=vAdd.m_fPoints[uCtr]; 00132 } 00133 } 00134 00135 inline void operator*= (CGXVector<count>& vMul) 00136 { 00137 for(UINT uCtr=0;uCtr<count;uCtr++) 00138 { 00139 m_fPoints[uCtr]*=vMul.m_fPoints[uCtr]; 00140 } 00141 } 00142 00143 inline void operator/= (CGXVector<count>& vDiv) 00144 { 00145 for(UINT uCtr=0;uCtr<count;uCtr++) 00146 { 00147 m_fPoints[uCtr]/=vDiv.m_fPoints[uCtr]; 00148 } 00149 } 00150 00151 00152 00153 00154 inline CGXVector operator- (CGXVector<count>& vSub) 00155 { 00156 CGXVector vOut=*this; 00157 vOut-=vSub; 00158 return vOut; 00159 } 00160 00161 00162 inline float Dot(CGXVector<count>& vRightSide) 00163 { 00164 float fResult=0; 00165 00166 for(UINT uCtr=0;uCtr<count;uCtr++) 00167 { 00168 fResult+=m_fPoints[uCtr]*(vRightSide.m_fPoints[uCtr]); 00169 } 00170 00171 return fResult; 00172 } 00173 00174 /* CBlakeUtil3DVector returnVal; 00175 returnVal.m_x=(m_y*vArg.m_z)-(m_z*vArg.m_y); 00176 returnVal.m_y=(m_z*vArg.m_x)-(m_x*vArg.m_z); 00177 returnVal.m_z=(m_x*vArg.m_y)-(m_y*vArg.m_x);*/ 00178 00179 00180 inline bool Cross(CGXVector<count>& vOut, CGXVector<count>& vRightSide) 00181 { 00182 if(3==count) 00183 { 00184 vOut.m_fPoints[0]=(m_fPoints[1]*vRightSide.m_fPoints[2])-(m_fPoints[2]*vRightSide.m_fPoints[1]); 00185 vOut.m_fPoints[1]=(m_fPoints[2]*vRightSide.m_fPoints[0])-(m_fPoints[0]*vRightSide.m_fPoints[2]); 00186 vOut.m_fPoints[2]=(m_fPoints[0]*vRightSide.m_fPoints[1])-(m_fPoints[1]*vRightSide.m_fPoints[0]); 00187 return true; 00188 } 00189 00190 return false; 00191 } 00192 00193 }; 00194 00195 00196 00197 00198 00199 #endif