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.
- package/.vscode/launch.json +18 -0
- package/README.md +16 -0
- package/babel.config.js +5 -0
- package/package.json +21 -3
- package/src/index.js +80 -0
- package/src/rectpack/geometry.js +224 -0
- package/src/rectpack/guillotine copy.js +406 -0
- package/src/rectpack/guillotine.js +621 -0
- package/src/rectpack/index.js +90 -0
- package/src/rectpack/packAlgo.js +154 -0
- package/src/rectpack/packer.js +578 -0
- package/src/rectpack/packer2.js +144 -0
- package/test/geometry/hsegment.test.js +77 -0
- package/test/geometry/point.test.js +120 -0
- package/test/geometry/rectangle.test.js +197 -0
- package/test/geometry/segment.test.js +87 -0
- package/test/geometry/vsegment.test.js +77 -0
- package/test/guillotine/GuillotineBssfSas.test.js +139 -0
- package/test/guillotine/GuillotineBssfSas2.test.js +141 -0
- package/test/guillotine/guillotine.test.js +248 -0
- package/test/packAlgo/packingAlgorithm.test.js +164 -0
- package/test/packer/packer.test.js +466 -0
- package/test/packer/sort.test.js +200 -0
- package/test/index.test.js +0 -0
|
@@ -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
|
+
|