65#ifndef STB_INCLUDE_STB_RECT_PACK_H
66#define STB_INCLUDE_STB_RECT_PACK_H
68#define STB_RECT_PACK_VERSION 1
71#define STBRP_DEF static
73#define STBRP_DEF extern
84#ifdef STBRP_LARGE_RECTS
205#ifdef STB_RECT_PACK_IMPLEMENTATION
208#define STBRP_SORT qsort
213#define STBRP_ASSERT assert
218#define STBRP__NOTUSED(v) (void)(v)
219#define STBRP__CDECL __cdecl
221#define STBRP__NOTUSED(v) (void)sizeof(v)
227 STBRP__INIT_skyline = 1
233 case STBRP__INIT_skyline:
244 if (allow_out_of_mem)
265#ifndef STBRP_LARGE_RECTS
266 STBRP_ASSERT(width <= 0xffff && height <= 0xffff);
269 for (i=0; i < num_nodes-1; ++i)
270 nodes[i].next = &nodes[i+1];
271 nodes[i].
next = NULL;
272 context->
init_mode = STBRP__INIT_skyline;
276 context->
width = width;
286#ifdef STBRP_LARGE_RECTS
287 context->
extra[1].
y = (1<<30);
289 context->
extra[1].
y = 65535;
299 int min_y, visited_width, waste_area;
303 STBRP_ASSERT(first->
x <= x0);
307 while (node->
next->
x <= x0)
310 STBRP_ASSERT(node->
next->
x > x0);
313 STBRP_ASSERT(node->
x <= x0);
318 while (node->
x < x1) {
319 if (node->
y > min_y) {
323 waste_area += visited_width * (node->
y - min_y);
327 visited_width += node->
next->
x - x0;
329 visited_width += node->
next->
x - node->
x;
332 int under_width = node->
next->
x - node->
x;
333 if (under_width + visited_width > width)
334 under_width = width - visited_width;
335 waste_area += under_width * (min_y - node->
y);
336 visited_width += under_width;
341 *pwaste = waste_area;
351static stbrp__findresult stbrp__skyline_find_best_pos(
stbrp_context *c,
int width,
int height)
353 int best_waste = (1<<30), best_x, best_y = (1 << 30);
354 stbrp__findresult fr;
355 stbrp_node **prev, *node, *tail, **best = NULL;
358 width = (width + c->
align - 1);
359 width -= width % c->
align;
360 STBRP_ASSERT(width % c->
align == 0);
371 while (node->
x + width <= c->width) {
373 y = stbrp__skyline_find_min_y(c, node, node->
x, width, &waste);
382 if (y + height <= c->height) {
384 if (y < best_y || (y == best_y && waste < best_waste)) {
395 best_x = (best == NULL) ? 0 : (*best)->
x;
415 tail = c->active_head;
416 node = c->active_head;
417 prev = &c->active_head;
419 while (tail->x < width)
422 int xpos = tail->x - width;
424 STBRP_ASSERT(xpos >= 0);
426 while (node->next->x <= xpos) {
430 STBRP_ASSERT(node->next->x > xpos && node->x <= xpos);
431 y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste);
432 if (y + height <= c->height) {
434 if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
436 STBRP_ASSERT(y <= best_y);
453static stbrp__findresult stbrp__skyline_pack_rectangle(
stbrp_context *context,
int width,
int height)
456 stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height);
463 if (res.prev_link == NULL || res.y + height > context->
height || context->
free_head == NULL) {
464 res.prev_link = NULL;
479 cur = *res.prev_link;
480 if (cur->
x < res.x) {
486 *res.prev_link = node;
491 while (cur->
next && cur->
next->
x <= res.x + width) {
502 if (cur->
x < res.x + width)
507 while (cur->
x < context->
width) {
508 STBRP_ASSERT(cur->
x < cur->
next->
x);
511 STBRP_ASSERT(cur->
next == NULL);
525 STBRP_ASSERT(count == context->
num_nodes+2);
533static int STBRP__CDECL rect_height_compare(
const void *a,
const void *b)
541 return (p->
w > q->
w) ? -1 : (p->
w < q->
w);
545static int STBRP__CDECL rect_original_order(
const void *a,
const void *b)
552#ifdef STBRP_LARGE_RECTS
553#define STBRP__MAXVAL 0xffffffff
555#define STBRP__MAXVAL 0xffff
560 int i, all_rects_packed = 1;
563 for (i=0; i < num_rects; ++i) {
568 STBRP_SORT(rects, num_rects,
sizeof(rects[0]), rect_height_compare);
570 for (i=0; i < num_rects; ++i) {
571 if (rects[i].w == 0 || rects[i].h == 0) {
572 rects[i].
x = rects[i].
y = 0;
574 stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
579 rects[i].
x = rects[i].
y = STBRP__MAXVAL;
585 STBRP_SORT(rects, num_rects,
sizeof(rects[0]), rect_original_order);
588 for (i=0; i < num_rects; ++i) {
589 rects[i].
was_packed = !(rects[i].
x == STBRP__MAXVAL && rects[i].
y == STBRP__MAXVAL);
590 if (!rects[i].was_packed)
591 all_rects_packed = 0;
595 return all_rects_packed;
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)
STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes)
STBRP_DEF int stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects)
x y t t *t x y t t t x y t t t x *y t *t t x *y t *t t x y t t t x y t t t x(y+z)
#define STBRP_DEF
Definition stb_rect_pack.h:64
@ STBRP_HEURISTIC_Skyline_BF_sortHeight
Definition stb_rect_pack.h:157
@ STBRP_HEURISTIC_Skyline_BL_sortHeight
Definition stb_rect_pack.h:156
@ STBRP_HEURISTIC_Skyline_default
Definition stb_rect_pack.h:155
unsigned short stbrp_coord
Definition stb_rect_pack.h:78
struct stbrp_node stbrp_node
Definition stb_rect_pack.h:72
Definition stb_rect_pack.h:173
int init_mode
Definition stb_rect_pack.h:177
int heuristic
Definition stb_rect_pack.h:178
int width
Definition stb_rect_pack.h:174
stbrp_node * free_head
Definition stb_rect_pack.h:181
stbrp_node * active_head
Definition stb_rect_pack.h:180
stbrp_node extra[2]
Definition stb_rect_pack.h:182
int align
Definition stb_rect_pack.h:176
int height
Definition stb_rect_pack.h:175
int num_nodes
Definition stb_rect_pack.h:179
Definition stb_rect_pack.h:167
stbrp_node * next
Definition stb_rect_pack.h:169
stbrp_coord x
Definition stb_rect_pack.h:168
stbrp_coord y
Definition stb_rect_pack.h:168
Definition stb_rect_pack.h:107
stbrp_coord w
Definition stb_rect_pack.h:112
stbrp_coord x
Definition stb_rect_pack.h:115
int was_packed
Definition stb_rect_pack.h:116
stbrp_coord y
Definition stb_rect_pack.h:115
stbrp_coord h
Definition stb_rect_pack.h:112