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.
@@ -0,0 +1,406 @@
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
+
262
+ _split(section, width, height) {
263
+ if (section.width - width < section.height - height) {
264
+ return this._split_horizontal(section, width, height);
265
+ } else {
266
+ return this._split_vertical(section, width, height);
267
+ }
268
+ }
269
+ }
270
+
271
+ /**
272
+ * Split the section into two parts, the width is greater than the height
273
+ */
274
+ class GuillotineLlas extends Guillotine {
275
+ _split(section, width, height) {
276
+ if (section.width - width >= section.height - height) {
277
+ return this._split_horizontal(section, width, height);
278
+ } else {
279
+ return this._split_vertical(section, width, height);
280
+ }
281
+ }
282
+ }
283
+
284
+ /**
285
+ * Split the section into two parts, the width is greater than the height
286
+ */
287
+ class GuillotineMaxas extends Guillotine {
288
+ _split(section, width, height) {
289
+ if (width * (section.height - height) <= height * (section.width - width)) {
290
+ return this._split_horizontal(section, width, height);
291
+ } else {
292
+ return this._split_vertical(section, width, height);
293
+ }
294
+ }
295
+ }
296
+
297
+ /**
298
+ * Split the section into two parts, the width is greater than the height
299
+ */
300
+ class GuillotineMinas extends Guillotine {
301
+ _split(section, width, height) {
302
+ if (width * (section.height - height) >= height * (section.width - width)) {
303
+ return this._split_horizontal(section, width, height);
304
+ } else {
305
+ return this._split_vertical(section, width, height);
306
+ }
307
+ }
308
+ }
309
+
310
+ // Combined Guillotine algorithms
311
+ class GuillotineBssfSas extends GuillotineBssf {
312
+ _split(section, width, height) {
313
+ if (section.width - width < section.height - height) {
314
+ return this._split_horizontal(section, width, height);
315
+ } else {
316
+ return this._split_vertical(section, width, height);
317
+ }
318
+ }
319
+ }
320
+ Object.assign(GuillotineBssfSas.prototype, GuillotineSas.prototype);
321
+
322
+
323
+
324
+ class GuillotineBssfLas extends GuillotineBssf {}
325
+ Object.assign(GuillotineBssfLas.prototype, GuillotineLas.prototype);
326
+
327
+ class GuillotineBssfSlas extends GuillotineBssf {}
328
+ Object.assign(GuillotineBssfSlas.prototype, GuillotineSlas.prototype);
329
+
330
+ class GuillotineBssfLlas extends GuillotineBssf {}
331
+ Object.assign(GuillotineBssfLlas.prototype, GuillotineLlas.prototype);
332
+
333
+ class GuillotineBssfMaxas extends GuillotineBssf {}
334
+ Object.assign(GuillotineBssfMaxas.prototype, GuillotineMaxas.prototype);
335
+
336
+ class GuillotineBssfMinas extends GuillotineBssf {}
337
+ Object.assign(GuillotineBssfMinas.prototype, GuillotineMinas.prototype);
338
+
339
+ class GuillotineBlsfSas extends GuillotineBlsf {}
340
+ Object.assign(GuillotineBlsfSas.prototype, GuillotineSas.prototype);
341
+
342
+ class GuillotineBlsfLas extends GuillotineBlsf {}
343
+ Object.assign(GuillotineBlsfLas.prototype, GuillotineLas.prototype);
344
+
345
+ class GuillotineBlsfSlas extends GuillotineBlsf {}
346
+ Object.assign(GuillotineBlsfSlas.prototype, GuillotineSlas.prototype);
347
+
348
+ class GuillotineBlsfLlas extends GuillotineBlsf {}
349
+ Object.assign(GuillotineBlsfLlas.prototype, GuillotineLlas.prototype);
350
+
351
+ class GuillotineBlsfMaxas extends GuillotineBlsf {}
352
+ Object.assign(GuillotineBlsfMaxas.prototype, GuillotineMaxas.prototype);
353
+
354
+ class GuillotineBlsfMinas extends GuillotineBlsf {}
355
+ Object.assign(GuillotineBlsfMinas.prototype, GuillotineMinas.prototype);
356
+
357
+ class GuillotineBafSas extends GuillotineBaf {}
358
+ Object.assign(GuillotineBafSas.prototype, GuillotineSas.prototype);
359
+
360
+ class GuillotineBafLas extends GuillotineBaf {}
361
+ Object.assign(GuillotineBafLas.prototype, GuillotineLas.prototype);
362
+
363
+ class GuillotineBafSlas extends GuillotineBaf {}
364
+ Object.assign(GuillotineBafSlas.prototype, GuillotineSlas.prototype);
365
+
366
+ class GuillotineBafLlas extends GuillotineBaf {}
367
+ Object.assign(GuillotineBafLlas.prototype, GuillotineLlas.prototype);
368
+
369
+ class GuillotineBafMaxas extends GuillotineBaf {}
370
+ Object.assign(GuillotineBafMaxas.prototype, GuillotineMaxas.prototype);
371
+
372
+ class GuillotineBafMinas extends GuillotineBaf {}
373
+ Object.assign(GuillotineBafMinas.prototype, GuillotineMinas.prototype);
374
+
375
+ module.exports = {
376
+ Guillotine,
377
+ GuillotineBaf,
378
+ GuillotineBlsf,
379
+ GuillotineBssf,
380
+ GuillotineSas,
381
+ GuillotineLas,
382
+ GuillotineSlas,
383
+ GuillotineLlas,
384
+ GuillotineMaxas,
385
+ GuillotineMinas,
386
+ GuillotineBssfSas,
387
+ GuillotineBssfLas,
388
+ GuillotineBssfSlas,
389
+ GuillotineBssfLlas,
390
+ GuillotineBssfMaxas,
391
+ GuillotineBssfMinas,
392
+ GuillotineBlsfSas,
393
+ GuillotineBlsfLas,
394
+ GuillotineBlsfSlas,
395
+ GuillotineBlsfLlas,
396
+ GuillotineBlsfMaxas,
397
+ GuillotineBlsfMinas,
398
+ GuillotineBafSas,
399
+ GuillotineBafLas,
400
+ GuillotineBafSlas,
401
+ GuillotineBafLlas,
402
+ GuillotineBafMaxas,
403
+ GuillotineBafMinas
404
+ };
405
+
406
+