Passed
Push — master ( 6d1077...c09ef5 )
by Doug
01:42
created

PackedBox::getBox()   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 = 0;
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 30
    public function getBox(): Box
53
    {
54 30
        return $this->box;
55
    }
56
57
    /**
58
     * Get items packed.
59
     */
60 74
    public function getItems(): PackedItemList
61
    {
62 74
        return $this->items;
63
    }
64
65
    /**
66
     * Get packed weight.
67
     *
68
     * @return int weight in grams
69
     */
70 10
    public function getWeight(): int
71
    {
72 10
        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 11
    public function getItemWeight(): int
81
    {
82 11
        return $this->itemWeight;
83
    }
84
85
    /**
86
     * Get remaining width inside box for another item.
87
     */
88 1
    public function getRemainingWidth(): int
89
    {
90 1
        return $this->box->getInnerWidth() - $this->getUsedWidth();
91
    }
92
93
    /**
94
     * Get remaining length inside box for another item.
95
     */
96 1
    public function getRemainingLength(): int
97
    {
98 1
        return $this->box->getInnerLength() - $this->getUsedLength();
99
    }
100
101
    /**
102
     * Get remaining depth inside box for another item.
103
     */
104 1
    public function getRemainingDepth(): int
105
    {
106 1
        return $this->box->getInnerDepth() - $this->getUsedDepth();
107
    }
108
109
    /**
110
     * Used width inside box for packing items.
111
     */
112 5
    public function getUsedWidth(): int
113
    {
114 5
        $maxWidth = 0;
115
116
        /** @var PackedItem $item */
117 5
        foreach ($this->items as $item) {
118 5
            $maxWidth = max($maxWidth, $item->getX() + $item->getWidth());
119
        }
120
121 5
        return $maxWidth;
122
    }
123
124
    /**
125
     * Used length inside box for packing items.
126
     */
127 5
    public function getUsedLength(): int
128
    {
129 5
        $maxLength = 0;
130
131
        /** @var PackedItem $item */
132 5
        foreach ($this->items as $item) {
133 5
            $maxLength = max($maxLength, $item->getY() + $item->getLength());
134
        }
135
136 5
        return $maxLength;
137
    }
138
139
    /**
140
     * Used depth inside box for packing items.
141
     */
142 5
    public function getUsedDepth(): int
143
    {
144 5
        $maxDepth = 0;
145
146
        /** @var PackedItem $item */
147 5
        foreach ($this->items as $item) {
148 5
            $maxDepth = max($maxDepth, $item->getZ() + $item->getDepth());
149
        }
150
151 5
        return $maxDepth;
152
    }
153
154
    /**
155
     * Get remaining weight inside box for another item.
156
     */
157 1
    public function getRemainingWeight(): int
158
    {
159 1
        return $this->box->getMaxWeight() - $this->getWeight();
160
    }
161
162 76
    public function getInnerVolume(): int
163
    {
164 76
        return $this->box->getInnerWidth() * $this->box->getInnerLength() * $this->box->getInnerDepth();
165
    }
166
167
    /**
168
     * Get used volume of the packed box.
169
     */
170 76
    public function getUsedVolume(): int
171
    {
172 76
        $volume = 0;
173
174
        /** @var PackedItem $item */
175 76
        foreach ($this->items as $item) {
176 76
            $volume += $item->getVolume();
177
        }
178
179 76
        return $volume;
180
    }
181
182
    /**
183
     * Get unused volume of the packed box.
184
     */
185 1
    public function getUnusedVolume(): int
186
    {
187 1
        return $this->getInnerVolume() - $this->getUsedVolume();
188
    }
189
190
    /**
191
     * Get volume utilisation of the packed box.
192
     */
193 15
    public function getVolumeUtilisation(): float
194
    {
195 15
        return $this->volumeUtilisation;
196
    }
197
198
    /**
199
     * Constructor.
200
     */
201 76
    public function __construct(Box $box, PackedItemList $packedItemList)
202
    {
203 76
        $this->box = $box;
204 76
        $this->items = $packedItemList;
205
206 76
        foreach ($this->items as $item) {
207 76
            $this->itemWeight += $item->getItem()->getWeight();
208
        }
209 76
        $this->volumeUtilisation = round($this->getUsedVolume() / ($this->getInnerVolume() ?: 1) * 100, 1);
210 76
    }
211
}
212