From bdb7c7716129fd1257fc806544f93d5a5dbcf383 Mon Sep 17 00:00:00 2001 From: Doug Wright Date: Sat, 15 Apr 2017 18:39:33 +0100 Subject: [PATCH] PackedBox->getUsedDepth() could incorrectly return a value of 0 in some situations. Fixes #52 --- VolumePacker.php | 3 ++- tests/PackerTest.php | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/VolumePacker.php b/VolumePacker.php index 83e9fd3e..0545c29f 100644 --- a/VolumePacker.php +++ b/VolumePacker.php @@ -156,7 +156,7 @@ public function pack() $prevItem = $orientatedItem; - if (!$nextItem) { + if ($this->items->isEmpty()) { $this->usedDepth += $layerDepth; } } else { @@ -172,6 +172,7 @@ public function pack() continue; } elseif ($this->lengthLeft < min($itemToPack->getWidth(), $itemToPack->getLength()) || $layerDepth == 0) { $this->logger->debug("doesn't fit on layer even when empty"); + $this->usedDepth += $layerDepth; continue; } diff --git a/tests/PackerTest.php b/tests/PackerTest.php index 95679fac..20b9fe04 100644 --- a/tests/PackerTest.php +++ b/tests/PackerTest.php @@ -343,7 +343,7 @@ public function testPackerPacksRotatedBoxesInNewRow() self::assertEquals(2, $packedBoxes->count()); } - public function testIssue52() + public function testIssue52A() { $packer = new Packer(); $packer->addBox(new TestBox('Box', 100, 50, 50, 0, 100, 50, 50, 5000)); @@ -356,6 +356,44 @@ public function testIssue52() self::assertEquals(8, $packedBoxes->top()->getUsedDepth()); } + public function testIssue52B() + { + $packer = new Packer(); + $packer->addBox(new TestBox('Box',370,375,60,140,364,374,40,3000)); + $packer->addItem(new TestItem('Item 1',220,310,12,679, true)); + $packer->addItem(new TestItem('Item 2',210,297,11,648, true)); + $packer->addItem(new TestItem('Item 3',210,297,5,187, true)); + $packer->addItem(new TestItem('Item 4',148,210,32,880, true)); + $packedBoxes = $packer->pack(); + + self::assertEquals(1, $packedBoxes->count()); + self::assertEquals(310, $packedBoxes->top()->getUsedWidth()); + self::assertEquals(368, $packedBoxes->top()->getUsedLength()); + self::assertEquals(32, $packedBoxes->top()->getUsedDepth()); + } + + public function testIssue52C() + { + $packer = new Packer(); + $packer->addBox(new TestBox('Box',230,300,240,160,230,300,240,15000)); + $packer->addItem(new TestItem('Item 1',210,297,4,213, true)); + $packer->addItem(new TestItem('Item 2',80,285,70,199, true)); + $packer->addItem(new TestItem('Item 3',80,285,70,199, true)); + $packedBoxes = $packer->pack(); + + self::assertEquals(2, $packedBoxes->count()); + $box1 = $packedBoxes->extract(); + $box2 = $packedBoxes->extract(); + + self::assertEquals(80, $box1->getUsedWidth()); + self::assertEquals(285, $box1->getUsedLength()); + self::assertEquals(70, $box1->getUsedDepth()); + + self::assertEquals(210, $box2->getUsedWidth()); + self::assertEquals(297, $box2->getUsedLength()); + self::assertEquals(4, $box2->getUsedDepth()); + } + /** * @dataProvider getSamples * @coversNothing