AreaAllocator.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 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. mFreeAreas.clear();
  32. IntRect initialArea(0, 0, width, height);
  33. mFreeAreas.push_back(initialArea);
  34. }
  35. bool AreaAllocator::reserve(int width, int height, int& x, int& y)
  36. {
  37. if (width < 0)
  38. width = 0;
  39. if (height < 0)
  40. height = 0;
  41. std::vector<IntRect>::iterator best = mFreeAreas.end();
  42. int bestFreeArea = M_MAX_INT;
  43. for (std::vector<IntRect>::iterator i = mFreeAreas.begin(); i != mFreeAreas.end(); ++i)
  44. {
  45. int freeWidth = i->mRight - i->mLeft;
  46. int freeHeight = i->mBottom - i->mTop;
  47. if ((freeWidth >= width) && (freeHeight >= height))
  48. {
  49. // Calculate rank for free area. Lower is better
  50. int freeArea = freeWidth * freeHeight;
  51. if ((best == mFreeAreas.end()) || (freeArea < bestFreeArea))
  52. {
  53. best = i;
  54. bestFreeArea = freeArea;
  55. }
  56. }
  57. }
  58. if (best == mFreeAreas.end())
  59. return false;
  60. IntRect reserved;
  61. reserved.mLeft = best->mLeft;
  62. reserved.mTop = best->mTop;
  63. reserved.mRight = best->mLeft + width;
  64. reserved.mBottom = best->mTop + height;
  65. x = best->mLeft;
  66. y = best->mTop;
  67. // Remove the reserved area from all free areas
  68. for (unsigned i = 0; i < mFreeAreas.size();)
  69. {
  70. if (splitRect(mFreeAreas[i], reserved))
  71. mFreeAreas.erase(mFreeAreas.begin() + 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.mRight > original.mLeft) && (reserve.mLeft < original.mRight) &&
  81. (reserve.mBottom > original.mTop) && (reserve.mTop < original.mBottom))
  82. {
  83. // Check for splitting from the right
  84. if (reserve.mRight < original.mRight)
  85. {
  86. IntRect newRect = original;
  87. newRect.mLeft = reserve.mRight;
  88. mFreeAreas.push_back(newRect);
  89. }
  90. // Check for splitting from the left
  91. if (reserve.mLeft > original.mLeft)
  92. {
  93. IntRect newRect = original;
  94. newRect.mRight = reserve.mLeft;
  95. mFreeAreas.push_back(newRect);
  96. }
  97. // Check for splitting from the bottom
  98. if (reserve.mBottom < original.mBottom)
  99. {
  100. IntRect newRect = original;
  101. newRect.mTop = reserve.mBottom;
  102. mFreeAreas.push_back(newRect);
  103. }
  104. // Check for splitting from the top
  105. if (reserve.mTop > original.mTop)
  106. {
  107. IntRect newRect = original;
  108. newRect.mBottom = reserve.mTop;
  109. mFreeAreas.push_back(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 < mFreeAreas.size(); )
  119. {
  120. bool erased = false;
  121. for (unsigned j = i + 1; j < mFreeAreas.size(); )
  122. {
  123. if ((mFreeAreas[i].mLeft >= mFreeAreas[j].mLeft) &&
  124. (mFreeAreas[i].mTop >= mFreeAreas[j].mTop) &&
  125. (mFreeAreas[i].mRight <= mFreeAreas[j].mRight) &&
  126. (mFreeAreas[i].mBottom <= mFreeAreas[j].mBottom))
  127. {
  128. mFreeAreas.erase(mFreeAreas.begin() + i);
  129. erased = true;
  130. break;
  131. }
  132. if ((mFreeAreas[j].mLeft >= mFreeAreas[i].mLeft) &&
  133. (mFreeAreas[j].mTop >= mFreeAreas[i].mTop) &&
  134. (mFreeAreas[j].mRight <= mFreeAreas[i].mRight) &&
  135. (mFreeAreas[j].mBottom <= mFreeAreas[i].mBottom))
  136. mFreeAreas.erase(mFreeAreas.begin() + j);
  137. else
  138. ++j;
  139. }
  140. if (!erased)
  141. ++i;
  142. }
  143. }