2
0

BsVector4.h 6.1 KB

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