-
Notifications
You must be signed in to change notification settings - Fork 13
/
boxing.class.php
375 lines (197 loc) · 5.8 KB
/
boxing.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
class boxing {
private $outer_boxes;
private $inner_boxes;
public function boxing() {
$this -> outer_boxes = array();
$this -> inner_boxes = array();
return;
}
public function add_outer_box($l,$w,$h) {
if ($l > 0 && $w > 0 && $h > 0) {
$this -> outer_boxes[] = array(
"dimensions" => $this -> sort_dimensions($l,$w,$h),
"packed" => false
);
}
return true;
}
public function add_inner_box($l,$w,$h) {
if ($l > 0 && $w > 0 && $h > 0) {
$this -> inner_boxes[] = array(
"dimensions" => $this -> sort_dimensions($l,$w,$h),
"packed" => false
);
}
return true;
}
public function fits() {
/* first we do a simple volume check, this can save a lot of calculations */
if (!$this -> fits_volume()) {
return false;
}
/* get next inner box */
while (true) {
$inner_box_id = $this->next_inner_box();
if ($inner_box_id === false) {
break;
}
$this -> sort_outer_boxes(); // smallest first
$found_fitting_box = false;
foreach ($this->outer_boxes as $outer_box_id => $outer_box) {
if (!$outer_box["packed"] && $this->fits_inside($inner_box_id, $outer_box_id)) {
/* matches! */
$this -> inner_boxes[$inner_box_id]["packed"] = true;
$this -> outer_boxes[$outer_box_id]["packed"] = true;
$this -> find_subboxes($inner_box_id, $outer_box_id);
$found_fitting_box = true;
break;
}
}
if (!$found_fitting_box) {
return false;
}
}
/* we ran out of inner boxes but have outer boxes left */
return true;
}
public function get_outer_volume() {
$outer_volume = 0;
foreach ($this -> outer_boxes as $outer) {
$outer_volume += ($outer["dimensions"][0] * $outer["dimensions"][1] * $outer["dimensions"][2]);
}
return $outer_volume;
}
public function get_inner_volume() {
$inner_volume = 0;
foreach ($this -> inner_boxes as $inner) {
$inner_volume += ($inner["dimensions"][0] * $inner["dimensions"][1] * $inner["dimensions"][2]);
}
return $inner_volume;
}
public function fits_volume() {
$return = false;
if ($this->get_inner_volume() > $this->get_outer_volume()) {
/* inner boxes have more volume than outer ones */
$return = false;
} else {
$return = true;
}
return $return;
}
private function find_subboxes($inner_box_id, $outer_box_id) {
$inner_dimensions = $this->inner_boxes[$inner_box_id]["dimensions"];
$outer_dimensions = $this->outer_boxes[$outer_box_id]["dimensions"];
sort($outer_dimensions);
$pairs = array();
foreach ($inner_dimensions as $inner_id => $inner_value) {
foreach ($outer_dimensions as $outer_id => $outer_value) {
if ($inner_value <= $outer_value) {
$unset = $outer_id;
$pairs[] = array(
"inner" => $inner_value,
"outer" => $outer_value,
"diff" => $outer_value-$inner_value
);
break;
}
}
unset($outer_dimensions[$unset]);
}
do {
$pairs = $this-> _diffsort($pairs);
$this -> add_outer_box($pairs[0]["diff"], $pairs[1]["outer"], $pairs[2]["outer"]);
$pairs[0]["diff"] = 0;
$pairs[0]["outer"] = $pairs[0]["inner"];
} while($pairs[0]["diff"] > 0 || $pairs[1]["diff"] > 0 || $pairs[2]["diff"] > 0);
return true;
}
private function fits_inside($inner_box_id, $outer_box_id) {
if (
$this->inner_boxes[$inner_box_id]["dimensions"][0] <= $this->outer_boxes[$outer_box_id]["dimensions"][0] &&
$this->inner_boxes[$inner_box_id]["dimensions"][1] <= $this->outer_boxes[$outer_box_id]["dimensions"][1] &&
$this->inner_boxes[$inner_box_id]["dimensions"][2] <= $this->outer_boxes[$outer_box_id]["dimensions"][2]
) {
/* fits */
return true;
} else {
/* fits not */
return false;
}
}
private function sort_dimensions($l,$w,$h) {
$dimensions = array($l,$w,$h);
rsort($dimensions);
return $dimensions;
}
private function sort_outer_boxes() {
foreach ($this -> outer_boxes as $k => $v) {
$this -> outer_boxes[$k]["longest_side"] = $v["dimensions"][0];
}
$this -> outer_boxes = $this -> _sksort($this -> outer_boxes, "longest_side", false, true);
return true;
}
private function next_outer_box() {
$biggest_size = 0;
$biggest_id = false;
foreach ($this -> outer_boxes as $id => $box) {
if (!$box["packed"] && $box["dimensions"][0] > $biggest_size) {
$biggest_size = $box["dimensions"][0];
$biggest_id = $id;
}
}
return $id;
}
private function next_inner_box() {
$biggest_size = 0;
$biggest_id = false;
foreach ($this -> inner_boxes as $id => $box) {
if (!$box["packed"] && $box["dimensions"][0] > $biggest_size) {
$biggest_size = $box["dimensions"][0];
$biggest_id = $id;
}
}
return $biggest_id;
}
private function _sksort($array, $subkey, $sort_descending=false, $keep_keys_in_sub=false) {
// slightly modified since stolen from http://www.php.net/manual/de/function.sort.php#93473
foreach ($array as $key => &$value) {
$sort = array();
foreach ($value as $index => $val) {
$sort[$index] = $value[$subkey];
}
asort($sort);
$keys = array_keys($sort);
$new_value = array();
foreach ($keys as $index) {
if($keep_keys_in_sub) {
$new_value[$index] = $value[$index];
} else {
$new_value[] = $value[$index];
}
}
if ($sort_descending) {
$value = array_reverse($new_value, $keep_keys_in_sub);
} else {
$value = $new_value;
}
}
return $array;
}
function _diffsort($array) {
/* quick and dirty hack since _sksort() does strange things */
$tmp_array = array();
foreach ($array as $item) {
$tmp_array[(string)$item["diff"]][] = $item;
}
krsort($tmp_array, SORT_NUMERIC);
$array = array();
foreach ($tmp_array as $a) {
foreach ($a as $item) {
$array[] = $item;
}
}
return $array;
}
}
?>