Rect.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 "../Math/Vector4.h"
  24. namespace Urho3D
  25. {
  26. /// Two-dimensional bounding rectangle.
  27. class URHO3D_API Rect
  28. {
  29. public:
  30. /// Construct an undefined rect.
  31. Rect() noexcept :
  32. min_(M_INFINITY, M_INFINITY),
  33. max_(-M_INFINITY, -M_INFINITY)
  34. {
  35. }
  36. /// Construct from minimum and maximum vectors.
  37. Rect(const Vector2& min, const Vector2& max) noexcept :
  38. min_(min),
  39. max_(max)
  40. {
  41. }
  42. /// Construct from coordinates.
  43. Rect(float left, float top, float right, float bottom) noexcept :
  44. min_(left, top),
  45. max_(right, bottom)
  46. {
  47. }
  48. /// Construct from a Vector4.
  49. explicit Rect(const Vector4& vector) noexcept :
  50. min_(vector.x_, vector.y_),
  51. max_(vector.z_, vector.w_)
  52. {
  53. }
  54. /// Construct from a float array.
  55. explicit Rect(const float* data) noexcept :
  56. min_(data[0], data[1]),
  57. max_(data[2], data[3])
  58. {
  59. }
  60. /// Copy-construct from another rect.
  61. Rect(const Rect& rect) noexcept = default;
  62. /// Assign from another rect.
  63. Rect& operator =(const Rect& rhs) noexcept = default;
  64. /// Test for equality with another rect.
  65. bool operator ==(const Rect& rhs) const { return min_ == rhs.min_ && max_ == rhs.max_; }
  66. /// Test for inequality with another rect.
  67. bool operator !=(const Rect& rhs) const { return min_ != rhs.min_ || max_ != rhs.max_; }
  68. /// Add another rect to this one inplace.
  69. Rect& operator +=(const Rect& rhs)
  70. {
  71. min_ += rhs.min_;
  72. max_ += rhs.max_;
  73. return *this;
  74. }
  75. /// Subtract another rect from this one inplace.
  76. Rect& operator -=(const Rect& rhs)
  77. {
  78. min_ -= rhs.min_;
  79. max_ -= rhs.max_;
  80. return *this;
  81. }
  82. /// Divide by scalar inplace.
  83. Rect& operator /=(float value)
  84. {
  85. min_ /= value;
  86. max_ /= value;
  87. return *this;
  88. }
  89. /// Multiply by scalar inplace.
  90. Rect& operator *=(float value)
  91. {
  92. min_ *= value;
  93. max_ *= value;
  94. return *this;
  95. }
  96. /// Divide by scalar.
  97. Rect operator /(float value) const
  98. {
  99. return Rect(min_ / value, max_ / value);
  100. }
  101. /// Multiply by scalar.
  102. Rect operator *(float value) const
  103. {
  104. return Rect(min_ * value, max_ * value);
  105. }
  106. /// Add another rect.
  107. Rect operator +(const Rect& rhs) const
  108. {
  109. return Rect(min_ + rhs.min_, max_ + rhs.max_);
  110. }
  111. /// Subtract another rect.
  112. Rect operator -(const Rect& rhs) const
  113. {
  114. return Rect(min_ - rhs.min_, max_ - rhs.max_);
  115. }
  116. /// Define from another rect.
  117. void Define(const Rect& rect)
  118. {
  119. min_ = rect.min_;
  120. max_ = rect.max_;
  121. }
  122. /// Define from minimum and maximum vectors.
  123. void Define(const Vector2& min, const Vector2& max)
  124. {
  125. min_ = min;
  126. max_ = max;
  127. }
  128. /// Define from a point.
  129. void Define(const Vector2& point)
  130. {
  131. min_ = max_ = point;
  132. }
  133. /// Merge a point.
  134. void Merge(const Vector2& point)
  135. {
  136. if (point.x_ < min_.x_)
  137. min_.x_ = point.x_;
  138. if (point.x_ > max_.x_)
  139. max_.x_ = point.x_;
  140. if (point.y_ < min_.y_)
  141. min_.y_ = point.y_;
  142. if (point.y_ > max_.y_)
  143. max_.y_ = point.y_;
  144. }
  145. /// Merge a rect.
  146. void Merge(const Rect& rect)
  147. {
  148. if (rect.min_.x_ < min_.x_)
  149. min_.x_ = rect.min_.x_;
  150. if (rect.min_.y_ < min_.y_)
  151. min_.y_ = rect.min_.y_;
  152. if (rect.max_.x_ > max_.x_)
  153. max_.x_ = rect.max_.x_;
  154. if (rect.max_.y_ > max_.y_)
  155. max_.y_ = rect.max_.y_;
  156. }
  157. /// Clear to undefined state.
  158. void Clear()
  159. {
  160. min_ = Vector2(M_INFINITY, M_INFINITY);
  161. max_ = Vector2(-M_INFINITY, -M_INFINITY);
  162. }
  163. /// Clip with another rect.
  164. void Clip(const Rect& rect);
  165. /// Return true if this rect is defined via a previous call to Define() or Merge().
  166. bool Defined() const
  167. {
  168. return min_.x_ != M_INFINITY;
  169. }
  170. /// Return center.
  171. Vector2 Center() const { return (max_ + min_) * 0.5f; }
  172. /// Return size.
  173. Vector2 Size() const { return max_ - min_; }
  174. /// Return half-size.
  175. Vector2 HalfSize() const { return (max_ - min_) * 0.5f; }
  176. /// Test for equality with another rect with epsilon.
  177. bool Equals(const Rect& rhs) const { return min_.Equals(rhs.min_) && max_.Equals(rhs.max_); }
  178. /// Test whether a point is inside.
  179. Intersection IsInside(const Vector2& point) const
  180. {
  181. if (point.x_ < min_.x_ || point.y_ < min_.y_ || point.x_ > max_.x_ || point.y_ > max_.y_)
  182. return OUTSIDE;
  183. else
  184. return INSIDE;
  185. }
  186. /// Test if another rect is inside, outside or intersects.
  187. Intersection IsInside(const Rect& rect) const
  188. {
  189. if (rect.max_.x_ < min_.x_ || rect.min_.x_ > max_.x_ || rect.max_.y_ < min_.y_ || rect.min_.y_ > max_.y_)
  190. return OUTSIDE;
  191. else if (rect.min_.x_ < min_.x_ || rect.max_.x_ > max_.x_ || rect.min_.y_ < min_.y_ || rect.max_.y_ > max_.y_)
  192. return INTERSECTS;
  193. else
  194. return INSIDE;
  195. }
  196. /// Return float data.
  197. const float* Data() const { return &min_.x_; }
  198. /// Return as a vector.
  199. Vector4 ToVector4() const { return Vector4(min_.x_, min_.y_, max_.x_, max_.y_); }
  200. /// Return as string.
  201. String ToString() const;
  202. /// Return left-top corner position.
  203. Vector2 Min() const { return min_; }
  204. /// Return right-bottom corner position.
  205. Vector2 Max() const { return max_; }
  206. /// Return left coordinate.
  207. float Left() const { return min_.x_; }
  208. /// Return top coordinate.
  209. float Top() const { return min_.y_; }
  210. /// Return right coordinate.
  211. float Right() const { return max_.x_; }
  212. /// Return bottom coordinate.
  213. float Bottom() const { return max_.y_; }
  214. /// Minimum vector.
  215. Vector2 min_;
  216. /// Maximum vector.
  217. Vector2 max_;
  218. /// Rect in the range (-1, -1) - (1, 1).
  219. static const Rect FULL;
  220. /// Rect in the range (0, 0) - (1, 1).
  221. static const Rect POSITIVE;
  222. /// Zero-sized rect.
  223. static const Rect ZERO;
  224. };
  225. /// Two-dimensional bounding rectangle with integer values.
  226. class URHO3D_API IntRect
  227. {
  228. public:
  229. /// Construct a zero rect.
  230. IntRect() noexcept :
  231. left_(0),
  232. top_(0),
  233. right_(0),
  234. bottom_(0)
  235. {
  236. }
  237. /// Construct from minimum and maximum vectors.
  238. IntRect(const IntVector2& min, const IntVector2& max) noexcept :
  239. left_(min.x_),
  240. top_(min.y_),
  241. right_(max.x_),
  242. bottom_(max.y_)
  243. {
  244. }
  245. /// Construct from coordinates.
  246. IntRect(int left, int top, int right, int bottom) noexcept :
  247. left_(left),
  248. top_(top),
  249. right_(right),
  250. bottom_(bottom)
  251. {
  252. }
  253. /// Construct from an int array.
  254. explicit IntRect(const int* data) noexcept :
  255. left_(data[0]),
  256. top_(data[1]),
  257. right_(data[2]),
  258. bottom_(data[3])
  259. {
  260. }
  261. /// Test for equality with another rect.
  262. bool operator ==(const IntRect& rhs) const
  263. {
  264. return left_ == rhs.left_ && top_ == rhs.top_ && right_ == rhs.right_ && bottom_ == rhs.bottom_;
  265. }
  266. /// Test for inequality with another rect.
  267. bool operator !=(const IntRect& rhs) const
  268. {
  269. return left_ != rhs.left_ || top_ != rhs.top_ || right_ != rhs.right_ || bottom_ != rhs.bottom_;
  270. }
  271. /// Add another rect to this one inplace.
  272. IntRect& operator +=(const IntRect& rhs)
  273. {
  274. left_ += rhs.left_;
  275. top_ += rhs.top_;
  276. right_ += rhs.right_;
  277. bottom_ += rhs.bottom_;
  278. return *this;
  279. }
  280. /// Subtract another rect from this one inplace.
  281. IntRect& operator -=(const IntRect& rhs)
  282. {
  283. left_ -= rhs.left_;
  284. top_ -= rhs.top_;
  285. right_ -= rhs.right_;
  286. bottom_ -= rhs.bottom_;
  287. return *this;
  288. }
  289. /// Divide by scalar inplace.
  290. IntRect& operator /=(float value)
  291. {
  292. left_ = static_cast<int>(left_ / value);
  293. top_ = static_cast<int>(top_ / value);
  294. right_ = static_cast<int>(right_ / value);
  295. bottom_ = static_cast<int>(bottom_ / value);
  296. return *this;
  297. }
  298. /// Multiply by scalar inplace.
  299. IntRect& operator *=(float value)
  300. {
  301. left_ = static_cast<int>(left_ * value);
  302. top_ = static_cast<int>(top_ * value);
  303. right_ = static_cast<int>(right_ * value);
  304. bottom_ = static_cast<int>(bottom_ * value);
  305. return *this;
  306. }
  307. /// Divide by scalar.
  308. IntRect operator /(float value) const
  309. {
  310. return {
  311. static_cast<int>(left_ / value), static_cast<int>(top_ / value),
  312. static_cast<int>(right_ / value), static_cast<int>(bottom_ / value)
  313. };
  314. }
  315. /// Multiply by scalar.
  316. IntRect operator *(float value) const
  317. {
  318. return {
  319. static_cast<int>(left_ * value), static_cast<int>(top_ * value),
  320. static_cast<int>(right_ * value), static_cast<int>(bottom_ * value)
  321. };
  322. }
  323. /// Add another rect.
  324. IntRect operator +(const IntRect& rhs) const
  325. {
  326. return {
  327. left_ + rhs.left_, top_ + rhs.top_,
  328. right_ + rhs.right_, bottom_ + rhs.bottom_
  329. };
  330. }
  331. /// Subtract another rect.
  332. IntRect operator -(const IntRect& rhs) const
  333. {
  334. return {
  335. left_ - rhs.left_, top_ - rhs.top_,
  336. right_ - rhs.right_, bottom_ - rhs.bottom_
  337. };
  338. }
  339. /// Return size.
  340. IntVector2 Size() const { return IntVector2(Width(), Height()); }
  341. /// Return width.
  342. int Width() const { return right_ - left_; }
  343. /// Return height.
  344. int Height() const { return bottom_ - top_; }
  345. /// Test whether a point is inside.
  346. Intersection IsInside(const IntVector2& point) const
  347. {
  348. if (point.x_ < left_ || point.y_ < top_ || point.x_ >= right_ || point.y_ >= bottom_)
  349. return OUTSIDE;
  350. else
  351. return INSIDE;
  352. }
  353. /// Clip with another rect. Since IntRect does not have an undefined state
  354. /// like Rect, return (0, 0, 0, 0) if the result is empty.
  355. void Clip(const IntRect& rect);
  356. /// Merge a rect. If this rect was empty, become the other rect. If the
  357. /// other rect is empty, do nothing.
  358. void Merge(const IntRect& rect);
  359. /// Return integer data.
  360. const int* Data() const { return &left_; }
  361. /// Return as string.
  362. String ToString() const;
  363. /// Return left-top corner position.
  364. IntVector2 Min() const { return {left_, top_}; }
  365. /// Return right-bottom corner position.
  366. IntVector2 Max() const { return {right_, bottom_}; }
  367. /// Return left coordinate.
  368. int Left() const { return left_; }
  369. /// Return top coordinate.
  370. int Top() const { return top_; }
  371. /// Return right coordinate.
  372. int Right() const { return right_; }
  373. /// Return bottom coordinate.
  374. int Bottom() const { return bottom_; }
  375. /// Left coordinate.
  376. int left_;
  377. /// Top coordinate.
  378. int top_;
  379. /// Right coordinate.
  380. int right_;
  381. /// Bottom coordinate.
  382. int bottom_;
  383. /// Zero-sized rect.
  384. static const IntRect ZERO;
  385. };
  386. }