Completed
Push — master ( 9d0a06...534437 )
by Doug
49:22
created

VolumePacker::canStackItemInLayer()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.2
cc 4
eloc 5
nc 4
nop 4
crap 4
1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem)
4
 * @package BoxPacker
5
 * @author Doug Wright
6
 */
7
namespace DVDoug\BoxPacker;
8
9
use Psr\Log\LoggerAwareInterface;
10
use Psr\Log\LoggerAwareTrait;
11
use Psr\Log\LogLevel;
12
use Psr\Log\NullLogger;
13
14
/**
15
 * Actual packer
16
 * @author Doug Wright
17
 * @package BoxPacker
18
 */
19
class VolumePacker implements LoggerAwareInterface
20
{
21
    use LoggerAwareTrait;
22
23
    /**
24
     * Box to pack items into
25
     * @var Box
26
     */
27
    protected $box;
28
29
    /**
30
     * List of items to be packed
31
     * @var ItemList
32
     */
33
    protected $items;
34
35
    /**
36
     * Constructor
37
     */
38 26
    public function __construct(Box $box, ItemList $items)
39
    {
40 26
        $this->box = $box;
41 26
        $this->items = $items;
42 26
        $this->logger = new NullLogger();
43 26
    }
44
45
    /**
46
     * Pack as many items as possible into specific given box
47
     * @return PackedBox packed box
48
     */
49 26
    public function pack()
50
    {
51 26
        $this->logger->debug("[EVALUATING BOX] {$this->box->getReference()}");
52
53 26
        $packedItems = new ItemList;
54 26
        $depthLeft = $this->box->getInnerDepth();
55 26
        $remainingWeight = $this->box->getMaxWeight() - $this->box->getEmptyWeight();
56 26
        $widthLeft = $this->box->getInnerWidth();
57 26
        $lengthLeft = $this->box->getInnerLength();
58
59 26
        $layerWidth = $layerLength = $layerDepth = 0;
60
61 26
        $prevItem = null;
62
63 26
        while (!$this->items->isEmpty()) {
64
65 26
            $itemToPack = $this->items->extract();
66
67
            //skip items that are simply too heavy
68 26
            if ($itemToPack->getWeight() > $remainingWeight) {
69 4
                continue;
70
            }
71
72 26
            $this->logger->debug("evaluating item {$itemToPack->getDescription()}");
73 26
            $this->logger->debug("remaining width: {$widthLeft}, length: {$lengthLeft}, depth: {$depthLeft}");
74 26
            $this->logger->debug("layerWidth: {$layerWidth}, layerLength: {$layerLength}, layerDepth: {$layerDepth}");
75
76 26
            $nextItem = !$this->items->isEmpty() ? $this->items->top() : null;
77 26
            $orientatedItem = $this->findBestOrientation($itemToPack, $prevItem, $nextItem, $widthLeft, $lengthLeft, $depthLeft);
78
79 26
            if ($orientatedItem) {
80
81 26
                $packedItems->insert($orientatedItem->getItem());
82 26
                $remainingWeight -= $itemToPack->getWeight();
83
84 26
                $lengthLeft -= $orientatedItem->getLength();
85 26
                $layerLength += $orientatedItem->getLength();
86 26
                $layerWidth = max($orientatedItem->getWidth(), $layerWidth);
87
88 26
                $layerDepth = max($layerDepth, $orientatedItem->getDepth()); //greater than 0, items will always be less deep
89
90
                //allow items to be stacked in place within the same footprint up to current layerdepth
91 26
                $maxStackDepth = $layerDepth - $orientatedItem->getDepth();
92 26
                while (!$this->items->isEmpty() && $this->canStackItemInLayer($itemToPack, $this->items->top(), $maxStackDepth, $remainingWeight)) {
93 1
                    $remainingWeight -= $this->items->top()->getWeight();
94 1
                    $maxStackDepth -= $this->items->top()->getDepth(); // XXX no attempt at best fit
95 1
                    $packedItems->insert($this->items->extract());
96 1
                }
97
98 26
                $prevItem = $orientatedItem;
99 26
            } else {
100
101 23
                $prevItem = null;
102
103 23
                if ($widthLeft >= min($itemToPack->getWidth(), $itemToPack->getLength()) && $this->isLayerStarted($layerWidth, $layerLength, $layerDepth)) {
104 22
                    $this->logger->debug("No more fit in lengthwise, resetting for new row");
105 22
                    $lengthLeft += $layerLength;
106 22
                    $widthLeft -= $layerWidth;
107 22
                    $layerWidth = $layerLength = 0;
108 22
                    $this->items->insert($itemToPack);
109 22
                    continue;
110 18
                } elseif ($lengthLeft < min($itemToPack->getWidth(), $itemToPack->getLength()) || $layerDepth == 0) {
111 7
                    $this->logger->debug("doesn't fit on layer even when empty");
112 7
                    continue;
113
                }
114
115 17
                $widthLeft = $layerWidth ? min(floor($layerWidth * 1.1), $this->box->getInnerWidth()) : $this->box->getInnerWidth();
116 17
                $lengthLeft = $layerLength ? min(floor($layerLength * 1.1), $this->box->getInnerLength()) : $this->box->getInnerLength();
117 17
                $depthLeft -= $layerDepth;
118
119 17
                $layerWidth = $layerLength = $layerDepth = 0;
120 17
                $this->logger->debug("doesn't fit, so starting next vertical layer");
121 17
                $this->items->insert($itemToPack);
122
            }
123 26
        }
124 26
        $this->logger->debug("done with this box");
125 26
        return new PackedBox($this->box, $packedItems, $widthLeft, $lengthLeft, $depthLeft, $remainingWeight);
126
    }
127
128
    /**
129
     * Figure out space left for next item if we pack this one in it's regular orientation
130
     * @param Item $item
131
     * @param int $widthLeft
132
     * @param int $lengthLeft
133
     * @return int
134
     */
135
    protected function fitsSameGap(Item $item, $widthLeft, $lengthLeft) {
136
        return min($widthLeft - $item->getWidth(), $lengthLeft - $item->getLength());
137
    }
138
139
    /**
140
     * Figure out space left for next item if we pack this one rotated by 90deg
141
     * @param Item $item
142
     * @param int $widthLeft
143
     * @param int $lengthLeft
144
     * @return int
145
     */
146
    protected function fitsRotatedGap(Item $item, $widthLeft, $lengthLeft) {
147
        return min($widthLeft - $item->getLength(), $lengthLeft - $item->getWidth());
148
    }
149
150
    /**
151
     * Get the best orientation for an item
152
     * @param Item $item
153
     * @param OrientatedItem|null $prevItem
154
     * @param Item|null $nextItem
155
     * @param int $widthLeft
156
     * @param int $lengthLeft
157
     * @param int $depthLeft
158
     * @return OrientatedItem|false
159
     */
160 26
    protected function findBestOrientation(Item $item, OrientatedItem $prevItem = null, Item $nextItem = null, $widthLeft, $lengthLeft, $depthLeft) {
161
162 26
        $orientations = [];
163
164
        //Special case items that are the same as what we just packed - keep orientation
165 26
        if ($prevItem && $prevItem->getItem() == $item) {
166 9
            $orientations[] = new OrientatedItem($item, $prevItem->getWidth(), $prevItem->getLength(), $prevItem->getDepth());
167 9
        } else {
168
169
            //simple 2D rotation
170 26
            $orientations[] = new OrientatedItem($item, $item->getWidth(), $item->getLength(), $item->getDepth());
171 26
            $orientations[] = new OrientatedItem($item, $item->getLength(), $item->getWidth(), $item->getDepth());
172
173
            //add 3D rotation if we're allowed
174 26
            if (!$item->getKeepFlat()) {
175 10
                $orientations[] = new OrientatedItem($item, $item->getWidth(), $item->getDepth(), $item->getLength());
176 10
                $orientations[] = new OrientatedItem($item, $item->getLength(), $item->getDepth(), $item->getWidth());
177 10
                $orientations[] = new OrientatedItem($item, $item->getDepth(), $item->getWidth(), $item->getLength());
178 10
                $orientations[] = new OrientatedItem($item, $item->getDepth(), $item->getLength(), $item->getWidth());
179 10
            }
180
        }
181
182 26
        $orientationFits = [];
183
184
        /** @var OrientatedItem $orientation */
185 26
        foreach ($orientations as $o => $orientation) {
186 26
            $orientationFit = min($widthLeft   - $orientation->getWidth(),
187 26
                                  $lengthLeft  - $orientation->getLength());
188
189 26
            if ($orientationFit >= 0 && $depthLeft - $orientation->getDepth() >= 0) {
190 26
                $orientationFits[$o] = $orientationFit;
191 26
            }
192 26
        }
193
194 26
        if ($orientationFits) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $orientationFits of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
195
196
            //special casing based on next item
197 26
            if (isset($orientationFits[0]) && $nextItem == $item && $lengthLeft >= 2 * $item->getLength()) {
198 5
                $this->logger->debug("not rotating based on next item");
199 5
                return $orientations[0];
200
            }
201
202 26
            asort($orientationFits);
203 26
            reset($orientationFits);
204 26
            $bestFit = key($orientationFits);
205 26
            $this->logger->debug("Using orientation #{$bestFit}");
206 26
            return $orientations[$bestFit];
207
        } else {
208 23
            return false;
209
        }
210
    }
211
212
    /**
213
     * Figure out if we can stack the next item vertically on top of this rather than side by side
214
     * Used when we've packed a tall item, and have just put a shorter one next to it
215
     * @param Item $item
216
     * @param Item $nextItem
217
     * @param $maxStackDepth
218
     * @param $remainingWeight
219
     * @return bool
220
     */
221 24
    protected function canStackItemInLayer(Item $item, Item $nextItem, $maxStackDepth, $remainingWeight)
222
    {
223 24
        return $nextItem->getDepth() <= $maxStackDepth &&
224 24
               $nextItem->getWeight() <= $remainingWeight &&
225 24
               $nextItem->getWidth() <= $item->getWidth() &&
226 24
               $nextItem->getLength() <= $item->getLength();
227
    }
228
229
    /**
230
     * @param $layerWidth
231
     * @param $layerLength
232
     * @param $layerDepth
233
     * @return bool
234
     */
235 23
    protected function isLayerStarted($layerWidth, $layerLength, $layerDepth) {
236 23
        return $layerWidth > 0 && $layerLength > 0 && $layerDepth > 0;
237
    }
238
}
239