Vec3.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #ifndef Vec3_H
  2. #define Vec3_H
  3. #include "FrameworkMath.h"
  4. namespace Framework
  5. {
  6. template< typename T >
  7. //! Ein 3D Vektor
  8. class Vec3
  9. {
  10. public:
  11. T x; //! X Komponente des Vektors
  12. T y; //! y Komponente des Vektors
  13. T z; //! z Komponente des Vektors
  14. //! Konstruktor
  15. inline Vec3()
  16. {}
  17. //! Konstruktor
  18. //! \param x Die X Komponente des neuen Vektors
  19. //! \param y Die Y Komponente des neuen Vektors
  20. //! \param z Die Z Komponente des neuen Vektors
  21. inline Vec3( T x, T y, T z )
  22. : x( x ),
  23. y( y ),
  24. z( z )
  25. {}
  26. //! Konstruktor
  27. //! \param vect Ein Vektor, dessen Werte kopiert werden sollen
  28. inline Vec3( const Vec3 &vect )
  29. : Vec3( vect.x, vect.y, vect.z )
  30. {}
  31. //! Skalliert den Vektor, so dass er die Länge 1 hat
  32. inline Vec3 &normalize()
  33. {
  34. const T length = getLength();
  35. x /= length;
  36. y /= length;
  37. z /= length;
  38. return *this;
  39. }
  40. //! Vertaucht die Werte des Vektors mit denen eines anderen Vektor
  41. //! \param vect Der andere Vektor
  42. inline Vec3 &swap( Vec3 &vect )
  43. {
  44. const Vec3 tmp = vect;
  45. vect = *this;
  46. *this = tmp;
  47. return *this;
  48. }
  49. //! Berechnet einen winkel zwischen diesem und einem anderen Vektor
  50. inline float angle( Vec3 vect )
  51. {
  52. return lowPrecisionACos( (float)( *this * vect ) / ( (float)getLength() * (float)vect.getLength() ) );
  53. }
  54. //! Kopiert die Werte eines anderen Vektors
  55. //! \param r Der andere Vektor
  56. inline Vec3 operator=( const Vec3 & r )
  57. {
  58. x = r.x;
  59. y = r.y;
  60. z = r.z;
  61. return *this;
  62. }
  63. //! Addiert einen anderen Vektor zu diesem Hinzu
  64. //! \param r Der andere Vektor
  65. inline Vec3 operator+=( const Vec3 & r )
  66. {
  67. x += r.x;
  68. y += r.y;
  69. z += r.z;
  70. return *this;
  71. }
  72. //! Zieht einen anderen Vektor von diesem ab
  73. //! \param r Der andere Vektor
  74. inline Vec3 operator-=( const Vec3 & r )
  75. {
  76. x -= r.x;
  77. y -= r.y;
  78. z -= r.z;
  79. return *this;
  80. }
  81. //! Skalliert diesen Vektor
  82. //! \param r Der Faktor
  83. inline Vec3 operator*=( const T & r )
  84. {
  85. x *= r;
  86. y *= r;
  87. z *= r;
  88. return *this;
  89. }
  90. //! Skalliert diesen Vektor mit 1/Faktor
  91. //! \param r Der Faktor
  92. inline Vec3 operator/=( const T & r )
  93. {
  94. x /= r;
  95. y /= r;
  96. z /= r;
  97. return *this;
  98. }
  99. //! Errechnet das Quadrat des Abstands zwischen zewi Vektoren
  100. //! \param p Der andere Vektor
  101. inline T abstandSq( const Vec3 & p ) const
  102. {
  103. return ( x - p.x ) * ( x - p.x ) + ( y - p.y ) * ( y - p.y ) + ( z - p.z ) * ( z - p.z );
  104. }
  105. //! Errechnet den Abstand zwischen zwei Vektoren
  106. //! \param p Der andere Vektor
  107. inline T abstand( const Vec3 & p ) const
  108. {
  109. return sqrt( abstandSq( p ) );
  110. }
  111. //! Gibt einen neuen Vektor zurück, der die negation von diesem ist
  112. inline Vec3 operator-() const
  113. {
  114. return{ -x, -y, -z };
  115. }
  116. template< typename T2 >
  117. //! Konvertiert den Typ des Vektors in einen anderen
  118. inline operator Vec3< T2 >() const
  119. {
  120. return{ (T2)x, (T2)y, (T2)z };
  121. }
  122. //! Errechnet das Quadrat der Länge des Vektors
  123. inline T getLengthSq() const
  124. {
  125. return *this **this;
  126. }
  127. //! Errechnet due Länge des Vektors
  128. inline T getLength() const
  129. {
  130. return (T)sqrt( getLengthSq() );
  131. }
  132. //! Bildet das Skalarprodukt zwischen zwei Vektoren
  133. //! \param r Der andere Vektor
  134. inline T operator*( const Vec3 & r ) const
  135. {
  136. return x * r.x + y * r.y + z * r.z;
  137. }
  138. //! Errechnet die Summe zweier Vektoren
  139. //! \param r Der andere Vektor
  140. inline Vec3 operator+( const Vec3 & r ) const
  141. {
  142. return Vec3( *this ) += r;
  143. }
  144. //! Zieht zwei Vektoren von einander ab
  145. //! \param r Der andere Vektor
  146. inline Vec3 operator-( const Vec3 & r ) const
  147. {
  148. return Vec3( *this ) -= r;
  149. }
  150. //! Skalliert den Vektor ohne ihn zu verändern
  151. //! \param r Der Faktor
  152. inline Vec3 operator*( const T & r ) const
  153. {
  154. return Vec3( *this ) *= r;
  155. }
  156. //! Skalliert den Vektor mit 1/Faktor ohne ihn zu Verändern
  157. //! \param r Der Faktor
  158. inline Vec3 operator/( const T & r ) const
  159. {
  160. return Vec3( *this ) /= r;
  161. }
  162. //! Überprüft zwei Vektoren auf Gleichheit
  163. //! \param r Der andere Vektor
  164. inline bool operator==( const Vec3 & r ) const
  165. {
  166. return x == r.x && y == r.y && z == r.z;
  167. }
  168. //! Überprüft zwei Vektoren auf Ungleichheit
  169. //! \param r Der andere Vektor
  170. inline bool operator!=( const Vec3 & r ) const
  171. {
  172. return !( *this == r );
  173. }
  174. //! Gibt das Kreutzprodukt zwischen diesem und einem anderen Vector zurück
  175. //! \param b der andere Vector
  176. inline Vec3 crossProduct( const Vec3 & b ) const
  177. {
  178. return Vec3( y * b.z - z * b.y, z * b.x - x * b.z, x * b.y - y * b.x );
  179. }
  180. inline void rotateY( float radian )
  181. {
  182. const float cosTheta = lowPrecisionCos( radian );
  183. const float sinTheta = lowPrecisionSin( radian );
  184. float x = cosTheta * this->x + sinTheta * this->z;
  185. float z = -sinTheta * this->x + cosTheta * this->z;
  186. this->x = (T)x;
  187. this->z = (T)z;
  188. }
  189. inline void rotateX( float radian )
  190. {
  191. const float cosTheta = lowPrecisionCos( radian );
  192. const float sinTheta = lowPrecisionSin( radian );
  193. float y = cosTheta * this->y + -sinTheta * this->z;
  194. float z = sinTheta * this->y + cosTheta * this->z;
  195. this->y = (T)y;
  196. this->z = (T)z;
  197. }
  198. inline void rotateZ( float radian )
  199. {
  200. const float cosTheta = lowPrecisionCos( radian );
  201. const float sinTheta = lowPrecisionSin( radian );
  202. float x = cosTheta * this->x + -sinTheta * this->y;
  203. this->x = (T)x;
  204. this->z = (T)z;
  205. }
  206. };
  207. }
  208. #endif