AreaAllocator.cpp 7.4 KB

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