-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbox.go
More file actions
211 lines (165 loc) · 3.87 KB
/
Copy pathbox.go
File metadata and controls
211 lines (165 loc) · 3.87 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package boxpacker3
// Box represents a box that can hold items.
type Box struct {
id string
width float64
height float64
depth float64
maxWeight float64
volume float64
items []*Item
maxLength float64
itemsVolume float64
itemsWeight float64
}
type boxSlice []*Box
func (bs boxSlice) Len() int {
return len(bs)
}
func (bs boxSlice) Less(i, j int) bool {
if bs[i] == nil {
return false
}
if bs[j] == nil {
return true
}
return bs[i].volume < bs[j].volume
}
func (bs boxSlice) Swap(i, j int) {
bs[i], bs[j] = bs[j], bs[i]
}
// NewBox creates a new Box with the given id, dimensions, and maximum weight.
func NewBox(id string, w, h, d, mw float64) *Box {
//nolint:exhaustruct
return &Box{
id: id,
width: w,
height: h,
depth: d,
maxWeight: mw,
maxLength: max(w, h, d),
volume: w * h * d,
items: make([]*Item, 0, 1),
}
}
// NewBox2D creates a new 2D Box with the given id, dimensions, and maximum weight.
// The depth is set to 1, making it effectively 2D (width x height).
// This is useful for packing flat items like sheets, boards, or panels.
func NewBox2D(id string, w, h, mw float64) *Box {
return NewBox(id, w, h, 1, mw)
}
func (b *Box) GetID() string {
return b.id
}
func (b *Box) GetWidth() float64 {
return b.width
}
func (b *Box) GetHeight() float64 {
return b.height
}
func (b *Box) GetDepth() float64 {
return b.depth
}
func (b *Box) GetVolume() float64 {
return b.volume
}
func (b *Box) GetMaxWeight() float64 {
return b.maxWeight
}
// GetItems returns a copy of the items slice.
func (b *Box) GetItems() []*Item {
return append([]*Item(nil), b.items...)
}
func (b *Box) GetRemainingVolume() float64 {
return b.volume - b.itemsVolume
}
func (b *Box) PutItem(item *Item, p Pivot) bool {
if item == nil {
return false
}
if !b.canQuota(item) {
return false
}
item.position = p
for rt := RotationTypeWhd; rt <= RotationTypeWdh; rt++ {
matrix := rotationMatrix[rt]
//nolint:gosec // rotationMatrix values are guaranteed to be in range [0, 2] by const definition
itemWidth := item.whd[matrix[WidthAxis]]
//nolint:gosec // rotationMatrix values are guaranteed to be in range [0, 2] by const definition
itemHeight := item.whd[matrix[HeightAxis]]
//nolint:gosec // rotationMatrix values are guaranteed to be in range [0, 2] by const definition
itemDepth := item.whd[matrix[DepthAxis]]
if b.width < p[WidthAxis]+itemWidth ||
b.height < p[HeightAxis]+itemHeight ||
b.depth < p[DepthAxis]+itemDepth {
continue
}
item.setRotationType(rt)
if b.itemsIntersect(item) {
continue
}
b.insert(item)
return true
}
return false
}
func (b *Box) itemsIntersect(item *Item) bool {
if item == nil {
return false
}
for _, ib := range b.items {
if ib != nil && ib.Intersect(item) {
return true
}
}
return false
}
func (b *Box) canQuota(item *Item) bool {
if item == nil {
return false
}
return b.canFitVolume(item) && b.canFitWeight(item)
}
func (b *Box) canFitVolume(item *Item) bool {
if item == nil {
return false
}
return b.itemsVolume+item.volume <= b.volume
}
func (b *Box) canFitWeight(item *Item) bool {
if item == nil {
return false
}
return b.itemsWeight+item.weight <= b.maxWeight
}
//nolint:ireturn
func (b *Box) clone() cloner {
copyBox := &Box{
id: b.id,
width: b.width,
height: b.height,
depth: b.depth,
maxWeight: b.maxWeight,
volume: b.volume,
maxLength: b.maxLength,
itemsVolume: b.itemsVolume,
itemsWeight: b.itemsWeight,
}
if b.items != nil {
copyBox.items = make([]*Item, len(b.items), cap(b.items))
copy(copyBox.items, b.items)
} else {
copyBox.items = nil
}
return copyBox
}
func (b *Box) insert(item *Item) {
b.items = append(b.items, item)
b.itemsVolume += item.volume
b.itemsWeight += item.weight
}
func (b *Box) Reset() {
b.items = b.items[:0]
b.itemsVolume = 0
b.itemsWeight = 0
}