Completed
Push — issue_187 ( fb1b49...8952f9 )
by Doug
158:46 queued 155:58
created

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