AreaAllocator.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "AreaAllocator.h"
  25. AreaAllocator::AreaAllocator(int width, int height)
  26. {
  27. Reset(width, height);
  28. }
  29. void AreaAllocator::Reset(int width, int height)
  30. {
  31. freeAreas_.Clear();
  32. IntRect initialArea(0, 0, width, height);
  33. freeAreas_.Push(initialArea);
  34. }
  35. bool AreaAllocator::Allocate(int width, int height, int& x, int& y)
  36. {
  37. if (width < 0)
  38. width = 0;
  39. if (height < 0)
  40. height = 0;
  41. PODVector<IntRect>::Iterator best = freeAreas_.End();
  42. int bestFreeArea = M_MAX_INT;
  43. for (PODVector<IntRect>::Iterator i = freeAreas_.Begin(); i != freeAreas_.End(); ++i)
  44. {
  45. int freeWidth = i->right_ - i->left_;
  46. int freeHeight = i->bottom_ - i->top_;
  47. if (freeWidth >= width && freeHeight >= height)
  48. {
  49. // Calculate rank for free area. Lower is better
  50. int freeArea = freeWidth * freeHeight;
  51. if (best == freeAreas_.End() || freeArea < bestFreeArea)
  52. {
  53. best = i;
  54. bestFreeArea = freeArea;
  55. }
  56. }
  57. }
  58. if (best == freeAreas_.End())
  59. return false;
  60. IntRect reserved;
  61. reserved.left_ = best->left_;
  62. reserved.top_ = best->top_;
  63. reserved.right_ = best->left_ + width;
  64. reserved.bottom_ = best->top_ + height;
  65. x = best->left_;
  66. y = best->top_;
  67. // Remove the reserved area from all free areas
  68. for (unsigned i = 0; i < freeAreas_.Size();)
  69. {
  70. if (SplitRect(freeAreas_[i], reserved))
  71. freeAreas_.Erase(i);
  72. else
  73. ++i;
  74. }
  75. Cleanup();
  76. return true;
  77. }
  78. bool AreaAllocator::SplitRect(IntRect original, const IntRect& reserve)
  79. {
  80. if (reserve.right_ > original.left_ && reserve.left_ < original.right_ && reserve.bottom_ > original.top_ &&
  81. reserve.top_ < original.bottom_)
  82. {
  83. // Check for splitting from the right
  84. if (reserve.right_ < original.right_)
  85. {
  86. IntRect newRect = original;
  87. newRect.left_ = reserve.right_;
  88. freeAreas_.Push(newRect);
  89. }
  90. // Check for splitting from the left
  91. if (reserve.left_ > original.left_)
  92. {
  93. IntRect newRect = original;
  94. newRect.right_ = reserve.left_;
  95. freeAreas_.Push(newRect);
  96. }
  97. // Check for splitting from the bottom
  98. if (reserve.bottom_ < original.bottom_)
  99. {
  100. IntRect newRect = original;
  101. newRect.top_ = reserve.bottom_;
  102. freeAreas_.Push(newRect);
  103. }
  104. // Check for splitting from the top
  105. if (reserve.top_ > original.top_)
  106. {
  107. IntRect newRect = original;
  108. newRect.bottom_ = reserve.top_;
  109. freeAreas_.Push(newRect);
  110. }
  111. return true;
  112. }
  113. return false;
  114. }
  115. void AreaAllocator::Cleanup()
  116. {
  117. // Remove rects which are contained within another rect
  118. for (unsigned i = 0; i < freeAreas_.Size(); )
  119. {
  120. bool erased = false;
  121. for (unsigned j = i + 1; j < freeAreas_.Size(); )
  122. {
  123. if ((freeAreas_[i].left_ >= freeAreas_[j].left_) &&
  124. (freeAreas_[i].top_ >= freeAreas_[j].top_) &&
  125. (freeAreas_[i].right_ <= freeAreas_[j].right_) &&
  126. (freeAreas_[i].bottom_ <= freeAreas_[j].bottom_))
  127. {
  128. freeAreas_.Erase(i);
  129. erased = true;
  130. break;
  131. }
  132. if ((freeAreas_[j].left_ >= freeAreas_[i].left_) &&
  133. (freeAreas_[j].top_ >= freeAreas_[i].top_) &&
  134. (freeAreas_[j].right_ <= freeAreas_[i].right_) &&
  135. (freeAreas_[j].bottom_ <= freeAreas_[i].bottom_))
  136. freeAreas_.Erase(j);
  137. else
  138. ++j;
  139. }
  140. if (!erased)
  141. ++i;
  142. }
  143. }