AreaAllocator.cpp 5.1 KB

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