00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 namespace PolyVox
00025 {
00026
00032 template <uint32_t Size,typename Type>
00033 Vector<Size,Type>::Vector(Type x, Type y) throw()
00034 {
00035 m_tElements[0] = x;
00036 m_tElements[1] = y;
00037
00038 }
00039
00046 template <uint32_t Size,typename Type>
00047 Vector<Size,Type>::Vector(Type x, Type y, Type z) throw()
00048 {
00049 m_tElements[0] = x;
00050 m_tElements[1] = y;
00051 m_tElements[2] = z;
00052
00053 }
00054
00062 template <uint32_t Size,typename Type>
00063 Vector<Size,Type>::Vector(Type x, Type y, Type z, Type w) throw()
00064 {
00065 m_tElements[0] = x;
00066 m_tElements[1] = y;
00067 m_tElements[2] = z;
00068 m_tElements[3] = w;
00069 }
00070
00074 template <uint32_t Size, typename Type>
00075 Vector<Size, Type>::Vector(void) throw()
00076 {
00077 }
00078
00083 template <uint32_t Size, typename Type>
00084 Vector<Size, Type>::Vector(const Vector<Size, Type>& vector) throw()
00085 {
00086 std::memcpy(m_tElements, vector.m_tElements, sizeof(Type) * Size);
00087 }
00088
00098 template <uint32_t Size, typename Type>
00099 template <typename CastType>
00100 Vector<Size, Type>::Vector(const Vector<Size, CastType>& vector) throw()
00101 {
00102 for(uint32_t ct = 0; ct < Size; ++ct)
00103 {
00104 m_tElements[ct] = static_cast<Type>(vector.getElement(ct));
00105 }
00106 }
00107
00111 template <uint32_t Size, typename Type>
00112 Vector<Size, Type>::~Vector(void) throw()
00113 {
00114 }
00115
00121 template <uint32_t Size, typename Type>
00122 Vector<Size, Type>& Vector<Size, Type>::operator=(const Vector<Size, Type>& rhs) throw()
00123 {
00124 if(this == &rhs)
00125 {
00126 return *this;
00127 }
00128 std::memcpy(m_tElements, rhs.m_tElements, sizeof(Type) * Size);
00129 return *this;
00130 }
00131
00138 template <uint32_t Size, typename Type>
00139 inline bool Vector<Size, Type>::operator==(const Vector<Size, Type> &rhs) const throw()
00140 {
00141 bool equal = true;
00142 for(uint32_t ct = 0; ct < Size; ++ct)
00143 {
00144 if(m_tElements[ct] != rhs.m_tElements[ct])
00145 {
00146 equal = false;
00147 break;
00148 }
00149 }
00150 return equal;
00151 }
00152
00159 template <uint32_t Size, typename Type>
00160 inline bool Vector<Size, Type>::operator!=(const Vector<Size, Type> &rhs) const throw()
00161 {
00162 return !(*this == rhs);
00163 }
00164
00172 template <uint32_t Size, typename Type>
00173 inline bool Vector<Size, Type>::operator<(const Vector<Size, Type> &rhs) const throw()
00174 {
00175 for(uint32_t ct = 0; ct < Size; ++ct)
00176 {
00177 if (m_tElements[ct] < rhs.m_tElements[ct])
00178 return true;
00179 if (rhs.m_tElements[ct] < m_tElements[ct])
00180 return false;
00181 }
00182 return false;
00183 }
00184
00190 template <uint32_t Size, typename Type>
00191 inline Vector<Size, Type>& Vector<Size, Type>::operator+=(const Vector<Size, Type>& rhs) throw()
00192 {
00193 for(uint32_t ct = 0; ct < Size; ++ct)
00194 {
00195 m_tElements[ct] += rhs.m_tElements[ct];
00196 }
00197 return *this;
00198 }
00199
00205 template <uint32_t Size, typename Type>
00206 inline Vector<Size, Type>& Vector<Size, Type>::operator-=(const Vector<Size, Type>& rhs) throw()
00207 {
00208 for(uint32_t ct = 0; ct < Size; ++ct)
00209 {
00210 m_tElements[ct] -= rhs.m_tElements[ct];
00211 }
00212 return *this;
00213 }
00214
00220 template <uint32_t Size, typename Type>
00221 inline Vector<Size, Type>& Vector<Size, Type>::operator*=(const Vector<Size, Type>& rhs) throw()
00222 {
00223 for(uint32_t ct = 0; ct < Size; ++ct)
00224 {
00225 m_tElements[ct] *= rhs.m_tElements[ct];
00226 }
00227 return *this;
00228 }
00229
00235 template <uint32_t Size, typename Type>
00236 inline Vector<Size, Type>& Vector<Size, Type>::operator/=(const Vector<Size, Type>& rhs) throw()
00237 {
00238 for(uint32_t ct = 0; ct < Size; ++ct)
00239 {
00240 m_tElements[ct] /= rhs.m_tElements[ct];
00241 }
00242 return *this;
00243 }
00244
00250 template <uint32_t Size, typename Type>
00251 inline Vector<Size, Type>& Vector<Size, Type>::operator*=(const Type& rhs) throw()
00252 {
00253 for(uint32_t ct = 0; ct < Size; ++ct)
00254 {
00255 m_tElements[ct] *= rhs;
00256 }
00257 return *this;
00258 }
00259
00265 template <uint32_t Size, typename Type>
00266 inline Vector<Size, Type>& Vector<Size, Type>::operator/=(const Type& rhs) throw()
00267 {
00268 for(uint32_t ct = 0; ct < Size; ++ct)
00269 {
00270 m_tElements[ct] /= rhs;
00271 }
00272 return *this;
00273 }
00274
00281 template <uint32_t Size,typename Type>
00282 Vector<Size,Type> operator+(const Vector<Size,Type>& lhs, const Vector<Size,Type>& rhs) throw()
00283 {
00284 Vector<Size,Type> result = lhs;
00285 result += rhs;
00286 return result;
00287 }
00288
00295 template <uint32_t Size,typename Type>
00296 Vector<Size,Type> operator-(const Vector<Size,Type>& lhs, const Vector<Size,Type>& rhs) throw()
00297 {
00298 Vector<Size,Type> result = lhs;
00299 result -= rhs;
00300 return result;
00301 }
00302
00309 template <uint32_t Size,typename Type>
00310 Vector<Size,Type> operator*(const Vector<Size,Type>& lhs, const Vector<Size,Type>& rhs) throw()
00311 {
00312 Vector<Size,Type> result = lhs;
00313 result *= rhs;
00314 return result;
00315 }
00316
00323 template <uint32_t Size,typename Type>
00324 Vector<Size,Type> operator/(const Vector<Size,Type>& lhs, const Vector<Size,Type>& rhs) throw()
00325 {
00326 Vector<Size,Type> result = lhs;
00327 result /= rhs;
00328 return result;
00329 }
00330
00337 template <uint32_t Size,typename Type>
00338 Vector<Size,Type> operator*(const Vector<Size,Type>& lhs, const Type& rhs) throw()
00339 {
00340 Vector<Size,Type> result = lhs;
00341 result *= rhs;
00342 return result;
00343 }
00344
00351 template <uint32_t Size,typename Type>
00352 Vector<Size,Type> operator/(const Vector<Size,Type>& lhs, const Type& rhs) throw()
00353 {
00354 Vector<Size,Type> result = lhs;
00355 result /= rhs;
00356 return result;
00357 }
00358
00365 template <uint32_t Size, typename Type>
00366 std::ostream& operator<<(std::ostream& os, const Vector<Size, Type>& vector) throw()
00367 {
00368 os << "(";
00369 for(uint32_t ct = 0; ct < Size; ++ct)
00370 {
00371 os << vector.getElement(ct);
00372 if(ct < (Size-1))
00373 {
00374 os << ",";
00375 }
00376 }
00377 os << ")";
00378 return os;
00379 }
00380
00386 template <uint32_t Size, typename Type>
00387 inline Type Vector<Size, Type>::getElement(uint32_t index) const throw()
00388 {
00389 return m_tElements[index];
00390 }
00391
00395 template <uint32_t Size, typename Type>
00396 inline Type Vector<Size, Type>::getX(void) const throw()
00397 {
00398 return m_tElements[0];
00399 }
00400
00404 template <uint32_t Size, typename Type>
00405 inline Type Vector<Size, Type>::getY(void) const throw()
00406 {
00407 return m_tElements[1];
00408 }
00409
00413 template <uint32_t Size, typename Type>
00414 inline Type Vector<Size, Type>::getZ(void) const throw()
00415 {
00416 return m_tElements[2];
00417 }
00418
00422 template <uint32_t Size, typename Type>
00423 inline Type Vector<Size, Type>::getW(void) const throw()
00424 {
00425 return m_tElements[3];
00426 }
00427
00432 template <uint32_t Size, typename Type>
00433 inline void Vector<Size, Type>::setElement(uint32_t index, Type tValue) throw()
00434 {
00435 m_tElements[index] = tValue;
00436 }
00437
00443 template <uint32_t Size,typename Type>
00444 inline void Vector<Size,Type>::setElements(Type x, Type y) throw()
00445 {
00446 m_tElements[0] = x;
00447 m_tElements[1] = y;
00448
00449 }
00450
00457 template <uint32_t Size,typename Type>
00458 inline void Vector<Size,Type>::setElements(Type x, Type y, Type z) throw()
00459 {
00460 m_tElements[0] = x;
00461 m_tElements[1] = y;
00462 m_tElements[2] = z;
00463
00464 }
00465
00473 template <uint32_t Size,typename Type>
00474 inline void Vector<Size,Type>::setElements(Type x, Type y, Type z, Type w) throw()
00475 {
00476 m_tElements[0] = x;
00477 m_tElements[1] = y;
00478 m_tElements[2] = z;
00479 m_tElements[3] = w;
00480 }
00481
00485 template <uint32_t Size, typename Type>
00486 inline void Vector<Size, Type>::setX(Type tX) throw()
00487 {
00488 m_tElements[0] = tX;
00489 }
00490
00494 template <uint32_t Size, typename Type>
00495 inline void Vector<Size, Type>::setY(Type tY) throw()
00496 {
00497 m_tElements[1] = tY;
00498 }
00499
00503 template <uint32_t Size, typename Type>
00504 inline void Vector<Size, Type>::setZ(Type tZ) throw()
00505 {
00506 m_tElements[2] = tZ;
00507 }
00508
00512 template <uint32_t Size, typename Type>
00513 inline void Vector<Size, Type>::setW(Type tW) throw()
00514 {
00515 m_tElements[3] = tW;
00516 }
00517
00522 template <uint32_t Size, typename Type>
00523 inline double Vector<Size, Type>::length(void) const throw()
00524 {
00525 return sqrt(lengthSquared());
00526 }
00527
00531 template <uint32_t Size, typename Type>
00532 inline double Vector<Size, Type>::lengthSquared(void) const throw()
00533 {
00534 double result = 0.0f;
00535 for(uint32_t ct = 0; ct < Size; ++ct)
00536 {
00537 result += m_tElements[ct] * m_tElements[ct];
00538 }
00539 return result;
00540 }
00541
00551 template <uint32_t Size, typename Type>
00552 inline double Vector<Size, Type>::angleTo(const Vector<Size, Type>& vector) const throw()
00553 {
00554 return acos(dot(vector) / (vector.length() * this->length()));
00555 }
00556
00570 template <uint32_t Size, typename Type>
00571 inline Vector<Size, Type> Vector<Size, Type>::cross(const Vector<Size, Type>& vector) const throw()
00572 {
00573 Type i = vector.getZ() * this->getY() - vector.getY() * this->getZ();
00574 Type j = vector.getX() * this->getZ() - vector.getZ() * this->getX();
00575 Type k = vector.getY() * this->getX() - vector.getX() * this->getY();
00576 return Vector<Size, Type>(i,j,k);
00577 }
00578
00586 template <uint32_t Size, typename Type>
00587 inline Type Vector<Size, Type>::dot(const Vector<Size, Type>& rhs) const throw()
00588 {
00589 Type dotProduct = static_cast<Type>(0);
00590 for(uint32_t ct = 0; ct < Size; ++ct)
00591 {
00592 dotProduct += m_tElements[ct] * rhs.m_tElements[ct];
00593 }
00594 return dotProduct;
00595 }
00596
00602 template <uint32_t Size, typename Type>
00603 inline void Vector<Size, Type>::normalise(void) throw()
00604 {
00605 double length = this->length();
00606
00607 if(length < 0.0001f)
00608 {
00609 return;
00610 }
00611 for(uint32_t ct = 0; ct < Size; ++ct)
00612 {
00613 m_tElements[ct] /= static_cast<Type>(length);
00614 }
00615 }
00616 }