-
Notifications
You must be signed in to change notification settings - Fork 0
/
maxrects.go
427 lines (361 loc) · 12.3 KB
/
maxrects.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package rectpack
import "math"
type heuristicFunc func(pack *maxRects, width, height int) (Rect, int, int)
type maxRects struct {
algorithmBase
findNode heuristicFunc
newLastSize int
newFreeRects []Rect
freeRects []Rect
}
func newMaxRects(width, height int, heuristic Heuristic) *maxRects {
var p maxRects
switch heuristic & fitMask {
case BestAreaFit:
p.findNode = findPositionBestAreaFit
case BottomLeft:
p.findNode = findPositionBottomLeft
case ContactPoint:
p.findNode = findPositionContactPoint
case BestLongSideFit:
p.findNode = findPositionBestLongSideFit
case BestShortSideFit:
p.findNode = findPositionBestShortSideFit
default: // BestShortSideFit
p.findNode = findPositionBestShortSideFit
}
p.Reset(width, height)
return &p
}
func (p *maxRects) Reset(width, height int) {
p.algorithmBase.Reset(width, height)
p.newFreeRects = p.newFreeRects[:0]
p.freeRects = p.freeRects[:0]
p.freeRects = append(p.freeRects, NewRect(0, 0, p.maxWidth, p.maxHeight))
}
func (p *maxRects) Insert(padding int, sizes ...Size) []Size {
for len(sizes) > 0 {
var bestNode Rect
bestScore1 := math.MaxInt
bestScore2 := math.MaxInt
bestRectIndex := -1
for i, size := range sizes {
padSize(&size, padding)
newNode, score1, score2 := p.scoreRect(size.Width, size.Height)
if score1 < bestScore1 || (score1 == bestScore1 && score2 < bestScore2) {
bestScore1 = score1
bestScore2 = score2
bestNode = newNode
bestNode.ID = size.ID
bestRectIndex = i
}
}
if bestRectIndex == -1 {
break
}
p.placeRect(bestNode)
unpadRect(&bestNode, padding)
p.packed = append(p.packed, bestNode)
last := len(sizes) - 1
sizes[bestRectIndex] = sizes[last]
sizes = sizes[:last]
}
return sizes
}
func (p *maxRects) scoreRect(width, height int) (Rect, int, int) {
newNode, score1, score2 := p.findNode(p, width, height)
if newNode.Height == 0 {
score1 = math.MaxInt
score2 = math.MaxInt
}
return newNode, score1, score2
}
func (p *maxRects) placeRect(node Rect) {
for i := 0; i < len(p.freeRects); {
if p.splitFreeNode(&p.freeRects[i], &node) {
last := len(p.freeRects) - 1
p.freeRects[i] = p.freeRects[last]
p.freeRects = p.freeRects[:last]
} else {
i++
}
}
p.pruneFreeList()
p.usedArea += node.Area()
}
func findPositionBottomLeft(p *maxRects, width, height int) (Rect, int, int) {
var bestNode Rect
bestY := math.MaxInt
bestX := math.MaxInt
for _, freeRect := range p.freeRects {
// Try to place the rectangle in upright (non-flipped) orientation.
if freeRect.Width >= width && freeRect.Height >= height {
topSideY := freeRect.Y + height
if topSideY < bestY || (topSideY == bestY && freeRect.X < bestX) {
bestNode.X = freeRect.X
bestNode.Y = freeRect.Y
bestNode.Width = width
bestNode.Height = height
bestY = topSideY
bestX = freeRect.X
}
}
if p.allowFlip && freeRect.Width >= height && freeRect.Height >= width {
topSideY := freeRect.Y + width
if topSideY < bestY || (topSideY == bestY && freeRect.X < bestX) {
bestNode.X = freeRect.X
bestNode.Y = freeRect.Y
bestNode.Width = height
bestNode.Height = width
bestNode.Flipped = true
bestY = topSideY
bestX = freeRect.X
}
}
}
return bestNode, bestY, bestX
}
func findPositionBestShortSideFit(p *maxRects, width, height int) (Rect, int, int) {
var bestNode Rect
bestShortSideFit := math.MaxInt
bestLongSideFit := math.MaxInt
for _, freeRect := range p.freeRects {
// Try to place the rectangle in upright (non-flipped) orientation.
if freeRect.Width >= width && freeRect.Height >= height {
leftoverHoriz := abs(freeRect.Width - width)
leftoverVert := abs(freeRect.Height - height)
shortSideFit := min(leftoverHoriz, leftoverVert)
longSideFit := max(leftoverHoriz, leftoverVert)
if shortSideFit < bestShortSideFit || (shortSideFit == bestShortSideFit && longSideFit < bestLongSideFit) {
bestNode.X = freeRect.X
bestNode.Y = freeRect.Y
bestNode.Width = width
bestNode.Height = height
bestShortSideFit = shortSideFit
bestLongSideFit = longSideFit
}
}
if p.allowFlip && freeRect.Width >= height && freeRect.Height >= width {
flippedLeftoverHoriz := abs(freeRect.Width - height)
flippedLeftoverVert := abs(freeRect.Height - width)
flippedShortSideFit := min(flippedLeftoverHoriz, flippedLeftoverVert)
flippedLongSideFit := max(flippedLeftoverHoriz, flippedLeftoverVert)
if flippedShortSideFit < bestShortSideFit || (flippedShortSideFit == bestShortSideFit && flippedLongSideFit < bestLongSideFit) {
bestNode.X = freeRect.X
bestNode.Y = freeRect.Y
bestNode.Width = height
bestNode.Height = width
bestNode.Flipped = true
bestShortSideFit = flippedShortSideFit
bestLongSideFit = flippedLongSideFit
}
}
}
return bestNode, bestShortSideFit, bestLongSideFit
}
func findPositionBestLongSideFit(p *maxRects, width, height int) (Rect, int, int) {
var bestNode Rect
bestShortSideFit := math.MaxInt
bestLongSideFit := math.MaxInt
for _, freeRect := range p.freeRects {
// Try to place the rectangle in upright (non-flipped) orientation.
if freeRect.Width >= width && freeRect.Height >= height {
leftoverHoriz := abs(freeRect.Width - width)
leftoverVert := abs(freeRect.Height - height)
shortSideFit := min(leftoverHoriz, leftoverVert)
longSideFit := max(leftoverHoriz, leftoverVert)
if longSideFit < bestLongSideFit || (longSideFit == bestLongSideFit && shortSideFit < bestShortSideFit) {
bestNode.X = freeRect.X
bestNode.Y = freeRect.Y
bestNode.Width = width
bestNode.Height = height
bestShortSideFit = shortSideFit
bestLongSideFit = longSideFit
}
}
if p.allowFlip && freeRect.Width >= height && freeRect.Height >= width {
leftoverHoriz := abs(freeRect.Width - height)
leftoverVert := abs(freeRect.Height - width)
shortSideFit := min(leftoverHoriz, leftoverVert)
longSideFit := max(leftoverHoriz, leftoverVert)
if longSideFit < bestLongSideFit || (longSideFit == bestLongSideFit && shortSideFit < bestShortSideFit) {
bestNode.X = freeRect.X
bestNode.Y = freeRect.Y
bestNode.Width = height
bestNode.Height = width
bestNode.Flipped = true
bestShortSideFit = shortSideFit
bestLongSideFit = longSideFit
}
}
}
return bestNode, bestShortSideFit, bestLongSideFit
}
func findPositionBestAreaFit(p *maxRects, width, height int) (Rect, int, int) {
var bestNode Rect
bestAreaFit := math.MaxInt
bestShortSideFit := math.MaxInt
for _, freeRect := range p.freeRects {
areaFit := freeRect.Width*freeRect.Height - width*height
// Try to place the rectangle in upright (non-flipped) orientation.
if freeRect.Width >= width && freeRect.Height >= height {
leftoverHoriz := abs(freeRect.Width - width)
leftoverVert := abs(freeRect.Height - height)
shortSideFit := min(leftoverHoriz, leftoverVert)
if areaFit < bestAreaFit || (areaFit == bestAreaFit && shortSideFit < bestShortSideFit) {
bestNode.X = freeRect.X
bestNode.Y = freeRect.Y
bestNode.Width = width
bestNode.Height = height
bestShortSideFit = shortSideFit
bestAreaFit = areaFit
}
}
if p.allowFlip && freeRect.Width >= height && freeRect.Height >= width {
leftoverHoriz := abs(freeRect.Width - height)
leftoverVert := abs(freeRect.Height - width)
shortSideFit := min(leftoverHoriz, leftoverVert)
if areaFit < bestAreaFit || (areaFit == bestAreaFit && shortSideFit < bestShortSideFit) {
bestNode.X = freeRect.X
bestNode.Y = freeRect.Y
bestNode.Width = height
bestNode.Height = width
bestNode.Flipped = true
bestShortSideFit = shortSideFit
bestAreaFit = areaFit
}
}
}
return bestNode, bestAreaFit, bestShortSideFit
}
// Returns 0 if the two intervals i1 and i2 are disjoint, or the length of their overlap otherwise
func (p *maxRects) commonIntervalLength(i1start, i1end, i2start, i2end int) int {
if i1end < i2start || i2end < i1start {
return 0
}
return min(i1end, i2end) - max(i1start, i2start)
}
func (p *maxRects) contactPointScoreNode(x, y, width, height int) int {
score := 0
if x == 0 || x+width == p.maxWidth {
score += height
}
if y == 0 || y+height == p.maxHeight {
score += width
}
for _, used := range p.packed {
if used.X == x+width || used.X+used.Width == x {
score += p.commonIntervalLength(used.Y, used.Y+used.Height, y, y+height)
}
if used.Y == y+height || used.Y+used.Height == y {
score += p.commonIntervalLength(used.X, used.X+used.Width, x, x+width)
}
}
return score
}
func findPositionContactPoint(p *maxRects, width, height int) (Rect, int, int) {
var bestNode Rect
bestContactScore := -1
for _, freeRect := range p.freeRects {
// Try to place the rectangle in upright (non-flipped) orientation.
if freeRect.Width >= width && freeRect.Height >= height {
score := p.contactPointScoreNode(freeRect.X, freeRect.Y, width, height)
if score > bestContactScore {
bestNode.X = freeRect.X
bestNode.Y = freeRect.Y
bestNode.Width = width
bestNode.Height = height
bestContactScore = score
}
}
if p.allowFlip && freeRect.Width >= height && freeRect.Height >= width {
score := p.contactPointScoreNode(freeRect.X, freeRect.Y, height, width)
if score > bestContactScore {
bestNode.X = freeRect.X
bestNode.Y = freeRect.Y
bestNode.Width = height
bestNode.Height = width
bestNode.Flipped = true
bestContactScore = score
}
}
}
return bestNode, bestContactScore, math.MaxInt
}
func (p *maxRects) insertNewFreeRectangle(newFreeRect Rect) {
for i := 0; i < p.newLastSize; {
// This new free rectangle is already accounted for?
if p.newFreeRects[i].ContainsRect(newFreeRect) {
return
}
// Does this new free rectangle obsolete a previous new free rectangle?
if newFreeRect.ContainsRect(p.newFreeRects[i]) {
// Remove i'th new free rectangle, but do so by retaining the order
// of the older vs newest free rectangles that we may still be placing
// in calling function SplitFreeNode().
p.newLastSize--
p.newFreeRects[i] = p.newFreeRects[p.newLastSize]
last := len(p.newFreeRects) - 1
p.newFreeRects[p.newLastSize] = p.newFreeRects[last]
p.newFreeRects = p.newFreeRects[:last]
continue
}
i++
}
p.newFreeRects = append(p.newFreeRects, newFreeRect)
}
func (p *maxRects) splitFreeNode(freeNode, usedNode *Rect) bool {
if usedNode.X >= freeNode.X+freeNode.Width || usedNode.X+usedNode.Width <= freeNode.X || usedNode.Y >= freeNode.Y+freeNode.Height || usedNode.Y+usedNode.Height <= freeNode.Y {
return false
}
p.newLastSize = len(p.newFreeRects)
if usedNode.X < freeNode.X+freeNode.Width && usedNode.X+usedNode.Width > freeNode.X {
// New node at the top side of the used node.
if usedNode.Y > freeNode.Y && usedNode.Y < freeNode.Y+freeNode.Height {
newNode := *freeNode
newNode.Height = usedNode.Y - newNode.Y
p.insertNewFreeRectangle(newNode)
}
// New node at the bottom side of the used node.
if usedNode.Y+usedNode.Height < freeNode.Y+freeNode.Height {
newNode := *freeNode
newNode.Y = usedNode.Y + usedNode.Height
newNode.Height = freeNode.Y + freeNode.Height - (usedNode.Y + usedNode.Height)
p.insertNewFreeRectangle(newNode)
}
}
if usedNode.Y < freeNode.Y+freeNode.Height && usedNode.Y+usedNode.Height > freeNode.Y {
// New node at the left side of the used node.
if usedNode.X > freeNode.X && usedNode.X < freeNode.X+freeNode.Width {
newNode := *freeNode
newNode.Width = usedNode.X - newNode.X
p.insertNewFreeRectangle(newNode)
}
// New node at the right side of the used node.
if usedNode.X+usedNode.Width < freeNode.X+freeNode.Width {
newNode := *freeNode
newNode.X = usedNode.X + usedNode.Width
newNode.Width = freeNode.X + freeNode.Width - (usedNode.X + usedNode.Width)
p.insertNewFreeRectangle(newNode)
}
}
return true
}
func (p *maxRects) pruneFreeList() {
// Test all newly introduced free rectangles against old free rectangles.
for i := 0; i < len(p.freeRects); i++ {
for j := 0; j < len(p.newFreeRects); {
if p.freeRects[i].ContainsRect(p.newFreeRects[j]) {
last := len(p.newFreeRects) - 1
p.newFreeRects[j] = p.newFreeRects[last]
p.newFreeRects = p.newFreeRects[:last]
continue
}
j++
}
}
// Merge new and old free rectangles to the group of old free rectangles.
p.freeRects = append(p.freeRects, p.newFreeRects...)
p.newFreeRects = p.newFreeRects[:0]
}
// vim: ts=4