Passed
Push — row_bookkeeping ( e37357 )
by Doug
07:44
created

PackedLayer::getDepth()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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