diff --git a/PackedItem.php b/PackedItem.php index 937042d3..864830ce 100644 --- a/PackedItem.php +++ b/PackedItem.php @@ -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; diff --git a/VolumePacker.php b/VolumePacker.php index 03f8694a..42b279ed 100644 --- a/VolumePacker.php +++ b/VolumePacker.php @@ -54,6 +54,12 @@ class VolumePacker implements LoggerAwareInterface */ protected $remainingWeight; + /** + * Whether the box was rotated for packing + * @var bool + */ + protected $boxRotated = false; + /** * Constructor * @@ -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; + } + } /** @@ -159,7 +171,7 @@ public function pack() } } $this->logger->debug("done with this box"); - return PackedBox::fromPackedItemList($this->box, $packedItems); + return $this->createPackedBox($packedItems); } /** @@ -188,7 +200,7 @@ protected function getOrientationForItem( 'maxWidth' => $maxWidth, 'maxLength' => $maxLength, 'maxDepth' => $maxDepth, - ] + ], ] ); @@ -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); + } } diff --git a/tests/PackerTest.php b/tests/PackerTest.php index 7df495b2..61e0ea6f 100644 --- a/tests/PackerTest.php +++ b/tests/PackerTest.php @@ -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()); } @@ -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()); } diff --git a/tests/VolumePackerTest.php b/tests/VolumePackerTest.php index cf30de9a..dfdbe97f 100644 --- a/tests/VolumePackerTest.php +++ b/tests/VolumePackerTest.php @@ -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;