Wm4GVector.inl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 namespace Wm4
00018 {
00019
00020 template <class Real>
00021 GVector<Real>::GVector (int iSize)
00022 {
00023 if (iSize > 0)
00024 {
00025 m_iSize = iSize;
00026 m_afTuple = WM4_NEW Real[m_iSize];
00027 memset(m_afTuple,0,m_iSize*sizeof(Real));
00028 }
00029 else
00030 {
00031 m_iSize = 0;
00032 m_afTuple = 0;
00033 }
00034 }
00035
00036 template <class Real>
00037 GVector<Real>::GVector (int iSize, const Real* afTuple)
00038 {
00039 if (iSize > 0)
00040 {
00041 m_iSize = iSize;
00042 m_afTuple = WM4_NEW Real[m_iSize];
00043 size_t uiSize = m_iSize*sizeof(Real);
00044 System::Memcpy(m_afTuple,uiSize,afTuple,uiSize);
00045 }
00046 else
00047 {
00048 m_iSize = 0;
00049 m_afTuple = 0;
00050 }
00051 }
00052
00053 template <class Real>
00054 GVector<Real>::GVector (const GVector& rkV)
00055 {
00056 m_iSize = rkV.m_iSize;
00057 if (m_iSize > 0)
00058 {
00059 m_afTuple = WM4_NEW Real[m_iSize];
00060 size_t uiSize = m_iSize*sizeof(Real);
00061 System::Memcpy(m_afTuple,uiSize,rkV.m_afTuple,uiSize);
00062 }
00063 else
00064 {
00065 m_afTuple = 0;
00066 }
00067 }
00068
00069 template <class Real>
00070 GVector<Real>::~GVector ()
00071 {
00072 WM4_DELETE[] m_afTuple;
00073 }
00074
00075 template <class Real>
00076 void GVector<Real>::SetSize (int iSize)
00077 {
00078 WM4_DELETE[] m_afTuple;
00079 if (iSize > 0)
00080 {
00081 m_iSize = iSize;
00082 m_afTuple = WM4_NEW Real[m_iSize];
00083 memset(m_afTuple,0,m_iSize*sizeof(Real));
00084 }
00085 else
00086 {
00087 m_iSize = 0;
00088 m_afTuple = 0;
00089 }
00090 }
00091
00092 template <class Real>
00093 int GVector<Real>::GetSize () const
00094 {
00095 return m_iSize;
00096 }
00097
00098 template <class Real>
00099 GVector<Real>::operator const Real* () const
00100 {
00101 return m_afTuple;
00102 }
00103
00104 template <class Real>
00105 GVector<Real>::operator Real* ()
00106 {
00107 return m_afTuple;
00108 }
00109
00110 template <class Real>
00111 Real GVector<Real>::operator[] (int i) const
00112 {
00113 assert(0 <= i && i < m_iSize);
00114 return m_afTuple[i];
00115 }
00116
00117 template <class Real>
00118 Real& GVector<Real>::operator[] (int i)
00119 {
00120 assert(0 <= i && i < m_iSize);
00121 return m_afTuple[i];
00122 }
00123
00124 template <class Real>
00125 GVector<Real>& GVector<Real>::operator= (const GVector& rkV)
00126 {
00127 if (rkV.m_iSize > 0)
00128 {
00129 if (m_iSize != rkV.m_iSize)
00130 {
00131 WM4_DELETE[] m_afTuple;
00132 m_iSize = rkV.m_iSize;
00133 m_afTuple = WM4_NEW Real[m_iSize];
00134 }
00135 size_t uiSize = m_iSize*sizeof(Real);
00136 System::Memcpy(m_afTuple,uiSize,rkV.m_afTuple,uiSize);
00137 }
00138 else
00139 {
00140 WM4_DELETE[] m_afTuple;
00141 m_iSize = 0;
00142 m_afTuple = 0;
00143 }
00144 return *this;
00145 }
00146
00147 template <class Real>
00148 int GVector<Real>::CompareArrays (const GVector& rkV) const
00149 {
00150 return memcmp(m_afTuple,rkV.m_afTuple,m_iSize*sizeof(Real));
00151 }
00152
00153 template <class Real>
00154 bool GVector<Real>::operator== (const GVector& rkV) const
00155 {
00156 return CompareArrays(rkV) == 0;
00157 }
00158
00159 template <class Real>
00160 bool GVector<Real>::operator!= (const GVector& rkV) const
00161 {
00162 return CompareArrays(rkV) != 0;
00163 }
00164
00165 template <class Real>
00166 bool GVector<Real>::operator< (const GVector& rkV) const
00167 {
00168 return CompareArrays(rkV) < 0;
00169 }
00170
00171 template <class Real>
00172 bool GVector<Real>::operator<= (const GVector& rkV) const
00173 {
00174 return CompareArrays(rkV) <= 0;
00175 }
00176
00177 template <class Real>
00178 bool GVector<Real>::operator> (const GVector& rkV) const
00179 {
00180 return CompareArrays(rkV) > 0;
00181 }
00182
00183 template <class Real>
00184 bool GVector<Real>::operator>= (const GVector& rkV) const
00185 {
00186 return CompareArrays(rkV) >= 0;
00187 }
00188
00189 template <class Real>
00190 GVector<Real> GVector<Real>::operator+ (const GVector& rkV) const
00191 {
00192 GVector<Real> kSum(m_iSize);
00193 for (int i = 0; i < m_iSize; i++)
00194 {
00195 kSum.m_afTuple[i] = m_afTuple[i] + rkV.m_afTuple[i];
00196 }
00197 return kSum;
00198 }
00199
00200 template <class Real>
00201 GVector<Real> GVector<Real>::operator- (const GVector& rkV) const
00202 {
00203 GVector<Real> kDiff(m_iSize);
00204 for (int i = 0; i < m_iSize; i++)
00205 {
00206 kDiff.m_afTuple[i] = m_afTuple[i] - rkV.m_afTuple[i];
00207 }
00208 return kDiff;
00209 }
00210
00211 template <class Real>
00212 GVector<Real> GVector<Real>::operator* (Real fScalar) const
00213 {
00214 GVector<Real> kProd(m_iSize);
00215 for (int i = 0; i < m_iSize; i++)
00216 {
00217 kProd.m_afTuple[i] = fScalar*m_afTuple[i];
00218 }
00219 return kProd;
00220 }
00221
00222 template <class Real>
00223 GVector<Real> GVector<Real>::operator/ (Real fScalar) const
00224 {
00225 GVector<Real> kQuot(m_iSize);
00226 int i;
00227
00228 if (fScalar != (Real)0.0)
00229 {
00230 Real fInvScalar = ((Real)1.0)/fScalar;
00231 for (i = 0; i < m_iSize; i++)
00232 {
00233 kQuot.m_afTuple[i] = fInvScalar*m_afTuple[i];
00234 }
00235 }
00236 else
00237 {
00238 for (i = 0; i < m_iSize; i++)
00239 {
00240 kQuot.m_afTuple[i] = Math<Real>::MAX_REAL;
00241 }
00242 }
00243
00244 return kQuot;
00245 }
00246
00247 template <class Real>
00248 GVector<Real> GVector<Real>::operator- () const
00249 {
00250 GVector<Real> kNeg(m_iSize);
00251 for (int i = 0; i < m_iSize; i++)
00252 {
00253 kNeg.m_afTuple[i] = -m_afTuple[i];
00254 }
00255 return kNeg;
00256 }
00257
00258 template <class Real>
00259 GVector<Real> operator* (Real fScalar, const GVector<Real>& rkV)
00260 {
00261 GVector<Real> kProd(rkV.GetSize());
00262 for (int i = 0; i < rkV.GetSize(); i++)
00263 {
00264 kProd[i] = fScalar*rkV[i];
00265 }
00266 return kProd;
00267 }
00268
00269 template <class Real>
00270 GVector<Real>& GVector<Real>::operator+= (const GVector& rkV)
00271 {
00272 for (int i = 0; i < m_iSize; i++)
00273 {
00274 m_afTuple[i] += rkV.m_afTuple[i];
00275 }
00276 return *this;
00277 }
00278
00279 template <class Real>
00280 GVector<Real>& GVector<Real>::operator-= (const GVector& rkV)
00281 {
00282 for (int i = 0; i < m_iSize; i++)
00283 {
00284 m_afTuple[i] -= rkV.m_afTuple[i];
00285 }
00286 return *this;
00287 }
00288
00289 template <class Real>
00290 GVector<Real>& GVector<Real>::operator*= (Real fScalar)
00291 {
00292 for (int i = 0; i < m_iSize; i++)
00293 {
00294 m_afTuple[i] *= fScalar;
00295 }
00296 return *this;
00297 }
00298
00299 template <class Real>
00300 GVector<Real>& GVector<Real>::operator/= (Real fScalar)
00301 {
00302 int i;
00303
00304 if (fScalar != (Real)0.0)
00305 {
00306 Real fInvScalar = ((Real)1.0)/fScalar;
00307 for (i = 0; i < m_iSize; i++)
00308 {
00309 m_afTuple[i] *= fInvScalar;
00310 }
00311 }
00312 else
00313 {
00314 for (i = 0; i < m_iSize; i++)
00315 {
00316 m_afTuple[i] = Math<Real>::MAX_REAL;
00317 }
00318 }
00319
00320 return *this;
00321 }
00322
00323 template <class Real>
00324 Real GVector<Real>::Length () const
00325 {
00326 Real fSqrLen = (Real)0.0;
00327 for (int i = 0; i < m_iSize; i++)
00328 {
00329 fSqrLen += m_afTuple[i]*m_afTuple[i];
00330 }
00331 return Math<Real>::Sqrt(fSqrLen);
00332 }
00333
00334 template <class Real>
00335 Real GVector<Real>::SquaredLength () const
00336 {
00337 Real fSqrLen = (Real)0.0;
00338 for (int i = 0; i < m_iSize; i++)
00339 {
00340 fSqrLen += m_afTuple[i]*m_afTuple[i];
00341 }
00342 return fSqrLen;
00343 }
00344
00345 template <class Real>
00346 Real GVector<Real>::Dot (const GVector& rkV) const
00347 {
00348 Real fDot = (Real)0.0;
00349 for (int i = 0; i < m_iSize; i++)
00350 {
00351 fDot += m_afTuple[i]*rkV.m_afTuple[i];
00352 }
00353 return fDot;
00354 }
00355
00356 template <class Real>
00357 Real GVector<Real>::Normalize ()
00358 {
00359 Real fLength = Length();
00360 int i;
00361
00362 if (fLength > Math<Real>::ZERO_TOLERANCE)
00363 {
00364 Real fInvLength = ((Real)1.0)/fLength;
00365 for (i = 0; i < m_iSize; i++)
00366 {
00367 m_afTuple[i] *= fInvLength;
00368 }
00369 }
00370 else
00371 {
00372 fLength = (Real)0.0;
00373 for (i = 0; i < m_iSize; i++)
00374 {
00375 m_afTuple[i] = (Real)0.0;
00376 }
00377 }
00378
00379 return fLength;
00380 }
00381
00382 }