| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | #ifndef STB_INCLUDE_STB_RECT_PACK_H |
| 71 | #define STB_INCLUDE_STB_RECT_PACK_H |
| 72 | |
| 73 | #define STB_RECT_PACK_VERSION 1 |
| 74 | |
| 75 | #ifdef STBRP_STATIC |
| 76 | #define STBRP_DEF static |
| 77 | #else |
| 78 | #define STBRP_DEF extern |
| 79 | #endif |
| 80 | |
| 81 | #ifdef __cplusplus |
| 82 | extern "C" { |
| 83 | #endif |
| 84 | |
| 85 | typedef struct stbrp_context stbrp_context; |
| 86 | typedef struct stbrp_node stbrp_node; |
| 87 | typedef struct stbrp_rect stbrp_rect; |
| 88 | |
| 89 | typedef int stbrp_coord; |
| 90 | |
| 91 | #define STBRP__MAXVAL 0x7fffffff |
| 92 | |
| 93 | |
| 94 | STBRP_DEF int stbrp_pack_rects (stbrp_context *context, stbrp_rect *rects, int num_rects); |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | struct stbrp_rect |
| 120 | { |
| 121 | |
| 122 | int id; |
| 123 | |
| 124 | |
| 125 | stbrp_coord w, h; |
| 126 | |
| 127 | |
| 128 | stbrp_coord x, y; |
| 129 | int was_packed; |
| 130 | |
| 131 | }; |
| 132 | |
| 133 | |
| 134 | STBRP_DEF void stbrp_init_target (stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes); |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | STBRP_DEF void stbrp_setup_allow_out_of_mem (stbrp_context *context, int allow_out_of_mem); |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | STBRP_DEF void stbrp_setup_heuristic (stbrp_context *context, int heuristic); |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | enum |
| 167 | { |
| 168 | STBRP_HEURISTIC_Skyline_default=0, |
| 169 | STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default, |
| 170 | STBRP_HEURISTIC_Skyline_BF_sortHeight |
| 171 | }; |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | struct stbrp_node |
| 180 | { |
| 181 | stbrp_coord x,y; |
| 182 | stbrp_node *next; |
| 183 | }; |
| 184 | |
| 185 | struct stbrp_context |
| 186 | { |
| 187 | int width; |
| 188 | int height; |
| 189 | int align; |
| 190 | int init_mode; |
| 191 | int heuristic; |
| 192 | int num_nodes; |
| 193 | stbrp_node *active_head; |
| 194 | stbrp_node *free_head; |
| 195 | stbrp_node extra[2]; |
| 196 | }; |
| 197 | |
| 198 | #ifdef __cplusplus |
| 199 | } |
| 200 | #endif |
| 201 | |
| 202 | #endif |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | #ifdef STB_RECT_PACK_IMPLEMENTATION |
| 210 | #ifndef STBRP_SORT |
| 211 | #include <stdlib.h> |
| 212 | #define STBRP_SORT qsort |
| 213 | #endif |
| 214 | |
| 215 | #ifndef STBRP_ASSERT |
| 216 | #include <assert.h> |
| 217 | #define STBRP_ASSERT assert |
| 218 | #endif |
| 219 | |
| 220 | #ifdef _MSC_VER |
| 221 | #define STBRP__NOTUSED(v) (void)(v) |
| 222 | #define STBRP__CDECL __cdecl |
| 223 | #else |
| 224 | #define STBRP__NOTUSED(v) (void)sizeof(v) |
| 225 | #define STBRP__CDECL |
| 226 | #endif |
| 227 | |
| 228 | enum |
| 229 | { |
| 230 | STBRP__INIT_skyline = 1 |
| 231 | }; |
| 232 | |
| 233 | STBRP_DEF void stbrp_setup_heuristic(stbrp_context *context, int heuristic) |
| 234 | { |
| 235 | switch (context->init_mode) { |
| 236 | case STBRP__INIT_skyline: |
| 237 | STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight); |
| 238 | context->heuristic = heuristic; |
| 239 | break; |
| 240 | default: |
| 241 | STBRP_ASSERT(0); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context *context, int allow_out_of_mem) |
| 246 | { |
| 247 | if (allow_out_of_mem) |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | context->align = 1; |
| 253 | else { |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | context->align = (context->width + context->num_nodes-1) / context->num_nodes; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes) |
| 266 | { |
| 267 | int i; |
| 268 | |
| 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; |
| 273 | context->heuristic = STBRP_HEURISTIC_Skyline_default; |
| 274 | context->free_head = &nodes[0]; |
| 275 | context->active_head = &context->extra[0]; |
| 276 | context->width = width; |
| 277 | context->height = height; |
| 278 | context->num_nodes = num_nodes; |
| 279 | stbrp_setup_allow_out_of_mem(context, 0); |
| 280 | |
| 281 | |
| 282 | context->extra[0].x = 0; |
| 283 | context->extra[0].y = 0; |
| 284 | context->extra[0].next = &context->extra[1]; |
| 285 | context->extra[1].x = (stbrp_coord) width; |
| 286 | context->extra[1].y = (1<<30); |
| 287 | context->extra[1].next = NULL; |
| 288 | } |
| 289 | |
| 290 | |
| 291 | static int stbrp__skyline_find_min_y(stbrp_context *c, stbrp_node *first, int x0, int width, int *pwaste) |
| 292 | { |
| 293 | stbrp_node *node = first; |
| 294 | int x1 = x0 + width; |
| 295 | int min_y, visited_width, waste_area; |
| 296 | |
| 297 | STBRP__NOTUSED(c); |
| 298 | |
| 299 | STBRP_ASSERT(first->x <= x0); |
| 300 | |
| 301 | #if 0 |
| 302 | |
| 303 | while (node->next->x <= x0) |
| 304 | ++node; |
| 305 | #else |
| 306 | STBRP_ASSERT(node->next->x > x0); |
| 307 | #endif |
| 308 | |
| 309 | STBRP_ASSERT(node->x <= x0); |
| 310 | |
| 311 | min_y = 0; |
| 312 | waste_area = 0; |
| 313 | visited_width = 0; |
| 314 | while (node->x < x1) { |
| 315 | if (node->y > min_y) { |
| 316 | |
| 317 | |
| 318 | |
| 319 | waste_area += visited_width * (node->y - min_y); |
| 320 | min_y = node->y; |
| 321 | |
| 322 | if (node->x < x0) |
| 323 | visited_width += node->next->x - x0; |
| 324 | else |
| 325 | visited_width += node->next->x - node->x; |
| 326 | } else { |
| 327 | |
| 328 | int under_width = node->next->x - node->x; |
| 329 | if (under_width + visited_width > width) |
| 330 | under_width = width - visited_width; |
| 331 | waste_area += under_width * (min_y - node->y); |
| 332 | visited_width += under_width; |
| 333 | } |
| 334 | node = node->next; |
| 335 | } |
| 336 | |
| 337 | *pwaste = waste_area; |
| 338 | return min_y; |
| 339 | } |
| 340 | |
| 341 | typedef struct |
| 342 | { |
| 343 | int x,y; |
| 344 | stbrp_node **prev_link; |
| 345 | } stbrp__findresult; |
| 346 | |
| 347 | static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height) |
| 348 | { |
| 349 | int best_waste = (1<<30), best_x, best_y = (1 << 30); |
| 350 | stbrp__findresult fr; |
| 351 | stbrp_node **prev, *node, *tail, **best = NULL; |
| 352 | |
| 353 | |
| 354 | width = (width + c->align - 1); |
| 355 | width -= width % c->align; |
| 356 | STBRP_ASSERT(width % c->align == 0); |
| 357 | |
| 358 | |
| 359 | if (width > c->width || height > c->height) { |
| 360 | fr.prev_link = NULL; |
| 361 | fr.x = fr.y = 0; |
| 362 | return fr; |
| 363 | } |
| 364 | |
| 365 | node = c->active_head; |
| 366 | prev = &c->active_head; |
| 367 | while (node->x + width <= c->width) { |
| 368 | int y,waste; |
| 369 | y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste); |
| 370 | if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { |
| 371 | |
| 372 | if (y < best_y) { |
| 373 | best_y = y; |
| 374 | best = prev; |
| 375 | } |
| 376 | } else { |
| 377 | |
| 378 | if (y + height <= c->height) { |
| 379 | |
| 380 | if (y < best_y || (y == best_y && waste < best_waste)) { |
| 381 | best_y = y; |
| 382 | best_waste = waste; |
| 383 | best = prev; |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | prev = &node->next; |
| 388 | node = node->next; |
| 389 | } |
| 390 | |
| 391 | best_x = (best == NULL) ? 0 : (*best)->x; |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) { |
| 411 | tail = c->active_head; |
| 412 | node = c->active_head; |
| 413 | prev = &c->active_head; |
| 414 | |
| 415 | while (tail->x < width) |
| 416 | tail = tail->next; |
| 417 | while (tail) { |
| 418 | int xpos = tail->x - width; |
| 419 | int y,waste; |
| 420 | STBRP_ASSERT(xpos >= 0); |
| 421 | |
| 422 | while (node->next->x <= xpos) { |
| 423 | prev = &node->next; |
| 424 | node = node->next; |
| 425 | } |
| 426 | STBRP_ASSERT(node->next->x > xpos && node->x <= xpos); |
| 427 | y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste); |
| 428 | if (y + height <= c->height) { |
| 429 | if (y <= best_y) { |
| 430 | if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) { |
| 431 | best_x = xpos; |
| 432 | |
| 433 | best_y = y; |
| 434 | best_waste = waste; |
| 435 | best = prev; |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | tail = tail->next; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | fr.prev_link = best; |
| 444 | fr.x = best_x; |
| 445 | fr.y = best_y; |
| 446 | return fr; |
| 447 | } |
| 448 | |
| 449 | static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height) |
| 450 | { |
| 451 | |
| 452 | stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height); |
| 453 | stbrp_node *node, *cur; |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) { |
| 460 | res.prev_link = NULL; |
| 461 | return res; |
| 462 | } |
| 463 | |
| 464 | |
| 465 | node = context->free_head; |
| 466 | node->x = (stbrp_coord) res.x; |
| 467 | node->y = (stbrp_coord) (res.y + height); |
| 468 | |
| 469 | context->free_head = node->next; |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | cur = *res.prev_link; |
| 476 | if (cur->x < res.x) { |
| 477 | |
| 478 | stbrp_node *next = cur->next; |
| 479 | cur->next = node; |
| 480 | cur = next; |
| 481 | } else { |
| 482 | *res.prev_link = node; |
| 483 | } |
| 484 | |
| 485 | |
| 486 | |
| 487 | while (cur->next && cur->next->x <= res.x + width) { |
| 488 | stbrp_node *next = cur->next; |
| 489 | |
| 490 | cur->next = context->free_head; |
| 491 | context->free_head = cur; |
| 492 | cur = next; |
| 493 | } |
| 494 | |
| 495 | |
| 496 | node->next = cur; |
| 497 | |
| 498 | if (cur->x < res.x + width) |
| 499 | cur->x = (stbrp_coord) (res.x + width); |
| 500 | |
| 501 | #ifdef _DEBUG |
| 502 | cur = context->active_head; |
| 503 | while (cur->x < context->width) { |
| 504 | STBRP_ASSERT(cur->x < cur->next->x); |
| 505 | cur = cur->next; |
| 506 | } |
| 507 | STBRP_ASSERT(cur->next == NULL); |
| 508 | |
| 509 | { |
| 510 | int count=0; |
| 511 | cur = context->active_head; |
| 512 | while (cur) { |
| 513 | cur = cur->next; |
| 514 | ++count; |
| 515 | } |
| 516 | cur = context->free_head; |
| 517 | while (cur) { |
| 518 | cur = cur->next; |
| 519 | ++count; |
| 520 | } |
| 521 | STBRP_ASSERT(count == context->num_nodes+2); |
| 522 | } |
| 523 | #endif |
| 524 | |
| 525 | return res; |
| 526 | } |
| 527 | |
| 528 | static int STBRP__CDECL rect_height_compare(const void *a, const void *b) |
| 529 | { |
| 530 | const stbrp_rect *p = (const stbrp_rect *) a; |
| 531 | const stbrp_rect *q = (const stbrp_rect *) b; |
| 532 | if (p->h > q->h) |
| 533 | return -1; |
| 534 | if (p->h < q->h) |
| 535 | return 1; |
| 536 | return (p->w > q->w) ? -1 : (p->w < q->w); |
| 537 | } |
| 538 | |
| 539 | static int STBRP__CDECL rect_original_order(const void *a, const void *b) |
| 540 | { |
| 541 | const stbrp_rect *p = (const stbrp_rect *) a; |
| 542 | const stbrp_rect *q = (const stbrp_rect *) b; |
| 543 | return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed); |
| 544 | } |
| 545 | |
| 546 | STBRP_DEF int stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects) |
| 547 | { |
| 548 | int i, all_rects_packed = 1; |
| 549 | |
| 550 | |
| 551 | for (i=0; i < num_rects; ++i) { |
| 552 | rects[i].was_packed = i; |
| 553 | } |
| 554 | |
| 555 | |
| 556 | STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare); |
| 557 | |
| 558 | for (i=0; i < num_rects; ++i) { |
| 559 | if (rects[i].w == 0 || rects[i].h == 0) { |
| 560 | rects[i].x = rects[i].y = 0; |
| 561 | } else { |
| 562 | stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h); |
| 563 | if (fr.prev_link) { |
| 564 | rects[i].x = (stbrp_coord) fr.x; |
| 565 | rects[i].y = (stbrp_coord) fr.y; |
| 566 | } else { |
| 567 | rects[i].x = rects[i].y = STBRP__MAXVAL; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | |
| 573 | STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order); |
| 574 | |
| 575 | |
| 576 | for (i=0; i < num_rects; ++i) { |
| 577 | rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL); |
| 578 | if (!rects[i].was_packed) |
| 579 | all_rects_packed = 0; |
| 580 | } |
| 581 | |
| 582 | |
| 583 | return all_rects_packed; |
| 584 | } |
| 585 | #endif |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 | |
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | |
| 626 | |
| 627 | |