rect_f.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/gfx/geometry/rect_f.h"
  5. #include <algorithm>
  6. #include <limits>
  7. #include "base/check.h"
  8. #include "base/check_op.h"
  9. #include "base/numerics/safe_conversions.h"
  10. #include "base/strings/stringprintf.h"
  11. #include "build/build_config.h"
  12. #include "ui/gfx/geometry/insets_f.h"
  13. #include "ui/gfx/geometry/outsets_f.h"
  14. #if BUILDFLAG(IS_IOS)
  15. #include <CoreGraphics/CoreGraphics.h>
  16. #elif BUILDFLAG(IS_MAC)
  17. #include <ApplicationServices/ApplicationServices.h>
  18. #endif
  19. namespace gfx {
  20. static void AdjustAlongAxis(float dst_origin,
  21. float dst_size,
  22. float* origin,
  23. float* size) {
  24. *size = std::min(dst_size, *size);
  25. if (*origin < dst_origin)
  26. *origin = dst_origin;
  27. else
  28. *origin = std::min(dst_origin + dst_size, *origin + *size) - *size;
  29. }
  30. #if BUILDFLAG(IS_APPLE)
  31. RectF::RectF(const CGRect& r)
  32. : origin_(r.origin.x, r.origin.y), size_(r.size.width, r.size.height) {
  33. }
  34. CGRect RectF::ToCGRect() const {
  35. return CGRectMake(x(), y(), width(), height());
  36. }
  37. #endif
  38. void RectF::Inset(const InsetsF& insets) {
  39. origin_ += Vector2dF(insets.left(), insets.top());
  40. set_width(width() - insets.width());
  41. set_height(height() - insets.height());
  42. }
  43. void RectF::Offset(float horizontal, float vertical) {
  44. origin_ += Vector2dF(horizontal, vertical);
  45. }
  46. void RectF::operator+=(const Vector2dF& offset) {
  47. origin_ += offset;
  48. }
  49. void RectF::operator-=(const Vector2dF& offset) {
  50. origin_ -= offset;
  51. }
  52. InsetsF RectF::InsetsFrom(const RectF& inner) const {
  53. return InsetsF::TLBR(inner.y() - y(), inner.x() - x(),
  54. bottom() - inner.bottom(), right() - inner.right());
  55. }
  56. bool RectF::operator<(const RectF& other) const {
  57. if (origin_ != other.origin_)
  58. return origin_ < other.origin_;
  59. if (width() == other.width())
  60. return height() < other.height();
  61. return width() < other.width();
  62. }
  63. bool RectF::Contains(float point_x, float point_y) const {
  64. return point_x >= x() && point_x < right() && point_y >= y() &&
  65. point_y < bottom();
  66. }
  67. bool RectF::InclusiveContains(float point_x, float point_y) const {
  68. return point_x >= x() && point_x <= right() && point_y >= y() &&
  69. point_y <= bottom();
  70. }
  71. bool RectF::Contains(const RectF& rect) const {
  72. return rect.x() >= x() && rect.right() <= right() && rect.y() >= y() &&
  73. rect.bottom() <= bottom();
  74. }
  75. bool RectF::Intersects(const RectF& rect) const {
  76. return !IsEmpty() && !rect.IsEmpty() && rect.x() < right() &&
  77. rect.right() > x() && rect.y() < bottom() && rect.bottom() > y();
  78. }
  79. void RectF::Intersect(const RectF& rect) {
  80. if (IsEmpty() || rect.IsEmpty()) {
  81. SetRect(0, 0, 0, 0);
  82. return;
  83. }
  84. float rx = std::max(x(), rect.x());
  85. float ry = std::max(y(), rect.y());
  86. float rr = std::min(right(), rect.right());
  87. float rb = std::min(bottom(), rect.bottom());
  88. if (rx >= rr || ry >= rb) {
  89. SetRect(0, 0, 0, 0);
  90. return;
  91. }
  92. SetRect(rx, ry, rr - rx, rb - ry);
  93. }
  94. bool RectF::InclusiveIntersect(const RectF& rect) {
  95. float rx = std::max(x(), rect.x());
  96. float ry = std::max(y(), rect.y());
  97. float rr = std::min(right(), rect.right());
  98. float rb = std::min(bottom(), rect.bottom());
  99. // Return a clean empty rectangle for non-intersecting cases.
  100. if (rx > rr || ry > rb) {
  101. SetRect(0, 0, 0, 0);
  102. return false;
  103. }
  104. SetRect(rx, ry, rr - rx, rb - ry);
  105. return true;
  106. }
  107. void RectF::Union(const RectF& rect) {
  108. if (IsEmpty()) {
  109. *this = rect;
  110. return;
  111. }
  112. if (rect.IsEmpty())
  113. return;
  114. UnionEvenIfEmpty(rect);
  115. }
  116. void RectF::UnionEvenIfEmpty(const RectF& rect) {
  117. float rx = std::min(x(), rect.x());
  118. float ry = std::min(y(), rect.y());
  119. float rr = std::max(right(), rect.right());
  120. float rb = std::max(bottom(), rect.bottom());
  121. SetRect(rx, ry, rr - rx, rb - ry);
  122. // Due to floating errors and SizeF::clamp(), the new rect may not fully
  123. // contain the original rects at the right/bottom side. Expand the rect in
  124. // the case.
  125. constexpr auto kFloatMax = std::numeric_limits<float>::max();
  126. if (UNLIKELY(right() < rr && width() < kFloatMax)) {
  127. size_.SetToNextWidth();
  128. DCHECK_GE(right(), rr);
  129. }
  130. if (UNLIKELY(bottom() < rb && height() < kFloatMax)) {
  131. size_.SetToNextHeight();
  132. DCHECK_GE(bottom(), rb);
  133. }
  134. }
  135. void RectF::Subtract(const RectF& rect) {
  136. if (!Intersects(rect))
  137. return;
  138. if (rect.Contains(*this)) {
  139. SetRect(0, 0, 0, 0);
  140. return;
  141. }
  142. float rx = x();
  143. float ry = y();
  144. float rr = right();
  145. float rb = bottom();
  146. if (rect.y() <= y() && rect.bottom() >= bottom()) {
  147. // complete intersection in the y-direction
  148. if (rect.x() <= x()) {
  149. rx = rect.right();
  150. } else if (rect.right() >= right()) {
  151. rr = rect.x();
  152. }
  153. } else if (rect.x() <= x() && rect.right() >= right()) {
  154. // complete intersection in the x-direction
  155. if (rect.y() <= y()) {
  156. ry = rect.bottom();
  157. } else if (rect.bottom() >= bottom()) {
  158. rb = rect.y();
  159. }
  160. }
  161. SetRect(rx, ry, rr - rx, rb - ry);
  162. }
  163. void RectF::AdjustToFit(const RectF& rect) {
  164. float new_x = x();
  165. float new_y = y();
  166. float new_width = width();
  167. float new_height = height();
  168. AdjustAlongAxis(rect.x(), rect.width(), &new_x, &new_width);
  169. AdjustAlongAxis(rect.y(), rect.height(), &new_y, &new_height);
  170. SetRect(new_x, new_y, new_width, new_height);
  171. }
  172. PointF RectF::CenterPoint() const {
  173. return PointF(x() + width() / 2, y() + height() / 2);
  174. }
  175. void RectF::ClampToCenteredSize(const SizeF& size) {
  176. float new_width = std::min(width(), size.width());
  177. float new_height = std::min(height(), size.height());
  178. float new_x = x() + (width() - new_width) / 2;
  179. float new_y = y() + (height() - new_height) / 2;
  180. SetRect(new_x, new_y, new_width, new_height);
  181. }
  182. void RectF::Transpose() {
  183. SetRect(y(), x(), height(), width());
  184. }
  185. void RectF::SplitVertically(RectF* left_half, RectF* right_half) const {
  186. DCHECK(left_half);
  187. DCHECK(right_half);
  188. left_half->SetRect(x(), y(), width() / 2, height());
  189. right_half->SetRect(
  190. left_half->right(), y(), width() - left_half->width(), height());
  191. }
  192. bool RectF::SharesEdgeWith(const RectF& rect) const {
  193. return (y() == rect.y() && height() == rect.height() &&
  194. (x() == rect.right() || right() == rect.x())) ||
  195. (x() == rect.x() && width() == rect.width() &&
  196. (y() == rect.bottom() || bottom() == rect.y()));
  197. }
  198. float RectF::ManhattanDistanceToPoint(const PointF& point) const {
  199. float x_distance =
  200. std::max<float>(0, std::max(x() - point.x(), point.x() - right()));
  201. float y_distance =
  202. std::max<float>(0, std::max(y() - point.y(), point.y() - bottom()));
  203. return x_distance + y_distance;
  204. }
  205. float RectF::ManhattanInternalDistance(const RectF& rect) const {
  206. RectF c(*this);
  207. c.Union(rect);
  208. static constexpr float kEpsilon = std::numeric_limits<float>::epsilon();
  209. float x = std::max(0.f, c.width() - width() - rect.width() + kEpsilon);
  210. float y = std::max(0.f, c.height() - height() - rect.height() + kEpsilon);
  211. return x + y;
  212. }
  213. PointF RectF::ClosestPoint(const PointF& point) const {
  214. return PointF(std::min(std::max(point.x(), x()), right()),
  215. std::min(std::max(point.y(), y()), bottom()));
  216. }
  217. bool RectF::IsExpressibleAsRect() const {
  218. return base::IsValueInRangeForNumericType<int>(x()) &&
  219. base::IsValueInRangeForNumericType<int>(y()) &&
  220. base::IsValueInRangeForNumericType<int>(width()) &&
  221. base::IsValueInRangeForNumericType<int>(height()) &&
  222. base::IsValueInRangeForNumericType<int>(right()) &&
  223. base::IsValueInRangeForNumericType<int>(bottom());
  224. }
  225. RectF IntersectRects(const RectF& a, const RectF& b) {
  226. RectF result = a;
  227. result.Intersect(b);
  228. return result;
  229. }
  230. RectF UnionRects(const RectF& a, const RectF& b) {
  231. RectF result = a;
  232. result.Union(b);
  233. return result;
  234. }
  235. RectF UnionRectsEvenIfEmpty(const RectF& a, const RectF& b) {
  236. RectF result = a;
  237. result.UnionEvenIfEmpty(b);
  238. return result;
  239. }
  240. RectF SubtractRects(const RectF& a, const RectF& b) {
  241. RectF result = a;
  242. result.Subtract(b);
  243. return result;
  244. }
  245. RectF BoundingRect(const PointF& p1, const PointF& p2) {
  246. float rx = std::min(p1.x(), p2.x());
  247. float ry = std::min(p1.y(), p2.y());
  248. float rr = std::max(p1.x(), p2.x());
  249. float rb = std::max(p1.y(), p2.y());
  250. return RectF(rx, ry, rr - rx, rb - ry);
  251. }
  252. RectF MaximumCoveredRect(const RectF& a, const RectF& b) {
  253. // Check a or b by itself.
  254. RectF maximum = a;
  255. float maximum_area = a.size().GetArea();
  256. if (b.size().GetArea() > maximum_area) {
  257. maximum = b;
  258. maximum_area = b.size().GetArea();
  259. }
  260. // Check the regions that include the intersection of a and b. This can be
  261. // done by taking the intersection and expanding it vertically and
  262. // horizontally. These expanded intersections will both still be covered by
  263. // a or b.
  264. RectF intersection = a;
  265. intersection.InclusiveIntersect(b);
  266. if (!intersection.size().IsZero()) {
  267. RectF vert_expanded_intersection = intersection;
  268. vert_expanded_intersection.set_y(std::min(a.y(), b.y()));
  269. vert_expanded_intersection.set_height(std::max(a.bottom(), b.bottom()) -
  270. vert_expanded_intersection.y());
  271. if (vert_expanded_intersection.size().GetArea() > maximum_area) {
  272. maximum = vert_expanded_intersection;
  273. maximum_area = vert_expanded_intersection.size().GetArea();
  274. }
  275. RectF horiz_expanded_intersection(intersection);
  276. horiz_expanded_intersection.set_x(std::min(a.x(), b.x()));
  277. horiz_expanded_intersection.set_width(std::max(a.right(), b.right()) -
  278. horiz_expanded_intersection.x());
  279. if (horiz_expanded_intersection.size().GetArea() > maximum_area) {
  280. maximum = horiz_expanded_intersection;
  281. maximum_area = horiz_expanded_intersection.size().GetArea();
  282. }
  283. }
  284. return maximum;
  285. }
  286. RectF MapRect(const RectF& r, const RectF& src_rect, const RectF& dest_rect) {
  287. if (src_rect.IsEmpty())
  288. return RectF();
  289. float width_scale = dest_rect.width() / src_rect.width();
  290. float height_scale = dest_rect.height() / src_rect.height();
  291. return RectF(dest_rect.x() + (r.x() - src_rect.x()) * width_scale,
  292. dest_rect.y() + (r.y() - src_rect.y()) * height_scale,
  293. r.width() * width_scale, r.height() * height_scale);
  294. }
  295. std::string RectF::ToString() const {
  296. return base::StringPrintf("%s %s", origin().ToString().c_str(),
  297. size().ToString().c_str());
  298. }
  299. bool RectF::ApproximatelyEqual(const RectF& rect,
  300. float tolerance_x,
  301. float tolerance_y) const {
  302. return std::abs(x() - rect.x()) <= tolerance_x &&
  303. std::abs(y() - rect.y()) <= tolerance_y &&
  304. std::abs(right() - rect.right()) <= tolerance_x &&
  305. std::abs(bottom() - rect.bottom()) <= tolerance_y;
  306. }
  307. } // namespace gfx