-
Notifications
You must be signed in to change notification settings - Fork 0
/
heuristic.go
356 lines (312 loc) · 9.55 KB
/
heuristic.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package rectpack
import (
"errors"
"strings"
)
// Heuristic is a bitfield used for configuration of a rectangle packing algorithm, including the
// general type, bin selection method, and strategy for how to split empty areas. Specific
// combinations of values can be XOR'ed together to achieve the desired behavior.
//
// Note that not not all combinations are valid, each constant of this type will indicate what it
// is valid with. If in doubt, simply use a preset.
//
// To test if a value is valid, use the Validate function, which will return an error message
// describing the issue. When an invalid value is used, the algorithm default will be used, but
// otherwise no error will occur.
type Heuristic uint16
const (
/**********************************************************************************************
* Algorithm types
**********************************************************************************************/
// MaxRects selects the MaxRects algorithm for packing. This generally results in the
// most efficiently packed results when packing to a static size. It can result in a lot of
// waste if a sensible size and bin heustic is not chosen for the given inputs, but has the
// most potential for efficiency.
//
// Type: Algorithm
MaxRects Heuristic = 0x0
// Skyline selects the Skyline algorithm for packing. Skyline provides a good balance between
// speed and efficiency, and is good for maintaining the least amount of waste at any given
// time, making it a good choice for dynamic data and simply using whatever the final size may
// be.
//
// Type: Algorithm
Skyline = 0x1
// Guillotine selects the Guillotine algorithm for packing. This algorithm is typically
// faster, but is much more sensitive to choosing the correct packing/splitting methods for
// specific inputs. This makes it less "general-purpose", but can still be the best choice
// in certain situations where the input sizes are predictable.
//
// Type: Algorithm
Guillotine = 0x2
/**********************************************************************************************
* Bin-Selection
**********************************************************************************************/
// BestShortSideFit (BSSF) positions the rectangle against the short side of a free rectangle
// into which it fits the best.
//
// * Type: Bin-Selection
// * Valid With: MaxRects, Guillotine
BestShortSideFit = 0x00
// BestLongSideFit (BLSF) positions the rectangle against the long side of a free rectangle
// into which it fits the best.
//
// * Type: Bin-Selection
// * Valid With: MaxRects, Guillotine
BestLongSideFit = 0x10
// BestAreaFit (BAF) positions the rectangle into the smallest free rect into which it fits.
//
// * Type: Bin-Selection
// * Valid With: MaxRects, Guillotine
BestAreaFit = 0x20
// BottomLeft (BL) does the Tetris placement.
//
// * Type: Bin-Selection
// * Valid With: MaxRects, Skyline
BottomLeft = 0x30
// ContactPoint (CP) choosest the placement where the rectangle touches other rects as much
// as possible.
//
// * Type: Bin-Selection
// * Valid With: MaxRects
ContactPoint = 0x40
// WorstAreaFit (WAF) is the opposite of the BestAreaFit (BAF) heuristic. Contrary to its
// name, this is not always "worse" with speciifc inputs.
//
// * Type: Bin-Selection
// * Valid With: Guillotine
WorstAreaFit = 0x50
// WorstShortSideFit (WSSF) is the opposite of the BestShortSideFit (BSSF) heuristic. Contrary
// to its name, this is not always "worse" with speciifc inputs.
//
// * Type: Bin-Selection
// * Valid With: Guillotine
WorstShortSideFit = 0x60
// WorstLongSideFit (WLSF) is the opposite of the BestLongSideFit (BLSF) heuristic. Contrary
// to its name, this is not always "worse" with speciifc inputs.
//
// * Type: Bin-Selection
// * Valid With: Guillotine
WorstLongSideFit = 0x70
// MinWaste (MW) uses a "waste map" to split empty spaces and determine which placement will
// result in the least amount of wasted space. This is most effective when flip/rotate is
// enabled by the packer.
//
// * Type: Bin-Selection
// * Valid With: Skyline
MinWaste = 0x80
/**********************************************************************************************
* Splitting algorithms (only used with guillotine algorithms)
**********************************************************************************************/
// SplitShorterLeftoverAxis (SLAS)
//
// * Type: Split Method
// * Valid With: Guillotine
SplitShorterLeftoverAxis = 0x0000
// SplitLongerLeftoverAxis (LLAS)
//
// * Type: Split Method
// * Valid With: Guillotine
SplitLongerLeftoverAxis = 0x0100
// SplitMinimizeArea (MINAS) try to make a single big rectangle at the expense of making the
// other small.
//
// * Type: Split Method
// * Valid With: Guillotine
SplitMinimizeArea = 0x0200
// SplitMaximizeArea (MAXAS) try to make both remaining rectangles as even-sized as possible.
//
// *Type: Split Method
// * Valid With: Guillotine
SplitMaximizeArea = 0x0300
// SplitShorterAxis (SAS)
//
// * Type: Split Method
// * Valid With: Guillotine
SplitShorterAxis = 0x0400
// SplitLongerAxis (LAS)
//
// * Type: Split Method
// * Valid With: Guillotine
SplitLongerAxis = 0x0500
/**********************************************************************************************
* Masks for extracting relevant bits
**********************************************************************************************/
typeMask = 0x000F
fitMask = 0x00F0
splitMask = 0x0F00
/**********************************************************************************************
* Present combinations of valid heuristics
**********************************************************************************************/
// MaxRectsBSSF
//
// * Type: Preset
MaxRectsBSSF = MaxRects | BestShortSideFit
// MaxRectsBL
//
// * Type: Preset
MaxRectsBL = MaxRects | BottomLeft
// MaxRectsCP
//
// * Type: Preset
MaxRectsCP = MaxRects | ContactPoint
// MaxRectsBLSF
//
// * Type: Preset
MaxRectsBLSF = MaxRects | BestLongSideFit
// MaxRectsBAF
//
// * Type: Preset
MaxRectsBAF = MaxRects | BestAreaFit
// GuillotineBAF
//
// * Type: Preset
GuillotineBAF = Guillotine | BestAreaFit
// GuillotineBSSF
//
// * Type: Preset
GuillotineBSSF = Guillotine | BestShortSideFit
// GuillotineBLSF
//
// * Type: Preset
GuillotineBLSF = Guillotine | BestLongSideFit
// GuillotineWAF
//
// * Type: Preset
GuillotineWAF = Guillotine | WorstAreaFit
// GuillotineWSSF
//
// * Type: Preset
GuillotineWSSF = Guillotine | WorstShortSideFit
// GuillotineWLSF
//
// * Type: Preset
GuillotineWLSF = Guillotine | WorstLongSideFit
// SkylineBLF
//
// * Type: Preset
SkylineBLF = Skyline | BottomLeft
// SkylineMinWaste
//
// * Type: Preset
SkylineMinWaste = Skyline | MinWaste
)
// Algorithm returns the algorithm portion of the bitmask.
func (e Heuristic) Algorithm() Heuristic {
return e & typeMask
}
// Bin returns the bin selection method portion of the bitmask.
func (e Heuristic) Bin() Heuristic {
return e & fitMask
}
// Split returns the split method portion of the bitmask.
func (e Heuristic) Split() Heuristic {
return e & splitMask
}
var (
algoErr = errors.New("invalid algorithm type specified")
splitErr = errors.New("split method heuristic is invalid for algorithm type and will be ignored")
binErr = errors.New("bin method heuristic is invalid for algorithm type")
)
// Validate tests whether the combination of heuristics are in good form. A value of nil is
// returned upon success, otherwise an error with message explaining the error.
//
// Note that invalid heuristics will silently fail and cause the packer to revert to its default
// for that setting.
func (e Heuristic) Validate() error {
bin := e & fitMask
split := e & splitMask
switch e & typeMask {
case MaxRects:
if split != 0 {
return splitErr
}
switch bin {
case BestShortSideFit, BestAreaFit, BottomLeft, ContactPoint, BestLongSideFit:
default:
return binErr
}
case Skyline:
if split != 0 {
return splitErr
}
switch bin {
case BottomLeft, MinWaste:
default:
return binErr
}
case Guillotine:
switch split {
case SplitShorterLeftoverAxis, SplitLongerLeftoverAxis, SplitMinimizeArea, SplitMaximizeArea, SplitShorterAxis, SplitLongerAxis:
default:
return splitErr
}
switch bin {
case BestShortSideFit, BottomLeft, ContactPoint, BestLongSideFit, BestAreaFit:
default:
return splitErr
}
default:
return algoErr
}
return nil
}
// String returns the string representation of the heuristic.
func (e Heuristic) String() string {
var sb strings.Builder
var split, bin string
switch e & typeMask {
case MaxRects:
sb.WriteString("MaxRects")
case Skyline:
sb.WriteString("Skyline")
case Guillotine:
sb.WriteString("Guillotine")
switch e & splitMask {
case SplitShorterLeftoverAxis:
split = "-SLAS"
case SplitLongerLeftoverAxis:
split = "-LLAS"
case SplitMinimizeArea:
split = "-MINAS"
case SplitMaximizeArea:
split = "-MAXAS"
case SplitShorterAxis:
split = "-SAS"
case SplitLongerAxis:
split = "-LAS"
}
}
switch e & fitMask {
case BestShortSideFit:
bin = "BSSF"
case BestLongSideFit:
bin = "BLSF"
case BestAreaFit:
bin = "BAF"
case BottomLeft:
bin = "BL"
case ContactPoint:
bin = "CP"
case WorstAreaFit:
bin = "WAF"
case WorstShortSideFit:
bin = "WSSF"
case WorstLongSideFit:
bin = "WLSF"
case MinWaste:
bin = "MW"
}
if bin != "" {
if sb.Len() > 0 {
sb.WriteRune('-')
}
sb.WriteString(bin)
}
if split != "" {
sb.WriteRune('-')
sb.WriteString(split)
}
return sb.String()
}
// vim: ts=4