CmVector2.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. #pragma once
  2. #include "CmPrerequisitesUtil.h"
  3. #include "CmMath.h"
  4. namespace CamelotFramework
  5. {
  6. /**
  7. * @brief A two dimensional vector.
  8. */
  9. class CM_UTILITY_EXPORT Vector2
  10. {
  11. public:
  12. float x, y;
  13. public:
  14. Vector2()
  15. { }
  16. Vector2(float x, float y)
  17. : x(x), y(y)
  18. { }
  19. /**
  20. * @brief Exchange the contents of this vector with another.
  21. */
  22. void swap(Vector2& other)
  23. {
  24. std::swap(x, other.x);
  25. std::swap(y, other.y);
  26. }
  27. float operator[] (UINT32 i) const
  28. {
  29. assert(i < 2);
  30. return *(&x+i);
  31. }
  32. float& operator[] (UINT32 i)
  33. {
  34. assert(i < 2);
  35. return *(&x+i);
  36. }
  37. float* ptr()
  38. {
  39. return &x;
  40. }
  41. const float* ptr() const
  42. {
  43. return &x;
  44. }
  45. Vector2& operator= (const Vector2& rhs)
  46. {
  47. x = rhs.x;
  48. y = rhs.y;
  49. return *this;
  50. }
  51. Vector2& operator= (float rhs)
  52. {
  53. x = rhs;
  54. y = rhs;
  55. return *this;
  56. }
  57. bool operator== (const Vector2& rhs) const
  58. {
  59. return (x == rhs.x && y == rhs.y);
  60. }
  61. bool operator!= (const Vector2& rhs) const
  62. {
  63. return (x != rhs.x || y != rhs.y);
  64. }
  65. Vector2 operator+ (const Vector2& rhs) const
  66. {
  67. return Vector2(x + rhs.x, y + rhs.y);
  68. }
  69. Vector2 operator- (const Vector2& rhs) const
  70. {
  71. return Vector2(x - rhs.x, y - rhs.y);
  72. }
  73. Vector2 operator* (const float rhs) const
  74. {
  75. return Vector2(x * rhs, y * rhs);
  76. }
  77. Vector2 operator* (const Vector2& rhs) const
  78. {
  79. return Vector2(x * rhs.x, y * rhs.y);
  80. }
  81. Vector2 operator/ (const float rhs) const
  82. {
  83. assert(rhs != 0.0);
  84. float fInv = 1.0f / rhs;
  85. return Vector2(x * fInv, y * fInv);
  86. }
  87. Vector2 operator/ (const Vector2& rhs) const
  88. {
  89. return Vector2(x / rhs.x, y / rhs.y);
  90. }
  91. const Vector2& operator+ () const
  92. {
  93. return *this;
  94. }
  95. Vector2 operator- () const
  96. {
  97. return Vector2(-x, -y);
  98. }
  99. friend Vector2 operator* (float lhs, const Vector2& rhs)
  100. {
  101. return Vector2(lhs * rhs.x, lhs * rhs.y);
  102. }
  103. friend Vector2 operator/ (float lhs, const Vector2& rhs)
  104. {
  105. return Vector2(lhs / rhs.x, lhs / rhs.y);
  106. }
  107. friend Vector2 operator+ (Vector2& lhs, float rhs)
  108. {
  109. return Vector2(lhs.x + rhs, lhs.y + rhs);
  110. }
  111. friend Vector2 operator+ (float lhs, const Vector2& rhs)
  112. {
  113. return Vector2(lhs + rhs.x, lhs + rhs.y);
  114. }
  115. friend Vector2 operator- (const Vector2& lhs, float rhs)
  116. {
  117. return Vector2(lhs.x - rhs, lhs.y - rhs);
  118. }
  119. friend Vector2 operator- (const float lhs, const Vector2& rhs)
  120. {
  121. return Vector2(lhs - rhs.x, lhs - rhs.y);
  122. }
  123. Vector2& operator+= (const Vector2& rhs)
  124. {
  125. x += rhs.x;
  126. y += rhs.y;
  127. return *this;
  128. }
  129. Vector2& operator+= (float rhs)
  130. {
  131. x += rhs;
  132. y += rhs;
  133. return *this;
  134. }
  135. Vector2& operator-= (const Vector2& rhs)
  136. {
  137. x -= rhs.x;
  138. y -= rhs.y;
  139. return *this;
  140. }
  141. Vector2& operator-= (float rhs)
  142. {
  143. x -= rhs;
  144. y -= rhs;
  145. return *this;
  146. }
  147. Vector2& operator*= (float rhs)
  148. {
  149. x *= rhs;
  150. y *= rhs;
  151. return *this;
  152. }
  153. Vector2& operator*= (const Vector2& rhs)
  154. {
  155. x *= rhs.x;
  156. y *= rhs.y;
  157. return *this;
  158. }
  159. Vector2& operator/= (float rhs)
  160. {
  161. assert(rhs != 0.0f);
  162. float inv = 1.0f / rhs;
  163. x *= inv;
  164. y *= inv;
  165. return *this;
  166. }
  167. Vector2& operator/= (const Vector2& rhs)
  168. {
  169. x /= rhs.x;
  170. y /= rhs.y;
  171. return *this;
  172. }
  173. /**
  174. * @brief Returns the length (magnitude) of the vector.
  175. */
  176. float length() const
  177. {
  178. return Math::sqrt(x * x + y * y);
  179. }
  180. /**
  181. * @brief Returns the square of the length(magnitude) of the vector.
  182. */
  183. float squaredLength() const
  184. {
  185. return x * x + y * y;
  186. }
  187. /**
  188. * @brief Returns the distance to another vector.
  189. */
  190. float distance(const Vector2& rhs) const
  191. {
  192. return (*this - rhs).length();
  193. }
  194. /**
  195. * @brief Returns the square of the distance to another vector.
  196. */
  197. float sqrdDistance(const Vector2& rhs) const
  198. {
  199. return (*this - rhs).squaredLength();
  200. }
  201. /**
  202. * @brief Calculates the dot (scalar) product of this vector with another.
  203. */
  204. float dot(const Vector2& vec) const
  205. {
  206. return x * vec.x + y * vec.y;
  207. }
  208. /**
  209. * @brief Normalizes the vector.
  210. */
  211. float normalize()
  212. {
  213. float len = Math::sqrt(x * x + y * y);
  214. // Will also work for zero-sized vectors, but will change nothing
  215. if (len > 1e-08)
  216. {
  217. float invLen = 1.0f / len;
  218. x *= invLen;
  219. y *= invLen;
  220. }
  221. return len;
  222. }
  223. /**
  224. * @brief Generates a vector perpendicular to this vector.
  225. */
  226. Vector2 perpendicular() const
  227. {
  228. return Vector2 (-y, x);
  229. }
  230. /**
  231. * @brief Calculates the 2 dimensional cross-product of 2 vectors, which results
  232. * in a single floating point value which is 2 times the area of the triangle.
  233. */
  234. float cross(const Vector2& other) const
  235. {
  236. return x * other.y - y * other.x;
  237. }
  238. /**
  239. * @brief Sets this vector's components to the minimum of its own and the
  240. * ones of the passed in vector.
  241. */
  242. void floor(const Vector2& cmp)
  243. {
  244. if(cmp.x < x) x = cmp.x;
  245. if(cmp.y < y) y = cmp.y;
  246. }
  247. /**
  248. * @brief Sets this vector's components to the maximum of its own and the
  249. * ones of the passed in vector.
  250. */
  251. void ceil(const Vector2& cmp)
  252. {
  253. if(cmp.x > x) x = cmp.x;
  254. if(cmp.y > y) y = cmp.y;
  255. }
  256. /**
  257. * @brief Returns true if this vector is zero length.
  258. */
  259. bool isZeroLength() const
  260. {
  261. float sqlen = (x * x) + (y * y);
  262. return (sqlen < (1e-06 * 1e-06));
  263. }
  264. /**
  265. * @brief Calculates a reflection vector to the plane with the given normal.
  266. */
  267. Vector2 reflect(const Vector2& normal) const
  268. {
  269. return Vector2(*this - (2 * this->dot(normal) * normal));
  270. }
  271. /**
  272. * @brief Performs Gram-Schmidt orthonormalization
  273. */
  274. static void orthonormalize(Vector2& u, Vector2& v)
  275. {
  276. u.normalize();
  277. float dot = u.dot(v);
  278. v -= u*dot;
  279. v.normalize();
  280. }
  281. /**
  282. * @brief Normalizes the provided vector and returns a new normalized instance.
  283. */
  284. static Vector2 normalize(const Vector2& val)
  285. {
  286. float len = Math::sqrt(val.x * val.x + val.y * val.y);
  287. // Will also work for zero-sized vectors, but will change nothing
  288. Vector2 normalizedVec;
  289. if (len > 1e-08)
  290. {
  291. float invLen = 1.0f / len;
  292. normalizedVec.x *= invLen;
  293. normalizedVec.y *= invLen;
  294. }
  295. return normalizedVec;
  296. }
  297. /**
  298. * @brief Checks are any of the vector components NaN.
  299. */
  300. bool isNaN() const
  301. {
  302. return Math::isNaN(x) || Math::isNaN(y);
  303. }
  304. /**
  305. * @brief Returns the minimum of all the vector components as a
  306. * new vector.
  307. */
  308. static Vector2 min(const Vector2& a, const Vector2& b)
  309. {
  310. return Vector2(std::min(a.x, b.x), std::min(a.y, b.y));
  311. }
  312. /**
  313. * @brief Returns the maximum of all the vector components as a
  314. * new vector.
  315. */
  316. static Vector2 max(const Vector2& a, const Vector2& b)
  317. {
  318. return Vector2(std::max(a.x, b.x), std::max(a.y, b.y));
  319. }
  320. static const Vector2 ZERO;
  321. static const Vector2 ONE;
  322. static const Vector2 UNIT_X;
  323. static const Vector2 UNIT_Y;
  324. };
  325. CM_ALLOW_MEMCPY_SERIALIZATION(Vector2);
  326. }