forked from Cloudstek/php-laff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaff-pack.php
548 lines (459 loc) · 13.8 KB
/
laff-pack.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
<?php
/**
* Largest Area Fit First (LAFF) 3D box packing algorithm class
*
* @author Maarten de Boer <info@maartendeboer.net>
* @copyright Maarten de Boer 2012
* @version 1.0
*
* Also see this PDF document for an explanation about the LAFF algorithm:
* @link http://www.zahidgurbuz.com/yayinlar/An%20Efficient%20Algorithm%20for%203D%20Rectangular%20Box%20Packing.pdf
*
* Copyright (C) 2012 Maarten de Boer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
class LAFFPack {
/** @var array $boxes Array of boxes to pack */
private $boxes = null;
/** @var array $packed_boxes Array of boxes that have been packed */
private $packed_boxes = null;
/** @var int $level Current level we're packing (0 based) */
private $level = -1;
/** @var array $container_dimensions Current container dimensions */
private $container_dimensions = null;
/**
* Constructor of the BoxPacking class
*
* @access public
* @param array $boxes Array of boxes to pack
*/
function __construct($boxes = null, $container = null)
{
if(isset($boxes) && is_array($boxes)) {
$this->boxes = $boxes;
$this->packed_boxes = array();
// Calculate container size
if(!is_array($container)) {
$this->container_dimensions = $this->_calc_container_dimensions();
}
else {
// Calculate container size
if(!is_array($container)) {
$this->container_dimensions = $this->_calc_container_dimensions();
}
else {
if(!array_key_exists('length', $container) ||
!array_key_exists('width', $container)) {
throw new InvalidArgumentException("Function _pack only accepts array (length, width, height) as argument for $container");
}
$this->container_dimensions['length'] = $container['length'];
$this->container_dimensions['width'] = $container['width'];
// Note: do NOT set height, it will be calculated on-the-go
}
}
}
}
/**
* Start packing boxes
*
* @access public
* @param array $boxes
* @param array $container Set fixed container dimensions
* @returns void
*/
function pack($boxes = null, $container = null) {
if(isset($boxes) && is_array($boxes)) {
$this->boxes = $boxes;
$this->packed_boxes = array();
$this->level = -1;
$this->container_dimensions = null;
// Calculate container size
if(!is_array($container)) {
$this->container_dimensions = $this->_calc_container_dimensions();
}
else {
if(!array_key_exists('length', $container) ||
!array_key_exists('width', $container)) {
throw new InvalidArgumentException("Pack function only accepts array (length, width, height) as argument for \$container");
}
$this->container_dimensions['length'] = $container['length'];
$this->container_dimensions['width'] = $container['width'];
// Note: do NOT set height, it will be calculated on-the-go
}
}
if(!isset($this->boxes)) {
throw new InvalidArgumentException("Pack function only accepts array (length, width, height) as argument for \$boxes or no boxes given!");
}
$this->pack_level();
}
/**
* Get remaining boxes to pack
*
* @access public
* @returns array
*/
function get_remaining_boxes() {
return $this->boxes;
}
/**
* Get packed boxes
*
* @access public
* @returns array
*/
function get_packed_boxes() {
return $this->packed_boxes;
}
/**
* Get container dimensions
*
* @access public
* @returns array
*/
function get_container_dimensions() {
return $this->container_dimensions;
}
/**
* Get container volume
*
* @access public
* @returns float
*/
function get_container_volume() {
if(!isset($this->container_dimensions)) {
return 0;
}
return $this->_get_volume($this->container_dimensions);
}
/**
* Get number of levels
*
* @access public
* @returns int
*/
function get_levels() {
return $this->level + 1;
}
/**
* Get total volume of packed boxes
*
* @access public
* @returns float
*/
function get_packed_volume() {
if(!isset($this->packed_boxes)) {
return 0;
}
$volume = 0;
for($i = 0; $i < count(array_keys($this->packed_boxes)); $i++) {
foreach($this->packed_boxes[$i] as $box) {
$volume += $this->_get_volume($box);
}
}
return $volume;
}
/**
* Get number of levels
*
* @access public
* @returns int
*/
function get_remaining_volume() {
if(!isset($this->packed_boxes)) {
return 0;
}
$volume = 0;
foreach($this->boxes as $box) {
$volume += $this->_get_volume($box);
}
return $volume;
}
/**
* Get dimensions of specified level
*
* @access public
* @param int $level
* @returns array
*/
function get_level_dimensions($level = 0) {
if($level < 0 || $level > $this->level || !array_key_exists($level, $this->packed_boxes)) {
throw new OutOfRangeException("Level {$level} not found!");
}
$boxes = $this->packed_boxes;
$edges = array('length', 'width', 'height');
// Get longest edge
$le = $this->_calc_longest_edge($boxes[$level], $edges);
$edges = array_diff($edges, array($le['edge_name']));
// Re-iterate and get longest edge now (second longest)
$sle = $this->_calc_longest_edge($boxes[$level], $edges);
return array(
'width' => $le['edge_size'],
'length' => $sle['edge_size'],
'height' => $boxes[$level][0]['height']
);
}
/**
* Get longest edge from boxes
*
* @access public
* @param array $edges Edges to select the longest from
* @returns array
*/
function _calc_longest_edge($boxes, $edges = array('length', 'width', 'height')) {
if(!isset($boxes) || !is_array($boxes)) {
throw new InvalidArgumentException('_calc_longest_edge function requires an array of boxes, '.typeof($boxes).' given');
}
// Longest edge
$le = null; // Longest edge
$lef = null; // Edge field (length | width | height) that is longest
// Get longest edges
foreach($boxes as $k => $box) {
foreach($edges as $edge) {
if(array_key_exists($edge, $box) && $box[$edge] > $le) {
$le = $box[$edge];
$lef = $edge;
}
}
}
return array(
'edge_size' => $le,
'edge_name' => $lef
);
}
/**
* Calculate container dimensions
*
* @access public
* @returns array
*/
function _calc_container_dimensions() {
if(!isset($this->boxes)){
return array(
'length' => 0,
'width' => 0,
'height' => 0
);
}
$boxes = $this->boxes;
$edges = array('length', 'width', 'height');
// Get longest edge
$le = $this->_calc_longest_edge($boxes, $edges);
$edges = array_diff($edges, array($le['edge_name']));
// Re-iterate and get longest edge now (second longest)
$sle = $this->_calc_longest_edge($boxes, $edges);
return array(
'length' => $le['edge_size'],
'width' => $sle['edge_size'],
'height' => 0
);
}
/**
* Utility function to swap two elements in an array
*
* @access public
* @param array $array
* @param mixed $el1 Index of item to be swapped
* @param mixed $el2 Index of item to swap with
* @returns array
*/
function _swap($array, $el1, $el2) {
if(!array_key_exists($el1, $array) || !array_key_exists($el2, $array)) {
throw new InvalidArgumentException("Both element to be swapped need to exist in the supplied array");
}
$tmp = $array[$el1];
$array[$el1] = $array[$el2];
$array[$el2] = $tmp;
return $array;
}
/**
* Utility function that returns the total volume of a box / container
*
* @access public
* @param array $box
* @returns float
*/
function _get_volume($box) {
if(!is_array($box) || count(array_keys($box)) < 3) {
throw new InvalidArgumentException("_get_volume function only accepts arrays with 3 values (length, width, height)");
}
$box = array_values($box);
return $box[0] * $box[1] * $box[2];
}
/**
* Check if box fits in specified space
*
* @access private
* @param array $box Box to fit in space
* @param array $space Space to fit box in
* @returns bool
*/
private function _try_fit_box($box, $space) {
if(count($box) < 3) {
throw new InvalidArgumentException("_try_fit_box function parameter $box only accepts arrays with 3 values (length, width, height)");
}
if(count($space) < 3) {
throw new InvalidArgumentException("_try_fit_box function parameter $space only accepts arrays with 3 values (length, width, height)");
}
for($i = 0; $i < count($box); $i++) {
if(array_key_exists($i, $space)) {
if($box[$i] > $space[$i]) {
return false;
}
}
}
return true;
}
/**
* Check if box fits in specified space
* and rotate (3d) if necessary
*
* @access public
* @param array $box Box to fit in space
* @param array $space Space to fit box in
* @returns bool
*/
function _box_fits($box, $space) {
$box = array_values($box);
$space = array_values($space);
if($this->_try_fit_box($box, $space)) {
return true;
}
for($i = 0; $i < count($box); $i++) {
// Temp box size
$t_box = $box;
// Remove fixed column from list to be swapped
unset($t_box[$i]);
// Keys to be swapped
$t_keys = array_keys($t_box);
// Temp box with swapped sides
$s_box = $this->_swap($box, $t_keys[0], $t_keys[1]);
if($this->_try_fit_box($s_box, $space))
return true;
}
return false;
}
/**
* Start a new packing level
*
* @access private
* @returns void
*/
private function pack_level() {
$biggest_box_index = null;
$biggest_surface = 0;
$this->level++;
// Find biggest (widest surface) box with minimum height
foreach($this->boxes as $k => $box)
{
$surface = $box['length'] * $box['width'];
if($surface > $biggest_surface) {
$biggest_surface = $surface;
$biggest_box_index = $k;
}
elseif($surface == $biggest_surface) {
if(!isset($biggest_box_index) || (isset($biggest_box_index) && $box['height'] < $this->boxes[$biggest_box_index]['height']))
$biggest_box_index = $k;
}
}
// Get biggest box as object
$biggest_box = $this->boxes[$biggest_box_index];
$this->packed_boxes[$this->level][] = $biggest_box;
// Set container height (ck = ck + ci)
$this->container_dimensions['height'] += $biggest_box['height'];
// Remove box from array (ki = ki - 1)
unset($this->boxes[$biggest_box_index]);
// Check if all boxes have been packed
if(count($this->boxes) == 0)
return;
$c_area = $this->container_dimensions['length'] * $this->container_dimensions['width'];
$p_area = $biggest_box['length'] * $biggest_box['width'];
// No space left (not even when rotated / length and width swapped)
if($c_area - $p_area <= 0) {
$this->pack_level();
}
else { // Space left, check if a package fits in
$spaces = array();
if($this->container_dimensions['length'] - $biggest_box['length'] > 0) {
$spaces[] = array(
'length' => $this->container_dimensions['length'] - $biggest_box['length'],
'width' => $this->container_dimensions['width'],
'height' => $biggest_box['height']
);
}
if($this->container_dimensions['width'] - $biggest_box['width'] > 0) {
$spaces[] = array(
'length' => $biggest_box['length'],
'width' => $this->container_dimensions['width'] - $biggest_box['width'],
'height' => $biggest_box['height']
);
}
// Fill each space with boxes
foreach($spaces as $space) {
$this->_fill_space($space);
}
// Start packing remaining boxes on a new level
if(count($this->boxes) > 0)
$this->pack_level();
}
}
/**
* Fills space with boxes recursively
*
* @access private
* @returns void
*/
private function _fill_space($space) {
// Total space volume
$s_volume = $this->_get_volume($space);
$fitting_box_index = null;
$fitting_box_volume = null;
foreach($this->boxes as $k => $box)
{
// Skip boxes that have a higher volume than target space
if($this->_get_volume($box) > $s_volume) {
continue;
}
if($this->_box_fits($box, $space)) {
$b_volume = $this->_get_volume($box);
if(!isset($fitting_box_volume) || $b_volume > $fitting_box_volume) {
$fitting_box_index = $k;
$fitting_box_volume = $b_volume;
}
}
}
if(isset($fitting_box_index))
{
$box = $this->boxes[$fitting_box_index];
// Pack box
$this->packed_boxes[$this->level][] = $this->boxes[$fitting_box_index];
unset($this->boxes[$fitting_box_index]);
// Calculate remaining space left (in current space)
$new_spaces = array();
if($space['length'] - $box['length'] > 0) {
$new_spaces[] = array(
'length' => $space['length'] - $box['length'],
'width' => $space['width'],
'height' => $box['height']
);
}
if($space['width'] - $box['width'] > 0) {
$new_spaces[] = array(
'length' => $box['length'],
'width' => $space['width'] - $box['width'],
'height' => $box['height']
);
}
if(count($new_spaces) > 0) {
foreach($new_spaces as $new_space) {
$this->_fill_space($new_space);
}
}
}
}
}
?>