AreaAllocator.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Math/AreaAllocator.h"
  5. #include "../DebugNew.h"
  6. namespace Urho3D
  7. {
  8. AreaAllocator::AreaAllocator()
  9. {
  10. Reset(0, 0);
  11. }
  12. AreaAllocator::AreaAllocator(i32 width, i32 height, bool fastMode)
  13. {
  14. Reset(width, height, width, height, fastMode);
  15. }
  16. AreaAllocator::AreaAllocator(i32 width, i32 height, i32 maxWidth, i32 maxHeight, bool fastMode)
  17. {
  18. Reset(width, height, maxWidth, maxHeight, fastMode);
  19. }
  20. void AreaAllocator::Reset(i32 width, i32 height, i32 maxWidth, i32 maxHeight, bool fastMode)
  21. {
  22. doubleWidth_ = true;
  23. size_ = IntVector2(width, height);
  24. maxSize_ = IntVector2(maxWidth, maxHeight);
  25. fastMode_ = fastMode;
  26. freeAreas_.Clear();
  27. IntRect initialArea(0, 0, width, height);
  28. freeAreas_.Push(initialArea);
  29. }
  30. bool AreaAllocator::Allocate(i32 width, i32 height, i32& x, i32& y)
  31. {
  32. if (width < 0)
  33. width = 0;
  34. if (height < 0)
  35. height = 0;
  36. Vector<IntRect>::Iterator best;
  37. i32 bestFreeArea;
  38. for (;;)
  39. {
  40. best = freeAreas_.End();
  41. bestFreeArea = M_MAX_INT;
  42. for (Vector<IntRect>::Iterator i = freeAreas_.Begin(); i != freeAreas_.End(); ++i)
  43. {
  44. i32 freeWidth = i->Width();
  45. i32 freeHeight = i->Height();
  46. if (freeWidth >= width && freeHeight >= height)
  47. {
  48. // Calculate rank for free area. Lower is better
  49. i32 freeArea = freeWidth * freeHeight;
  50. if (freeArea < bestFreeArea)
  51. {
  52. best = i;
  53. bestFreeArea = freeArea;
  54. }
  55. }
  56. }
  57. if (best == freeAreas_.End())
  58. {
  59. if (doubleWidth_ && size_.x_ < maxSize_.x_)
  60. {
  61. i32 oldWidth = size_.x_;
  62. size_.x_ <<= 1;
  63. // If no allocations yet, simply expand the single free area
  64. IntRect& first = freeAreas_.Front();
  65. if (freeAreas_.Size() == 1 && first.left_ == 0 && first.top_ == 0 && first.right_ == oldWidth &&
  66. first.bottom_ == size_.y_)
  67. first.right_ = size_.x_;
  68. else
  69. {
  70. IntRect newArea(oldWidth, 0, size_.x_, size_.y_);
  71. freeAreas_.Push(newArea);
  72. }
  73. }
  74. else if (!doubleWidth_ && size_.y_ < maxSize_.y_)
  75. {
  76. i32 oldHeight = size_.y_;
  77. size_.y_ <<= 1;
  78. // If no allocations yet, simply expand the single free area
  79. IntRect& first = freeAreas_.Front();
  80. if (freeAreas_.Size() == 1 && first.left_ == 0 && first.top_ == 0 && first.right_ == size_.x_ &&
  81. first.bottom_ == oldHeight)
  82. first.bottom_ = size_.y_;
  83. else
  84. {
  85. IntRect newArea(0, oldHeight, size_.x_, size_.y_);
  86. freeAreas_.Push(newArea);
  87. }
  88. }
  89. else
  90. return false;
  91. doubleWidth_ = !doubleWidth_;
  92. }
  93. else
  94. break;
  95. }
  96. IntRect reserved(best->left_, best->top_, best->left_ + width, best->top_ + height);
  97. x = best->left_;
  98. y = best->top_;
  99. if (fastMode_)
  100. {
  101. // Reserve the area by splitting up the remaining free area
  102. best->left_ = reserved.right_;
  103. if (best->Height() > 2 * height || height >= size_.y_ / 2)
  104. {
  105. IntRect splitArea(reserved.left_, reserved.bottom_, best->right_, best->bottom_);
  106. best->bottom_ = reserved.bottom_;
  107. freeAreas_.Push(splitArea);
  108. }
  109. }
  110. else
  111. {
  112. // Remove the reserved area from all free areas
  113. for (i32 i = 0; i < freeAreas_.Size();)
  114. {
  115. if (SplitRect(i, reserved))
  116. freeAreas_.Erase(i);
  117. else
  118. ++i;
  119. }
  120. Cleanup();
  121. }
  122. return true;
  123. }
  124. bool AreaAllocator::SplitRect(i32 freeAreaIndex, const IntRect& reserve)
  125. {
  126. assert(freeAreaIndex >= 0);
  127. // Make a copy, as the vector will be modified
  128. IntRect original = freeAreas_[freeAreaIndex];
  129. if (reserve.right_ > original.left_ && reserve.left_ < original.right_ && reserve.bottom_ > original.top_ &&
  130. reserve.top_ < original.bottom_)
  131. {
  132. // Check for splitting from the right
  133. if (reserve.right_ < original.right_)
  134. {
  135. IntRect newRect = original;
  136. newRect.left_ = reserve.right_;
  137. freeAreas_.Push(newRect);
  138. }
  139. // Check for splitting from the left
  140. if (reserve.left_ > original.left_)
  141. {
  142. IntRect newRect = original;
  143. newRect.right_ = reserve.left_;
  144. freeAreas_.Push(newRect);
  145. }
  146. // Check for splitting from the bottom
  147. if (reserve.bottom_ < original.bottom_)
  148. {
  149. IntRect newRect = original;
  150. newRect.top_ = reserve.bottom_;
  151. freeAreas_.Push(newRect);
  152. }
  153. // Check for splitting from the top
  154. if (reserve.top_ > original.top_)
  155. {
  156. IntRect newRect = original;
  157. newRect.bottom_ = reserve.top_;
  158. freeAreas_.Push(newRect);
  159. }
  160. return true;
  161. }
  162. return false;
  163. }
  164. void AreaAllocator::Cleanup()
  165. {
  166. // Remove rects which are contained within another rect
  167. for (i32 i = 0; i < freeAreas_.Size();)
  168. {
  169. bool erased = false;
  170. for (i32 j = i + 1; j < freeAreas_.Size();)
  171. {
  172. if ((freeAreas_[i].left_ >= freeAreas_[j].left_) &&
  173. (freeAreas_[i].top_ >= freeAreas_[j].top_) &&
  174. (freeAreas_[i].right_ <= freeAreas_[j].right_) &&
  175. (freeAreas_[i].bottom_ <= freeAreas_[j].bottom_))
  176. {
  177. freeAreas_.Erase(i);
  178. erased = true;
  179. break;
  180. }
  181. if ((freeAreas_[j].left_ >= freeAreas_[i].left_) &&
  182. (freeAreas_[j].top_ >= freeAreas_[i].top_) &&
  183. (freeAreas_[j].right_ <= freeAreas_[i].right_) &&
  184. (freeAreas_[j].bottom_ <= freeAreas_[i].bottom_))
  185. freeAreas_.Erase(j);
  186. else
  187. ++j;
  188. }
  189. if (!erased)
  190. ++i;
  191. }
  192. }
  193. }