Passed
Push — 1.x-dev ( cc1b76...09423a )
by Doug
02:10
created

PackedBox::getItemWeight()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
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
     * Total weight of items in the box.
40
     *
41
     * @var int
42
     */
43
    protected $itemWeight;
44
45
    /**
46
     * Remaining width inside box for another item.
47
     *
48
     * @var int
49
     */
50
    protected $remainingWidth;
51
52
    /**
53
     * Remaining length inside box for another item.
54
     *
55
     * @var int
56
     */
57
    protected $remainingLength;
58
59
    /**
60
     * Remaining depth inside box for another item.
61
     *
62
     * @var int
63
     */
64
    protected $remainingDepth;
65
66
    /**
67
     * Remaining weight inside box for another item.
68
     *
69
     * @var int
70
     */
71
    protected $remainingWeight;
72
73
    /**
74
     * Used width inside box for packing items.
75
     *
76
     * @var int
77
     */
78
    protected $usedWidth;
79
80
    /**
81
     * Used length inside box for packing items.
82
     *
83
     * @var int
84
     */
85
    protected $usedLength;
86
87
    /**
88
     * Used depth inside box for packing items.
89
     *
90
     * @var int
91
     */
92
    protected $usedDepth;
93
94
    /**
95
     * Get box used.
96
     *
97
     * @return Box
98
     */
99 6
    public function getBox()
100
    {
101 6
        return $this->box;
102
    }
103
104
    /**
105
     * Get items packed.
106
     *
107
     * @return ItemList
108
     */
109 20
    public function getItems()
110
    {
111 20
        return $this->items;
112
    }
113
114
    /**
115
     * Get packed weight.
116
     *
117
     * @return int weight in grams
118
     */
119 2
    public function getWeight()
120
    {
121 2
        return $this->box->getEmptyWeight() + $this->getItemWeight();
122
    }
123
124
    /**
125
     * Get packed weight of the items only.
126
     *
127
     * @return int weight in grams
128
     */
129 3
    public function getItemWeight()
130
    {
131 3
        if (!is_null($this->itemWeight)) {
0 ignored issues
show
introduced by
The condition ! is_null($this->itemWeight) can never be false.
Loading history...
132 1
            return $this->itemWeight;
133
        }
134 3
        $this->itemWeight = 0;
135
        /** @var Item $item */
136 3
        foreach (clone $this->items as $item) {
137 3
            $this->itemWeight += $item->getWeight();
138
        }
139
140 3
        return $this->itemWeight;
141
    }
142
143
    /**
144
     * Get remaining width inside box for another item.
145
     *
146
     * @return int
147
     */
148 1
    public function getRemainingWidth()
149
    {
150 1
        return $this->remainingWidth;
151
    }
152
153
    /**
154
     * Get remaining length inside box for another item.
155
     *
156
     * @return int
157
     */
158 1
    public function getRemainingLength()
159
    {
160 1
        return $this->remainingLength;
161
    }
162
163
    /**
164
     * Get remaining depth inside box for another item.
165
     *
166
     * @return int
167
     */
168 1
    public function getRemainingDepth()
169
    {
170 1
        return $this->remainingDepth;
171
    }
172
173
    /**
174
     * Used width inside box for packing items.
175
     *
176
     * @return int
177
     */
178
    public function getUsedWidth()
179
    {
180
        return $this->usedWidth;
181
    }
182
183
    /**
184
     * Used length inside box for packing items.
185
     *
186
     * @return int
187
     */
188
    public function getUsedLength()
189
    {
190
        return $this->usedLength;
191
    }
192
193
    /**
194
     * Used depth inside box for packing items.
195
     *
196
     * @return int
197
     */
198
    public function getUsedDepth()
199
    {
200
        return $this->usedDepth;
201
    }
202
203
    /**
204
     * Get remaining weight inside box for another item.
205
     *
206
     * @return int
207
     */
208 1
    public function getRemainingWeight()
209
    {
210 1
        return $this->remainingWeight;
211
    }
