-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
117 lines (95 loc) · 2.58 KB
/
item.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package boxpacker3
import (
"math"
"golang.org/x/exp/slices"
)
type Item struct {
id string
width float64
height float64
depth float64
weight float64
volume float64
maxLength float64
rotationType RotationType
position Pivot
}
type itemSlice []*Item
func (it itemSlice) Len() int {
return len(it)
}
func (it itemSlice) Less(i, j int) bool {
return it[i].volume < it[j].volume
}
func (it itemSlice) Swap(i, j int) {
it[i], it[j] = it[j], it[i]
}
func NewItem(id string, w, h, d, wg float64) *Item {
//nolint:exhaustruct
return &Item{
id: id,
width: w,
height: h,
depth: d,
weight: wg,
volume: w * h * d,
maxLength: slices.Max([]float64{w, h, d}),
}
}
func (i *Item) GetID() string {
return i.id
}
func (i *Item) GetWidth() float64 {
return i.width
}
func (i *Item) GetHeight() float64 {
return i.height
}
func (i *Item) GetDepth() float64 {
return i.depth
}
func (i *Item) GetVolume() float64 {
return i.volume
}
func (i *Item) GetWeight() float64 {
return i.weight
}
func (i *Item) GetPosition() Pivot {
return i.position
}
func (i *Item) GetDimension() Dimension {
switch i.rotationType {
case RotationTypeWhd:
return Dimension{i.GetWidth(), i.GetHeight(), i.GetDepth()}
case RotationTypeHwd:
return Dimension{i.GetHeight(), i.GetWidth(), i.GetDepth()}
case RotationTypeHdw:
return Dimension{i.GetHeight(), i.GetDepth(), i.GetWidth()}
case RotationTypeDhw:
return Dimension{i.GetDepth(), i.GetHeight(), i.GetWidth()}
case RotationTypeDwh:
return Dimension{i.GetDepth(), i.GetWidth(), i.GetHeight()}
case RotationTypeWdh:
return Dimension{i.GetWidth(), i.GetDepth(), i.GetHeight()}
default: // RotationTypeWhd
return Dimension{i.GetWidth(), i.GetHeight(), i.GetDepth()}
}
}
// Intersect Tests for intersections between the i element and the it element.
func (i *Item) Intersect(it *Item) bool {
d1 := i.GetDimension()
d2 := it.GetDimension()
return rectIntersect(d1, d2, i, it, WidthAxis, HeightAxis) &&
rectIntersect(d1, d2, i, it, HeightAxis, DepthAxis) &&
rectIntersect(d1, d2, i, it, WidthAxis, DepthAxis)
}
// rectIntersect Checks if two rectangles intersect from the x and y axes of elements i1 and i2.
func rectIntersect(d1, d2 Dimension, i1, i2 *Item, x, y Axis) bool {
cx1 := i1.position[x] + d1[x]/2 //nolint:gomnd
cy1 := i1.position[y] + d1[y]/2 //nolint:gomnd
cx2 := i2.position[x] + d2[x]/2 //nolint:gomnd
cy2 := i2.position[y] + d2[y]/2 //nolint:gomnd
ix := math.Max(cx1, cx2) - math.Min(cx1, cx2)
iy := math.Max(cy1, cy2) - math.Min(cy1, cy2)
return ix < (d1[x]+d2[x])/2 && iy < (d1[y]+d2[y])/2
}