From 343a8950e108e925047d726170231703bac94db9 Mon Sep 17 00:00:00 2001 From: Nick Fenwick Date: Mon, 27 Jun 2022 19:17:30 +0700 Subject: [PATCH 1/3] Bump version to 1.7.3 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index e0d833b..d2b5187 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "dagmike/bin-packing", - "version": "1.7.2", + "version": "1.7.3", "description": "2D bin packing PHP implementation", "type": "library", "license": "MIT", From f0291f9204d937fbd7da335f481d904aeb921334 Mon Sep 17 00:00:00 2001 From: Nick Fenwick Date: Tue, 1 Nov 2022 09:58:05 +0700 Subject: [PATCH 2/3] Set rect fillColour based on passed rectVisOpts, allowing per-rect colouring. --- src/Helpers/VisualisationHelper.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Helpers/VisualisationHelper.php b/src/Helpers/VisualisationHelper.php index 3560f2e..01445b8 100644 --- a/src/Helpers/VisualisationHelper.php +++ b/src/Helpers/VisualisationHelper.php @@ -51,6 +51,7 @@ public static function generateVisualisation(RectangleBinPack $bin, $opts = []) if (isset($rectVisOpts['fontSize'])) { $draw->setFontSize($rectVisOpts['fontSize']); } + $draw->setFillColor(isset($rectVisOpts['fillColour']) ? $rectVisOpts['fillColour'] : $fillColour); $labelMargin = isset($rectVisOpts['labelMargin']) ? $rectVisOpts['labelMargin'] : 20; $topLeftX = $margin + $rect->getX(); From 43887e060c6ed2b6c3ccfa659fdc5cc805ef3710 Mon Sep 17 00:00:00 2001 From: Nick Fenwick Date: Tue, 1 Nov 2022 09:58:41 +0700 Subject: [PATCH 3/3] Fix logic bug meaning some flipped rects were placed overlapping the edge of the bin. --- src/Algorithms/BestAreaFit.php | 6 +++--- src/Algorithms/BestLongSideFit.php | 5 +++-- src/Algorithms/BestShortSideFit.php | 7 ++++--- src/Algorithms/BottomLeft.php | 5 +++-- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Algorithms/BestAreaFit.php b/src/Algorithms/BestAreaFit.php index 5eb69ce..aab7ea5 100644 --- a/src/Algorithms/BestAreaFit.php +++ b/src/Algorithms/BestAreaFit.php @@ -36,9 +36,9 @@ public static function findNewPosition( } } - if ($rectangle->getAllowFlip() == FlipType::ForceFlip || - ($bin->isFlipAllowed() && $rectangle->getWidth() > $rectangle->getHeight())) { - + $tryFlip = $rectangle->getAllowFlip() == FlipType::ForceFlip || + ($bin->isFlipAllowed() && $rectangle->getWidth() > $rectangle->getHeight()); + if ($tryFlip && ($freeRect->getWidth() >= $rectangle->getHeight() && $freeRect->getHeight() >= $rectangle->getWidth())) { $leftoverHoriz = abs($freeRect->getWidth() - $rectangle->getHeight()); $leftoverVert = abs($freeRect->getHeight() - $rectangle->getWidth()); $shortSideFit = min($leftoverHoriz, $leftoverVert); diff --git a/src/Algorithms/BestLongSideFit.php b/src/Algorithms/BestLongSideFit.php index a066350..c23be53 100644 --- a/src/Algorithms/BestLongSideFit.php +++ b/src/Algorithms/BestLongSideFit.php @@ -35,8 +35,9 @@ public static function findNewPosition( } } - if ($rectangle->getAllowFlip() == FlipType::ForceFlip || - ($bin->isFlipAllowed() && $rectangle->getWidth() > $rectangle->getHeight())) { + $tryFlip = $rectangle->getAllowFlip() == FlipType::ForceFlip || + ($bin->isFlipAllowed() && $rectangle->getWidth() > $rectangle->getHeight()); + if ($tryFlip && ($freeRect->getWidth() >= $rectangle->getHeight() && $freeRect->getHeight() >= $rectangle->getWidth())) { $leftoverHoriz = abs($freeRect->getWidth() - $rectangle->getHeight()); $leftoverVert = abs($freeRect->getHeight() - $rectangle->getWidth()); $shortSideFit = min($leftoverHoriz, $leftoverVert); diff --git a/src/Algorithms/BestShortSideFit.php b/src/Algorithms/BestShortSideFit.php index a8d4d70..0edafa7 100644 --- a/src/Algorithms/BestShortSideFit.php +++ b/src/Algorithms/BestShortSideFit.php @@ -35,8 +35,9 @@ public static function findNewPosition( } } - if ($rectangle->getAllowFlip() == FlipType::ForceFlip || - ($bin->isFlipAllowed() && $rectangle->getWidth() > $rectangle->getHeight())) { + $tryFlip = $rectangle->getAllowFlip() == FlipType::ForceFlip || + ($bin->isFlipAllowed() && $rectangle->getWidth() > $rectangle->getHeight()); + if ($tryFlip && ($freeRect->getWidth() >= $rectangle->getHeight() && $freeRect->getHeight() >= $rectangle->getWidth())) { $leftoverHoriz = abs($freeRect->getWidth() - $rectangle->getHeight()); $leftoverVert = abs($freeRect->getHeight() - $rectangle->getWidth()); $shortSideFit = min($leftoverHoriz, $leftoverVert); @@ -47,7 +48,7 @@ public static function findNewPosition( $bestNode->setX($freeRect->getX()); $bestNode->setY($freeRect->getY()); $bestNode->rotate(); - + $bestShortSideFit = $shortSideFit; $bestLongSideFit = $longSideFit; } diff --git a/src/Algorithms/BottomLeft.php b/src/Algorithms/BottomLeft.php index 062b195..aa654dd 100644 --- a/src/Algorithms/BottomLeft.php +++ b/src/Algorithms/BottomLeft.php @@ -30,8 +30,9 @@ public static function findNewPosition( } } - if ($rectangle->getAllowFlip() == FlipType::ForceFlip || - ($bin->isFlipAllowed() && $rectangle->getWidth() > $rectangle->getHeight())) { + $tryFlip = $rectangle->getAllowFlip() == FlipType::ForceFlip || + ($bin->isFlipAllowed() && $rectangle->getWidth() > $rectangle->getHeight()); + if ($tryFlip && ($freeRect->getWidth() >= $rectangle->getHeight() && $freeRect->getHeight() >= $rectangle->getWidth())) { $topSideY = $freeRect->getY() + $rectangle->getWidth(); if ($topSideY < $bestY || ($topSideY == $bestY && $freeRect->getX() < $bestX)) { $bestNode = RectangleFactory::fromRectangle($rectangle);