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