stb_rect_pack.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. #include "stb_rect_pack.h"
  2. #define STB_RECT_PACK_IMPLEMENTATION
  3. //////////////////////////////////////////////////////////////////////////////
  4. //
  5. // IMPLEMENTATION SECTION
  6. //
  7. #ifdef STB_RECT_PACK_IMPLEMENTATION
  8. #ifndef STBRP_SORT
  9. #include <stdlib.h>
  10. #define STBRP_SORT qsort
  11. #endif
  12. #ifndef STBRP_ASSERT
  13. #include <assert.h>
  14. #define STBRP_ASSERT assert
  15. #endif
  16. #ifdef _MSC_VER
  17. #define STBRP__NOTUSED(v) (void)(v)
  18. #else
  19. #define STBRP__NOTUSED(v) (void)sizeof(v)
  20. #endif
  21. enum
  22. {
  23. STBRP__INIT_skyline = 1
  24. };
  25. STBRP_DEF void stbrp_setup_heuristic(stbrp_context *context, int heuristic)
  26. {
  27. switch (context->init_mode) {
  28. case STBRP__INIT_skyline:
  29. STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight);
  30. context->heuristic = heuristic;
  31. break;
  32. default:
  33. STBRP_ASSERT(0);
  34. }
  35. }
  36. STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context *context, int allow_out_of_mem)
  37. {
  38. if (allow_out_of_mem)
  39. // if it's ok to run out of memory, then don't bother aligning them;
  40. // this gives better packing, but may fail due to OOM (even though
  41. // the rectangles easily fit). @TODO a smarter approach would be to only
  42. // quantize once we've hit OOM, then we could get rid of this parameter.
  43. context->align = 1;
  44. else {
  45. // if it's not ok to run out of memory, then quantize the widths
  46. // so that num_nodes is always enough nodes.
  47. //
  48. // I.e. num_nodes * align >= width
  49. // align >= width / num_nodes
  50. // align = ceil(width/num_nodes)
  51. context->align = (context->width + context->num_nodes-1) / context->num_nodes;
  52. }
  53. }
  54. STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes)
  55. {
  56. int i;
  57. #ifndef STBRP_LARGE_RECTS
  58. STBRP_ASSERT(width <= 0xffff && height <= 0xffff);
  59. #endif
  60. for (i=0; i < num_nodes-1; ++i)
  61. nodes[i].next = &nodes[i+1];
  62. nodes[i].next = NULL;
  63. context->init_mode = STBRP__INIT_skyline;
  64. context->heuristic = STBRP_HEURISTIC_Skyline_default;
  65. context->free_head = &nodes[0];
  66. context->active_head = &context->extra[0];
  67. context->width = width;
  68. context->height = height;
  69. context->num_nodes = num_nodes;
  70. stbrp_setup_allow_out_of_mem(context, 0);
  71. // node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly)
  72. context->extra[0].x = 0;
  73. context->extra[0].y = 0;
  74. context->extra[0].next = &context->extra[1];
  75. context->extra[1].x = (stbrp_coord) width;
  76. #ifdef STBRP_LARGE_RECTS
  77. context->extra[1].y = (1<<30);
  78. #else
  79. context->extra[1].y = 65535;
  80. #endif
  81. context->extra[1].next = NULL;
  82. }
  83. // find minimum y position if it starts at x1
  84. static int stbrp__skyline_find_min_y(stbrp_context *c, stbrp_node *first, int x0, int width, int *pwaste)
  85. {
  86. stbrp_node *node = first;
  87. int x1 = x0 + width;
  88. int min_y, visited_width, waste_area;
  89. STBRP__NOTUSED(c);
  90. STBRP_ASSERT(first->x <= x0);
  91. #if 0
  92. // skip in case we're past the node
  93. while (node->next->x <= x0)
  94. ++node;
  95. #else
  96. STBRP_ASSERT(node->next->x > x0); // we ended up handling this in the caller for efficiency
  97. #endif
  98. STBRP_ASSERT(node->x <= x0);
  99. min_y = 0;
  100. waste_area = 0;
  101. visited_width = 0;
  102. while (node->x < x1) {
  103. if (node->y > min_y) {
  104. // raise min_y higher.
  105. // we've accounted for all waste up to min_y,
  106. // but we'll now add more waste for everything we've visted
  107. waste_area += visited_width * (node->y - min_y);
  108. min_y = node->y;
  109. // the first time through, visited_width might be reduced
  110. if (node->x < x0)
  111. visited_width += node->next->x - x0;
  112. else
  113. visited_width += node->next->x - node->x;
  114. } else {
  115. // add waste area
  116. int under_width = node->next->x - node->x;
  117. if (under_width + visited_width > width)
  118. under_width = width - visited_width;
  119. waste_area += under_width * (min_y - node->y);
  120. visited_width += under_width;
  121. }
  122. node = node->next;
  123. }
  124. *pwaste = waste_area;
  125. return min_y;
  126. }
  127. typedef struct
  128. {
  129. int x,y;
  130. stbrp_node **prev_link;
  131. } stbrp__findresult;
  132. static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height)
  133. {
  134. int best_waste = (1<<30), best_x, best_y = (1 << 30);
  135. stbrp__findresult fr;
  136. stbrp_node **prev, *node, *tail, **best = NULL;
  137. // align to multiple of c->align
  138. width = (width + c->align - 1);
  139. width -= width % c->align;
  140. STBRP_ASSERT(width % c->align == 0);
  141. node = c->active_head;
  142. prev = &c->active_head;
  143. while (node->x + width <= c->width) {
  144. int y,waste;
  145. y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste);
  146. if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { // actually just want to test BL
  147. // bottom left
  148. if (y < best_y) {
  149. best_y = y;
  150. best = prev;
  151. }
  152. } else {
  153. // best-fit
  154. if (y + height <= c->height) {
  155. // can only use it if it first vertically
  156. if (y < best_y || (y == best_y && waste < best_waste)) {
  157. best_y = y;
  158. best_waste = waste;
  159. best = prev;
  160. }
  161. }
  162. }
  163. prev = &node->next;
  164. node = node->next;
  165. }
  166. best_x = (best == NULL) ? 0 : (*best)->x;
  167. // if doing best-fit (BF), we also have to try aligning right edge to each node position
  168. //
  169. // e.g, if fitting
  170. //
  171. // ____________________
  172. // |____________________|
  173. //
  174. // into
  175. //
  176. // | |
  177. // | ____________|
  178. // |____________|
  179. //
  180. // then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned
  181. //
  182. // This makes BF take about 2x the time
  183. if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) {
  184. tail = c->active_head;
  185. node = c->active_head;
  186. prev = &c->active_head;
  187. // find first node that's admissible
  188. while (tail->x < width)
  189. tail = tail->next;
  190. while (tail) {
  191. int xpos = tail->x - width;
  192. int y,waste;
  193. STBRP_ASSERT(xpos >= 0);
  194. // find the left position that matches this
  195. while (node->next->x <= xpos) {
  196. prev = &node->next;
  197. node = node->next;
  198. }
  199. STBRP_ASSERT(node->next->x > xpos && node->x <= xpos);
  200. y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste);
  201. if (y + height < c->height) {
  202. if (y <= best_y) {
  203. if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
  204. best_x = xpos;
  205. STBRP_ASSERT(y <= best_y);
  206. best_y = y;
  207. best_waste = waste;
  208. best = prev;
  209. }
  210. }
  211. }
  212. tail = tail->next;
  213. }
  214. }
  215. fr.prev_link = best;
  216. fr.x = best_x;
  217. fr.y = best_y;
  218. return fr;
  219. }
  220. static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height)
  221. {
  222. // find best position according to heuristic
  223. stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height);
  224. stbrp_node *node, *cur;
  225. // bail if:
  226. // 1. it failed
  227. // 2. the best node doesn't fit (we don't always check this)
  228. // 3. we're out of memory
  229. if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) {
  230. res.prev_link = NULL;
  231. return res;
  232. }
  233. // on success, create new node
  234. node = context->free_head;
  235. node->x = (stbrp_coord) res.x;
  236. node->y = (stbrp_coord) (res.y + height);
  237. context->free_head = node->next;
  238. // insert the new node into the right starting point, and
  239. // let 'cur' point to the remaining nodes needing to be
  240. // stiched back in
  241. cur = *res.prev_link;
  242. if (cur->x < res.x) {
  243. // preserve the existing one, so start testing with the next one
  244. stbrp_node *next = cur->next;
  245. cur->next = node;
  246. cur = next;
  247. } else {
  248. *res.prev_link = node;
  249. }
  250. // from here, traverse cur and free the nodes, until we get to one
  251. // that shouldn't be freed
  252. while (cur->next && cur->next->x <= res.x + width) {
  253. stbrp_node *next = cur->next;
  254. // move the current node to the free list
  255. cur->next = context->free_head;
  256. context->free_head = cur;
  257. cur = next;
  258. }
  259. // stitch the list back in
  260. node->next = cur;
  261. if (cur->x < res.x + width)
  262. cur->x = (stbrp_coord) (res.x + width);
  263. #ifdef _DEBUG
  264. cur = context->active_head;
  265. while (cur->x < context->width) {
  266. STBRP_ASSERT(cur->x < cur->next->x);
  267. cur = cur->next;
  268. }
  269. STBRP_ASSERT(cur->next == NULL);
  270. {
  271. stbrp_node *L1 = NULL, *L2 = NULL;
  272. int count=0;
  273. cur = context->active_head;
  274. while (cur) {
  275. L1 = cur;
  276. cur = cur->next;
  277. ++count;
  278. }
  279. cur = context->free_head;
  280. while (cur) {
  281. L2 = cur;
  282. cur = cur->next;
  283. ++count;
  284. }
  285. STBRP_ASSERT(count == context->num_nodes+2);
  286. }
  287. #endif
  288. return res;
  289. }
  290. static int rect_height_compare(const void *a, const void *b)
  291. {
  292. const stbrp_rect *p = (const stbrp_rect *) a;
  293. const stbrp_rect *q = (const stbrp_rect *) b;
  294. if (p->h > q->h)
  295. return -1;
  296. if (p->h < q->h)
  297. return 1;
  298. return (p->w > q->w) ? -1 : (p->w < q->w);
  299. }
  300. static int rect_width_compare(const void *a, const void *b)
  301. {
  302. const stbrp_rect *p = (const stbrp_rect *) a;
  303. const stbrp_rect *q = (const stbrp_rect *) b;
  304. if (p->w > q->w)
  305. return -1;
  306. if (p->w < q->w)
  307. return 1;
  308. return (p->h > q->h) ? -1 : (p->h < q->h);
  309. }
  310. static int rect_original_order(const void *a, const void *b)
  311. {
  312. const stbrp_rect *p = (const stbrp_rect *) a;
  313. const stbrp_rect *q = (const stbrp_rect *) b;
  314. return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
  315. }
  316. #ifdef STBRP_LARGE_RECTS
  317. #define STBRP__MAXVAL 0xffffffff
  318. #else
  319. #define STBRP__MAXVAL 0xffff
  320. #endif
  321. STBRP_DEF int stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects)
  322. {
  323. int i, all_rects_packed = 1;
  324. // we use the 'was_packed' field internally to allow sorting/unsorting
  325. for (i=0; i < num_rects; ++i) {
  326. rects[i].was_packed = i;
  327. #ifndef STBRP_LARGE_RECTS
  328. STBRP_ASSERT(rects[i].w <= 0xffff && rects[i].h <= 0xffff);
  329. #endif
  330. }
  331. // sort according to heuristic
  332. STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare);
  333. for (i=0; i < num_rects; ++i) {
  334. if (rects[i].w == 0 || rects[i].h == 0) {
  335. rects[i].x = rects[i].y = 0; // empty rect needs no space
  336. } else {
  337. stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
  338. if (fr.prev_link) {
  339. rects[i].x = (stbrp_coord) fr.x;
  340. rects[i].y = (stbrp_coord) fr.y;
  341. } else {
  342. rects[i].x = rects[i].y = STBRP__MAXVAL;
  343. }
  344. }
  345. }
  346. // unsort
  347. STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order);
  348. // set was_packed flags and all_rects_packed status
  349. for (i=0; i < num_rects; ++i) {
  350. rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL);
  351. if (!rects[i].was_packed)
  352. all_rects_packed = 0;
  353. }
  354. // return the all_rects_packed status
  355. return all_rects_packed;
  356. }
  357. #endif