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.
package/README.md CHANGED
@@ -1 +1,17 @@
1
1
 
2
+
3
+
4
+ ```js
5
+ const { newPacker, PackingMode, PackingBin, SORT_AREA } = require('./packer');
6
+
7
+ // Create a new packer with default settings (Offline mode, BBF algorithm)
8
+ const packer = newPacker();
9
+
10
+ // Create a packer with custom settings
11
+ const customPacker = newPacker({
12
+ mode: PackingMode.Online,
13
+ binAlgo: PackingBin.BFF,
14
+ sortAlgo: SORT_AREA,
15
+ rotation: false
16
+ });
17
+ ```
@@ -0,0 +1,5 @@
1
+ module.exports = {
2
+ presets: [
3
+ ['@babel/preset-env', {targets: {node: 'current'}}],
4
+ ],
5
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rectpackbin",
3
- "version": "0.0.2",
3
+ "version": "0.0.3",
4
4
  "description": "A helpful description of your package",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -11,5 +11,19 @@
11
11
  "url": "https://github.com/diljots99/rectpack.git"
12
12
  },
13
13
  "author": "Diljot Singh",
14
- "license": "MIT"
14
+ "license": "MIT",
15
+ "dependencies": {
16
+
17
+ },
18
+ "jest": {
19
+ "transform": {
20
+ "^.+\\.js$": "babel-jest"
21
+ },
22
+ "testEnvironment": "node"
23
+ },
24
+ "devDependencies": {
25
+ "@babel/core": "^7.26.10",
26
+ "@babel/preset-env": "^7.26.9",
27
+ "jest": "^29.7.0"
28
+ }
15
29
  }
