123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640 |
- #ifndef STB_INCLUDE_STB_RECT_PACK_H
- #define STB_INCLUDE_STB_RECT_PACK_H
- #define STB_RECT_PACK_VERSION 1
- #ifdef STBRP_STATIC
- #define STBRP_DEF static
- #else
- #define STBRP_DEF extern
- #endif
- #ifdef __cplusplus
- extern "C" {
- #endif
- typedef struct stbrp_context stbrp_context;
- typedef struct stbrp_node stbrp_node;
- typedef struct stbrp_rect stbrp_rect;
- #ifdef STBRP_LARGE_RECTS
- typedef int stbrp_coord;
- #else
- typedef unsigned short stbrp_coord;
- #endif
- STBRP_DEF int stbrp_pack_rects (stbrp_context *context, stbrp_rect *rects, int num_rects);
- struct stbrp_rect
- {
-
- int id;
-
- stbrp_coord w, h;
-
- stbrp_coord x, y;
- int was_packed;
- };
- STBRP_DEF void stbrp_init_target (stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes);
- STBRP_DEF void stbrp_setup_allow_out_of_mem (stbrp_context *context, int allow_out_of_mem);
- STBRP_DEF void stbrp_setup_heuristic (stbrp_context *context, int heuristic);
- enum
- {
- STBRP_HEURISTIC_Skyline_default=0,
- STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default,
- STBRP_HEURISTIC_Skyline_BF_sortHeight
- };
- struct stbrp_node
- {
- stbrp_coord x,y;
- stbrp_node *next;
- };
- struct stbrp_context
- {
- int width;
- int height;
- int align;
- int init_mode;
- int heuristic;
- int num_nodes;
- stbrp_node *active_head;
- stbrp_node *free_head;
- stbrp_node extra[2];
- };
- #ifdef __cplusplus
- }
- #endif
- #endif
- #ifdef STB_RECT_PACK_IMPLEMENTATION
- #ifndef STBRP_SORT
- #include <stdlib.h>
- #define STBRP_SORT qsort
- #endif
- #ifndef STBRP_ASSERT
- #include <assert.h>
- #define STBRP_ASSERT assert
- #endif
- #ifdef _MSC_VER
- #define STBRP__NOTUSED(v) (void)(v)
- #define STBRP__CDECL __cdecl
- #else
- #define STBRP__NOTUSED(v) (void)sizeof(v)
- #define STBRP__CDECL
- #endif
- enum
- {
- STBRP__INIT_skyline = 1
- };
- STBRP_DEF void stbrp_setup_heuristic(stbrp_context *context, int heuristic)
- {
- switch (context->init_mode) {
- case STBRP__INIT_skyline:
- STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight);
- context->heuristic = heuristic;
- break;
- default:
- STBRP_ASSERT(0);
- }
- }
- STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context *context, int allow_out_of_mem)
- {
- if (allow_out_of_mem)
-
-
-
-
- context->align = 1;
- else {
-
-
-
-
-
-
- context->align = (context->width + context->num_nodes-1) / context->num_nodes;
- }
- }
- STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes)
- {
- int i;
- #ifndef STBRP_LARGE_RECTS
- STBRP_ASSERT(width <= 0xffff && height <= 0xffff);
- #endif
- for (i=0; i < num_nodes-1; ++i)
- nodes[i].next = &nodes[i+1];
- nodes[i].next = NULL;
- context->init_mode = STBRP__INIT_skyline;
- context->heuristic = STBRP_HEURISTIC_Skyline_default;
- context->free_head = &nodes[0];
- context->active_head = &context->extra[0];
- context->width = width;
- context->height = height;
- context->num_nodes = num_nodes;
- stbrp_setup_allow_out_of_mem(context, 0);
-
- context->extra[0].x = 0;
- context->extra[0].y = 0;
- context->extra[0].next = &context->extra[1];
- context->extra[1].x = (stbrp_coord) width;
- #ifdef STBRP_LARGE_RECTS
- context->extra[1].y = (1<<30);
- #else
- context->extra[1].y = 65535;
- #endif
- context->extra[1].next = NULL;
- }
- static int stbrp__skyline_find_min_y(stbrp_context *c, stbrp_node *first, int x0, int width, int *pwaste)
- {
- stbrp_node *node = first;
- int x1 = x0 + width;
- int min_y, visited_width, waste_area;
- STBRP__NOTUSED(c);
- STBRP_ASSERT(first->x <= x0);
- #if 0
-
- while (node->next->x <= x0)
- ++node;
- #else
- STBRP_ASSERT(node->next->x > x0);
- #endif
- STBRP_ASSERT(node->x <= x0);
- min_y = 0;
- waste_area = 0;
- visited_width = 0;
- while (node->x < x1) {
- if (node->y > min_y) {
-
-
-
- waste_area += visited_width * (node->y - min_y);
- min_y = node->y;
-
- if (node->x < x0)
- visited_width += node->next->x - x0;
- else
- visited_width += node->next->x - node->x;
- } else {
-
- int under_width = node->next->x - node->x;
- if (under_width + visited_width > width)
- under_width = width - visited_width;
- waste_area += under_width * (min_y - node->y);
- visited_width += under_width;
- }
- node = node->next;
- }
- *pwaste = waste_area;
- return min_y;
- }
- typedef struct
- {
- int x,y;
- stbrp_node **prev_link;
- } stbrp__findresult;
- static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height)
- {
- int best_waste = (1<<30), best_x, best_y = (1 << 30);
- stbrp__findresult fr;
- stbrp_node **prev, *node, *tail, **best = NULL;
-
- width = (width + c->align - 1);
- width -= width % c->align;
- STBRP_ASSERT(width % c->align == 0);
-
- if (width > c->width || height > c->height) {
- fr.prev_link = NULL;
- fr.x = fr.y = 0;
- return fr;
- }
- node = c->active_head;
- prev = &c->active_head;
- while (node->x + width <= c->width) {
- int y,waste;
- y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste);
- if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) {
-
- if (y < best_y) {
- best_y = y;
- best = prev;
- }
- } else {
-
- if (y + height <= c->height) {
-
- if (y < best_y || (y == best_y && waste < best_waste)) {
- best_y = y;
- best_waste = waste;
- best = prev;
- }
- }
- }
- prev = &node->next;
- node = node->next;
- }
- best_x = (best == NULL) ? 0 : (*best)->x;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) {
- tail = c->active_head;
- node = c->active_head;
- prev = &c->active_head;
-
- while (tail->x < width)
- tail = tail->next;
- while (tail) {
- int xpos = tail->x - width;
- int y,waste;
- STBRP_ASSERT(xpos >= 0);
-
- while (node->next->x <= xpos) {
- prev = &node->next;
- node = node->next;
- }
- STBRP_ASSERT(node->next->x > xpos && node->x <= xpos);
- y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste);
- if (y + height <= c->height) {
- if (y <= best_y) {
- if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
- best_x = xpos;
- STBRP_ASSERT(y <= best_y);
- best_y = y;
- best_waste = waste;
- best = prev;
- }
- }
- }
- tail = tail->next;
- }
- }
- fr.prev_link = best;
- fr.x = best_x;
- fr.y = best_y;
- return fr;
- }
- static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height)
- {
-
- stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height);
- stbrp_node *node, *cur;
-
-
-
-
- if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) {
- res.prev_link = NULL;
- return res;
- }
-
- node = context->free_head;
- node->x = (stbrp_coord) res.x;
- node->y = (stbrp_coord) (res.y + height);
- context->free_head = node->next;
-
-
-
- cur = *res.prev_link;
- if (cur->x < res.x) {
-
- stbrp_node *next = cur->next;
- cur->next = node;
- cur = next;
- } else {
- *res.prev_link = node;
- }
-
-
- while (cur->next && cur->next->x <= res.x + width) {
- stbrp_node *next = cur->next;
-
- cur->next = context->free_head;
- context->free_head = cur;
- cur = next;
- }
-
- node->next = cur;
- if (cur->x < res.x + width)
- cur->x = (stbrp_coord) (res.x + width);
- #ifdef _DEBUG
- cur = context->active_head;
- while (cur->x < context->width) {
- STBRP_ASSERT(cur->x < cur->next->x);
- cur = cur->next;
- }
- STBRP_ASSERT(cur->next == NULL);
- {
- int count=0;
- cur = context->active_head;
- while (cur) {
- cur = cur->next;
- ++count;
- }
- cur = context->free_head;
- while (cur) {
- cur = cur->next;
- ++count;
- }
- STBRP_ASSERT(count == context->num_nodes+2);
- }
- #endif
- return res;
- }
- static int STBRP__CDECL rect_height_compare(const void *a, const void *b)
- {
- const stbrp_rect *p = (const stbrp_rect *) a;
- const stbrp_rect *q = (const stbrp_rect *) b;
- if (p->h > q->h)
- return -1;
- if (p->h < q->h)
- return 1;
- return (p->w > q->w) ? -1 : (p->w < q->w);
- }
- static int STBRP__CDECL rect_original_order(const void *a, const void *b)
- {
- const stbrp_rect *p = (const stbrp_rect *) a;
- const stbrp_rect *q = (const stbrp_rect *) b;
- return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
- }
- #ifdef STBRP_LARGE_RECTS
- #define STBRP__MAXVAL 0xffffffff
- #else
- #define STBRP__MAXVAL 0xffff
- #endif
- STBRP_DEF int stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects)
- {
- int i, all_rects_packed = 1;
-
- for (i=0; i < num_rects; ++i) {
- rects[i].was_packed = i;
- }
-
- STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare);
- for (i=0; i < num_rects; ++i) {
- if (rects[i].w == 0 || rects[i].h == 0) {
- rects[i].x = rects[i].y = 0;
- } else {
- stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
- if (fr.prev_link) {
- rects[i].x = (stbrp_coord) fr.x;
- rects[i].y = (stbrp_coord) fr.y;
- } else {
- rects[i].x = rects[i].y = STBRP__MAXVAL;
- }
- }
- }
-
- STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order);
-
- for (i=0; i < num_rects; ++i) {
- rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL);
- if (!rects[i].was_packed)
- all_rects_packed = 0;
- }
-
- return all_rects_packed;
- }
- #endif
|