BsVector4.h 6.1 KB

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