|
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 JsonSerializable; |
|
12
|
|
|
use ReturnTypeWillChange; |
|
13
|
|
|
|
|
14
|
|
|
use function array_merge; |
|
15
|
|
|
use function is_array; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* A packed item. |
|
19
|
|
|
* |
|
20
|
|
|
* @author Doug Wright |
|
21
|
|
|
*/ |
|
22
|
|
|
class PackedItem implements JsonSerializable |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var int |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $x; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $y; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var int |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $z; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Item |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $item; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var int |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $width; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var int |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $length; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var int |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $depth; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* PackedItem constructor. |
|
61
|
|
|
*/ |
|
62
|
86 |
|
public function __construct(Item $item, int $x, int $y, int $z, int $width, int $length, int $depth) |
|
63
|
|
|
{ |
|
64
|
86 |
|
$this->item = $item; |
|
65
|
86 |
|
$this->x = $x; |
|
66
|
86 |
|
$this->y = $y; |
|
67
|
86 |
|
$this->z = $z; |
|
68
|
86 |
|
$this->width = $width; |
|
69
|
86 |
|
$this->length = $length; |
|
70
|
86 |
|
$this->depth = $depth; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
84 |
|
public function getX(): int |
|
74
|
|
|
{ |
|
75
|
84 |
|
return $this->x; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
84 |
|
public function getY(): int |
|
79
|
|
|
{ |
|
80
|
84 |
|
return $this->y; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
84 |
|
public function getZ(): int |
|
84
|
|
|
{ |
|
85
|
84 |
|
return $this->z; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
84 |
|
public function getItem(): Item |
|
89
|
|
|
{ |
|
90
|
84 |
|
return $this->item; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
84 |
|
public function getWidth(): int |
|
94
|
|
|
{ |
|
95
|
84 |
|
return $this->width; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
84 |
|
public function getLength(): int |
|
99
|
|
|
{ |
|
100
|
84 |
|
return $this->length; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
84 |
|
public function getDepth(): int |
|
104
|
|
|
{ |
|
105
|
84 |
|
return $this->depth; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
84 |
|
public function getVolume(): int |
|
109
|
|
|
{ |
|
110
|
84 |
|
return $this->width * $this->length * $this->depth; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
83 |
|
public static function fromOrientatedItem(OrientatedItem $orientatedItem, int $x, int $y, int $z): self |
|
114
|
|
|
{ |
|
115
|
83 |
|
return new static( |
|
116
|
83 |
|
$orientatedItem->getItem(), |
|
117
|
83 |
|
$x, |
|
118
|
83 |
|
$y, |
|
119
|
83 |
|
$z, |
|
120
|
83 |
|
$orientatedItem->getWidth(), |
|
121
|
83 |
|
$orientatedItem->getLength(), |
|
122
|
83 |
|
$orientatedItem->getDepth() |
|
123
|
83 |
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @deprecated |
|
128
|
|
|
*/ |
|
129
|
|
|
public function toOrientatedItem(): OrientatedItem |
|
130
|
|
|
{ |
|
131
|
|
|
return new OrientatedItem($this->item, $this->width, $this->length, $this->depth); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
1 |
|
#[ReturnTypeWillChange] |
|
135
|
|
|
public function jsonSerialize()/* : mixed */ |
|
136
|
|
|
{ |
|
137
|
1 |
|
$userValues = []; |
|
138
|
|
|
|
|
139
|
1 |
|
if ($this->item instanceof JsonSerializable) { |
|
140
|
1 |
|
$userSerialisation = $this->item->jsonSerialize(); |
|
141
|
1 |
|
if (is_array($userSerialisation)) { |
|
142
|
1 |
|
$userValues = $userSerialisation; |
|
143
|
|
|
} else { |
|
144
|
|
|
$userValues = ['extra' => $userSerialisation]; |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
1 |
|
return [ |
|
149
|
1 |
|
'x' => $this->x, |
|
150
|
1 |
|
'y' => $this->y, |
|
151
|
1 |
|
'z' => $this->z, |
|
152
|
1 |
|
'width' => $this->width, |
|
153
|
1 |
|
'length' => $this->length, |
|
154
|
1 |
|
'depth' => $this->depth, |
|
155
|
1 |
|
'item' => array_merge( |
|
156
|
1 |
|
$userValues, |
|
157
|
1 |
|
[ |
|
158
|
1 |
|
'description' => $this->item->getDescription(), |
|
159
|
1 |
|
'width' => $this->item->getWidth(), |
|
160
|
1 |
|
'length' => $this->item->getLength(), |
|
161
|
1 |
|
'depth' => $this->item->getDepth(), |
|
162
|
1 |
|
'keepFlat' => $this->item->getKeepFlat(), |
|
163
|
1 |
|
] |
|
164
|
1 |
|
), |
|
165
|
1 |
|
]; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|