Completed
Push — 2.x-dev ( 5f5b4d...48a1b4 )
by Doug
61:10 queued 51:42
created

PackedBox::getInnerVolume()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem).
4
 *
5
 * @author Doug Wright
6
 */
7
8
namespace DVDoug\BoxPacker;
9
10
/**
11
 * A "box" with items.
12
 *
13
 * @author Doug Wright
14
 */
15
class PackedBox
16
{
17
    /**
18
     * Box used.
19
     *
20
     * @var Box
21
     */
22
    protected $box;
23
24
    /**
25
     * Items in the box.
26
     *
27
     * @var ItemList
28
     */
29
    protected $items;
30
31
    /**
32
     * Total weight of box.
33
     *
34
     * @var int
35
     */
36
    protected $weight;
37
38
    /**
39
     * Remaining width inside box for another item.
40
     *
41
     * @var int
42
     */
43
    protected $remainingWidth;
44
45
    /**
46
     * Remaining length inside box for another item.
47
     *
48
     * @var int
49
     */
50
    protected $remainingLength;
51
52
    /**
53
     * Remaining depth inside box for another item.
54
     *
55
     * @var int
56
     */
57
    protected $remainingDepth;
58
59
    /**
60
     * Remaining weight inside box for another item.
61
     *
62
     * @var int
63
     */
64
    protected $remainingWeight;
65
66
    /**
67
     * Used width inside box for packing items.
68
     *
69
     * @var int
70
     */
71
    protected $usedWidth;
72
73
    /**
74
     * Used length inside box for packing items.
75
     *
76
     * @var int
77
     */
78
    protected $usedLength;
79
80
    /**
81
     * Used depth inside box for packing items.
82
     *
83
     * @var int
84
     */
85
    protected $usedDepth;
86
87
    /**
88
     * Get box used.
89
     *
90
     * @return Box
91
     */
92 6
    public function getBox()
93
    {
94 6
        return $this->box;
95
    }
96
97
    /**
98
     * Get items packed.
99
     *
100
     * @return ItemList
101
     */
102 21
    public function getItems()
103
    {
104 21
        return $this->items;
105
    }
106
107
    /**
108
     * Get packed weight.
109
     *
110
     * @return int weight in grams
111
     */
112 3
    public function getWeight()
113
    {
114 3
        if (!is_null($this->weight)) {
115 1
            return $this->weight;
116
        }
117
118 3
        $this->weight = $this->box->getEmptyWeight();
119 3
        $items = clone $this->items;
120 3
        foreach ($items as $item) {
121 3
            $this->weight += $item->getWeight();
122
        }
123
124 3
        return $this->weight;
125
    }
126
127
    /**
128
     * Get remaining width inside box for another item.
129
     *
130
     * @return int
131
     */
132 1
    public function getRemainingWidth()
133
    {
134 1
        return $this->remainingWidth;
135
    }
136
137
    /**
138
     * Get remaining length inside box for another item.
139
     *
140
     * @return int
141
     */
142 1
    public function getRemainingLength()
143
    {
144 1
        return $this->remainingLength;
145
    }
146
147
    /**
148
     * Get remaining depth inside box for another item.
149
     *
150
     * @return int
151
     */
152 1
    public function getRemainingDepth()
153
    {
154 1
        return $this->remainingDepth;
155
    }
156
157
    /**
158
     * Used width inside box for packing items.
159
     *
160
     * @return int
161
     */
162
    public function getUsedWidth()
163
    {
164
        return $this->usedWidth;
165
    }
166
167
    /**
168
     * Used length inside box for packing items.
169
     *
170
     * @return int
171
     */
172
    public function getUsedLength()
173
    {
174
        return $this->usedLength;
175
    }
176
177
    /**
178
     * Used depth inside box for packing items.
179
     *
180
     * @return int
181
     */
182
    public function getUsedDepth()
183
    {
184
        return $this->usedDepth;
185
    }
186
187
    /**
188
     * Get remaining weight inside box for another item.
189
     *
190
     * @return int
191
     */
192 1
    public function getRemainingWeight()
193
    {
194 1
        return $this->remainingWeight;
195
    }
196
197
    /**
198
     * @return int
199
     */
200 2
    public function getInnerVolume()
201
    {
202 2
        return $this->box->getInnerWidth() * $this->box->getInnerLength() * $this->box->getInnerDepth();
203
    }
204
205
    /**
206
     * Get volume utilisation of the packed box.
207
     *
208
     * @return float
209
     */
210 1
    public function getVolumeUtilisation()
211
    {
212 1
        $itemVolume = 0;
213
214
        /** @var Item $item */
215 1
        foreach (clone $this->items as $item) {
216 1
            $itemVolume += $item->getVolume();
217
        }
218
219 1
        return round($itemVolume / $this->box->getInnerVolume() * 100, 1);
220
    }
221
222
    /**
223
     * Legacy constructor.
224
     *
225
     * @deprecated
226
     *
227
     * @param Box      $box
228
     * @param ItemList $itemList
229
     * @param int      $remainingWidth
230
     * @param int      $remainingLength
231
     * @param int      $remainingDepth
232
     * @param int      $remainingWeight
233
     * @param int      $usedWidth
234
     * @param int      $usedLength
235
     * @param int      $usedDepth
236
     */
237 23
    public function __construct(
238
        Box $box,
239
        ItemList $itemList,
240
        $remainingWidth,
241
        $remainingLength,
242
        $remainingDepth,
243
        $remainingWeight,
244
        $usedWidth,
245
        $usedLength,
246
        $usedDepth
247
    ) {
248 23
        $this->box = $box;
249 23
        $this->items = $itemList;
250 23
        $this->remainingWidth = $remainingWidth;
251 23
        $this->remainingLength = $remainingLength;
252 23
        $this->remainingDepth = $remainingDepth;
253 23
        $this->remainingWeight = $remainingWeight;
254 23
        $this->usedWidth = $usedWidth;
255 23
        $this->usedLength = $usedLength;
256 23
        $this->usedDepth = $usedDepth;
257 23
    }
258
259
    /**
260
     * The constructor from v3.
261
     *
262
     * @param Box            $box
263
     * @param PackedItemList $packedItems
264
     */
265 20
    public static function fromPackedItemList(Box $box, PackedItemList $packedItems)
266
    {
267 20
        $maxWidth = $maxLength = $maxDepth = $weight = 0;
268
        /** @var PackedItem $item */
269 20
        foreach (clone $packedItems as $item) {
270 20
            $maxWidth = max($maxWidth, $item->getX() + $item->getWidth());
271 20
            $maxLength = max($maxLength, $item->getY() + $item->getLength());
272 20
            $maxDepth = max($maxDepth, $item->getZ() + $item->getDepth());
273 20
            $weight += $item->getItem()->getWeight();
274
        }
275
276 20
        $packedBox = new self(
277 20
            $box,
278 20
            $packedItems->asItemList(),
279 20
            $box->getInnerWidth() - $maxWidth,
280 20
            $box->getInnerLength() - $maxLength,
281 20
            $box->getInnerDepth() - $maxDepth,
282 20
            $box->getMaxWeight() - $box->getEmptyWeight() - $weight,
283 20
            $maxWidth,
284 20
            $maxLength,
285 20
            $maxDepth
286
        );
287
288 20
        return $packedBox;
289
    }
290
}
291