Rect.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 "../Math/Vector4.h"
  24. namespace Atomic
  25. {
  26. /// Two-dimensional bounding rectangle.
  27. class ATOMIC_API Rect
  28. {
  29. public:
  30. /// Construct an undefined rect.
  31. Rect() :
  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) :
  38. min_(min),
  39. max_(max)
  40. {
  41. }
  42. /// Construct from coordinates.
  43. Rect(float left, float top, float right, float bottom) :
  44. min_(left, top),
  45. max_(right, bottom)
  46. {
  47. }
  48. /// Construct from a Vector4.
  49. Rect(const Vector4& vector) :
  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) :
  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) :
  62. min_(rect.min_),
  63. max_(rect.max_)
  64. {
  65. }
  66. /// Assign from another rect.
  67. Rect& operator =(const Rect& rhs)
  68. {
  69. min_ = rhs.min_;
  70. max_ = rhs.max_;
  71. return *this;
  72. }
  73. /// Test for equality with another rect.
  74. bool operator ==(const Rect& rhs) const { return min_ == rhs.min_ && max_ == rhs.max_; }
  75. /// Test for inequality with another rect.
  76. bool operator !=(const Rect& rhs) const { return min_ != rhs.min_ || max_ != rhs.max_; }
  77. /// Define from another rect.
  78. void Define(const Rect& rect)
  79. {
  80. min_ = rect.min_;
  81. max_ = rect.max_;
  82. }
  83. /// Define from minimum and maximum vectors.
  84. void Define(const Vector2& min, const Vector2& max)
  85. {
  86. min_ = min;
  87. max_ = max;
  88. }
  89. /// Define from a point.
  90. void Define(const Vector2& point)
  91. {
  92. min_ = max_ = point;
  93. }
  94. /// Merge a point.
  95. void Merge(const Vector2& point)
  96. {
  97. if (point.x_ < min_.x_)
  98. min_.x_ = point.x_;
  99. if (point.x_ > max_.x_)
  100. max_.x_ = point.x_;
  101. if (point.y_ < min_.y_)
  102. min_.y_ = point.y_;
  103. if (point.y_ > max_.y_)
  104. max_.y_ = point.y_;
  105. }
  106. /// Merge a rect.
  107. void Merge(const Rect& rect)
  108. {
  109. if (rect.min_.x_ < min_.x_)
  110. min_.x_ = rect.min_.x_;
  111. if (rect.min_.y_ < min_.y_)
  112. min_.y_ = rect.min_.y_;
  113. if (rect.max_.x_ > max_.x_)
  114. max_.x_ = rect.max_.x_;
  115. if (rect.max_.y_ > max_.y_)
  116. max_.y_ = rect.max_.y_;
  117. }
  118. /// Clear to undefined state.
  119. void Clear()
  120. {
  121. min_ = Vector2(M_INFINITY, M_INFINITY);
  122. max_ = Vector2(-M_INFINITY, -M_INFINITY);
  123. }
  124. /// Clip with another rect.
  125. void Clip(const Rect& rect);
  126. /// Return true if this rect is defined via a previous call to Define() or Merge().
  127. bool Defined() const
  128. {
  129. return min_.x_ != M_INFINITY;
  130. }
  131. /// Return center.
  132. Vector2 Center() const { return (max_ + min_) * 0.5f; }
  133. /// Return size.
  134. Vector2 Size() const { return max_ - min_; }
  135. /// Return half-size.
  136. Vector2 HalfSize() const { return (max_ - min_) * 0.5f; }
  137. /// Test for equality with another rect with epsilon.
  138. bool Equals(const Rect& rhs) const { return min_.Equals(rhs.min_) && max_.Equals(rhs.max_); }
  139. /// Test whether a point is inside.
  140. Intersection IsInside(const Vector2& point) const
  141. {
  142. if (point.x_ < min_.x_ || point.y_ < min_.y_ || point.x_ > max_.x_ || point.y_ > max_.y_)
  143. return OUTSIDE;
  144. else
  145. return INSIDE;
  146. }
  147. /// Test if another rect is inside, outside or intersects.
  148. Intersection IsInside(const Rect& rect) const
  149. {
  150. if (rect.max_.x_ < min_.x_ || rect.min_.x_ > max_.x_ || rect.max_.y_ < min_.y_ || rect.min_.y_ > max_.y_)
  151. return OUTSIDE;
  152. else if (rect.min_.x_ < min_.x_ || rect.max_.x_ > max_.x_ || rect.min_.y_ < min_.y_ || rect.max_.y_ > max_.y_)
  153. return INTERSECTS;
  154. else
  155. return INSIDE;
  156. }
  157. /// Return float data.
  158. const float* Data() const { return &min_.x_; }
  159. /// Return as a vector.
  160. Vector4 ToVector4() const { return Vector4(min_.x_, min_.y_, max_.x_, max_.y_); }
  161. /// Return as string.
  162. String ToString() const;
  163. /// Minimum vector.
  164. Vector2 min_;
  165. /// Maximum vector.
  166. Vector2 max_;
  167. /// Rect in the range (-1, -1) - (1, 1)
  168. static const Rect FULL;
  169. /// Rect in the range (0, 0) - (1, 1)
  170. static const Rect POSITIVE;
  171. /// Zero-sized rect.
  172. static const Rect ZERO;
  173. };
  174. /// Two-dimensional bounding rectangle with integer values.
  175. class ATOMIC_API IntRect
  176. {
  177. public:
  178. /// Construct a zero rect.
  179. IntRect() :
  180. left_(0),
  181. top_(0),
  182. right_(0),
  183. bottom_(0)
  184. {
  185. }
  186. /// Construct from coordinates.
  187. IntRect(int left, int top, int right, int bottom) :
  188. left_(left),
  189. top_(top),
  190. right_(right),
  191. bottom_(bottom)
  192. {
  193. }
  194. /// Construct from an int array.
  195. IntRect(const int* data) :
  196. left_(data[0]),
  197. top_(data[1]),
  198. right_(data[2]),
  199. bottom_(data[3])
  200. {
  201. }
  202. /// Test for equality with another rect.
  203. bool operator ==(const IntRect& rhs) const
  204. {
  205. return left_ == rhs.left_ && top_ == rhs.top_ && right_ == rhs.right_ && bottom_ == rhs.bottom_;
  206. }
  207. /// Test for inequality with another rect.
  208. bool operator !=(const IntRect& rhs) const
  209. {
  210. return left_ != rhs.left_ || top_ != rhs.top_ || right_ != rhs.right_ || bottom_ != rhs.bottom_;
  211. }
  212. /// Return size.
  213. IntVector2 Size() const { return IntVector2(Width(), Height()); }
  214. /// Return width.
  215. int Width() const { return right_ - left_; }
  216. /// Return height.
  217. int Height() const { return bottom_ - top_; }
  218. /// Test whether a point is inside.
  219. Intersection IsInside(const IntVector2& point) const
  220. {
  221. if (point.x_ < left_ || point.y_ < top_ || point.x_ >= right_ || point.y_ >= bottom_)
  222. return OUTSIDE;
  223. else
  224. return INSIDE;
  225. }
  226. /// Return integer data.
  227. const int* Data() const { return &left_; }
  228. /// Return as string.
  229. String ToString() const;
  230. /// Left coordinate.
  231. int left_;
  232. /// Top coordinate.
  233. int top_;
  234. /// Right coordinate.
  235. int right_;
  236. /// Bottom coordinate.
  237. int bottom_;
  238. /// Zero-sized rect.
  239. static const IntRect ZERO;
  240. };
  241. }