212
213
    /**
214
     * @return int
215
     */
216 2
    public function getInnerVolume()
217
    {
218 2
        return $this->box->getInnerWidth() * $this->box->getInnerLength() * $this->box->getInnerDepth();
219
    }
220
221
    /**
222
     * Get used volume of the packed box.
223
     *
224
     * @return int
225
     */
226 2
    public function getUsedVolume()
227
    {
228 2
        $volume = 0;
229
        /** @var PackedItem $item */
230 2
        foreach (clone $this->items as $item) {
231 2
            $volume += ($item->getWidth() * $item->getLength() * $item->getDepth());
232
        }
233
234 2
        return $volume;
235
    }
236
237
    /**
238
     * Get unused volume of the packed box.
239
     *
240
     * @return int
241
     */
242 1
    public function getUnusedVolume()
243
    {
244 1
        return $this->getInnerVolume() - $this->getUsedVolume();
245
    }
246
247
    /**
248
     * Get volume utilisation of the packed box.
249
     *
250
     * @return float
251
     */
252 2
    public function getVolumeUtilisation()
253
    {
254 2
        $itemVolume = 0;
255
256
        /** @var Item $item */
257 2
        foreach (clone $this->items as $item) {
258 2
            $itemVolume += $item->getVolume();
259
        }
260
261 2
        return round($itemVolume / $this->box->getInnerVolume() * 100, 1);
262
    }
263
264
    /**
265
     * Legacy constructor.
266
     *
267
     * @deprecated
268
     *
269
     * @param Box      $box
270
     * @param ItemList $itemList
271
     * @param int      $remainingWidth
272
     * @param int      $remainingLength
273
     * @param int      $remainingDepth
274
     * @param int      $remainingWeight
275
     * @param int      $usedWidth
276
     * @param int      $usedLength
277
     * @param int      $usedDepth
278
     */
279 22
    public function __construct(
280
        Box $box,
281
        ItemList $itemList,
282
        $remainingWidth,
283
        $remainingLength,
284
        $remainingDepth,
285
        $remainingWeight,
286
        $usedWidth,
287
        $usedLength,
288
        $usedDepth
289
    ) {
290 22
        $this->box = $box;
291 22
        $this->items = $itemList;
292 22
        $this->remainingWidth = $remainingWidth;
293 22
        $this->remainingLength = $remainingLength;
294 22
        $this->remainingDepth = $remainingDepth;
295 22
        $this->remainingWeight = $remainingWeight;
296 22
        $this->usedWidth = $usedWidth;
297 22
        $this->usedLength = $usedLength;
298 22
        $this->usedDepth = $usedDepth;
299 22
    }
300
301
    /**
302
     * The constructor from v3.
303
     *
304
     * @param Box            $box
305
     * @param PackedItemList $packedItems
306
     *
307
     * @return self
308
     */
309 22
    public static function fromPackedItemList(Box $box, PackedItemList $packedItems)
310
    {
311 22
        $maxWidth = $maxLength = $maxDepth = $weight = 0;
312
        /** @var PackedItem $item */
313 22
        foreach (clone $packedItems as $item) {
314 22
            $maxWidth = max($maxWidth, $item->getX() + $item->getWidth());
315 22
            $maxLength = max($maxLength, $item->getY() + $item->getLength());
316 22
            $maxDepth = max($maxDepth, $item->getZ() + $item->getDepth());
317 22
            $weight += $item->getItem()->getWeight();
318
        }
319
320 22
        $packedBox = new self(
321 22
            $box,
322 22
            $packedItems->asItemList(),
323 22
            $box->getInnerWidth() - $maxWidth,
324 22
            $box->getInnerLength() - $maxLength,
325 22
            $box->getInnerDepth() - $maxDepth,
326 22
            $box->getMaxWeight() - $box->getEmptyWeight() - $weight,
327 22
            $maxWidth,
328 22
            $maxLength,
329 22
            $maxDepth
330
        );
331
332 22
        return $packedBox;
333
    }
334
}
335