Vector2.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. //
  2. // Copyright (c) 2008-2020 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 Urho3D
  26. {
  27. /// Two-dimensional vector with integer values.
  28. class URHO3D_API IntVector2
  29. {
  30. public:
  31. /// Construct a zero vector.
  32. IntVector2() noexcept :
  33. x_(0),
  34. y_(0)
  35. {
  36. }
  37. /// Construct from coordinates.
  38. IntVector2(int x, int y) noexcept :
  39. x_(x),
  40. y_(y)
  41. {
  42. }
  43. /// Construct from an int array.
  44. explicit IntVector2(const int* data) noexcept :
  45. x_(data[0]),
  46. y_(data[1])
  47. {
  48. }
  49. /// Construct from an float array.
  50. explicit IntVector2(const float* data) :
  51. x_((int)data[0]),
  52. y_((int)data[1])
  53. {
  54. }
  55. /// Copy-construct from another vector.
  56. IntVector2(const IntVector2& rhs) noexcept = default;
  57. /// Assign from another vector.
  58. IntVector2& operator =(const IntVector2& rhs) noexcept = default;
  59. /// Test for equality with another vector.
  60. bool operator ==(const IntVector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
  61. /// Test for inequality with another vector.
  62. bool operator !=(const IntVector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
  63. /// Add a vector.
  64. IntVector2 operator +(const IntVector2& rhs) const { return IntVector2(x_ + rhs.x_, y_ + rhs.y_); }
  65. /// Return negation.
  66. IntVector2 operator -() const { return IntVector2(-x_, -y_); }
  67. /// Subtract a vector.
  68. IntVector2 operator -(const IntVector2& rhs) const { return IntVector2(x_ - rhs.x_, y_ - rhs.y_); }
  69. /// Multiply with a scalar.
  70. IntVector2 operator *(int rhs) const { return IntVector2(x_ * rhs, y_ * rhs); }
  71. /// Multiply with a vector.
  72. IntVector2 operator *(const IntVector2& rhs) const { return IntVector2(x_ * rhs.x_, y_ * rhs.y_); }
  73. /// Divide by a scalar.
  74. IntVector2 operator /(int rhs) const { return IntVector2(x_ / rhs, y_ / rhs); }
  75. /// Divide by a vector.
  76. IntVector2 operator /(const IntVector2& rhs) const { return IntVector2(x_ / rhs.x_, y_ / rhs.y_); }
  77. /// Add-assign a vector.
  78. IntVector2& operator +=(const IntVector2& rhs)
  79. {
  80. x_ += rhs.x_;
  81. y_ += rhs.y_;
  82. return *this;
  83. }
  84. /// Subtract-assign a vector.
  85. IntVector2& operator -=(const IntVector2& rhs)
  86. {
  87. x_ -= rhs.x_;
  88. y_ -= rhs.y_;
  89. return *this;
  90. }
  91. /// Multiply-assign a scalar.
  92. IntVector2& operator *=(int rhs)
  93. {
  94. x_ *= rhs;
  95. y_ *= rhs;
  96. return *this;
  97. }
  98. /// Multiply-assign a vector.
  99. IntVector2& operator *=(const IntVector2& rhs)
  100. {
  101. x_ *= rhs.x_;
  102. y_ *= rhs.y_;
  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. /// Divide-assign a vector.
  113. IntVector2& operator /=(const IntVector2& rhs)
  114. {
  115. x_ /= rhs.x_;
  116. y_ /= rhs.y_;
  117. return *this;
  118. }
  119. /// Return integer data.
  120. const int* Data() const { return &x_; }
  121. /// Return as string.
  122. String ToString() const;
  123. /// Return hash value for HashSet & HashMap.
  124. unsigned ToHash() const { return (unsigned)x_ * 31 + (unsigned)y_; }
  125. /// Return length.
  126. float Length() const { return sqrtf((float)(x_ * x_ + y_ * y_)); }
  127. /// X coordinate.
  128. int x_;
  129. /// Y coordinate.
  130. int y_;
  131. /// Zero vector.
  132. static const IntVector2 ZERO;
  133. /// (-1,0) vector.
  134. static const IntVector2 LEFT;
  135. /// (1,0) vector.
  136. static const IntVector2 RIGHT;
  137. /// (0,1) vector.
  138. static const IntVector2 UP;
  139. /// (0,-1) vector.
  140. static const IntVector2 DOWN;
  141. /// (1,1) vector.
  142. static const IntVector2 ONE;
  143. };
  144. /// Two-dimensional vector.
  145. class URHO3D_API Vector2
  146. {
  147. public:
  148. /// Construct a zero vector.
  149. Vector2() noexcept :
  150. x_(0.0f),
  151. y_(0.0f)
  152. {
  153. }
  154. /// Copy-construct from another vector.
  155. Vector2(const Vector2& vector) noexcept = default;
  156. /// Construct from an IntVector2.
  157. explicit Vector2(const IntVector2& vector) noexcept :
  158. x_((float)vector.x_),
  159. y_((float)vector.y_)
  160. {
  161. }
  162. /// Construct from coordinates.
  163. Vector2(float x, float y) noexcept :
  164. x_(x),
  165. y_(y)
  166. {
  167. }
  168. /// Construct from a float array.
  169. explicit Vector2(const float* data) noexcept :
  170. x_(data[0]),
  171. y_(data[1])
  172. {
  173. }
  174. /// Assign from another vector.
  175. Vector2& operator =(const Vector2& rhs) noexcept = default;
  176. /// Test for equality with another vector without epsilon.
  177. bool operator ==(const Vector2& rhs) const { return x_ == rhs.x_ && y_ == rhs.y_; }
  178. /// Test for inequality with another vector without epsilon.
  179. bool operator !=(const Vector2& rhs) const { return x_ != rhs.x_ || y_ != rhs.y_; }
  180. /// Add a vector.
  181. Vector2 operator +(const Vector2& rhs) const { return Vector2(x_ + rhs.x_, y_ + rhs.y_); }
  182. /// Return negation.
  183. Vector2 operator -() const { return Vector2(-x_, -y_); }
  184. /// Subtract a vector.
  185. Vector2 operator -(const Vector2& rhs) const { return Vector2(x_ - rhs.x_, y_ - rhs.y_); }
  186. /// Multiply with a scalar.
  187. Vector2 operator *(float rhs) const { return Vector2(x_ * rhs, y_ * rhs); }
  188. /// Multiply with a vector.
  189. Vector2 operator *(const Vector2& rhs) const { return Vector2(x_ * rhs.x_, y_ * rhs.y_); }
  190. /// Divide by a scalar.
  191. Vector2 operator /(float rhs) const { return Vector2(x_ / rhs, y_ / rhs); }
  192. /// Divide by a vector.
  193. Vector2 operator /(const Vector2& rhs) const { return Vector2(x_ / rhs.x_, y_ / rhs.y_); }
  194. /// Add-assign a vector.
  195. Vector2& operator +=(const Vector2& rhs)
  196. {
  197. x_ += rhs.x_;
  198. y_ += rhs.y_;
  199. return *this;
  200. }
  201. /// Subtract-assign a vector.
  202. Vector2& operator -=(const Vector2& rhs)
  203. {
  204. x_ -= rhs.x_;
  205. y_ -= rhs.y_;
  206. return *this;
  207. }
  208. /// Multiply-assign a scalar.
  209. Vector2& operator *=(float rhs)
  210. {
  211. x_ *= rhs;
  212. y_ *= rhs;
  213. return *this;
  214. }
  215. /// Multiply-assign a vector.
  216. Vector2& operator *=(const Vector2& rhs)
  217. {
  218. x_ *= rhs.x_;
  219. y_ *= rhs.y_;
  220. return *this;
  221. }
  222. /// Divide-assign a scalar.
  223. Vector2& operator /=(float rhs)
  224. {
  225. float invRhs = 1.0f / rhs;
  226. x_ *= invRhs;
  227. y_ *= invRhs;
  228. return *this;
  229. }
  230. /// Divide-assign a vector.
  231. Vector2& operator /=(const Vector2& rhs)
  232. {
  233. x_ /= rhs.x_;
  234. y_ /= rhs.y_;
  235. return *this;
  236. }
  237. /// Normalize to unit length.
  238. void Normalize()
  239. {
  240. float lenSquared = LengthSquared();
  241. if (!Urho3D::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  242. {
  243. float invLen = 1.0f / sqrtf(lenSquared);
  244. x_ *= invLen;
  245. y_ *= invLen;
  246. }
  247. }
  248. /// Return length.
  249. /// @property
  250. float Length() const { return sqrtf(x_ * x_ + y_ * y_); }
  251. /// Return squared length.
  252. /// @property
  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 Urho3D::Abs(x_ * rhs.x_) + Urho3D::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 Urho3D::Acos(DotProduct(rhs) / (Length() * rhs.Length())); }
  262. /// Return absolute vector.
  263. Vector2 Abs() const { return Vector2(Urho3D::Abs(x_), Urho3D::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 Urho3D::Equals(x_, rhs.x_) && Urho3D::Equals(y_, rhs.y_); }
  268. /// Return whether any component is NaN.
  269. bool IsNaN() const { return Urho3D::IsNaN(x_) || Urho3D::IsNaN(y_); }
  270. /// Return whether any component is Inf.
  271. bool IsInf() const { return Urho3D::IsInf(x_) || Urho3D::IsInf(y_); }
  272. /// Return normalized to unit length.
  273. Vector2 Normalized() const
  274. {
  275. const float lenSquared = LengthSquared();
  276. if (!Urho3D::Equals(lenSquared, 1.0f) && lenSquared > 0.0f)
  277. {
  278. const float invLen = 1.0f / sqrtf(lenSquared);
  279. return *this * invLen;
  280. }
  281. else
  282. return *this;
  283. }
  284. /// Return normalized to unit length or zero if length is too small.
  285. Vector2 NormalizedOrDefault(const Vector2& defaultValue = Vector2::ZERO, float eps = M_LARGE_EPSILON) const
  286. {
  287. const float lenSquared = LengthSquared();
  288. if (lenSquared < eps * eps)
  289. return defaultValue;
  290. return *this / sqrtf(lenSquared);
  291. }
  292. /// Return normalized vector with length in given range.
  293. Vector2 ReNormalized(float minLength, float maxLength, const Vector2& defaultValue = Vector2::ZERO, float eps = M_LARGE_EPSILON) const
  294. {
  295. const float lenSquared = LengthSquared();
  296. if (lenSquared < eps * eps)
  297. return defaultValue;
  298. const float len = sqrtf(lenSquared);
  299. const float newLen = Clamp(len, minLength, maxLength);
  300. return *this * (newLen / len);
  301. }
  302. /// Return float data.
  303. const float* Data() const { return &x_; }
  304. /// Return as string.
  305. String ToString() const;
  306. /// X coordinate.
  307. float x_;
  308. /// Y coordinate.
  309. float y_;
  310. /// Zero vector.
  311. static const Vector2 ZERO;
  312. /// (-1,0) vector.
  313. static const Vector2 LEFT;
  314. /// (1,0) vector.
  315. static const Vector2 RIGHT;
  316. /// (0,1) vector.
  317. static const Vector2 UP;
  318. /// (0,-1) vector.
  319. static const Vector2 DOWN;
  320. /// (1,1) vector.
  321. static const Vector2 ONE;
  322. };
  323. /// Multiply Vector2 with a scalar.
  324. inline Vector2 operator *(float lhs, const Vector2& rhs) { return rhs * lhs; }
  325. /// Multiply IntVector2 with a scalar.
  326. inline IntVector2 operator *(int lhs, const IntVector2& rhs) { return rhs * lhs; }
  327. /// Per-component linear interpolation between two 2-vectors.
  328. inline Vector2 VectorLerp(const Vector2& lhs, const Vector2& rhs, const Vector2& t) { return lhs + (rhs - lhs) * t; }
  329. /// Per-component min of two 2-vectors.
  330. inline Vector2 VectorMin(const Vector2& lhs, const Vector2& rhs) { return Vector2(Min(lhs.x_, rhs.x_), Min(lhs.y_, rhs.y_)); }
  331. /// Per-component max of two 2-vectors.
  332. inline Vector2 VectorMax(const Vector2& lhs, const Vector2& rhs) { return Vector2(Max(lhs.x_, rhs.x_), Max(lhs.y_, rhs.y_)); }
  333. /// Per-component floor of 2-vector.
  334. inline Vector2 VectorFloor(const Vector2& vec) { return Vector2(Floor(vec.x_), Floor(vec.y_)); }
  335. /// Per-component round of 2-vector.
  336. inline Vector2 VectorRound(const Vector2& vec) { return Vector2(Round(vec.x_), Round(vec.y_)); }
  337. /// Per-component ceil of 2-vector.
  338. inline Vector2 VectorCeil(const Vector2& vec) { return Vector2(Ceil(vec.x_), Ceil(vec.y_)); }
  339. /// Per-component absolute value of 2-vector.
  340. inline Vector2 VectorAbs(const Vector2& vec) { return Vector2(Abs(vec.x_), Abs(vec.y_)); }
  341. /// Per-component floor of 2-vector. Returns IntVector2.
  342. inline IntVector2 VectorFloorToInt(const Vector2& vec) { return IntVector2(FloorToInt(vec.x_), FloorToInt(vec.y_)); }
  343. /// Per-component round of 2-vector. Returns IntVector2.
  344. inline IntVector2 VectorRoundToInt(const Vector2& vec) { return IntVector2(RoundToInt(vec.x_), RoundToInt(vec.y_)); }
  345. /// Per-component ceil of 2-vector. Returns IntVector2.
  346. inline IntVector2 VectorCeilToInt(const Vector2& vec) { return IntVector2(CeilToInt(vec.x_), CeilToInt(vec.y_)); }
  347. /// Per-component min of two 2-vectors.
  348. inline IntVector2 VectorMin(const IntVector2& lhs, const IntVector2& rhs) { return IntVector2(Min(lhs.x_, rhs.x_), Min(lhs.y_, rhs.y_)); }
  349. /// Per-component max of two 2-vectors.
  350. inline IntVector2 VectorMax(const IntVector2& lhs, const IntVector2& rhs) { return IntVector2(Max(lhs.x_, rhs.x_), Max(lhs.y_, rhs.y_)); }
  351. /// Per-component absolute value of integer 2-vector.
  352. inline IntVector2 VectorAbs(const IntVector2& vec) { return IntVector2(Abs(vec.x_), Abs(vec.y_)); }
  353. /// Return a random value from [0, 1) from 2-vector seed.
  354. /// http://stackoverflow.com/questions/12964279/whats-the-origin-of-this-glsl-rand-one-liner
  355. inline float StableRandom(const Vector2& seed) { return Fract(Sin(seed.DotProduct(Vector2(12.9898f, 78.233f)) * M_RADTODEG) * 43758.5453f); }
  356. /// Return a random value from [0, 1) from scalar seed.
  357. inline float StableRandom(float seed) { return StableRandom(Vector2(seed, seed)); }
  358. }