latuconsinafr / 3d-bin-packager
PHP 3D 容器装箱库。
v1.0
2021-11-29 17:49 UTC
Requires
- php: ^7.4 || ^8.0
Requires (Dev)
- phpunit/phpunit: ^9.5
README
此包包含一个 PHP 实现,用于解决 3D 容器装箱问题,基于 Go 上的 gedex 实现和 Python 上的 enzoruiz 实现进行了修改,以使其更模块化。
安装
通过将包作为 Composer 依赖项添加来安装
$ composer require latuconsinafr/3d-bin-packager
用法
根项目内部有一个示例 example.php
。只需在项目的根目录下简单地输入命令 php example.php
,它就会显示经过的时间以及相应的结果,包括下限数量和容器的列表(支持 JSON 编码,因为每个类都实现了 \JsonSerializable
接口)。
首先,您必须初始化打包器对象
$packager = new Packager(2, SortType::DESCENDING);
打包器构造函数接受两个参数
- 精度:小数点后的位数。它将用于四舍五入的位数。
- 排序类型:有两种类型可供选择
SortType::ASCENDING
和SortType::DESCENDING
。此排序类型将用于根据选择的排序类型(是升序还是降序)对打包器中的容器和项目体积进行排序
初始化打包器后,您可以使用以下 4 个方法之一开始向打包器中添加容器或项目
// To add the bin one by one $packager->addBin(new Bin('your-bin-identifier', 4, 4.5, 5.221, 50)); // Or if you wish to add all the bins at once $packager->addBins([ new Bin('your-first-bin-identifier', 2.22, 5.4, 20, 100), new Bin('your-second-bin-identifier', 1, 2, 1.5, 10) ]); // It would be the same for the item(s) $packager->addItem(new Item('item-id-1', 2, 2, 2, 5)); $packager->addItems([ new Item('item-id-2', 1, 4.225, 2, 5), new Item('item-id-3', 2, 5, 2, 2.525), new Item('item-id-4', 1, 3.5, 3, 10), new Item('item-id-5', 3.551, 2, 2, 12.5) ]);
然后将所有项目打包到所有容器中
$packager->withFirstFit()->pack()
打包后的结果和其他详细信息如下所示
- 要获取打包后的所有容器,您可以使用
$packager->getBins()
或$packager->getIterableBins()
来返回ArrayIterator
数据类型。您可以在可迭代的容器中看到 id、长度、高度等,以及在fittedItems
属性中的所有适合的项目。 - 由于首次适应方法,装箱过程将尽可能地将尽可能多的项目适应到第一个容器中,如果容器中没有更多空间,所有剩余的项目将列在
unfittedItems
属性中,并移至下一个容器。您还可以通过getTotalFittedVolume()
、getTotalFittedWeight()
等查看总适应或未适应的体积和重量。 - 标识符用于使每个容器和项目都是唯一的。
- 您还可以使用
getTotalBinsVolume()
、getTotalBinsWeight()
等从 Packager 类获取所有容器和项目的总数。 - 您可以通过
getRotationType()
获取容器内适合项目的详细信息,例如应用于将项目适应到容器中的当前旋转类型(您可以通过RotationCombinationType::class
看到旋转列表)。 - 您可以将结果容器序列化以查看类似以下内容
{ "bin-id": { "id": "bin-id", "length": 8, "breadth": 5, "height": 8, "volume": 320, "weight": 100, "fittedItems": [ { "id": "item-id", "length": 3.1, "breadth": 5, "height": 4, "volume": 62, "weight": 2, "rotationType": 0, // You can get the rotation type of any item inside the bin "position": { // You can also get the detailed position of any item inside the bin "x-axis": 0, "y-axis": 0, "z-axis": 0 } } ... ], "totalFittedVolume": 212.23, "totalFittedWeight": 65.83, ... } }
- 您还可以使用
getPosition()
获取容器内任何项目的相应位置,该位置由 x 轴、y 轴和 z 轴表示。如果您想进行绘图或用于进一步分析。 - 您可以查看 phpdoc 或代码以获取更多信息。
致谢
许可证
本软件包遵循MIT许可协议。