AreaAllocator.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 Lasse Oorni
  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. namespace Urho3D
  26. {
  27. AreaAllocator::AreaAllocator(int width, int height)
  28. {
  29. Reset(width, height);
  30. }
  31. void AreaAllocator::Reset(int width, int height)
  32. {
  33. freeAreas_.Clear();
  34. IntRect initialArea(0, 0, width, height);
  35. freeAreas_.Push(initialArea);
  36. }
  37. bool AreaAllocator::Allocate(int width, int height, int& x, int& y)
  38. {
  39. if (width < 0)
  40. width = 0;
  41. if (height < 0)
  42. height = 0;
  43. PODVector<IntRect>::Iterator best = freeAreas_.End();
  44. int bestFreeArea = M_MAX_INT;
  45. for (PODVector<IntRect>::Iterator i = freeAreas_.Begin(); i != freeAreas_.End(); ++i)
  46. {
  47. int freeWidth = i->right_ - i->left_;
  48. int freeHeight = i->bottom_ - i->top_;
  49. if (freeWidth >= width && freeHeight >= height)
  50. {
  51. // Calculate rank for free area. Lower is better
  52. int freeArea = freeWidth * freeHeight;
  53. if (best == freeAreas_.End() || freeArea < bestFreeArea)
  54. {
  55. best = i;
  56. bestFreeArea = freeArea;
  57. }
  58. }
  59. }
  60. if (best == freeAreas_.End())
  61. return false;
  62. IntRect reserved;
  63. reserved.left_ = best->left_;
  64. reserved.top_ = best->top_;
  65. reserved.right_ = best->left_ + width;
  66. reserved.bottom_ = best->top_ + height;
  67. x = best->left_;
  68. y = best->top_;
  69. // Remove the reserved area from all free areas
  70. for (unsigned i = 0; i < freeAreas_.Size();)
  71. {
  72. if (SplitRect(freeAreas_[i], reserved))
  73. freeAreas_.Erase(i);
  74. else
  75. ++i;
  76. }
  77. Cleanup();
  78. return true;
  79. }
  80. bool AreaAllocator::SplitRect(IntRect original, const IntRect& reserve)
  81. {
  82. if (reserve.right_ > original.left_ && reserve.left_ < original.right_ && reserve.bottom_ > original.top_ &&
  83. reserve.top_ < original.bottom_)
  84. {
  85. // Check for splitting from the right
  86. if (reserve.right_ < original.right_)
  87. {
  88. IntRect newRect = original;
  89. newRect.left_ = reserve.right_;
  90. freeAreas_.Push(newRect);
  91. }
  92. // Check for splitting from the left
  93. if (reserve.left_ > original.left_)
  94. {
  95. IntRect newRect = original;
  96. newRect.right_ = reserve.left_;
  97. freeAreas_.Push(newRect);
  98. }
  99. // Check for splitting from the bottom
  100. if (reserve.bottom_ < original.bottom_)
  101. {
  102. IntRect newRect = original;
  103. newRect.top_ = reserve.bottom_;
  104. freeAreas_.Push(newRect);
  105. }
  106. // Check for splitting from the top
  107. if (reserve.top_ > original.top_)
  108. {
  109. IntRect newRect = original;
  110. newRect.bottom_ = reserve.top_;
  111. freeAreas_.Push(newRect);
  112. }
  113. return true;
  114. }
  115. return false;
  116. }
  117. void AreaAllocator::Cleanup()
  118. {
  119. // Remove rects which are contained within another rect
  120. for (unsigned i = 0; i < freeAreas_.Size(); )
  121. {
  122. bool erased = false;
  123. for (unsigned j = i + 1; j < freeAreas_.Size(); )
  124. {
  125. if ((freeAreas_[i].left_ >= freeAreas_[j].left_) &&
  126. (freeAreas_[i].top_ >= freeAreas_[j].top_) &&
  127. (freeAreas_[i].right_ <= freeAreas_[j].right_) &&
  128. (freeAreas_[i].bottom_ <= freeAreas_[j].bottom_))
  129. {
  130. freeAreas_.Erase(i);
  131. erased = true;
  132. break;
  133. }
  134. if ((freeAreas_[j].left_ >= freeAreas_[i].left_) &&
  135. (freeAreas_[j].top_ >= freeAreas_[i].top_) &&
  136. (freeAreas_[j].right_ <= freeAreas_[i].right_) &&
  137. (freeAreas_[j].bottom_ <= freeAreas_[i].bottom_))
  138. freeAreas_.Erase(j);
  139. else
  140. ++j;
  141. }
  142. if (!erased)
  143. ++i;
  144. }
  145. }
  146. }