|
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
|
62 |
|
public function insert(PackedItem $packedItem): void |
|
69
|
|
|
{ |
|
70
|
62 |
|
$this->items[] = $packedItem; |
|
71
|
62 |
|
$this->weight += $packedItem->getItem()->getWeight(); |
|
72
|
62 |
|
$this->startX = min($this->startX, $packedItem->getX()); |
|
73
|
62 |
|
$this->endX = max($this->endX, $packedItem->getX() + $packedItem->getWidth()); |
|
74
|
62 |
|
$this->startY = min($this->startY, $packedItem->getY()); |
|
75
|
62 |
|
$this->endY = max($this->endY, $packedItem->getY() + $packedItem->getLength()); |
|
76
|
62 |
|
$this->startZ = min($this->startZ, $packedItem->getZ()); |
|
77
|
62 |
|
$this->endZ = max($this->endZ, $packedItem->getZ() + $packedItem->getDepth()); |
|
78
|
62 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get the packed items. |
|
82
|
|
|
* |
|
83
|
|
|
* @return PackedItem[] |
|
84
|
|
|
*/ |
|
85
|
62 |
|
public function getItems(): array |
|
86
|
|
|
{ |
|
87
|
62 |
|
return $this->items; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Calculate footprint area of this layer. |
|
92
|
|
|
* |
|
93
|
|
|
* @return int mm^2 |
|
94
|
|
|
*/ |
|
95
|
35 |
|
public function getFootprint(): int |
|
96
|
|
|
{ |
|
97
|
35 |
|
return $this->getWidth() * $this->getLength(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function getStartX(): int |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->startX; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
62 |
|
public function getEndX(): int |
|
106
|
|
|
{ |
|
107
|
62 |
|
return $this->endX; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
35 |
|
public function getWidth(): int |
|
111
|
|
|
{ |
|
112
|
35 |
|
return $this->endX - $this->startX; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function getStartY(): int |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->startY; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
62 |
|
public function getEndY(): int |
|
121
|
|
|
{ |
|
122
|
62 |
|
return $this->endY; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
35 |
|
public function getLength(): int |
|
126
|
|
|
{ |
|
127
|
35 |
|
return $this->endY - $this->startY; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
62 |
|
public function getStartZ(): int |
|
131
|
|
|
{ |
|
132
|
62 |
|
return $this->startZ; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function getEndZ(): int |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->endZ; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
62 |
|
public function getDepth(): int |
|
141
|
|
|
{ |
|
142
|
62 |
|
return $this->endZ - $this->startZ; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
62 |
|
public function getWeight(): int |
|
146
|
|
|
{ |
|
147
|
62 |
|
return $this->weight; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|