Passed
Push — master ( f6d69b...61d457 )
by Doug
03:39
created

OrientatedItem::__toString()   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
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 float[]
43
     */
44
    protected static $tippingPointCache = [];
45
46
    /**
47
     * Constructor.
48
     *
49
     * @param Item $item
50
     * @param int  $width
51
     * @param int  $length
52
     * @param int  $depth
53
     */
54 21
    public function __construct(Item $item, int $width, int $length, int $depth)
55
    {
56 21
        $this->item = $item;
57 21
        $this->width = $width;
58 21
        $this->length = $length;
59 21
        $this->depth = $depth;
60 21
    }
61
62
    /**
63
     * Item.
64
     *
65
     * @return Item
66
     */
67 21
    public function getItem(): Item
68
    {
69 21
        return $this->item;
70
    }
71
72
    /**
73
     * Item width in mm in it's packed orientation.
74
     *
75
     * @return int
76
     */
77 21
    public function getWidth(): int
78
    {
79 21
        return $this->width;
80
    }
81
82
    /**
83
     * Item length in mm in it's packed orientation.
84
     *
85
     * @return int
86
     */
87 21
    public function getLength(): int
88
    {
89 21
        return $this->length;
90
    }
91
92
    /**
93
     * Item depth in mm in it's packed orientation.
94
     *
95
     * @return int
96
     */
97 21
    public function getDepth(): int
98
    {
99 21
        return $this->depth;
100
    }
101
102
    /**
103
     * Calculate the surface footprint of the current orientation.
104
     *
105
     * @return int
106
     */
107 1
    public function getSurfaceFootprint(): int
108
    {
109 1
        return $this->width * $this->length;
110
    }
111
112
    /**
113
     * @return float
114
     */
115 21
    public function getTippingPoint(): float
116
    {
117 21
        $cacheKey = $this->width . '|' . $this->length . '|' . $this->depth;
118
119 21
        if (isset(static::$tippingPointCache[$cacheKey])) {
120 21
            $tippingPoint = static::$tippingPointCache[$cacheKey];
121
        } else {
122 18
            $tippingPoint = atan(min($this->length, $this->width) / ($this->depth ?: 1));
123 18
            static::$tippingPointCache[$cacheKey] = $tippingPoint;
124
        }
125
126 21
        return $tippingPoint;
127
    }
128
129
    /**
130
     * Is this item stable (low centre of gravity), calculated as if the tipping point is >15 degrees.
131
     *
132
     * N.B. Assumes equal weight distribution.
133
     *
134
     * @return bool
135
     */
136 21
    public function isStable(): bool
137
    {
138 21
        return $this->getTippingPoint() > 0.261;
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function jsonSerialize()
145
    {
146
        return [
147
            'item' => $this->item,
148
            'width' => $this->width,
149
            'length' => $this->length,
150
            'depth' => $this->depth,
151
        ];
152
    }
153
154
    /**
155
     * @return string
156
     */
157 21
    public function __toString(): string
158
    {
159 21
        return $this->width . '|' . $this->length . '|' . $this->depth;
160
    }
161
}
162