From 007dd5f23567b65cd19523a7475d82c220c73cb9 Mon Sep 17 00:00:00 2001 From: Doug Wright Date: Sat, 29 Mar 2014 19:39:27 +0000 Subject: [PATCH] Don't abandon a packing as soon as an item doesn't fit (fixes #2) --- Packer.php | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/Packer.php b/Packer.php index 6fd6d67b..6a13c3a1 100644 --- a/Packer.php +++ b/Packer.php @@ -143,8 +143,21 @@ public function doVolumePacking() { //Find best box of iteration, and remove packed items from unpacked list $bestBox = $packedBoxesIteration->top(); - for ($i = 0; $i < $bestBox->getItems()->count(); $i++) { - $this->items->extract(); + $bestBoxItems = $bestBox->getItems()->asArray(); + $unpackedItems = $this->items->asArray(); + + foreach ($bestBoxItems as $bestBoxItem) { + foreach ($unpackedItems as $key => $unpackedItem) { + if ($bestBoxItem == $unpackedItem) { + unset($unpackedItems[$key]); + break; + } + } + } + + $this->items = new ItemList; + foreach ($unpackedItems as $item) { + $this->items->insert($item); } $packedBoxes->insert($bestBox); @@ -251,7 +264,8 @@ public function packBox(Box $aBox, ItemList $aItems) { $itemToPack = $aItems->top(); if ($itemToPack->getDepth() > ($layerDepth ?: $remainingDepth) || $itemToPack->getWeight() > $remainingWeight) { - break; + $aItems->extract(); //ignore, move on + continue; } $this->logger->log(LogLevel::DEBUG, "evaluating item {$itemToPack->getDescription()}");