Skip to content

Commit

Permalink
Allow items with dissimilar depths to be placed side-by-side
Browse files Browse the repository at this point in the history
This was in place to ensure stability of layers so that items packed
above wouldn't slip and fall diagonally in transit. However, the
horizontal overhang checks should already prevent that situation
  • Loading branch information
dvdoug committed Jan 23, 2014
1 parent 6399955 commit 6cc9fab
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
9 changes: 4 additions & 5 deletions Packer.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,8 @@ public function packBox(Box $aBox, ItemList $aItems) {

$fitsSameGap = min($remainingWidth - $itemWidth, $remainingLength - $itemLength);
$fitsRotatedGap = min($remainingWidth - $itemLength, $remainingLength - $itemWidth);
$notTooMuchShallower = ($layerDepth ? $aItems->top()->getDepth() > ($layerDepth * 0.9) : true);

if ($notTooMuchShallower && $fitsSameGap >= 0 && $fitsRotatedGap < 0) {
if ($fitsSameGap >= 0 && $fitsRotatedGap < 0) {
$this->logger->debug("fits only without rotation");

$itemToPack = $aItems->extract();
Expand All @@ -288,7 +287,7 @@ public function packBox(Box $aBox, ItemList $aItems) {
$layerDepth = max($layerDepth, $itemToPack->getDepth()); //greater than 0, items will always be less deep

}
else if ($notTooMuchShallower && $fitsSameGap < 0 && $fitsRotatedGap >= 0) {
else if ($fitsSameGap < 0 && $fitsRotatedGap >= 0) {
$this->logger->debug("fits only with rotation");

$itemToPack = $aItems->extract();
Expand All @@ -301,7 +300,7 @@ public function packBox(Box $aBox, ItemList $aItems) {
$layerLength += $itemWidth;
$layerDepth = max($layerDepth, $itemToPack->getDepth()); //greater than 0, items will always be less deep
}
else if ($notTooMuchShallower && $fitsSameGap >= 0 && $fitsRotatedGap >= 0) {
else if ($fitsSameGap >= 0 && $fitsRotatedGap >= 0) {
$this->logger->debug("fits both ways");

$itemToPack = $aItems->extract();
Expand All @@ -321,7 +320,7 @@ public function packBox(Box $aBox, ItemList $aItems) {
}
$layerDepth = max($layerDepth, $itemToPack->getDepth()); //greater than 0, items will always be less deep
}
else if (!$notTooMuchShallower || ($fitsSameGap < 0 && $fitsRotatedGap < 0)) {
else if ($fitsSameGap < 0 && $fitsRotatedGap < 0) {
$this->logger->debug("doesn't fit at all");

if ($layerWidth) {
Expand Down
12 changes: 12 additions & 0 deletions tests/PackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,18 @@ public function testPackSingleItemFitsBetterRotated() {
self::assertEquals(1, $packedItems->count());
}

public function testIssue1() {

$packer = new Packer();
$packer->addBox(new TestBox('Le petite box', 292, 336, 60, 10, 292, 336, 60, 9000));
$packer->addBox(new TestBox('Le grande box', 421, 548, 335, 100, 421, 548, 335, 10000));
$packer->addItem(new TestItem('Item 1', 226, 200, 40, 440));
$packer->addItem(new TestItem('Item 2', 200, 200, 155, 1660));
$packedBoxes = $packer->pack();

self::assertEquals(1, $packedBoxes->count());
}


/**
* @coversNothing
Expand Down

0 comments on commit 6cc9fab

Please sign in to comment.