BsVector4.h 6.2 KB

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