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