Skip to content

Commit

Permalink
Allow items to be stacked (fixes #11)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdoug committed Aug 10, 2014
1 parent 569c0a6 commit ae87d6f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Packer.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,23 @@ public function packIntoBox(Box $aBox, ItemList $aItems) {
$layerWidth = max($itemLength, $layerWidth);
}
$layerDepth = max($layerDepth, $itemToPack->getDepth()); //greater than 0, items will always be less deep

//allow items to be stacked in place within the same footprint up to current layerdepth
$maxStackDepth = $layerDepth - $itemToPack->getDepth();
while(!$aItems->isEmpty()) {
$potentialStackItem = $aItems->top();
if ($potentialStackItem->getDepth() <= $maxStackDepth &&
$potentialStackItem->getWeight() <= $remainingWeight &&
$potentialStackItem->getWidth() <= $itemToPack->getWidth() &&
$potentialStackItem->getLength() <= $itemToPack->getLength()) {
$remainingWeight -= $potentialStackItem->getWeight();
$maxStackDepth -= $potentialStackItem->getDepth();
$packedItems->insert($aItems->extract());
}
else {
break;
}
}
}
else {
if ($remainingWidth >= min($itemWidth, $itemLength) && $layerDepth > 0) {
Expand Down
11 changes: 11 additions & 0 deletions tests/PackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,17 @@ public function testIssue9() {
self::assertEquals(1, $packedBoxes->count());
}

public function testIssue11() {
$packer = new Packer();
$packer->addBox(new TestBox('4x4x4Box', 4, 4, 4, 4, 4, 4, 4, 100));

$packer->addItem(new TestItem('BigItem', 2, 2, 4, 1), 2);
$packer->addItem(new TestItem('SmallItem', 1, 1, 1, 1), 32);
$packedBoxes = $packer->pack();

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

public function testIssue13() {
$packer = new Packer();
$packer->addBox(new TestBox('Le petite box', 12, 12, 12, 10, 10, 10, 10, 1000));
Expand Down

0 comments on commit ae87d6f

Please sign in to comment.