forked from dvdoug/BoxPacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VolumePacker.php
347 lines (307 loc) · 11.4 KB
/
VolumePacker.php
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
<?php
/**
* Box packing (3D bin packing, knapsack problem)
* @package BoxPacker
* @author Doug Wright
*/
namespace DVDoug\BoxPacker;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
/**
* Actual packer
* @author Doug Wright
* @package BoxPacker
*/
class VolumePacker implements LoggerAwareInterface
{
use LoggerAwareTrait;
/**
* Box to pack items into
* @var Box
*/
protected $box;
/**
* @var int
*/
protected $boxWidth;
/**
* @var int
*/
protected $boxLength;
/**
* List of items to be packed
* @var ItemList
*/
protected $items;
/**
* List of items to be packed
* @var ItemList
*/
protected $skippedItems;
/**
* Remaining weight capacity of the box
* @var int
*/
protected $remainingWeight;
/**
* Whether the box was rotated for packing
* @var bool
*/
protected $boxRotated = false;
/**
* Constructor
*
* @param Box $box
* @param ItemList $items
*/
public function __construct(Box $box, ItemList $items)
{
$this->box = $box;
$this->items = $items;
$this->boxWidth = max($this->box->getInnerWidth(), $this->box->getInnerLength());
$this->boxLength = min($this->box->getInnerWidth(), $this->box->getInnerLength());
$this->remainingWeight = $this->box->getMaxWeight() - $this->box->getEmptyWeight();
$this->skippedItems = new ItemList();
$this->logger = new NullLogger();
// we may have just rotated the box for packing purposes, record if we did
if ($this->box->getInnerWidth() != $this->boxWidth || $this->box->getInnerLength() != $this->boxLength) {
$this->boxRotated = true;
}
}
/**
* Pack as many items as possible into specific given box
*
* @return PackedBox packed box
*/
public function pack()
{
$this->logger->debug("[EVALUATING BOX] {$this->box->getReference()}");
$packedItems = new PackedItemList;
$prevItem = null;
$x = $y = $z = $rowWidth = $rowLength = $layerWidth = $layerLength = $layerDepth = 0;
$packingWidthLeft = $this->boxWidth;
$packingLengthLeft = $this->boxLength;
$packingDepthLeft = $this->box->getInnerDepth();
while (!$this->items->isEmpty()) {
$itemToPack = $this->items->extract();
$isLastItem = $this->skippedItems->isEmpty() && $this->items->isEmpty();
//skip items that are simply too heavy or too large
if (!$this->checkConstraints($itemToPack, $packedItems)) {
$this->rebuildItemList();
continue;
}
$orientatedItem = $this->getOrientationForItem($itemToPack, $prevItem, $isLastItem, $packingWidthLeft, $packingLengthLeft, $packingDepthLeft);
if ($orientatedItem instanceof OrientatedItem) {
$packedItem = PackedItem::fromOrientatedItem($orientatedItem, $x, $y, $z);
$packedItems->insert($packedItem);
$this->remainingWeight -= $orientatedItem->getItem()->getWeight();
$packingWidthLeft -= $orientatedItem->getWidth();
$rowWidth += $orientatedItem->getWidth();
$rowLength = max($rowLength, $orientatedItem->getLength());
$layerDepth = max($layerDepth, $orientatedItem->getDepth());
//allow items to be stacked in place within the same footprint up to current layer depth
$stackableDepth = $layerDepth - $orientatedItem->getDepth();
$this->tryAndStackItemsIntoSpace($packedItems, $prevItem, $orientatedItem->getWidth(), $orientatedItem->getLength(), $stackableDepth, $x, $y, $z + $orientatedItem->getDepth());
$x += $orientatedItem->getWidth();
$prevItem = $packedItem;
$this->rebuildItemList();
} else {
if ($layerWidth == 0 && $layerDepth == 0) { // zero items on layer
$this->logger->debug("doesn't fit on layer even when empty, skipping for good");
$prevItem = null;
continue;
} elseif (!$this->items->isEmpty()) { // skip for now, move on to the next item
$this->logger->debug("doesn't fit, skipping for now");
$this->skippedItems->insert($itemToPack);
} elseif ($x > 0 && $packingLengthLeft >= min($itemToPack->getWidth(), $itemToPack->getLength())) {
$this->logger->debug("No more fit in width wise, resetting for new row");
$layerWidth = max($layerWidth, $rowWidth);
$layerLength += $rowLength;
$packingWidthLeft += $rowWidth;
$packingLengthLeft -= $rowLength;
$y += $rowLength;
$x = $rowWidth = $rowLength = 0;
$this->rebuildItemList();
$this->items->insert($itemToPack);
$prevItem = null;
continue;
} else {
$this->logger->debug("no items fit, so starting next vertical layer");
$layerWidth = max($layerWidth, $rowWidth);
$layerLength += $rowLength;
$packingWidthLeft = $rowWidth ? min(floor($layerWidth * 1.1), $this->boxWidth) : $this->boxWidth;
$packingLengthLeft = $rowLength ? min(floor($layerLength * 1.1), $this->boxLength) : $this->boxLength;
$packingDepthLeft -= $layerDepth;
$z += $layerDepth;
$x = $y = $rowWidth = $rowLength = $layerWidth = $layerLength = $layerDepth = 0;
$this->rebuildItemList();
$this->items->insert($itemToPack);
$prevItem = null;
}
}
}
$this->logger->debug("done with this box");
return $this->createPackedBox($packedItems);
}
/**
* @param Item $itemToPack
* @param PackedItem|null $prevItem
* @param bool $isLastItem
* @param int $maxWidth
* @param int $maxLength
* @param int $maxDepth
*
* @return OrientatedItem|false
*/
protected function getOrientationForItem(
Item $itemToPack,
PackedItem $prevItem = null,
$isLastItem,
$maxWidth,
$maxLength,
$maxDepth
) {
$this->logger->debug(
"evaluating item {$itemToPack->getDescription()} for fit",
[
'item' => $itemToPack,
'space' => [
'maxWidth' => $maxWidth,
'maxLength' => $maxLength,
'maxDepth' => $maxDepth,
],
]
);
$orientatedItemFactory = new OrientatedItemFactory();
$orientatedItemFactory->setLogger($this->logger);
$orientatedItem = $orientatedItemFactory->getBestOrientation($this->box, $itemToPack, $prevItem, $isLastItem, $maxWidth, $maxLength, $maxDepth);
return $orientatedItem;
}
/**
* Figure out if we can stack the next item vertically on top of this rather than side by side
* Used when we've packed a tall item, and have just put a shorter one next to it
*
* @param PackedItemList $packedItems
* @param PackedItem|null $prevItem
* @param int $maxWidth
* @param int $maxLength
* @param int $maxDepth
* @param int $x
* @param int $y
* @param int $z
*/
protected function tryAndStackItemsIntoSpace(
PackedItemList $packedItems,
PackedItem $prevItem = null,
$maxWidth,
$maxLength,
$maxDepth,
$x,
$y,
$z
) {
while (!$this->items->isEmpty() && $this->checkNonDimensionalConstraints($this->items->top(), $packedItems)) {
$stackedItem = $this->getOrientationForItem(
$this->items->top(),
$prevItem,
$this->items->count() === 1,
$maxWidth,
$maxLength,
$maxDepth
);
if ($stackedItem) {
$this->remainingWeight -= $this->items->top()->getWeight();
$packedItems->insert(PackedItem::fromOrientatedItem($stackedItem, $x, $y, $z));
$this->items->extract();
$maxDepth -= $stackedItem->getDepth();
$z += $stackedItem->getDepth();
} else {
break;
}
}
}
/**
* Check item generally fits into box
*
* @param Item $itemToPack
* @param PackedItemList $packedItems
*
* @return bool
*/
protected function checkConstraints(
Item $itemToPack,
PackedItemList $packedItems
) {
return $this->checkNonDimensionalConstraints($itemToPack, $packedItems) &&
$this->checkDimensionalConstraints($itemToPack);
}
/**
* As well as purely dimensional constraints, there are other constraints that need to be met
* e.g. weight limits or item-specific restrictions (e.g. max <x> batteries per box)
*
* @param Item $itemToPack
* @param PackedItemList $packedItems
*
* @return bool
*/
protected function checkNonDimensionalConstraints(Item $itemToPack, PackedItemList $packedItems)
{
$weightOK = $itemToPack->getWeight() <= $this->remainingWeight;
if ($itemToPack instanceof ConstrainedItem) {
return $weightOK && $itemToPack->canBePackedInBox($packedItems->asItemList(), $this->box);
}
return $weightOK;
}
/**
* Check the item physically fits in the box (at all)
*
* @param Item $itemToPack
*
* @return bool
*/
protected function checkDimensionalConstraints(Item $itemToPack)
{
$orientatedItemFactory = new OrientatedItemFactory();
$orientatedItemFactory->setLogger($this->logger);
return !!$orientatedItemFactory->getPossibleOrientationsInEmptyBox($itemToPack, $this->box);
}
/**
* Reintegrate skipped items into main list when nothing left to process
*/
protected function rebuildItemList() {
if ($this->items->isEmpty()) {
$this->items = $this->skippedItems;
$this->skippedItems = new ItemList();
}
}
/**
* @param PackedItemList $packedItems
*
* @return PackedBox
*/
protected function createPackedBox(PackedItemList $packedItems)
{
//if we rotated the box for packing, need to swap back width/length of the packed items
if ($this->boxRotated) {
$items = iterator_to_array($packedItems);
$packedItems = new PackedItemList();
/** @var PackedItem $item */
foreach($items as $item) {
$packedItems->insert(
new PackedItem(
$item->getItem(),
$item->getY(),
$item->getX(),
$item->getZ(),
$item->getLength(),
$item->getWidth(),
$item->getDepth()
)
);
}
}
return PackedBox::fromPackedItemList($this->box, $packedItems);
}
}