Skip to content

Commit

Permalink
Ensure item width/lengths match the orientation of the box. Fixes #86
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdoug committed Sep 4, 2017
1 parent e3d2179 commit fa93b46
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
2 changes: 1 addition & 1 deletion PackedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class PackedItem
* @param int $length
* @param int $depth
*/
protected function __construct(Item $item, $x, $y, $z, $width, $length, $depth)
public function __construct(Item $item, $x, $y, $z, $width, $length, $depth)
{
$this->item = $item;
$this->x = $x;
Expand Down
46 changes: 44 additions & 2 deletions VolumePacker.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class VolumePacker implements LoggerAwareInterface
*/
protected $remainingWeight;

/**
* Whether the box was rotated for packing
* @var bool
*/
protected $boxRotated = false;

/**
* Constructor
*
Expand All @@ -70,6 +76,12 @@ public function __construct(Box $box, ItemList $items)
$this->remainingWeight = $this->box->getMaxWeight() - $this->box->getEmptyWeight();
$this->skippedItems = new ItemList();
$this->logger = new NullLogger();

// we may have just rotated the box for packing purposes, record if we did
if ($this->box->getInnerWidth() != $this->boxWidth || $this->box->getInnerLength() != $this->boxLength) {
$this->boxRotated = true;
}

}

/**
Expand Down Expand Up @@ -159,7 +171,7 @@ public function pack()
}
}
$this->logger->debug("done with this box");
return PackedBox::fromPackedItemList($this->box, $packedItems);
return $this->createPackedBox($packedItems);
}

/**
Expand Down Expand Up @@ -188,7 +200,7 @@ protected function getOrientationForItem(
'maxWidth' => $maxWidth,
'maxLength' => $maxLength,
'maxDepth' => $maxDepth,
]
],
]
);

Expand Down Expand Up @@ -302,4 +314,34 @@ protected function rebuildItemList() {
$this->skippedItems = new ItemList();
}
}

/**
* @param PackedItemList $packedItems
*
* @return PackedBox
*/
protected function createPackedBox(PackedItemList $packedItems)
{
//if we rotated the box for packing, need to swap back width/length of the packed items
if ($this->boxRotated) {
$items = iterator_to_array($packedItems);
$packedItems = new PackedItemList();
/** @var PackedItem $item */
foreach($items as $item) {
$packedItems->insert(
new PackedItem(
$item->getItem(),
$item->getY(),
$item->getX(),
$item->getZ(),
$item->getLength(),
$item->getWidth(),
$item->getDepth()
)
);
}
}

return PackedBox::fromPackedItemList($this->box, $packedItems);
}
}
12 changes: 6 additions & 6 deletions tests/PackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ public function testIssue52B()
$packedBoxes = $packer->pack();

self::assertEquals(1, $packedBoxes->count());
self::assertEquals(368, $packedBoxes->top()->getUsedWidth());
self::assertEquals(310, $packedBoxes->top()->getUsedLength());
self::assertEquals(310, $packedBoxes->top()->getUsedWidth());
self::assertEquals(368, $packedBoxes->top()->getUsedLength());
self::assertEquals(32, $packedBoxes->top()->getUsedDepth());
}

Expand All @@ -387,12 +387,12 @@ public function testIssue52C()
$box1 = $packedBoxes->extract();
$box2 = $packedBoxes->extract();

self::assertEquals(285, $box1->getUsedWidth());
self::assertEquals(160, $box1->getUsedLength());
self::assertEquals(160, $box1->getUsedWidth());
self::assertEquals(285, $box1->getUsedLength());
self::assertEquals(70, $box1->getUsedDepth());

self::assertEquals(297, $box2->getUsedWidth());
self::assertEquals(210, $box2->getUsedLength());
self::assertEquals(210, $box2->getUsedWidth());
self::assertEquals(297, $box2->getUsedLength());
self::assertEquals(4, $box2->getUsedDepth());
}

Expand Down
22 changes: 22 additions & 0 deletions tests/VolumePackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,28 @@ public function testIssue75()
self::assertEquals(1, $packedBoxes->count());
}


public function testIssue86()
{
$packer = new Packer();
$packer->addBox(new TestBox('Box', 22.5, 27, 13.5, 0.2, 22.5, 27, 13.5, 30));
$packer->addItem(new TestItem('Item 1', 11.2, 22.2, 2.2, 0.075),3);
$packer->addItem(new TestItem('Item 2', 10.8, 21.5, 2, 0.102),4);
$packer->addItem(new TestItem('Item 3', 6.2, 17, 1.8, 0.03),3);
$packedBoxes = $packer->pack();

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

/** @var PackedBox $packedBox */
$packedBox = $packedBoxes->top();
self::assertEquals(22.2, $packedBox->getUsedWidth()); //23.2
self::assertEquals(23.2, $packedBox->getUsedLength()); //22.2
self::assertEquals(10.2, $packedBox->getUsedDepth());
self::assertEquals(0.3, $packedBox->getRemainingWidth());
self::assertEquals(3.8, $packedBox->getRemainingLength());
self::assertEquals(3.3, $packedBox->getRemainingDepth());
}

public function testConstraints()
{
TestConstrainedTestItem::$limit = 2;
Expand Down

0 comments on commit fa93b46

Please sign in to comment.