Vec2.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. #ifndef ANKI_MATH_VEC2_H
  2. #define ANKI_MATH_VEC2_H
  3. #include "anki/math/CommonIncludes.h"
  4. namespace anki {
  5. /// @addtogroup Math
  6. /// @{
  7. /// 2D vector
  8. template<typename T>
  9. class TVec2
  10. {
  11. /// @name Friends
  12. template<typename Y>
  13. friend TVec2<Y> operator+(const Y f, const TVec2<Y>& v2);
  14. template<typename Y>
  15. friend TVec2<Y> operator-(const Y f, const TVec2<Y>& v2);
  16. template<typename Y>
  17. friend TVec2<Y> operator*(const Y f, const TVec2<Y>& v2);
  18. template<typename Y>
  19. friend TVec2<Y> operator/(const Y f, const TVec2<Y>& v2);
  20. ///@]
  21. public:
  22. /// @name Constructors
  23. /// @{
  24. explicit TVec2()
  25. {}
  26. explicit TVec2(const T x_, const T y_)
  27. {
  28. x() = x_;
  29. y() = y_;
  30. }
  31. explicit TVec2(const T f)
  32. {
  33. x() = y() = f;
  34. }
  35. explicit TVec2(const T arr_[])
  36. {
  37. x() = arr_[0];
  38. y() = arr_[1];
  39. }
  40. TVec2(const TVec2& b)
  41. {
  42. x() = b.x();
  43. y() = b.y();
  44. }
  45. explicit TVec2(const TVec3<T>& v3)
  46. {
  47. x() = v3.x();
  48. y() = v3.y();
  49. }
  50. explicit TVec2(const TVec4<T>& v4)
  51. {
  52. x() = v4.x();
  53. y() = v4.y();
  54. }
  55. /// @}
  56. /// @name Accessors
  57. /// @{
  58. T& x()
  59. {
  60. return vec.x;
  61. }
  62. T x() const
  63. {
  64. return vec.x;
  65. }
  66. T& y()
  67. {
  68. return vec.y;
  69. }
  70. T y() const
  71. {
  72. return vec.y;
  73. }
  74. T& operator[](const U i)
  75. {
  76. return arr[i];
  77. }
  78. T operator[](const U i) const
  79. {
  80. return arr[i];
  81. }
  82. /// @}
  83. /// @name Operators with same type
  84. /// @{
  85. TVec2& operator=(const TVec2& b)
  86. {
  87. x() = b.x();
  88. y() = b.y();
  89. return *this;
  90. }
  91. TVec2 operator+(const TVec2& b) const
  92. {
  93. return TVec2(x() + b.x(), y() + b.y());
  94. }
  95. TVec2& operator+=(const TVec2& b)
  96. {
  97. x() += b.x();
  98. y() += b.y();
  99. return (*this);
  100. }
  101. TVec2 operator-(const TVec2& b) const
  102. {
  103. return TVec2(x() - b.x(), y() - b.y());
  104. }
  105. TVec2& operator-=(const TVec2& b)
  106. {
  107. x() -= b.x();
  108. y() -= b.y();
  109. return (*this);
  110. }
  111. TVec2 operator*(const TVec2& b) const
  112. {
  113. return TVec2(x() * b.x(), y() * b.y());
  114. }
  115. TVec2& operator*=(const TVec2& b)
  116. {
  117. x() *= b.x();
  118. y() *= b.y();
  119. return (*this);
  120. }
  121. TVec2 operator/(const TVec2& b) const
  122. {
  123. return TVec2(x() / b.x(), y() / b.y());
  124. }
  125. TVec2& operator/=(const TVec2& b)
  126. {
  127. x() /= b.x();
  128. y() /= b.y();
  129. return (*this);
  130. }
  131. TVec2 operator-() const
  132. {
  133. return TVec2(-x(), -y());
  134. }
  135. Bool operator==(const TVec2& b) const
  136. {
  137. return isZero<T>(x() - b.x()) && isZero<T>(y() - b.y());
  138. }
  139. Bool operator!=(const TVec2& b) const
  140. {
  141. return !(*this == b);
  142. }
  143. Bool operator<(const TVec2& b) const
  144. {
  145. return vec.x < b.vec.x && vec.y < b.vec.y;
  146. }
  147. Bool operator<=(const TVec2& b) const
  148. {
  149. return vec.x <= b.vec.x && vec.y <= b.vec.y;
  150. }
  151. Bool operator>(const TVec2& b) const
  152. {
  153. return vec.x > b.vec.x && vec.y > b.vec.y;
  154. }
  155. Bool operator>=(const TVec2& b) const
  156. {
  157. return vec.x >= b.vec.x && vec.y >= b.vec.y;
  158. }
  159. /// @}
  160. /// @name Operators with T
  161. /// @{
  162. TVec2 operator+(const T f) const
  163. {
  164. return (*this) + TVec2(f);
  165. }
  166. TVec2& operator+=(const T f)
  167. {
  168. (*this) += TVec2(f);
  169. return (*this);
  170. }
  171. TVec2 operator-(const T f) const
  172. {
  173. return (*this) - TVec2(f);
  174. }
  175. TVec2& operator-=(const T f)
  176. {
  177. (*this) -= TVec2(f);
  178. return (*this);
  179. }
  180. TVec2 operator*(const T f) const
  181. {
  182. return (*this) * TVec2(f);
  183. }
  184. TVec2& operator*=(const T f)
  185. {
  186. (*this) *= TVec2(f);
  187. return (*this);
  188. }
  189. TVec2 operator/(const T f) const
  190. {
  191. return (*this) / TVec2(f);
  192. }
  193. TVec2& operator/=(const T f)
  194. {
  195. (*this) /= TVec2(f);
  196. return (*this);
  197. }
  198. /// @}
  199. /// @name Other
  200. /// @{
  201. T getLengthSquared() const
  202. {
  203. return x() * x() + y() * y();
  204. }
  205. T getLength() const
  206. {
  207. return sqrt<T>(getLengthSquared());
  208. }
  209. TVec2 getNormalized() const
  210. {
  211. return (*this) / getLength();
  212. }
  213. void normalize()
  214. {
  215. (*this) /= getLength();
  216. }
  217. T dot(const TVec2& b) const
  218. {
  219. return x() * b.x() + y() * b.y();
  220. }
  221. std::string toString() const
  222. {
  223. return std::to_string(x()) + " " + std::to_string(y());
  224. }
  225. /// @}
  226. private:
  227. /// @name Data members
  228. /// @{
  229. union
  230. {
  231. struct
  232. {
  233. T x, y;
  234. } vec;
  235. Array<T, 2> arr;
  236. };
  237. /// @}
  238. };
  239. template<typename T>
  240. TVec2<T> operator+(const T f, const TVec2<T>& v2)
  241. {
  242. return v2 + f;
  243. }
  244. template<typename T>
  245. TVec2<T> operator-(const T f, const TVec2<T>& v2)
  246. {
  247. return TVec2<T>(f - v2.x(), f - v2.y());
  248. }
  249. template<typename T>
  250. TVec2<T> operator*(const T f, const TVec2<T>& v2)
  251. {
  252. return v2 * f;
  253. }
  254. template<typename T>
  255. TVec2<T> operator/(const T f, const TVec2<T>& v2)
  256. {
  257. return TVec2<T>(f / v2.x(), f / v2.y());
  258. }
  259. /// F32 2D vector
  260. typedef TVec2<F32> Vec2;
  261. static_assert(sizeof(Vec2) == sizeof(F32) * 2, "Incorrect size");
  262. /// Half float 2D vector
  263. typedef TVec2<F16> HVec2;
  264. /// 32bit signed integer 2D vector
  265. typedef TVec2<I32> IVec2;
  266. /// 32bit unsigned integer 2D vector
  267. typedef TVec2<U32> UVec2;
  268. /// @}
  269. } // end namespace anki
  270. #endif