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 +16 -0
- package/babel.config.js +5 -0
- package/package.json +16 -2
- package/src/index.js +59 -0
- package/src/rectpack/geometry.js +567 -0
- package/src/rectpack/guillotine.js +405 -0
- package/src/rectpack/packAlgo.js +153 -0
- package/src/rectpack/packer.js +317 -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/guillotine.test.js +248 -0
- package/test/packAlgo/packingAlgorithm.test.js +164 -0
- package/test/packer/sort.test.js +200 -0
- package/test/index.test.js +0 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Point, Segment } from '../../src/rectpack/geometry.js';
|
|
2
|
+
|
|
3
|
+
describe('Segment', () => {
|
|
4
|
+
let point1;
|
|
5
|
+
let point2;
|
|
6
|
+
let segment;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
point1 = new Point(0, 0);
|
|
10
|
+
point2 = new Point(3, 4);
|
|
11
|
+
segment = new Segment(point1, point2);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
describe('constructor', () => {
|
|
15
|
+
it('should create a segment with valid points', () => {
|
|
16
|
+
expect(segment.start).toBe(point1);
|
|
17
|
+
expect(segment.end).toBe(point2);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it('should throw error when points are invalid', () => {
|
|
21
|
+
expect(() => new Segment('not a point', point2)).toThrow('Start and end must be Point instances');
|
|
22
|
+
expect(() => new Segment(point1, 'not a point')).toThrow('Start and end must be Point instances');
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe('equals', () => {
|
|
27
|
+
it('should return true for identical segments', () => {
|
|
28
|
+
const segment2 = new Segment(new Point(0, 0), new Point(3, 4));
|
|
29
|
+
expect(segment.equals(segment2)).toBe(true);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('should return false for different segments', () => {
|
|
33
|
+
const segment2 = new Segment(new Point(0, 0), new Point(5, 5));
|
|
34
|
+
expect(segment.equals(segment2)).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should return false for non-segment objects', () => {
|
|
38
|
+
expect(segment.equals('not a segment')).toBe(false);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('toString', () => {
|
|
43
|
+
it('should return correct string representation', () => {
|
|
44
|
+
expect(segment.toString()).toBe('S(P(0, 0), P(3, 4))');
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('lengthSquared', () => {
|
|
49
|
+
it('should return correct squared length', () => {
|
|
50
|
+
// For points (0,0) and (3,4), lengthSquared should be 25 (3² + 4²)
|
|
51
|
+
expect(segment.lengthSquared).toBe(25);
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('length', () => {
|
|
56
|
+
it('should return correct length', () => {
|
|
57
|
+
// For points (0,0) and (3,4), length should be 5
|
|
58
|
+
expect(segment.length).toBe(5);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe('boundaries', () => {
|
|
63
|
+
it('should calculate correct top value', () => {
|
|
64
|
+
expect(segment.top).toBe(4);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should calculate correct bottom value', () => {
|
|
68
|
+
expect(segment.bottom).toBe(0);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('should calculate correct right value', () => {
|
|
72
|
+
expect(segment.right).toBe(3);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should calculate correct left value', () => {
|
|
76
|
+
expect(segment.left).toBe(0);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('should handle segments with reversed points', () => {
|
|
80
|
+
const reversedSegment = new Segment(new Point(3, 4), new Point(0, 0));
|
|
81
|
+
expect(reversedSegment.top).toBe(4);
|
|
82
|
+
expect(reversedSegment.bottom).toBe(0);
|
|
83
|
+
expect(reversedSegment.right).toBe(3);
|
|
84
|
+
expect(reversedSegment.left).toBe(0);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Point, VSegment } from '../../src/rectpack/geometry.js';
|
|
2
|
+
|
|
3
|
+
describe('VSegment', () => {
|
|
4
|
+
let startPoint;
|
|
5
|
+
let length;
|
|
6
|
+
let vSegment;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
startPoint = new Point(5, 0);
|
|
10
|
+
length = 10;
|
|
11
|
+
vSegment = new VSegment(startPoint, length);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
describe('constructor', () => {
|
|
15
|
+
it('should create a vertical segment with valid start point and length', () => {
|
|
16
|
+
expect(vSegment.start).toBe(startPoint);
|
|
17
|
+
expect(vSegment.end.x).toBe(startPoint.x);
|
|
18
|
+
expect(vSegment.end.y).toBe(startPoint.y + length);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should throw error when start point is invalid', () => {
|
|
22
|
+
expect(() => new VSegment('not a point', 10)).toThrow('Start must be a Point and length must be a number');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('should create a segment with negative length', () => {
|
|
26
|
+
const negativeLength = -5;
|
|
27
|
+
const negativeVSegment = new VSegment(startPoint, negativeLength);
|
|
28
|
+
expect(negativeVSegment.start).toBe(startPoint);
|
|
29
|
+
expect(negativeVSegment.end.x).toBe(startPoint.x);
|
|
30
|
+
expect(negativeVSegment.end.y).toBe(startPoint.y + negativeLength);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe('length', () => {
|
|
35
|
+
it('should return correct positive length', () => {
|
|
36
|
+
expect(vSegment.length).toBe(10);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('should return correct negative length', () => {
|
|
40
|
+
const negativeVSegment = new VSegment(startPoint, -5);
|
|
41
|
+
expect(negativeVSegment.length).toBe(-5);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should return zero for zero-length segment', () => {
|
|
45
|
+
const zeroLengthSegment = new VSegment(startPoint, 0);
|
|
46
|
+
expect(zeroLengthSegment.length).toBe(0);
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('inherited properties', () => {
|
|
51
|
+
it('should calculate correct left and right values', () => {
|
|
52
|
+
expect(vSegment.left).toBe(5);
|
|
53
|
+
expect(vSegment.right).toBe(5);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('should calculate correct top and bottom values', () => {
|
|
57
|
+
expect(vSegment.bottom).toBe(0);
|
|
58
|
+
expect(vSegment.top).toBe(10);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should have correct string representation', () => {
|
|
62
|
+
expect(vSegment.toString()).toBe('S(P(5, 0), P(5, 10))');
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('equals', () => {
|
|
67
|
+
it('should return true for identical vertical segments', () => {
|
|
68
|
+
const vSegment2 = new VSegment(new Point(5, 0), 10);
|
|
69
|
+
expect(vSegment.equals(vSegment2)).toBe(true);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should return false for different vertical segments', () => {
|
|
73
|
+
const vSegment2 = new VSegment(new Point(6, 0), 10);
|
|
74
|
+
expect(vSegment.equals(vSegment2)).toBe(false);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
});
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
const { Rectangle } = require('../../src/rectpack/geometry');
|
|
2
|
+
const { GuillotineBssfSas } = require('../../src/rectpack/guillotine');
|
|
3
|
+
|
|
4
|
+
describe('GuillotineBssfSas', () => {
|
|
5
|
+
let packer;
|
|
6
|
+
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
packer = new GuillotineBssfSas(100, 100);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
describe('constructor', () => {
|
|
12
|
+
it('should create packer with default values', () => {
|
|
13
|
+
expect(packer.width).toBe(100);
|
|
14
|
+
expect(packer.height).toBe(100);
|
|
15
|
+
expect(packer.rot).toBe(true);
|
|
16
|
+
expect(packer._merge).toBe(true);
|
|
17
|
+
expect(packer._sections).toHaveLength(1);
|
|
18
|
+
expect(packer._sections[0]).toEqual(new Rectangle(0, 0, 100, 100,0));
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should create packer with custom values', () => {
|
|
22
|
+
const custom = new GuillotineBssfSas(200, 150, false, false, 'test-id');
|
|
23
|
+
expect(custom.width).toBe(200);
|
|
24
|
+
expect(custom.height).toBe(150);
|
|
25
|
+
expect(custom.rot).toBe(false);
|
|
26
|
+
expect(custom._merge).toBe(false);
|
|
27
|
+
expect(custom.bid).toBe('test-id');
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('_section_fitness (BSSF)', () => {
|
|
32
|
+
it('should return null for sections too small', () => {
|
|
33
|
+
const section = new Rectangle(0, 0, 30, 30);
|
|
34
|
+
expect(packer._section_fitness(section, 40, 20)).toBeNull();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should return minimum of remaining sides', () => {
|
|
38
|
+
const section = new Rectangle(0, 0, 100, 100);
|
|
39
|
+
// For a 60x40 rectangle in 100x100 section:
|
|
40
|
+
// Remaining sides are: 100-60=40 and 100-40=60
|
|
41
|
+
// BSSF should return the minimum = 40
|
|
42
|
+
expect(packer._section_fitness(section, 60, 40)).toBe(40);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe('_split (SAS)', () => {
|
|
47
|
+
it('should split horizontally when width < height', () => {
|
|
48
|
+
const section = new Rectangle(0, 0, 50, 100);
|
|
49
|
+
packer._sections = [];
|
|
50
|
+
packer._split(section, 30, 40);
|
|
51
|
+
|
|
52
|
+
// Should split horizontally because width (50) < height (100)
|
|
53
|
+
expect(packer._sections).toHaveLength(2);
|
|
54
|
+
expect(packer._sections).toContainEqual(new Rectangle(0, 40, 50, 60,0)); // Top section
|
|
55
|
+
expect(packer._sections).toContainEqual(new Rectangle(30, 0, 20, 40,0)); // Right section
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should split vertically when width >= height', () => {
|
|
59
|
+
const section = new Rectangle(0, 0, 100, 50);
|
|
60
|
+
packer._sections = [];
|
|
61
|
+
packer._split(section, 30, 40);
|
|
62
|
+
|
|
63
|
+
// Should split vertically because width (100) >= height (50)
|
|
64
|
+
expect(packer._sections).toHaveLength(2);
|
|
65
|
+
expect(packer._sections).toContainEqual(new Rectangle(0, 40, 30, 10,0)); // Top section
|
|
66
|
+
expect(packer._sections).toContainEqual(new Rectangle(30, 0, 70, 50,0)); // Right section
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// describe('addRect', () => {
|
|
71
|
+
// it('should place rectangle using BSSF and SAS', () => {
|
|
72
|
+
// const rect = packer.addRect(60, 40, 'test');
|
|
73
|
+
// expect(rect).toBeTruthy();
|
|
74
|
+
// expect(rect.width).toBe(60);
|
|
75
|
+
// expect(rect.height).toBe(40);
|
|
76
|
+
// expect(rect.x).toBe(0);
|
|
77
|
+
// expect(rect.y).toBe(0);
|
|
78
|
+
|
|
79
|
+
// // Verify sections were split according to SAS rule
|
|
80
|
+
// expect(packer._sections).toHaveLength(2);
|
|
81
|
+
// // Since width (100) >= height (100), it should split vertically
|
|
82
|
+
// expect(packer._sections[0]).toContainEqual(new Rectangle(0, 40, 60, 60,0)); // Top section
|
|
83
|
+
// expect(packer._sections[1]).toContainEqual(new Rectangle(60, 0, 40, 100,0)); // Right section
|
|
84
|
+
// });
|
|
85
|
+
|
|
86
|
+
// it('should handle multiple rectangles', () => {
|
|
87
|
+
// const rect1 = packer.addRect(60, 40, 'rect1');
|
|
88
|
+
// const rect2 = packer.addRect(30, 30, 'rect2');
|
|
89
|
+
|
|
90
|
+
// expect(rect1).toBeTruthy();
|
|
91
|
+
// expect(rect2).toBeTruthy();
|
|
92
|
+
// expect(packer.rectangles).toHaveLength(2);
|
|
93
|
+
// });
|
|
94
|
+
|
|
95
|
+
// it('should utilize rotation when beneficial', () => {
|
|
96
|
+
// packer._sections = [new Rectangle(0, 0, 100, 130,0)];
|
|
97
|
+
// const rect = packer.addRect(120, 30);
|
|
98
|
+
// expect(rect).toBeTruthy();
|
|
99
|
+
// expect(rect.width).toBe(30);
|
|
100
|
+
// expect(rect.height).toBe(120);
|
|
101
|
+
// });
|
|
102
|
+
|
|
103
|
+
// it('should return null when rectangle cannot be placed', () => {
|
|
104
|
+
// packer.addRect(90, 90); // Fill most of the space
|
|
105
|
+
// const rect = packer.addRect(50, 50); // Try to place another large rectangle
|
|
106
|
+
// expect(rect).toBeNull();
|
|
107
|
+
// });
|
|
108
|
+
// });
|
|
109
|
+
|
|
110
|
+
describe('fitness', () => {
|
|
111
|
+
it('should calculate correct fitness for placeable rectangles', () => {
|
|
112
|
+
const fitness = packer.fitness(60, 40);
|
|
113
|
+
expect(fitness).toBe(40); // min(100-60, 100-40) = 40
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should handle rotation in fitness calculation', () => {
|
|
117
|
+
packer._sections = [new Rectangle(0, 0, 100, 130,0)];
|
|
118
|
+
const fitness = packer.fitness(120, 30);
|
|
119
|
+
expect(fitness).toBe(10); // min(100-30, 100-120) after rotation
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('should return null for unplaceable rectangles', () => {
|
|
123
|
+
const fitness = packer.fitness(150, 150);
|
|
124
|
+
expect(fitness).toBeNull();
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe('reset', () => {
|
|
129
|
+
it('should restore initial state', () => {
|
|
130
|
+
packer.addRect(60, 40);
|
|
131
|
+
packer.addRect(30, 30);
|
|
132
|
+
packer.reset();
|
|
133
|
+
|
|
134
|
+
expect(packer.rectangles).toHaveLength(0);
|
|
135
|
+
expect(packer._sections).toHaveLength(1);
|
|
136
|
+
expect(packer._sections[0]).toEqual(new Rectangle(0, 0, 100, 100,0));
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
});
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
const { Rectangle } = require('../../src/rectpack/geometry');
|
|
2
|
+
const { Guillotine } = require('../../src/rectpack/guillotine');
|
|
3
|
+
|
|
4
|
+
describe('Guillotine', () => {
|
|
5
|
+
let guillotine;
|
|
6
|
+
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
guillotine = new Guillotine(100, 100);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
describe('constructor', () => {
|
|
12
|
+
it('should create guillotine with default values', () => {
|
|
13
|
+
expect(guillotine.width).toBe(100);
|
|
14
|
+
expect(guillotine.height).toBe(100);
|
|
15
|
+
expect(guillotine.rot).toBe(true);
|
|
16
|
+
expect(guillotine._merge).toBe(true);
|
|
17
|
+
expect(guillotine._sections).toHaveLength(1);
|
|
18
|
+
expect(guillotine._sections[0]).toEqual(new Rectangle(0, 0, 100, 100,0));
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it('should create guillotine with custom values', () => {
|
|
22
|
+
const custom = new Guillotine(200, 150, false, false, 'test-id');
|
|
23
|
+
expect(custom.width).toBe(200);
|
|
24
|
+
expect(custom.height).toBe(150);
|
|
25
|
+
expect(custom.rot).toBe(false);
|
|
26
|
+
expect(custom._merge).toBe(false);
|
|
27
|
+
expect(custom.bid).toBe('test-id');
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe('_add_section', () => {
|
|
32
|
+
it('should add section when merge is disabled', () => {
|
|
33
|
+
guillotine._merge = false;
|
|
34
|
+
const section = new Rectangle(10, 10, 50, 50);
|
|
35
|
+
guillotine._add_section(section);
|
|
36
|
+
expect(guillotine._sections).toContainEqual(section);
|
|
37
|
+
expect(section.rid).toBe(0);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should merge sections when possible', () => {
|
|
41
|
+
guillotine._sections = [];
|
|
42
|
+
const section1 = new Rectangle(0, 0, 50, 100);
|
|
43
|
+
const section2 = new Rectangle(50, 0, 50, 100);
|
|
44
|
+
guillotine._add_section(section1);
|
|
45
|
+
guillotine._add_section(section2);
|
|
46
|
+
expect(guillotine._sections).toHaveLength(1);
|
|
47
|
+
expect(guillotine._sections[0]).toEqual(new Rectangle(0, 0, 100, 100,0));
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe('_split_horizontal', () => {
|
|
52
|
+
it('should create correct sections after horizontal split', () => {
|
|
53
|
+
const section = new Rectangle(0, 0, 100, 100);
|
|
54
|
+
guillotine._sections = [];
|
|
55
|
+
guillotine._split_horizontal(section, 60, 40);
|
|
56
|
+
|
|
57
|
+
expect(guillotine._sections).toHaveLength(2);
|
|
58
|
+
expect(guillotine._sections).toContainEqual(new Rectangle(0, 40, 100, 60,0)); // Top section
|
|
59
|
+
expect(guillotine._sections).toContainEqual(new Rectangle(60, 0, 40, 40,0)); // Right section
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should handle split when width equals section width', () => {
|
|
63
|
+
const section = new Rectangle(0, 0, 100, 100);
|
|
64
|
+
guillotine._sections = [];
|
|
65
|
+
guillotine._split_horizontal(section, 100, 40);
|
|
66
|
+
|
|
67
|
+
expect(guillotine._sections).toHaveLength(1);
|
|
68
|
+
expect(guillotine._sections[0]).toEqual(new Rectangle(0, 40, 100, 60,0));
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe('_split_vertical', () => {
|
|
73
|
+
it('should create correct sections after vertical split', () => {
|
|
74
|
+
const section = new Rectangle(0, 0, 100, 100);
|
|
75
|
+
guillotine._sections = [];
|
|
76
|
+
guillotine._split_vertical(section, 60, 40);
|
|
77
|
+
|
|
78
|
+
expect(guillotine._sections).toHaveLength(2);
|
|
79
|
+
expect(guillotine._sections).toContainEqual(new Rectangle(0, 40, 60, 60,0)); // Top section
|
|
80
|
+
expect(guillotine._sections).toContainEqual(new Rectangle(60, 0, 40, 100,0)); // Right section
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should handle split when height equals section height', () => {
|
|
84
|
+
const section = new Rectangle(0, 0, 100, 100);
|
|
85
|
+
guillotine._sections = [];
|
|
86
|
+
guillotine._split_vertical(section, 60, 100);
|
|
87
|
+
|
|
88
|
+
expect(guillotine._sections).toHaveLength(1);
|
|
89
|
+
expect(guillotine._sections[0]).toEqual(new Rectangle(60, 0, 40, 100,0));
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe('_select_fittest_section', () => {
|
|
94
|
+
class TestGuillotine extends Guillotine {
|
|
95
|
+
_section_fitness(section, width, height) {
|
|
96
|
+
if (width > section.width || height > section.height) {
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
return Math.min(section.width - width, section.height - height);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
_split(section, width, height) {
|
|
103
|
+
if (section.width < section.height) {
|
|
104
|
+
return this._split_horizontal(section, width, height);
|
|
105
|
+
} else {
|
|
106
|
+
return this._split_vertical(section, width, height);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
let testGuillotine;
|
|
112
|
+
|
|
113
|
+
beforeEach(() => {
|
|
114
|
+
testGuillotine = new TestGuillotine(100, 100);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('should select best fitting section', () => {
|
|
118
|
+
const [section, rotated] = testGuillotine._select_fittest_section(50, 50);
|
|
119
|
+
expect(section).toBeTruthy();
|
|
120
|
+
expect(rotated).toBe(false);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('should handle rotation when enabled', () => {
|
|
124
|
+
testGuillotine._add_section(new Rectangle(0, 0, 100, 120,0));
|
|
125
|
+
const [section, rotated] = testGuillotine._select_fittest_section(120, 50);
|
|
126
|
+
expect(section).toBeTruthy();
|
|
127
|
+
expect(rotated).toBe(true);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('should return null when no fit is possible', () => {
|
|
131
|
+
const [section, rotated] = testGuillotine._select_fittest_section(150, 150);
|
|
132
|
+
expect(section).toBeNull();
|
|
133
|
+
expect(rotated).toBeNull();
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
describe('addRect', () => {
|
|
138
|
+
class TestGuillotine extends Guillotine {
|
|
139
|
+
_section_fitness(section, width, height) {
|
|
140
|
+
if (width > section.width || height > section.height) {
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
return Math.min(section.width - width, section.height - height);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
_split(section, width, height) {
|
|
147
|
+
if (section.width < section.height) {
|
|
148
|
+
return this._split_horizontal(section, width, height);
|
|
149
|
+
} else {
|
|
150
|
+
return this._split_vertical(section, width, height);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
let testGuillotine;
|
|
156
|
+
|
|
157
|
+
beforeEach(() => {
|
|
158
|
+
testGuillotine = new TestGuillotine(100, 100);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('should add rectangle successfully', () => {
|
|
162
|
+
const rect = testGuillotine.addRect(50, 50, 'test');
|
|
163
|
+
expect(rect).toBeTruthy();
|
|
164
|
+
expect(rect.width).toBe(50);
|
|
165
|
+
expect(rect.height).toBe(50);
|
|
166
|
+
expect(rect.rid).toBe('test');
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('should handle rotation when needed', () => {
|
|
170
|
+
testGuillotine._add_section(new Rectangle(0, 0, 100, 120,0));
|
|
171
|
+
const rect = testGuillotine.addRect(120, 50);
|
|
172
|
+
expect(rect).toBeTruthy();
|
|
173
|
+
expect(rect.width).toBe(50);
|
|
174
|
+
expect(rect.height).toBe(120);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it('should return null when rectangle cannot be placed', () => {
|
|
178
|
+
const rect = testGuillotine.addRect(150, 150);
|
|
179
|
+
expect(rect).toBeNull();
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('should throw error for invalid dimensions', () => {
|
|
183
|
+
expect(() => testGuillotine.addRect(0, 50)).toThrow('Width and height must be positive');
|
|
184
|
+
expect(() => testGuillotine.addRect(50, 0)).toThrow('Width and height must be positive');
|
|
185
|
+
expect(() => testGuillotine.addRect(-50, 50)).toThrow('Width and height must be positive');
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
describe('fitness', () => {
|
|
190
|
+
class TestGuillotine extends Guillotine {
|
|
191
|
+
_section_fitness(section, width, height) {
|
|
192
|
+
if (width > section.width || height > section.height) {
|
|
193
|
+
return null;
|
|
194
|
+
}
|
|
195
|
+
return Math.min(section.width - width, section.height - height);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
_split(section, width, height) {
|
|
199
|
+
if (section.width < section.height) {
|
|
200
|
+
return this._split_horizontal(section, width, height);
|
|
201
|
+
} else {
|
|
202
|
+
return this._split_vertical(section, width, height);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
let testGuillotine;
|
|
208
|
+
|
|
209
|
+
beforeEach(() => {
|
|
210
|
+
testGuillotine = new TestGuillotine(100, 130);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('should calculate fitness correctly', () => {
|
|
214
|
+
|
|
215
|
+
const fitness = testGuillotine.fitness(50, 50);
|
|
216
|
+
expect(fitness).toBe(50);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('should handle rotation in fitness calculation', () => {
|
|
220
|
+
const fitness = testGuillotine.fitness(120, 50);
|
|
221
|
+
expect(fitness).toBe(10);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('should return null when no fit is possible', () => {
|
|
225
|
+
const fitness = testGuillotine.fitness(150, 150);
|
|
226
|
+
expect(fitness).toBeNull();
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('should throw error for invalid dimensions', () => {
|
|
230
|
+
expect(() => testGuillotine.fitness(0, 50)).toThrow('Width and height must be positive');
|
|
231
|
+
expect(() => testGuillotine.fitness(50, 0)).toThrow('Width and height must be positive');
|
|
232
|
+
expect(() => testGuillotine.fitness(-50, 50)).toThrow('Width and height must be positive');
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
describe('reset', () => {
|
|
237
|
+
it('should reset to initial state', () => {
|
|
238
|
+
const testGuillotine = new Guillotine(100, 100);
|
|
239
|
+
testGuillotine.rectangles.push(new Rectangle(0, 0, 50, 50,0));
|
|
240
|
+
// testGuillotine.addRect(50, 50);
|
|
241
|
+
testGuillotine.reset();
|
|
242
|
+
|
|
243
|
+
expect(testGuillotine.rectangles).toHaveLength(0);
|
|
244
|
+
expect(testGuillotine._sections).toHaveLength(1);
|
|
245
|
+
expect(testGuillotine._sections[0]).toEqual(new Rectangle(0, 0, 100, 100,0));
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
});
|