rectpackbin 0.0.3 → 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.
@@ -1,7 +1,8 @@
1
1
  const { Rectangle } = require('./geometry');
2
2
  const PackingAlgorithm = require('./packAlgo');
3
3
 
4
- class Guillotine extends PackingAlgorithm {
4
+
5
+ const GuillotineMixin = Base => class extends Base {
5
6
  /**
6
7
  * Implementation of several variants of Guillotine packing algorithm
7
8
  * For a more detailed explanation of the algorithm used, see:
@@ -120,17 +121,21 @@ class Guillotine extends PackingAlgorithm {
120
121
  })
121
122
  .filter(x => x !== null);
122
123
 
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) : [];
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
+ }
129
134
 
130
135
  const fit = [...fitn, ...fitr];
131
136
 
132
137
  if (fit.length === 0) {
133
- return [null, null];
138
+ return [];
134
139
  }
135
140
 
136
141
  const [_, sec, rot] = fit.reduce((min, curr) =>
@@ -158,7 +163,9 @@ class Guillotine extends PackingAlgorithm {
158
163
  }
159
164
 
160
165
  if (rotated) {
161
- [width, height] = [height, width];
166
+ let temp = width;
167
+ width = height;
168
+ height = temp;
162
169
  }
163
170
 
164
171
  this._sections = this._sections.filter(s => s !== section);
@@ -200,6 +207,13 @@ class Guillotine extends PackingAlgorithm {
200
207
  }
201
208
  }
202
209
 
210
+ const GuillotineBase = GuillotineMixin(PackingAlgorithm);
211
+ class Guillotine extends GuillotineBase {
212
+ constructor(...args) {
213
+ super(...args);
214
+ }
215
+ }
216
+
203
217
  // Implementing the various Guillotine algorithms
204
218
  class GuillotineBaf extends Guillotine {
205
219
  _section_fitness(section, width, height) {
@@ -209,6 +223,18 @@ class GuillotineBaf extends Guillotine {
209
223
  return section.area() - width * height;
210
224
  }
211
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
+ }
212
238
 
213
239
  class GuillotineBlsf extends Guillotine {
214
240
  _section_fitness(section, width, height) {
@@ -219,6 +245,20 @@ class GuillotineBlsf extends Guillotine {
219
245
  }
220
246
  }
221
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
+
222
262
  class GuillotineBssf extends Guillotine {
223
263
  _section_fitness(section, width, height) {
224
264
  if (width > section.width || height > section.height) {
@@ -228,10 +268,25 @@ class GuillotineBssf extends Guillotine {
228
268
  }
229
269
  }
230
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
+
231
283
  /**
232
284
  * Split the section into two parts, the width is less than the height
233
285
  */
234
- class GuillotineSas extends Guillotine {
286
+ const SasMixin = Base => class extends Base {
287
+ constructor(...args) {
288
+ super(...args);
289
+ }
235
290
  _split(section, width, height) {
236
291
  if (section.width < section.height) {
237
292
  return this._split_horizontal(section, width, height);
@@ -239,137 +294,298 @@ class GuillotineSas extends Guillotine {
239
294
  return this._split_vertical(section, width, height);
240
295
  }
241
296
  }
297
+ };
298
+
299
+ const GuillotineSasBase = SasMixin(Guillotine);
300
+ class GuillotineSas extends GuillotineSasBase {
301
+ constructor(...args) {
302
+ super(...args);
303
+ }
242
304
  }
243
305
 
244
306
  /**
245
307
  * Split the section into two parts, the width is greater than the height
246
308
  */
247
- class GuillotineLas extends Guillotine {
248
- _split(section, width, height) {
309
+ const LasMixin = Base => class extends Base {
310
+ constructor(...args) {
311
+ super(...args);
312
+ }
313
+ _split(section, width, height) {
249
314
  if (section.width >= section.height) {
250
315
  return this._split_horizontal(section, width, height);
251
316
  } else {
252
317
  return this._split_vertical(section, width, height);
253
318
  }
254
319
  }
320
+ };
321
+
322
+ const GuillotineLasBase = LasMixin(Guillotine);
323
+ class GuillotineLas extends GuillotineLasBase {
324
+ constructor(...args) {
325
+ super(...args);
326
+ }
255
327
  }
256
328
 
257
329
  /**
258
330
  * Split the section into two parts, the width is less than the height
259
331
  */
260
- class GuillotineSlas extends Guillotine {
261
- _split(section, width, height) {
332
+ const SlasMixin = Base => class extends Base {
333
+ constructor(...args) {
334
+ super(...args);
335
+ }
336
+ _split(section, width, height) {
262
337
  if (section.width - width < section.height - height) {
263
338
  return this._split_horizontal(section, width, height);
264
339
  } else {
265
340
  return this._split_vertical(section, width, height);
266
341
  }
267
342
  }
268
- }
343
+ };
269
344
 
345
+ const GuillotineSlasBase = SlasMixin(Guillotine);
346
+ class GuillotineSlas extends GuillotineSlasBase {
347
+ constructor(...args) {
348
+ super(...args);
349
+ }
350
+ }
270
351
  /**
271
352
  * Split the section into two parts, the width is greater than the height
272
353
  */
273
- class GuillotineLlas extends Guillotine {
274
- _split(section, width, height) {
275
- if (section.width - width >= section.height - height) {
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) {
276
360
  return this._split_horizontal(section, width, height);
277
361
  } else {
278
362
  return this._split_vertical(section, width, height);
279
363
  }
280
364
  }
365
+ };
366
+
367
+ const GuillotineLlasBase = LlasMixin(Guillotine);
368
+ class GuillotineLlas extends GuillotineLlasBase {
369
+ constructor(...args) {
370
+ super(...args);
371
+ }
281
372
  }
282
373
 
283
374
  /**
284
375
  * Split the section into two parts, the width is greater than the height
285
376
  */
286
- class GuillotineMaxas extends Guillotine {
287
- _split(section, width, height) {
288
- if (width * (section.height - height) <= height * (section.width - width)) {
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) {
289
383
  return this._split_horizontal(section, width, height);
290
384
  } else {
291
385
  return this._split_vertical(section, width, height);
292
386
  }
293
387
  }
294
- }
388
+ };
295
389
 
390
+ const GuillotineMaxasBase = MaxasMixin(Guillotine);
391
+ class GuillotineMaxas extends GuillotineMaxasBase {
392
+ constructor(...args) {
393
+ super(...args);
394
+ }
395
+ }
296
396
  /**
297
397
  * Split the section into two parts, the width is greater than the height
298
398
  */
299
- class GuillotineMinas extends Guillotine {
300
- _split(section, width, height) {
399
+
400
+ const MinasMixin = Base => class extends Base {
401
+ constructor(...args) {
402
+ super(...args);
403
+ }
404
+ _split(section, width, height) {
301
405
  if (width * (section.height - height) >= height * (section.width - width)) {
302
406
  return this._split_horizontal(section, width, height);
303
407
  } else {
304
408
  return this._split_vertical(section, width, height);
305
409
  }
306
410
  }
411
+ };
412
+
413
+ const GuillotineMinasBase = MinasMixin(Guillotine);
414
+ class GuillotineMinas extends GuillotineMinasBase {
415
+ constructor(...args) {
416
+ super(...args);
417
+ }
307
418
  }
308
419
 
309
420
  // 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
- }
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
+ }
318
442
  }
319
- Object.assign(GuillotineBssfSas.prototype, GuillotineSas.prototype);
320
443
 
444
+ const GuillotineBssfSlasBase = SlasMixin(BssfMixin(Guillotine));
321
445
 
446
+ class GuillotineBssfSlas extends GuillotineBssfSlasBase {
447
+ constructor(...args) {
448
+ super(...args); // 🔑 this forwards the argument!
449
+
450
+ }
451
+ }
322
452
 
323
- class GuillotineBssfLas extends GuillotineBssf {}
324
- Object.assign(GuillotineBssfLas.prototype, GuillotineLas.prototype);
453
+ const GuillotineBssfLlasBase = LlasMixin(BssfMixin(Guillotine));
325
454
 
326
- class GuillotineBssfSlas extends GuillotineBssf {}
327
- Object.assign(GuillotineBssfSlas.prototype, GuillotineSlas.prototype);
455
+ class GuillotineBssfLlas extends GuillotineBssfLlasBase {
456
+ constructor(...args) {
457
+ super(...args); // 🔑 this forwards the argument!
458
+
459
+ }
460
+ }
328
461
 
329
- class GuillotineBssfLlas extends GuillotineBssf {}
330
- Object.assign(GuillotineBssfLlas.prototype, GuillotineLlas.prototype);
462
+ const GuillotineBssfMaxasBase = MaxasMixin(BssfMixin(Guillotine));
331
463
 
332
- class GuillotineBssfMaxas extends GuillotineBssf {}
333
- Object.assign(GuillotineBssfMaxas.prototype, GuillotineMaxas.prototype);
464
+ class GuillotineBssfMaxas extends GuillotineBssfMaxasBase {
465
+ constructor(...args) {
466
+ super(...args); // 🔑 this forwards the argument!
467
+
468
+ }
469
+ }
334
470
 
335
- class GuillotineBssfMinas extends GuillotineBssf {}
336
- Object.assign(GuillotineBssfMinas.prototype, GuillotineMinas.prototype);
471
+ const GuillotineBssfMinasBase = MinasMixin(BssfMixin(Guillotine));
337
472
 
338
- class GuillotineBlsfSas extends GuillotineBlsf {}
339
- Object.assign(GuillotineBlsfSas.prototype, GuillotineSas.prototype);
473
+ class GuillotineBssfMinas extends GuillotineBssfMinasBase {
474
+ constructor(...args) {
475
+ super(...args); // 🔑 this forwards the argument!
476
+
477
+ }
478
+ }
340
479
 
341
- class GuillotineBlsfLas extends GuillotineBlsf {}
342
- Object.assign(GuillotineBlsfLas.prototype, GuillotineLas.prototype);
480
+ const GuillotineBlsfSasBase = SasMixin(BlsfMixin(Guillotine));
343
481
 
344
- class GuillotineBlsfSlas extends GuillotineBlsf {}
345
- Object.assign(GuillotineBlsfSlas.prototype, GuillotineSlas.prototype);
482
+ class GuillotineBlsfSas extends GuillotineBlsfSasBase {
483
+ constructor(...args) {
484
+ super(...args); // 🔑 this forwards the argument!
485
+
486
+ }
487
+ }
346
488
 
347
- class GuillotineBlsfLlas extends GuillotineBlsf {}
348
- Object.assign(GuillotineBlsfLlas.prototype, GuillotineLlas.prototype);
489
+ const GuillotineBlsfLasBase = LasMixin(BlsfMixin(Guillotine));
349
490
 
350
- class GuillotineBlsfMaxas extends GuillotineBlsf {}
351
- Object.assign(GuillotineBlsfMaxas.prototype, GuillotineMaxas.prototype);
491
+ class GuillotineBlsfLas extends GuillotineBlsfLasBase {
492
+ constructor(...args) {
493
+ super(...args); // 🔑 this forwards the argument!
494
+
495
+ }
496
+ }
352
497
 
353
- class GuillotineBlsfMinas extends GuillotineBlsf {}
354
- Object.assign(GuillotineBlsfMinas.prototype, GuillotineMinas.prototype);
498
+ const GuillotineBlsfSlasBase = SlasMixin(BlsfMixin(Guillotine));
355
499
 
356
- class GuillotineBafSas extends GuillotineBaf {}
357
- Object.assign(GuillotineBafSas.prototype, GuillotineSas.prototype);
500
+ class GuillotineBlsfSlas extends GuillotineBlsfSlasBase {
501
+ constructor(...args) {
502
+ super(...args); // 🔑 this forwards the argument!
503
+
504
+ }
505
+ }
358
506
 
359
- class GuillotineBafLas extends GuillotineBaf {}
360
- Object.assign(GuillotineBafLas.prototype, GuillotineLas.prototype);
361
507
 
362
- class GuillotineBafSlas extends GuillotineBaf {}
363
- Object.assign(GuillotineBafSlas.prototype, GuillotineSlas.prototype);
508
+ const GuillotineBlsfLlasBase = LlasMixin(BlsfMixin(Guillotine));
364
509
 
365
- class GuillotineBafLlas extends GuillotineBaf {}
366
- Object.assign(GuillotineBafLlas.prototype, GuillotineLlas.prototype);
510
+ class GuillotineBlsfLlas extends GuillotineBlsfLlasBase {
511
+ constructor(...args) {
512
+ super(...args); // 🔑 this forwards the argument!
513
+
514
+ }
515
+ }
367
516
 
368
- class GuillotineBafMaxas extends GuillotineBaf {}
369
- Object.assign(GuillotineBafMaxas.prototype, GuillotineMaxas.prototype);
517
+ const GuillotineBlsfMaxasBase = MaxasMixin(BlsfMixin(Guillotine));
370
518
 
371
- class GuillotineBafMinas extends GuillotineBaf {}
372
- Object.assign(GuillotineBafMinas.prototype, GuillotineMinas.prototype);
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
+ }
373
589
 
374
590
  module.exports = {
375
591
  Guillotine,
@@ -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
+
@@ -45,8 +45,9 @@ class PackingAlgorithm {
45
45
  }
46
46
 
47
47
  if (this.rot && (width > this.width || height > this.height)) {
48
- width = height
49
- height = width
48
+ let temp = width;
49
+ width = height;
50
+ height = temp;
50
51
  }
51
52
 
52
53
  if (width > this.width || height > this.height) {