CmVector4.h 6.1 KB

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