-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRectangleBinPack.pas
588 lines (529 loc) · 19.6 KB
/
RectangleBinPack.pas
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
{
Copyright (c) 2021 by Ryan Joseph
Based on the Public Domain MaxRectsBinPack.cpp source by Jukka Jylänki
https://github.com/juj/RectangleBinPack/
}
{$mode objfpc}
{$modeswitch autoderef}
unit RectangleBinPack;
interface
uses
GLCanvas, Math, FGL;
type
TBinPacker = class
public type
TRect = TRecti;
TRectList = specialize TFPGList<TRect>;
TChoiceHeuristic = (
RectBestShortSideFit, // BSSF: Positions the rectangle against the short side of a free rectangle into which it fits the best.
RectBestLongSideFit, // BLSF: Positions the rectangle against the long side of a free rectangle into which it fits the best.
RectBestAreaFit, // BAF: Positions the rectangle into the smallest free rect into which it fits.
RectBottomLeftRule, // BL: Does the Tetris placement.
RectContactPointRule // CP: Choosest the placement where the rectangle touches other rects as much as possible.
);
private
binWidth: integer;
binHeight: integer;
allowRotations: boolean;
procedure PlaceRect(node: TRect);
function ScoreRect(width, height: integer; method: TChoiceHeuristic; out score1, score2: integer): TRect;
function FindPositionForNewNodeBottomLeft(width, height: integer; var bestY, bestX: integer): TRect;
function FindPositionForNewNodeBestShortSideFit(width, height: integer; var bestShortSideFit, bestLongSideFit: integer): TRect;
function FindPositionForNewNodeBestLongSideFit(width, height: integer; var bestShortSideFit, bestLongSideFit: integer): TRect;
function FindPositionForNewNodeBestAreaFit(width, height: integer; var bestAreaFit, bestShortSideFit: integer): TRect;
function CommonIntervalLength(i1start, i1end, i2start, i2end: integer): integer;
function ContactPointScoreNode(x, y, width, height: integer): integer;
function FindPositionForNewNodeContactPoint(width, height: integer; var bestContactScore: integer): TRect;
function SplitFreeNode(freeNode: TRect; var usedNode: TRect): boolean;
procedure PruneFreeList;
function IsContainedIn(a, b: TRect): boolean; inline;
public
usedRectangles: TRectList;
freeRectangles: TRectList;
public
{ Constructors }
constructor Create(width, height: integer; rotations: boolean = true);
destructor Destroy; override;
{ Methods }
function Insert(width, height: integer; method: TChoiceHeuristic = RectBestShortSideFit): TRect;
procedure Insert(rects, dst: TRectList; method: TChoiceHeuristic = RectBestShortSideFit);
function Occupancy: float;
end;
implementation
function TBinPacker.Insert(width, height: integer; method: TChoiceHeuristic): TRect;
var
newNode: TRect;
i,
score1,
score2,
numRectanglesToProcess: integer;
begin
score1 := 0;
score2 := 0;
case method of
RectBestShortSideFit:
newNode := FindPositionForNewNodeBestShortSideFit(width, height, score1, score2);
RectBottomLeftRule:
newNode := FindPositionForNewNodeBottomLeft(width, height, score1, score2);
RectContactPointRule:
newNode := FindPositionForNewNodeContactPoint(width, height, score1);
RectBestLongSideFit:
newNode := FindPositionForNewNodeBestLongSideFit(width, height, score2, score1);
RectBestAreaFit:
newNode := FindPositionForNewNodeBestAreaFit(width, height, score1, score2);
end;
if newNode.height = 0 then
exit(newNode);
numRectanglesToProcess := freeRectangles.Count;
i := 0;
while i < numRectanglesToProcess do
begin
if SplitFreeNode(freeRectangles[i], newNode) then
begin
freeRectangles.Delete(i);
Dec(i);
Dec(numRectanglesToProcess);
end;
Inc(i);
end;
PruneFreeList;
usedRectangles.Add(newNode);
result := newNode;
end;
procedure TBinPacker.Insert(rects, dst: TRectList; method: TChoiceHeuristic);
var
bestScore1: integer = MaxInt;
bestScore2: integer = MaxInt;
bestRectIndex: integer = -1;
bestNode: TRect;
i,
score1,
score2: integer;
newNode: TRect;
begin
dst.Clear;
while rects.Count > 0 do
begin
for i := 0 to rects.Count - 1 do
begin
score1 := 0;
score2 := 0;
newNode := ScoreRect(rects[i].width, rects[i].height, method, score1, score2);
if (score1 < bestScore1) or ((score1 = bestScore1) and (score2 < bestScore2)) then
begin
bestScore1 := score1;
bestScore2 := score2;
bestNode := newNode;
bestRectIndex := i;
end;
end;
if bestRectIndex = -1 then
exit;
PlaceRect(bestNode);
rects.Delete(bestRectIndex);
end;
end;
procedure TBinPacker.PlaceRect(node: TRect);
var
numRectanglesToProcess: integer;
i: integer;
begin
numRectanglesToProcess := freeRectangles.Count;
i := 0;
while i < numRectanglesToProcess do
begin
if SplitFreeNode(freeRectangles[i], node) then
begin
freeRectangles.Delete(i);
Dec(i);
Dec(numRectanglesToProcess);
end
else
Inc(i);
end;
PruneFreeList;
usedRectangles.Add(node);
end;
function TBinPacker.ScoreRect(width, height: integer; method: TChoiceHeuristic; out score1, score2: integer): TRect;
var
newNode: TRect;
begin
score1 := MaxInt;
score2 := MaxInt;
case method of
RectBestShortSideFit:
newNode := FindPositionForNewNodeBestShortSideFit(width, height, score1, score2);
RectBottomLeftRule:
newNode := FindPositionForNewNodeBottomLeft(width, height, score1, score2);
RectContactPointRule:
begin
newNode := FindPositionForNewNodeContactPoint(width, height, score1);
score1 := -score1; // Reverse since we are minimizing, but for contact point score bigger is better.
end;
RectBestLongSideFit:
newNode := FindPositionForNewNodeBestLongSideFit(width, height, score2, score1);
RectBestAreaFit:
newNode := FindPositionForNewNodeBestAreaFit(width, height, score1, score2);
end;
// Cannot fit the current rectangle.
if newNode.height = 0 then
begin
score1 := MaxInt;
score2 := MaxInt;
end;
result := newNode;
end;
{ Computes the ratio of used surface area. }
function TBinPacker.Occupancy: float;
var
usedSurfaceArea: longint;
i: integer;
begin
usedSurfaceArea := 0;
for i := 0 to usedRectangles.Count - 1 do
usedSurfaceArea += usedRectangles[i].width * usedRectangles[i].height;
result := usedSurfaceArea / (binWidth * binHeight);
end;
function TBinPacker.FindPositionForNewNodeBottomLeft(width, height: integer; var bestY, bestX: integer): TRect;
var
i,
topSideY: integer;
bestNode: TRect;
begin
bestY := MaxInt;
for i := 0 to freeRectangles.Count - 1 do
begin
// Try to place the rectangle in upright (non-flipped) orientation.
if (freeRectangles[i].width >= width) and (freeRectangles[i].height >= height) then
begin
topSideY := freeRectangles[i].y + height;
if (topSideY < bestY) or ((topSideY = bestY) and (freeRectangles[i].x < bestX)) then
begin
bestNode.x := freeRectangles[i].x;
bestNode.y := freeRectangles[i].y;
bestNode.width := width;
bestNode.height := height;
bestY := topSideY;
bestX := freeRectangles[i].x;
end;
end;
if allowRotations and (freeRectangles[i].width >= height) and (freeRectangles[i].height >= width) then
begin
topSideY := freeRectangles[i].y + width;
if (topSideY < bestY) or ((topSideY = bestY) and (freeRectangles[i].x < bestX)) then
begin
bestNode.x := freeRectangles[i].x;
bestNode.y := freeRectangles[i].y;
bestNode.width := height;
bestNode.height := width;
bestY := topSideY;
bestX := freeRectangles[i].x;
end;
end;
end;
result := bestNode;
end;
function TBinPacker.FindPositionForNewNodeBestShortSideFit(width, height: integer; var bestShortSideFit, bestLongSideFit: integer): TRect;
var
bestNode: TRect;
i,
leftoverHoriz,
leftoverVert,
shortSideFit,
longSideFit,
flippedLeftoverHoriz,
flippedLeftoverVert,
flippedShortSideFit,
flippedLongSideFit: integer;
begin
bestShortSideFit := MaxInt;
for i := 0 to freeRectangles.Count - 1 do
begin
// Try to place the rectangle in upright (non-flipped) orientation.
if (freeRectangles[i].width >= width) and (freeRectangles[i].height >= height) then
begin
leftoverHoriz := Abs(freeRectangles[i].width - width);
leftoverVert := Abs(freeRectangles[i].height - height);
shortSideFit := Min(leftoverHoriz, leftoverVert);
longSideFit := Max(leftoverHoriz, leftoverVert);
if (shortSideFit < bestShortSideFit) or ((shortSideFit = bestShortSideFit) and (longSideFit < bestLongSideFit)) then
begin
bestNode.x := freeRectangles[i].x;
bestNode.y := freeRectangles[i].y;
bestNode.width := width;
bestNode.height := height;
bestShortSideFit := shortSideFit;
bestLongSideFit := longSideFit;
end;
end;
if allowRotations and (freeRectangles[i].width >= height) and (freeRectangles[i].height >= width) then
begin
flippedLeftoverHoriz := Abs(freeRectangles[i].width - height);
flippedLeftoverVert := Abs(freeRectangles[i].height - width);
flippedShortSideFit := Min(flippedLeftoverHoriz, flippedLeftoverVert);
flippedLongSideFit := Max(flippedLeftoverHoriz, flippedLeftoverVert);
if (flippedShortSideFit < bestShortSideFit) or ((flippedShortSideFit = bestShortSideFit) and (flippedLongSideFit < bestLongSideFit)) then
begin
bestNode.x := freeRectangles[i].x;
bestNode.y := freeRectangles[i].y;
bestNode.width := height;
bestNode.height := width;
bestShortSideFit := flippedShortSideFit;
bestLongSideFit := flippedLongSideFit;
end;
end;
end;
result := bestNode;
end;
function TBinPacker.FindPositionForNewNodeBestLongSideFit(width, height: integer; var bestShortSideFit, bestLongSideFit: integer): TRect;
var
bestNode: TRect;
i: integer;
leftoverHoriz,
leftoverVert,
shortSideFit,
longSideFit: integer;
begin
bestLongSideFit := MaxInt;
for i := 0 to freeRectangles.Count - 1 do
begin
// Try to place the rectangle in upright (non-flipped) orientation.
if (freeRectangles[i].width >= width) and (freeRectangles[i].height >= height) then
begin
leftoverHoriz := Abs(freeRectangles[i].width - width);
leftoverVert := Abs(freeRectangles[i].height - height);
shortSideFit := Min(leftoverHoriz, leftoverVert);
longSideFit := Max(leftoverHoriz, leftoverVert);
if (longSideFit < bestLongSideFit) or ((longSideFit = bestLongSideFit) and (shortSideFit < bestShortSideFit)) then
begin
bestNode.x := freeRectangles[i].x;
bestNode.y := freeRectangles[i].y;
bestNode.width := width;
bestNode.height := height;
bestShortSideFit := shortSideFit;
bestLongSideFit := longSideFit;
end;
end;
if allowRotations and (freeRectangles[i].width >= height) and (freeRectangles[i].height >= width) then
begin
leftoverHoriz := Abs(freeRectangles[i].width - height);
leftoverVert := Abs(freeRectangles[i].height - width);
shortSideFit := Min(leftoverHoriz, leftoverVert);
longSideFit := Max(leftoverHoriz, leftoverVert);
if (longSideFit < bestLongSideFit) or ((longSideFit = bestLongSideFit) and (shortSideFit < bestShortSideFit)) then
begin
bestNode.x := freeRectangles[i].x;
bestNode.y := freeRectangles[i].y;
bestNode.width := height;
bestNode.height := width;
bestShortSideFit := shortSideFit;
bestLongSideFit := longSideFit;
end;
end;
end;
result := bestNode;
end;
function TBinPacker.FindPositionForNewNodeBestAreaFit(width, height: integer; var bestAreaFit, bestShortSideFit: integer): TRect;
var
bestNode: TRect;
i,
areaFit,
leftoverHoriz,
leftoverVert,
shortSideFit: integer;
begin
bestAreaFit := MaxInt;
for i := 0 to freeRectangles.Count - 1 do
begin
areaFit := freeRectangles[i].width * freeRectangles[i].height - width * height;
// Try to place the rectangle in upright (non-flipped) orientation.
if (freeRectangles[i].width >= width) and (freeRectangles[i].height >= height) then
begin
leftoverHoriz := Abs(freeRectangles[i].width - width);
leftoverVert := Abs(freeRectangles[i].height - height);
shortSideFit := Min(leftoverHoriz, leftoverVert);
if (areaFit < bestAreaFit) or ((areaFit = bestAreaFit) and (shortSideFit < bestShortSideFit)) then
begin
bestNode.x := freeRectangles[i].x;
bestNode.y := freeRectangles[i].y;
bestNode.width := width;
bestNode.height := height;
bestShortSideFit := shortSideFit;
bestAreaFit := areaFit;
end;
end;
if allowRotations and (freeRectangles[i].width >= height) and (freeRectangles[i].height >= width) then
begin
leftoverHoriz := Abs(freeRectangles[i].width - height);
leftoverVert := Abs(freeRectangles[i].height - width);
shortSideFit := Min(leftoverHoriz, leftoverVert);
if (areaFit < bestAreaFit) or ((areaFit = bestAreaFit) and (shortSideFit < bestShortSideFit)) then
begin
bestNode.x := freeRectangles[i].x;
bestNode.y := freeRectangles[i].y;
bestNode.width := height;
bestNode.height := width;
bestShortSideFit := shortSideFit;
bestAreaFit := areaFit;
end;
end;
end;
result := bestNode;
end;
function TBinPacker.CommonIntervalLength(i1start, i1end, i2start, i2end: integer): integer;
begin
if (i1end < i2start) or (i2end < i1start) then
exit(0);
result := Min(i1end, i2end) - Max(i1start, i2start);
end;
function TBinPacker.ContactPointScoreNode(x, y, width, height: integer): integer;
var
i,
score: integer;
begin
score := 0;
if (x = 0) or (x + width = binWidth) then
score += height;
if (y = 0) or (y + height = binHeight) then
score += width;
for i := 0 to usedRectangles.Count - 1 do
begin
if (usedRectangles[i].x = x + width) or (usedRectangles[i].x + usedRectangles[i].width = x) then
score += CommonIntervalLength(usedRectangles[i].y, usedRectangles[i].y + usedRectangles[i].height, y, y + height);
if (usedRectangles[i].y = y + height) or (usedRectangles[i].y + usedRectangles[i].height = y) then
score += CommonIntervalLength(usedRectangles[i].x, usedRectangles[i].x + usedRectangles[i].width, x, x + width);
end;
result := score;
end;
function TBinPacker.FindPositionForNewNodeContactPoint(width, height: integer; var bestContactScore: integer): TRect;
var
bestNode: TRect;
i,
score: integer;
begin
bestContactScore := -1;
for i := 0 to freeRectangles.Count - 1 do
begin
// Try to place the rectangle in upright (non-flipped) orientation.
if (freeRectangles[i].width >= width) and (freeRectangles[i].height >= height) then
begin
score := ContactPointScoreNode(freeRectangles[i].x, freeRectangles[i].y, width, height);
if (score > bestContactScore) then
begin
bestNode.x := freeRectangles[i].x;
bestNode.y := freeRectangles[i].y;
bestNode.width := width;
bestNode.height := height;
bestContactScore := score;
end;
end;
if allowRotations and (freeRectangles[i].width >= height) and (freeRectangles[i].height >= width) then
begin
score := ContactPointScoreNode(freeRectangles[i].x, freeRectangles[i].y, height, width);
if (score > bestContactScore) then
begin
bestNode.x := freeRectangles[i].x;
bestNode.y := freeRectangles[i].y;
bestNode.width := height;
bestNode.height := width;
bestContactScore := score;
end;
end;
end;
result := bestNode;
end;
function TBinPacker.SplitFreeNode(freeNode: TRect; var usedNode: TRect): boolean;
var
newNode: TRect;
begin
// Test with SAT if the rectangles even intersect.
if (usedNode.x >= freeNode.x + freeNode.width) or (usedNode.x + usedNode.width <= freeNode.x) or
(usedNode.y >= freeNode.y + freeNode.height) or (usedNode.y + usedNode.height <= freeNode.y) then
exit(false);
if (usedNode.x < freeNode.x + freeNode.width) and (usedNode.x + usedNode.width > freeNode.x) then
begin
// New node at the top side of the used node.
if (usedNode.y > freeNode.y) and (usedNode.y < freeNode.y + freeNode.height) then
begin
newNode := freeNode;
newNode.height := usedNode.y - newNode.y;
freeRectangles.Add(newNode);
end;
// New node at the bottom side of the used node.
if usedNode.y + usedNode.height < freeNode.y + freeNode.height then
begin
newNode := freeNode;
newNode.y := usedNode.y + usedNode.height;
newNode.height := freeNode.y + freeNode.height - (usedNode.y + usedNode.height);
freeRectangles.Add(newNode);
end;
end;
if (usedNode.y < freeNode.y + freeNode.height) and (usedNode.y + usedNode.height > freeNode.y) then
begin
// New node at the left side of the used node.
if (usedNode.x > freeNode.x) and (usedNode.x < freeNode.x + freeNode.width) then
begin
newNode := freeNode;
newNode.width := usedNode.x - newNode.x;
freeRectangles.Add(newNode);
end;
// New node at the right side of the used node.
if (usedNode.x + usedNode.width < freeNode.x + freeNode.width) then
begin
newNode := freeNode;
newNode.x := usedNode.x + usedNode.width;
newNode.width := freeNode.x + freeNode.width - (usedNode.x + usedNode.width);
freeRectangles.Add(newNode);
end;
end;
result := true;
end;
procedure TBinPacker.PruneFreeList;
var
i, j: integer;
begin
i := 0;
while i < freeRectangles.Count do
begin
j := i + 1;
while j < freeRectangles.Count do
begin
if (IsContainedIn(freeRectangles[i], freeRectangles[j])) then
begin
freeRectangles.Delete(i);
Dec(i);
break;
end;
if (IsContainedIn(freeRectangles[j], freeRectangles[i])) then
begin
freeRectangles.Delete(j);
Dec(j);
end;
Inc(j);
end;
Inc(i);
end;
end;
function TBinPacker.IsContainedIn(a, b: TRect): boolean;
begin
result := (a.x >= b.x) and
(a.y >= b.y) and
(a.x + a.width <= b.x + b.width) and
(a.y + a.height <= b.y + b.height);
end;
destructor TBinPacker.Destroy;
begin
usedRectangles.Free;
freeRectangles.Free;
inherited;
end;
constructor TBinPacker.Create(width, height: integer; rotations: boolean);
begin
binWidth := width;
binHeight := height;
allowRotations := rotations;
usedRectangles := TRectList.Create;
freeRectangles := TRectList.Create;
freeRectangles.Add(RectMake(0, 0, width, height));
end;
end.