Skip to content

Commit

Permalink
Merge pull request #34 from IBBoard/new-row-rotate-bug
Browse files Browse the repository at this point in the history
New row rotate bug
  • Loading branch information
dvdoug committed Jan 3, 2016
2 parents a1afcdd + 0f8c07a commit 45f4719
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Packer.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public function packIntoBox(Box $aBox, ItemList $aItems) {

if ($fitsRotatedGap < 0 ||
($fitsSameGap >= 0 && $fitsSameGap <= $fitsRotatedGap) ||
(!$aItems->isEmpty() && $aItems->top() == $itemToPack && $remainingLength >= 2 * $itemLength)) {
($itemWidth <= $remainingWidth && !$aItems->isEmpty() && $aItems->top() == $itemToPack && $remainingLength >= 2 * $itemLength)) {
$this->logger->log(LogLevel::DEBUG, "fits (better) unrotated");
$remainingLength -= $itemLength;
$layerLength += $itemLength;
Expand Down
34 changes: 33 additions & 1 deletion tests/PackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public function testIssue6() {
$packer->addItem(new TestItem('Item 7', 330.2, 127, 101.6, 1));
$packedBoxes = $packer->pack();

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

}

Expand Down Expand Up @@ -387,6 +387,38 @@ public function testIssue14() {
self::assertEquals(1, $packedBoxes->count());
}

public function testPackerPacksRotatedBoxesInNewRow() {
$packer = new Packer();
$packer->addItem(new TestItem('30x10x30item', 30, 10, 30, 0), 9);

//Box can hold 7 items in a row and then is completely full, so 9 items won't fit
$packer->addBox(new TestBox('30x70x30InternalBox', 30, 70, 30, 0, 30, 70, 30, 0, 1000));
$packedBoxes = $packer->pack();
self::assertEquals(2, $packedBoxes->count());

//Box can hold 7 items in a row, plus two more rotated, making 9 items
// with a 10x10x30 hole in the corner.
//
// Overhead view:
//
// +--+--++
// ++++++++
// ||||||||
// ++++++++
//
$packer = new Packer();
$packer->addItem(new TestItem('30x10x30item', 30, 10, 30, 0), 9);
$packer->addBox(new TestBox('40x70x30InternalBox', 40, 70, 30, 0, 40, 70, 30, 0, 1000));
$packedBoxes = $packer->pack();
self::assertEquals(1, $packedBoxes->count());

// Make sure that it doesn't try to fit in a 10th item
$packer = new Packer();
$packer->addItem(new TestItem('30x10x30item', 30, 10, 30, 0), 10);
$packer->addBox(new TestBox('40x70x30InternalBox', 40, 70, 30, 0, 40, 70, 30, 0, 1000));
$packedBoxes = $packer->pack();
self::assertEquals(2, $packedBoxes->count());
}

/**
* @dataProvider getSamples
Expand Down

0 comments on commit 45f4719

Please sign in to comment.