-
Notifications
You must be signed in to change notification settings - Fork 188
/
GuillotineBinPack.cpp
641 lines (555 loc) · 19.1 KB
/
GuillotineBinPack.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/** @file GuillotineBinPack.cpp
@author Jukka Jylänki
@brief Implements different bin packer algorithms that use the GUILLOTINE data structure.
This work is released to Public Domain, do whatever you want with it.
*/
#include <algorithm>
#include <utility>
#include <iostream>
#include <limits>
#include <cassert>
#include <cstring>
#include <cmath>
#include "GuillotineBinPack.h"
namespace rbp {
using namespace std;
GuillotineBinPack::GuillotineBinPack()
:binWidth(0),
binHeight(0)
{
}
GuillotineBinPack::GuillotineBinPack(int width, int height)
{
Init(width, height);
}
void GuillotineBinPack::Init(int width, int height)
{
binWidth = width;
binHeight = height;
#ifdef _DEBUG
disjointRects.Clear();
#endif
// Clear any memory of previously packed rectangles.
usedRectangles.clear();
// We start with a single big free rectangle that spans the whole bin.
Rect n;
n.x = 0;
n.y = 0;
n.width = width;
n.height = height;
freeRectangles.clear();
freeRectangles.push_back(n);
}
void GuillotineBinPack::Insert(std::vector<RectSize> &rects, bool merge,
FreeRectChoiceHeuristic rectChoice, GuillotineSplitHeuristic splitMethod)
{
// Remember variables about the best packing choice we have made so far during the iteration process.
int bestFreeRect = 0;
int bestRect = 0;
bool bestFlipped = false;
// Pack rectangles one at a time until we have cleared the rects array of all rectangles.
// rects will get destroyed in the process.
while(rects.size() > 0)
{
// Stores the penalty score of the best rectangle placement - bigger=worse, smaller=better.
int bestScore = std::numeric_limits<int>::max();
for(size_t i = 0; i < freeRectangles.size(); ++i)
{
for(size_t j = 0; j < rects.size(); ++j)
{
// If this rectangle is a perfect match, we pick it instantly.
if (rects[j].width == freeRectangles[i].width && rects[j].height == freeRectangles[i].height)
{
bestFreeRect = i;
bestRect = j;
bestFlipped = false;
bestScore = std::numeric_limits<int>::min();
i = freeRectangles.size(); // Force a jump out of the outer loop as well - we got an instant fit.
break;
}
// If flipping this rectangle is a perfect match, pick that then.
else if (rects[j].height == freeRectangles[i].width && rects[j].width == freeRectangles[i].height)
{
bestFreeRect = i;
bestRect = j;
bestFlipped = true;
bestScore = std::numeric_limits<int>::min();
i = freeRectangles.size(); // Force a jump out of the outer loop as well - we got an instant fit.
break;
}
// Try if we can fit the rectangle upright.
else if (rects[j].width <= freeRectangles[i].width && rects[j].height <= freeRectangles[i].height)
{
int score = ScoreByHeuristic(rects[j].width, rects[j].height, freeRectangles[i], rectChoice);
if (score < bestScore)
{
bestFreeRect = i;
bestRect = j;
bestFlipped = false;
bestScore = score;
}
}
// If not, then perhaps flipping sideways will make it fit?
else if (rects[j].height <= freeRectangles[i].width && rects[j].width <= freeRectangles[i].height)
{
int score = ScoreByHeuristic(rects[j].height, rects[j].width, freeRectangles[i], rectChoice);
if (score < bestScore)
{
bestFreeRect = i;
bestRect = j;
bestFlipped = true;
bestScore = score;
}
}
}
}
// If we didn't manage to find any rectangle to pack, abort.
if (bestScore == std::numeric_limits<int>::max())
return;
// Otherwise, we're good to go and do the actual packing.
Rect newNode;
newNode.x = freeRectangles[bestFreeRect].x;
newNode.y = freeRectangles[bestFreeRect].y;
newNode.width = rects[bestRect].width;
newNode.height = rects[bestRect].height;
if (bestFlipped)
std::swap(newNode.width, newNode.height);
// Remove the free space we lost in the bin.
SplitFreeRectByHeuristic(freeRectangles[bestFreeRect], newNode, splitMethod);
freeRectangles.erase(freeRectangles.begin() + bestFreeRect);
// Remove the rectangle we just packed from the input list.
rects.erase(rects.begin() + bestRect);
// Perform a Rectangle Merge step if desired.
if (merge)
MergeFreeList();
// Remember the new used rectangle.
usedRectangles.push_back(newNode);
// Check that we're really producing correct packings here.
debug_assert(disjointRects.Add(newNode) == true);
}
}
/// @return True if r fits inside freeRect (possibly rotated).
bool Fits(const RectSize &r, const Rect &freeRect)
{
return (r.width <= freeRect.width && r.height <= freeRect.height) ||
(r.height <= freeRect.width && r.width <= freeRect.height);
}
/// @return True if r fits perfectly inside freeRect, i.e. the leftover area is 0.
bool FitsPerfectly(const RectSize &r, const Rect &freeRect)
{
return (r.width == freeRect.width && r.height == freeRect.height) ||
(r.height == freeRect.width && r.width == freeRect.height);
}
/*
// A helper function for GUILLOTINE-MAXFITTING. Counts how many rectangles fit into the given rectangle
// after it has been split.
void CountNumFitting(const Rect &freeRect, int width, int height, const std::vector<RectSize> &rects,
int usedRectIndex, bool splitHorizontal, int &score1, int &score2)
{
const int w = freeRect.width - width;
const int h = freeRect.height - height;
Rect bottom;
bottom.x = freeRect.x;
bottom.y = freeRect.y + height;
bottom.height = h;
Rect right;
right.x = freeRect.x + width;
right.y = freeRect.y;
right.width = w;
if (splitHorizontal)
{
bottom.width = freeRect.width;
right.height = height;
}
else // Split vertically
{
bottom.width = width;
right.height = freeRect.height;
}
int fitBottom = 0;
int fitRight = 0;
for(size_t i = 0; i < rects.size(); ++i)
if (i != usedRectIndex)
{
if (FitsPerfectly(rects[i], bottom))
fitBottom |= 0x10000000;
if (FitsPerfectly(rects[i], right))
fitRight |= 0x10000000;
if (Fits(rects[i], bottom))
++fitBottom;
if (Fits(rects[i], right))
++fitRight;
}
score1 = min(fitBottom, fitRight);
score2 = max(fitBottom, fitRight);
}
*/
/*
// Implements GUILLOTINE-MAXFITTING, an experimental heuristic that's really cool but didn't quite work in practice.
void GuillotineBinPack::InsertMaxFitting(std::vector<RectSize> &rects, std::vector<Rect> &dst, bool merge,
FreeRectChoiceHeuristic rectChoice, GuillotineSplitHeuristic splitMethod)
{
dst.clear();
int bestRect = 0;
bool bestFlipped = false;
bool bestSplitHorizontal = false;
// Pick rectangles one at a time and pack the one that leaves the most choices still open.
while(rects.size() > 0 && freeRectangles.size() > 0)
{
int bestScore1 = -1;
int bestScore2 = -1;
///\todo Different sort predicates.
clb::sort::QuickSort(&freeRectangles[0], freeRectangles.size(), CompareRectShortSide);
Rect &freeRect = freeRectangles[0];
for(size_t j = 0; j < rects.size(); ++j)
{
int score1;
int score2;
if (rects[j].width == freeRect.width && rects[j].height == freeRect.height)
{
bestRect = j;
bestFlipped = false;
bestScore1 = bestScore2 = std::numeric_limits<int>::max();
break;
}
else if (rects[j].width <= freeRect.width && rects[j].height <= freeRect.height)
{
CountNumFitting(freeRect, rects[j].width, rects[j].height, rects, j, false, score1, score2);
if (score1 > bestScore1 || (score1 == bestScore1 && score2 > bestScore2))
{
bestRect = j;
bestScore1 = score1;
bestScore2 = score2;
bestFlipped = false;
bestSplitHorizontal = false;
}
CountNumFitting(freeRect, rects[j].width, rects[j].height, rects, j, true, score1, score2);
if (score1 > bestScore1 || (score1 == bestScore1 && score2 > bestScore2))
{
bestRect = j;
bestScore1 = score1;
bestScore2 = score2;
bestFlipped = false;
bestSplitHorizontal = true;
}
}
if (rects[j].height == freeRect.width && rects[j].width == freeRect.height)
{
bestRect = j;
bestFlipped = true;
bestScore1 = bestScore2 = std::numeric_limits<int>::max();
break;
}
else if (rects[j].height <= freeRect.width && rects[j].width <= freeRect.height)
{
CountNumFitting(freeRect, rects[j].height, rects[j].width, rects, j, false, score1, score2);
if (score1 > bestScore1 || (score1 == bestScore1 && score2 > bestScore2))
{
bestRect = j;
bestScore1 = score1;
bestScore2 = score2;
bestFlipped = true;
bestSplitHorizontal = false;
}
CountNumFitting(freeRect, rects[j].height, rects[j].width, rects, j, true, score1, score2);
if (score1 > bestScore1 || (score1 == bestScore1 && score2 > bestScore2))
{
bestRect = j;
bestScore1 = score1;
bestScore2 = score2;
bestFlipped = true;
bestSplitHorizontal = true;
}
}
}
if (bestScore1 >= 0)
{
Rect newNode;
newNode.x = freeRect.x;
newNode.y = freeRect.y;
newNode.width = rects[bestRect].width;
newNode.height = rects[bestRect].height;
if (bestFlipped)
std::swap(newNode.width, newNode.height);
assert(disjointRects.Disjoint(newNode));
SplitFreeRectAlongAxis(freeRect, newNode, bestSplitHorizontal);
rects.erase(rects.begin() + bestRect);
if (merge)
MergeFreeList();
usedRectangles.push_back(newNode);
#ifdef _DEBUG
disjointRects.Add(newNode);
#endif
}
freeRectangles.erase(freeRectangles.begin());
}
}
*/
Rect GuillotineBinPack::Insert(int width, int height, bool merge, FreeRectChoiceHeuristic rectChoice,
GuillotineSplitHeuristic splitMethod)
{
// Find where to put the new rectangle.
int freeNodeIndex = 0;
Rect newRect = FindPositionForNewNode(width, height, rectChoice, &freeNodeIndex);
// Abort if we didn't have enough space in the bin.
if (newRect.height == 0)
return newRect;
// Remove the space that was just consumed by the new rectangle.
SplitFreeRectByHeuristic(freeRectangles[freeNodeIndex], newRect, splitMethod);
freeRectangles.erase(freeRectangles.begin() + freeNodeIndex);
// Perform a Rectangle Merge step if desired.
if (merge)
MergeFreeList();
// Remember the new used rectangle.
usedRectangles.push_back(newRect);
// Check that we're really producing correct packings here.
debug_assert(disjointRects.Add(newRect) == true);
return newRect;
}
/// Computes the ratio of used surface area to the total bin area.
float GuillotineBinPack::Occupancy() const
{
///\todo The occupancy rate could be cached/tracked incrementally instead
/// of looping through the list of packed rectangles here.
unsigned long usedSurfaceArea = 0;
for(size_t i = 0; i < usedRectangles.size(); ++i)
usedSurfaceArea += usedRectangles[i].width * usedRectangles[i].height;
return (float)usedSurfaceArea / (binWidth * binHeight);
}
/// Returns the heuristic score value for placing a rectangle of size width*height into freeRect. Does not try to rotate.
int GuillotineBinPack::ScoreByHeuristic(int width, int height, const Rect &freeRect, FreeRectChoiceHeuristic rectChoice)
{
switch(rectChoice)
{
case RectBestAreaFit: return ScoreBestAreaFit(width, height, freeRect);
case RectBestShortSideFit: return ScoreBestShortSideFit(width, height, freeRect);
case RectBestLongSideFit: return ScoreBestLongSideFit(width, height, freeRect);
case RectWorstAreaFit: return ScoreWorstAreaFit(width, height, freeRect);
case RectWorstShortSideFit: return ScoreWorstShortSideFit(width, height, freeRect);
case RectWorstLongSideFit: return ScoreWorstLongSideFit(width, height, freeRect);
default: assert(false); return std::numeric_limits<int>::max();
}
}
int GuillotineBinPack::ScoreBestAreaFit(int width, int height, const Rect &freeRect)
{
return freeRect.width * freeRect.height - width * height;
}
int GuillotineBinPack::ScoreBestShortSideFit(int width, int height, const Rect &freeRect)
{
int leftoverHoriz = abs(freeRect.width - width);
int leftoverVert = abs(freeRect.height - height);
int leftover = min(leftoverHoriz, leftoverVert);
return leftover;
}
int GuillotineBinPack::ScoreBestLongSideFit(int width, int height, const Rect &freeRect)
{
int leftoverHoriz = abs(freeRect.width - width);
int leftoverVert = abs(freeRect.height - height);
int leftover = max(leftoverHoriz, leftoverVert);
return leftover;
}
int GuillotineBinPack::ScoreWorstAreaFit(int width, int height, const Rect &freeRect)
{
return -ScoreBestAreaFit(width, height, freeRect);
}
int GuillotineBinPack::ScoreWorstShortSideFit(int width, int height, const Rect &freeRect)
{
return -ScoreBestShortSideFit(width, height, freeRect);
}
int GuillotineBinPack::ScoreWorstLongSideFit(int width, int height, const Rect &freeRect)
{
return -ScoreBestLongSideFit(width, height, freeRect);
}
Rect GuillotineBinPack::FindPositionForNewNode(int width, int height, FreeRectChoiceHeuristic rectChoice, int *nodeIndex)
{
Rect bestNode;
memset(&bestNode, 0, sizeof(Rect));
int bestScore = std::numeric_limits<int>::max();
/// Try each free rectangle to find the best one for placement.
for(size_t i = 0; i < freeRectangles.size(); ++i)
{
// If this is a perfect fit upright, choose it immediately.
if (width == freeRectangles[i].width && height == freeRectangles[i].height)
{
bestNode.x = freeRectangles[i].x;
bestNode.y = freeRectangles[i].y;
bestNode.width = width;
bestNode.height = height;
bestScore = std::numeric_limits<int>::min();
*nodeIndex = i;
debug_assert(disjointRects.Disjoint(bestNode));
break;
}
// If this is a perfect fit sideways, choose it.
else if (height == freeRectangles[i].width && width == freeRectangles[i].height)
{
bestNode.x = freeRectangles[i].x;
bestNode.y = freeRectangles[i].y;
bestNode.width = height;
bestNode.height = width;
bestScore = std::numeric_limits<int>::min();
*nodeIndex = i;
debug_assert(disjointRects.Disjoint(bestNode));
break;
}
// Does the rectangle fit upright?
else if (width <= freeRectangles[i].width && height <= freeRectangles[i].height)
{
int score = ScoreByHeuristic(width, height, freeRectangles[i], rectChoice);
if (score < bestScore)
{
bestNode.x = freeRectangles[i].x;
bestNode.y = freeRectangles[i].y;
bestNode.width = width;
bestNode.height = height;
bestScore = score;
*nodeIndex = i;
debug_assert(disjointRects.Disjoint(bestNode));
}
}
// Does the rectangle fit sideways?
else if (height <= freeRectangles[i].width && width <= freeRectangles[i].height)
{
int score = ScoreByHeuristic(height, width, freeRectangles[i], rectChoice);
if (score < bestScore)
{
bestNode.x = freeRectangles[i].x;
bestNode.y = freeRectangles[i].y;
bestNode.width = height;
bestNode.height = width;
bestScore = score;
*nodeIndex = i;
debug_assert(disjointRects.Disjoint(bestNode));
}
}
}
return bestNode;
}
void GuillotineBinPack::SplitFreeRectByHeuristic(const Rect &freeRect, const Rect &placedRect, GuillotineSplitHeuristic method)
{
// Compute the lengths of the leftover area.
const int w = freeRect.width - placedRect.width;
const int h = freeRect.height - placedRect.height;
// Placing placedRect into freeRect results in an L-shaped free area, which must be split into
// two disjoint rectangles. This can be achieved with by splitting the L-shape using a single line.
// We have two choices: horizontal or vertical.
// Use the given heuristic to decide which choice to make.
bool splitHorizontal;
switch(method)
{
case SplitShorterLeftoverAxis:
// Split along the shorter leftover axis.
splitHorizontal = (w <= h);
break;
case SplitLongerLeftoverAxis:
// Split along the longer leftover axis.
splitHorizontal = (w > h);
break;
case SplitMinimizeArea:
// Maximize the larger area == minimize the smaller area.
// Tries to make the single bigger rectangle.
splitHorizontal = (placedRect.width * h > w * placedRect.height);
break;
case SplitMaximizeArea:
// Maximize the smaller area == minimize the larger area.
// Tries to make the rectangles more even-sized.
splitHorizontal = (placedRect.width * h <= w * placedRect.height);
break;
case SplitShorterAxis:
// Split along the shorter total axis.
splitHorizontal = (freeRect.width <= freeRect.height);
break;
case SplitLongerAxis:
// Split along the longer total axis.
splitHorizontal = (freeRect.width > freeRect.height);
break;
default:
splitHorizontal = true;
assert(false);
}
// Perform the actual split.
SplitFreeRectAlongAxis(freeRect, placedRect, splitHorizontal);
}
/// This function will add the two generated rectangles into the freeRectangles array. The caller is expected to
/// remove the original rectangle from the freeRectangles array after that.
void GuillotineBinPack::SplitFreeRectAlongAxis(const Rect &freeRect, const Rect &placedRect, bool splitHorizontal)
{
// Form the two new rectangles.
Rect bottom;
bottom.x = freeRect.x;
bottom.y = freeRect.y + placedRect.height;
bottom.height = freeRect.height - placedRect.height;
Rect right;
right.x = freeRect.x + placedRect.width;
right.y = freeRect.y;
right.width = freeRect.width - placedRect.width;
if (splitHorizontal)
{
bottom.width = freeRect.width;
right.height = placedRect.height;
}
else // Split vertically
{
bottom.width = placedRect.width;
right.height = freeRect.height;
}
// Add the new rectangles into the free rectangle pool if they weren't degenerate.
if (bottom.width > 0 && bottom.height > 0)
freeRectangles.push_back(bottom);
if (right.width > 0 && right.height > 0)
freeRectangles.push_back(right);
debug_assert(disjointRects.Disjoint(bottom));
debug_assert(disjointRects.Disjoint(right));
}
void GuillotineBinPack::MergeFreeList()
{
#ifdef _DEBUG
DisjointRectCollection test;
for(size_t i = 0; i < freeRectangles.size(); ++i)
assert(test.Add(freeRectangles[i]) == true);
#endif
// Do a Theta(n^2) loop to see if any pair of free rectangles could me merged into one.
// Note that we miss any opportunities to merge three rectangles into one. (should call this function again to detect that)
for(size_t i = 0; i < freeRectangles.size(); ++i)
for(size_t j = i+1; j < freeRectangles.size(); ++j)
{
if (freeRectangles[i].width == freeRectangles[j].width && freeRectangles[i].x == freeRectangles[j].x)
{
if (freeRectangles[i].y == freeRectangles[j].y + freeRectangles[j].height)
{
freeRectangles[i].y -= freeRectangles[j].height;
freeRectangles[i].height += freeRectangles[j].height;
freeRectangles.erase(freeRectangles.begin() + j);
--j;
}
else if (freeRectangles[i].y + freeRectangles[i].height == freeRectangles[j].y)
{
freeRectangles[i].height += freeRectangles[j].height;
freeRectangles.erase(freeRectangles.begin() + j);
--j;
}
}
else if (freeRectangles[i].height == freeRectangles[j].height && freeRectangles[i].y == freeRectangles[j].y)
{
if (freeRectangles[i].x == freeRectangles[j].x + freeRectangles[j].width)
{
freeRectangles[i].x -= freeRectangles[j].width;
freeRectangles[i].width += freeRectangles[j].width;
freeRectangles.erase(freeRectangles.begin() + j);
--j;
}
else if (freeRectangles[i].x + freeRectangles[i].width == freeRectangles[j].x)
{
freeRectangles[i].width += freeRectangles[j].width;
freeRectangles.erase(freeRectangles.begin() + j);
--j;
}
}
}
#ifdef _DEBUG
test.Clear();
for(size_t i = 0; i < freeRectangles.size(); ++i)
assert(test.Add(freeRectangles[i]) == true);
#endif
}
}