CmVector2.h 9.0 KB

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