AreaAllocator.cpp 7.4 KB

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