rectpackbin 0.0.2 → 0.0.4

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,621 @@
1
+ const { Rectangle } = require('./geometry');
2
+ const PackingAlgorithm = require('./packAlgo');
3
+
4
+
5
+ const GuillotineMixin = Base => class extends Base {
6
+ /**
7
+ * Implementation of several variants of Guillotine packing algorithm
8
+ * For a more detailed explanation of the algorithm used, see:
9
+ * Jukka Jylanki - A Thousand Ways to Pack the Bin (February 27, 2010)
10
+ * @param {number} width - Surface width
11
+ * @param {number} height - Surface height
12
+ * @param {boolean} rot - Enable rotation
13
+ * @param {boolean} merge - Enable section merging
14
+ */
15
+ constructor(width, height, rot = true, merge = true, ...args) {
16
+ super(width, height, rot, ...args);
17
+ this._merge = merge;
18
+ this._sections = [];
19
+ this._add_section(new Rectangle(0, 0, width, height,null));
20
+ }
21
+
22
+ /**
23
+ * Adds a new section to the free section list
24
+ * If section merge is enabled, tries to join the rectangle with existing sections
25
+ * @param {Rectangle} section - New free section
26
+ */
27
+ _add_section(section) {
28
+ section.rid = 0;
29
+ let plen = 0;
30
+
31
+ while (this._merge && this._sections && plen !== this._sections.length) {
32
+ plen = this._sections.length;
33
+ this._sections = this._sections.filter(s => !section.join(s));
34
+ }
35
+ this._sections.push(section);
36
+ }
37
+
38
+ /**
39
+ * Performs horizontal split of a section
40
+ * @param {Rectangle} section - Section to split
41
+ * @param {number} width - Rectangle width
42
+ * @param {number} height - Rectangle height
43
+ */
44
+ _split_horizontal(section, width, height) {
45
+ if (height < section.height) {
46
+ this._add_section(new Rectangle(
47
+ section.x,
48
+ section.y + height,
49
+ section.width,
50
+ section.height - height
51
+ ));
52
+ }
53
+
54
+ if (width < section.width) {
55
+ this._add_section(new Rectangle(
56
+ section.x + width,
57
+ section.y,
58
+ section.width - width,
59
+ height
60
+ ));
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Performs vertical split of a section
66
+ * @param {Rectangle} section - Section to split
67
+ * @param {number} width - Rectangle width
68
+ * @param {number} height - Rectangle height
69
+ */
70
+ _split_vertical(section, width, height) {
71
+ if (height < section.height) {
72
+ this._add_section(new Rectangle(
73
+ section.x,
74
+ section.y + height,
75
+ width,
76
+ section.height - height
77
+ ));
78
+ }
79
+
80
+ if (width < section.width) {
81
+ this._add_section(new Rectangle(
82
+ section.x + width,
83
+ section.y,
84
+ section.width - width,
85
+ section.height
86
+ ));
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Abstract method to select best split for a section
92
+ * @param {Rectangle} section - Section to split
93
+ * @param {number} width - Rectangle width
94
+ * @param {number} height - Rectangle height
95
+ */
96
+ _split(section, width, height) {
97
+ throw new Error("Not implemented");
98
+ }
99
+
100
+ /**
101
+ * Abstract method for section fitness calculation
102
+ * @param {Rectangle} section - Section to evaluate
103
+ * @param {number} width - Rectangle width
104
+ * @param {number} height - Rectangle height
105
+ */
106
+ _section_fitness(section, width, height) {
107
+ throw new Error("Not implemented");
108
+ }
109
+
110
+ /**
111
+ * Selects the fittest section for placing a rectangle
112
+ * @param {number} w - Rectangle width
113
+ * @param {number} h - Rectangle height
114
+ * @returns {[Rectangle, boolean]} Section and rotation flag
115
+ */
116
+ _select_fittest_section(w, h) {
117
+ const fitn = this._sections
118
+ .map(s => {
119
+ const fitness = this._section_fitness(s, w, h);
120
+ return fitness !== null ? [fitness, s, false] : null;
121
+ })
122
+ .filter(x => x !== null);
123
+
124
+ let fitr = []
125
+
126
+ if (this.rot.rot === true) {
127
+ fitr = this._sections
128
+ .map(s => {
129
+ const fitness = this._section_fitness(s, h, w);
130
+ return fitness !== null ? [fitness, s, true] : null;
131
+ })
132
+ .filter(x => x !== null) ;
133
+ }
134
+
135
+ const fit = [...fitn, ...fitr];
136
+
137
+ if (fit.length === 0) {
138
+ return [];
139
+ }
140
+
141
+ const [_, sec, rot] = fit.reduce((min, curr) =>
142
+ curr[0] < min[0] ? curr : min
143
+ );
144
+
145
+ return [sec, rot];
146
+ }
147
+
148
+ /**
149
+ * Add rectangle of width x height dimensions
150
+ * @param {number} width - Rectangle width
151
+ * @param {number} height - Rectangle height
152
+ * @param {string|number} rid - Optional rectangle ID
153
+ * @returns {Rectangle|null} Placed rectangle or null if placement failed
154
+ */
155
+ addRect(width, height, rid = null) {
156
+ if (!(width > 0 && height > 0)) {
157
+ throw new Error("Width and height must be positive");
158
+ }
159
+
160
+ const [section, rotated] = this._select_fittest_section(width, height);
161
+ if (!section) {
162
+ return null;
163
+ }
164
+
165
+ if (rotated) {
166
+ let temp = width;
167
+ width = height;
168
+ height = temp;
169
+ }
170
+
171
+ this._sections = this._sections.filter(s => s !== section);
172
+ this._split(section, width, height);
173
+
174
+ const rect = new Rectangle(section.x, section.y, width, height, rid);
175
+ this.rectangles.push(rect);
176
+ return rect;
177
+ }
178
+
179
+ /**
180
+ * Calculate fitness for given dimensions
181
+ * @param {number} width - Rectangle width
182
+ * @param {number} height - Rectangle height
183
+ * @returns {number|null} Fitness value or null if placement impossible
184
+ */
185
+ fitness(width, height) {
186
+ if (!(width > 0 && height > 0)) {
187
+ throw new Error("Width and height must be positive");
188
+ }
189
+
190
+ const [section, rotated] = this._select_fittest_section(width, height);
191
+ if (!section) {
192
+ return null;
193
+ }
194
+
195
+ return rotated ?
196
+ this._section_fitness(section, height, width) :
197
+ this._section_fitness(section, width, height);
198
+ }
199
+
200
+ /**
201
+ * Reset the algorithm state
202
+ */
203
+ reset() {
204
+ super.reset();
205
+ this._sections = [];
206
+ this._add_section(new Rectangle(0, 0, this.width, this.height));
207
+ }
208
+ }
209
+
210
+ const GuillotineBase = GuillotineMixin(PackingAlgorithm);
211
+ class Guillotine extends GuillotineBase {
212
+ constructor(...args) {
213
+ super(...args);
214
+ }
215
+ }
216
+
217
+ // Implementing the various Guillotine algorithms
218
+ class GuillotineBaf extends Guillotine {
219
+ _section_fitness(section, width, height) {
220
+ if (width > section.width || height > section.height) {
221
+ return null;
222
+ }
223
+ return section.area() - width * height;
224
+ }
225
+ }
226
+ const BafMixin = Base => class extends Base {
227
+ constructor(...args) {
228
+ super(...args);
229
+ }
230
+
231
+ _section_fitness(section, width, height) {
232
+ if (width > section.width || height > section.height) {
233
+ return null;
234
+ }
235
+ return section.area() - width * height;
236
+ }
237
+ }
238
+
239
+ class GuillotineBlsf extends Guillotine {
240
+ _section_fitness(section, width, height) {
241
+ if (width > section.width || height > section.height) {
242
+ return null;
243
+ }
244
+ return Math.max(section.width - width, section.height - height);
245
+ }
246
+ }
247
+
248
+ const BlsfMixin = Base => class extends Base {
249
+ constructor(...args) {
250
+ super(...args);
251
+ }
252
+
253
+ _section_fitness(section, width, height) {
254
+ if (width > section.width || height > section.height) {
255
+ return null;
256
+ }
257
+ return Math.max(section.width - width, section.height - height);
258
+ }
259
+ }
260
+
261
+
262
+ class GuillotineBssf extends Guillotine {
263
+ _section_fitness(section, width, height) {
264
+ if (width > section.width || height > section.height) {
265
+ return null;
266
+ }
267
+ return Math.min(section.width - width, section.height - height);
268
+ }
269
+ }
270
+
271
+ const BssfMixin = Base => class extends Base {
272
+ constructor(...args) {
273
+ super(...args);
274
+ }
275
+ _section_fitness(section, width, height) {
276
+ if (width > section.width || height > section.height) {
277
+ return null;
278
+ }
279
+ return Math.min(section.width - width, section.height - height);
280
+ }
281
+ }
282
+
283
+ /**
284
+ * Split the section into two parts, the width is less than the height
285
+ */
286
+ const SasMixin = Base => class extends Base {
287
+ constructor(...args) {
288
+ super(...args);
289
+ }
290
+ _split(section, width, height) {
291
+ if (section.width < section.height) {
292
+ return this._split_horizontal(section, width, height);
293
+ } else {
294
+ return this._split_vertical(section, width, height);
295
+ }
296
+ }
297
+ };
298
+
299
+ const GuillotineSasBase = SasMixin(Guillotine);
300
+ class GuillotineSas extends GuillotineSasBase {
301
+ constructor(...args) {
302
+ super(...args);
303
+ }
304
+ }
305
+
306
+ /**
307
+ * Split the section into two parts, the width is greater than the height
308
+ */
309
+ const LasMixin = Base => class extends Base {
310
+ constructor(...args) {
311
+ super(...args);
312
+ }
313
+ _split(section, width, height) {
314
+ if (section.width >= section.height) {
315
+ return this._split_horizontal(section, width, height);
316
+ } else {
317
+ return this._split_vertical(section, width, height);
318
+ }
319
+ }
320
+ };
321
+
322
+ const GuillotineLasBase = LasMixin(Guillotine);
323
+ class GuillotineLas extends GuillotineLasBase {
324
+ constructor(...args) {
325
+ super(...args);
326
+ }
327
+ }
328
+
329
+ /**
330
+ * Split the section into two parts, the width is less than the height
331
+ */
332
+ const SlasMixin = Base => class extends Base {
333
+ constructor(...args) {
334
+ super(...args);
335
+ }
336
+ _split(section, width, height) {
337
+ if (section.width - width < section.height - height) {
338
+ return this._split_horizontal(section, width, height);
339
+ } else {
340
+ return this._split_vertical(section, width, height);
341
+ }
342
+ }
343
+ };
344
+
345
+ const GuillotineSlasBase = SlasMixin(Guillotine);
346
+ class GuillotineSlas extends GuillotineSlasBase {
347
+ constructor(...args) {
348
+ super(...args);
349
+ }
350
+ }
351
+ /**
352
+ * Split the section into two parts, the width is greater than the height
353
+ */
354
+ const LlasMixin = Base => class extends Base {
355
+ constructor(...args) {
356
+ super(...args);
357
+ }
358
+ _split(section, width, height) {
359
+ if (section.width - width < section.height - height) {
360
+ return this._split_horizontal(section, width, height);
361
+ } else {
362
+ return this._split_vertical(section, width, height);
363
+ }
364
+ }
365
+ };
366
+
367
+ const GuillotineLlasBase = LlasMixin(Guillotine);
368
+ class GuillotineLlas extends GuillotineLlasBase {
369
+ constructor(...args) {
370
+ super(...args);
371
+ }
372
+ }
373
+
374
+ /**
375
+ * Split the section into two parts, the width is greater than the height
376
+ */
377
+ const MaxasMixin = Base => class extends Base {
378
+ constructor(...args) {
379
+ super(...args);
380
+ }
381
+ _split(section, width, height) {
382
+ if (section.width - width < section.height - height) {
383
+ return this._split_horizontal(section, width, height);
384
+ } else {
385
+ return this._split_vertical(section, width, height);
386
+ }
387
+ }
388
+ };
389
+
390
+ const GuillotineMaxasBase = MaxasMixin(Guillotine);
391
+ class GuillotineMaxas extends GuillotineMaxasBase {
392
+ constructor(...args) {
393
+ super(...args);
394
+ }
395
+ }
396
+ /**
397
+ * Split the section into two parts, the width is greater than the height
398
+ */
399
+
400
+ const MinasMixin = Base => class extends Base {
401
+ constructor(...args) {
402
+ super(...args);
403
+ }
404
+ _split(section, width, height) {
405
+ if (width * (section.height - height) >= height * (section.width - width)) {
406
+ return this._split_horizontal(section, width, height);
407
+ } else {
408
+ return this._split_vertical(section, width, height);
409
+ }
410
+ }
411
+ };
412
+
413
+ const GuillotineMinasBase = MinasMixin(Guillotine);
414
+ class GuillotineMinas extends GuillotineMinasBase {
415
+ constructor(...args) {
416
+ super(...args);
417
+ }
418
+ }
419
+
420
+ // Combined Guillotine algorithms
421
+
422
+ const GuillotineBssfSasBase = SasMixin(BssfMixin(Guillotine));
423
+
424
+ class GuillotineBssfSas extends GuillotineBssfSasBase {
425
+ constructor(...args) {
426
+ super(...args); // 🔑 this forwards the argument!
427
+
428
+ }
429
+
430
+ // Optionally override or combine behaviors here
431
+ }
432
+
433
+
434
+
435
+ const GuillotineBssfLasBase = LasMixin(BssfMixin(Guillotine));
436
+
437
+ class GuillotineBssfLas extends GuillotineBssfLasBase {
438
+ constructor(...args) {
439
+ super(...args); // 🔑 this forwards the argument!
440
+
441
+ }
442
+ }
443
+
444
+ const GuillotineBssfSlasBase = SlasMixin(BssfMixin(Guillotine));
445
+
446
+ class GuillotineBssfSlas extends GuillotineBssfSlasBase {
447
+ constructor(...args) {
448
+ super(...args); // 🔑 this forwards the argument!
449
+
450
+ }
451
+ }
452
+
453
+ const GuillotineBssfLlasBase = LlasMixin(BssfMixin(Guillotine));
454
+
455
+ class GuillotineBssfLlas extends GuillotineBssfLlasBase {
456
+ constructor(...args) {
457
+ super(...args); // 🔑 this forwards the argument!
458
+
459
+ }
460
+ }
461
+
462
+ const GuillotineBssfMaxasBase = MaxasMixin(BssfMixin(Guillotine));
463
+
464
+ class GuillotineBssfMaxas extends GuillotineBssfMaxasBase {
465
+ constructor(...args) {
466
+ super(...args); // 🔑 this forwards the argument!
467
+
468
+ }
469
+ }
470
+
471
+ const GuillotineBssfMinasBase = MinasMixin(BssfMixin(Guillotine));
472
+
473
+ class GuillotineBssfMinas extends GuillotineBssfMinasBase {
474
+ constructor(...args) {
475
+ super(...args); // 🔑 this forwards the argument!
476
+
477
+ }
478
+ }
479
+
480
+ const GuillotineBlsfSasBase = SasMixin(BlsfMixin(Guillotine));
481
+
482
+ class GuillotineBlsfSas extends GuillotineBlsfSasBase {
483
+ constructor(...args) {
484
+ super(...args); // 🔑 this forwards the argument!
485
+
486
+ }
487
+ }
488
+
489
+ const GuillotineBlsfLasBase = LasMixin(BlsfMixin(Guillotine));
490
+
491
+ class GuillotineBlsfLas extends GuillotineBlsfLasBase {
492
+ constructor(...args) {
493
+ super(...args); // 🔑 this forwards the argument!
494
+
495
+ }
496
+ }
497
+
498
+ const GuillotineBlsfSlasBase = SlasMixin(BlsfMixin(Guillotine));
499
+
500
+ class GuillotineBlsfSlas extends GuillotineBlsfSlasBase {
501
+ constructor(...args) {
502
+ super(...args); // 🔑 this forwards the argument!
503
+
504
+ }
505
+ }
506
+
507
+
508
+ const GuillotineBlsfLlasBase = LlasMixin(BlsfMixin(Guillotine));
509
+
510
+ class GuillotineBlsfLlas extends GuillotineBlsfLlasBase {
511
+ constructor(...args) {
512
+ super(...args); // 🔑 this forwards the argument!
513
+
514
+ }
515
+ }
516
+
517
+ const GuillotineBlsfMaxasBase = MaxasMixin(BlsfMixin(Guillotine));
518
+
519
+ class GuillotineBlsfMaxas extends GuillotineBlsfMaxasBase {
520
+ constructor(...args) {
521
+ super(...args); // 🔑 this forwards the argument!
522
+
523
+ }
524
+ }
525
+
526
+ const GuillotineBlsfMinasBase = MinasMixin(BlsfMixin(Guillotine));
527
+
528
+ class GuillotineBlsfMinas extends GuillotineBlsfMinasBase {
529
+ constructor(...args) {
530
+ super(...args); // 🔑 this forwards the argument!
531
+
532
+ }
533
+ }
534
+
535
+ const GuillotineBafSasBase = SasMixin(BafMixin(Guillotine));
536
+
537
+ class GuillotineBafSas extends GuillotineBafSasBase {
538
+ constructor(...args) {
539
+ super(...args); // 🔑 this forwards the argument!
540
+
541
+ }
542
+ }
543
+
544
+ const GuillotineBafLasBase = LasMixin(BafMixin(Guillotine));
545
+
546
+ class GuillotineBafLas extends GuillotineBafLasBase {
547
+ constructor(...args) {
548
+ super(...args); // 🔑 this forwards the argument!
549
+
550
+ }
551
+ }
552
+
553
+ const GuillotineBafSlasBase = SlasMixin(BafMixin(Guillotine));
554
+
555
+ class GuillotineBafSlas extends GuillotineBafSlasBase {
556
+ constructor(...args) {
557
+ super(...args); // 🔑 this forwards the argument!
558
+
559
+ }
560
+ }
561
+
562
+
563
+ const GuillotineBafLlasBase = LlasMixin(BafMixin(Guillotine));
564
+
565
+ class GuillotineBafLlas extends GuillotineBafLlasBase {
566
+ constructor(...args) {
567
+ super(...args); // 🔑 this forwards the argument!
568
+
569
+ }
570
+ }
571
+
572
+ const GuillotineBafMaxasBase = MaxasMixin(BafMixin(Guillotine));
573
+
574
+ class GuillotineBafMaxas extends GuillotineBafMaxasBase {
575
+ constructor(...args) {
576
+ super(...args); // 🔑 this forwards the argument!
577
+
578
+ }
579
+ }
580
+
581
+ const GuillotineBafMinasBase = MinasMixin(BafMixin(Guillotine));
582
+
583
+ class GuillotineBafMinas extends GuillotineBafMinasBase {
584
+ constructor(...args) {
585
+ super(...args); // 🔑 this forwards the argument!
586
+
587
+ }
588
+ }
589
+
590
+ module.exports = {
591
+ Guillotine,
592
+ GuillotineBaf,
593
+ GuillotineBlsf,
594
+ GuillotineBssf,
595
+ GuillotineSas,
596
+ GuillotineLas,
597
+ GuillotineSlas,
598
+ GuillotineLlas,
599
+ GuillotineMaxas,
600
+ GuillotineMinas,
601
+ GuillotineBssfSas,
602
+ GuillotineBssfLas,
603
+ GuillotineBssfSlas,
604
+ GuillotineBssfLlas,
605
+ GuillotineBssfMaxas,
606
+ GuillotineBssfMinas,
607
+ GuillotineBlsfSas,
608
+ GuillotineBlsfLas,
609
+ GuillotineBlsfSlas,
610
+ GuillotineBlsfLlas,
611
+ GuillotineBlsfMaxas,
612
+ GuillotineBlsfMinas,
613
+ GuillotineBafSas,
614
+ GuillotineBafLas,
615
+ GuillotineBafSlas,
616
+ GuillotineBafLlas,
617
+ GuillotineBafMaxas,
618
+ GuillotineBafMinas
619
+ };
620
+
621
+
@@ -0,0 +1,90 @@
1
+ // guillotine.js
2
+ import {
3
+ GuillotineBssfSas, GuillotineBssfLas,
4
+ GuillotineBssfSlas, GuillotineBssfLlas,
5
+ GuillotineBssfMaxas, GuillotineBssfMinas,
6
+ GuillotineBlsfSas, GuillotineBlsfLas,
7
+ GuillotineBlsfSlas, GuillotineBlsfLlas,
8
+ GuillotineBlsfMaxas, GuillotineBlsfMinas,
9
+ GuillotineBafSas, GuillotineBafLas,
10
+ GuillotineBafSlas, GuillotineBafLlas,
11
+ GuillotineBafMaxas, GuillotineBafMinas
12
+ } from './guillotine.js';
13
+
14
+
15
+
16
+
17
+ // packer.js - classes and functions
18
+ import {
19
+ float2dec,
20
+ SORT_AREA,
21
+ SORT_PERI,
22
+ SORT_DIFF,
23
+ SORT_SSIDE,
24
+ SORT_LSIDE,
25
+ SORT_RATIO,
26
+ SORT_NONE,
27
+ BinFactory,
28
+ PackerBNFMixin,
29
+ PackerBFFMixin,
30
+ PackerBBFMixin,
31
+ PackerOnline,
32
+ PackingMode,
33
+ PackingBin,
34
+ newPacker,
35
+ Packer,
36
+ PackerBNF,
37
+ PackerBFF,
38
+ PackerBBF,
39
+ PackerOnlineBNF,
40
+ PackerOnlineBFF,
41
+ PackerOnlineBBF,
42
+ PackerGlobal,
43
+ } from './packer.js';
44
+
45
+ // guillotine.js
46
+ export {
47
+ GuillotineBssfSas, GuillotineBssfLas,
48
+ GuillotineBssfSlas, GuillotineBssfLlas,
49
+ GuillotineBssfMaxas, GuillotineBssfMinas,
50
+ GuillotineBlsfSas, GuillotineBlsfLas,
51
+ GuillotineBlsfSlas, GuillotineBlsfLlas,
52
+ GuillotineBlsfMaxas, GuillotineBlsfMinas,
53
+ GuillotineBafSas, GuillotineBafLas,
54
+ GuillotineBafSlas, GuillotineBafLlas,
55
+ GuillotineBafMaxas, GuillotineBafMinas
56
+ }
57
+
58
+
59
+
60
+
61
+
62
+
63
+ // packer.js - classes and functions
64
+ export {
65
+ float2dec,
66
+ SORT_AREA,
67
+ SORT_PERI,
68
+ SORT_DIFF,
69
+ SORT_SSIDE,
70
+ SORT_LSIDE,
71
+ SORT_RATIO,
72
+ SORT_NONE,
73
+ BinFactory,
74
+ PackerBNFMixin,
75
+ PackerBFFMixin,
76
+ PackerBBFMixin,
77
+ PackerOnline,
78
+ PackingMode,
79
+ PackingBin,
80
+ newPacker,
81
+ Packer,
82
+ PackerBNF,
83
+ PackerBFF,
84
+ PackerBBF,
85
+ PackerOnlineBNF,
86
+ PackerOnlineBFF,
87
+ PackerOnlineBBF,
88
+ PackerGlobal,
89
+ }
90
+