protected function findBestOrientation(Item $item, OrientatedItem $prevItem = null, Item $nextItem = null, $widthLeft, $lengthLeft, $depthLeft)
{
$orientations = $this->findPossibleOrientations($item, $prevItem, $widthLeft, $lengthLeft, $depthLeft);
// special casing based on next item
if (isset($orientations[0]) && $nextItem == $item && $lengthLeft >= 2 * $item->getLength()) {
$this->logger->debug("not rotating based on next item");
return $orientations[0];
// XXX this is tied to the ordering from ->findPossibleOrientations()
}
$orientationFits = [];
/** @var OrientatedItem $orientation */
foreach ($orientations as $o => $orientation) {
$orientationFit = min($widthLeft - $orientation->getWidth(), $lengthLeft - $orientation->getLength());
$orientationFits[$o] = $orientationFit;
}
if (!empty($orientationFits)) {
asort($orientationFits);
reset($orientationFits);
$bestFit = key($orientationFits);
$this->logger->debug("Using orientation #{$bestFit}");
return $orientations[$bestFit];
} else {
return false;
}
}