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,18 @@
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+
8
+ {
9
+ "type": "node",
10
+ "request": "launch",
11
+ "name": "Launch Program",
12
+ "skipFiles": [
13
+ "<node_internals>/**"
14
+ ],
15
+ "program": "${workspaceFolder}\\driver_code.js"
16
+ }
17
+ ]
18
+ }
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,9 +1,11 @@
1
1
  {
2
2
  "name": "rectpackbin",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "A helpful description of your package",
5
- "main": "index.js",
5
+ "main": "src/index.js",
6
+ "module": "commonjs",
6
7
  "scripts": {
8
+ "test:single": "jest -t default_options test/packer/packer.test.js",
7
9
  "test": "jest"
8
10
  },
9
11
  "repository": {
@@ -11,5 +13,21 @@
11
13
  "url": "https://github.com/diljots99/rectpack.git"
12
14
  },
13
15
  "author": "Diljot Singh",
14
- "license": "MIT"
16
+ "license": "MIT",
17
+ "dependencies": {
18
+ "canvas": "^3.1.0",
19
+ "guillotine-packer": "^1.0.2",
20
+ "sharp": "^0.34.1"
21
+ },
22
+ "jest": {
23
+ "transform": {
24
+ "^.+\\.js$": "babel-jest"
25
+ },
26
+ "testEnvironment": "node"
27
+ },
28
+ "devDependencies": {
29
+ "@babel/core": "^7.26.10",
30
+ "@babel/preset-env": "^7.26.9",
31
+ "jest": "^29.7.0"
32
+ }
15
33
  }
