00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef SALT_TVECTOR_H
00034 #define SALT_TVECTOR_H
00035
00036
00037
00038
00039
00040 #include "defines.h"
00041 #include "gmath.h"
00042 #include <iostream>
00043
00044 namespace salt
00045 {
00046
00050 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00051 class TVector
00052 {
00053
00054
00055
00056 public:
00057 TVector() {}
00058
00059
00060
00062 f_inline DATATYPE& operator[](int row)
00063 { return El(row); }
00064
00066 f_inline const DATATYPE& operator[](int row) const
00067 { return El(row); }
00068
00070 f_inline DATATYPE& Get(int row) { return El(row); }
00071
00073 f_inline const DATATYPE& Get(int row) const { return El(row); }
00074
00075
00076
00078 f_inline const TYPE& SetData(const DATATYPE *copy);
00079
00081 f_inline DATATYPE* GetData()
00082 { return mData; }
00083
00084
00085
00087 void Dump() const;
00088
00090 f_inline const TYPE& Fill(const DATATYPE& fill);
00091
00093 f_inline TYPE& Zero();
00094
00095
00096
00098 f_inline const TYPE operator+(const TYPE &v) const;
00099
00101 f_inline const TYPE operator-(const TYPE &v) const;
00102
00104 f_inline const TYPE operator*(const DATATYPE &v) const;
00105
00107 f_inline const TYPE operator/(const DATATYPE &v) const;
00108
00110 f_inline TYPE& operator+=(const TYPE &v);
00111
00113 f_inline TYPE& operator-=(const TYPE &v);
00114
00116 f_inline TYPE& operator*=(const DATATYPE &v);
00117
00119 f_inline TYPE& operator/=(const DATATYPE &v);
00120
00122 f_inline TYPE operator-() const;
00123
00125 f_inline bool operator==(const TYPE& v)const;
00126
00128 f_inline bool operator!=(const TYPE& v)const;
00129
00131 f_inline DATATYPE Dot(const TYPE& v) const;
00132
00134 f_inline const TYPE& Normalize();
00135
00137 f_inline TYPE Normalized() const;
00138
00140 f_inline DATATYPE SquareLength() const;
00141
00143 f_inline DATATYPE Length() const
00144 { return gSqrt(SquareLength()); }
00145
00147 f_inline int GetLeastSignificantAxis() const;
00148
00150 f_inline int GetMostSignificantAxis() const;
00151
00153 f_inline TYPE LinearInterpolate(const TYPE& to, float delta) const;
00154
00158 f_inline TYPE NormalizedLinearInterpolate(const TYPE& to, float delta) const
00159 {
00160 return LinearInterpolate(to, delta).Normalize();
00161 }
00162
00163 protected:
00164
00165 TVector(const TYPE &v)
00166 {
00167 for (int i=0; i<ELEMENTS; i++)
00168 mData[i] = v[i];
00169 }
00170
00171
00172 const DATATYPE& El(int index) const
00173 {
00174 return mData[index];
00175 }
00176 DATATYPE& El(int index)
00177 {
00178 return mData[index];
00179 }
00180
00181
00182
00183
00184 private:
00185 DATATYPE mData[ELEMENTS];
00186 };
00187
00188 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00189 std::ostream& operator <<(std::ostream& ost, const TVector<DATATYPE,ELEMENTS,TYPE>& v)
00190 {
00191 if (ELEMENTS < 1) return ost;
00192 ost << v[0];
00193 for (int i=1; i<ELEMENTS; ++i) ost << " " << v[i];
00194 return ost;
00195 }
00196
00198 template <typename DATATYPE, class TYPE>
00199 class TVector2 : public TVector<DATATYPE, 2, TYPE>
00200 {
00201 public:
00203 TVector2() : TVector<DATATYPE, 2, TYPE>() {};
00204
00206 TVector2(DATATYPE x, DATATYPE y) : TVector<DATATYPE, 2, TYPE>()
00207 { Set(x, y); }
00208
00209
00210
00212 f_inline DATATYPE& x()
00213 { return this->El(0); }
00214
00216 f_inline const DATATYPE& x() const
00217 { return this->El(0); }
00218
00220 f_inline DATATYPE& y()
00221 { return this->El(1); }
00222
00224 f_inline const DATATYPE& y() const
00225 { return this->El(1); }
00226
00228 f_inline const TYPE& Set(const DATATYPE& x, const DATATYPE& y)
00229 {
00230 this->El(0) = x;
00231 this->El(1) = y;
00232 return *static_cast<TYPE*>(this);
00233 }
00234 };
00235
00236
00238 template <typename DATATYPE, class TYPE>
00239 class TVector3 : public TVector<DATATYPE, 3, TYPE>
00240 {
00241 public:
00243 TVector3() : TVector<DATATYPE, 3, TYPE>() {};
00244
00246 TVector3(const DATATYPE& x, const DATATYPE& y, const DATATYPE& z)
00247 : TVector<DATATYPE, 3, TYPE>()
00248 { Set(x, y, z); }
00249
00250
00251
00253 f_inline DATATYPE& x()
00254 { return this->El(0); }
00255
00257 f_inline const DATATYPE& x() const
00258 { return this->El(0); }
00259
00261 f_inline DATATYPE& y()
00262 { return this->El(1); }
00263
00265 f_inline const DATATYPE& y() const
00266 { return this->El(1); }
00267
00269 f_inline DATATYPE& z()
00270 { return this->El(2); }
00271
00273 f_inline const DATATYPE& z() const
00274 { return this->El(2); }
00275
00277 const TYPE Cross(const TVector<DATATYPE, 3, TYPE>& v) const
00278 {
00279
00280 TYPE r;
00281 r[0] = this->El(1) * v[2] - this->El(2) * v[1];
00282 r[1] = this->El(2) * v[0] - this->El(0) * v[2];
00283 r[2] = this->El(0) * v[1] - this->El(1) * v[0];
00284 return r;
00285 }
00286
00287
00288
00289
00290
00292 const TYPE& Set(const DATATYPE& x, const DATATYPE& y, const DATATYPE& z)
00293 {
00294 this->El(0) = x;
00295 this->El(1) = y;
00296 this->El(2) = z;
00297 return *static_cast<TYPE*>(this);
00298 }
00299
00300
00302 const TYPE& Set(const TYPE& v)
00303 {
00304 this->El(0) = v.x();
00305 this->El(1) = v.y();
00306 this->El(2) = v.z();
00307 return *static_cast<TYPE*>(this);
00308 }
00309 };
00310
00311
00312 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00313 inline const TYPE& TVector<DATATYPE, ELEMENTS, TYPE>::SetData(const DATATYPE *copy)
00314 {
00315 for (int i=0; i<ELEMENTS; i++)
00316 mData[i] = copy[i];
00317 return *static_cast<TYPE*>(this);
00318 }
00319
00320
00321 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00322 inline void TVector<DATATYPE, ELEMENTS, TYPE>::Dump() const
00323 {
00324 for (int i=0; i<ELEMENTS; i++)
00325 std::cout << mData[i] << " ";
00326 std::cout << "\n";
00327 }
00328
00329
00330 template <typename DATATYPE, int ELEMENTS, class TYPE>
00331 f_inline const TYPE& TVector<DATATYPE, ELEMENTS, TYPE>::Fill(const DATATYPE& fill)
00332 {
00333 for (int c=0; c < ELEMENTS; c++)
00334 mData[c] = fill;
00335 return *static_cast<TYPE*>(this);
00336 }
00337
00338
00339 template <typename DATATYPE, int ELEMENTS, class TYPE>
00340 f_inline TYPE& TVector<DATATYPE, ELEMENTS, TYPE>::Zero()
00341 {
00342 for (int c=0; c < ELEMENTS; c++)
00343 mData[c] = DATATYPE(0);
00344 return *static_cast<TYPE*>(this);
00345 }
00346
00347
00348 template <typename DATATYPE, int ELEMENTS, class TYPE>
00349 f_inline const TYPE operator*(const DATATYPE& f, const TVector<DATATYPE, ELEMENTS, TYPE>& vec)
00350 {
00351 return vec * f;
00352 }
00353
00354
00355
00356 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00357 f_inline const TYPE TVector<DATATYPE, ELEMENTS, TYPE>::operator+(const TYPE& v) const
00358 {
00359 TYPE r;
00360 for (int c=0; c < ELEMENTS; c++)
00361 r[c]=mData[c] + v[c];
00362 return r;
00363 }
00364
00365
00366
00367 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00368 f_inline const TYPE TVector<DATATYPE, ELEMENTS, TYPE>::operator-(const TYPE& v) const
00369 {
00370 TYPE r;
00371 for (int c=0; c < ELEMENTS; c++)
00372 r[c]=mData[c] - v[c];
00373 return r;
00374 }
00375
00376
00377
00378 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00379 f_inline const TYPE TVector<DATATYPE, ELEMENTS, TYPE>::operator*(const DATATYPE& v) const
00380 {
00381 TYPE r;
00382 for (int c=0; c < ELEMENTS; c++)
00383 r[c]=mData[c] * v;
00384 return r;
00385 }
00386
00387
00388
00389 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00390 f_inline const TYPE TVector<DATATYPE, ELEMENTS, TYPE>::operator/(const DATATYPE& v) const
00391 {
00392 TYPE r;
00393 for (int c=0; c < ELEMENTS; c++)
00394 r[c]=mData[c] / v;
00395 return r;
00396 }
00397
00398
00399 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00400 f_inline TYPE& TVector<DATATYPE, ELEMENTS, TYPE>::operator+=(const TYPE& v)
00401 {
00402 for (int c=0; c<ELEMENTS; c++)
00403 mData[c] += v[c];
00404 return *static_cast<TYPE*>(this);
00405 }
00406
00407
00408 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00409 f_inline TYPE& TVector<DATATYPE, ELEMENTS, TYPE>::operator-=(const TYPE& v)
00410 {
00411 for (int c=0; c<ELEMENTS; c++)
00412 mData[c] -= v[c];
00413 return *static_cast<TYPE*>(this);
00414 }
00415
00416
00417 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00418 f_inline TYPE& TVector<DATATYPE, ELEMENTS, TYPE>::operator*=(const DATATYPE& v)
00419 {
00420 for (int c=0; c<ELEMENTS; c++)
00421 mData[c] *= v;
00422 return *static_cast<TYPE*>(this);
00423 }
00424
00425
00426 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00427 f_inline TYPE& TVector<DATATYPE, ELEMENTS, TYPE>::operator/=(const DATATYPE& v)
00428 {
00429 for (int c=0; c<ELEMENTS; c++)
00430 mData[c] *= v;
00431 return *static_cast<TYPE*>(this);
00432 }
00433
00434
00435 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00436 f_inline TYPE TVector<DATATYPE, ELEMENTS, TYPE>::operator-() const
00437 {
00438 TYPE r;
00439 for (int c=0; c < ELEMENTS; c++)
00440 r[c] = -mData[c];
00441 return r;
00442 }
00443
00444
00445 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00446 f_inline bool TVector<DATATYPE, ELEMENTS, TYPE>::operator==(const TYPE& v) const
00447 {
00448 return (0==memcmp(this,& v, sizeof(*this)));
00449 }
00450
00451
00452 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00453 f_inline bool TVector<DATATYPE, ELEMENTS, TYPE>::operator!=(const TYPE& v) const
00454 {
00455 return (0!=memcmp(this,& v, sizeof(*this)));
00456 }
00457
00458
00459 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00460 f_inline DATATYPE TVector<DATATYPE, ELEMENTS, TYPE>::Dot(const TYPE& v) const
00461 {
00462 DATATYPE r = mData[0] * v[0];
00463 for (int c=1; c < ELEMENTS; c++)
00464 r += mData[c] * v[c];
00465 return r;
00466 }
00467
00468
00469 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00470 f_inline const TYPE& TVector<DATATYPE, ELEMENTS, TYPE>::Normalize()
00471 {
00472 DATATYPE length = Length();
00473 DATATYPE lengthInv = DATATYPE(DATATYPE(1) / length);
00474 for (int c=0; c<ELEMENTS; c++)
00475 mData[c] *= lengthInv;
00476 return *static_cast<TYPE*>(this);
00477 }
00478
00479 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00480 f_inline TYPE TVector<DATATYPE, ELEMENTS, TYPE>::Normalized() const
00481 {
00482 TYPE r;
00483 DATATYPE length = Length();
00484 DATATYPE lengthInv = DATATYPE(DATATYPE(1) / length);
00485 for (int c=0; c<ELEMENTS; c++)
00486 r[c] = mData[c] * lengthInv;
00487 return r;
00488 }
00489
00490
00491 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00492 f_inline DATATYPE TVector<DATATYPE, ELEMENTS, TYPE>::SquareLength() const
00493 {
00494 DATATYPE r = mData[0] * mData[0];
00495 for (int c=1; c<ELEMENTS; c++)
00496 r += mData[c] * mData[c];
00497 return r;
00498 }
00499
00500
00501 template <typename DATATYPE, int ELEMENTS, typename TYPE>
00502 f_inline TYPE TVector<DATATYPE, ELEMENTS, TYPE>::LinearInterpolate(const TYPE& to, float t) const
00503 {
00504 float it = 1.0f - t;
00505 TYPE r;
00506 for (int c=0; c<ELEMENTS; c++)
00507 r[c] = mData[c] * it + to[c] * t;
00508 return r;
00509 }
00510
00511 }
00512
00513 #endif //SALT_TVECTOR_H