1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Box packing (3D bin packing, knapsack problem) |
4
|
|
|
* @package BoxPacker |
5
|
|
|
* @author Doug Wright |
6
|
|
|
*/ |
7
|
|
|
namespace DVDoug\BoxPacker; |
8
|
|
|
|
9
|
|
|
use Psr\Log\LoggerAwareInterface; |
10
|
|
|
use Psr\Log\LoggerAwareTrait; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Figure out orientations for an item and a given set of dimensions |
14
|
|
|
* @author Doug Wright |
15
|
|
|
* @package BoxPacker |
16
|
|
|
*/ |
17
|
|
|
class OrientatedItemFactory implements LoggerAwareInterface |
18
|
|
|
{ |
19
|
|
|
use LoggerAwareTrait; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var OrientatedItem[] |
23
|
|
|
*/ |
24
|
|
|
protected static $emptyBoxCache = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get the best orientation for an item |
28
|
|
|
* @param Box $box |
29
|
|
|
* @param Item $item |
30
|
|
|
* @param PackedItem|null $prevItem |
31
|
|
|
* @param bool $isLastItem |
32
|
|
|
* @param int $widthLeft |
33
|
|
|
* @param int $lengthLeft |
34
|
|
|
* @param int $depthLeft |
35
|
|
|
* @return OrientatedItem|false |
36
|
|
|
*/ |
37
|
34 |
|
public function getBestOrientation(Box $box, Item $item, PackedItem $prevItem = null, $isLastItem, $widthLeft, $lengthLeft, $depthLeft) { |
38
|
|
|
|
39
|
34 |
|
$possibleOrientations = $this->getPossibleOrientations($item, $prevItem, $widthLeft, $lengthLeft, $depthLeft); |
40
|
34 |
|
$usableOrientations = $this->getUsableOrientations($possibleOrientations, $box, $item, $isLastItem); |
41
|
|
|
|
42
|
34 |
|
$orientationFits = []; |
43
|
|
|
/** @var OrientatedItem $orientation */ |
44
|
34 |
|
foreach ($usableOrientations as $o => $orientation) { |
45
|
34 |
|
$orientationFit = min($widthLeft - $orientation->getWidth(), $lengthLeft - $orientation->getLength()); |
46
|
34 |
|
$orientationFits[$o] = $orientationFit; |
47
|
|
|
} |
48
|
|
|
|
49
|
34 |
|
if (!empty($orientationFits)) { |
50
|
34 |
|
asort($orientationFits); |
51
|
34 |
|
reset($orientationFits); |
52
|
34 |
|
$bestFit = $usableOrientations[key($orientationFits)]; |
53
|
34 |
|
$this->logger->debug("Selected best fit orientation", ['orientation' => $bestFit]); |
54
|
34 |
|
return $bestFit; |
55
|
|
|
} else { |
56
|
32 |
|
return false; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Find all possible orientations for an item |
62
|
|
|
* @param Item $item |
63
|
|
|
* @param PackedItem|null $prevItem |
64
|
|
|
* @param int $widthLeft |
65
|
|
|
* @param int $lengthLeft |
66
|
|
|
* @param int $depthLeft |
67
|
|
|
* @return OrientatedItem[] |
68
|
|
|
*/ |
69
|
34 |
|
public function getPossibleOrientations(Item $item, PackedItem $prevItem = null, $widthLeft, $lengthLeft, $depthLeft) { |
70
|
|
|
|
71
|
34 |
|
$orientations = []; |
72
|
|
|
|
73
|
|
|
//Special case items that are the same as what we just packed - keep orientation |
74
|
|
|
/** @noinspection PhpNonStrictObjectEqualityInspection */ |
75
|
34 |
|
if ($prevItem && $prevItem->getItem() == $item) { |
76
|
13 |
|
$orientations[] = new OrientatedItem($item, $prevItem->getWidth(), $prevItem->getLength(), $prevItem->getDepth()); |
77
|
|
|
} else { |
78
|
|
|
|
79
|
|
|
//simple 2D rotation |
80
|
34 |
|
$orientations[] = new OrientatedItem($item, $item->getWidth(), $item->getLength(), $item->getDepth()); |
81
|
34 |
|
$orientations[] = new OrientatedItem($item, $item->getLength(), $item->getWidth(), $item->getDepth()); |
82
|
|
|
|
83
|
|
|
//add 3D rotation if we're allowed |
84
|
34 |
|
if (!$item->getKeepFlat()) { |
85
|
13 |
|
$orientations[] = new OrientatedItem($item, $item->getWidth(), $item->getDepth(), $item->getLength()); |
86
|
13 |
|
$orientations[] = new OrientatedItem($item, $item->getLength(), $item->getDepth(), $item->getWidth()); |
87
|
13 |
|
$orientations[] = new OrientatedItem($item, $item->getDepth(), $item->getWidth(), $item->getLength()); |
88
|
13 |
|
$orientations[] = new OrientatedItem($item, $item->getDepth(), $item->getLength(), $item->getWidth()); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
//remove any that simply don't fit |
93
|
34 |
|
return array_filter($orientations, function(OrientatedItem $i) use ($widthLeft, $lengthLeft, $depthLeft) { |
94
|
34 |
|
return $i->getWidth() <= $widthLeft && $i->getLength() <= $lengthLeft && $i->getDepth() <= $depthLeft; |
95
|
34 |
|
}); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Item $item |
100
|
|
|
* @param Box $box |
101
|
|
|
* @return OrientatedItem[] |
|
|
|
|
102
|
|
|
*/ |
103
|
34 |
|
public function getPossibleOrientationsInEmptyBox(Item $item, Box $box) |
104
|
|
|
{ |
105
|
34 |
|
$cacheKey = $item->getWidth() . |
106
|
34 |
|
'|' . |
107
|
34 |
|
$item->getLength() . |
108
|
34 |
|
'|' . |
109
|
34 |
|
$item->getDepth() . |
110
|
34 |
|
'|' . |
111
|
34 |
|
($item->getKeepFlat() ? '2D' : '3D') . |
112
|
34 |
|
'|' . |
113
|
34 |
|
$box->getInnerWidth() . |
114
|
34 |
|
'|' . |
115
|
34 |
|
$box->getInnerLength() . |
116
|
34 |
|
'|' . |
117
|
34 |
|
$box->getInnerDepth(); |
118
|
|
|
|
119
|
34 |
|
if (isset(static::$emptyBoxCache[$cacheKey])) { |
120
|
29 |
|
$orientations = static::$emptyBoxCache[$cacheKey]; |
121
|
|
|
} else { |
122
|
32 |
|
$orientations = $this->getPossibleOrientations( |
123
|
32 |
|
$item, |
124
|
32 |
|
null, |
125
|
32 |
|
$box->getInnerWidth(), |
126
|
32 |
|
$box->getInnerLength(), |
127
|
32 |
|
$box->getInnerDepth() |
128
|
|
|
); |
129
|
32 |
|
static::$emptyBoxCache[$cacheKey] = $orientations; |
130
|
|
|
} |
131
|
34 |
|
return $orientations; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param OrientatedItem[] $possibleOrientations |
136
|
|
|
* @param Box $box |
137
|
|
|
* @param Item $item |
138
|
|
|
* @param bool $isLastItem |
139
|
|
|
* |
140
|
|
|
* @return OrientatedItem[] |
141
|
|
|
*/ |
142
|
34 |
|
protected function getUsableOrientations( |
143
|
|
|
$possibleOrientations, |
144
|
|
|
Box $box, |
145
|
|
|
Item $item, |
146
|
|
|
$isLastItem |
147
|
|
|
) { |
148
|
|
|
/* |
149
|
|
|
* Divide possible orientations into stable (low centre of gravity) and unstable (high centre of gravity) |
150
|
|
|
*/ |
151
|
34 |
|
$stableOrientations = []; |
152
|
34 |
|
$unstableOrientations = []; |
153
|
|
|
|
154
|
34 |
|
foreach ($possibleOrientations as $o => $orientation) { |
155
|
34 |
|
if ($orientation->isStable()) { |
156
|
31 |
|
$stableOrientations[] = $orientation; |
157
|
|
|
} else { |
158
|
34 |
|
$unstableOrientations[] = $orientation; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
34 |
|
$orientationsToUse = []; |
163
|
|
|
|
164
|
|
|
/* |
165
|
|
|
* We prefer to use stable orientations only, but allow unstable ones if either |
166
|
|
|
* the item is the last one left to pack OR |
167
|
|
|
* the item doesn't fit in the box any other way |
168
|
|
|
*/ |
169
|
34 |
|
if (count($stableOrientations) > 0) { |
170
|
31 |
|
$orientationsToUse = $stableOrientations; |
171
|
33 |
|
} else if (count($unstableOrientations) > 0) { |
172
|
4 |
|
$orientationsInEmptyBox = $this->getPossibleOrientationsInEmptyBox($item, $box); |
173
|
|
|
|
174
|
4 |
|
$stableOrientationsInEmptyBox = array_filter( |
175
|
4 |
|
$orientationsInEmptyBox, |
176
|
4 |
|
function(OrientatedItem $orientation) { |
177
|
4 |
|
return $orientation->isStable(); |
178
|
4 |
|
} |
179
|
|
|
); |
180
|
|
|
|
181
|
|
|
if ($isLastItem || count($stableOrientationsInEmptyBox) == 0) { |
182
|
|
|
$orientationsToUse = $unstableOrientations; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $orientationsToUse; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.