56#ifndef STB_INCLUDE_STB_RECT_PACK_H
57#define STB_INCLUDE_STB_RECT_PACK_H
59#define STB_RECT_PACK_VERSION 1
62#define STBRP_DEF static
64#define STBRP_DEF extern
75#ifdef STBRP_LARGE_RECTS
196#ifdef STB_RECT_PACK_IMPLEMENTATION
199#define STBRP_SORT qsort
204#define STBRP_ASSERT assert
208#define STBRP__NOTUSED(v) (void)(v)
210#define STBRP__NOTUSED(v) (void)sizeof(v)
215 STBRP__INIT_skyline = 1
221 case STBRP__INIT_skyline:
232 if (allow_out_of_mem)
253#ifndef STBRP_LARGE_RECTS
254 STBRP_ASSERT(width <= 0xffff && height <= 0xffff);
257 for (i=0; i < num_nodes-1; ++i)
258 nodes[i].next = &nodes[i+1];
259 nodes[i].
next = NULL;
260 context->
init_mode = STBRP__INIT_skyline;
264 context->
width = width;
274#ifdef STBRP_LARGE_RECTS
275 context->
extra[1].
y = (1<<30);
277 context->
extra[1].
y = 65535;
287 int min_y, visited_width, waste_area;
291 STBRP_ASSERT(first->
x <= x0);
295 while (node->
next->
x <= x0)
298 STBRP_ASSERT(node->
next->
x > x0);
301 STBRP_ASSERT(node->
x <= x0);
306 while (node->
x < x1) {
307 if (node->
y > min_y) {
311 waste_area += visited_width * (node->
y - min_y);
315 visited_width += node->
next->
x - x0;
317 visited_width += node->
next->
x - node->
x;
320 int under_width = node->
next->
x - node->
x;
321 if (under_width + visited_width > width)
322 under_width = width - visited_width;
323 waste_area += under_width * (min_y - node->
y);
324 visited_width += under_width;
329 *pwaste = waste_area;
339static stbrp__findresult stbrp__skyline_find_best_pos(
stbrp_context *c,
int width,
int height)
341 int best_waste = (1<<30), best_x, best_y = (1 << 30);
342 stbrp__findresult fr;
343 stbrp_node **prev, *node, *tail, **best = NULL;
346 width = (width + c->
align - 1);
347 width -= width % c->
align;
348 STBRP_ASSERT(width % c->
align == 0);
352 while (node->
x + width <= c->width) {
354 y = stbrp__skyline_find_min_y(c, node, node->
x, width, &waste);
363 if (y + height <= c->height) {
365 if (y < best_y || (y == best_y && waste < best_waste)) {
376 best_x = (best == NULL) ? 0 : (*best)->
x;
396 tail = c->active_head;
397 node = c->active_head;
398 prev = &c->active_head;
400 while (tail->x < width)
403 int xpos = tail->x - width;
405 STBRP_ASSERT(xpos >= 0);
407 while (node->next->x <= xpos) {
411 STBRP_ASSERT(node->next->x > xpos && node->x <= xpos);
412 y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste);
413 if (y + height < c->height) {
415 if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
417 STBRP_ASSERT(y <= best_y);
434static stbrp__findresult stbrp__skyline_pack_rectangle(
stbrp_context *context,
int width,
int height)
437 stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height);
444 if (res.prev_link == NULL || res.y + height > context->
height || context->
free_head == NULL) {
445 res.prev_link = NULL;
460 cur = *res.prev_link;
461 if (cur->
x < res.x) {
467 *res.prev_link = node;
472 while (cur->
next && cur->
next->
x <= res.x + width) {
483 if (cur->
x < res.x + width)
488 while (cur->
x < context->
width) {
489 STBRP_ASSERT(cur->
x < cur->
next->
x);
492 STBRP_ASSERT(cur->
next == NULL);
509 STBRP_ASSERT(count == context->
num_nodes+2);
516static int rect_height_compare(
const void *a,
const void *b)
524 return (p->
w > q->
w) ? -1 : (p->
w < q->
w);
527static int rect_original_order(
const void *a,
const void *b)
534#ifdef STBRP_LARGE_RECTS
535#define STBRP__MAXVAL 0xffffffff
537#define STBRP__MAXVAL 0xffff
542 int i, all_rects_packed = 1;
545 for (i=0; i < num_rects; ++i) {
547 #ifndef STBRP_LARGE_RECTS
548 STBRP_ASSERT(rects[i].w <= 0xffff && rects[i].h <= 0xffff);
553 STBRP_SORT(rects, num_rects,
sizeof(rects[0]), rect_height_compare);
555 for (i=0; i < num_rects; ++i) {
556 if (rects[i].w == 0 || rects[i].h == 0) {
557 rects[i].
x = rects[i].
y = 0;
559 stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
564 rects[i].
x = rects[i].
y = STBRP__MAXVAL;
570 STBRP_SORT(rects, num_rects,
sizeof(rects[0]), rect_original_order);
573 for (i=0; i < num_rects; ++i) {
574 rects[i].
was_packed = !(rects[i].
x == STBRP__MAXVAL && rects[i].
y == STBRP__MAXVAL);
575 if (!rects[i].was_packed)
576 all_rects_packed = 0;
580 return all_rects_packed;
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_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_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
STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes)
unsigned short stbrp_coord
Definition stb_rect_pack.h:78
STBRP_DEF int stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects)
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
int id
Definition stb_rect_pack.h:109
stbrp_coord y
Definition stb_rect_pack.h:115
stbrp_coord h
Definition stb_rect_pack.h:112