Passed
Pull Request — master (#521)
by
unknown
13:53 queued 12:16
created

PackedLayer   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 77.78%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
eloc 30
c 2
b 0
f 0
dl 0
loc 110
ccs 28
cts 36
cp 0.7778
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getItems() 0 3 1
A getLength() 0 3 2
A getFootprint() 0 3 1
A getStartZ() 0 3 1
A getWidth() 0 3 2
A getEndX() 0 3 1
A insert() 0 10 1
A merge() 0 4 2
A getDepth() 0 3 2
A getEndY() 0 3 1
A getWeight() 0 3 1
A getStartX() 0 3 1
A getStartY() 0 3 1
A getEndZ() 0 3 1
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 max;
12
use function min;
13
14
use const PHP_INT_MAX;
15
16
/**
17
 * A packed layer.
18
 * @internal
19
 */
20
class PackedLayer
21
{
22
    private int $startX = PHP_INT_MAX;
23
24
    private int $endX = 0;
25
26
    private int $startY = PHP_INT_MAX;
27
28
    private int $endY = 0;
29
30
    private int $startZ = PHP_INT_MAX;
31
32
    private int $endZ = 0;
33
34
    private int $weight = 0;
35
36
    /**
37
     * @var PackedItem[]
38
     */
39
    protected array $items = [];
40
41
    /**
42
     * Add a packed item to this layer.
43
     */
44 82
    public function insert(PackedItem $packedItem): void
45
    {
46 82
        $this->items[] = $packedItem;
47 82
        $this->weight += $packedItem->getItem()->getWeight();
48 82
        $this->startX = min($this->startX, $packedItem->getX());
49 82
        $this->endX = max($this->endX, $packedItem->getX() + $packedItem->getWidth());
50 82
        $this->startY = min($this->startY, $packedItem->getY());
51 82
        $this->endY = max($this->endY, $packedItem->getY() + $packedItem->getLength());
52 82
        $this->startZ = min($this->startZ, $packedItem->getZ());
53 82
        $this->endZ = max($this->endZ, $packedItem->getZ() + $packedItem->getDepth());
54
    }
55
56
    /**
57
     * Get the packed items.
58
     *
59
     * @return PackedItem[]
60
     */
61 84
    public function getItems(): array
62
    {
63 84
        return $this->items;
64
    }
65
66
    /**
67
     * Calculate footprint area of this layer.
68
     *
69
     * @return int mm^2
70
     */
71 46
    public function getFootprint(): int
72
    {
73 46
        return $this->getWidth() * $this->getLength();
74
    }
75
76
    public function getStartX(): int
77
    {
78
        return $this->startX;
79
    }
80
81 82
    public function getEndX(): int
82
    {
83 82
        return $this->endX;
84
    }
85
86 46
    public function getWidth(): int
87
    {
88 46
        return $this->endX ? $this->endX - $this->startX : 0;
89
    }
90
91
    public function getStartY(): int
92
    {
93
        return $this->startY;
94
    }
95
96 82
    public function getEndY(): int
97
    {
98 82
        return $this->endY;
99
    }
100
101 46
    public function getLength(): int
102
    {
103 46
        return $this->endY ? $this->endY - $this->startY : 0;
104
    }
105
106 82
    public function getStartZ(): int
107
    {
108 82
        return $this->startZ;
109
    }
110
111
    public function getEndZ(): int
112
    {
113
        return $this->endZ;
114
    }
115
116 82
    public function getDepth(): int
117
    {
118 82
        return $this->endZ ? $this->endZ - $this->startZ : 0;
119
    }
120
121
    public function getWeight(): int
122
    {
123
        return $this->weight;
124
    }
125
126 82
    public function merge(self $otherLayer): void
127
    {
128 82
        foreach ($otherLayer->items as $packedItem) {
129 8
            $this->insert($packedItem);
130
        }
131
    }
132
}
133