rectpackbin 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/launch.json +18 -0
- package/{src/index.js → index.js} +35 -14
- package/package.json +7 -3
- package/src/rectpack/geometry.js +0 -343
- package/src/rectpack/guillotine copy.js +406 -0
- package/src/rectpack/guillotine.js +283 -67
- package/src/rectpack/index.js +90 -0
- package/src/rectpack/packAlgo.js +3 -2
- package/src/rectpack/packer.js +342 -81
- package/src/rectpack/packer2.js +144 -0
- package/test/guillotine/GuillotineBssfSas2.test.js +141 -0
- package/test/packer/packer.test.js +466 -0
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
const { GuillotineBssfSas, Rectangle } = require('../../src/rectpack/guillotine');
|
|
2
|
+
|
|
3
|
+
describe('GuillotineBssfSas', () => {
|
|
4
|
+
let packer;
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
// Initialize a new packer before each test with a 100x200 container
|
|
8
|
+
packer = new GuillotineBssfSas(100, 200);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
describe('initialization', () => {
|
|
12
|
+
it('should initialize with correct dimensions', () => {
|
|
13
|
+
expect(packer.width).toBe(100);
|
|
14
|
+
expect(packer.height).toBe(200);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('should start with an empty rectangles list', () => {
|
|
18
|
+
expect(packer.rectangles).toHaveLength(0);
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('addRect', () => {
|
|
23
|
+
it('should add a rectangle successfully', () => {
|
|
24
|
+
const rect = packer.addRect(50, 30);
|
|
25
|
+
expect(rect).toBeTruthy();
|
|
26
|
+
expect(rect.width).toBe(50);
|
|
27
|
+
expect(rect.height).toBe(30);
|
|
28
|
+
expect(rect.x).toBe(0);
|
|
29
|
+
expect(rect.y).toBe(0);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should return null when rectangle does not fit', () => {
|
|
33
|
+
const rect = packer.addRect(150, 150);
|
|
34
|
+
expect(rect).toBeNull();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should handle multiple rectangles', () => {
|
|
38
|
+
const rect1 = packer.addRect(50, 50);
|
|
39
|
+
const rect2 = packer.addRect(50, 50);
|
|
40
|
+
|
|
41
|
+
expect(rect1).toBeTruthy();
|
|
42
|
+
expect(rect2).toBeTruthy();
|
|
43
|
+
expect(rect1.intersects(rect2)).toBeFalsy();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('should throw error for invalid dimensions', () => {
|
|
47
|
+
expect(() => packer.addRect(0, 50)).toThrow();
|
|
48
|
+
expect(() => packer.addRect(50, 0)).toThrow();
|
|
49
|
+
expect(() => packer.addRect(-10, 50)).toThrow();
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('BSSF (Best Short Side Fit) strategy', () => {
|
|
54
|
+
it('should choose section with minimum short side difference', () => {
|
|
55
|
+
// Add a rectangle that will create two sections
|
|
56
|
+
const rect1 = packer.addRect(60, 40);
|
|
57
|
+
expect(rect1).toBeTruthy();
|
|
58
|
+
|
|
59
|
+
// The next rectangle should be placed in the section with the best short side fit
|
|
60
|
+
const rect2 = packer.addRect(30, 30);
|
|
61
|
+
expect(rect2).toBeTruthy();
|
|
62
|
+
|
|
63
|
+
// Based on BSSF, it should prefer the section with smaller waste on the shorter side
|
|
64
|
+
expect(rect2.x).toBe(60);
|
|
65
|
+
expect(rect2.y).toBe(0);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe('SAS (Shorter Axis Split) strategy', () => {
|
|
70
|
+
it('should split along shorter axis', () => {
|
|
71
|
+
// Add a rectangle that will trigger a split
|
|
72
|
+
const rect = packer.addRect(60, 40);
|
|
73
|
+
expect(rect).toBeTruthy();
|
|
74
|
+
|
|
75
|
+
// The split should create two new sections:
|
|
76
|
+
// 1. A section to the right (width: 40, height: 40)
|
|
77
|
+
// 2. A section below (width: 100, height: 160)
|
|
78
|
+
|
|
79
|
+
// Test that we can place rectangles in both sections
|
|
80
|
+
const rectRight = packer.addRect(35, 35);
|
|
81
|
+
expect(rectRight).toBeTruthy();
|
|
82
|
+
expect(rectRight.x).toBe(60);
|
|
83
|
+
|
|
84
|
+
const rectBottom = packer.addRect(90, 150);
|
|
85
|
+
expect(rectBottom).toBeTruthy();
|
|
86
|
+
expect(rectBottom.y).toBe(40);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe('reset', () => {
|
|
91
|
+
it('should clear all placed rectangles', () => {
|
|
92
|
+
packer.addRect(50, 50);
|
|
93
|
+
packer.addRect(30, 30);
|
|
94
|
+
expect(packer.rectangles).toHaveLength(2);
|
|
95
|
+
|
|
96
|
+
packer.reset();
|
|
97
|
+
expect(packer.rectangles).toHaveLength(0);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should restore initial container state', () => {
|
|
101
|
+
packer.addRect(50, 50);
|
|
102
|
+
packer.reset();
|
|
103
|
+
|
|
104
|
+
// Should be able to add a rectangle using full container dimensions
|
|
105
|
+
const rect = packer.addRect(100, 200);
|
|
106
|
+
expect(rect).toBeTruthy();
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
describe('edge cases', () => {
|
|
111
|
+
it('should handle exact fit rectangles', () => {
|
|
112
|
+
const rect = packer.addRect(100, 200);
|
|
113
|
+
expect(rect).toBeTruthy();
|
|
114
|
+
expect(rect.width).toBe(100);
|
|
115
|
+
expect(rect.height).toBe(200);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('should handle rectangles with one maximum dimension', () => {
|
|
119
|
+
const rect = packer.addRect(100, 100);
|
|
120
|
+
expect(rect).toBeTruthy();
|
|
121
|
+
expect(rect.width).toBe(100);
|
|
122
|
+
expect(rect.height).toBe(100);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('should handle multiple small rectangles efficiently', () => {
|
|
126
|
+
const smallRects = [];
|
|
127
|
+
for (let i = 0; i < 10; i++) {
|
|
128
|
+
const rect = packer.addRect(20, 20);
|
|
129
|
+
expect(rect).toBeTruthy();
|
|
130
|
+
smallRects.push(rect);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Verify no overlaps
|
|
134
|
+
for (let i = 0; i < smallRects.length; i++) {
|
|
135
|
+
for (let j = i + 1; j < smallRects.length; j++) {
|
|
136
|
+
expect(smallRects[i].intersects(smallRects[j])).toBeFalsy();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
});
|
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
const {
|
|
2
|
+
SORT_NONE,
|
|
3
|
+
SORT_AREA,
|
|
4
|
+
SORT_PERI,
|
|
5
|
+
SORT_DIFF,
|
|
6
|
+
SORT_SSIDE,
|
|
7
|
+
SORT_LSIDE,
|
|
8
|
+
SORT_RATIO,
|
|
9
|
+
PackerOnlineBNF,
|
|
10
|
+
PackerOnlineBFF,
|
|
11
|
+
PackerOnlineBBF,
|
|
12
|
+
PackerBNF,
|
|
13
|
+
PackerBFF,
|
|
14
|
+
PackerBBF,
|
|
15
|
+
PackerGlobal,
|
|
16
|
+
PackingMode,
|
|
17
|
+
PackingBin,
|
|
18
|
+
newPacker
|
|
19
|
+
} = require('../../src/rectpack/packer');
|
|
20
|
+
|
|
21
|
+
const { GuillotineBafSas, GuillotineBssfSas, GuillotineBafMaxas, GuillotineBlsfMaxas } = require('../../src/rectpack/guillotine');
|
|
22
|
+
// const { SkylineMwfl, SkylineBl } = require('../../src/rectpack/skyline');
|
|
23
|
+
|
|
24
|
+
describe('Rectangle Sort Tests', () => {
|
|
25
|
+
test('sort none returns list as is', () => {
|
|
26
|
+
const a = [[3, 3], [3, 1], [3, 2], [1, 2], [2, 1]];
|
|
27
|
+
const ordered = SORT_NONE(a);
|
|
28
|
+
expect(ordered).toEqual(a);
|
|
29
|
+
|
|
30
|
+
// Test empty list
|
|
31
|
+
expect(SORT_NONE([])).toEqual([]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('sort area sorts rectangles by descending area', () => {
|
|
35
|
+
const a = [[5, 5], [7, 7], [3, 4], [100, 1]];
|
|
36
|
+
const ordered = SORT_AREA(a);
|
|
37
|
+
expect(ordered).toEqual([[100, 1], [7, 7], [5, 5], [3, 4]]);
|
|
38
|
+
|
|
39
|
+
// Test empty list
|
|
40
|
+
expect(SORT_AREA([])).toEqual([]);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('sort perimeter sorts rectangles by perimeter', () => {
|
|
44
|
+
const a = [[5, 5], [7, 7], [3, 4], [40, 1]];
|
|
45
|
+
const ordered = SORT_PERI(a);
|
|
46
|
+
expect(ordered).toEqual([[40, 1], [7, 7], [5, 5], [3, 4]]);
|
|
47
|
+
|
|
48
|
+
// Test empty list
|
|
49
|
+
expect(SORT_PERI([])).toEqual([]);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('sort diff sorts rectangles by difference in side lengths', () => {
|
|
53
|
+
const a = [[7, 1], [1, 9], [2, 11], [5, 1]];
|
|
54
|
+
const ordered = SORT_DIFF(a);
|
|
55
|
+
expect(ordered).toEqual([[2, 11], [1, 9], [7, 1], [5, 1]]);
|
|
56
|
+
|
|
57
|
+
// Test empty list
|
|
58
|
+
expect(SORT_DIFF([])).toEqual([]);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('sort short side sorts rectangles by short side descending', () => {
|
|
62
|
+
const a = [[2, 9], [7, 3], [4, 5], [11, 3]];
|
|
63
|
+
const ordered = SORT_SSIDE(a);
|
|
64
|
+
expect(ordered).toEqual([[4, 5], [11, 3], [7, 3], [2, 9]]);
|
|
65
|
+
|
|
66
|
+
// Test empty list
|
|
67
|
+
expect(SORT_SSIDE([])).toEqual([]);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('sort long side sorts rectangles by long side descending', () => {
|
|
71
|
+
const a = [[19, 5], [32, 5], [6, 19], [9, 11]];
|
|
72
|
+
const ordered = SORT_LSIDE(a);
|
|
73
|
+
expect(ordered).toEqual([[32, 5], [6, 19], [19, 5], [9, 11]]);
|
|
74
|
+
|
|
75
|
+
// Test empty list
|
|
76
|
+
expect(SORT_LSIDE([])).toEqual([]);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('sort ratio sorts rectangles by width/height descending', () => {
|
|
80
|
+
const a = [[12, 5], [15, 4], [4, 1], [1, 2]];
|
|
81
|
+
const ordered = SORT_RATIO(a);
|
|
82
|
+
expect(ordered).toEqual([[4, 1], [15, 4], [12, 5], [1, 2]]);
|
|
83
|
+
|
|
84
|
+
// Test empty list
|
|
85
|
+
expect(SORT_RATIO([])).toEqual([]);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('PackerOnline Tests', () => {
|
|
90
|
+
test('bin iteration only loops over closed and open bins', () => {
|
|
91
|
+
const p = new PackerOnlineBNF(GuillotineBssfSas, false);
|
|
92
|
+
p.addBin(50, 55);
|
|
93
|
+
p.addBin(30, 30);
|
|
94
|
+
p.addBin(5, 5);
|
|
95
|
+
p.addBin(40, 40);
|
|
96
|
+
|
|
97
|
+
// No bins to iterate over
|
|
98
|
+
const bins = Array.from(p);
|
|
99
|
+
expect(bins.length).toBe(0);
|
|
100
|
+
|
|
101
|
+
// One open bin to iterate
|
|
102
|
+
p.addRect(50, 50);
|
|
103
|
+
const binsAfterOneRect = Array.from(p);
|
|
104
|
+
expect(binsAfterOneRect.length).toBe(1);
|
|
105
|
+
|
|
106
|
+
// One closed and one open bin
|
|
107
|
+
p.addRect(29, 29);
|
|
108
|
+
const binsAfterTwoRects = Array.from(p);
|
|
109
|
+
expect(binsAfterTwoRects.length).toBe(2);
|
|
110
|
+
expect(binsAfterTwoRects[0].width).toBe(50); // Test closed bins are first
|
|
111
|
+
|
|
112
|
+
// Two closed bins, one skipped bin, and an open bin
|
|
113
|
+
p.addRect(40, 40);
|
|
114
|
+
const binsAfterThreeRects = Array.from(p);
|
|
115
|
+
expect(binsAfterThreeRects.length).toBe(3);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
test('bin order - bins are packed in order they were added', () => {
|
|
119
|
+
const p = new PackerOnlineBNF(GuillotineBssfSas, false);
|
|
120
|
+
p.addBin(45, 45);
|
|
121
|
+
p.addBin(30, 30);
|
|
122
|
+
p.addBin(40, 40);
|
|
123
|
+
|
|
124
|
+
p.addRect(20, 20);
|
|
125
|
+
expect(p.length).toBe(1);
|
|
126
|
+
const firstBin = Array.from(p)[0];
|
|
127
|
+
expect(firstBin.width).toBe(45);
|
|
128
|
+
expect(firstBin.height).toBe(45);
|
|
129
|
+
|
|
130
|
+
p.addRect(29, 29);
|
|
131
|
+
expect(p.length).toBe(2);
|
|
132
|
+
const secondBin = Array.from(p)[1];
|
|
133
|
+
expect(secondBin.width).toBe(30);
|
|
134
|
+
expect(secondBin.height).toBe(30);
|
|
135
|
+
|
|
136
|
+
// Check bins are added at the end of the queue and used last
|
|
137
|
+
p.addBin(39, 39);
|
|
138
|
+
p.addRect(39, 39);
|
|
139
|
+
expect(p.length).toBe(3);
|
|
140
|
+
const thirdBin = Array.from(p)[2];
|
|
141
|
+
expect(thirdBin.width).toBe(40);
|
|
142
|
+
expect(thirdBin.height).toBe(40);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe('PackerOnlineBNF Tests', () => {
|
|
147
|
+
test('bin selection - closes bin after failed packing attempt', () => {
|
|
148
|
+
const p = new PackerOnlineBNF(GuillotineBssfSas, false);
|
|
149
|
+
p.addBin(30, 30);
|
|
150
|
+
p.addBin(10, 10);
|
|
151
|
+
p.addBin(40, 40);
|
|
152
|
+
|
|
153
|
+
// This rectangle fits into first bin
|
|
154
|
+
p.addRect(5, 5);
|
|
155
|
+
expect(p.length).toBe(1);
|
|
156
|
+
expect(Array.from(p)[0].width).toBe(30);
|
|
157
|
+
|
|
158
|
+
// This rectangle doesn't fit into the open bin (first), so the bin
|
|
159
|
+
// is closed and rectangle packed into next one where it fits
|
|
160
|
+
p.addRect(29, 29);
|
|
161
|
+
expect(p.length).toBe(2);
|
|
162
|
+
expect(Array.from(p)[1].width).toBe(40);
|
|
163
|
+
|
|
164
|
+
// Try to add a rectangle that would have fit into the first bin
|
|
165
|
+
// but it is packed since first bin is closed
|
|
166
|
+
p.addRect(10, 10);
|
|
167
|
+
expect(p.length).toBe(2);
|
|
168
|
+
const rectList = p.rectList();
|
|
169
|
+
const lastRect = rectList[rectList.length - 1];
|
|
170
|
+
const [binIndex, x, y, w, h, rid] = lastRect;
|
|
171
|
+
expect(binIndex).toBe(1);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test('infinite bins are only tested once when rectangle doesnt fit', () => {
|
|
175
|
+
const p = new PackerOnlineBNF({packAlgo: GuillotineBssfSas, rotation: true});
|
|
176
|
+
p.addBin(50, 50, 50);
|
|
177
|
+
p.addBin(100, 100, Infinity);
|
|
178
|
+
|
|
179
|
+
p.addRect(90, 90);
|
|
180
|
+
expect(p.length).toBe(1);
|
|
181
|
+
p.addRect(95, 95);
|
|
182
|
+
expect(p.length).toBe(2);
|
|
183
|
+
|
|
184
|
+
// Check other bins
|
|
185
|
+
p.addRect(40, 40);
|
|
186
|
+
expect(p.length).toBe(3);
|
|
187
|
+
expect(Array.from(p)[2].width).toBe(50);
|
|
188
|
+
|
|
189
|
+
p.addRect(45, 45);
|
|
190
|
+
expect(p.length).toBe(4);
|
|
191
|
+
expect(Array.from(p)[3].width).toBe(50);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
test('rotation handling', () => {
|
|
195
|
+
const p = new PackerOnlineBNF({packAlgo: GuillotineBssfSas, rotation: false});
|
|
196
|
+
p.addBin(30, 10);
|
|
197
|
+
p.addBin(50, 10);
|
|
198
|
+
p.addRect(10, 30);
|
|
199
|
+
|
|
200
|
+
// Rectangle didn't fit in any bins when rotation was disabled
|
|
201
|
+
const rectList = p.rectList();
|
|
202
|
+
expect(rectList.length).toBe(0);
|
|
203
|
+
// expect(p.length).toBe(0);
|
|
204
|
+
|
|
205
|
+
// With rotation the rectangle is successfully packed
|
|
206
|
+
const p2 = new PackerOnlineBNF({packAlgo: GuillotineBssfSas, rotation: true});
|
|
207
|
+
p2.addBin(30, 10);
|
|
208
|
+
p2.addBin(50, 10);
|
|
209
|
+
p2.addRect(10, 30);
|
|
210
|
+
const rectList2 = p2.rectList();
|
|
211
|
+
expect(rectList2[0]).toEqual([0, 0, 0, 30, 10, null]);
|
|
212
|
+
expect(p2.length).toBe(1);
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
describe('PackerOnlineBFF Tests', () => {
|
|
217
|
+
test('bin selection - packs into first bin where it fits', () => {
|
|
218
|
+
const p = new PackerOnlineBFF({
|
|
219
|
+
packAlgo: GuillotineBafSas,
|
|
220
|
+
rotation: false
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
p.addBin(20, 20, 2);
|
|
224
|
+
p.addBin(100, 100);
|
|
225
|
+
|
|
226
|
+
// Packed into second bin
|
|
227
|
+
p.addRect(90, 90);
|
|
228
|
+
expect(p.length).toBe(1);
|
|
229
|
+
expect(Array.from(p)[0].width).toBe(100);
|
|
230
|
+
|
|
231
|
+
// Rectangles are packed into open bins whenever possible
|
|
232
|
+
p.addRect(10, 10);
|
|
233
|
+
expect(p.length).toBe(1);
|
|
234
|
+
expect(p.rectList().length).toBe(2);
|
|
235
|
+
|
|
236
|
+
p.addRect(5, 5);
|
|
237
|
+
expect(p.length).toBe(1);
|
|
238
|
+
expect(p.rectList().length).toBe(3);
|
|
239
|
+
|
|
240
|
+
// Rectangle doesn't fit, open new bin
|
|
241
|
+
p.addRect(15, 15);
|
|
242
|
+
expect(p.length).toBe(2);
|
|
243
|
+
expect(p.rectList().length).toBe(4);
|
|
244
|
+
|
|
245
|
+
// If there are more than one possible bin select first one
|
|
246
|
+
p.addRect(5, 5);
|
|
247
|
+
expect(p.length).toBe(2);
|
|
248
|
+
expect(p.rectList().length).toBe(5);
|
|
249
|
+
expect(p.rectList()).toContainEqual([0, 10, 90, 5, 5, null]);
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
describe('PackerOnlineBBF Tests', () => {
|
|
254
|
+
test('bin selection - packs into bin with best fitness', () => {
|
|
255
|
+
const p = new PackerOnlineBBF(
|
|
256
|
+
GuillotineBafSas,
|
|
257
|
+
false
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
p.addBin(10, 10);
|
|
261
|
+
p.addBin(15, 15);
|
|
262
|
+
p.addBin(55, 55);
|
|
263
|
+
p.addBin(50, 50);
|
|
264
|
+
|
|
265
|
+
// First rectangle is packed into first bin where it fits
|
|
266
|
+
p.addRect(50, 30);
|
|
267
|
+
expect(p.length).toBe(1);
|
|
268
|
+
expect(Array.from(p)[0].width).toBe(55);
|
|
269
|
+
|
|
270
|
+
// Another bin is opened when it doesn't fit into first one
|
|
271
|
+
p.addRect(50, 30);
|
|
272
|
+
expect(p.length).toBe(2);
|
|
273
|
+
expect(Array.from(p)[1].width).toBe(50);
|
|
274
|
+
|
|
275
|
+
// Rectangle is placed into bin with best fitness, not first where it fits
|
|
276
|
+
p.addRect(20, 20);
|
|
277
|
+
expect(p.length).toBe(2);
|
|
278
|
+
expect(p.rectList()).toContainEqual([1, 0, 30, 20, 20, null]);
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
describe('Packer Tests', () => {
|
|
283
|
+
test('initialization defaults', () => {
|
|
284
|
+
// Test rotation is enabled by default
|
|
285
|
+
const p = new PackerBNF({packAlgo: GuillotineBafSas, rotation: true});
|
|
286
|
+
p.addBin(100, 10);
|
|
287
|
+
p.addRect(10, 89);
|
|
288
|
+
p.pack();
|
|
289
|
+
const rectList = p.rectList();
|
|
290
|
+
expect(rectList[0]).toEqual([0, 0, 0, 89, 10, null]);
|
|
291
|
+
|
|
292
|
+
// Test default packing algo
|
|
293
|
+
const p2 = new PackerBFF();
|
|
294
|
+
p2.addBin(10, 10);
|
|
295
|
+
p2.addRect(1, 1);
|
|
296
|
+
p2.pack();
|
|
297
|
+
for (const b of p2) {
|
|
298
|
+
expect(b).toBeInstanceOf(GuillotineBssfSas);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
// Test default sorting algo is unsorted
|
|
302
|
+
const p3 = new PackerBFF({
|
|
303
|
+
packAlgo: GuillotineBssfSas,
|
|
304
|
+
rotation: false
|
|
305
|
+
});
|
|
306
|
+
p3.addBin(100, 100, 20);
|
|
307
|
+
|
|
308
|
+
p3.addRect(70, 70);
|
|
309
|
+
p3.addRect(90, 55);
|
|
310
|
+
p3.addRect(90, 90);
|
|
311
|
+
p3.addRect(55, 90);
|
|
312
|
+
p3.addRect(60, 60);
|
|
313
|
+
p3.pack();
|
|
314
|
+
|
|
315
|
+
expect(p3.rectList()).toContainEqual([0, 0, 0, 70, 70, null]);
|
|
316
|
+
expect(p3.rectList()).toContainEqual([1, 0, 0, 90, 55, null]);
|
|
317
|
+
expect(p3.rectList()).toContainEqual([2, 0, 0, 90, 90, null]);
|
|
318
|
+
expect(p3.rectList()).toContainEqual([3, 0, 0, 55, 90, null]);
|
|
319
|
+
expect(p3.rectList()).toContainEqual([4, 0, 0, 60, 60, null]);
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
test('packing functionality', () => {
|
|
323
|
+
// Check packing without bins doesn't raise errors
|
|
324
|
+
const p = new PackerBFF();
|
|
325
|
+
p.addRect(10, 10);
|
|
326
|
+
p.pack();
|
|
327
|
+
|
|
328
|
+
expect(p.rectList()).toEqual([]);
|
|
329
|
+
expect(p.binList()).toEqual([]);
|
|
330
|
+
expect(Array.from(p)).toEqual([]);
|
|
331
|
+
|
|
332
|
+
// No errors when there are no rectangles to pack
|
|
333
|
+
const p2 = new PackerBFF();
|
|
334
|
+
p2.addBin(10, 10);
|
|
335
|
+
p2.pack();
|
|
336
|
+
expect(p2.rectList()).toEqual([]);
|
|
337
|
+
expect(p2.binList()).toEqual([]);
|
|
338
|
+
|
|
339
|
+
// Test rectangles are packed into first available bin
|
|
340
|
+
const p3 = new PackerBFF({
|
|
341
|
+
packAlgo: GuillotineBssfSas,
|
|
342
|
+
rotation: false,
|
|
343
|
+
});
|
|
344
|
+
p3.addBin(20, 10);
|
|
345
|
+
p3.addBin(50, 50);
|
|
346
|
+
p3.addRect(10, 20,1);
|
|
347
|
+
p3.addRect(41, 41,2); // Not enough space for this rectangle
|
|
348
|
+
p3.pack();
|
|
349
|
+
const binList = p3.binList();
|
|
350
|
+
expect(binList.length).toBe(2); // Both bins should be in the list
|
|
351
|
+
|
|
352
|
+
// Verify the bins are correct
|
|
353
|
+
const bins = Array.from(p3);
|
|
354
|
+
expect(bins[0].width).toBe(20); // First bin
|
|
355
|
+
expect(bins[0].height).toBe(10);
|
|
356
|
+
expect(bins[1].width).toBe(50); // Second bin
|
|
357
|
+
expect(bins[1].height).toBe(50);
|
|
358
|
+
|
|
359
|
+
// Verify rectangle placements
|
|
360
|
+
const rectList = p3.rectList();
|
|
361
|
+
expect(rectList.length).toBe(1); // Both rectangles should be packed
|
|
362
|
+
|
|
363
|
+
// First rectangle (10x20) should be in second bin (bin index 1)
|
|
364
|
+
expect(rectList[0]).toEqual([1, 0, 0, 10, 20, 1]);
|
|
365
|
+
|
|
366
|
+
// Second rectangle (41x41) should also be in second bin
|
|
367
|
+
// Note: exact position may vary based on packing algorithm, but must be in bin 1
|
|
368
|
+
expect(rectList[1][0]).toBe(1); // Bin index
|
|
369
|
+
expect(rectList[1][3]).toBe(41); // Width
|
|
370
|
+
expect(rectList[1][4]).toBe(41); // Height
|
|
371
|
+
expect(rectList[1][5]).toBe(2); // Rectangle ID
|
|
372
|
+
});
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
describe('newPacker Tests', () => {
|
|
376
|
+
|
|
377
|
+
test('default_options', () => {
|
|
378
|
+
// Test default options
|
|
379
|
+
const p = newPacker();
|
|
380
|
+
|
|
381
|
+
// Default mode Online BBF
|
|
382
|
+
expect(p).toBeInstanceOf(PackerBBF);
|
|
383
|
+
|
|
384
|
+
// Default rotations true
|
|
385
|
+
expect(p._rotation).toBe(true);
|
|
386
|
+
|
|
387
|
+
// Default packing algo
|
|
388
|
+
p.addRect(100, 10);
|
|
389
|
+
p.addBin(10, 100);
|
|
390
|
+
p.pack();
|
|
391
|
+
for (const b of p) {
|
|
392
|
+
expect(b).toBeInstanceOf(GuillotineBssfSas);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
// Test default sorting algo is SORT_AREA
|
|
396
|
+
const p2 = newPacker({
|
|
397
|
+
rotation: false
|
|
398
|
+
});
|
|
399
|
+
expect(p2._rotation).toBe(false);
|
|
400
|
+
p2.addBin(100, 100, 10);
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
p2.addRect(70, 70);
|
|
406
|
+
p2.addRect(90, 55);
|
|
407
|
+
p2.addRect(90, 90);
|
|
408
|
+
p2.addRect(55, 90);
|
|
409
|
+
p2.addRect(60, 60);
|
|
410
|
+
p2.pack();
|
|
411
|
+
|
|
412
|
+
const rectList = p2.rectList();
|
|
413
|
+
expect(rectList.length).toBe(5);
|
|
414
|
+
expect(rectList).toContainEqual([0, 0, 0, 90, 90, null]); // Largest area rectangle
|
|
415
|
+
expect(rectList).toContainEqual([1, 0, 0, 90, 55, null]); // Second largest
|
|
416
|
+
expect(rectList).toContainEqual([2, 0, 0, 55, 90, null]); // Third largest
|
|
417
|
+
expect(rectList).toContainEqual([3, 0, 0, 70, 70, null]); // Fourth largest
|
|
418
|
+
expect(rectList).toContainEqual([4, 0, 0, 60, 60, null]); // Fifth largest
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
test('packing modes', () => {
|
|
422
|
+
// Test online mode
|
|
423
|
+
const p1 = newPacker({
|
|
424
|
+
mode: PackingMode.Online,
|
|
425
|
+
binAlgo: PackingBin.BNF
|
|
426
|
+
});
|
|
427
|
+
expect(p1).toBeInstanceOf(PackerOnlineBNF);
|
|
428
|
+
|
|
429
|
+
const p2 = newPacker({
|
|
430
|
+
mode: PackingMode.Online,
|
|
431
|
+
binAlgo: PackingBin.BFF
|
|
432
|
+
});
|
|
433
|
+
expect(p2).toBeInstanceOf(PackerOnlineBFF);
|
|
434
|
+
|
|
435
|
+
const p3 = newPacker({
|
|
436
|
+
mode: PackingMode.Online,
|
|
437
|
+
binAlgo: PackingBin.BBF
|
|
438
|
+
});
|
|
439
|
+
expect(p3).toBeInstanceOf(PackerOnlineBBF);
|
|
440
|
+
|
|
441
|
+
// Test offline mode
|
|
442
|
+
const p4 = newPacker({
|
|
443
|
+
mode: PackingMode.Offline,
|
|
444
|
+
binAlgo: PackingBin.BNF
|
|
445
|
+
});
|
|
446
|
+
expect(p4).toBeInstanceOf(PackerBNF);
|
|
447
|
+
|
|
448
|
+
const p5 = newPacker({
|
|
449
|
+
mode: PackingMode.Offline,
|
|
450
|
+
binAlgo: PackingBin.BFF
|
|
451
|
+
});
|
|
452
|
+
expect(p5).toBeInstanceOf(PackerBFF);
|
|
453
|
+
|
|
454
|
+
const p6 = newPacker({
|
|
455
|
+
mode: PackingMode.Offline,
|
|
456
|
+
binAlgo: PackingBin.BBF
|
|
457
|
+
});
|
|
458
|
+
expect(p6).toBeInstanceOf(PackerBBF);
|
|
459
|
+
|
|
460
|
+
const p7 = newPacker({
|
|
461
|
+
mode: PackingMode.Offline,
|
|
462
|
+
binAlgo: PackingBin.Global
|
|
463
|
+
});
|
|
464
|
+
expect(p7).toBeInstanceOf(PackerGlobal);
|
|
465
|
+
});
|
|
466
|
+
});
|