Vector2.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. // ATOMIC BEGIN
  50. /// Construct from an int array.
  51. IntVector2(const float* data) :
  52. x_((int)data[0]),
  53. y_((int)data[1])
  54. {
  55. }
  56. // ATOMIC END
  57. /// Copy-construct from another vector.
  58. IntVector2(const IntVector2& rhs) :
  59. x_(rhs.x_),
  60. y_(rhs.y_)
  61. {
  62. }
  63. /// Assign from another vector.
  64. IntVector2& operator =(const IntVector2& rhs)
  65. {
  66. x_ = rhs.x_;
  67. y_ = rhs.y_;
  68. return *this;
  69. }
  70. /// Test for equality with another vector.
  71. bool operator ==(const IntVector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
  72. /// Test for inequality with another vector.
  73. bool operator !=(const IntVector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
  74. /// Add a vector.
  75. IntVector2 operator +(const IntVector2& rhs) const { return IntVector2(x_ + rhs.x_, y_ + rhs.y_); }
  76. /// Return negation.
  77. IntVector2 operator -() const { return IntVector2(-x_, -y_); }
  78. /// Subtract a vector.
  79. IntVector2 operator -(const IntVector2& rhs) const { return IntVector2(x_ - rhs.x_, y_ - rhs.y_); }
  80. /// Multiply with a scalar.
  81. IntVector2 operator *(int rhs) const { return IntVector2(x_ * rhs, y_ * rhs); }
  82. /// Divide by a scalar.
  83. IntVector2 operator /(int rhs) const { return IntVector2(x_ / rhs, y_ / rhs); }
  84. /// Add-assign a vector.
  85. IntVector2& operator +=(const IntVector2& rhs)
  86. {
  87. x_ += rhs.x_;
  88. y_ += rhs.y_;
  89. return *this;
  90. }
  91. /// Subtract-assign a vector.
  92. IntVector2& operator -=(const IntVector2& rhs)
  93. {
  94. x_ -= rhs.x_;
  95. y_ -= rhs.y_;
  96. return *this;
  97. }
  98. /// Multiply-assign a scalar.
  99. IntVector2& operator *=(int rhs)
  100. {
  101. x_ *= rhs;
  102. y_ *= rhs;
  103. return *this;
  104. }
  105. /// Divide-assign a scalar.
  106. IntVector2& operator /=(int rhs)
  107. {
  108. x_ /= rhs;
  109. y_ /= rhs;
  110. return *this;
  111. }
  112. /// Return integer data.
  113. const int* Data() const { return &x_; }
  114. /// Return as string.
  115. String ToString() const;
  116. /// Return hash value for HashSet & HashMap.
  117. unsigned ToHash() const { return (unsigned)x_ * 31 + (unsigned)y_; }
  118. /// Return length.
  119. float Length() const { return sqrtf((float)(x_ * x_ + y_ * y_)); }
  120. /// X coordinate.
  121. int x_;
  122. /// Y coordinate.
  123. int y_;
  124. /// Zero vector.
  125. static const IntVector2 ZERO;
  126. /// (-1,0) vector.
  127. static const IntVector2 LEFT;
  128. /// (1,0) vector.
  129. static const IntVector2 RIGHT;
  130. /// (0,1) vector.
  131. static const IntVector2 UP;
  132. /// (0,-1) vector.
  133. static const IntVector2 DOWN;
  134. /// (1,1) vector.
  135. static const IntVector2 ONE;
  136. };
  137. /// Two-dimensional vector.
  138. class ATOMIC_API Vector2
  139. {
  140. public:
  141. /// Construct a zero vector.
  142. Vector2() :
  143. x_(0.0f),
  144. y_(0.0f)
  145. {
  146. }
  147. /// Copy-construct from another vector.
  148. Vector2(const Vector2& vector) :
  149. x_(vector.x_),
  150. y_(vector.y_)
  151. {
  152. }
  153. /// Construct from an IntVector2.
  154. explicit Vector2(const IntVector2& vector) :
  155. x_((float)vector.x_),
  156. y_((float)vector.y_)
  157. {
  158. }
  159. /// Construct from coordinates.
  160. Vector2(float x, float y) :
  161. x_(x),
  162. y_(y)
  163. {
  164. }
  165. /// Construct from a float array.
  166. explicit Vector2(const float* data) :
  167. x_(data[0]),
  168. y_(data[1])
  169. {
  170. }
  171. /// Assign from another vector.
  172. Vector2& operator =(const Vector2& rhs)
  173. {
  174. x_ = rhs.x_;
  175. y_ = rhs.y_;
  176. return *this;
  177. }
  178. /// Test for equality with another vector without epsilon.
  179. bool operator ==(const Vector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
  180. /// Test for inequality with another vector without epsilon.
  181. bool operator !=(const Vector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
  182. /// Add a vector.
  183. Vector2 operator +(const Vector2& rhs) const { return Vector2(x_ + rhs.x_, y_ + rhs.y_); }
  184. /// Return negation.
  185. Vector2 operator -() const { return Vector2(-x_, -y_); }
  186. /// Subtract a vector.
  187. Vector2 operator -(const Vector2& rhs) const { return Vector2(x_ - rhs.x_, y_ - rhs.y_); }
  188. /// Multiply with a scalar.
  189. Vector2 operator *(float rhs) const { return Vector2(x_ * rhs, y_ * rhs); }
  190. /// Multiply with a vector.
  191. Vector2 operator *(const Vector2& rhs) const { return Vector2(x_ * rhs.x_, y_ * rhs.y_); }
  192. /// Divide by a scalar.
  193. Vector2 operator /(float rhs) const { return Vector2(x_ / rhs, y_ / rhs); }
  194. /// Divide by a vector.
  195. Vector2 operator /(const Vector2& rhs) const { return Vector2(x_ / rhs.x_, y_ / rhs.y_); }
  196. /// Add-assign a vector.
  197. Vector2& operator +=(const Vector2& rhs)
  198. {
  199. x_ += rhs.x_;
  200. y_ += rhs.y_;
  201. return *this;
  202. }
  203. /// Subtract-assign a vector.
  204. Vector2& operator -=(const Vector2& rhs)
  205. {
  206. x_ -= rhs.x_;
  207. y_ -= rhs.y_;
  208. return *this;
  209. }
  210. /// Multiply-assign a scalar.
  211. Vector2& operator *=(float rhs)
  212. {
  213. x_ *= rhs;
  214. y_ *= rhs;
  215. return *this;
  216. }
  217. /// Multiply-assign a vector.
  218. Vector2& operator *=(const Vector2& rhs)
  219. {
  220. x_ *= rhs.x_;
  221. y_ *= rhs.y_;
  222. return *this;
  223. }
  224. /// Divide-assign a scalar.
  225. Vector2& operator /=(float rhs)
  226. {
  227. float invRhs = 1.0f / rhs;
  228. x_ *= invRhs;
  229. y_ *= invRhs;
  230. return *this;
  231. }
  232. /// Divide-assign a vector.
  233. Vector2& operator /=(const Vector2& rhs)
  234. {
  235. x_ /= rhs.x_;
  236. y_ /= rhs.y_;
  237. return *this;
  238. }
  239. /// Normalize to unit length.
  240. void Normalize()
  241. {
  242. float lenSquared = LengthSquared();
  243. if (!Atomic::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  244. {
  245. float invLen = 1.0f / sqrtf(lenSquared);
  246. x_ *= invLen;
  247. y_ *= invLen;
  248. }
  249. }
  250. /// Return length.
  251. float Length() const { return sqrtf(x_ * x_ + y_ * y_); }
  252. /// Return squared length.
  253. float LengthSquared() const { return x_ * x_ + y_ * y_; }
  254. /// Calculate dot product.
  255. float DotProduct(const Vector2& rhs) const { return x_ * rhs.x_ + y_ * rhs.y_; }
  256. /// Calculate absolute dot product.
  257. float AbsDotProduct(const Vector2& rhs) const { return Atomic::Abs(x_ * rhs.x_) + Atomic::Abs(y_ * rhs.y_); }
  258. /// Project vector onto axis.
  259. float ProjectOntoAxis(const Vector2& axis) const { return DotProduct(axis.Normalized()); }
  260. /// Returns the angle between this vector and another vector in degrees.
  261. float Angle(const Vector2& rhs) const { return Atomic::Acos(DotProduct(rhs) / (Length() * rhs.Length())); }
  262. /// Return absolute vector.
  263. Vector2 Abs() const { return Vector2(Atomic::Abs(x_), Atomic::Abs(y_)); }
  264. /// Linear interpolation with another vector.
  265. Vector2 Lerp(const Vector2& rhs, float t) const { return *this * (1.0f - t) + rhs * t; }
  266. /// Test for equality with another vector with epsilon.
  267. bool Equals(const Vector2& rhs) const { return Atomic::Equals(x_, rhs.x_) && Atomic::Equals(y_, rhs.y_); }
  268. /// Return whether is NaN.
  269. bool IsNaN() const { return Atomic::IsNaN(x_) || Atomic::IsNaN(y_); }
  270. /// Return normalized to unit length.
  271. Vector2 Normalized() const
  272. {
  273. float lenSquared = LengthSquared();
  274. if (!Atomic::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  275. {
  276. float invLen = 1.0f / sqrtf(lenSquared);
  277. return *this * invLen;
  278. }
  279. else
  280. return *this;
  281. }
  282. /// Return float data.
  283. const float* Data() const { return &x_; }
  284. /// Return as string.
  285. String ToString() const;
  286. /// X coordinate.
  287. float x_;
  288. /// Y coordinate.
  289. float y_;
  290. /// Zero vector.
  291. static const Vector2 ZERO;
  292. /// (-1,0) vector.
  293. static const Vector2 LEFT;
  294. /// (1,0) vector.
  295. static const Vector2 RIGHT;
  296. /// (0,1) vector.
  297. static const Vector2 UP;
  298. /// (0,-1) vector.
  299. static const Vector2 DOWN;
  300. /// (1,1) vector.
  301. static const Vector2 ONE;
  302. };
  303. /// Multiply Vector2 with a scalar
  304. inline Vector2 operator *(float lhs, const Vector2& rhs) { return rhs * lhs; }
  305. /// Multiply IntVector2 with a scalar.
  306. inline IntVector2 operator *(int lhs, const IntVector2& rhs) { return rhs * lhs; }
  307. /// Per-component linear interpolation between two 2-vectors.
  308. inline Vector2 VectorLerp(const Vector2& lhs, const Vector2& rhs, const Vector2& t) { return lhs + (rhs - lhs) * t; }
  309. /// Per-component min of two 2-vectors.
  310. inline Vector2 VectorMin(const Vector2& lhs, const Vector2& rhs) { return Vector2(Min(lhs.x_, rhs.x_), Min(lhs.y_, rhs.y_)); }
  311. /// Per-component max of two 2-vectors.
  312. inline Vector2 VectorMax(const Vector2& lhs, const Vector2& rhs) { return Vector2(Max(lhs.x_, rhs.x_), Max(lhs.y_, rhs.y_)); }
  313. /// Per-component floor of 2-vector.
  314. inline Vector2 VectorFloor(const Vector2& vec) { return Vector2(Floor(vec.x_), Floor(vec.y_)); }
  315. /// Per-component round of 2-vector.
  316. inline Vector2 VectorRound(const Vector2& vec) { return Vector2(Round(vec.x_), Round(vec.y_)); }
  317. /// Per-component ceil of 2-vector.
  318. inline Vector2 VectorCeil(const Vector2& vec) { return Vector2(Ceil(vec.x_), Ceil(vec.y_)); }
  319. /// Per-component floor of 2-vector. Returns IntVector2.
  320. inline IntVector2 VectorFloorToInt(const Vector2& vec) { return IntVector2(FloorToInt(vec.x_), FloorToInt(vec.y_)); }
  321. /// Per-component round of 2-vector. Returns IntVector2.
  322. inline IntVector2 VectorRoundToInt(const Vector2& vec) { return IntVector2(RoundToInt(vec.x_), RoundToInt(vec.y_)); }
  323. /// Per-component ceil of 2-vector. Returns IntVector2.
  324. inline IntVector2 VectorCeilToInt(const Vector2& vec) { return IntVector2(CeilToInt(vec.x_), CeilToInt(vec.y_)); }
  325. /// Per-component min of two 2-vectors.
  326. inline IntVector2 VectorMin(const IntVector2& lhs, const IntVector2& rhs) { return IntVector2(Min(lhs.x_, rhs.x_), Min(lhs.y_, rhs.y_)); }
  327. /// Per-component max of two 2-vectors.
  328. inline IntVector2 VectorMax(const IntVector2& lhs, const IntVector2& rhs) { return IntVector2(Max(lhs.x_, rhs.x_), Max(lhs.y_, rhs.y_)); }
  329. /// Return a random value from [0, 1) from 2-vector seed.
  330. /// http://stackoverflow.com/questions/12964279/whats-the-origin-of-this-glsl-rand-one-liner
  331. inline float StableRandom(const Vector2& seed) { return Fract(Sin(seed.DotProduct(Vector2(12.9898f, 78.233f)) * M_RADTODEG) * 43758.5453f); }
  332. /// Return a random value from [0, 1) from scalar seed.
  333. inline float StableRandom(float seed) { return StableRandom(Vector2(seed, seed)); }
  334. }