DisjointRectSet.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Vector.h>
  8. #include <LibGfx/Point.h>
  9. #include <LibGfx/Rect.h>
  10. namespace Gfx {
  11. class DisjointRectSet {
  12. public:
  13. DisjointRectSet(const DisjointRectSet&) = delete;
  14. DisjointRectSet& operator=(const DisjointRectSet&) = delete;
  15. DisjointRectSet() { }
  16. ~DisjointRectSet() { }
  17. DisjointRectSet(const IntRect& rect)
  18. {
  19. m_rects.append(rect);
  20. }
  21. DisjointRectSet(DisjointRectSet&&) = default;
  22. DisjointRectSet& operator=(DisjointRectSet&&) = default;
  23. DisjointRectSet clone() const
  24. {
  25. DisjointRectSet rects;
  26. rects.m_rects = m_rects;
  27. return rects;
  28. }
  29. void move_by(int dx, int dy);
  30. void move_by(const IntPoint& delta)
  31. {
  32. move_by(delta.x(), delta.y());
  33. }
  34. void add(const IntRect& rect)
  35. {
  36. if (add_no_shatter(rect) && m_rects.size() > 1)
  37. shatter();
  38. }
  39. template<typename Container>
  40. void add_many(const Container& rects)
  41. {
  42. bool added = false;
  43. for (const auto& rect : rects) {
  44. if (add_no_shatter(rect))
  45. added = true;
  46. }
  47. if (added && m_rects.size() > 1)
  48. shatter();
  49. }
  50. void add(const DisjointRectSet& rect_set)
  51. {
  52. if (this == &rect_set)
  53. return;
  54. if (m_rects.is_empty()) {
  55. m_rects = rect_set.m_rects;
  56. } else {
  57. add_many(rect_set.rects());
  58. }
  59. }
  60. DisjointRectSet shatter(const IntRect&) const;
  61. DisjointRectSet shatter(const DisjointRectSet& hammer) const;
  62. bool contains(const IntRect&) const;
  63. bool intersects(const IntRect&) const;
  64. bool intersects(const DisjointRectSet&) const;
  65. DisjointRectSet intersected(const IntRect&) const;
  66. DisjointRectSet intersected(const DisjointRectSet&) const;
  67. template<typename Function>
  68. IterationDecision for_each_intersected(const IntRect& rect, Function f) const
  69. {
  70. if (is_empty() || rect.is_empty())
  71. return IterationDecision::Continue;
  72. for (auto& r : m_rects) {
  73. auto intersected_rect = r.intersected(rect);
  74. if (intersected_rect.is_empty())
  75. continue;
  76. IterationDecision decision = f(intersected_rect);
  77. if (decision != IterationDecision::Continue)
  78. return decision;
  79. }
  80. return IterationDecision::Continue;
  81. }
  82. template<typename Function>
  83. IterationDecision for_each_intersected(const DisjointRectSet& rects, Function f) const
  84. {
  85. if (is_empty() || rects.is_empty())
  86. return IterationDecision::Continue;
  87. if (this == &rects) {
  88. for (auto& r : m_rects) {
  89. IterationDecision decision = f(r);
  90. if (decision != IterationDecision::Continue)
  91. return decision;
  92. }
  93. } else {
  94. for (auto& r : m_rects) {
  95. for (auto& r2 : rects.m_rects) {
  96. auto intersected_rect = r.intersected(r2);
  97. if (intersected_rect.is_empty())
  98. continue;
  99. IterationDecision decision = f(intersected_rect);
  100. if (decision != IterationDecision::Continue)
  101. return decision;
  102. }
  103. }
  104. }
  105. return IterationDecision::Continue;
  106. }
  107. bool is_empty() const { return m_rects.is_empty(); }
  108. size_t size() const { return m_rects.size(); }
  109. void clear() { m_rects.clear(); }
  110. void clear_with_capacity() { m_rects.clear_with_capacity(); }
  111. const Vector<IntRect, 32>& rects() const { return m_rects; }
  112. Vector<IntRect, 32> take_rects() { return move(m_rects); }
  113. void translate_by(int dx, int dy)
  114. {
  115. for (auto& rect : m_rects)
  116. rect.translate_by(dx, dy);
  117. }
  118. void translate_by(Gfx::IntPoint const& delta)
  119. {
  120. for (auto& rect : m_rects)
  121. rect.translate_by(delta);
  122. }
  123. private:
  124. bool add_no_shatter(const IntRect&);
  125. void shatter();
  126. Vector<IntRect, 32> m_rects;
  127. };
  128. }