package/src/index.js CHANGED
@@ -0,0 +1,59 @@
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 './rectpack/guillotine.js';
13
+
14
+
15
+
16
+ // packer.js - constants
17
+ import {
18
+ SORT_AREA, SORT_PERI, SORT_DIFF, SORT_SSIDE,
19
+ SORT_LSIDE, SORT_RATIO, SORT_NONE
20
+ } from './rectpack/packer.js';
21
+
22
+ // packer.js - classes and functions
23
+ import {
24
+ PackerBNF, PackerBFF, PackerBBF, PackerOnlineBNF,
25
+ PackerOnlineBFF, PackerOnlineBBF, PackerGlobal,
26
+ newPacker, PackingMode, PackingBin, float2dec
27
+ } from './rectpack/packer.js';
28
+
29
+ // guillotine.js
30
+ export {
31
+ GuillotineBssfSas, GuillotineBssfLas,
32
+ GuillotineBssfSlas, GuillotineBssfLlas,
33
+ GuillotineBssfMaxas, GuillotineBssfMinas,
34
+ GuillotineBlsfSas, GuillotineBlsfLas,
35
+ GuillotineBlsfSlas, GuillotineBlsfLlas,
36
+ GuillotineBlsfMaxas, GuillotineBlsfMinas,
37
+ GuillotineBafSas, GuillotineBafLas,
38
+ GuillotineBafSlas, GuillotineBafLlas,
39
+ GuillotineBafMaxas, GuillotineBafMinas
40
+ }
41
+
42
+
43
+
44
+
45
+
46
+ // packer.js - constants
47
+ export {
48
+ SORT_AREA, SORT_PERI, SORT_DIFF, SORT_SSIDE,
49
+ SORT_LSIDE, SORT_RATIO, SORT_NONE
50
+ }
51
+
52
+ // packer.js - classes and functions
53
+ export {
54
+ PackerBNF, PackerBFF, PackerBBF, PackerOnlineBNF,
55
+ PackerOnlineBFF, PackerOnlineBBF, PackerGlobal,
56
+ newPacker, PackingMode, PackingBin, float2dec
57
+ }
58
+
59
+
@@ -0,0 +1,567 @@
1
+ // from math import sqrt
2
+
3
+
4
+
5
+ // class Point(object):
6
+
7
+ // __slots__ = ('x', 'y')
8
+
9
+ // def __init__(self, x, y):
10
+ // self.x = x
11
+ // self.y = y
12
+
13
+ // def __eq__(self, other):
14
+ // return (self.x == other.x and self.y == other.y)
15
+
16
+ // def __repr__(self):
17
+ // return "P({}, {})".format(self.x, self.y)
18
+
19
+ // def distance(self, point):
20
+ // """
21
+ // Calculate distance to another point
22
+ // """
23
+ // return sqrt((self.x-point.x)**2+(self.y-point.y)**2)
24
+
25
+ // def distance_squared(self, point):
26
+ // return (self.x-point.x)**2+(self.y-point.y)**2
27
+
28
+
29
+ // class Segment(object):
30
+
31
+ // __slots__ = ('start', 'end')
32
+
33
+ // def __init__(self, start, end):
34
+ // """
35
+ // Arguments:
36
+ // start (Point): Segment start point
37
+ // end (Point): Segment end point
38
+ // """
39
+ // assert(isinstance(start, Point) and isinstance(end, Point))
40
+ // self.start = start
41
+ // self.end = end
42
+
43
+ // def __eq__(self, other):
44
+ // if not isinstance(other, self.__class__):
45
+ // None
46
+ // return self.start==other.start and self.end==other.end
47
+
48
+ // def __repr__(self):
49
+ // return "S({}, {})".format(self.start, self.end)
50
+
51
+ // @property
52
+ // def length_squared(self):
53
+ // """Faster than length and useful for some comparisons"""
54
+ // return self.start.distance_squared(self.end)
55
+
56
+ // @property
57
+ // def length(self):
58
+ // return self.start.distance(self.end)
59
+
60
+ // @property
61
+ // def top(self):
62
+ // return max(self.start.y, self.end.y)
63
+
64
+ // @property
65
+ // def bottom(self):
66
+ // return min(self.start.y, self.end.y)
67
+
68
+ // @property
69
+ // def right(self):
70
+ // return max(self.start.x, self.end.x)
71
+
72
+ // @property
73
+ // def left(self):
74
+ // return min(self.start.x, self.end.x)
75
+
76
+
77
+ // class HSegment(Segment):
78
+ // """Horizontal Segment"""
79
+
80
+ // def __init__(self, start, length):
81
+ // """
82
+ // Create an Horizontal segment given its left most end point and its
83
+ // length.
84
+
85
+ // Arguments:
86
+ // - start (Point): Starting Point
87
+ // - length (number): segment length
88
+ // """
89
+ // assert(isinstance(start, Point) and not isinstance(length, Point))
90
+ // super(HSegment, self).__init__(start, Point(start.x+length, start.y))
91
+
92
+ // @property
93
+ // def length(self):
94
+ // return self.end.x-self.start.x
95
+
96
+
97
+ // class VSegment(Segment):
98
+ // """Vertical Segment"""
99
+
100
+ // def __init__(self, start, length):
101
+ // """
102
+ // Create a Vertical segment given its bottom most end point and its
103
+ // length.
104
+
105
+ // Arguments:
106
+ // - start (Point): Starting Point
107
+ // - length (number): segment length
108
+ // """
109
+ // assert(isinstance(start, Point) and not isinstance(length, Point))
110
+ // super(VSegment, self).__init__(start, Point(start.x, start.y+length))
111
+
112
+ // @property
113
+ // def length(self):
114
+ // return self.end.y-self.start.y
115
+
116
+
117
+
118
+ // class Rectangle(object):
119
+ // """Basic rectangle primitive class.
120
+ // x, y-> Lower right corner coordinates
121
+ // width -
122
+ // height -
123
+ // """
124
+ // __slots__ = ('width', 'height', 'x', 'y', 'rid')
125
+
126
+ // def __init__(self, x, y, width, height, rid = None):
127
+ // """
128
+ // Args:
129
+ // x (int, float):
130
+ // y (int, float):
131
+ // width (int, float):
132
+ // height (int, float):
133
+ // rid (int):
134
+ // """
135
+ // assert(height >=0 and width >=0)
136
+
137
+ // self.width = width
138
+ // self.height = height
139
+ // self.x = x
140
+ // self.y = y
141
+ // self.rid = rid
142
+
143
+ // @property
144
+ // def bottom(self):
145
+ // """
146
+ // Rectangle bottom edge y coordinate
147
+ // """
148
+ // return self.y
149
+
150
+ // @property
151
+ // def top(self):
152
+ // """
153
+ // Rectangle top edge y coordiante
154
+ // """
155
+ // return self.y+self.height
156
+
157
+ // @property
158
+ // def left(self):
159
+ // """
160
+ // Rectangle left ednge x coordinate
161
+ // """
162
+ // return self.x
163
+
164
+ // @property
165
+ // def right(self):
166
+ // """
167
+ // Rectangle right edge x coordinate
168
+ // """
169
+ // return self.x+self.width
170
+
171
+ // @property
172
+ // def corner_top_l(self):
173
+ // return Point(self.left, self.top)
174
+
175
+ // @property
176
+ // def corner_top_r(self):
177
+ // return Point(self.right, self.top)
178
+
179
+ // @property
180
+ // def corner_bot_r(self):
181
+ // return Point(self.right, self.bottom)
182
+
183
+ // @property
184
+ // def corner_bot_l(self):
185
+ // return Point(self.left, self.bottom)
186
+
187
+ // def __lt__(self, other):
188
+ // """
189
+ // Compare rectangles by area (used for sorting)
190
+ // """
191
+ // return self.area() < other.area()
192
+
193
+ // def __eq__(self, other):
194
+ // """
195
+ // Equal rectangles have same area.
196
+ // """
197
+ // if not isinstance(other, self.__class__):
198
+ // return False
199
+
200
+ // return (self.width == other.width and \
201
+ // self.height == other.height and \
202
+ // self.x == other.x and \
203
+ // self.y == other.y)
204
+
205
+ // def __hash__(self):
206
+ // return hash((self.x, self.y, self.width, self.height))
207
+
208
+ // def __iter__(self):
209
+ // """
210
+ // Iterate through rectangle corners
211
+ // """
212
+ // yield self.corner_top_l
213
+ // yield self.corner_top_r
214
+ // yield self.corner_bot_r
215
+ // yield self.corner_bot_l
216
+
217
+ // def __repr__(self):
218
+ // return "R({}, {}, {}, {})".format(self.x, self.y, self.width, self.height)
219
+
220
+ // def area(self):
221
+ // """
222
+ // Rectangle area
223
+ // """
224
+ // return self.width * self.height
225
+
226
+ // def move(self, x, y):
227
+ // """
228
+ // Move Rectangle to x,y coordinates
229
+
230
+ // Arguments:
231
+ // x (int, float): X coordinate
232
+ // y (int, float): Y coordinate
233
+ // """
234
+ // self.x = x
235
+ // self.y = y
236
+
237
+ // def contains(self, rect):
238
+ // """
239
+ // Tests if another rectangle is contained by this one
240
+
241
+ // Arguments:
242
+ // rect (Rectangle): The other rectangle
243
+
244
+ // Returns:
245
+ // bool: True if it is container, False otherwise
246
+ // """
247
+ // return (rect.y >= self.y and \
248
+ // rect.x >= self.x and \
249
+ // rect.y+rect.height <= self.y+self.height and \
250
+ // rect.x+rect.width <= self.x+self.width)
251
+
252
+ // def intersects(self, rect, edges=False):
253
+ // """
254
+ // Detect intersections between this and another Rectangle.
255
+
256
+ // Parameters:
257
+ // rect (Rectangle): The other rectangle.
258
+ // edges (bool): True to consider rectangles touching by their
259
+ // edges or corners to be intersecting.
260
+ // (Should have been named include_touching)
261
+
262
+ // Returns:
263
+ // bool: True if the rectangles intersect, False otherwise
264
+ // """
265
+ // if edges:
266
+ // if (self.bottom > rect.top or self.top < rect.bottom or\
267
+ // self.left > rect.right or self.right < rect.left):
268
+ // return False
269
+ // else:
270
+ // if (self.bottom >= rect.top or self.top <= rect.bottom or
271
+ // self.left >= rect.right or self.right <= rect.left):
272
+ // return False
273
+
274
+ // return True
275
+
276
+ // def intersection(self, rect, edges=False):
277
+ // """
278
+ // Returns the rectangle resulting of the intersection between this and another
279
+ // rectangle. If the rectangles are only touching by their edges, and the
280
+ // argument 'edges' is True the rectangle returned will have an area of 0.
281
+ // Returns None if there is no intersection.
282
+
283
+ // Arguments:
284
+ // rect (Rectangle): The other rectangle.
285
+ // edges (bool): If True Rectangles touching by their edges are
286
+ // considered to be intersection. In this case a rectangle of
287
+ // 0 height or/and width will be returned.
288
+
289
+ // Returns:
290
+ // Rectangle: Intersection.
291
+ // None: There was no intersection.
292
+ // """
293
+ // if not self.intersects(rect, edges=edges):
294
+ // return None
295
+
296
+ // bottom = max(self.bottom, rect.bottom)
297
+ // left = max(self.left, rect.left)
298
+ // top = min(self.top, rect.top)
299
+ // right = min(self.right, rect.right)
300
+
301
+ // return Rectangle(left, bottom, right-left, top-bottom)
302
+
303
+ // def join(self, other):
304
+ // """
305
+ // Try to join a rectangle to this one, if the result is also a rectangle
306
+ // and the operation is successful and this rectangle is modified to the union.
307
+
308
+ // Arguments:
309
+ // other (Rectangle): Rectangle to join
310
+
311
+ // Returns:
312
+ // bool: True when successfully joined, False otherwise
313
+ // """
314
+ // if self.contains(other):
315
+ // return True
316
+
317
+ // if other.contains(self):
318
+ // self.x = other.x
319
+ // self.y = other.y
320
+ // self.width = other.width
321
+ // self.height = other.height
322
+ // return True
323
+
324
+ // if not self.intersects(other, edges=True):
325
+ // return False
326
+
327
+ // # Other rectangle is Up/Down from this
328
+ // if self.left == other.left and self.width == other.width:
329
+ // y_min = min(self.bottom, other.bottom)
330
+ // y_max = max(self.top, other.top)
331
+ // self.y = y_min
332
+ // self.height = y_max-y_min
333
+ // return True
334
+
335
+ // # Other rectangle is Right/Left from this
336
+ // if self.bottom == other.bottom and self.height == other.height:
337
+ // x_min = min(self.left, other.left)
338
+ // x_max = max(self.right, other.right)
339
+ // self.x = x_min
340
+ // self.width = x_max-x_min
341
+ // return True
342
+
343
+ // return False
344
+
345
+ class Point {
346
+ constructor(x, y) {
347
+ this.x = x;
348
+ this.y = y;
349
+ }
350
+
351
+ equals(other) {
352
+ return (this.x === other.x && this.y === other.y);
353
+ }
354
+
355
+ toString() {
356
+ return `P(${this.x}, ${this.y})`;
357
+ }
358
+
359
+ distance(point) {
360
+ return Math.sqrt((this.x - point.x) ** 2 + (this.y - point.y) ** 2);
361
+ }
362
+
363
+ distanceSquared(point) {
364
+ return (this.x - point.x) ** 2 + (this.y - point.y) ** 2;
365
+ }
366
+ }
367
+
368
+ class Segment {
369
+ constructor(start, end) {
370
+ if (!(start instanceof Point) || !(end instanceof Point)) {
371
+ throw new Error("Start and end must be Point instances");
372
+ }
373
+ this.start = start;
374
+ this.end = end;
375
+ }
376
+
377
+ equals(other) {
378
+ if (!(other instanceof Segment)) {
379
+ return false;
380
+ }
381
+ return this.start.equals(other.start) && this.end.equals(other.end);
382
+ }
383
+
384
+ toString() {
385
+ return `S(${this.start}, ${this.end})`;
386
+ }
387
+
388
+ get lengthSquared() {
389
+ return this.start.distanceSquared(this.end);
390
+ }
391
+
392
+ get length() {
393
+ return this.start.distance(this.end);
394
+ }
395
+
396
+ get top() {
397
+ return Math.max(this.start.y, this.end.y);
398
+ }
399
+
400
+ get bottom() {
401
+ return Math.min(this.start.y, this.end.y);
402
+ }
403
+
404
+ get right() {
405
+ return Math.max(this.start.x, this.end.x);
406
+ }
407
+
408
+ get left() {
409
+ return Math.min(this.start.x, this.end.x);
410
+ }
411
+ }
412
+
413
+ class HSegment extends Segment {
414
+ constructor(start, length) {
415
+ if (!(start instanceof Point) || typeof length !== 'number') {
416
+ throw new Error("Start must be a Point and length must be a number");
417
+ }
418
+ super(start, new Point(start.x + length, start.y));
419
+ }
420
+
421
+ get length() {
422
+ return this.end.x - this.start.x;
423
+ }
424
+ }
425
+
426
+ class VSegment extends Segment {
427
+ constructor(start, length) {
428
+ if (!(start instanceof Point) || typeof length !== 'number') {
429
+ throw new Error("Start must be a Point and length must be a number");
430
+ }
431
+ super(start, new Point(start.x, start.y + length));
432
+ }
433
+
434
+ get length() {
435
+ return this.end.y - this.start.y;
436
+ }
437
+ }
438
+
439
+ class Rectangle {
440
+ constructor(x, y, width, height, rid = null) {
441
+ if (height < 0 || width < 0) {
442
+ throw new Error("Height and width must be non-negative");
443
+ }
444
+ this.width = width;
445
+ this.height = height;
446
+ this.x = x;
447
+ this.y = y;
448
+ this.rid = rid;
449
+ }
450
+
451
+ get bottom() {
452
+ return this.y;
453
+ }
454
+
455
+ get top() {
456
+ return this.y + this.height;
457
+ }
458
+
459
+ get left() {
460
+ return this.x;
461
+ }
462
+
463
+ get right() {
464
+ return this.x + this.width;
465
+ }
466
+
467
+ get cornerTopL() {
468
+ return new Point(this.left, this.top);
469
+ }
470
+
471
+ get cornerTopR() {
472
+ return new Point(this.right, this.top);
473
+ }
474
+
475
+ get cornerBotR() {
476
+ return new Point(this.right, this.bottom);
477
+ }
478
+
479
+ get cornerBotL() {
480
+ return new Point(this.left, this.bottom);
481
+ }
482
+
483
+ area() {
484
+ return this.width * this.height;
485
+ }
486
+
487
+ move(x, y) {
488
+ this.x = x;
489
+ this.y = y;
490
+ }
491
+
492
+ contains(rect) {
493
+ return (rect.y >= this.y &&
494
+ rect.x >= this.x &&
495
+ rect.y + rect.height <= this.y + this.height &&
496
+ rect.x + rect.width <= this.x + this.width);
497
+ }
498
+
499
+ intersects(rect, edges = false) {
500
+ if (edges) {
501
+ if (this.bottom > rect.top || this.top < rect.bottom ||
502
+ this.left > rect.right || this.right < rect.left) {
503
+ return false;
504
+ }
505
+ } else {
506
+ if (this.bottom >= rect.top || this.top <= rect.bottom ||
507
+ this.left >= rect.right || this.right <= rect.left) {
508
+ return false;
509
+ }
510
+ }
511
+ return true;
512
+ }
513
+
514
+ intersection(rect, edges = false) {
515
+ if (!this.intersects(rect, edges)) {
516
+ return null;
517
+ }
518
+
519
+ const bottom = Math.max(this.bottom, rect.bottom);
520
+ const left = Math.max(this.left, rect.left);
521
+ const top = Math.min(this.top, rect.top);
522
+ const right = Math.min(this.right, rect.right);
523
+
524
+ return new Rectangle(left, bottom, right - left, top - bottom);
525
+ }
526
+
527
+ join(other) {
528
+ if (this.contains(other)) {
529
+ return true;
530
+ }
531
+
532
+ if (other.contains(this)) {
533
+ this.x = other.x;
534
+ this.y = other.y;
535
+ this.width = other.width;
536
+ this.height = other.height;
537
+ return true;
538
+ }
539
+
540
+ if (!this.intersects(other, true)) {
541
+ return false;
542
+ }
543
+
544
+ // Other rectangle is Up/Down from this
545
+ if (this.left === other.left && this.width === other.width) {
546
+ const yMin = Math.min(this.bottom, other.bottom);
547
+ const yMax = Math.max(this.top, other.top);
548
+ this.y = yMin;
549
+ this.height = yMax - yMin;
550
+ return true;
551
+ }
552
+
553
+ // Other rectangle is Right/Left from this
554
+ if (this.bottom === other.bottom && this.height === other.height) {
555
+ const xMin = Math.min(this.left, other.left);
556
+ const xMax = Math.max(this.right, other.right);
557
+ this.x = xMin;
558
+ this.width = xMax - xMin;
559
+ return true;
560
+ }
561
+
562
+ return false;
563
+ }
564
+ }
565
+
566
+ // Export the classes
567
+ module.exports = { Point, Segment, HSegment, VSegment, Rectangle };