rectpackbin 0.0.2 → 0.0.3

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.
@@ -0,0 +1,405 @@
1
+ const { Rectangle } = require('./geometry');
2
+ const PackingAlgorithm = require('./packAlgo');
3
+
4
+ class Guillotine extends PackingAlgorithm {
5
+ /**
6
+ * Implementation of several variants of Guillotine packing algorithm
7
+ * For a more detailed explanation of the algorithm used, see:
8
+ * Jukka Jylanki - A Thousand Ways to Pack the Bin (February 27, 2010)
9
+ * @param {number} width - Surface width
10
+ * @param {number} height - Surface height
11
+ * @param {boolean} rot - Enable rotation
12
+ * @param {boolean} merge - Enable section merging
13
+ */
14
+ constructor(width, height, rot = true, merge = true, ...args) {
15
+ super(width, height, rot, ...args);
16
+ this._merge = merge;
17
+ this._sections = [];
18
+ this._add_section(new Rectangle(0, 0, width, height,null));
19
+ }
20
+
21
+ /**
22
+ * Adds a new section to the free section list
23
+ * If section merge is enabled, tries to join the rectangle with existing sections
24
+ * @param {Rectangle} section - New free section
25
+ */
26
+ _add_section(section) {
27
+ section.rid = 0;
28
+ let plen = 0;
29
+
30
+ while (this._merge && this._sections && plen !== this._sections.length) {
31
+ plen = this._sections.length;
32
+ this._sections = this._sections.filter(s => !section.join(s));
33
+ }
34
+ this._sections.push(section);
35
+ }
36
+
37
+ /**
38
+ * Performs horizontal split of a section
39
+ * @param {Rectangle} section - Section to split
40
+ * @param {number} width - Rectangle width
41
+ * @param {number} height - Rectangle height
42
+ */
43
+ _split_horizontal(section, width, height) {
44
+ if (height < section.height) {
45
+ this._add_section(new Rectangle(
46
+ section.x,
47
+ section.y + height,
48
+ section.width,
49
+ section.height - height
50
+ ));
51
+ }
52
+
53
+ if (width < section.width) {
54
+ this._add_section(new Rectangle(
55
+ section.x + width,
56
+ section.y,
57
+ section.width - width,
58
+ height
59
+ ));
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Performs vertical split of a section
65
+ * @param {Rectangle} section - Section to split
66
+ * @param {number} width - Rectangle width
67
+ * @param {number} height - Rectangle height
68
+ */
69
+ _split_vertical(section, width, height) {
70
+ if (height < section.height) {
71
+ this._add_section(new Rectangle(
72
+ section.x,
73
+ section.y + height,
74
+ width,
75
+ section.height - height
76
+ ));
77
+ }
78
+
79
+ if (width < section.width) {
80
+ this._add_section(new Rectangle(
81
+ section.x + width,
82
+ section.y,
83
+ section.width - width,
84
+ section.height
85
+ ));
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Abstract method to select best split for a section
91
+ * @param {Rectangle} section - Section to split
92
+ * @param {number} width - Rectangle width
93
+ * @param {number} height - Rectangle height
94
+ */
95
+ _split(section, width, height) {
96
+ throw new Error("Not implemented");
97
+ }
98
+
99
+ /**
100
+ * Abstract method for section fitness calculation
101
+ * @param {Rectangle} section - Section to evaluate
102
+ * @param {number} width - Rectangle width
103
+ * @param {number} height - Rectangle height
104
+ */
105
+ _section_fitness(section, width, height) {
106
+ throw new Error("Not implemented");
107
+ }
108
+
109
+ /**
110
+ * Selects the fittest section for placing a rectangle
111
+ * @param {number} w - Rectangle width
112
+ * @param {number} h - Rectangle height
113
+ * @returns {[Rectangle, boolean]} Section and rotation flag
114
+ */
115
+ _select_fittest_section(w, h) {
116
+ const fitn = this._sections
117
+ .map(s => {
118
+ const fitness = this._section_fitness(s, w, h);
119
+ return fitness !== null ? [fitness, s, false] : null;
120
+ })
121
+ .filter(x => x !== null);
122
+
123
+ const fitr = this.rot ? this._sections
124
+ .map(s => {
125
+ const fitness = this._section_fitness(s, h, w);
126
+ return fitness !== null ? [fitness, s, true] : null;
127
+ })
128
+ .filter(x => x !== null) : [];
129
+
130
+ const fit = [...fitn, ...fitr];
131
+
132
+ if (fit.length === 0) {
133
+ return [null, null];
134
+ }
135
+
136
+ const [_, sec, rot] = fit.reduce((min, curr) =>
137
+ curr[0] < min[0] ? curr : min
138
+ );
139
+
140
+ return [sec, rot];
141
+ }
142
+
143
+ /**
144
+ * Add rectangle of width x height dimensions
145
+ * @param {number} width - Rectangle width
146
+ * @param {number} height - Rectangle height
147
+ * @param {string|number} rid - Optional rectangle ID
148
+ * @returns {Rectangle|null} Placed rectangle or null if placement failed
149
+ */
150
+ addRect(width, height, rid = null) {
151
+ if (!(width > 0 && height > 0)) {
152
+ throw new Error("Width and height must be positive");
153
+ }
154
+
155
+ const [section, rotated] = this._select_fittest_section(width, height);
156
+ if (!section) {
157
+ return null;
158
+ }
159
+
160
+ if (rotated) {
161
+ [width, height] = [height, width];
162
+ }
163
+
164
+ this._sections = this._sections.filter(s => s !== section);
165
+ this._split(section, width, height);
166
+
167
+ const rect = new Rectangle(section.x, section.y, width, height, rid);
168
+ this.rectangles.push(rect);
169
+ return rect;
170
+ }
171
+
172
+ /**
173
+ * Calculate fitness for given dimensions
174
+ * @param {number} width - Rectangle width
175
+ * @param {number} height - Rectangle height
176
+ * @returns {number|null} Fitness value or null if placement impossible
177
+ */
178
+ fitness(width, height) {
179
+ if (!(width > 0 && height > 0)) {
180
+ throw new Error("Width and height must be positive");
181
+ }
182
+
183
+ const [section, rotated] = this._select_fittest_section(width, height);
184
+ if (!section) {
185
+ return null;
186
+ }
187
+
188
+ return rotated ?
189
+ this._section_fitness(section, height, width) :
190
+ this._section_fitness(section, width, height);
191
+ }
192
+
193
+ /**
194
+ * Reset the algorithm state
195
+ */
196
+ reset() {
197
+ super.reset();
198
+ this._sections = [];
199
+ this._add_section(new Rectangle(0, 0, this.width, this.height));
200
+ }
201
+ }
202
+
203
+ // Implementing the various Guillotine algorithms
204
+ class GuillotineBaf extends Guillotine {
205
+ _section_fitness(section, width, height) {
206
+ if (width > section.width || height > section.height) {
207
+ return null;
208
+ }
209
+ return section.area() - width * height;
210
+ }
211
+ }
212
+
213
+ class GuillotineBlsf extends Guillotine {
214
+ _section_fitness(section, width, height) {
215
+ if (width > section.width || height > section.height) {
216
+ return null;
217
+ }
218
+ return Math.max(section.width - width, section.height - height);
219
+ }
220
+ }
221
+
222
+ class GuillotineBssf extends Guillotine {
223
+ _section_fitness(section, width, height) {
224
+ if (width > section.width || height > section.height) {
225
+ return null;
226
+ }
227
+ return Math.min(section.width - width, section.height - height);
228
+ }
229
+ }
230
+
231
+ /**
232
+ * Split the section into two parts, the width is less than the height
233
+ */
234
+ class GuillotineSas extends Guillotine {
235
+ _split(section, width, height) {
236
+ if (section.width < section.height) {
237
+ return this._split_horizontal(section, width, height);
238
+ } else {
239
+ return this._split_vertical(section, width, height);
240
+ }
241
+ }
242
+ }
243
+
244
+ /**
245
+ * Split the section into two parts, the width is greater than the height
246
+ */
247
+ class GuillotineLas extends Guillotine {
248
+ _split(section, width, height) {
249
+ if (section.width >= section.height) {
250
+ return this._split_horizontal(section, width, height);
251
+ } else {
252
+ return this._split_vertical(section, width, height);
253
+ }
254
+ }
255
+ }
256
+
257
+ /**
258
+ * Split the section into two parts, the width is less than the height
259
+ */
260
+ class GuillotineSlas extends Guillotine {
261
+ _split(section, width, height) {
262
+ if (section.width - width < section.height - height) {
263
+ return this._split_horizontal(section, width, height);
264
+ } else {
265
+ return this._split_vertical(section, width, height);
266
+ }
267
+ }
268
+ }
269
+
270
+ /**
271
+ * Split the section into two parts, the width is greater than the height
272
+ */
273
+ class GuillotineLlas extends Guillotine {
274
+ _split(section, width, height) {
275
+ if (section.width - width >= section.height - height) {
276
+ return this._split_horizontal(section, width, height);
277
+ } else {
278
+ return this._split_vertical(section, width, height);
279
+ }
280
+ }
281
+ }
282
+
283
+ /**
284
+ * Split the section into two parts, the width is greater than the height
285
+ */
286
+ class GuillotineMaxas extends Guillotine {
287
+ _split(section, width, height) {
288
+ if (width * (section.height - height) <= height * (section.width - width)) {
289
+ return this._split_horizontal(section, width, height);
290
+ } else {
291
+ return this._split_vertical(section, width, height);
292
+ }
293
+ }
294
+ }
295
+
296
+ /**
297
+ * Split the section into two parts, the width is greater than the height
298
+ */
299
+ class GuillotineMinas extends Guillotine {
300
+ _split(section, width, height) {
301
+ if (width * (section.height - height) >= height * (section.width - width)) {
302
+ return this._split_horizontal(section, width, height);
303
+ } else {
304
+ return this._split_vertical(section, width, height);
305
+ }
306
+ }
307
+ }
308
+
309
+ // Combined Guillotine algorithms
310
+ class GuillotineBssfSas extends GuillotineBssf {
311
+ _split(section, width, height) {
312
+ if (section.width - width < section.height - height) {
313
+ return this._split_horizontal(section, width, height);
314
+ } else {
315
+ return this._split_vertical(section, width, height);
316
+ }
317
+ }
318
+ }
319
+ Object.assign(GuillotineBssfSas.prototype, GuillotineSas.prototype);
320
+
321
+
322
+
323
+ class GuillotineBssfLas extends GuillotineBssf {}
324
+ Object.assign(GuillotineBssfLas.prototype, GuillotineLas.prototype);
325
+
326
+ class GuillotineBssfSlas extends GuillotineBssf {}
327
+ Object.assign(GuillotineBssfSlas.prototype, GuillotineSlas.prototype);
328
+
329
+ class GuillotineBssfLlas extends GuillotineBssf {}
330
+ Object.assign(GuillotineBssfLlas.prototype, GuillotineLlas.prototype);
331
+
332
+ class GuillotineBssfMaxas extends GuillotineBssf {}
333
+ Object.assign(GuillotineBssfMaxas.prototype, GuillotineMaxas.prototype);
334
+
335
+ class GuillotineBssfMinas extends GuillotineBssf {}
336
+ Object.assign(GuillotineBssfMinas.prototype, GuillotineMinas.prototype);
337
+
338
+ class GuillotineBlsfSas extends GuillotineBlsf {}
339
+ Object.assign(GuillotineBlsfSas.prototype, GuillotineSas.prototype);
340
+
341
+ class GuillotineBlsfLas extends GuillotineBlsf {}
342
+ Object.assign(GuillotineBlsfLas.prototype, GuillotineLas.prototype);
343
+
344
+ class GuillotineBlsfSlas extends GuillotineBlsf {}
345
+ Object.assign(GuillotineBlsfSlas.prototype, GuillotineSlas.prototype);
346
+
347
+ class GuillotineBlsfLlas extends GuillotineBlsf {}
348
+ Object.assign(GuillotineBlsfLlas.prototype, GuillotineLlas.prototype);
349
+
350
+ class GuillotineBlsfMaxas extends GuillotineBlsf {}
351
+ Object.assign(GuillotineBlsfMaxas.prototype, GuillotineMaxas.prototype);
352
+
353
+ class GuillotineBlsfMinas extends GuillotineBlsf {}
354
+ Object.assign(GuillotineBlsfMinas.prototype, GuillotineMinas.prototype);
355
+
356
+ class GuillotineBafSas extends GuillotineBaf {}
357
+ Object.assign(GuillotineBafSas.prototype, GuillotineSas.prototype);
358
+
359
+ class GuillotineBafLas extends GuillotineBaf {}
360
+ Object.assign(GuillotineBafLas.prototype, GuillotineLas.prototype);
361
+
362
+ class GuillotineBafSlas extends GuillotineBaf {}
363
+ Object.assign(GuillotineBafSlas.prototype, GuillotineSlas.prototype);
364
+
365
+ class GuillotineBafLlas extends GuillotineBaf {}
366
+ Object.assign(GuillotineBafLlas.prototype, GuillotineLlas.prototype);
367
+
368
+ class GuillotineBafMaxas extends GuillotineBaf {}
369
+ Object.assign(GuillotineBafMaxas.prototype, GuillotineMaxas.prototype);
370
+
371
+ class GuillotineBafMinas extends GuillotineBaf {}
372
+ Object.assign(GuillotineBafMinas.prototype, GuillotineMinas.prototype);
373
+
374
+ module.exports = {
375
+ Guillotine,
376
+ GuillotineBaf,
377
+ GuillotineBlsf,
378
+ GuillotineBssf,
379
+ GuillotineSas,
380
+ GuillotineLas,
381
+ GuillotineSlas,
382
+ GuillotineLlas,
383
+ GuillotineMaxas,
384
+ GuillotineMinas,
385
+ GuillotineBssfSas,
386
+ GuillotineBssfLas,
387
+ GuillotineBssfSlas,
388
+ GuillotineBssfLlas,
389
+ GuillotineBssfMaxas,
390
+ GuillotineBssfMinas,
391
+ GuillotineBlsfSas,
392
+ GuillotineBlsfLas,
393
+ GuillotineBlsfSlas,
394
+ GuillotineBlsfLlas,
395
+ GuillotineBlsfMaxas,
396
+ GuillotineBlsfMinas,
397
+ GuillotineBafSas,
398
+ GuillotineBafLas,
399
+ GuillotineBafSlas,
400
+ GuillotineBafLlas,
401
+ GuillotineBafMaxas,
402
+ GuillotineBafMinas
403
+ };
404
+
405
+
@@ -0,0 +1,153 @@
1
+ const { Rectangle } = require('./geometry');
2
+
3
+ class PackingAlgorithm {
4
+ /**
5
+ * Initialize packing algorithm
6
+ * @param {number} width - Packing surface width
7
+ * @param {number} height - Packing surface height
8
+ * @param {boolean} rot - Rectangle rotation enabled or disabled
9
+ * @param {string|number} bid - Packing surface identification
10
+ */
11
+ constructor(width, height, rot = true, bid = null) {
12
+ this.width = width;
13
+ this.height = height;
14
+ this.rot = rot;
15
+ this.rectangles = [];
16
+ this.bid = bid;
17
+ this._surface = new Rectangle(0, 0, width, height);
18
+ this.reset();
19
+ }
20
+
21
+ /**
22
+ * Get the number of rectangles
23
+ * @returns {number}
24
+ */
25
+ get length() {
26
+ return this.rectangles.length;
27
+ }
28
+
29
+ /**
30
+ * Make the class iterable
31
+ */
32
+ [Symbol.iterator]() {
33
+ return this.rectangles[Symbol.iterator]();
34
+ }
35
+
36
+ /**
37
+ * Test if surface is big enough to place a rectangle
38
+ * @param {number} width - Rectangle width
39
+ * @param {number} height - Rectangle height
40
+ * @returns {boolean} - True if it could be placed, False otherwise
41
+ */
42
+ _fitsSurface(width, height) {
43
+ if (!(width > 0 && height > 0)) {
44
+ throw new Error("Width and height must be positive");
45
+ }
46
+
47
+ if (this.rot && (width > this.width || height > this.height)) {
48
+ width = height
49
+ height = width
50
+ }
51
+
52
+ if (width > this.width || height > this.height) {
53
+ return false;
54
+ }
55
+
56
+ return true;
57
+ }
58
+
59
+ /**
60
+ * Return rectangle in selected position
61
+ * @param {number} key - Index of the rectangle
62
+ * @returns {Rectangle}
63
+ */
64
+ getItem(key) {
65
+ return this.rectangles[key];
66
+ }
67
+
68
+ /**
69
+ * Total area of rectangles placed
70
+ * @returns {number} - Area
71
+ */
72
+ usedArea() {
73
+ return this.rectangles.reduce((sum, rect) => sum + rect.area(), 0);
74
+ }
75
+
76
+ /**
77
+ * Metric used to rate how much space is wasted if a rectangle is placed
78
+ * @param {number} width - Rectangle width
79
+ * @param {number} height - Rectangle height
80
+ * @param {boolean} rot - Enable rectangle rotation
81
+ * @returns {number|null} - Rectangle fitness or null if can't be placed
82
+ */
83
+ fitness(width, height, rot = false) {
84
+ throw new Error("Not implemented");
85
+ }
86
+
87
+ /**
88
+ * Add rectangle of width x height dimensions
89
+ * @param {number} width - Rectangle width
90
+ * @param {number} height - Rectangle height
91
+ * @param {string|number} rid - Optional rectangle user id
92
+ * @returns {Rectangle|null} - Rectangle with placement coordinates or null if couldn't be placed
93
+ */
94
+ addRect(width, height, rid = null) {
95
+ throw new Error("Not implemented");
96
+ }
97
+
98
+ /**
99
+ * Returns a list with all rectangles placed into the surface
100
+ * @returns {Array} - Format [[x, y, width, height, rid], ...]
101
+ */
102
+ rectList() {
103
+ return this.rectangles.map(r => [r.x, r.y, r.width, r.height, r.rid]);
104
+ }
105
+
106
+ /**
107
+ * Check for collisions between rectangles and validate placement inside surface
108
+ * @throws {Error} If validation fails
109
+ */
110
+ validatePacking() {
111
+ const surface = new Rectangle(0, 0, this.width, this.height);
112
+
113
+ // Check if all rectangles are within surface
114
+ for (const rect of this.rectangles) {
115
+ if (!surface.contains(rect)) {
116
+ throw new Error("Rectangle placed outside surface");
117
+ }
118
+ }
119
+
120
+ // Check for collisions between rectangles
121
+ if (this.rectangles.length <= 1) {
122
+ return;
123
+ }
124
+
125
+ for (let r1 = 0; r1 <= this.rectangles.length - 2; r1++) {
126
+
127
+ for (let r2 = r1 + 1; r2 <= this.rectangles.length - 1; r2++) {
128
+ if (this.rectangles[r1].intersects(this.rectangles[r2])) {
129
+ throw new Error("Rectangle collision detected");
130
+ }
131
+ }
132
+ }
133
+ }
134
+
135
+ /**
136
+ * Returns true if there are no rectangles placed
137
+ * @returns {boolean}
138
+ */
139
+ isEmpty() {
140
+ return this.rectangles.length === 0;
141
+ }
142
+
143
+ /**
144
+ * Reset the packing algorithm
145
+ */
146
+ reset() {
147
+ this.rectangles = [];
148
+ }
149
+ }
150
+
151
+ module.exports = PackingAlgorithm;
152
+
153
+