Vector2.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Container/Str.h"
  24. #include "../Math/MathDefs.h"
  25. namespace Atomic
  26. {
  27. /// Two-dimensional vector with integer values.
  28. class ATOMIC_API IntVector2
  29. {
  30. public:
  31. /// Construct a zero vector.
  32. IntVector2() :
  33. x_(0),
  34. y_(0)
  35. {
  36. }
  37. /// Construct from coordinates.
  38. IntVector2(int x, int y) :
  39. x_(x),
  40. y_(y)
  41. {
  42. }
  43. /// Construct from an int array.
  44. IntVector2(const int* data) :
  45. x_(data[0]),
  46. y_(data[1])
  47. {
  48. }
  49. /// Copy-construct from another vector.
  50. IntVector2(const IntVector2& rhs) :
  51. x_(rhs.x_),
  52. y_(rhs.y_)
  53. {
  54. }
  55. /// Assign from another vector.
  56. IntVector2& operator =(const IntVector2& rhs)
  57. {
  58. x_ = rhs.x_;
  59. y_ = rhs.y_;
  60. return *this;
  61. }
  62. /// Test for equality with another vector.
  63. bool operator ==(const IntVector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
  64. /// Test for inequality with another vector.
  65. bool operator !=(const IntVector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
  66. /// Add a vector.
  67. IntVector2 operator +(const IntVector2& rhs) const { return IntVector2(x_ + rhs.x_, y_ + rhs.y_); }
  68. /// Return negation.
  69. IntVector2 operator -() const { return IntVector2(-x_, -y_); }
  70. /// Subtract a vector.
  71. IntVector2 operator -(const IntVector2& rhs) const { return IntVector2(x_ - rhs.x_, y_ - rhs.y_); }
  72. /// Multiply with a scalar.
  73. IntVector2 operator *(int rhs) const { return IntVector2(x_ * rhs, y_ * rhs); }
  74. /// Divide by a scalar.
  75. IntVector2 operator /(int rhs) const { return IntVector2(x_ / rhs, y_ / rhs); }
  76. /// Add-assign a vector.
  77. IntVector2& operator +=(const IntVector2& rhs)
  78. {
  79. x_ += rhs.x_;
  80. y_ += rhs.y_;
  81. return *this;
  82. }
  83. /// Subtract-assign a vector.
  84. IntVector2& operator -=(const IntVector2& rhs)
  85. {
  86. x_ -= rhs.x_;
  87. y_ -= rhs.y_;
  88. return *this;
  89. }
  90. /// Multiply-assign a scalar.
  91. IntVector2& operator *=(int rhs)
  92. {
  93. x_ *= rhs;
  94. y_ *= rhs;
  95. return *this;
  96. }
  97. /// Divide-assign a scalar.
  98. IntVector2& operator /=(int rhs)
  99. {
  100. x_ /= rhs;
  101. y_ /= rhs;
  102. return *this;
  103. }
  104. /// Return integer data.
  105. const int* Data() const { return &x_; }
  106. /// Return as string.
  107. String ToString() const;
  108. /// Return hash value for HashSet & HashMap.
  109. unsigned ToHash() const { return (unsigned)x_ * 31 + (unsigned)y_; }
  110. /// Return length.
  111. float Length() const { return sqrtf((float)(x_ * x_ + y_ * y_)); }
  112. /// X coordinate.
  113. int x_;
  114. /// Y coordinate.
  115. int y_;
  116. /// Zero vector.
  117. static const IntVector2 ZERO;
  118. /// (-1,0) vector.
  119. static const IntVector2 LEFT;
  120. /// (1,0) vector.
  121. static const IntVector2 RIGHT;
  122. /// (0,1) vector.
  123. static const IntVector2 UP;
  124. /// (0,-1) vector.
  125. static const IntVector2 DOWN;
  126. /// (1,1) vector.
  127. static const IntVector2 ONE;
  128. };
  129. /// Two-dimensional vector.
  130. class ATOMIC_API Vector2
  131. {
  132. public:
  133. /// Construct a zero vector.
  134. Vector2() :
  135. x_(0.0f),
  136. y_(0.0f)
  137. {
  138. }
  139. /// Copy-construct from another vector.
  140. Vector2(const Vector2& vector) :
  141. x_(vector.x_),
  142. y_(vector.y_)
  143. {
  144. }
  145. /// Construct from an IntVector2.
  146. explicit Vector2(const IntVector2& vector) :
  147. x_((float)vector.x_),
  148. y_((float)vector.y_)
  149. {
  150. }
  151. /// Construct from coordinates.
  152. Vector2(float x, float y) :
  153. x_(x),
  154. y_(y)
  155. {
  156. }
  157. /// Construct from a float array.
  158. explicit Vector2(const float* data) :
  159. x_(data[0]),
  160. y_(data[1])
  161. {
  162. }
  163. /// Assign from another vector.
  164. Vector2& operator =(const Vector2& rhs)
  165. {
  166. x_ = rhs.x_;
  167. y_ = rhs.y_;
  168. return *this;
  169. }
  170. /// Test for equality with another vector without epsilon.
  171. bool operator ==(const Vector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
  172. /// Test for inequality with another vector without epsilon.
  173. bool operator !=(const Vector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
  174. /// Add a vector.
  175. Vector2 operator +(const Vector2& rhs) const { return Vector2(x_ + rhs.x_, y_ + rhs.y_); }
  176. /// Return negation.
  177. Vector2 operator -() const { return Vector2(-x_, -y_); }
  178. /// Subtract a vector.
  179. Vector2 operator -(const Vector2& rhs) const { return Vector2(x_ - rhs.x_, y_ - rhs.y_); }
  180. /// Multiply with a scalar.
  181. Vector2 operator *(float rhs) const { return Vector2(x_ * rhs, y_ * rhs); }
  182. /// Multiply with a vector.
  183. Vector2 operator *(const Vector2& rhs) const { return Vector2(x_ * rhs.x_, y_ * rhs.y_); }
  184. /// Divide by a scalar.
  185. Vector2 operator /(float rhs) const { return Vector2(x_ / rhs, y_ / rhs); }
  186. /// Divide by a vector.
  187. Vector2 operator /(const Vector2& rhs) const { return Vector2(x_ / rhs.x_, y_ / rhs.y_); }
  188. /// Add-assign a vector.
  189. Vector2& operator +=(const Vector2& rhs)
  190. {
  191. x_ += rhs.x_;
  192. y_ += rhs.y_;
  193. return *this;
  194. }
  195. /// Subtract-assign a vector.
  196. Vector2& operator -=(const Vector2& rhs)
  197. {
  198. x_ -= rhs.x_;
  199. y_ -= rhs.y_;
  200. return *this;
  201. }
  202. /// Multiply-assign a scalar.
  203. Vector2& operator *=(float rhs)
  204. {
  205. x_ *= rhs;
  206. y_ *= rhs;
  207. return *this;
  208. }
  209. /// Multiply-assign a vector.
  210. Vector2& operator *=(const Vector2& rhs)
  211. {
  212. x_ *= rhs.x_;
  213. y_ *= rhs.y_;
  214. return *this;
  215. }
  216. /// Divide-assign a scalar.
  217. Vector2& operator /=(float rhs)
  218. {
  219. float invRhs = 1.0f / rhs;
  220. x_ *= invRhs;
  221. y_ *= invRhs;
  222. return *this;
  223. }
  224. /// Divide-assign a vector.
  225. Vector2& operator /=(const Vector2& rhs)
  226. {
  227. x_ /= rhs.x_;
  228. y_ /= rhs.y_;
  229. return *this;
  230. }
  231. /// Normalize to unit length.
  232. void Normalize()
  233. {
  234. float lenSquared = LengthSquared();
  235. if (!Atomic::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  236. {
  237. float invLen = 1.0f / sqrtf(lenSquared);
  238. x_ *= invLen;
  239. y_ *= invLen;
  240. }
  241. }
  242. /// Return length.
  243. float Length() const { return sqrtf(x_ * x_ + y_ * y_); }
  244. /// Return squared length.
  245. float LengthSquared() const { return x_ * x_ + y_ * y_; }
  246. /// Calculate dot product.
  247. float DotProduct(const Vector2& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_; }
  248. /// Calculate absolute dot product.
  249. float AbsDotProduct(const Vector2& rhs) const { return Atomic::Abs(x_ * rhs.x_) + Atomic::Abs(y_ * rhs.y_); }
  250. /// Project vector onto axis.
  251. float ProjectOntoAxis(const Vector2& axis) const { return DotProduct(axis.Normalized()); }
  252. /// Returns the angle between this vector and another vector in degrees.
  253. float Angle(const Vector2& rhs) const { return Atomic::Acos(DotProduct(rhs) / (Length() * rhs.Length())); }
  254. /// Return absolute vector.
  255. Vector2 Abs() const { return Vector2(Atomic::Abs(x_), Atomic::Abs(y_)); }
  256. /// Linear interpolation with another vector.
  257. Vector2 Lerp(const Vector2& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
  258. /// Test for equality with another vector with epsilon.
  259. bool Equals(const Vector2& rhs) const { return Atomic::Equals(x_, rhs.x_) && Atomic::Equals(y_, rhs.y_); }
  260. /// Return whether is NaN.
  261. bool IsNaN() const { return Atomic::IsNaN(x_) || Atomic::IsNaN(y_); }
  262. /// Return normalized to unit length.
  263. Vector2 Normalized() const
  264. {
  265. float lenSquared = LengthSquared();
  266. if (!Atomic::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  267. {
  268. float invLen = 1.0f / sqrtf(lenSquared);
  269. return *this * invLen;
  270. }
  271. else
  272. return *this;
  273. }
  274. /// Return float data.
  275. const float* Data() const { return &x_; }
  276. /// Return as string.
  277. String ToString() const;
  278. /// X coordinate.
  279. float x_;
  280. /// Y coordinate.
  281. float y_;
  282. /// Zero vector.
  283. static const Vector2 ZERO;
  284. /// (-1,0) vector.
  285. static const Vector2 LEFT;
  286. /// (1,0) vector.
  287. static const Vector2 RIGHT;
  288. /// (0,1) vector.
  289. static const Vector2 UP;
  290. /// (0,-1) vector.
  291. static const Vector2 DOWN;
  292. /// (1,1) vector.
  293. static const Vector2 ONE;
  294. };
  295. /// Multiply Vector2 with a scalar
  296. inline Vector2 operator *(float lhs, const Vector2& rhs) { return rhs * lhs; }
  297. /// Multiply IntVector2 with a scalar.
  298. inline IntVector2 operator *(int lhs, const IntVector2& rhs) { return rhs * lhs; }
  299. /// Per-component linear interpolation between two 2-vectors.
  300. inline Vector2 VectorLerp(const Vector2& lhs, const Vector2& rhs, const Vector2& t) { return lhs + (rhs - lhs) * t; }
  301. /// Per-component min of two 2-vectors.
  302. inline Vector2 VectorMin(const Vector2& lhs, const Vector2& rhs) { return Vector2(Min(lhs.x_, rhs.x_), Min(lhs.y_, rhs.y_)); }
  303. /// Per-component max of two 2-vectors.
  304. inline Vector2 VectorMax(const Vector2& lhs, const Vector2& rhs) { return Vector2(Max(lhs.x_, rhs.x_), Max(lhs.y_, rhs.y_)); }
  305. /// Per-component floor of 2-vector.
  306. inline Vector2 VectorFloor(const Vector2& vec) { return Vector2(Floor(vec.x_), Floor(vec.y_)); }
  307. /// Per-component round of 2-vector.
  308. inline Vector2 VectorRound(const Vector2& vec) { return Vector2(Round(vec.x_), Round(vec.y_)); }
  309. /// Per-component ceil of 2-vector.
  310. inline Vector2 VectorCeil(const Vector2& vec) { return Vector2(Ceil(vec.x_), Ceil(vec.y_)); }
  311. /// Per-component floor of 2-vector. Returns IntVector2.
  312. inline IntVector2 VectorFloorToInt(const Vector2& vec) { return IntVector2(FloorToInt(vec.x_), FloorToInt(vec.y_)); }
  313. /// Per-component round of 2-vector. Returns IntVector2.
  314. inline IntVector2 VectorRoundToInt(const Vector2& vec) { return IntVector2(RoundToInt(vec.x_), RoundToInt(vec.y_)); }
  315. /// Per-component ceil of 2-vector. Returns IntVector2.
  316. inline IntVector2 VectorCeilToInt(const Vector2& vec) { return IntVector2(CeilToInt(vec.x_), CeilToInt(vec.y_)); }
  317. /// Per-component min of two 2-vectors.
  318. inline IntVector2 VectorMin(const IntVector2& lhs, const IntVector2& rhs) { return IntVector2(Min(lhs.x_, rhs.x_), Min(lhs.y_, rhs.y_)); }
  319. /// Per-component max of two 2-vectors.
  320. inline IntVector2 VectorMax(const IntVector2& lhs, const IntVector2& rhs) { return IntVector2(Max(lhs.x_, rhs.x_), Max(lhs.y_, rhs.y_)); }
  321. /// Return a random value from [0, 1) from 2-vector seed.
  322. /// http://stackoverflow.com/questions/12964279/whats-the-origin-of-this-glsl-rand-one-liner
  323. inline float StableRandom(const Vector2& seed) { return Fract(Sin(seed.DotProduct(Vector2(12.9898f, 78.233f)) * M_RADTODEG) * 43758.5453f); }
  324. /// Return a random value from [0, 1) from scalar seed.
  325. inline float StableRandom(float seed) { return StableRandom(Vector2(seed, seed)); }
  326. }