Passed
Push — master ( aa6a1b...7c9e12 )
by Doug
01:50
created

OrientatedItem   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 120
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 9

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getTippingPoint() 0 12 2
A getDepth() 0 3 1
A getWidth() 0 3 1
A __construct() 0 6 1
A getLength() 0 3 1
A getSurfaceFootprint() 0 3 1
A isStable() 0 3 1
A getItem() 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 min;
12
13
/**
14
 * An item to be packed.
15
 *
16
 * @author Doug Wright
17
 */
18
class OrientatedItem
19
{
20
    /**
21
     * @var Item
22
     */
23
    protected $item;
24
25
    /**
26
     * @var int
27
     */
28
    protected $width;
29
30
    /**
31
     * @var int
32
     */
33
    protected $length;
34
35
    /**
36
     * @var int
37
     */
38
    protected $depth;
39
40
    /**
41
     * @var float[]
42
     */
43
    protected static $tippingPointCache = [];
44
45
    /**
46
     * Constructor.
47
     *
48
     * @param Item $item
49
     * @param int  $width
50
     * @param int  $length
51
     * @param int  $depth
52
     */
53 21
    public function __construct(Item $item, int $width, int $length, int $depth)
54
    {
55 21
        $this->item = $item;
56 21
        $this->width = $width;
57 21
        $this->length = $length;
58 21
        $this->depth = $depth;
59 21
    }
60
61
    /**
62
     * Item.
63
     *
64
     * @return Item
65
     */
66 21
    public function getItem(): Item
67
    {
68 21
        return $this->item;
69
    }
70
71
    /**
72
     * Item width in mm in it's packed orientation.
73
     *
74
     * @return int
75
     */
76 21
    public function getWidth(): int
77
    {
78 21
        return $this->width;
79
    }
80
81
    /**
82
     * Item length in mm in it's packed orientation.
83
     *
84
     * @return int
85
     */
86 21
    public function getLength(): int
87
    {
88 21
        return $this->length;
89
    }
90
91
    /**
92
     * Item depth in mm in it's packed orientation.
93
     *
94
     * @return int
95
     */
96 21
    public function getDepth(): int
97
    {
98 21
        return $this->depth;
99
    }
100
101
    /**
102
     * Calculate the surface footprint of the current orientation.
103
     *
104
     * @return int
105
     */
106 13
    public function getSurfaceFootprint(): int
107
    {
108 13
        return $this->width * $this->length;
109
    }
110
111
    /**
112
     * @return float
113
     */
114 21
    public function getTippingPoint(): float
115
    {
116 21
        $cacheKey = $this->width . '|' . $this->length . '|' . $this->depth;
117
118 21
        if (isset(static::$tippingPointCache[$cacheKey])) {
119 21
            $tippingPoint = static::$tippingPointCache[$cacheKey];
120
        } else {
121 18
            $tippingPoint = atan(min($this->length, $this->width) / $this->depth);
122 18
            static::$tippingPointCache[$cacheKey] = $tippingPoint;
123
        }
124
125 21
        return $tippingPoint;
126
    }
127
128
    /**
129
     * Is this item stable (low centre of gravity), calculated as if the tipping point is >15 degrees.
130
     *
131
     * N.B. Assumes equal weight distribution.
132
     *
133
     * @return bool
134
     */
135 21
    public function isStable(): bool
136
    {
137 21
        return $this->getTippingPoint() > 0.261;
138
    }
139
}
140