Passed
Pull Request — master (#568)
by
unknown
16:44 queued 13:18
created

src/LayerStabiliser.php (1 issue)

Severity
1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem).
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace DVDoug\BoxPacker;
10
11
use function usort;
12
13
/**
14
 * Applies load stability to generated result.
15
 * @internal
16
 */
17
class LayerStabiliser
18
{
19
    /**
20
     * @param PackedLayer[] $packedLayers
21
     *
22
     * @return PackedLayer[]
23
     */
24 93
    public function stabilise(array $packedLayers): array
25
    {
26
        // first re-order according to footprint
27 93
        $stabilisedLayers = [];
28 93
        usort($packedLayers, $this->compare(...));
29
30
        // then for each item in the layer, re-calculate each item's z position
31 93
        $currentZ = 0;
32 93
        foreach ($packedLayers as $oldZLayer) {
33 93
            $oldZStart = $oldZLayer->getStartZ();
34 93
            $newZLayer = new PackedLayer();
35 93
            foreach ($oldZLayer->getItems() as $oldZItem) {
36 93
                $newZ = $oldZItem->z - $oldZStart + $currentZ;
37 93
                $newZItem = new PackedItem($oldZItem->item, $oldZItem->x, $oldZItem->y, $newZ, $oldZItem->width, $oldZItem->length, $oldZItem->depth);
38 93
                $newZLayer->insert($newZItem);
39
            }
40
41 93
            $stabilisedLayers[] = $newZLayer;
42 93
            $currentZ += $newZLayer->getDepth();
43
        }
44
45 93
        return $stabilisedLayers;
46
    }
47
48 45
    private function compare(PackedLayer $layerA, PackedLayer $layerB): int
0 ignored issues
show
The method compare() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
49
    {
50 45
        return ($layerB->getFootprint() <=> $layerA->getFootprint()) ?: ($layerB->getDepth() <=> $layerA->getDepth());
51
    }
52
}
53