This repository has been archived by the owner on May 20, 2019. It is now read-only.
forked from dvdoug/BoxPacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrientatedItemFactory.php
155 lines (135 loc) · 5.55 KB
/
OrientatedItemFactory.php
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
<?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;
/**
* Get the best orientation for an item
* @param Box $box
* @param Item $item
* @param OrientatedItem|null $prevItem
* @param Item|null $nextItem
* @param int $widthLeft
* @param int $lengthLeft
* @param int $depthLeft
* @return OrientatedItem|false
*/
public function getBestOrientation(Box $box, Item $item, OrientatedItem $prevItem = null, Item $nextItem = null, $widthLeft, $lengthLeft, $depthLeft) {
$possibleOrientations = $this->getPossibleOrientations($item, $prevItem, $widthLeft, $lengthLeft, $depthLeft);
$usableOrientations = $this->getUsableOrientations($possibleOrientations, $box, $item, $prevItem, $nextItem);
$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 OrientatedItem|null $prevItem
* @param int $widthLeft
* @param int $lengthLeft
* @param int $depthLeft
* @return OrientatedItem[]
*/
public function getPossibleOrientations(Item $item, OrientatedItem $prevItem = null, $widthLeft, $lengthLeft, $depthLeft) {
$orientations = [];
//Special case items that are the same as what we just packed - keep orientation
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 OrientatedItem[] $possibleOrientations
* @param Box $box
* @param Item $item
* @param OrientatedItem $prevItem
* @param Item $nextItem
*
* @return array
*/
protected function getUsableOrientations(
$possibleOrientations,
Box $box,
Item $item,
OrientatedItem $prevItem = null,
Item $nextItem = null
) {
/*
* 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->getPossibleOrientations(
$item,
$prevItem,
$box->getInnerWidth(),
$box->getInnerLength(),
$box->getInnerDepth()
);
$stableOrientationsInEmptyBox = array_filter(
$orientationsInEmptyBox,
function(OrientatedItem $orientation) {
return $orientation->isStable();
}
);
if (is_null($nextItem) || count($stableOrientationsInEmptyBox) == 0) {
$orientationsToUse = $unstableOrientations;
}
}
return $orientationsToUse;
}
}