forked from juj/RectangleBinPack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShelfBinPack.cpp
397 lines (342 loc) · 10.2 KB
/
ShelfBinPack.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
/** @file ShelfBinPack.cpp
@author Jukka Jylänki
@brief Implements different bin packer algorithms that use the SHELF data structure.
This work is released to Public Domain, do whatever you want with it.
*/
#include <utility>
#include <iostream>
#include <cassert>
#include <cstring>
#include "ShelfBinPack.h"
namespace rbp {
using namespace std;
ShelfBinPack::ShelfBinPack()
:binWidth(0),
binHeight(0),
currentY(0),
usedSurfaceArea(0),
useWasteMap(false)
{
}
ShelfBinPack::ShelfBinPack(int width, int height, bool useWasteMap)
{
Init(width, height, useWasteMap);
}
void ShelfBinPack::Init(int width, int height, bool useWasteMap_)
{
useWasteMap = useWasteMap_;
binWidth = width;
binHeight = height;
currentY = 0;
usedSurfaceArea = 0;
shelves.clear();
StartNewShelf(0);
if (useWasteMap)
{
wasteMap.Init(width, height);
wasteMap.GetFreeRectangles().clear();
}
}
bool ShelfBinPack::CanStartNewShelf(int height) const
{
return shelves.back().startY + shelves.back().height + height <= binHeight;
}
void ShelfBinPack::StartNewShelf(int startingHeight)
{
if (shelves.size() > 0)
{
assert(shelves.back().height != 0);
currentY += shelves.back().height;
assert(currentY < binHeight);
}
Shelf shelf;
shelf.currentX = 0;
shelf.height = startingHeight;
shelf.startY = currentY;
assert(shelf.startY + shelf.height <= binHeight);
shelves.push_back(shelf);
}
bool ShelfBinPack::FitsOnShelf(const Shelf &shelf, int width, int height, bool canResize) const
{
const int shelfHeight = canResize ? (binHeight - shelf.startY) : shelf.height;
if ((shelf.currentX + width <= binWidth && height <= shelfHeight) ||
(shelf.currentX + height <= binWidth && width <= shelfHeight))
return true;
else
return false;
}
void ShelfBinPack::RotateToShelf(const Shelf &shelf, int &width, int &height) const
{
// If the width > height and the long edge of the new rectangle fits vertically onto the current shelf,
// flip it. If the short edge is larger than the current shelf height, store
// the short edge vertically.
if ((width > height && width > binWidth - shelf.currentX) ||
(width > height && width < shelf.height) ||
(width < height && height > shelf.height && height <= binWidth - shelf.currentX))
swap(width, height);
}
void ShelfBinPack::AddToShelf(Shelf &shelf, int width, int height, Rect &newNode)
{
assert(FitsOnShelf(shelf, width, height, true));
// Swap width and height if the rect fits better that way.
RotateToShelf(shelf, width, height);
// Add the rectangle to the shelf.
newNode.x = shelf.currentX;
newNode.y = shelf.startY;
newNode.width = width;
newNode.height = height;
shelf.usedRectangles.push_back(newNode);
// Advance the shelf end position horizontally.
shelf.currentX += width;
assert(shelf.currentX <= binWidth);
// Grow the shelf height.
shelf.height = max(shelf.height, height);
assert(shelf.height <= binHeight);
usedSurfaceArea += width * height;
}
Rect ShelfBinPack::Insert(int width, int height, ShelfChoiceHeuristic method)
{
Rect newNode;
// First try to pack this rectangle into the waste map, if it fits.
if (useWasteMap)
{
newNode = wasteMap.Insert(width, height, true, GuillotineBinPack::RectBestShortSideFit,
GuillotineBinPack::SplitMaximizeArea);
if (newNode.height != 0)
{
// Track the space we just used.
usedSurfaceArea += width * height;
return newNode;
}
}
switch(method)
{
case ShelfNextFit:
if (FitsOnShelf(shelves.back(), width, height, true))
{
AddToShelf(shelves.back(), width, height, newNode);
return newNode;
}
break;
case ShelfFirstFit:
for(size_t i = 0; i < shelves.size(); ++i)
if (FitsOnShelf(shelves[i], width, height, i == shelves.size()-1))
{
AddToShelf(shelves[i], width, height, newNode);
return newNode;
}
break;
case ShelfBestAreaFit:
{
// Best Area Fit rule: Choose the shelf with smallest remaining shelf area.
Shelf *bestShelf = 0;
unsigned long bestShelfSurfaceArea = (unsigned long)-1;
for(size_t i = 0; i < shelves.size(); ++i)
{
// Pre-rotate the rect onto the shelf here already so that the area fit computation
// is done correctly.
RotateToShelf(shelves[i], width, height);
if (FitsOnShelf(shelves[i], width, height, i == shelves.size()-1))
{
unsigned long surfaceArea = (binWidth - shelves[i].currentX) * shelves[i].height;
if (surfaceArea < bestShelfSurfaceArea)
{
bestShelf = &shelves[i];
bestShelfSurfaceArea = surfaceArea;
}
}
}
if (bestShelf)
{
AddToShelf(*bestShelf, width, height, newNode);
return newNode;
}
}
break;
case ShelfWorstAreaFit:
{
// Worst Area Fit rule: Choose the shelf with smallest remaining shelf area.
Shelf *bestShelf = 0;
int bestShelfSurfaceArea = -1;
for(size_t i = 0; i < shelves.size(); ++i)
{
// Pre-rotate the rect onto the shelf here already so that the area fit computation
// is done correctly.
RotateToShelf(shelves[i], width, height);
if (FitsOnShelf(shelves[i], width, height, i == shelves.size()-1))
{
int surfaceArea = (binWidth - shelves[i].currentX) * shelves[i].height;
if (surfaceArea > bestShelfSurfaceArea)
{
bestShelf = &shelves[i];
bestShelfSurfaceArea = surfaceArea;
}
}
}
if (bestShelf)
{
AddToShelf(*bestShelf, width, height, newNode);
return newNode;
}
}
break;
case ShelfBestHeightFit:
{
// Best Height Fit rule: Choose the shelf with best-matching height.
Shelf *bestShelf = 0;
int bestShelfHeightDifference = 0x7FFFFFFF;
for(size_t i = 0; i < shelves.size(); ++i)
{
// Pre-rotate the rect onto the shelf here already so that the height fit computation
// is done correctly.
RotateToShelf(shelves[i], width, height);
if (FitsOnShelf(shelves[i], width, height, i == shelves.size()-1))
{
int heightDifference = max(shelves[i].height - height, 0);
assert(heightDifference >= 0);
if (heightDifference < bestShelfHeightDifference)
{
bestShelf = &shelves[i];
bestShelfHeightDifference = heightDifference;
}
}
}
if (bestShelf)
{
AddToShelf(*bestShelf, width, height, newNode);
return newNode;
}
}
break;
case ShelfBestWidthFit:
{
// Best Width Fit rule: Choose the shelf with smallest remaining shelf width.
Shelf *bestShelf = 0;
int bestShelfWidthDifference = 0x7FFFFFFF;
for(size_t i = 0; i < shelves.size(); ++i)
{
// Pre-rotate the rect onto the shelf here already so that the height fit computation
// is done correctly.
RotateToShelf(shelves[i], width, height);
if (FitsOnShelf(shelves[i], width, height, i == shelves.size()-1))
{
int widthDifference = binWidth - shelves[i].currentX - width;
assert(widthDifference >= 0);
if (widthDifference < bestShelfWidthDifference)
{
bestShelf = &shelves[i];
bestShelfWidthDifference = widthDifference;
}
}
}
if (bestShelf)
{
AddToShelf(*bestShelf, width, height, newNode);
return newNode;
}
}
break;
case ShelfWorstWidthFit:
{
// Worst Width Fit rule: Choose the shelf with smallest remaining shelf width.
Shelf *bestShelf = 0;
int bestShelfWidthDifference = -1;
for(size_t i = 0; i < shelves.size(); ++i)
{
// Pre-rotate the rect onto the shelf here already so that the height fit computation
// is done correctly.
RotateToShelf(shelves[i], width, height);
if (FitsOnShelf(shelves[i], width, height, i == shelves.size()-1))
{
int widthDifference = binWidth - shelves[i].currentX - width;
assert(widthDifference >= 0);
if (widthDifference > bestShelfWidthDifference)
{
bestShelf = &shelves[i];
bestShelfWidthDifference = widthDifference;
}
}
}
if (bestShelf)
{
AddToShelf(*bestShelf, width, height, newNode);
return newNode;
}
}
break;
}
// The rectangle did not fit on any of the shelves. Open a new shelf.
// Flip the rectangle so that the long side is horizontal.
if (width < height && height <= binWidth)
swap(width, height);
if (CanStartNewShelf(height))
{
if (useWasteMap)
MoveShelfToWasteMap(shelves.back());
StartNewShelf(height);
assert(FitsOnShelf(shelves.back(), width, height, true));
AddToShelf(shelves.back(), width, height, newNode);
return newNode;
}
/*
///\todo This is problematic: If we couldn't start a new shelf - should we give up
/// and move all the remaining space of the bin for the waste map to track,
/// or should we just wait if the next rectangle would fit better? For now,
/// don't add the leftover space to the waste map.
else if (useWasteMap)
{
assert(binHeight - shelves.back().startY >= shelves.back().height);
shelves.back().height = binHeight - shelves.back().startY;
if (shelves.back().height > 0)
MoveShelfToWasteMap(shelves.back());
// Try to pack the rectangle again to the waste map.
GuillotineBinPack::Node node = wasteMap.Insert(width, height, true, 1, 3);
if (node.height != 0)
{
newNode.x = node.x;
newNode.y = node.y;
newNode.width = node.width;
newNode.height = node.height;
return newNode;
}
}
*/
// The rectangle didn't fit.
memset(&newNode, 0, sizeof(Rect));
return newNode;
}
void ShelfBinPack::MoveShelfToWasteMap(Shelf &shelf)
{
std::vector<Rect> &freeRects = wasteMap.GetFreeRectangles();
// Add the gaps between each rect top and shelf ceiling to the waste map.
for(size_t i = 0; i < shelf.usedRectangles.size(); ++i)
{
const Rect &r = shelf.usedRectangles[i];
Rect newNode;
newNode.x = r.x;
newNode.y = r.y + r.height;
newNode.width = r.width;
newNode.height = shelf.height - r.height;
if (newNode.height > 0)
freeRects.push_back(newNode);
}
shelf.usedRectangles.clear();
// Add the space after the shelf end (right side of the last rect) and the shelf right side.
Rect newNode;
newNode.x = shelf.currentX;
newNode.y = shelf.startY;
newNode.width = binWidth - shelf.currentX;
newNode.height = shelf.height;
if (newNode.width > 0)
freeRects.push_back(newNode);
// This shelf is DONE.
shelf.currentX = binWidth;
// Perform a rectangle merge step.
wasteMap.MergeFreeList();
}
/// Computes the ratio of used surface area to the bin area.
float ShelfBinPack::Occupancy() const
{
return (float)usedSurfaceArea / (binWidth * binHeight);
}
}