Passed
Push — 3.x ( 0d7474...922c95 )
by Doug
15:19 queued 14:00
created

PackedBox::getWeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem).
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace DVDoug\BoxPacker;
10
11
use function max;
12
use function round;
13
14
/**
15
 * A "box" with items.
16
 *
17
 * @author Doug Wright
18
 */
19
class PackedBox
20
{
21
    /**
22
     * Box used.
23
     *
24
     * @var Box
25
     */
26
    protected $box;
27
28
    /**
29
     * Items in the box.
30
     *
31
     * @var PackedItemList
32
     */
33
    protected $items;
34
35
    /**
36
     * Total weight of items in the box.
37
     *
38
     * @var int
39
     */
40
    protected $itemWeight;
41
42
    /**
43
     * Volume used for items as % of box.
44
     *
45
     * @var float
46
     */
47
    protected $volumeUtilisation;
48
49
    /**
50
     * Get box used.
51
     */
52 17
    public function getBox(): Box
53
    {
54 17
        return $this->box;
55
    }
56
57
    /**
58
     * Get items packed.
59
     */
60 62
    public function getItems(): PackedItemList
61
    {
62 62
        return $this->items;
63
    }
64
65
    /**
66
     * Get packed weight.
67
     *
68
     * @return int weight in grams
69
     */
70 3
    public function getWeight(): int
71
    {
72 3
        return $this->box->getEmptyWeight() + $this->getItemWeight();
73
    }
74
75
    /**
76
     * Get packed weight of the items only.
77
     *
78
     * @return int weight in grams
79
     */
80 3
    public function getItemWeight(): int
81
    {
82 3
        if ($this->itemWeight !== null) {
83 3
            return $this->itemWeight;
84
        }
85
86 3
        $this->itemWeight = 0;
87
        /** @var PackedItem $item */
88 3
        foreach ($this->items as $item) {
89 3
            $this->itemWeight += $item->getItem()->getWeight();
90
        }
91
92 3
        return $this->itemWeight;
93
    }
94
95
    /**
96
     * Get remaining width inside box for another item.
97
     */
98
    public function getRemainingWidth(): int
99
    {
100
        return $this->box->getInnerWidth() - $this->getUsedWidth();
101
    }
102
103
    /**
104
     * Get remaining length inside box for another item.
105
     */
106
    public function getRemainingLength(): int
107
    {
108
        return $this->box->getInnerLength() - $this->getUsedLength();
109
    }
110
111
    /**
112
     * Get remaining depth inside box for another item.
113
     */
114
    public function getRemainingDepth(): int
115
    {
116
        return $this->box->getInnerDepth() - $this->getUsedDepth();
117
    }
118
119
    /**
120
     * Used width inside box for packing items.
121
     */
122
    public function getUsedWidth(): int
123
    {
124
        $maxWidth = 0;
125
126
        /** @var PackedItem $item */
127
        foreach ($this->items as $item) {
128
            $maxWidth = max($maxWidth, $item->getX() + $item->getWidth());
129
        }
130
131
        return $maxWidth;
132
    }
133
134
    /**
135
     * Used length inside box for packing items.
136
     */
137
    public function getUsedLength(): int
138
    {
139
        $maxLength = 0;
140
141
        /** @var PackedItem $item */
142
        foreach ($this->items as $item) {
143
            $maxLength = max($maxLength, $item->getY() + $item->getLength());
144
        }
145
146
        return $maxLength;
147
    }
148
149
    /**
150
     * Used depth inside box for packing items.
151
     */
152
    public function getUsedDepth(): int
153
    {
154
        $maxDepth = 0;
155
156
        /** @var PackedItem $item */
157
        foreach ($this->items as $item) {
158
            $maxDepth = max($maxDepth, $item->getZ() + $item->getDepth());
159
        }
160
161
        return $maxDepth;
162
    }
163
164
    /**
165
     * Get remaining weight inside box for another item.
166
     */
167
    public function getRemainingWeight(): int
168
    {
169
        return $this->box->getMaxWeight() - $this->getWeight();
170
    }
171
172 62
    public function getInnerVolume(): int
173
    {
174 62
        return $this->box->getInnerWidth() * $this->box->getInnerLength() * $this->box->getInnerDepth();
175
    }
176
177
    /**
178
     * Get used volume of the packed box.
179
     */
180 62
    public function getUsedVolume(): int
181
    {
182 62
        return $this->items->getVolume();
183
    }
184
185
    /**
186
     * Get unused volume of the packed box.
187
     */
188
    public function getUnusedVolume(): int
189
    {
190
        return $this->getInnerVolume() - $this->getUsedVolume();
191
    }
192
193
    /**
194
     * Get volume utilisation of the packed box.
195
     */
196 9
    public function getVolumeUtilisation(): float
197
    {
198 9
        return $this->volumeUtilisation;
199
    }
200
201
    /**
202
     * Constructor.
203
     */
204 62
    public function __construct(Box $box, PackedItemList $packedItemList)
205
    {
206 62
        $this->box = $box;
207 62
        $this->items = $packedItemList;
208
209 62
        $this->volumeUtilisation = round($this->getUsedVolume() / ($this->getInnerVolume() ?: 1) * 100, 1);
210 62
    }
211
}
212