66#ifndef STB_INCLUDE_STB_RECT_PACK_H 
   67#define STB_INCLUDE_STB_RECT_PACK_H 
   69#define STB_RECT_PACK_VERSION  1 
   72#define STBRP_DEF static 
   74#define STBRP_DEF extern 
   85typedef int            stbrp_coord;
 
   87#define STBRP__MAXVAL  0x7fffffff 
  130STBRP_DEF 
void stbrp_init_target (
stbrp_context *context, 
int width, 
int height, 
stbrp_node *nodes, 
int num_nodes);
 
  151STBRP_DEF 
void stbrp_setup_allow_out_of_mem (
stbrp_context *context, 
int allow_out_of_mem);
 
  157STBRP_DEF 
void stbrp_setup_heuristic (
stbrp_context *context, 
int heuristic);
 
  164   STBRP_HEURISTIC_Skyline_default=0,
 
  165   STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default,
 
  166   STBRP_HEURISTIC_Skyline_BF_sortHeight
 
  205#ifdef STB_RECT_PACK_IMPLEMENTATION 
  208#define STBRP_SORT qsort 
  213#define STBRP_ASSERT assert 
  217#define STBRP__NOTUSED(v)  (void)(v) 
  218#define STBRP__CDECL       __cdecl 
  220#define STBRP__NOTUSED(v)  (void)sizeof(v) 
  226   STBRP__INIT_skyline = 1
 
  229STBRP_DEF 
void stbrp_setup_heuristic(
stbrp_context *context, 
int heuristic)
 
  231   switch (context->init_mode) {
 
  232      case STBRP__INIT_skyline:
 
  233         STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight);
 
  234         context->heuristic = heuristic;
 
  241STBRP_DEF 
void stbrp_setup_allow_out_of_mem(
stbrp_context *context, 
int allow_out_of_mem)
 
  243   if (allow_out_of_mem)
 
  257      context->align = (context->width + context->num_nodes-1) / context->num_nodes;
 
  261STBRP_DEF 
