|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Box packing (3D bin packing, knapsack problem). |
|
4
|
|
|
* |
|
5
|
|
|
* @author Doug Wright |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace DVDoug\BoxPacker; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* A packed layer. |
|
11
|
|
|
* |
|
12
|
|
|
* @author Doug Wright |
|
13
|
|
|
* @internal |
|
14
|
|
|
*/ |
|
15
|
|
|
class PackedLayer |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var int |
|
19
|
|
|
*/ |
|
20
|
|
|
private $startDepth = PHP_INT_MAX; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var int |
|
24
|
|
|
*/ |
|
25
|
|
|
private $endDepth = 0; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
private $weight = 0; |
|
31
|
|
|
/** |
|
32
|
|
|
* Items packed into this layer. |
|
33
|
|
|
* |
|
34
|
|
|
* @var PackedItem[] |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $items = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Add a packed item to this layer. |
|
40
|
|
|
* |
|
41
|
|
|
* @param PackedItem $packedItem |
|
42
|
|
|
*/ |
|
43
|
42 |
|
public function insert(PackedItem $packedItem) |
|
44
|
|
|
{ |
|
45
|
42 |
|
$this->items[] = $packedItem; |
|
46
|
42 |
|
$this->weight += $packedItem->getItem()->getWeight(); |
|
47
|
42 |
|
$this->startDepth = min($this->startDepth, $packedItem->getZ()); |
|
48
|
42 |
|
$this->endDepth = max($this->endDepth, $packedItem->getZ() + $packedItem->getDepth()); |
|
49
|
42 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get the packed items. |
|
53
|
|
|
* |
|
54
|
|
|
* @return PackedItem[] |
|
55
|
|
|
*/ |
|
56
|
42 |
|
public function getItems() |
|
57
|
|
|
{ |
|
58
|
42 |
|
return $this->items; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Calculate footprint area of this layer. |
|
63
|
|
|
* |
|
64
|
|
|
* @return int mm^2 |
|
65
|
|
|
*/ |
|
66
|
21 |
|
public function getFootprint() |
|
67
|
|
|
{ |
|
68
|
21 |
|
$layerWidth = 0; |
|
69
|
21 |
|
$layerLength = 0; |
|
70
|
|
|
|
|
71
|
21 |
|
foreach ($this->items as $item) { |
|
72
|
21 |
|
$layerWidth = max($layerWidth, $item->getX() + $item->getWidth()); |
|
73
|
21 |
|
$layerLength = max($layerLength, $item->getY() + $item->getLength()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
21 |
|
return $layerWidth * $layerLength; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Calculate start depth of this layer. |
|
81
|
|
|
* |
|
82
|
|
|
* @return int mm |
|
83
|
|
|
*/ |
|
84
|
42 |
|
public function getStartDepth() |
|
85
|
|
|
{ |
|
86
|
42 |
|
return $this->startDepth; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Calculate depth of this layer. |
|
91
|
|
|
* |
|
92
|
|
|
* @return int mm |
|
93
|
|
|
*/ |
|
94
|
42 |
|
public function getDepth() |
|
95
|
|
|
{ |
|
96
|
42 |
|
return $this->endDepth - $this->getStartDepth(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Calculate weight of this layer. |
|
101
|
|
|
* |
|
102
|
|
|
* @return int weight in grams |
|
103
|
|
|
*/ |
|
104
|
34 |
|
public function getWeight() |
|
105
|
|
|
{ |
|
106
|
34 |
|
return $this->weight; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|