Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply fixes from StyleCI #106

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions docs/principles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ At a high level, the algorithm works like this:
* Pack largest (by volume) items first
* Pack vertically up the side of the box
* Pack side-by-side where item under consideration fits alongside the previous item
* Only very small overhangs are allowed (10%) to prevent items bending in transit
* The available width/height for each layer will therefore decrease as the stack of items gets taller
* If more than 1 box is needed to accommodate all of the items, then aim for boxes of roughly equal weight
(e.g. 3 medium size/weight boxes are better than 1 small light box and 2 that are large and heavy)

Expand Down
2 changes: 1 addition & 1 deletion src/LayerStabiliser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/**
* Applies load stability to generated result.

*
* @author Doug Wright
*/
class LayerStabiliser implements LoggerAwareInterface
Expand Down
59 changes: 31 additions & 28 deletions src/VolumePacker.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ class VolumePacker implements LoggerAwareInterface
*/
protected $layers = [];

private $z = 0;
private $packingWidthLeft;
private $packingLengthLeft;
private $packingDepthLeft;

/**
* Constructor.
*
Expand Down Expand Up @@ -108,12 +103,8 @@ public function pack(): PackedBox
{
$this->logger->debug("[EVALUATING BOX] {$this->box->getReference()}");

$this->packingWidthLeft = $this->boxWidth;
$this->packingLengthLeft = $this->boxLength;
$this->packingDepthLeft = $this->box->getInnerDepth();

while (count($this->items) > 0) {
$this->packLayer();
$this->packLayer($this->getCurrentPackedDepth());
}

if ($this->boxRotated) {
Expand All @@ -129,15 +120,21 @@ public function pack(): PackedBox

/**
* Pack items into an individual vertical layer.
*
* @param int $startDepth
*/
protected function packLayer(): void
protected function packLayer(int $startDepth): void
{
$this->layers[] = $layer = new PackedLayer();

$prevItem = null;

$x = $y = $rowWidth = $rowLength = $layerWidth = $layerLength = $layerDepth = 0;

$packingWidthLeft = $this->boxWidth;
$packingLengthLeft = $this->boxLength;
$packingDepthLeft = $this->box->getInnerDepth() - $startDepth;

while (count($this->items) > 0) {
$itemToPack = $this->items->extract();
$nextItem = $this->getNextItem();
Expand All @@ -148,21 +145,21 @@ protected function packLayer(): void
continue;
}

$orientatedItem = $this->getOrientationForItem($itemToPack, $prevItem, $nextItem, $this->hasItemsLeftToPack(), $this->packingWidthLeft, $this->packingLengthLeft, $this->packingDepthLeft);
$orientatedItem = $this->getOrientationForItem($itemToPack, $prevItem, $nextItem, $this->hasItemsLeftToPack(), $packingWidthLeft, $packingLengthLeft, $packingDepthLeft);

if ($orientatedItem instanceof OrientatedItem) {
$packedItem = PackedItem::fromOrientatedItem($orientatedItem, $x, $y, $this->z);
$packedItem = PackedItem::fromOrientatedItem($orientatedItem, $x, $y, $startDepth);
$layer->insert($packedItem);
$this->remainingWeight -= $orientatedItem->getItem()->getWeight();
$this->packingWidthLeft -= $orientatedItem->getWidth();
$packingWidthLeft -= $orientatedItem->getWidth();

$rowWidth += $orientatedItem->getWidth();
$rowLength = max($rowLength, $orientatedItem->getLength());
$layerDepth = max($layerDepth, $orientatedItem->getDepth());

//allow items to be stacked in place within the same footprint up to current layer depth
$stackableDepth = $layerDepth - $orientatedItem->getDepth();
$this->tryAndStackItemsIntoSpace($layer, $prevItem, $nextItem, $orientatedItem->getWidth(), $orientatedItem->getLength(), $stackableDepth, $x, $y, $this->z + $orientatedItem->getDepth());
$this->tryAndStackItemsIntoSpace($layer, $prevItem, $nextItem, $orientatedItem->getWidth(), $orientatedItem->getLength(), $stackableDepth, $x, $y, $startDepth + $orientatedItem->getDepth());
$x += $orientatedItem->getWidth();

$prevItem = $packedItem;
Expand All @@ -175,28 +172,19 @@ protected function packLayer(): void
} elseif (count($this->items) > 0) { // skip for now, move on to the next item
$this->logger->debug("doesn't fit, skipping for now");
$this->skippedItems->insert($itemToPack);
} elseif ($x > 0 && $this->packingLengthLeft >= min($itemToPack->getWidth(), $itemToPack->getLength())) {
} elseif ($x > 0 && $packingLengthLeft >= min($itemToPack->getWidth(), $itemToPack->getLength())) {
$this->logger->debug('No more fit in width wise, resetting for new row');
$layerWidth = max($layerWidth, $rowWidth);
$layerLength += $rowLength;
$this->packingWidthLeft += $rowWidth;
$this->packingLengthLeft -= $rowLength;
$packingWidthLeft += $rowWidth;
$packingLengthLeft -= $rowLength;
$y += $rowLength;
$x = $rowWidth = $rowLength = 0;
$this->rebuildItemList($itemToPack);
$prevItem = null;
continue;
} else {
$this->logger->debug('no items fit, so starting next vertical layer');

$layerWidth = max($layerWidth, $rowWidth);
$layerLength += $rowLength;
$this->packingWidthLeft = $rowWidth ? min(intval($layerWidth * 1.1), $this->boxWidth) : $this->boxWidth;
$this->packingLengthLeft = $rowLength ? min(intval($layerLength * 1.1), $this->boxLength) : $this->boxLength;
$this->packingDepthLeft -= $layerDepth;

$this->z += $layerDepth;

$this->rebuildItemList($itemToPack);

return;
Expand Down Expand Up @@ -371,7 +359,7 @@ protected function rebuildItemList(?Item $currentItem = null): void
}

/**
* Swap back width/length of the packed items to match orientation of the box if needed
* Swap back width/length of the packed items to match orientation of the box if needed.
*/
protected function rotateLayersNinetyDegrees(): void
{
Expand Down Expand Up @@ -421,4 +409,19 @@ protected function getPackedItemList(): PackedItemList

return $packedItemList;
}

/**
* Return the current packed depth.
*
* @return int
*/
protected function getCurrentPackedDepth(): int
{
$depth = 0;
foreach ($this->layers as $layer) {
$depth += $layer->getDepth();
}

return $depth;
}
}
12 changes: 4 additions & 8 deletions tests/PackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,10 @@ public function testIssue52C(): void
/** @var PackedBox[] $packedBoxes */
$packedBoxes = iterator_to_array($packer->pack(), false);

self::assertEquals(2, count($packedBoxes));
self::assertEquals(1, count($packedBoxes));

self::assertEquals(160, $packedBoxes[0]->getUsedWidth());
self::assertEquals(285, $packedBoxes[0]->getUsedLength());
self::assertEquals(70, $packedBoxes[0]->getUsedDepth());

self::assertEquals(210, $packedBoxes[1]->getUsedWidth());
self::assertEquals(297, $packedBoxes[1]->getUsedLength());
self::assertEquals(4, $packedBoxes[1]->getUsedDepth());
self::assertEquals(210, $packedBoxes[0]->getUsedWidth());
self::assertEquals(297, $packedBoxes[0]->getUsedLength());
self::assertEquals(74, $packedBoxes[0]->getUsedDepth());
}
}