Passed
Push — weight_redistributor ( 060e5b...8de9a1 )
by Doug
02:00
created

PackedLayer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 79
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A insert() 0 4 1
A getItems() 0 4 1
A getFootprint() 0 12 2
A getStartDepth() 0 10 2
A getDepth() 0 10 2
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
/**
12
 * A packed layer.
13
 *
14
 * @author Doug Wright
15
 */
16
class PackedLayer
17
{
18
    /**
19
     * Items packed into this layer.
20
     *
21
     * @var PackedItem[]
22
     */
23
    protected $items = [];
24
25
    /**
26
     * Add a packed item to this layer.
27
     *
28
     * @param PackedItem $packedItem
29
     */
30 20
    public function insert(PackedItem $packedItem): void
31
    {
32 20
        $this->items[] = $packedItem;
33 20
    }
34
35
    /**
36
     * Get the packed items.
37
     *
38
     * @return PackedItem[]
39
     */
40 20
    public function getItems(): array
41
    {
42 20
        return $this->items;
43
    }
44
45
    /**
46
     * Calculate footprint area of this layer.
47
     *
48
     * @return int mm^2
49
     */
50 11
    public function getFootprint(): int
51
    {
52 11
        $layerWidth = 0;
53 11
        $layerLength = 0;
54
55 11
        foreach ($this->items as $item) {
56 11
            $layerWidth = max($layerWidth, $item->getX() + $item->getWidth());
57 11
            $layerLength = max($layerLength, $item->getY() + $item->getLength());
58
        }
59
60 11
        return $layerWidth * $layerLength;
61
    }
62
63
    /**
64
     * Calculate start depth of this layer.
65
     *
66
     * @return int mm
67
     */
68 20
    public function getStartDepth(): int
69
    {
70 20
        $startDepth = PHP_INT_MAX;
71
72 20
        foreach ($this->items as $item) {
73 20
            $startDepth = min($startDepth, $item->getZ());
74
        }
75
76 20
        return $startDepth;
77
    }
78
79
    /**
80
     * Calculate depth of this layer.
81
     *
82
     * @return int mm
83
     */
84 20
    public function getDepth(): int
85
    {
86 20
        $layerDepth = 0;
87
88 20
        foreach ($this->items as $item) {
89 20
            $layerDepth = max($layerDepth, $item->getZ() + $item->getDepth());
90
        }
91
92 20
        return $layerDepth - $this->getStartDepth();
93
    }
94
}
95