Skip to content

Commit 323dab2

Browse files
committed
Initial commit
0 parents  commit 323dab2

16 files changed

+1099
-0
lines changed

.github/FUNDING.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: stloyd

.github/workflows/tests.yml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Tests
2+
on: [push, pull_request]
3+
jobs:
4+
tests:
5+
name: PHP ${{ matrix.php-version }}
6+
runs-on: ubuntu-latest
7+
strategy:
8+
fail-fast: true
9+
matrix:
10+
php-version:
11+
- "8.1"
12+
13+
env:
14+
php-extensions: xdebug, yaml
15+
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v3
19+
20+
- name: Setup PHP
21+
uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: ${{ matrix.php-version }}
24+
extensions: ${{ env.php-extensions }}
25+
26+
- uses: "ramsey/composer-install@v2"
27+
28+
- name: Running static analyse
29+
run: php vendor/bin/phpstan analyse src/ tests/ --level max
30+
31+
- name: Running tests
32+
run: php vendor/bin/phpunit --coverage-text

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/composer.phar
2+
/composer.lock
3+
/.php-cs-fixer.cache
4+
/.phpunit.result.cache
5+
/.idea/
6+
/vendor/

.php-cs-fixer.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the PHP-LAFF package.
7+
*
8+
* (c) Joseph Bielawski <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
$fileHeaderComment = <<<'EOF'
15+
This file is part of the PHP-LAFF package.
16+
17+
(c) Joseph Bielawski <[email protected]>
18+
19+
For the full copyright and license information, please view the LICENSE
20+
file that was distributed with this source code.
21+
EOF;
22+
23+
$finder = (new PhpCsFixer\Finder())
24+
->in(__DIR__.'/src')
25+
->in(__DIR__.'/tests')
26+
->append([__FILE__])
27+
;
28+
29+
return (new PhpCsFixer\Config())
30+
->setRules(
31+
[
32+
'@Symfony' => true,
33+
'@Symfony:risky' => true,
34+
'header_comment' => ['header' => $fileHeaderComment],
35+
'protected_to_private' => false,
36+
'native_constant_invocation' => ['strict' => false],
37+
'modernize_strpos' => true,
38+
]
39+
)
40+
->setRiskyAllowed(true)
41+
->setFinder($finder)
42+
;

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Joseph Bielawski
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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!

composer.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "php-laff/analazer",
3+
"description": "PHP Implementation of the Largest Area Fit First (LAFF) 3D (three dimension) box packing algorithm",
4+
"license": "MIT",
5+
"keywords": ["largest area fit first", "box packing algorithm", "packaging", "optimization problems", "three-dimensional packing", "combinatorial problem"],
6+
"authors": [
7+
{
8+
"name": "Joseph Bielawski",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"LAFF\\Analyzer\\": "src/LAFF/Analyzer/"
15+
}
16+
},
17+
"autoload-dev": {
18+
"psr-4": {
19+
"LAFF\\Analyzer\\Tests\\": "tests/LAFF/Analyzer/"
20+
}
21+
},
22+
"require": {
23+
"php": ">=8.1"
24+
},
25+
"require-dev": {
26+
"friendsofphp/php-cs-fixer": "^3.8",
27+
"phpunit/phpunit": "^9.5",
28+
"phpstan/phpstan": "^1.8"
29+
},
30+
"minimum-stability": "stable",
31+
"scripts": {
32+
"cs-fixer": "@php vendor/bin/php-cs-fixer fix",
33+
"test": "@php vendor/bin/phpunit",
34+
"coverage": "@php vendor/bin/phpunit --coverage-text",
35+
"phpstan": "@php vendor/bin/phpstan analyse src/ tests/ --level max"
36+
}
37+
}

phpunit.xml.dist

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
6+
bootstrap="vendor/autoload.php"
7+
colors="true"
8+
>
9+
<testsuites>
10+
<testsuite name="LAFF Test Suite">
11+
<directory>tests/LAFF/Analyzer/</directory>
12+
</testsuite>
13+
</testsuites>
14+
15+
<coverage>
16+
<include>
17+
<directory suffix=".php">src/LAFF/Analyzer</directory>
18+
</include>
19+
</coverage>
20+
</phpunit>

0 commit comments

Comments
 (0)