Completed
Push — test_jit ( c34f6e...87859e )
by Doug
10:03
created

OrientatedItem::isSameDimensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 1
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 JsonSerializable;
12
use function min;
13
14
/**
15
 * An item to be packed.
16
 *
17
 * @author Doug Wright
18
 */
19
class OrientatedItem implements JsonSerializable
20
{
21
    /**
22
     * @var Item
23
     */
24
    protected $item;
25
26
    /**
27
     * @var int
28
     */
29
    protected $width;
30
31
    /**
32
     * @var int
33
     */
34
    protected $length;
35
36
    /**
37
     * @var int
38
     */
39
    protected $depth;
40
41
    /**
42
     * @var int
43
     */
44
    protected $surfaceFootprint;
45
46
    /**
47
     * @var bool[]
48
     */
49
    protected static $stabilityCache = [];
50
51
    /**
52
     * @var array
53
     */
54
    protected $dimensionsAsArray;
55
56
    /**
57
     * Constructor.
58
     */
59 57
    public function __construct(Item $item, int $width, int $length, int $depth)
60
    {
61 57
        $this->item = $item;
62 57
        $this->width = $width;
63 57
        $this->length = $length;
64 57
        $this->depth = $depth;
65 57
        $this->surfaceFootprint = $width * $length;
66
67 57
        $this->dimensionsAsArray = [$width, $length, $depth];
68 57
        sort($this->dimensionsAsArray);
69 57
    }
70
71
    /**
72
     * Item.
73
     */
74 56
    public function getItem(): Item
75
    {
76 56
        return $this->item;
77
    }
78
79
    /**
80
     * Item width in mm in it's packed orientation.
81
     */
82 56
    public function getWidth(): int
83
    {
84 56
        return $this->width;
85
    }
86
87
    /**
88
     * Item length in mm in it's packed orientation.
89
     */
90 56
    public function getLength(): int
91
    {
92 56
        return $this->length;
93
    }
94
95
    /**
96
     * Item depth in mm in it's packed orientation.
97
     */
98 56
    public function getDepth(): int
99
    {
100 56
        return $this->depth;
101
    }
102
103
    /**
104
     * Calculate the surface footprint of the current orientation.
105
     */
106 30
    public function getSurfaceFootprint(): int
107
    {
108 30
        return $this->surfaceFootprint;
109
    }
110
111
    /**
112
     * Is this item stable (low centre of gravity), calculated as if the tipping point is >15 degrees.
113
     *
114
     * N.B. Assumes equal weight distribution.
115
     */
116 56
    public function isStable(): bool
117
    {
118 56
        $cacheKey = $this->width . '|' . $this->length . '|' . $this->depth;
119
120 56
        return static::$stabilityCache[$cacheKey] ?? (static::$stabilityCache[$cacheKey] = atan(min($this->length, $this->width) / ($this->depth ?: 1)) > 0.261);
121
    }
122
123
    /**
124
     * Is the supplied item the same size as this one?
125
     * @internal
126
     */
127 50
    public function isSameDimensions(Item $item): bool
128
    {
129 50
        $itemDimensions = [$item->getWidth(), $item->getLength(), $item->getDepth()];
130 50
        sort($itemDimensions);
131
132 50
        return $this->dimensionsAsArray === $itemDimensions;
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 1
    public function jsonSerialize()
139
    {
140
        return [
141 1
            'item' => $this->item,
142 1
            'width' => $this->width,
143 1
            'length' => $this->length,
144 1
            'depth' => $this->depth,
145
        ];
146
    }
147
148 1
    public function __toString(): string
149
    {
150 1
        return $this->width . '|' . $this->length . '|' . $this->depth;
151
    }
152
}
153