1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Box packing (3D bin packing, knapsack problem). |
4
|
|
|
* |
5
|
|
|
* @author Doug Wright |
6
|
|
|
*/ |
7
|
|
|
namespace DVDoug\BoxPacker; |
8
|
|
|
|
9
|
|
|
use JsonSerializable; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* An item to be packed. |
13
|
|
|
* |
14
|
|
|
* @author Doug Wright |
15
|
|
|
*/ |
16
|
|
|
class OrientatedItem implements JsonSerializable |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Item |
20
|
|
|
*/ |
21
|
|
|
protected $item; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
protected $width; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var int |
30
|
|
|
*/ |
31
|
|
|
protected $length; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
protected $depth; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
protected $surfaceFootprint; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var bool[] |
45
|
|
|
*/ |
46
|
|
|
protected static $stabilityCache = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructor. |
50
|
|
|
* |
51
|
|
|
* @param Item $item |
52
|
|
|
* @param int $width |
53
|
|
|
* @param int $length |
54
|
|
|
* @param int $depth |
55
|
|
|
*/ |
56
|
33 |
|
public function __construct(Item $item, $width, $length, $depth) |
57
|
|
|
{ |
58
|
33 |
|
$this->item = $item; |
59
|
33 |
|
$this->width = $width; |
60
|
33 |
|
$this->length = $length; |
61
|
33 |
|
$this->depth = $depth; |
62
|
33 |
|
$this->surfaceFootprint = $width * $length; |
63
|
33 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Item. |
67
|
|
|
* |
68
|
|
|
* @return Item |
69
|
|
|
*/ |
70
|
30 |
|
public function getItem() |
71
|
|
|
{ |
72
|
30 |
|
return $this->item; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Item width in mm in it's packed orientation. |
77
|
|
|
* |
78
|
|
|
* @return int |
79
|
|
|
*/ |
80
|
32 |
|
public function getWidth() |
81
|
|
|
{ |
82
|
32 |
|
return $this->width; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Item length in mm in it's packed orientation. |
87
|
|
|
* |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
32 |
|
public function getLength() |
91
|
|
|
{ |
92
|
32 |
|
return $this->length; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Item depth in mm in it's packed orientation. |
97
|
|
|
* |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
32 |
|
public function getDepth() |
101
|
|
|
{ |
102
|
32 |
|
return $this->depth; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Calculate the surface footprint of the current orientation. |
107
|
|
|
* |
108
|
|
|
* @return int |
109
|
|
|
*/ |
110
|
|
|
public function getSurfaceFootprint() |
111
|
|
|
{ |
112
|
|
|
return $this->surfaceFootprint; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Is this item stable (low centre of gravity), calculated as if the tipping point is >15 degrees. |
117
|
|
|
* |
118
|
|
|
* N.B. Assumes equal weight distribution. |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
32 |
|
public function isStable() |
123
|
|
|
{ |
124
|
32 |
|
$cacheKey = $this->width . '|' . $this->length . '|' . $this->depth; |
125
|
|
|
|
126
|
32 |
|
if (!isset(static::$stabilityCache[$cacheKey])) { |
127
|
27 |
|
static::$stabilityCache[$cacheKey] = (atan(min($this->length, $this->width) / ($this->depth ?: 1)) > 0.261); |
128
|
|
|
} |
129
|
|
|
|
130
|
32 |
|
return static::$stabilityCache[$cacheKey]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
1 |
|
public function jsonSerialize() |
137
|
|
|
{ |
138
|
|
|
return [ |
139
|
1 |
|
'item' => $this->item, |
140
|
1 |
|
'width' => $this->width, |
141
|
1 |
|
'length' => $this->length, |
142
|
1 |
|
'depth' => $this->depth, |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
1 |
|
public function __toString() |
150
|
|
|
{ |
151
|
1 |
|
return $this->width . '|' . $this->length . '|' . $this->depth; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|