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
|
|
|
/** |
10
|
|
|
* A "box" with items |
11
|
|
|
* @author Doug Wright |
12
|
|
|
* @package BoxPacker |
13
|
|
|
*/ |
14
|
|
|
class PackedBox |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Box used |
19
|
|
|
* @var Box |
20
|
|
|
*/ |
21
|
|
|
protected $box; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Items in the box |
25
|
|
|
* @var ItemList |
26
|
|
|
*/ |
27
|
|
|
protected $items; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Total weight of box |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
protected $weight; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Remaining width inside box for another item |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
protected $remainingWidth; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Remaining length inside box for another item |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
protected $remainingLength; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Remaining depth inside box for another item |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
protected $remainingDepth; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Remaining weight inside box for another item |
55
|
|
|
* @var int |
56
|
|
|
*/ |
57
|
|
|
protected $remainingWeight; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Used width inside box for packing items |
61
|
|
|
* @var int |
62
|
|
|
*/ |
63
|
6 |
|
protected $usedWidth; |
64
|
|
|
|
65
|
6 |
|
/** |
66
|
|
|
* Used length inside box for packing items |
67
|
|
|
* @var int |
68
|
|
|
*/ |
69
|
|
|
protected $usedLength; |
70
|
|
|
|
71
|
|
|
/** |
72
|
27 |
|
* Used depth inside box for packing items |
73
|
|
|
* @var int |
74
|
27 |
|
*/ |
75
|
|
|
protected $usedDepth; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get box used |
79
|
|
|
* @return Box |
80
|
|
|
*/ |
81
|
7 |
|
public function getBox() |
82
|
|
|
{ |
83
|
|
|
return $this->box; |
84
|
7 |
|
} |
85
|
5 |
|
|
86
|
|
|
/** |
87
|
|
|
* Get items packed |
88
|
7 |
|
* @return ItemList |
89
|
7 |
|
*/ |
90
|
7 |
|
public function getItems() |
91
|
7 |
|
{ |
92
|
|
|
return $this->items; |
93
|
7 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get packed weight |
97
|
|
|
* @return int weight in grams |
98
|
|
|
*/ |
99
|
|
|
public function getWeight() |
100
|
1 |
|
{ |
101
|
|
|
|
102
|
1 |
|
if (!is_null($this->weight)) { |
103
|
|
|
return $this->weight; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->weight = $this->box->getEmptyWeight(); |
107
|
|
|
$items = clone $this->items; |
108
|
|
|
foreach ($items as $item) { |
109
|
1 |
|
$this->weight += $item->getWeight(); |
110
|
|
|
} |
111
|
1 |
|
return $this->weight; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get remaining width inside box for another item |
116
|
|
|
* @return int |
117
|
|
|
*/ |
118
|
1 |
|
public function getRemainingWidth() |
119
|
|
|
{ |
120
|
1 |
|
return $this->remainingWidth; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get remaining length inside box for another item |
125
|
|
|
* @return int |
126
|
|
|
*/ |
127
|
1 |
|
public function getRemainingLength() |
128
|
|
|
{ |
129
|
1 |
|
return $this->remainingLength; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get remaining depth inside box for another item |
134
|
|
|
* @return int |
135
|
|
|
*/ |
136
|
1 |
|
public function getRemainingDepth() |
137
|
|
|
{ |
138
|
1 |
|
return $this->remainingDepth; |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
/** |
142
|
1 |
|
* Used width inside box for packing items |
143
|
|
|
* @return int |
144
|
|
|
*/ |
145
|
1 |
|
public function getUsedWidth() |
146
|
|
|
{ |
147
|
|
|
return $this->usedWidth; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Used length inside box for packing items |
152
|
|
|
* @return int |
153
|
|
|
*/ |
154
|
|
|
public function getUsedLength() |
155
|
|
|
{ |
156
|
|
|
return $this->usedLength; |
157
|
|
|
} |
158
|
|
|
|
159
|
30 |
|
/** |
160
|
|
|
* Used depth inside box for packing items |
161
|
30 |
|
* @return int |
162
|
30 |
|
*/ |
163
|
30 |
|
public function getUsedDepth() |
164
|
30 |
|
{ |
165
|
30 |
|
return $this->usedDepth; |
166
|
30 |
|
} |
167
|
30 |
|
|
168
|
|
|
/** |
169
|
|
|
* Get remaining weight inside box for another item |
170
|
|
|
* @return int |
171
|
|
|
*/ |
172
|
|
|
public function getRemainingWeight() |
173
|
|
|
{ |
174
|
|
|
return $this->remainingWeight; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Get volume utilisation of the packed box |
179
|
|
|
* @return float |
180
|
|
|
*/ |
181
|
|
|
public function getVolumeUtilisation() |
182
|
|
|
{ |
183
|
|
|
$itemVolume = 0; |
184
|
|
|
|
185
|
|
|
/** @var Item $item */ |
186
|
|
|
foreach (clone $this->items as $item) { |
187
|
|
|
$itemVolume += $item->getVolume(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return round($itemVolume / $this->box->getInnerVolume() * 100, 1); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Constructor |
196
|
|
|
* |
197
|
|
|
* @param Box $box |
198
|
|
|
* @param ItemList $itemList |
199
|
|
|
* @param int $remainingWidth |
200
|
|
|
* @param int $remainingLength |
201
|
|
|
* @param int $remainingDepth |
202
|
|
|
* @param int $remainingWeight |
203
|
|
|
* @param int $usedWidth |
204
|
|
|
* @param int $usedLength |
205
|
|
|
* @param int $usedDepth |
206
|
|
|
*/ |
207
|
|
|
public function __construct( |
208
|
|
|
Box $box, |
209
|
|
|
ItemList $itemList, |
210
|
|
|
$remainingWidth, |
211
|
|
|
$remainingLength, |
212
|
|
|
$remainingDepth, |
213
|
|
|
$remainingWeight, |
214
|
|
|
$usedWidth, |
215
|
|
|
$usedLength, |
216
|
|
|
$usedDepth |
217
|
|
|
) |
218
|
|
|
{ |
219
|
|
|
$this->box = $box; |
220
|
|
|
$this->items = $itemList; |
221
|
|
|
$this->remainingWidth = $remainingWidth; |
222
|
|
|
$this->remainingLength = $remainingLength; |
223
|
|
|
$this->remainingDepth = $remainingDepth; |
224
|
|
|
$this->remainingWeight = $remainingWeight; |
225
|
|
|
$this->usedWidth = $usedWidth; |
226
|
|
|
$this->usedLength = $usedLength; |
227
|
|
|
$this->usedDepth = $usedDepth; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|