Completed
Push — master ( 534437...cd15bf )
by Doug
83:28 queued 35:58
created

VolumePacker   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 198
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 95.7%

Importance

Changes 9
Bugs 2 Features 2
Metric Value
c 9
b 2
f 2
dl 0
loc 198
wmc 32
lcom 1
cbo 8
ccs 89
cts 93
cp 0.957
rs 9.6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C pack() 0 78 13
C findBestOrientation() 0 51 11
A canStackItemInLayer() 0 7 4
A isLayerStarted() 0 3 3
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
     * Get the best orientation for an item
130
     * @param Item $item
131
     * @param OrientatedItem|null $prevItem
132
     * @param Item|null $nextItem
133
     * @param int $widthLeft
134
     * @param int $lengthLeft
135
     * @param int $depthLeft
136
     * @return OrientatedItem|false
137
     */
138
    protected function findBestOrientation(Item $item, OrientatedItem $prevItem = null, Item $nextItem = null, $widthLeft, $lengthLeft, $depthLeft) {
139
140
        $orientations = [];
141
142
        //Special case items that are the same as what we just packed - keep orientation
143
        if ($prevItem && $prevItem->getItem() == $item) {
144
            $orientations[] = new OrientatedItem($item, $prevItem->getWidth(), $prevItem->getLength(), $prevItem->getDepth());
145
        } else {
146
147
            //simple 2D rotation
148
            $orientations[] = new OrientatedItem($item, $item->getWidth(), $item->getLength(), $item->getDepth());
149
            $orientations[] = new OrientatedItem($item, $item->getLength(), $item->getWidth(), $item->getDepth());
150
151
            //add 3D rotation if we're allowed
152
            if (!$item->getKeepFlat()) {
153
                $orientations[] = new OrientatedItem($item, $item->getWidth(), $item->getDepth(), $item->getLength());
154
                $orientations[] = new OrientatedItem($item, $item->getLength(), $item->getDepth(), $item->getWidth());
155
                $orientations[] = new OrientatedItem($item, $item->getDepth(), $item->getWidth(), $item->getLength());
156
                $orientations[] = new OrientatedItem($item, $item->getDepth(), $item->getLength(), $item->getWidth());
157
            }
158
        }
159
160 26
        $orientationFits = [];
161
162 26
        /** @var OrientatedItem $orientation */
163
        foreach ($orientations as $o => $orientation) {
164
            $orientationFit = min($widthLeft   - $orientation->getWidth(),
165 26
                                  $lengthLeft  - $orientation->getLength());
166 9
167 9
            if ($orientationFit >= 0 && $depthLeft - $orientation->getDepth() >= 0) {
168
                $orientationFits[$o] = $orientationFit;
169
            }
170 26
        }
171 26
172
        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...
173
174 26
            //special casing based on next item
175 10
            if (isset($orientationFits[0]) && $nextItem == $item && $lengthLeft >= 2 * $item->getLength()) {
176 10
                $this->logger->debug("not rotating based on next item");
177 10
                return $orientations[0];
178 10
            }
179 10
180
            asort($orientationFits);
181
            reset($orientationFits);
182 26
            $bestFit = key($orientationFits);
183
            $this->logger->debug("Using orientation #{$bestFit}");
184
            return $orientations[$bestFit];
185 26
        } else {
186 26
            return false;
187 26
        }
188
    }
189 26
190 26
    /**
191 26
     * Figure out if we can stack the next item vertically on top of this rather than side by side
192 26
     * Used when we've packed a tall item, and have just put a shorter one next to it
193
     * @param Item $item
194 26
     * @param Item $nextItem
195
     * @param $maxStackDepth
196
     * @param $remainingWeight
197 26
     * @return bool
198 5
     */
199 5
    protected function canStackItemInLayer(Item $item, Item $nextItem, $maxStackDepth, $remainingWeight)
200
    {
201
        return $nextItem->getDepth() <= $maxStackDepth &&
202 26
               $nextItem->getWeight() <= $remainingWeight &&
203 26
               $nextItem->getWidth() <= $item->getWidth() &&
204 26
               $nextItem->getLength() <= $item->getLength();
205 26
    }
206 26
207
    /**
208 23
     * @param $layerWidth
209
     * @param $layerLength
210
     * @param $layerDepth
211
     * @return bool
212
     */
213
    protected function isLayerStarted($layerWidth, $layerLength, $layerDepth) {
214
        return $layerWidth > 0 && $layerLength > 0 && $layerDepth > 0;
215
    }
216
}
217