package/src/index.js CHANGED
@@ -0,0 +1,80 @@
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
+ float2dec,
25
+ SORT_AREA,
26
+ SORT_PERI,
27
+ SORT_DIFF,
28
+ SORT_SSIDE,
29
+ SORT_LSIDE,
30
+ SORT_RATIO,
31
+ SORT_NONE,
32
+ BinFactory,
33
+ PackerBNFMixin,
34
+ PackerBFFMixin,
35
+ PackerBBFMixin,
36
+ PackerOnline,
37
+ PackingMode,
38
+ PackingBin,
39
+ newPacker
40
+ } from './rectpack/packer';
41
+
42
+ // guillotine.js
43
+ export {
44
+ GuillotineBssfSas, GuillotineBssfLas,
45
+ GuillotineBssfSlas, GuillotineBssfLlas,
46
+ GuillotineBssfMaxas, GuillotineBssfMinas,
47
+ GuillotineBlsfSas, GuillotineBlsfLas,
48
+ GuillotineBlsfSlas, GuillotineBlsfLlas,
49
+ GuillotineBlsfMaxas, GuillotineBlsfMinas,
50
+ GuillotineBafSas, GuillotineBafLas,
51
+ GuillotineBafSlas, GuillotineBafLlas,
52
+ GuillotineBafMaxas, GuillotineBafMinas
53
+ }
54
+
55
+
56
+
57
+
58
+
59
+
60
+ // packer.js - classes and functions
61
+ export {
62
+ float2dec,
63
+ SORT_AREA,
64
+ SORT_PERI,
65
+ SORT_DIFF,
66
+ SORT_SSIDE,
67
+ SORT_LSIDE,
68
+ SORT_RATIO,
69
+ SORT_NONE,
70
+ BinFactory,
71
+ PackerBNFMixin,
72
+ PackerBFFMixin,
73
+ PackerBBFMixin,
74
+ PackerOnline,
75
+ PackingMode,
76
+ PackingBin,
77
+ newPacker
78
+ }
79
+
80
+
@@ -0,0 +1,224 @@
1
+
2
+ class Point {
3
+ constructor(x, y) {
4
+ this.x = x;
5
+ this.y = y;
6
+ }
7
+
8
+ equals(other) {
9
+ return (this.x === other.x && this.y === other.y);
10
+ }
11
+
12
+ toString() {
13
+ return `P(${this.x}, ${this.y})`;
14
+ }
15
+
16
+ distance(point) {
17
+ return Math.sqrt((this.x - point.x) ** 2 + (this.y - point.y) ** 2);
18
+ }
19
+
20
+ distanceSquared(point) {
21
+ return (this.x - point.x) ** 2 + (this.y - point.y) ** 2;
22
+ }
23
+ }
24
+
25
+ class Segment {
26
+ constructor(start, end) {
27
+ if (!(start instanceof Point) || !(end instanceof Point)) {
28
+ throw new Error("Start and end must be Point instances");
29
+ }
30
+ this.start = start;
31
+ this.end = end;
32
+ }
33
+
34
+ equals(other) {
35
+ if (!(other instanceof Segment)) {
36
+ return false;
37
+ }
38
+ return this.start.equals(other.start) && this.end.equals(other.end);
39
+ }
40
+
41
+ toString() {
42
+ return `S(${this.start}, ${this.end})`;
43
+ }
44
+
45
+ get lengthSquared() {
46
+ return this.start.distanceSquared(this.end);
47
+ }
48
+
49
+ get length() {
50
+ return this.start.distance(this.end);
51
+ }
52
+
53
+ get top() {
54
+ return Math.max(this.start.y, this.end.y);
55
+ }
56
+
57
+ get bottom() {
58
+ return Math.min(this.start.y, this.end.y);
59
+ }
60
+
61
+ get right() {
62
+ return Math.max(this.start.x, this.end.x);
63
+ }
64
+
65
+ get left() {
66
+ return Math.min(this.start.x, this.end.x);
67
+ }
68
+ }
69
+
70
+ class HSegment extends Segment {
71
+ constructor(start, length) {
72
+ if (!(start instanceof Point) || typeof length !== 'number') {
73
+ throw new Error("Start must be a Point and length must be a number");
74
+ }
75
+ super(start, new Point(start.x + length, start.y));
76
+ }
77
+
78
+ get length() {
79
+ return this.end.x - this.start.x;
80
+ }
81
+ }
82
+
83
+ class VSegment extends Segment {
84
+ constructor(start, length) {
85
+ if (!(start instanceof Point) || typeof length !== 'number') {
86
+ throw new Error("Start must be a Point and length must be a number");
87
+ }
88
+ super(start, new Point(start.x, start.y + length));
89
+ }
90
+
91
+ get length() {
92
+ return this.end.y - this.start.y;
93
+ }
94
+ }
95
+
96
+ class Rectangle {
97
+ constructor(x, y, width, height, rid = null) {
98
+ if (height < 0 || width < 0) {
99
+ throw new Error("Height and width must be non-negative");
100
+ }
101
+ this.width = width;
102
+ this.height = height;
103
+ this.x = x;
104
+ this.y = y;
105
+ this.rid = rid;
106
+ }
107
+
108
+ get bottom() {
109
+ return this.y;
110
+ }
111
+
112
+ get top() {
113
+ return this.y + this.height;
114
+ }
115
+
116
+ get left() {
117
+ return this.x;
118
+ }
119
+
120
+ get right() {
121
+ return this.x + this.width;
122
+ }
123
+
124
+ get cornerTopL() {
125
+ return new Point(this.left, this.top);
126
+ }
127
+
128
+ get cornerTopR() {
129
+ return new Point(this.right, this.top);
130
+ }
131
+
132
+ get cornerBotR() {
133
+ return new Point(this.right, this.bottom);
134
+ }
135
+
136
+ get cornerBotL() {
137
+ return new Point(this.left, this.bottom);
138
+ }
139
+
140
+ area() {
141
+ return this.width * this.height;
142
+ }
143
+
144
+ move(x, y) {
145
+ this.x = x;
146
+ this.y = y;
147
+ }
148
+
149
+ contains(rect) {
150
+ return (rect.y >= this.y &&
151
+ rect.x >= this.x &&
152
+ rect.y + rect.height <= this.y + this.height &&
153
+ rect.x + rect.width <= this.x + this.width);
154
+ }
155
+
156
+ intersects(rect, edges = false) {
157
+ if (edges) {
158
+ if (this.bottom > rect.top || this.top < rect.bottom ||
159
+ this.left > rect.right || this.right < rect.left) {
160
+ return false;
161
+ }
162
+ } else {
163
+ if (this.bottom >= rect.top || this.top <= rect.bottom ||
164
+ this.left >= rect.right || this.right <= rect.left) {
165
+ return false;
166
+ }
167
+ }
168
+ return true;
169
+ }
170
+
171
+ intersection(rect, edges = false) {
172
+ if (!this.intersects(rect, edges)) {
173
+ return null;
174
+ }
175
+
176
+ const bottom = Math.max(this.bottom, rect.bottom);
177
+ const left = Math.max(this.left, rect.left);
178
+ const top = Math.min(this.top, rect.top);
179
+ const right = Math.min(this.right, rect.right);
180
+
181
+ return new Rectangle(left, bottom, right - left, top - bottom);
182
+ }
183
+
184
+ join(other) {
185
+ if (this.contains(other)) {
186
+ return true;
187
+ }
188
+
189
+ if (other.contains(this)) {
190
+ this.x = other.x;
191
+ this.y = other.y;
192
+ this.width = other.width;
193
+ this.height = other.height;
194
+ return true;
195
+ }
196
+
197
+ if (!this.intersects(other, true)) {
198
+ return false;
199
+ }
200
+
201
+ // Other rectangle is Up/Down from this
202
+ if (this.left === other.left && this.width === other.width) {
203
+ const yMin = Math.min(this.bottom, other.bottom);
204
+ const yMax = Math.max(this.top, other.top);
205
+ this.y = yMin;
206
+ this.height = yMax - yMin;
207
+ return true;
208
+ }
209
+
210
+ // Other rectangle is Right/Left from this
211
+ if (this.bottom === other.bottom && this.height === other.height) {
212
+ const xMin = Math.min(this.left, other.left);
213
+ const xMax = Math.max(this.right, other.right);
214
+ this.x = xMin;
215
+ this.width = xMax - xMin;
216
+ return true;
217
+ }
218
+
219
+ return false;
220
+ }
221
+ }
222
+
223
+ // Export the classes
224
+ module.exports = { Point, Segment, HSegment, VSegment, Rectangle };