-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStripPackingUI.java
More file actions
224 lines (192 loc) · 7.82 KB
/
Copy pathStripPackingUI.java
File metadata and controls
224 lines (192 loc) · 7.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.scene.input.*;
import java.util.*;
import java.io.File;
import java.io.IOException;
public class StripPackingUI extends Application {
public static Group root;
public static final int resX = 800;
public static final int resY = 600;
public static final int MAX_SNAPSHOTS = 5000;
public static int nSnapshotCalls = 0;
public static int snapshotStartPoint = 0;
public static int currentSnapshot = 0;
public static int currentWidth = 0;
public static final ArrayList<Rect[]> snapshots = new ArrayList<>();
public static final ArrayList<Integer> snapshotHeights = new ArrayList<>();
public interface SnapshotFunction{
public void run(Rect[] array, ArrayList<Rect> arrayList, int height);
}
public static class TestCase {
public FloatingRect[] floatingRects;
public int width;
public TestCase(FloatingRect[] floatingRects, int width) {
this.floatingRects = floatingRects;
this.width = width;
}
}
private ArrayList<Rectangle> existingRectangles = new ArrayList<>();
public static void main(String[] args) {
Application.launch(args);
}
public static TestCase getTestCase() {
return new TestCase(
new FloatingRect[] {
FloatingRect.create(3,5),
FloatingRect.create(6,9),
FloatingRect.create(6,3),
FloatingRect.create(3,1),
FloatingRect.create(1,4)
}, 10
);
}
public static TestCase readStdinTestCase() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int w = sc.nextInt();
FloatingRect[] rects = new FloatingRect[n];
for (int i=0;i<n;++i) {
rects[i] = FloatingRect.create(sc.nextInt(),sc.nextInt());
}
return new TestCase(rects, w);
}
public static TestCase readFromFile(String fileName) {
try {
Scanner sc = new Scanner(new File(fileName));
int n = sc.nextInt();
int w = sc.nextInt();
FloatingRect[] rects = new FloatingRect[n];
for (int i=0;i<n;++i) {
rects[i] = FloatingRect.create(sc.nextInt(),sc.nextInt());
}
return new TestCase(rects, w);
} catch (IOException e) {
System.out.println("Error while reading case.");
return null;
}
}
public static TestCase getCaseGen(String outputFile, int n, int w) {
try {
CaseGen.generateCase(outputFile, n, w);
return readFromFile(outputFile);
} catch (IOException e) {
System.out.println("Error while generating case.");
return null;
}
}
@Override
public void start(Stage primaryStage) {
boolean snapshotsOn = true;
primaryStage.setTitle("");
root = new Group();
Scene scene = new Scene(root, resX, resY, Color.WHITE);
TestCase testCase = getCaseGen("hahahahah.txt", 20, 900);
//TestCase testCase = readFromFile("hahahahah.txt");
//TestCase testCase = readFromFile("evil.txt");
//TestCase testCase = readStdinTestCase();
//TestCase testCase = getTestCase();
//TestCase testCase = getCaseGen("test.txt", 5, 40);
//TestCase testCase = readFromFile("test.txt");
//StripPacking sp = new StripPacking(testCase.floatingRects, testCase.width);
//StripPacking sp = new FirstFitDecreasingHeight(testCase.floatingRects, testCase.width);
//StripPacking sp = new SplitFit(testCase.floatingRects, testCase.width);
StripPacking sp = new BruteForce(testCase.floatingRects, testCase.width);
if (snapshotsOn) {
sp.setSnapshotFunction((array, arrayList, height) -> {
nSnapshotCalls++;
if (nSnapshotCalls < snapshotStartPoint) return;
if (snapshots.size() >= MAX_SNAPSHOTS) return;
if (array == null) snapshots.add(arrayList.toArray(new Rect[arrayList.size()]));
else snapshots.add(Arrays.copyOf(array, array.length));
snapshotHeights.add(height);
});
} else {
sp.setSnapshotFunction((array, arrayList, height) -> {nSnapshotCalls++;});
}
sp.execute();
if (!sp.validate()) throw new UnsupportedOperationException("INVALID PACKING");
System.out.println("Height: " + sp.height);
redraw(sp.rects, sp.width, sp.height);
primaryStage.setScene(scene);
primaryStage.show();
if (snapshotsOn) {
System.out.println("Total Snapshot Calls: " + nSnapshotCalls);
scene.setOnKeyPressed(event -> {
switch(event.getCode()) {
case A:
case LEFT:
currentSnapshot--;
if (currentSnapshot < 0) currentSnapshot = snapshots.size()-1;
break;
case D:
case RIGHT:
currentSnapshot++;
if (currentSnapshot >= snapshots.size()) currentSnapshot = 0;
break;
case E:
currentSnapshot+=500;
if (currentSnapshot >= snapshots.size()) currentSnapshot = snapshots.size() - 1;
break;
case Q:
currentSnapshot-=500;
if (currentSnapshot < 0) currentSnapshot = 0;
break;
}
System.out.println(currentSnapshot + ": SNAPSHOT with height " + snapshotHeights.get(currentSnapshot));
redraw(snapshots.get(currentSnapshot), testCase.width, snapshotHeights.get(currentSnapshot));
});
} else {
System.out.println("Snapshot Calls: " + nSnapshotCalls);
}
}
public void redraw(Rect[] rects, int width, int height) {
redraw(Arrays.asList(rects), width, height);
}
public void redraw(Iterable<Rect> rects, int width, int height) {
clearRectangles();
float scale = Math.min((float)resX/width, (float)resY/height);
drawRectangle(width*scale,0,resX,resY,1, Color.BLACK);
drawRectangle(0,height*scale,resX,resY,1, Color.BLACK);
for (Rect rect : rects) {
if (rect != null) drawRectangle(rect, scale);
//else System.out.println("Null Rect Found");
}
}
private void clearRectangles() {
for (Rectangle r : existingRectangles) {
root.getChildren().remove(r);
}
existingRectangles.clear();
}
private void addRectangle(Rectangle r) {
existingRectangles.add(r);
root.getChildren().add(r);
}
public void drawRectangle(float x1,float y1, float x2, float y2, float scale, Color color) {
Rectangle r = new Rectangle();
r.setFill(color);
x1 *= scale; x2 *= scale; y1 *= scale; y2 *= scale;
float temp = y2;
y2 = resY-y1;
y1 = resY-temp;
r.setX(x1);
r.setY(y1);
r.setWidth(x2-x1);
r.setHeight(y2-y1);
addRectangle(r);
}
public void drawRectangle(Rect rect, float scale) {
drawRectangle(rect.x1,rect.y1,rect.x2,rect.y2, scale, Color.DARKGREEN);
float borderWidth = 0.05f*Math.min(rect.y2-rect.y1, rect.x2-rect.x1);
float x1s = rect.x1 + borderWidth;
float y1s = rect.y1 + borderWidth;
float x2s = rect.x2 - borderWidth;
float y2s = rect.y2 - borderWidth;
drawRectangle(x1s, y1s, x2s, y2s, scale, Color.GREEN);
}
}