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