Passed
Push — 1.x-dev ( cc1b76...09423a )
by Doug
02:10
created

PackedLayer::getFootprint()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Box packing (3D bin packing, knapsack problem).
4
 *
5
 * @author Doug Wright
6
 */
7
8
namespace DVDoug\BoxPacker;
9
10
/**
11
 * A packed layer.
12
 *
13
 * @author Doug Wright
14
 */
15
class PackedLayer
16
{
17
    /**
18
     * Items packed into this layer.
19
     *
20
     * @var PackedItem[]
21
     */
22
    protected $items = [];
23
24
    /**
25
     * Add a packed item to this layer.
26
     *
27
     * @param PackedItem $packedItem
28
     */
29 19
    public function insert(PackedItem $packedItem)
30
    {
31 19
        $this->items[] = $packedItem;
32 19
    }
33
34
    /**
35
     * Get the packed items.
36
     *
37
     * @return PackedItem[]
38
     */
39 19
    public function getItems()
40
    {
41 19
        return $this->items;
42
    }
43
44
    /**
45
     * Calculate footprint area of this layer.
46
     *
47
     * @return int mm^2
48
     */
49 11
    public function getFootprint()
50
    {
51 11
        $layerWidth = 0;
52 11
        $layerLength = 0;
53
54 11
        foreach ($this->items as $item) {
55 11
            $layerWidth = max($layerWidth, $item->getX() + $item->getWidth());
56 11
            $layerLength = max($layerLength, $item->getY() + $item->getLength());
57
        }
58
59 11
        return $layerWidth * $layerLength;
60
    }
61
62
    /**
63
     * Calculate start depth of this layer.
64
     *
65
     * @return int mm
66
     */
67 19
    public function getStartDepth()
68
    {
69 19
        $startDepth = PHP_INT_MAX;
70
71 19
        foreach ($this->items as $item) {
72 19
            $startDepth = min($startDepth, $item->getZ());
73
        }
74
75 19
        return $startDepth;
76
    }
77
78
    /**
79
     * Calculate depth of this layer.
80
     *
81
     * @return int mm
82
     */
83 19
    public function getDepth()
84
    {
85 19
        $layerDepth = 0;
86
87 19
        foreach ($this->items as $item) {
88 19
            $layerDepth = max($layerDepth, $item->getZ() + $item->getDepth());
89
        }
90
91 19
        return $layerDepth - $this->getStartDepth();
92
    }
93
}
94