Vec4.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #pragma once
  2. namespace Framework
  3. {
  4. template<typename T>
  5. //! A 3D vector
  6. class Vec4
  7. {
  8. public:
  9. T x; //! X component of the vector
  10. T y; //! Y component of the vector
  11. T z; //! Z component of the vector
  12. T w; //! W component of the vector
  13. //! Constructor
  14. inline Vec4() {}
  15. //! Constructor
  16. //! \param x The X component of the new vector
  17. //! \param y The Y component of the new vector
  18. //! \param z The Z component of the new vector
  19. //! \param w The W component of the new vector
  20. inline Vec4(T x, T y, T z, T w)
  21. : x(x),
  22. y(y),
  23. z(z),
  24. w(w)
  25. {}
  26. //! Constructor
  27. //! \param vect A vector whose values should be copied
  28. inline Vec4(const Vec4& vect)
  29. : Vec4(vect.x, vect.y, vect.z, vect.w)
  30. {}
  31. //! Scales the vector so that it has length 1
  32. inline Vec4& normalize()
  33. {
  34. const T length = getLength();
  35. x /= length;
  36. y /= length;
  37. z /= length;
  38. w /= length;
  39. return *this;
  40. }
  41. //! Swaps the values of the vector with those of another vector
  42. //! \param vect The other vector
  43. inline Vec4& swap(Vec4& vect)
  44. {
  45. const Vec4 tmp = vect;
  46. vect = *this;
  47. *this = tmp;
  48. return *this;
  49. }
  50. //! Calculates an angle between this and another vector
  51. inline float angle(Vec4 vect)
  52. {
  53. return lowPrecisionACos(
  54. (float)(*this * vect)
  55. / ((float)getLength() * (float)vect.getLength()));
  56. }
  57. //! Copies the values of another vector
  58. //! \param r The other vector
  59. inline Vec4 operator=(const Vec4& r)
  60. {
  61. x = r.x;
  62. y = r.y;
  63. z = r.z;
  64. w = r.w;
  65. return *this;
  66. }
  67. //! Adds another vector to this one
  68. //! \param r The other vector
  69. inline Vec4 operator+=(const Vec4& r)
  70. {
  71. x += r.x;
  72. y += r.y;
  73. z += r.z;
  74. w += r.w;
  75. return *this;
  76. }
  77. //! Subtracts another vector from this one
  78. //! \param r The other vector
  79. inline Vec4 operator-=(const Vec4& r)
  80. {
  81. x -= r.x;
  82. y -= r.y;
  83. z -= r.z;
  84. w -= r.w;
  85. return *this;
  86. }
  87. //! Scales this vector
  88. //! \param r The factor
  89. inline Vec4 operator*=(const T& r)
  90. {
  91. x *= r;
  92. y *= r;
  93. z *= r;
  94. w *= r;
  95. return *this;
  96. }
  97. //! Scales this vector by 1/factor
  98. //! \param r The factor
  99. inline Vec4 operator/=(const T& r)
  100. {
  101. x /= r;
  102. y /= r;
  103. z /= r;
  104. w /= r;
  105. return *this;
  106. }
  107. //! Calculates the square of the distance between two vectors
  108. //! \param p The other vector
  109. inline T distanceSq(const Vec4& p) const
  110. {
  111. return (x - p.x) * (x - p.x) + (y - p.y) * (y - p.y)
  112. + (z - p.z) * (z - p.z) + (w - p.w) * (w - p.w);
  113. }
  114. //! Calculates the distance between two vectors
  115. //! \param p The other vector
  116. inline T distance(const Vec4& p) const
  117. {
  118. return sqrt(distanceSq(p));
  119. }
  120. //! Returns a new vector that is the negation of this one
  121. inline Vec4 operator-() const
  122. {
  123. return {-x, -y, -z, -w};
  124. }
  125. template<typename T2>
  126. //! Converts the vector type to another
  127. inline operator Vec4<T2>() const
  128. {
  129. return {(T2)x, (T2)y, (T2)z, (T2)w};
  130. }
  131. //! Calculates the square of the vector's length
  132. inline T getLengthSq() const
  133. {
  134. return *this * *this;
  135. }
  136. //! Calculates the length of the vector
  137. inline T getLength() const
  138. {
  139. return (T)sqrt(getLengthSq());
  140. }
  141. //! Calculates the dot product between two vectors
  142. //! \param r The other vector
  143. inline T operator*(const Vec4& r) const
  144. {
  145. return x * r.x + y * r.y + z * r.z + w * r.w;
  146. }
  147. //! Calculates the sum of two vectors
  148. //! \param r The other vector
  149. inline Vec4 operator+(const Vec4& r) const
  150. {
  151. return Vec4(*this) += r;
  152. }
  153. //! Subtracts two vectors from each other
  154. //! \param r The other vector
  155. inline Vec4 operator-(const Vec4& r) const
  156. {
  157. return Vec4(*this) -= r;
  158. }
  159. //! Scales the vector without modifying it
  160. //! \param r The factor
  161. inline Vec4 operator*(const T& r) const
  162. {
  163. return Vec4(*this) *= r;
  164. }
  165. //! Scales the vector by 1/factor without modifying it
  166. //! \param r The factor
  167. inline Vec4 operator/(const T& r) const
  168. {
  169. return Vec4(*this) /= r;
  170. }
  171. //! Checks two vectors for equality
  172. //! \param r The other vector
  173. inline bool operator==(const Vec4& r) const
  174. {
  175. return x == r.x && y == r.y && z == r.z && w == r.w;
  176. }
  177. //! Checks two vectors for inequality
  178. //! \param r The other vector
  179. inline bool operator!=(const Vec4& r) const
  180. {
  181. return !(*this == r);
  182. }
  183. //! Returns the cross product between this and another vector
  184. //! \param b The other vector
  185. inline Vec4 crossProduct(const Vec4& b) const
  186. {
  187. return Vec4(y * b.z - z * b.y,
  188. z * b.x - x * b.z,
  189. x * b.y - y * b.x,
  190. w * b.w - w * b.w);
  191. }
  192. };
  193. } // namespace Framework