Passed
Push — 1.x ( 457fa0...308477 )
by Doug
03:11
created

PackedLayer::getWeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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