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