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,154 @@
1
+ const { Rectangle } = require('./geometry');
2
+
3
+ class PackingAlgorithm {
4
+ /**
5
+ * Initialize packing algorithm
6
+ * @param {number} width - Packing surface width
7
+ * @param {number} height - Packing surface height
8
+ * @param {boolean} rot - Rectangle rotation enabled or disabled
9
+ * @param {string|number} bid - Packing surface identification
10
+ */
11
+ constructor(width, height, rot = true, bid = null) {
12
+ this.width = width;
13
+ this.height = height;
14
+ this.rot = rot;
15
+ this.rectangles = [];
16
+ this.bid = bid;
17
+ this._surface = new Rectangle(0, 0, width, height);
18
+ this.reset();
19
+ }
20
+
21
+ /**
22
+ * Get the number of rectangles
23
+ * @returns {number}
24
+ */
25
+ get length() {
26
+ return this.rectangles.length;
27
+ }
28
+
29
+ /**
30
+ * Make the class iterable
31
+ */
32
+ [Symbol.iterator]() {
33
+ return this.rectangles[Symbol.iterator]();
34
+ }
35
+
36
+ /**
37
+ * Test if surface is big enough to place a rectangle
38
+ * @param {number} width - Rectangle width
39
+ * @param {number} height - Rectangle height
40
+ * @returns {boolean} - True if it could be placed, False otherwise
41
+ */
42
+ _fitsSurface(width, height) {
43
+ if (!(width > 0 && height > 0)) {
44
+ throw new Error("Width and height must be positive");
45
+ }
46
+
47
+ if (this.rot && (width > this.width || height > this.height)) {
48
+ let temp = width;
49
+ width = height;
50
+ height = temp;
51
+ }
52
+
53
+ if (width > this.width || height > this.height) {
54
+ return false;
55
+ }
56
+
57
+ return true;
58
+ }
59
+
60
+ /**
61
+ * Return rectangle in selected position
62
+ * @param {number} key - Index of the rectangle
63
+ * @returns {Rectangle}
64
+ */
65
+ getItem(key) {
66
+ return this.rectangles[key];
67
+ }
68
+
69
+ /**
70
+ * Total area of rectangles placed
71
+ * @returns {number} - Area
72
+ */
73
+ usedArea() {
74
+ return this.rectangles.reduce((sum, rect) => sum + rect.area(), 0);
75
+ }
76
+
77
+ /**
78
+ * Metric used to rate how much space is wasted if a rectangle is placed
79
+ * @param {number} width - Rectangle width
80
+ * @param {number} height - Rectangle height
81
+ * @param {boolean} rot - Enable rectangle rotation
82
+ * @returns {number|null} - Rectangle fitness or null if can't be placed
83
+ */
84
+ fitness(width, height, rot = false) {
85
+ throw new Error("Not implemented");
86
+ }
87
+
88
+ /**
89
+ * Add rectangle of width x height dimensions
90
+ * @param {number} width - Rectangle width
91
+ * @param {number} height - Rectangle height
92
+ * @param {string|number} rid - Optional rectangle user id
93
+ * @returns {Rectangle|null} - Rectangle with placement coordinates or null if couldn't be placed
94
+ */
95
+ addRect(width, height, rid = null) {
96
+ throw new Error("Not implemented");
97
+ }
98
+
99
+ /**
100
+ * Returns a list with all rectangles placed into the surface
101
+ * @returns {Array} - Format [[x, y, width, height, rid], ...]
102
+ */
103
+ rectList() {
104
+ return this.rectangles.map(r => [r.x, r.y, r.width, r.height, r.rid]);
105
+ }
106
+
107
+ /**
108
+ * Check for collisions between rectangles and validate placement inside surface
109
+ * @throws {Error} If validation fails
110
+ */
111
+ validatePacking() {
112
+ const surface = new Rectangle(0, 0, this.width, this.height);
113
+
114
+ // Check if all rectangles are within surface
115
+ for (const rect of this.rectangles) {
116
+ if (!surface.contains(rect)) {
117
+ throw new Error("Rectangle placed outside surface");
118
+ }
119
+ }
120
+
121
+ // Check for collisions between rectangles
122
+ if (this.rectangles.length <= 1) {
123
+ return;
124
+ }
125
+
126
+ for (let r1 = 0; r1 <= this.rectangles.length - 2; r1++) {
127
+
128
+ for (let r2 = r1 + 1; r2 <= this.rectangles.length - 1; r2++) {
129
+ if (this.rectangles[r1].intersects(this.rectangles[r2])) {
130
+ throw new Error("Rectangle collision detected");
131
+ }
132
+ }
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Returns true if there are no rectangles placed
138
+ * @returns {boolean}
139
+ */
140
+ isEmpty() {
141
+ return this.rectangles.length === 0;
142
+ }
143
+
144
+ /**
145
+ * Reset the packing algorithm
146
+ */
147
+ reset() {
148
+ this.rectangles = [];
149
+ }
150
+ }
151
+
152
+ module.exports = PackingAlgorithm;
153
+
154
+