forked from phux/BoxPacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VolumePacker.php
257 lines (212 loc) · 9.48 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
<?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\NullLogger;
/**
* Actual packer
* @author Doug Wright
* @package BoxPacker
*/
class VolumePacker implements LoggerAwareInterface
{
use LoggerAwareTrait;
/**
* Box to pack items into
* @var Box
*/
protected $box;
/**
* List of items to be packed
* @var ItemList
*/
protected $items;
/**
* Remaining width of the box to pack items into
* @var int
*/
protected $widthLeft;
/**
* Remaining length of the box to pack items into
* @var int
*/
protected $lengthLeft;
/**
* Remaining depth of the box to pack items into
* @var int
*/
protected $depthLeft;
/**
* Remaining weight capacity of the box
* @var int
*/
protected $remainingWeight;
/**
* Constructor
*/
public function __construct(Box $box, ItemList $items)
{
$this->logger = new NullLogger();
$this->box = $box;
$this->items = $items;
$this->depthLeft = $this->box->getInnerDepth();
$this->remainingWeight = $this->box->getMaxWeight() - $this->box->getEmptyWeight();
$this->widthLeft = $this->box->getInnerWidth();
$this->lengthLeft = $this->box->getInnerLength();
}
/**
* 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 ItemList;
$layerWidth = $layerLength = $layerDepth = 0;
$prevItem = null;
while (!$this->items->isEmpty()) {
$itemToPack = $this->items->extract();
//skip items that are simply too heavy
if ($itemToPack->getWeight() > $this->remainingWeight) {
continue;
}
$this->logger->debug("evaluating item {$itemToPack->getDescription()}");
$this->logger->debug("remaining width: {$this->widthLeft}, length: {$this->lengthLeft}, depth: {$this->depthLeft}");
$this->logger->debug("layerWidth: {$layerWidth}, layerLength: {$layerLength}, layerDepth: {$layerDepth}");
$nextItem = !$this->items->isEmpty() ? $this->items->top() : null;
$orientatedItem = $this->findBestOrientation($itemToPack, $prevItem, $nextItem, $this->widthLeft, $this->lengthLeft, $this->depthLeft);
if ($orientatedItem) {
$packedItems->insert($orientatedItem->getItem());
$this->remainingWeight -= $itemToPack->getWeight();
$this->lengthLeft -= $orientatedItem->getLength();
$layerLength += $orientatedItem->getLength();
$layerWidth = max($orientatedItem->getWidth(), $layerWidth);
$layerDepth = max($layerDepth, $orientatedItem->getDepth()); //greater than 0, items will always be less deep
//allow items to be stacked in place within the same footprint up to current layerdepth
$stackableDepth = $layerDepth - $orientatedItem->getDepth();
$this->tryAndStackItemsIntoSpace($packedItems, $orientatedItem->getWidth(), $orientatedItem->getLength(), $stackableDepth);
$prevItem = $orientatedItem;
} else {
$prevItem = null;
if ($this->widthLeft >= min($itemToPack->getWidth(), $itemToPack->getLength()) && $this->isLayerStarted($layerWidth, $layerLength, $layerDepth)) {
$this->logger->debug("No more fit in lengthwise, resetting for new row");
$this->lengthLeft += $layerLength;
$this->widthLeft -= $layerWidth;
$layerWidth = $layerLength = 0;
$this->items->insert($itemToPack);
continue;
} elseif ($this->lengthLeft < min($itemToPack->getWidth(), $itemToPack->getLength()) || $layerDepth == 0) {
$this->logger->debug("doesn't fit on layer even when empty");
continue;
}
$this->widthLeft = $layerWidth ? min(floor($layerWidth * 1.1), $this->box->getInnerWidth()) : $this->box->getInnerWidth();
$this->lengthLeft = $layerLength ? min(floor($layerLength * 1.1), $this->box->getInnerLength()) : $this->box->getInnerLength();
$this->depthLeft -= $layerDepth;
$layerWidth = $layerLength = $layerDepth = 0;
$this->logger->debug("doesn't fit, so starting next vertical layer");
$this->items->insert($itemToPack);
}
}
$this->logger->debug("done with this box");
return new PackedBox($this->box, $packedItems, $this->widthLeft, $this->lengthLeft, $this->depthLeft, $this->remainingWeight);
}
/**
* Get the best orientation for an item
* @param Item $item
* @param OrientatedItem|null $prevItem
* @param Item|null $nextItem
* @param int $widthLeft
* @param int $lengthLeft
* @param int $depthLeft
* @return OrientatedItem|false
*/
protected function findBestOrientation(Item $item, OrientatedItem $prevItem = null, Item $nextItem = null, $widthLeft, $lengthLeft, $depthLeft) {
$orientations = $this->findPossibleOrientations($item, $prevItem, $widthLeft, $lengthLeft, $depthLeft);
// special casing based on next item
if (isset($orientations[0]) && $nextItem == $item && $lengthLeft >= 2 * $item->getLength()) {
$this->logger->debug("not rotating based on next item");
return $orientations[0]; // XXX this is tied to the ordering from ->findPossibleOrientations()
}
$orientationFits = [];
/** @var OrientatedItem $orientation */
foreach ($orientations as $o => $orientation) {
$orientationFit = min($widthLeft - $orientation->getWidth(), $lengthLeft - $orientation->getLength());
$orientationFits[$o] = $orientationFit;
}
if (!empty($orientationFits)) {
asort($orientationFits);
reset($orientationFits);
$bestFit = key($orientationFits);
$this->logger->debug("Using orientation #{$bestFit}");
return $orientations[$bestFit];
} else {
return false;
}
}
/**
* Find all possible orientations for an item
* @param Item $item
* @param OrientatedItem|null $prevItem
* @param int $widthLeft
* @param int $lengthLeft
* @param int $depthLeft
* @return OrientatedItem[]
*/
protected function findPossibleOrientations(Item $item, OrientatedItem $prevItem = null, $widthLeft, $lengthLeft, $depthLeft) {
$orientations = [];
//Special case items that are the same as what we just packed - keep orientation
if ($prevItem && $prevItem->getItem() == $item) {
$orientations[] = new OrientatedItem($item, $prevItem->getWidth(), $prevItem->getLength(), $prevItem->getDepth());
} else {
//simple 2D rotation
$orientations[] = new OrientatedItem($item, $item->getWidth(), $item->getLength(), $item->getDepth());
$orientations[] = new OrientatedItem($item, $item->getLength(), $item->getWidth(), $item->getDepth());
//add 3D rotation if we're allowed
if (!$item->getKeepFlat()) {
$orientations[] = new OrientatedItem($item, $item->getWidth(), $item->getDepth(), $item->getLength());
$orientations[] = new OrientatedItem($item, $item->getLength(), $item->getDepth(), $item->getWidth());
$orientations[] = new OrientatedItem($item, $item->getDepth(), $item->getWidth(), $item->getLength());
$orientations[] = new OrientatedItem($item, $item->getDepth(), $item->getLength(), $item->getWidth());
}
}
//remove any that simply don't fit
return array_filter($orientations, function (OrientatedItem $i) use ($widthLeft, $lengthLeft, $depthLeft) {
return $i->getWidth() <= $widthLeft && $i->getLength() <= $lengthLeft && $i->getDepth() <= $depthLeft;
});
}
/**
* 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 ItemList $packedItems
* @param int $maxWidth
* @param int $maxLength
* @param int $maxDepth
*/
protected function tryAndStackItemsIntoSpace(ItemList $packedItems, $maxWidth, $maxLength, $maxDepth)
{
while (!$this->items->isEmpty() && $this->remainingWeight >= $this->items->top()->getWeight()) {
$stackedItem = $this->findBestOrientation($this->items->top(), null, null, $maxWidth, $maxLength, $maxDepth);
if ($stackedItem) {
$this->remainingWeight -= $this->items->top()->getWeight();
$maxDepth -= $stackedItem->getDepth();
$packedItems->insert($this->items->extract());
} else {
break;
}
}
}
/**
* @param int $layerWidth
* @param int $layerLength
* @param int $layerDepth
* @return bool
*/
protected function isLayerStarted($layerWidth, $layerLength, $layerDepth) {
return $layerWidth > 0 && $layerLength > 0 && $layerDepth > 0;
}
}