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