BsVector4.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsVector3.h"
  4. namespace BansheeEngine
  5. {
  6. /**
  7. * @brief A four dimensional vector.
  8. */
  9. class BS_UTILITY_EXPORT Vector4
  10. {
  11. public:
  12. float x, y, z, w;
  13. public:
  14. Vector4()
  15. :x(0.0f), y(0.0f), z(0.0f), w(0.0f)
  16. { }
  17. Vector4(float x, float y, float z, float w)
  18. :x(x), y(y), z(z), w(w)
  19. { }
  20. explicit Vector4(const Vector3& vec, float w = 0.0f)
  21. :x(vec.x), y(vec.y), z(vec.z), w(w)
  22. { }
  23. /**
  24. * @brief Exchange the contents of this vector with another.
  25. */
  26. void swap(Vector4& other)
  27. {
  28. std::swap(x, other.x);
  29. std::swap(y, other.y);
  30. std::swap(z, other.z);
  31. std::swap(w, other.w);
  32. }
  33. float operator[] (UINT32 i) const
  34. {
  35. assert (i < 4);
  36. return *(&x+i);
  37. }
  38. float& operator[] (UINT32 i)
  39. {
  40. assert(i < 4);
  41. return *(&x+i);
  42. }
  43. /**
  44. * @brief Pointer accessor for direct copying.
  45. */
  46. float* ptr()
  47. {
  48. return &x;
  49. }
  50. /**
  51. * @brief Pointer accessor for direct copying.
  52. */
  53. const float* ptr() const
  54. {
  55. return &x;
  56. }
  57. Vector4& operator= (const Vector4& rhs)
  58. {
  59. x = rhs.x;
  60. y = rhs.y;
  61. z = rhs.z;
  62. w = rhs.w;
  63. return *this;
  64. }
  65. Vector4& operator= (float rhs)
  66. {
  67. x = rhs;
  68. y = rhs;
  69. z = rhs;
  70. w = rhs;
  71. return *this;
  72. }
  73. bool operator== (const Vector4& rhs) const
  74. {
  75. return (x == rhs.x && y == rhs.y && z == rhs.z && w == rhs.w);
  76. }
  77. bool operator!= (const Vector4& rhs) const
  78. {
  79. return (x != rhs.x || y != rhs.y || z != rhs.z || w != rhs.w);
  80. }
  81. Vector4& operator= (const Vector3& rhs)
  82. {
  83. x = rhs.x;
  84. y = rhs.y;
  85. z = rhs.z;
  86. w = 1.0f;
  87. return *this;
  88. }
  89. Vector4 operator+ (const Vector4& rhs) const
  90. {
  91. return Vector4(x + rhs.x, y + rhs.y, z + rhs.z, w + rhs.w);
  92. }
  93. Vector4 operator- (const Vector4& rhs) const
  94. {
  95. return Vector4(x - rhs.x, y - rhs.y, z - rhs.z, w - rhs.w);
  96. }
  97. Vector4 operator* (float rhs) const
  98. {
  99. return Vector4(x * rhs, y * rhs, z * rhs, w * rhs);
  100. }
  101. Vector4 operator* (const Vector4& rhs) const
  102. {
  103. return Vector4(rhs.x * x, rhs.y * y, rhs.z * z, rhs.w * w);
  104. }
  105. Vector4 operator/ (float rhs) const
  106. {
  107. assert(rhs != 0.0f);
  108. float inv = 1.0f / rhs;
  109. return Vector4(x * inv, y * inv, z * inv, w * inv);
  110. }
  111. Vector4 operator/ (const Vector4& rhs) const
  112. {
  113. return Vector4(x / rhs.x, y / rhs.y, z / rhs.z, w / rhs.w);
  114. }
  115. const Vector4& operator+ () const
  116. {
  117. return *this;
  118. }
  119. Vector4 operator- () const
  120. {
  121. return Vector4(-x, -y, -z, -w);
  122. }
  123. friend Vector4 operator* (float lhs, const Vector4& rhs)
  124. {
  125. return Vector4(lhs * rhs.x, lhs * rhs.y, lhs * rhs.z, lhs * rhs.w);
  126. }
  127. friend Vector4 operator/ (float lhs, const Vector4& rhs)
  128. {
  129. return Vector4(lhs / rhs.x, lhs / rhs.y, lhs / rhs.z, lhs / rhs.w);
  130. }
  131. friend Vector4 operator+ (const Vector4& lhs, float rhs)
  132. {
  133. return Vector4(lhs.x + rhs, lhs.y + rhs, lhs.z + rhs, lhs.w + rhs);
  134. }
  135. friend Vector4 operator+ (float lhs, const Vector4& rhs)
  136. {
  137. return Vector4(lhs + rhs.x, lhs + rhs.y, lhs + rhs.z, lhs + rhs.w);
  138. }
  139. friend Vector4 operator- (const Vector4& lhs, float rhs)
  140. {
  141. return Vector4(lhs.x - rhs, lhs.y - rhs, lhs.z - rhs, lhs.w - rhs);
  142. }
  143. friend Vector4 operator- (float lhs, Vector4& rhs)
  144. {
  145. return Vector4(lhs - rhs.x, lhs - rhs.y, lhs - rhs.z, lhs - rhs.w);
  146. }
  147. Vector4& operator+= (const Vector4& rhs)
  148. {
  149. x += rhs.x;
  150. y += rhs.y;
  151. z += rhs.z;
  152. w += rhs.w;
  153. return *this;
  154. }
  155. Vector4& operator-= (const Vector4& rhs)
  156. {
  157. x -= rhs.x;
  158. y -= rhs.y;
  159. z -= rhs.z;
  160. w -= rhs.w;
  161. return *this;
  162. }
  163. Vector4& operator*= (float rhs)
  164. {
  165. x *= rhs;
  166. y *= rhs;
  167. z *= rhs;
  168. w *= rhs;
  169. return *this;
  170. }
  171. Vector4& operator+= (float rhs)
  172. {
  173. x += rhs;
  174. y += rhs;
  175. z += rhs;
  176. w += rhs;
  177. return *this;
  178. }
  179. Vector4& operator-= (float rhs)
  180. {
  181. x -= rhs;
  182. y -= rhs;
  183. z -= rhs;
  184. w -= rhs;
  185. return *this;
  186. }
  187. Vector4& operator*= (Vector4& rhs)
  188. {
  189. x *= rhs.x;
  190. y *= rhs.y;
  191. z *= rhs.z;
  192. w *= rhs.w;
  193. return *this;
  194. }
  195. Vector4& operator/= (float rhs)
  196. {
  197. assert(rhs != 0.0f);
  198. float inv = 1.0f / rhs;
  199. x *= inv;
  200. y *= inv;
  201. z *= inv;
  202. w *= inv;
  203. return *this;
  204. }
  205. Vector4& operator/= (const Vector4& rhs)
  206. {
  207. x /= rhs.x;
  208. y /= rhs.y;
  209. z /= rhs.z;
  210. w /= rhs.w;
  211. return *this;
  212. }
  213. /**
  214. * @brief Calculates the dot (scalar) product of this vector with another.
  215. */
  216. float dot(const Vector4& vec) const
  217. {
  218. return x * vec.x + y * vec.y + z * vec.z + w * vec.w;
  219. }
  220. /**
  221. * @brief Checks are any of the vector components NaN.
  222. */
  223. bool isNaN() const
  224. {
  225. return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z) || Math::isNaN(w);
  226. }
  227. static const Vector4 ZERO;
  228. };
  229. BS_ALLOW_MEMCPY_SERIALIZATION(Vector4);
  230. }