home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 4819 / rage.7z / slVector3D.h < prev    next >
Encoding:
C/C++ Source or Header  |  2011-09-14  |  2.2 KB  |  122 lines

  1. #ifndef __SL_VECTOR_3D_H
  2. #define __SL_VECTOR_3D_H
  3.  
  4. #include<iostream>
  5. #include<cstring>
  6. #include<cmath>
  7.  
  8. #include "slMathTraits.h"
  9.  
  10. namespace sl {
  11.  
  12. template<class T> class vector3D;
  13. template<class T> class matrix4x4;
  14.  
  15. template<class T>
  16. class vector3D {
  17.   friend class matrix4x4<T>;
  18.  private :
  19.   T data[3];
  20.  public :
  21.   T& operator [](int index);
  22.   const T& operator [](int index)const;
  23.  public :
  24.   vector3D<T>& operator =(const vector3D& v);
  25.  public :
  26.   const T* c_ptr(void)const;
  27.   T length(void)const;
  28.   T length_squared(void)const;
  29.   void normalize(void);
  30.  public :
  31.   vector3D();
  32.   vector3D(T x, T y, T z);
  33.   vector3D(const vector3D& v);
  34.  ~vector3D();
  35. };
  36.  
  37. template<class T>
  38. inline vector3D<T>::vector3D()
  39. {
  40. }
  41.  
  42. template<class T>
  43. inline vector3D<T>::vector3D(T x, T y, T z)
  44. {
  45.  data[0] = x;
  46.  data[1] = y;
  47.  data[2] = z;
  48. }
  49.  
  50. template<class T>
  51. inline vector3D<T>::vector3D(const vector3D& v)
  52. {
  53.  data[0] = v.data[0];
  54.  data[1] = v.data[1];
  55.  data[2] = v.data[2];
  56. }
  57.  
  58. template<class T>
  59. inline vector3D<T>::~vector3D()
  60. {
  61. }
  62.  
  63. template<class T>
  64. inline vector3D<T>& vector3D<T>::operator =(const vector3D& v)
  65. {
  66.  if(this == &v) return *this;
  67.  data[0] = v.data[0];
  68.  data[1] = v.data[1];
  69.  data[2] = v.data[2];
  70.  return *this;
  71. }
  72.  
  73. template<class T>
  74. inline T& vector3D<T>::operator [](int index)
  75. {
  76.  return data[index];
  77. }
  78.  
  79. template<class T>
  80. inline const T& vector3D<T>::operator [](int index)const
  81. {
  82.  return data[index];
  83. }
  84.  
  85. template<class T>
  86. inline const T* vector3D<T>::c_ptr(void)const
  87. {
  88.  return &data[0];
  89. }
  90.  
  91. template<class T>
  92. inline T vector3D<T>::length(void)const
  93. {
  94.  return std::sqrt(data[0]*data[0] + data[1]*data[1] + data[2]*data[2]);
  95. }
  96.  
  97. template<class T>
  98. inline T vector3D<T>::length_squared(void)const
  99. {
  100.  return data[0]*data[0] + data[1]*data[1] + data[2]*data[2];
  101. }
  102.  
  103. template<class T>
  104. inline void vector3D<T>::normalize(void)
  105. {
  106.  T denom = math_traits<T>::one()/length();
  107.  data[0] *= denom;
  108.  data[1] *= denom;
  109.  data[2] *= denom;
  110. }
  111.  
  112. };
  113.  
  114. template<class T>
  115. inline ostream& operator <<(ostream& os, const sl::vector3D<T>& v)
  116. {
  117.  os << "<" << v[0] << "," << v[1] << "," << v[2] << ">";
  118.  return os;
  119. }
  120.  
  121. #endif
  122.