-
Notifications
You must be signed in to change notification settings - Fork 0
/
packer.go
161 lines (125 loc) · 3.3 KB
/
packer.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package boxpacker3
import (
"sort"
"golang.org/x/exp/slices"
)
type Packer struct{}
type Result struct {
UnfitItems itemSlice
Boxes boxSlice
}
func NewPacker() *Packer {
return &Packer{}
}
func (p *Packer) Pack(inputBoxes []*Box, inputItems []*Item) *Result {
boxes := boxSlice(copySlicePtr(inputBoxes))
items := itemSlice(copySlicePtr(inputItems))
sort.Sort(boxes)
sort.Sort(items)
result := &Result{
UnfitItems: nil,
Boxes: p.preferredSort(boxes, items),
}
for _, box := range result.Boxes {
if items = p.packToBox(box, items); len(items) == 0 {
break
}
}
if len(items) > 0 {
result.UnfitItems = append(result.UnfitItems, items...)
}
return result
}
func (p *Packer) preferredSort(boxes boxSlice, items itemSlice) boxSlice {
volume := 0.
weight := 0.
maxLength := 0.
for _, item := range items {
volume += item.GetVolume()
weight += item.GetWeight()
// optimize
if maxLength < item.maxLength {
maxLength = item.maxLength
}
}
for i, b := range boxes {
if b.volume >= volume && b.maxWeight >= weight && b.maxLength >= maxLength {
return append(boxSlice{b}, slices.Delete(boxes, i, i+1)...)
}
}
return boxes
}
// packToBox Packs goods in a box b. Returns unpackaged goods.
//
//nolint:cyclop,gocognit,funlen
func (p *Packer) packToBox(b *Box, items []*Item) []*Item {
var fitted bool
cntItems := len(items)
unpacked := make([]*Item, 0, cntItems)
pv := Pivot{}
if b.items == nil && cntItems > 0 && b.PutItem(items[0], pv) {
items = items[1:]
}
// Packing unpackaged goods.
for _, i := range items {
fitted = false
// Trying anchor points for the box that don't intersect with existing items in the box.
for j := range b.items {
dimension := b.items[j].GetDimension()
for pt := WidthAxis; pt <= DepthAxis && !fitted; pt++ {
pv[WidthAxis] = b.items[j].position[WidthAxis]
pv[HeightAxis] = b.items[j].position[HeightAxis]
pv[DepthAxis] = b.items[j].position[DepthAxis]
switch pt {
case WidthAxis:
pv[WidthAxis] += dimension[WidthAxis]
case HeightAxis:
pv[HeightAxis] += dimension[HeightAxis]
case DepthAxis:
pv[DepthAxis] += dimension[DepthAxis]
}
fitted = b.PutItem(i, pv)
}
}
if !fitted {
backup := copyPtr(b)
copyItems := copySlicePtr(b.items)
b.purge()
if b.PutItem(i, Pivot{}) {
total := len(copyItems)
itemsFit := 0
for k := 0; k < total && itemsFit < total; k++ {
// Trying anchor points for the box that don't intersect with existing items in the box.
for j := len(b.items) - 1; j >= 0; j-- {
dimension := b.items[j].GetDimension()
for pt := WidthAxis; pt <= DepthAxis && k < total; pt++ {
pv[WidthAxis] = b.items[j].position[WidthAxis]
pv[HeightAxis] = b.items[j].position[HeightAxis]
pv[DepthAxis] = b.items[j].position[DepthAxis]
switch pt {
case WidthAxis:
pv[WidthAxis] += dimension[WidthAxis]
case HeightAxis:
pv[HeightAxis] += dimension[HeightAxis]
case DepthAxis:
pv[DepthAxis] += dimension[DepthAxis]
}
if b.PutItem(copyItems[k], pv) {
itemsFit++
break
}
}
}
}
fitted = itemsFit == total
}
if !fitted {
*b = *backup
}
}
if !fitted {
unpacked = append(unpacked, i)
}
}
return unpacked
}