Completed
Push — master ( 1d342b...59040c )
by Doug
19:50
created

OrientatedItem::getTippingPoint()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 3
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
     * Constructor.
53
     *
54
     * @param Item $item
55
     * @param int  $width
56
     * @param int  $length
57
     * @param int  $depth
58
     */
59 21
    public function __construct(Item $item, int $width, int $length, int $depth)
60
    {
61 21
        $this->item = $item;
62 21
        $this->width = $width;
63 21
        $this->length = $length;
64 21
        $this->depth = $depth;
65 21
        $this->surfaceFootprint = $width * $length;
66 21
    }
67
68
    /**
69
     * Item.
70
     *
71
     * @return Item
72
     */
73 21
    public function getItem(): Item
74
    {
75 21
        return $this->item;
76
    }
77
78
    /**
79
     * Item width in mm in it's packed orientation.
80
     *
81
     * @return int
82
     */
83 21
    public function getWidth(): int
84
    {
85 21
        return $this->width;
86
    }
87
88
    /**
89
     * Item length in mm in it's packed orientation.
90
     *
91
     * @return int
92
     */
93 21
    public function getLength(): int
94
    {
95 21
        return $this->length;
96
    }
97
98
    /**
99
     * Item depth in mm in it's packed orientation.
100
     *
101
     * @return int
102
     */
103 21
    public function getDepth(): int
104
    {
105 21
        return $this->depth;
106
    }
107
108
    /**
109
     * Calculate the surface footprint of the current orientation.
110
     *
111
     * @return int
112
     */
113 1
    public function getSurfaceFootprint(): int
114
    {
115 1
        return $this->surfaceFootprint;
116
    }
117
118
    /**
119
     * Is this item stable (low centre of gravity), calculated as if the tipping point is >15 degrees.
120
     *
121
     * N.B. Assumes equal weight distribution.
122
     *
123
     * @return bool
124
     */
125 21
    public function isStable(): bool
126
    {
127 21
        $cacheKey = $this->width . '|' . $this->length . '|' . $this->depth;
128 21
        return static::$stabilityCache[$cacheKey] ?? (static::$stabilityCache[$cacheKey] = atan(min($this->length, $this->width) / ($this->depth ?: 1)) > 0.261);
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function jsonSerialize()
135
    {
136
        return [
137
            'item' => $this->item,
138
            'width' => $this->width,
139
            'length' => $this->length,
140
            'depth' => $this->depth,
141
        ];
142
    }
143
144
    /**
145
     * @return string
146
     */
147
    public function __toString(): string
148
    {
149
        return $this->width . '|' . $this->length . '|' . $this->depth;
150
    }
151
}
152