MaxRectsBinPack.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /** @file MaxRectsBinPack.cpp
  2. @author Jukka Jylänki
  3. @brief Implements different bin packer algorithms that use the MAXRECTS data structure.
  4. This work is released to Public Domain, do whatever you want with it.
  5. */
  6. #include "MaxRectsBinPack.h"
  7. MaxRectsBinPack::MaxRectsBinPack()
  8. :binWidth(0),
  9. binHeight(0),
  10. allowRotate(false)
  11. {
  12. }
  13. MaxRectsBinPack::MaxRectsBinPack(int width, int height, bool allowRotate)
  14. {
  15. Init(width, height, allowRotate);
  16. }
  17. void MaxRectsBinPack::Init(int width, int height, bool allowRotate_)
  18. {
  19. binWidth = width;
  20. binHeight = height;
  21. allowRotate = allowRotate_;
  22. Rect n;
  23. n.x = 0;
  24. n.y = 0;
  25. n.width = width;
  26. n.height = height;
  27. usedRectangles.clear();
  28. freeRectangles.clear();
  29. freeRectangles.add(n);
  30. }
  31. Rect MaxRectsBinPack::Insert(int width, int height, FreeRectChoiceHeuristic method)
  32. {
  33. Rect newNode;
  34. int score1; // Unused in this function. We don't need to know the score after finding the position.
  35. int score2;
  36. switch(method)
  37. {
  38. case RectBestShortSideFit: newNode = FindPositionForNewNodeBestShortSideFit(width, height, score1, score2); break;
  39. case RectBottomLeftRule: newNode = FindPositionForNewNodeBottomLeft(width, height, score1, score2); break;
  40. case RectContactPointRule: newNode = FindPositionForNewNodeContactPoint(width, height, score1); break;
  41. case RectBestLongSideFit: newNode = FindPositionForNewNodeBestLongSideFit(width, height, score2, score1); break;
  42. case RectBestAreaFit: newNode = FindPositionForNewNodeBestAreaFit(width, height, score1, score2); break;
  43. }
  44. if (newNode.height == 0)
  45. return newNode;
  46. SplitFreeRects(newNode);
  47. PruneFreeList();
  48. usedRectangles.add(newNode);
  49. return newNode;
  50. }
  51. void MaxRectsBinPack::Insert(MemPtr<RectSizeIndex> rects, MemPtr<RectIndex> dst, FreeRectChoiceHeuristic method)
  52. {
  53. dst.clear();
  54. while(rects.elms() > 0)
  55. {
  56. int bestScore1 = INT_MAX;
  57. int bestScore2 = INT_MAX;
  58. int bestRectIndex = -1;
  59. Rect bestNode;
  60. for(Int i = 0; i < rects.elms(); ++i)
  61. {
  62. int score1;
  63. int score2;
  64. Rect newNode = ScoreRect(rects[i].width, rects[i].height, method, score1, score2);
  65. if (score1 < bestScore1 || (score1 == bestScore1 && score2 < bestScore2))
  66. {
  67. bestScore1 = score1;
  68. bestScore2 = score2;
  69. bestNode = newNode;
  70. bestRectIndex = i;
  71. }
  72. }
  73. if (bestRectIndex == -1)
  74. return;
  75. PlaceRect(bestNode);
  76. RectIndex &dest=dst.New();
  77. SCAST(Rect, dest)=bestNode;
  78. dest.index=rects[bestRectIndex].index;
  79. rects.remove(bestRectIndex);
  80. }
  81. }
  82. void MaxRectsBinPack::PlaceRect(const Rect &node)
  83. {
  84. SplitFreeRects(node);
  85. PruneFreeList();
  86. usedRectangles.add(node);
  87. }
  88. Rect MaxRectsBinPack::ScoreRect(int width, int height, FreeRectChoiceHeuristic method, int &score1, int &score2) const
  89. {
  90. Rect newNode;
  91. score1 = INT_MAX;
  92. score2 = INT_MAX;
  93. switch(method)
  94. {
  95. case RectBestShortSideFit: newNode = FindPositionForNewNodeBestShortSideFit(width, height, score1, score2); break;
  96. case RectBottomLeftRule: newNode = FindPositionForNewNodeBottomLeft(width, height, score1, score2); break;
  97. case RectContactPointRule: newNode = FindPositionForNewNodeContactPoint(width, height, score1);
  98. score1 = -score1; // Reverse since we are minimizing, but for contact point score bigger is better.
  99. break;
  100. case RectBestLongSideFit: newNode = FindPositionForNewNodeBestLongSideFit(width, height, score2, score1); break;
  101. case RectBestAreaFit: newNode = FindPositionForNewNodeBestAreaFit(width, height, score1, score2); break;
  102. }
  103. // Cannot fit the current rectangle.
  104. if (newNode.height == 0)
  105. {
  106. score1 = INT_MAX;
  107. score2 = INT_MAX;
  108. }
  109. return newNode;
  110. }
  111. /// Computes the ratio of used surface area.
  112. float MaxRectsBinPack::Occupancy() const
  113. {
  114. unsigned long usedSurfaceArea = 0;
  115. for(Int i = 0; i < usedRectangles.elms(); ++i)
  116. usedSurfaceArea += usedRectangles[i].width * usedRectangles[i].height;
  117. return (float)usedSurfaceArea / (binWidth * binHeight);
  118. }
  119. Rect MaxRectsBinPack::FindPositionForNewNodeBottomLeft(int width, int height, int &bestY, int &bestX) const
  120. {
  121. Rect bestNode;
  122. memset(&bestNode, 0, sizeof(Rect));
  123. bestY = INT_MAX;
  124. for(Int i = 0; i < freeRectangles.elms(); ++i)
  125. {
  126. // Try to place the rectangle in upright (non-flipped) orientation.
  127. if (freeRectangles[i].width >= width && freeRectangles[i].height >= height)
  128. {
  129. int topSideY = freeRectangles[i].y + height;
  130. if (topSideY < bestY || (topSideY == bestY && freeRectangles[i].x < bestX))
  131. {
  132. bestNode.x = freeRectangles[i].x;
  133. bestNode.y = freeRectangles[i].y;
  134. bestNode.width = width;
  135. bestNode.height = height;
  136. bestY = topSideY;
  137. bestX = freeRectangles[i].x;
  138. }
  139. }
  140. if (allowRotate && freeRectangles[i].width >= height && freeRectangles[i].height >= width)
  141. {
  142. int topSideY = freeRectangles[i].y + width;
  143. if (topSideY < bestY || (topSideY == bestY && freeRectangles[i].x < bestX))
  144. {
  145. bestNode.x = freeRectangles[i].x;
  146. bestNode.y = freeRectangles[i].y;
  147. bestNode.width = height;
  148. bestNode.height = width;
  149. bestY = topSideY;
  150. bestX = freeRectangles[i].x;
  151. }
  152. }
  153. }
  154. return bestNode;
  155. }
  156. Rect MaxRectsBinPack::FindPositionForNewNodeBestShortSideFit(int width, int height,
  157. int &bestShortSideFit, int &bestLongSideFit) const
  158. {
  159. Rect bestNode;
  160. memset(&bestNode, 0, sizeof(Rect));
  161. bestShortSideFit = INT_MAX;
  162. for(Int i = 0; i < freeRectangles.elms(); ++i)
  163. {
  164. // Try to place the rectangle in upright (non-flipped) orientation.
  165. if (freeRectangles[i].width >= width && freeRectangles[i].height >= height)
  166. {
  167. int leftoverHoriz = abs(freeRectangles[i].width - width);
  168. int leftoverVert = abs(freeRectangles[i].height - height);
  169. int shortSideFit = Min(leftoverHoriz, leftoverVert);
  170. int longSideFit = Max(leftoverHoriz, leftoverVert);
  171. if (shortSideFit < bestShortSideFit || (shortSideFit == bestShortSideFit && longSideFit < bestLongSideFit))
  172. {
  173. bestNode.x = freeRectangles[i].x;
  174. bestNode.y = freeRectangles[i].y;
  175. bestNode.width = width;
  176. bestNode.height = height;
  177. bestShortSideFit = shortSideFit;
  178. bestLongSideFit = longSideFit;
  179. }
  180. }
  181. if (allowRotate && freeRectangles[i].width >= height && freeRectangles[i].height >= width)
  182. {
  183. int flippedLeftoverHoriz = abs(freeRectangles[i].width - height);
  184. int flippedLeftoverVert = abs(freeRectangles[i].height - width);
  185. int flippedShortSideFit = Min(flippedLeftoverHoriz, flippedLeftoverVert);
  186. int flippedLongSideFit = Max(flippedLeftoverHoriz, flippedLeftoverVert);
  187. if (flippedShortSideFit < bestShortSideFit || (flippedShortSideFit == bestShortSideFit && flippedLongSideFit < bestLongSideFit))
  188. {
  189. bestNode.x = freeRectangles[i].x;
  190. bestNode.y = freeRectangles[i].y;
  191. bestNode.width = height;
  192. bestNode.height = width;
  193. bestShortSideFit = flippedShortSideFit;
  194. bestLongSideFit = flippedLongSideFit;
  195. }
  196. }
  197. }
  198. return bestNode;
  199. }
  200. Rect MaxRectsBinPack::FindPositionForNewNodeBestLongSideFit(int width, int height,
  201. int &bestShortSideFit, int &bestLongSideFit) const
  202. {
  203. Rect bestNode;
  204. memset(&bestNode, 0, sizeof(Rect));
  205. bestLongSideFit = INT_MAX;
  206. for(Int i = 0; i < freeRectangles.elms(); ++i)
  207. {
  208. // Try to place the rectangle in upright (non-flipped) orientation.
  209. if (freeRectangles[i].width >= width && freeRectangles[i].height >= height)
  210. {
  211. int leftoverHoriz = abs(freeRectangles[i].width - width);
  212. int leftoverVert = abs(freeRectangles[i].height - height);
  213. int shortSideFit = Min(leftoverHoriz, leftoverVert);
  214. int longSideFit = Max(leftoverHoriz, leftoverVert);
  215. if (longSideFit < bestLongSideFit || (longSideFit == bestLongSideFit && shortSideFit < bestShortSideFit))
  216. {
  217. bestNode.x = freeRectangles[i].x;
  218. bestNode.y = freeRectangles[i].y;
  219. bestNode.width = width;
  220. bestNode.height = height;
  221. bestShortSideFit = shortSideFit;
  222. bestLongSideFit = longSideFit;
  223. }
  224. }
  225. if (allowRotate && freeRectangles[i].width >= height && freeRectangles[i].height >= width)
  226. {
  227. int leftoverHoriz = abs(freeRectangles[i].width - height);
  228. int leftoverVert = abs(freeRectangles[i].height - width);
  229. int shortSideFit = Min(leftoverHoriz, leftoverVert);
  230. int longSideFit = Max(leftoverHoriz, leftoverVert);
  231. if (longSideFit < bestLongSideFit || (longSideFit == bestLongSideFit && shortSideFit < bestShortSideFit))
  232. {
  233. bestNode.x = freeRectangles[i].x;
  234. bestNode.y = freeRectangles[i].y;
  235. bestNode.width = height;
  236. bestNode.height = width;
  237. bestShortSideFit = shortSideFit;
  238. bestLongSideFit = longSideFit;
  239. }
  240. }
  241. }
  242. return bestNode;
  243. }
  244. Rect MaxRectsBinPack::FindPositionForNewNodeBestAreaFit(int width, int height,
  245. int &bestAreaFit, int &bestShortSideFit) const
  246. {
  247. Rect bestNode;
  248. memset(&bestNode, 0, sizeof(Rect));
  249. bestAreaFit = INT_MAX;
  250. for(Int i = 0; i < freeRectangles.elms(); ++i)
  251. {
  252. int areaFit = freeRectangles[i].width * freeRectangles[i].height - width * height;
  253. // Try to place the rectangle in upright (non-flipped) orientation.
  254. if (freeRectangles[i].width >= width && freeRectangles[i].height >= height)
  255. {
  256. int leftoverHoriz = abs(freeRectangles[i].width - width);
  257. int leftoverVert = abs(freeRectangles[i].height - height);
  258. int shortSideFit = Min(leftoverHoriz, leftoverVert);
  259. if (areaFit < bestAreaFit || (areaFit == bestAreaFit && shortSideFit < bestShortSideFit))
  260. {
  261. bestNode.x = freeRectangles[i].x;
  262. bestNode.y = freeRectangles[i].y;
  263. bestNode.width = width;
  264. bestNode.height = height;
  265. bestShortSideFit = shortSideFit;
  266. bestAreaFit = areaFit;
  267. }
  268. }
  269. if (allowRotate && freeRectangles[i].width >= height && freeRectangles[i].height >= width)
  270. {
  271. int leftoverHoriz = abs(freeRectangles[i].width - height);
  272. int leftoverVert = abs(freeRectangles[i].height - width);
  273. int shortSideFit = Min(leftoverHoriz, leftoverVert);
  274. if (areaFit < bestAreaFit || (areaFit == bestAreaFit && shortSideFit < bestShortSideFit))
  275. {
  276. bestNode.x = freeRectangles[i].x;
  277. bestNode.y = freeRectangles[i].y;
  278. bestNode.width = height;
  279. bestNode.height = width;
  280. bestShortSideFit = shortSideFit;
  281. bestAreaFit = areaFit;
  282. }
  283. }
  284. }
  285. return bestNode;
  286. }
  287. /// Returns 0 if the two intervals i1 and i2 are disjoint, or the length of their overlap otherwise.
  288. int CommonIntervalLength(int i1start, int i1end, int i2start, int i2end)
  289. {
  290. if (i1end < i2start || i2end < i1start)
  291. return 0;
  292. return Min(i1end, i2end) - Max(i1start, i2start);
  293. }
  294. int MaxRectsBinPack::ContactPointScoreNode(int x, int y, int width, int height) const
  295. {
  296. int score = 0;
  297. if (x == 0 || x + width == binWidth)
  298. score += height;
  299. if (y == 0 || y + height == binHeight)
  300. score += width;
  301. for(Int i = 0; i < usedRectangles.elms(); ++i)
  302. {
  303. if (usedRectangles[i].x == x + width || usedRectangles[i].x + usedRectangles[i].width == x)
  304. score += CommonIntervalLength(usedRectangles[i].y, usedRectangles[i].y + usedRectangles[i].height, y, y + height);
  305. if (usedRectangles[i].y == y + height || usedRectangles[i].y + usedRectangles[i].height == y)
  306. score += CommonIntervalLength(usedRectangles[i].x, usedRectangles[i].x + usedRectangles[i].width, x, x + width);
  307. }
  308. return score;
  309. }
  310. Rect MaxRectsBinPack::FindPositionForNewNodeContactPoint(int width, int height, int &bestContactScore) const
  311. {
  312. Rect bestNode;
  313. memset(&bestNode, 0, sizeof(Rect));
  314. bestContactScore = -1;
  315. for(Int i = 0; i < freeRectangles.elms(); ++i)
  316. {
  317. // Try to place the rectangle in upright (non-flipped) orientation.
  318. if (freeRectangles[i].width >= width && freeRectangles[i].height >= height)
  319. {
  320. int score = ContactPointScoreNode(freeRectangles[i].x, freeRectangles[i].y, width, height);
  321. if (score > bestContactScore)
  322. {
  323. bestNode.x = freeRectangles[i].x;
  324. bestNode.y = freeRectangles[i].y;
  325. bestNode.width = width;
  326. bestNode.height = height;
  327. bestContactScore = score;
  328. }
  329. }
  330. if (allowRotate && freeRectangles[i].width >= height && freeRectangles[i].height >= width)
  331. {
  332. int score = ContactPointScoreNode(freeRectangles[i].x, freeRectangles[i].y, height, width);
  333. if (score > bestContactScore)
  334. {
  335. bestNode.x = freeRectangles[i].x;
  336. bestNode.y = freeRectangles[i].y;
  337. bestNode.width = height;
  338. bestNode.height = width;
  339. bestContactScore = score;
  340. }
  341. }
  342. }
  343. return bestNode;
  344. }
  345. bool MaxRectsBinPack::SplitFreeNode(Rect freeNode, const Rect &usedNode)
  346. {
  347. // Test with SAT if the rectangles even intersect.
  348. if (usedNode.x >= freeNode.x + freeNode.width || usedNode.x + usedNode.width <= freeNode.x ||
  349. usedNode.y >= freeNode.y + freeNode.height || usedNode.y + usedNode.height <= freeNode.y)
  350. return false;
  351. if (usedNode.x < freeNode.x + freeNode.width && usedNode.x + usedNode.width > freeNode.x)
  352. {
  353. // New node at the top side of the used node.
  354. if (usedNode.y > freeNode.y && usedNode.y < freeNode.y + freeNode.height)
  355. {
  356. Rect newNode = freeNode;
  357. newNode.height = usedNode.y - newNode.y;
  358. freeRectangles.add(newNode);
  359. }
  360. // New node at the bottom side of the used node.
  361. if (usedNode.y + usedNode.height < freeNode.y + freeNode.height)
  362. {
  363. Rect newNode = freeNode;
  364. newNode.y = usedNode.y + usedNode.height;
  365. newNode.height = freeNode.y + freeNode.height - (usedNode.y + usedNode.height);
  366. freeRectangles.add(newNode);
  367. }
  368. }
  369. if (usedNode.y < freeNode.y + freeNode.height && usedNode.y + usedNode.height > freeNode.y)
  370. {
  371. // New node at the left side of the used node.
  372. if (usedNode.x > freeNode.x && usedNode.x < freeNode.x + freeNode.width)
  373. {
  374. Rect newNode = freeNode;
  375. newNode.width = usedNode.x - newNode.x;
  376. freeRectangles.add(newNode);
  377. }
  378. // New node at the right side of the used node.
  379. if (usedNode.x + usedNode.width < freeNode.x + freeNode.width)
  380. {
  381. Rect newNode = freeNode;
  382. newNode.x = usedNode.x + usedNode.width;
  383. newNode.width = freeNode.x + freeNode.width - (usedNode.x + usedNode.width);
  384. freeRectangles.add(newNode);
  385. }
  386. }
  387. return true;
  388. }
  389. void MaxRectsBinPack::SplitFreeRects(const Rect &usedNode)
  390. {
  391. REPA(freeRectangles)
  392. if(SplitFreeNode(freeRectangles[i], usedNode))
  393. freeRectangles.remove(i);
  394. }
  395. void MaxRectsBinPack::PruneFreeList()
  396. {
  397. /*
  398. /// Would be nice to do something like this, to avoid a Theta(n^2) loop through each pair.
  399. /// But unfortunately it doesn't quite cut it, since we also want to detect containment.
  400. /// Perhaps there's another way to do this faster than Theta(n^2).
  401. if (freeRectangles.elms() > 0)
  402. clb::sort::QuickSort(&freeRectangles[0], freeRectangles.elms(), NodeSortCmp);
  403. for(Int i = 0; i < freeRectangles.elms()-1; ++i)
  404. if (freeRectangles[i].x == freeRectangles[i+1].x &&
  405. freeRectangles[i].y == freeRectangles[i+1].y &&
  406. freeRectangles[i].width == freeRectangles[i+1].width &&
  407. freeRectangles[i].height == freeRectangles[i+1].height)
  408. {
  409. freeRectangles.erase(freeRectangles.begin() + i);
  410. --i;
  411. }
  412. */
  413. /// Go through each pair and remove any rectangle that is redundant.
  414. for(Int i = 0; i < freeRectangles.elms(); ++i)
  415. for(Int j = i+1; j < freeRectangles.elms(); ++j)
  416. {
  417. if (IsContainedIn(freeRectangles[i], freeRectangles[j]))
  418. {
  419. freeRectangles.remove(i);
  420. --i;
  421. break;
  422. }
  423. if (IsContainedIn(freeRectangles[j], freeRectangles[i]))
  424. {
  425. freeRectangles.remove(j);
  426. --j;
  427. }
  428. }
  429. }