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 min; |
13
|
|
|
use const PHP_INT_MAX; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A packed layer. |
17
|
|
|
* |
18
|
|
|
* @author Doug Wright |
19
|
|
|
* @internal |
20
|
|
|
*/ |
21
|
|
|
class PackedLayer |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
private $startX = PHP_INT_MAX; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
private $endX = 0; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
private $startY = PHP_INT_MAX; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
private $endY = 0; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
|
|
private $startZ = PHP_INT_MAX; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var int |
50
|
|
|
*/ |
51
|
|
|
private $endZ = 0; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
private $weight = 0; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Items packed into this layer. |
60
|
|
|
* |
61
|
|
|
* @var PackedItem[] |
62
|
|
|
*/ |
63
|
|
|
protected $items = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Add a packed item to this layer. |
67
|
|
|
*/ |
68
|
70 |
|
public function insert(PackedItem $packedItem): void |
69
|
|
|
{ |
70
|
70 |
|
$this->items[] = $packedItem; |
71
|
70 |
|
$this->weight += $packedItem->getItem()->getWeight(); |
72
|
70 |
|
$this->startX = min($this->startX, $packedItem->getX()); |
73
|
70 |
|
$this->endX = max($this->endX, $packedItem->getX() + $packedItem->getWidth()); |
74
|
70 |
|
$this->startY = min($this->startY, $packedItem->getY()); |
75
|
70 |
|
$this->endY = max($this->endY, $packedItem->getY() + $packedItem->getLength()); |
76
|
70 |
|
$this->startZ = min($this->startZ, $packedItem->getZ()); |
77
|
70 |
|
$this->endZ = max($this->endZ, $packedItem->getZ() + $packedItem->getDepth()); |
78
|
70 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the packed items. |
82
|
|
|
* |
83
|
|
|
* @return PackedItem[] |
84
|
|
|
*/ |
85
|
68 |
|
public function getItems(): array |
86
|
|
|
{ |
87
|
68 |
|
return $this->items; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Calculate footprint area of this layer. |
92
|
|
|
* |
93
|
|
|
* @return int mm^2 |
94
|
|
|
*/ |
95
|
30 |
|
public function getFootprint(): int |
96
|
|
|
{ |
97
|
30 |
|
return $this->getWidth() * $this->getLength(); |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
public function getStartX(): int |
101
|
|
|
{ |
102
|
2 |
|
return $this->startX; |
103
|
|
|
} |
104
|
|
|
|
105
|
70 |
|
public function getEndX(): int |
106
|
|
|
{ |
107
|
70 |
|
return $this->endX; |
108
|
|
|
} |
109
|
|
|
|
110
|
30 |
|
public function getWidth(): int |
111
|
|
|
{ |
112
|
30 |
|
return $this->endX ? $this->endX - $this->startX : 0; |
113
|
|
|
} |
114
|
|
|
|
115
|
2 |
|
public function getStartY(): int |
116
|
|
|
{ |
117
|
2 |
|
return $this->startY; |
118
|
|
|
} |
119
|
|
|
|
120
|
70 |
|
public function getEndY(): int |
121
|
|
|
{ |
122
|
70 |
|
return $this->endY; |
123
|
|
|
} |
124
|
|
|
|
125
|
30 |
|
public function getLength(): int |
126
|
|
|
{ |
127
|
30 |
|
return $this->endY ? $this->endY - $this->startY : 0; |
128
|
|
|
} |
129
|
|
|
|
130
|
68 |
|
public function getStartZ(): int |
131
|
|
|
{ |
132
|
68 |
|
return $this->startZ; |
133
|
|
|
} |
134
|
|
|
|
135
|
2 |
|
public function getEndZ(): int |
136
|
|
|
{ |
137
|
2 |
|
return $this->endZ; |
138
|
|
|
} |
139
|
|
|
|
140
|
70 |
|
public function getDepth(): int |
141
|
|
|
{ |
142
|
70 |
|
return $this->endZ ? $this->endZ - $this->startZ : 0; |
143
|
|
|
} |
144
|
|
|
|
145
|
2 |
|
public function getWeight(): int |
146
|
|
|
{ |
147
|
2 |
|
return $this->weight; |
148
|
|
|
} |
149
|
|
|
|
150
|
18 |
|
public function merge(self $otherLayer): void |
151
|
|
|
{ |
152
|
18 |
|
foreach ($otherLayer->items as $packedItem) { |
153
|
10 |
|
$this->insert($packedItem); |
154
|
|
|
} |
155
|
18 |
|
} |
156
|
|
|
} |
157
|
|
|
|