From 6cc9fabe4eec7b67034bb5a1d8691acb3d694ceb Mon Sep 17 00:00:00 2001 From: Doug Wright Date: Thu, 16 Jan 2014 21:19:06 +0000 Subject: [PATCH] Allow items with dissimilar depths to be placed side-by-side 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 --- Packer.php | 9 ++++----- tests/PackerTest.php | 12 ++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Packer.php b/Packer.php index 989ecfed..7898f7f5 100644 --- a/Packer.php +++ b/Packer.php @@ -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(); @@ -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(); @@ -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(); @@ -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) { diff --git a/tests/PackerTest.php b/tests/PackerTest.php index d9a98b8d..e3e75c4f 100644 --- a/tests/PackerTest.php +++ b/tests/PackerTest.php @@ -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