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