-
Notifications
You must be signed in to change notification settings - Fork 1
/
Simulation.java
607 lines (538 loc) · 20.9 KB
/
Simulation.java
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
package tue.algorithms.viewer;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.Preferences;
import javax.swing.JFileChooser;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_LINES;
import static org.lwjgl.opengl.GL11.GL_POLYGON;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glLineWidth;
import static org.lwjgl.opengl.GL11.glPopMatrix;
import static org.lwjgl.opengl.GL11.glPushMatrix;
import static org.lwjgl.opengl.GL11.glScalef;
import static org.lwjgl.opengl.GL11.glTranslatef;
import static org.lwjgl.opengl.GL11.glVertex2f;
import static org.lwjgl.opengl.GL11.glVertex3f;
import tue.algorithms.implementation.general.MultipleImplementation;
import tue.algorithms.implementation.general.NetworkImplementation;
import tue.algorithms.implementation.general.ProblemType;
import tue.algorithms.implementation.general.SingleImplementation;
import tue.algorithms.other.Debug;
import tue.algorithms.other.Pair;
import tue.algorithms.test.FakeInputReader;
import tue.algorithms.utility.Node;
import tue.algorithms.utility.Segment;
public class Simulation {
final private static float ERASER_RADIUS = 0.025f;
final private static float ERASER_RADIUS_NETWORK = 0.008f;
final private static float NODE_RADIUS = 0.0054f;
/**
* Get an instance of the class that is chosen to provide input.
*
* @return An instance of a class that extends FakeInputReader.
*/
public static FakeInputReader getFakeInputReader() {
return SimulationSettings.getFakeInputReader();
}
/**
* Get an instance of the class that is chosen to solve single-curve problem
* cases.
*
* @return An instance of a class that extends SingleImplementation.
*/
public static SingleImplementation getSingleImplementation() {
return SimulationSettings.getSingleImplementation();
}
/**
* Get an instance of the class that is chosen to solve multiple-curve
* problem cases.
*
* @return An instance of a class that extends MultipleImplementation.
*/
public static MultipleImplementation getMultipleImplementation() {
return SimulationSettings.getMultipleImplementation();
}
/**
* Get an instance of the class that is chosen to solve network problem
* cases.
*
* @return An instance of a class that extends NetworkImplementation.
*/
public static NetworkImplementation getNetworkImplementation() {
return SimulationSettings.getNetworkImplementation();
}
// Problem type
public ProblemType problemType;
// Input
private Node[] inputNodes;
private Segment[] calculatedSegments;
private Node[] newNetworkNodes;
private FakeInputReader fakeInputReader;
private Pair<ProblemType, Node[]> input;
// Nodes
private ArrayList<Node> nodes;
private ArrayList<Node> networkNodes;
private ArrayList<Segment> segments;
// Keypress memory
private boolean addButtonDown;
private boolean clearKeyDown;
private boolean runKeyDown;
private boolean oneKeyDown;
private boolean twoKeyDown;
private boolean threeKeyDown;
private boolean saveKeyDown;
private boolean tikzKeyDown;
private boolean openKeyDown;
private boolean escKeyDown;
private boolean flipKeyDown;
private boolean helpKeyDown;
// File open
public Preferences prefs;
// Clear
private boolean showSegments;
private boolean brushMode;
private float eraserRadius;
// Constructor
public Simulation() {
addButtonDown = false;
runKeyDown = false;
clearKeyDown = false;
showSegments = true;
saveKeyDown = false;
tikzKeyDown = false;
openKeyDown = false;
oneKeyDown = false;
twoKeyDown = false;
threeKeyDown = false;
brushMode = false;
escKeyDown = false;
flipKeyDown = false;
helpKeyDown = false;
prefs = Preferences.userRoot().node(this.getClass().getName());
prefs.put("file", "none");
eraserRadius = ERASER_RADIUS;
networkNodes = new ArrayList<>();
segments = new ArrayList<>();
}
public void initialize() {
// Read the input
fakeInputReader = getFakeInputReader();
input = fakeInputReader.readInput();
problemType = /*input.first();*/ SimulationSettings.getInitialProblemType();
inputNodes = input.second();
// Convert to arraylists
this.nodes = new ArrayList<>();
for (Node node : inputNodes) {
this.nodes.add(node);
}
}
public Node getMousePosition() {
float clickX = (float) Mouse.getX() / Camera.width * 1.0f / Camera.SCALINGFACTOR - Camera.OFFSETFACTOR;
float clickY = (float) Mouse.getY() / Camera.heigth * 1.0f / Camera.SCALINGFACTOR - Camera.OFFSETFACTOR;
if (Camera.flipped) {
clickY = 1f - clickY;
}
return new Node(Node.FAKE_NODE_ID, clickX, clickY);
}
public void getInput() throws IOException {
while (Mouse.next()) {
if (Mouse.getEventButton() > -1) {
// Add
if (Mouse.getEventButton() == 0) {
addButtonDown = Mouse.getEventButtonState() && !addButtonDown;
}
// Erase
if (Mouse.getEventButton() == 1) {
brushMode = Mouse.getEventButtonState();
}
}
}
while (Keyboard.next()) {
// Run
if (Keyboard.getEventKey() == Keyboard.KEY_R) {
runKeyDown = !runKeyDown && Keyboard.getEventKeyState();
}
// Clear
if (Keyboard.getEventKey() == Keyboard.KEY_C) {
clearKeyDown = !clearKeyDown && Keyboard.getEventKeyState();
}
// Flip
if (Keyboard.getEventKey() == Keyboard.KEY_F) {
flipKeyDown = !flipKeyDown && Keyboard.getEventKeyState();
}
// Help
if (Keyboard.getEventKey() == Keyboard.KEY_F1) {
helpKeyDown = !helpKeyDown && Keyboard.getEventKeyState();
}
// Save
if (Keyboard.getEventKey() == Keyboard.KEY_S) {
saveKeyDown = !saveKeyDown && Keyboard.getEventKeyState();
}
// Save as tikz
if (Keyboard.getEventKey() == Keyboard.KEY_T) {
tikzKeyDown = !tikzKeyDown && Keyboard.getEventKeyState();
}
// Open
if (Keyboard.getEventKey() == Keyboard.KEY_O) {
openKeyDown = !openKeyDown && Keyboard.getEventKeyState();
}
// Type
if (Keyboard.getEventKey() == Keyboard.KEY_1) {
oneKeyDown = Keyboard.getEventKeyState();
}
if (Keyboard.getEventKey() == Keyboard.KEY_2) {
twoKeyDown = Keyboard.getEventKeyState();
}
if (Keyboard.getEventKey() == Keyboard.KEY_3) {
threeKeyDown = Keyboard.getEventKeyState();
}
// Close
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
escKeyDown = true;
}
}
}
public KeyboardValue processInput() throws FileNotFoundException{
if (addButtonDown){
addNode();
addButtonDown = false;
}
if (brushMode) {
deleteNodes();
}
if (runKeyDown) {
calculateSegments();
showSegments = true;
runKeyDown = false;
}
if (clearKeyDown){
showSegments = false;
clearKeyDown = false;
}
if (saveKeyDown){
save();
saveKeyDown = false;
}
if (tikzKeyDown) {
saveAsTikzPicture();
tikzKeyDown = false;
}
if (openKeyDown){
open();
openKeyDown = false;
}
if(oneKeyDown){
problemType = ProblemType.SINGLE;
oneKeyDown = false;
}
if(twoKeyDown){
problemType = ProblemType.MULTIPLE;
twoKeyDown = false;
}
if(threeKeyDown){
problemType = ProblemType.NETWORK;
threeKeyDown = false;
}
// Returning values
if(escKeyDown){
return KeyboardValue.CLOSE;
}
if(flipKeyDown){
flipKeyDown = false;
return KeyboardValue.FLIPSCREEN;
}
if(helpKeyDown){
helpKeyDown = false;
return KeyboardValue.HELP;
}
return KeyboardValue.CONTINUE;
}
private void calculateSegments() {
Node[] allNodes = nodes.toArray(new Node[nodes.size()]);
if (problemType == ProblemType.SINGLE) {
SingleImplementation singleImplementation = getSingleImplementation();
Debug.log("Running single implementation...");
Debug.log("Name: '" + singleImplementation.getClass().getCanonicalName() + "'");
Debug.log("Input size: " + allNodes.length);
long startTime = System.nanoTime();
calculatedSegments = singleImplementation.getOutput(allNodes);
Debug.log("Time taken (millis): " + (System.nanoTime()-startTime)/1000000);
System.out.println("-----");
} else if (problemType == ProblemType.MULTIPLE) {
MultipleImplementation multipleImplementation = getMultipleImplementation();
Debug.log("Running multiple implementation...");
Debug.log("Name: '" + multipleImplementation.getClass().getCanonicalName() + "'");
Debug.log("Input size: " + allNodes.length);
long startTime = System.nanoTime();
calculatedSegments = multipleImplementation.getOutput(allNodes);
Debug.log("Time taken (millis): " + (System.nanoTime()-startTime)/1000000);
System.out.println("-----");
} else if (problemType == ProblemType.NETWORK) {
networkNodes.clear();
NetworkImplementation networkImplementation = getNetworkImplementation();
Debug.log("Running network implementation...");
Debug.log("Name: '" + networkImplementation.getClass().getCanonicalName() + "'");
Debug.log("Input size: " + allNodes.length);
long startTime = System.nanoTime();
Pair<Segment[], Node[]> output = networkImplementation.getOutput(allNodes);
Debug.log("Time taken (millis): " + (System.nanoTime()-startTime)/1000000);
System.out.println("-----");
calculatedSegments = output.first();
newNetworkNodes = output.second();
for (Node node : newNetworkNodes) {
networkNodes.add(node);
}
}
this.segments = new ArrayList<>(calculatedSegments.length);
for (Segment segment : calculatedSegments) {
this.segments.add(segment);
}
}
private void deleteNodes() {
if (problemType.equals(ProblemType.NETWORK)) {
eraserRadius = ERASER_RADIUS_NETWORK;
} else{
eraserRadius = ERASER_RADIUS;
}
float clickX = getMousePosition().getX();
float clickY = getMousePosition().getY();
Node mouseNode = new Node(Node.FAKE_NODE_ID, clickX, clickY);
for (Node node : nodes) {
if (node.getDistanceTo(mouseNode) < eraserRadius) {
// Found a node under the eraser, reconstruct the whole set of nodes.
ArrayList<Node> oldNodes = nodes;
nodes = new ArrayList<Node>(nodes.size());
for (Node n : oldNodes) {
if (n.getDistanceTo(mouseNode) >= eraserRadius) {
nodes.add(new Node(n.x, n.y));
}
}
break;
}
}
}
private void addNode() {
float clickX = getMousePosition().getX();
float clickY = getMousePosition().getY();
if (clickX >= 0 && clickX <= 1 && clickY >= 0 && clickY <= 1) {
boolean exists = false;
for (Node node : nodes) {
if (node.getX()==clickX && node.getY()==clickY){
exists = true;
break;
}
}
if (!exists) {
ArrayList<Node> oldNodes = nodes;
nodes = new ArrayList<Node>();
for (Node node : oldNodes) {
nodes.add(new Node(node.x, node.y));
}
nodes.add(new Node(clickX, clickY));
}
}
}
private void buildFile(File file){
try (PrintStream fileStream = new PrintStream(file);) {
fileStream.print("reconstruct ");
if (problemType.equals(ProblemType.SINGLE)){
fileStream.println("single");
} else if (problemType.equals(ProblemType.MULTIPLE)){
fileStream.println("multiple");
} else {
fileStream.println("network");
}
fileStream.println(nodes.size() + " number of sample points");
for (Node node : nodes) {
fileStream.println(node.getId() + " " + node.getX() + " " + node.getY());
}
fileStream.flush();
} catch (IOException ex) {
Logger.getLogger(Simulation.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void buildTikzPicture(PrintStream os) {
// How to incliude them? Pick whatever you prefer:
// http://tex.stackexchange.com/questions/79594/outsourcing-tikz-code
// os.println("\\documentclass{standalone}");
// os.println("\\usepackage{tikz}");
// os.println("\\begin{document}");
os.println("%% Include the picture as follows: \\");
os.println("% \\begingroup");
os.println("% \\tikzset{every picture/.style={scale=1,radius=0.05,line width=0.4}}%");
os.println("% \\input{tikz/[filename].tex}%");
os.println("% \\endgroup");
os.println("%");
os.println("%% Want to add a label? Use node[<position>]{label},");
os.println("%% where <position> is one of: below / above / left / right");
os.println("% \\fill (0,0) circle node[below]{$p_1$};");
os.println("%% Want to give the point a different color?");
os.println("% \\fill[red] (0,0) circle;");
os.println("%");
os.println("\\begin{tikzpicture}");
for (Segment seg : segments) {
os.printf("\\draw (%.4f,%.4f) -- (%.4f,%.4f);\n",
tikzX(seg.node1), tikzY(seg.node1),
tikzX(seg.node2), tikzY(seg.node2));
}
for (Node node : nodes) {
os.printf("\\fill (%.4f,%.4f) circle;\n", tikzX(node), tikzY(node));
}
os.println("\\end{tikzpicture}");
// os.println("\\end{document}")
}
// Helper functions, float formatted as int (multiplier to be kept in sync with printf flag).
// Assuming that most if not all coordinates are [0,1)
private float tikzX(Node node) {
return node.x * 10f;
}
private float tikzY(Node node) {
float y = node.y;
if (Camera.flipped) {
y = 1f - y;
}
return y * 10f;
}
private void save() {
JFileChooser saveFile = new JFileChooser(prefs.get("loc", null));
saveFile.showSaveDialog(null);
if (saveFile.getSelectedFile() != null) {
File file = saveFile.getSelectedFile();
try {
file.createNewFile();
buildFile(file);
prefs.put("loc", file.getParent());
} catch (IOException ex) {
Logger.getLogger(Simulation.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void saveAsTikzPicture() {
JFileChooser exportFile = new JFileChooser(prefs.get("exportLoc", prefs.get("loc", null)));
exportFile.showSaveDialog(null);
if (exportFile.getSelectedFile() != null) {
File file = exportFile.getSelectedFile();
try {
prefs.put("exportLoc", file.getParent());
file.createNewFile();
try (PrintStream fileStream = new PrintStream(file)) {
buildTikzPicture(fileStream);
} catch (IOException e) {
throw e;
}
} catch (IOException ex) {
Logger.getLogger(Simulation.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private void open() throws FileNotFoundException {
JFileChooser openFile = new JFileChooser(prefs.get("loc", null));
boolean done = false;
while (!done) {
done = true;
if (openFile.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File file = openFile.getSelectedFile();
try {
prefs.put("loc", file.getParent());
prefs.put("file", file.getName());
Scanner scanner = new Scanner(file);
String line = scanner.nextLine();
ProblemType pType = ProblemType.valueOf(line.substring(12).toUpperCase());
line = scanner.nextLine();
int numberOfNodes = Integer.parseInt(line.substring(0, line.indexOf(' ')));
nodes.clear();
for (int i = 0; i < numberOfNodes; i++) {
String a = scanner.next();
String b = scanner.next();
String c = scanner.next();
nodes.add(new Node(Integer.parseInt(a),
Float.parseFloat(b),
Float.parseFloat(c)));
}
problemType = pType;
showSegments = false;
} catch (Exception e) {
done = false;
}
}
}
}
public void render() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0f, 0f, 0f);
glLineWidth(2f);
if (showSegments) {
for (Segment segment : segments) {
drawSegment(segment);
}
}
glColor3f(0f, 0f, 0f);
for (Node node : nodes) {
drawPoint(NODE_RADIUS, node, true);
}
if (problemType.equals(ProblemType.NETWORK)) {
glColor3f(1f, 0f, 0f);
for (Node node : networkNodes) {
drawPoint(NODE_RADIUS, node, true);
}
}
glColor3f(1f, 0f, 0f);
if (brushMode){
drawPoint(eraserRadius, new Node(Node.FAKE_NODE_ID, getMousePosition().getX(), getMousePosition().getY()), false);
}
}
private void drawSegment(Segment segment) {
glBegin(GL_LINES);
glVertex3f(segment.getX1(), segment.getY1(), 0);
glVertex3f(segment.getX2(), segment.getY2(), 0);
glEnd();
}
private void drawPoint(float radius, Node n, boolean scale) {
float ratio = ((float) Camera.width) / Camera.heigth;
glPushMatrix();
glTranslatef(n.getX(), n.getY(), 0);
float pointSize = Math.min(Camera.width, Camera.heigth);
glScalef(640 / pointSize, 640 / pointSize, 1);
if(scale){
if (ratio > 1f) {
glScalef(1f / ratio, 1f, 1);
} else if (ratio < 1f) {
glScalef(1f, 1f / ratio, 1);
}
}
drawCircle(radius, 18);
glPopMatrix();
}
// source: http://slabode.exofire.net/circle_draw.shtml
private void drawCircle(float r, int num_segments) {
final float theta = 2f * 3.1415926f / (float) num_segments;
final float c = (float) cos(theta);
final float s = (float) sin(theta);
float t;
float x = r;// we start at angle = 0
float y = 0;
glBegin(GL_POLYGON);
for (int ii = 0; ii < num_segments; ii++) {
glVertex2f(x, y);// output vertex
// apply the rotation matrix
t = x;
x = c * x - s * y;
y = s * t + c * y;
}
glEnd();
}
}