void stbrp_init_target(
stbrp_context *context, 
int width, 
int height, 
stbrp_node *nodes, 
int num_nodes)
 
  265   for (i=0; i < num_nodes-1; ++i)
 
  266      nodes[i].next = &nodes[i+1];
 
  267   nodes[i].next = NULL;
 
  268   context->init_mode = STBRP__INIT_skyline;
 
  269   context->heuristic = STBRP_HEURISTIC_Skyline_default;
 
  270   context->free_head = &nodes[0];
 
  271   context->active_head = &context->extra[0];
 
  272   context->width = width;
 
  273   context->height = height;
 
  274   context->num_nodes = num_nodes;
 
  275   stbrp_setup_allow_out_of_mem(context, 0);
 
  278   context->extra[0].x = 0;
 
  279   context->extra[0].y = 0;
 
  280   context->extra[0].next = &context->extra[1];
 
  281   context->extra[1].x = (stbrp_coord) width;
 
  282   context->extra[1].y = (1<<30);
 
  283   context->extra[1].next = NULL;
 
  291   int min_y, visited_width, waste_area;
 
  295   STBRP_ASSERT(first->x <= x0);
 
  299   while (node->next->x <= x0)
 
  302   STBRP_ASSERT(node->next->x > x0); 
 
  305   STBRP_ASSERT(node->x <= x0);
 
  310   while (node->x < x1) {
 
  311      if (node->y > min_y) {
 
  315         waste_area += visited_width * (node->y - min_y);
 
  319            visited_width += node->next->x - x0;
 
  321            visited_width += node->next->x - node->x;
 
  324         int under_width = node->next->x - node->x;
 
  325         if (under_width + visited_width > width)
 
  326            under_width = width - visited_width;
 
  327         waste_area += under_width * (min_y - node->y);
 
  328         visited_width += under_width;
 
  333   *pwaste = waste_area;
 
  343static stbrp__findresult stbrp__skyline_find_best_pos(
stbrp_context *c, 
int width, 
int height)
 
  345   int best_waste = (1<<30), best_x, best_y = (1 << 30);
 
  346   stbrp__findresult fr;
 
  347   stbrp_node **prev, *node, *tail, **best = NULL;
 
  350   width = (width + c->align - 1);
 
  351   width -= width % c->align;
 
  352   STBRP_ASSERT(width % c->align == 0);
 
  355   if (width > c->width || height > c->height) {
 
  361   node = c->active_head;
 
  362   prev = &c->active_head;
 
  363   while (node->x + width <= c->width) {
 
  365      y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste);
 
  366      if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { 
 
  374         if (y + height <= c->height) {
 
  376            if (y < best_y || (y == best_y && waste < best_waste)) {
 
  387   best_x = (best == NULL) ? 0 : (*best)->x;
 
  406   if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) {
 
  407      tail = c->active_head;
 
  408      node = c->active_head;
 
  409      prev = &c->active_head;
 
  411      while (tail->x < width)
 
  414         int xpos = tail->x - width;
 
  416         STBRP_ASSERT(xpos >= 0);
 
  418         while (node->next->x <= xpos) {
 
  422         STBRP_ASSERT(node->next->x > xpos && node->x <= xpos);
 
  423         y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste);
 
  424         if (y + height <= c->height) {
 
  426               if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
 
  428                  STBRP_ASSERT(y <= best_y);
 
  445static stbrp__findresult stbrp__skyline_pack_rectangle(
stbrp_context *context, 
int width, 
int height)
 
  448   stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height);
 
  455   if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) {
 
  456      res.prev_link = NULL;
 
  461   node = context->free_head;
 
  462   node->x = (stbrp_coord) res.x;
 
  463   node->y = (stbrp_coord) (res.y + height);
 
  465   context->free_head = node->next;
 
  471   cur = *res.prev_link;
 
  472   if (cur->x < res.x) {
 
  478      *res.prev_link = node;
 
  483   while (cur->next && cur->next->x <= res.x + width) {
 
  486      cur->next = context->free_head;
 
  487      context->free_head = cur;
 
  494   if (cur->x < res.x + width)
 
  495      cur->x = (stbrp_coord) (res.x + width);
 
  498   cur = context->active_head;
 
  499   while (cur->x < context->width) {
 
  500      STBRP_ASSERT(cur->x < cur->next->x);
 
  503   STBRP_ASSERT(cur->next == NULL);
 
  507      cur = context->active_head;
 
  512      cur = context->free_head;
 
  517      STBRP_ASSERT(count == context->num_nodes+2);
 
  524static int STBRP__CDECL rect_height_compare(
const void *a, 
const void *b)
 
  532   return (p->w > q->w) ? -1 : (p->w < q->w);
 
  535static int STBRP__CDECL rect_original_order(
const void *a, 
const void *b)
 
  539   return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
 
  544   int i, all_rects_packed = 1;
 
  547   for (i=0; i < num_rects; ++i) {
 
  548      rects[i].was_packed = i;
 
  552   STBRP_SORT(rects, num_rects, 
sizeof(rects[0]), rect_height_compare);
 
  554   for (i=0; i < num_rects; ++i) {
 
  555      if (rects[i].w == 0 || rects[i].h == 0) {
 
  556         rects[i].x = rects[i].y = 0;  
 
  558         stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
 
  560            rects[i].x = (stbrp_coord) fr.x;
 
  561            rects[i].y = (stbrp_coord) fr.y;
 
  563            rects[i].x = rects[i].y = STBRP__MAXVAL;
 
  569   STBRP_SORT(rects, num_rects, 
sizeof(rects[0]), rect_original_order);
 
  572   for (i=0; i < num_rects; ++i) {
 
  573      rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL);
 
  574      if (!rects[i].was_packed)
 
  575         all_rects_packed = 0;
 
  579   return all_rects_packed;