|
| 1 | +#include "stb_rect_pack.h" |
| 2 | + |
| 3 | +#include <stdlib.h> |
| 4 | + |
| 5 | +enum |
| 6 | +{ |
| 7 | + STBRP__INIT_skyline = 1 |
| 8 | +}; |
| 9 | + |
| 10 | +void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes) |
| 11 | +{ |
| 12 | + int i; |
| 13 | + for (i=0; i < num_nodes-1; ++i) |
| 14 | + nodes[i].next = &nodes[i+1]; |
| 15 | + nodes[i].next = NULL; |
| 16 | + context->init_mode = STBRP__INIT_skyline; |
| 17 | + context->heuristic = STBRP_HEURISTIC_Skyline_default; |
| 18 | + context->free_head = &nodes[0]; |
| 19 | + context->active_head = &context->extra[0]; |
| 20 | + context->width = width; |
| 21 | + context->height = height; |
| 22 | + context->num_nodes = num_nodes; |
| 23 | + context->align = (context->width + context->num_nodes-1) / context->num_nodes; |
| 24 | + |
| 25 | + /* node 0 is the full width, |
| 26 | + * node 1 is the sentinel (lets us not store width explicitly) */ |
| 27 | + context->extra[0].x = 0; |
| 28 | + context->extra[0].y = 0; |
| 29 | + context->extra[0].next = &context->extra[1]; |
| 30 | + context->extra[1].x = (uint16_t) width; |
| 31 | + context->extra[1].y = 65535; |
| 32 | + context->extra[1].next = NULL; |
| 33 | +} |
| 34 | + |
| 35 | +/* Find minimum y position if it starts at x1 */ |
| 36 | +static int stbrp__skyline_find_min_y(stbrp_context *c, |
| 37 | + stbrp_node *first, int x0, int width, int *pwaste) |
| 38 | +{ |
| 39 | + int min_y, visited_width, waste_area; |
| 40 | + stbrp_node *node = first; |
| 41 | + int x1 = x0 + width; |
| 42 | + min_y = 0; |
| 43 | + waste_area = 0; |
| 44 | + visited_width = 0; |
| 45 | + while (node->x < x1) |
| 46 | + { |
| 47 | + if (node->y > min_y) |
| 48 | + { |
| 49 | + /* raise min_y higher. |
| 50 | + * we've accounted for all waste up to min_y, |
| 51 | + * but we'll now add more waste for everything we've visted |
| 52 | + */ |
| 53 | + waste_area += visited_width * (node->y - min_y); |
| 54 | + min_y = node->y; |
| 55 | + |
| 56 | + /* the first time through, visited_width might be reduced */ |
| 57 | + if (node->x < x0) |
| 58 | + visited_width += node->next->x - x0; |
| 59 | + else |
| 60 | + visited_width += node->next->x - node->x; |
| 61 | + } |
| 62 | + else |
| 63 | + { |
| 64 | + /* add waste area */ |
| 65 | + int under_width = node->next->x - node->x; |
| 66 | + if (under_width + visited_width > width) |
| 67 | + under_width = width - visited_width; |
| 68 | + waste_area += under_width * (min_y - node->y); |
| 69 | + visited_width += under_width; |
| 70 | + } |
| 71 | + node = node->next; |
| 72 | + } |
| 73 | + |
| 74 | + *pwaste = waste_area; |
| 75 | + return min_y; |
| 76 | +} |
| 77 | + |
| 78 | +typedef struct |
| 79 | +{ |
| 80 | + int x,y; |
| 81 | + stbrp_node **prev_link; |
| 82 | +} stbrp__findresult; |
| 83 | + |
| 84 | +static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height) |
| 85 | +{ |
| 86 | + int best_waste = (1<<30), best_x, best_y = (1 << 30); |
| 87 | + stbrp__findresult fr; |
| 88 | + stbrp_node **prev, *node, *tail, **best = NULL; |
| 89 | + |
| 90 | + /* align to multiple of c->align */ |
| 91 | + width = (width + c->align - 1); |
| 92 | + width -= width % c->align; |
| 93 | + |
| 94 | + node = c->active_head; |
| 95 | + prev = &c->active_head; |
| 96 | + while (node->x + width <= c->width) |
| 97 | + { |
| 98 | + int waste; |
| 99 | + int y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste); |
| 100 | + |
| 101 | + if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) |
| 102 | + { |
| 103 | + /* actually just want to test BL bottom left */ |
| 104 | + if (y < best_y) |
| 105 | + { |
| 106 | + best_y = y; |
| 107 | + best = prev; |
| 108 | + } |
| 109 | + } |
| 110 | + else |
| 111 | + { |
| 112 | + /* best-fit */ |
| 113 | + if (y + height <= c->height) |
| 114 | + { |
| 115 | + /* can only use it if it first vertically */ |
| 116 | + if (y < best_y || (y == best_y && waste < best_waste)) |
| 117 | + { |
| 118 | + best_y = y; |
| 119 | + best_waste = waste; |
| 120 | + best = prev; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + prev = &node->next; |
| 125 | + node = node->next; |
| 126 | + } |
| 127 | + |
| 128 | + best_x = (best == NULL) ? 0 : (*best)->x; |
| 129 | + |
| 130 | + /* if doing best-fit (BF), we also have to try aligning right edge to each node position |
| 131 | + * |
| 132 | + * e.g, if fitting |
| 133 | + * |
| 134 | + * ____________________ |
| 135 | + * |____________________| |
| 136 | + * |
| 137 | + * into |
| 138 | + * |
| 139 | + * | | |
| 140 | + * | ____________| |
| 141 | + * |____________| |
| 142 | + * |
| 143 | + * then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned |
| 144 | + * |
| 145 | + * This makes BF take about 2x the time |
| 146 | + */ |
| 147 | + |
| 148 | + if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) |
| 149 | + { |
| 150 | + tail = c->active_head; |
| 151 | + node = c->active_head; |
| 152 | + prev = &c->active_head; |
| 153 | + /* find first node that's admissible */ |
| 154 | + while (tail->x < width) |
| 155 | + tail = tail->next; |
| 156 | + while (tail) |
| 157 | + { |
| 158 | + int xpos = tail->x - width; |
| 159 | + int y,waste; |
| 160 | + |
| 161 | + /* find the left position that matches this */ |
| 162 | + while (node->next->x <= xpos) |
| 163 | + { |
| 164 | + prev = &node->next; |
| 165 | + node = node->next; |
| 166 | + } |
| 167 | + |
| 168 | + y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste); |
| 169 | + |
| 170 | + if (y + height < c->height) |
| 171 | + { |
| 172 | + if (y <= best_y) |
| 173 | + { |
| 174 | + if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) |
| 175 | + { |
| 176 | + best_x = xpos; |
| 177 | + best_y = y; |
| 178 | + best_waste = waste; |
| 179 | + best = prev; |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + tail = tail->next; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + fr.prev_link = best; |
| 188 | + fr.x = best_x; |
| 189 | + fr.y = best_y; |
| 190 | + return fr; |
| 191 | +} |
| 192 | + |
| 193 | +static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height) |
| 194 | +{ |
| 195 | + /* find best position according to heuristic */ |
| 196 | + stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height); |
| 197 | + |
| 198 | + /* bail if: |
| 199 | + * 1. it failed |
| 200 | + * 2. the best node doesn't fit (we don't always check this) |
| 201 | + * 3. we're out of memory |
| 202 | + */ |
| 203 | + if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) |
| 204 | + res.prev_link = NULL; |
| 205 | + else |
| 206 | + { |
| 207 | + stbrp_node *cur; |
| 208 | + /* on success, create new node */ |
| 209 | + stbrp_node *node = context->free_head; |
| 210 | + node->x = (uint16_t) res.x; |
| 211 | + node->y = (uint16_t) (res.y + height); |
| 212 | + |
| 213 | + context->free_head = node->next; |
| 214 | + |
| 215 | + /* insert the new node into the right starting point, and |
| 216 | + * let 'cur' point to the remaining nodes needing to be |
| 217 | + * stiched back in |
| 218 | + */ |
| 219 | + |
| 220 | + cur = *res.prev_link; |
| 221 | + if (cur->x < res.x) |
| 222 | + { |
| 223 | + /* preserve the existing one, so start testing with the next one */ |
| 224 | + stbrp_node *next = cur->next; |
| 225 | + cur->next = node; |
| 226 | + cur = next; |
| 227 | + } |
| 228 | + else |
| 229 | + *res.prev_link = node; |
| 230 | + |
| 231 | + /* from here, traverse cur and free the nodes, until we get to one |
| 232 | + * that shouldn't be freed */ |
| 233 | + while (cur->next && cur->next->x <= res.x + width) |
| 234 | + { |
| 235 | + stbrp_node *next = cur->next; |
| 236 | + |
| 237 | + /* move the current node to the free list */ |
| 238 | + cur->next = context->free_head; |
| 239 | + context->free_head = cur; |
| 240 | + cur = next; |
| 241 | + } |
| 242 | + |
| 243 | + /* stitch the list back in */ |
| 244 | + node->next = cur; |
| 245 | + |
| 246 | + if (cur->x < res.x + width) |
| 247 | + cur->x = (uint16_t) (res.x + width); |
| 248 | + } |
| 249 | + return res; |
| 250 | +} |
| 251 | + |
| 252 | +static int rect_height_compare(const void *a, const void *b) |
| 253 | +{ |
| 254 | + stbrp_rect *p = (stbrp_rect *) a; |
| 255 | + stbrp_rect *q = (stbrp_rect *) b; |
| 256 | + if (p->h > q->h) |
| 257 | + return -1; |
| 258 | + if (p->h < q->h) |
| 259 | + return 1; |
| 260 | + return (p->w > q->w) ? -1 : (p->w < q->w); |
| 261 | +} |
| 262 | + |
| 263 | +static int rect_original_order(const void *a, const void *b) |
| 264 | +{ |
| 265 | + stbrp_rect *p = (stbrp_rect *) a; |
| 266 | + stbrp_rect *q = (stbrp_rect *) b; |
| 267 | + return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed); |
| 268 | +} |
| 269 | + |
| 270 | +#define STBRP__MAXVAL 0xffff |
| 271 | + |
| 272 | +void stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects) |
| 273 | +{ |
| 274 | + int i; |
| 275 | + |
| 276 | + /* we use the 'was_packed' field internally to allow sorting/unsorting */ |
| 277 | + for (i=0; i < num_rects; ++i) |
| 278 | + rects[i].was_packed = i; |
| 279 | + |
| 280 | + /* sort according to heuristic */ |
| 281 | + qsort(rects, num_rects, sizeof(rects[0]), rect_height_compare); |
| 282 | + |
| 283 | + for (i=0; i < num_rects; ++i) |
| 284 | + { |
| 285 | + stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h); |
| 286 | + if (fr.prev_link) |
| 287 | + { |
| 288 | + rects[i].x = (uint16_t) fr.x; |
| 289 | + rects[i].y = (uint16_t) fr.y; |
| 290 | + } |
| 291 | + else |
| 292 | + rects[i].x = rects[i].y = STBRP__MAXVAL; |
| 293 | + } |
| 294 | + |
| 295 | + /* unsort */ |
| 296 | + qsort(rects, num_rects, sizeof(rects[0]), rect_original_order); |
| 297 | + |
| 298 | + /* set was_packed flags */ |
| 299 | + for (i=0; i < num_rects; ++i) |
| 300 | + rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL); |
| 301 | +} |
0 commit comments