|
| 1 | +# PHP-LAFF Analyzer |
| 2 | +PHP Implementation of the Largest Area Fit First (LAFF) 3D (three dimensions: length, width, height) box packing algorithm. |
| 3 | + |
| 4 | +With this library you can easily: |
| 5 | +- get the required dimensions of the container that will fit all given packages, |
| 6 | +- split packages per defined amount of containers, |
| 7 | +- split packages per layer in a given container, |
| 8 | +- get information about the wasted amount of space per container and per layer, |
| 9 | +- get the number of remaining packages that couldn't fit into given containers, |
| 10 | + |
| 11 | +## Algorithm definition |
| 12 | + |
| 13 | +Implementation of the used algorithm was defined by M. Zahid Gürbüz, Selim Akyokus, Ibrahim Emiroglu, and Aysun Güran in a paper called ["An Efficient Algorithm for 3D Rectangular Box Packing"](http://www.zahidgurbuz.com/yayinlar/An%20Efficient%20Algorithm%20for%203D%20Rectangular%20Box%20Packing.pdf). |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +> **Note** |
| 18 | +> To use this library you need PHP in version 8.1+ |
| 19 | +
|
| 20 | +```bash |
| 21 | +composer require php-laff/analyzer |
| 22 | +``` |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +### Get the size of the required container for selected packages |
| 27 | +```php |
| 28 | +<?php |
| 29 | + |
| 30 | +use LAFF\Analyzer\Analyzer; |
| 31 | +use LAFF\Analyzer\Model\Package; |
| 32 | + |
| 33 | +$packages = [ |
| 34 | + new Package(50, 50, 8), |
| 35 | + new Package(33, 8, 8), |
| 36 | + new Package(16, 20, 8), |
| 37 | + new Package(3, 18, 8), |
| 38 | + new Package(14, 12, 8), |
| 39 | +]; |
| 40 | + |
| 41 | +$analyzer = new Analyzer(); |
| 42 | +$analyzer->analyze($packages); |
| 43 | + |
| 44 | +$containers = $analyzer->getContainers(); |
| 45 | +/** @var Container $container */ |
| 46 | +$container = reset($containers); |
| 47 | + |
| 48 | +var_dump($container->toArray()); |
| 49 | +// Output: |
| 50 | +// array(3) { |
| 51 | +// ["length"]=> |
| 52 | +// int(50) |
| 53 | +// ["width"]=> |
| 54 | +// int(50) |
| 55 | +// ["height"]=> |
| 56 | +// int(16) |
| 57 | +// } |
| 58 | +var_dump($container->countLayers()); |
| 59 | +// Output: |
| 60 | +// int(2) |
| 61 | +var_dump($analyzer->getWastePercentage()); |
| 62 | +// Output (%): |
| 63 | +// int(32) |
| 64 | +var_dump($analyzer->getWasteVolume()); |
| 65 | +// Output (cm3): |
| 66 | +// int(13552) |
| 67 | +``` |
| 68 | + |
| 69 | +### Check how many packages can be fitter into a given container |
| 70 | +```php |
| 71 | +<?php |
| 72 | + |
| 73 | +use LAFF\Analyzer\Analyzer; |
| 74 | +use LAFF\Analyzer\Model\Container; |
| 75 | +use LAFF\Analyzer\Model\Package; |
| 76 | + |
| 77 | +$packages = [ |
| 78 | + new Package(50, 50, 8), |
| 79 | + new Package(33, 8, 8), |
| 80 | + new Package(16, 20, 8), |
| 81 | + new Package(3, 18, 8), |
| 82 | + new Package(14, 12, 8), |
| 83 | +]; |
| 84 | + |
| 85 | +$container = new Container(65, 60, 8); |
| 86 | + |
| 87 | +$analyzer = new Analyzer(); |
| 88 | +$analyzer->analyze($packages, [$container]); |
| 89 | + |
| 90 | +var_dump($container->full); |
| 91 | +// Output: |
| 92 | +// bool(true) |
| 93 | +var_dump($container->countLayers()); |
| 94 | +// Output: |
| 95 | +// int(1) |
| 96 | +var_dump($container->getWastePercentage()); |
| 97 | +// Output (%) |
| 98 | +//: int(15) |
| 99 | +var_dump($container->getWasteVolume()); |
| 100 | +// Output (cm3): |
| 101 | +// int(4752) |
| 102 | +``` |
| 103 | + |
| 104 | +## Development |
| 105 | +To install dependencies, launch the following commands: |
| 106 | +```bash |
| 107 | +composer install |
| 108 | +``` |
| 109 | + |
| 110 | +## Run Tests |
| 111 | +To execute full test suite, static analyse or coding style fixed, launch the following commands: |
| 112 | +```bash |
| 113 | +composer test |
| 114 | +composer phpstan |
| 115 | +composer cs-fixer |
| 116 | +``` |
| 117 | + |
| 118 | +## Kudos |
| 119 | +There is already a library for LAFF in PHP: [Cloudstek/php-laff](https://github.com/Cloudstek/php-laff); while both use the same algorithm, the internals is different. The main difference between those libraries is that this one can work on an array of containers. I want to say "thank you" for the work on that library in the past! |
0 commit comments