-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSVGConverter.java
442 lines (369 loc) · 17.6 KB
/
SVGConverter.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
package com.wtv.converter;
import com.wtv.processing.ColorGenerator;
import com.wtv.processing.Curves;
import com.wtv.processing.Rounder;
import com.wtv.structures.Knoten;
import com.wtv.structures.PathNode;
import com.wtv.structures.LineSegment;
import com.wtv.structures.Spot;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class SVGConverter {
/**
* Determines the thickness of the lines in the written SVGs <br>
* Note: the thickness is also affected by {@link #scale}
*/
public static double thickness = 0.05;
/**
* causes the lines to be curved with bezier curves. <br>
* This only affects {@link #writeFileFromPath(Map, String, int, int)}
*/
public static boolean curvedMode = false;
/**
* Causes individual paths to be visualized by differing thickness <br>
* This only affects {@link #writeFileFromPath(Map, String, int, int)}
*/
public static boolean visualizePaths = false;
/**
* This can be used to up/downscale the result.
* Its 10 by default because the given examples can be pretty small otherwise.
*/
public static double scale = 10.0;
/**
* This object is used for all Rounding Operations
*/
public static Rounder rounder = new Rounder();
public static void main(String[] args) throws Exception{
SVGCreate(args[0]);
}
public static void SVGCreate(String fileName) throws Exception{
double [][] d = CSVReader.dateiLesen2D(fileName, ",");
List<LineSegment> segments = new ArrayList<>();
for(int i = 0; i < 3; i++){
segments.add(new LineSegment(d[i][0],d[i][1],d[i][2],d[i][3]));
}
String s = createFile(fileName);
writeFileTriangle(segments, s);
}
public static void SVGCreateIsoFromSegments(Map<Double, List<LineSegment>> segments,
String filePath, int width, int height){
String s = createFile(filePath);
writeFileFromSegments(segments, s, width, height);
}
public static void SVGCreateIsoFromPathNodes(Map<Double, List<PathNode>> linePaths,
String filePath, int width, int height) {
String s = createFile(filePath);
writeFileFromPathNodes(linePaths, s, width, height);
}
public static void SVGCreateIsoFromPath(Map<Double, List<Knoten>> linePaths,
String filePath, int width, int height) {
String s = createFile(filePath);
writeFileFromPath(linePaths, s, width, height);
}
private static String createFile(String filePath){
filePath = filePath.split("\\.")[0];
String newFilePath;
newFilePath = filePath + ".svg";
try {
File file = new File(newFilePath);
int x = 0;
//Files überschreiben:
//noinspection ResultOfMethodCallIgnored
file.createNewFile();
//Files nicht Überschreiben:
/*
while(!file.createNewFile()){
System.out.println("File " + x + " already exists.");
x++;
newFilePath = filePath + "(" + x + ").svg";
file = new File(newFilePath);
}
System.out.println("File created: " + file.getName());
*/
} catch (IOException e) {
System.err.println("An error occurred.");
e.printStackTrace();
}
return newFilePath;
}
private static void writeFileTriangle(List<LineSegment> s, String filePath){
try {
FileWriter myWriter = new FileWriter(filePath, false);
String file = "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" x=\"7\" y=\"7\" width=\"300\" height=\"300\"> \n" +
"<polygon \n" +
" points=\"" + s.get(0).p1x+","+s.get(0).p1y + " " + s.get(1).p1x+","+s.get(1).p1y + " " + s.get(2).p1x+","+s.get(2).p1y + "\"" +
" stroke=\"black\"\n" +
" stroke-width=\"3\"\n" +
" fill=\"none\" />\n" +
"</svg>";
myWriter.write(file);
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.err.println("An error occurred.");
e.printStackTrace();
}
}
private static void writeFileFromSegments(Map<Double, List<LineSegment>> segments,
String filePath, int width, int height){
try {
double scale = 1; //can be used to scale the output map
StringBuilder stringBuilder = new StringBuilder("<svg xmlns=\"http://www.w3.org/2000/svg\" ")
.append("version=\"1.1\" x=\"1\" y=\"1\" ")
.append("width=\"").append(width*scale)
.append("\" height=\"").append(height*scale)
.append("\"> \n");
/*
The whole thing is pretty ineffective: It starts with a height array, from wich it generates an unsorted Map,
from wich it gets an unsorted Set of heights, which is converted to an array, then sorted.
I can't be bothered to change that thought.
*/
Set<Double> h = segments.keySet();
Double[] heights = new Double[h.size()];
h.toArray(heights);
Arrays.sort(heights);
int i = 0;
for(Double d: heights) {
String color = ColorGenerator.arrayToColorString(ColorGenerator.lineColor(segments.size(), i));
i++;
for (LineSegment lS : segments.get(d)) {
stringBuilder
.append("<path d=\"M ")
.append(rounder.round(lS.p1x)*scale).append(",").append(rounder.round(lS.p1y)*scale)
.append(" l ").append(rounder.round(lS.p2x - lS.p1x)*scale)
.append(",").append(rounder.round(lS.p2y - lS.p1y)*scale)
.append("\" stroke=\"").append(color)
.append("\" stroke-width=\"").append(thickness*scale).append("\" /> \n");
}
}
stringBuilder.append("</svg>");
String file = stringBuilder.toString();
FileWriter myWriter = new FileWriter(filePath, false);
myWriter.write(file);
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.err.println("An error occurred.");
e.printStackTrace();
}
}
private static void writeFileFromPathNodes(Map<Double, List<PathNode>> linePaths,
String filePath, int width, int height){
try {
double scale = 1; //can be used to scale the output map
StringBuilder stringBuilder = new StringBuilder("<svg xmlns=\"http://www.w3.org/2000/svg\" ")
.append("version=\"1.1\" x=\"1\" y=\"1\" ")
.append("width=\"").append(width*scale)
.append("\" height=\"").append(height*scale)
.append("\"> \n");
/*
The whole thing is pretty ineffective: It starts with a height array, from wich it generates an unsorted Map,
from wich it gets an unsorted Set of heights, which is converted to an array, then sorted.
I can't be bothered to change that thought.
*/
Set<Double> h = linePaths.keySet();
Double[] heights = new Double[h.size()];
h.toArray(heights);
Arrays.sort(heights);
int i = 0;
int p = 0;
for(Double d: heights) { // iterate through each line height
String color = ColorGenerator.arrayToColorString(ColorGenerator.lineColor(linePaths.size(), i));
i++;
stringBuilder.append("<path d=\"");
p++;
for (PathNode path : linePaths.get(d)) { //iterate trough each path
Stack<PathNode> missed = new Stack<>();
missed.push(path);
while(!missed.empty()) { //iterate trough each branch
PathNode currentElement = missed.pop();
stringBuilder
.append("M ")
.append(rounder.roundToString(currentElement.getX() * scale))
.append(",")
.append(rounder.roundToString(currentElement.getY() * scale))
.append(" ");
while (currentElement.getFollowing().size()>0) { //iterate trough each element
PathNode nextNode = currentElement.getFollowing().iterator().next();
if(currentElement.getFollowing().size()>1) {
Set<PathNode> newFollowing = currentElement.getFollowing();
newFollowing.remove(nextNode);
currentElement.setFollowing(newFollowing);
missed.push(currentElement);
}
currentElement = nextNode;
stringBuilder
.append("L ")
.append(rounder.roundToString(currentElement.getX() * scale))
.append(",")
.append(rounder.roundToString(currentElement.getY() * scale))
.append(" ");
}
}
}
stringBuilder
.append("\" stroke=\"").append(color)
.append("\" stroke-width=\"").append(rounder.roundToString(thickness)).append("\" fill=\"none\" /> \n");
}
stringBuilder.append("</svg>");
String file = stringBuilder.toString();
FileWriter myWriter = new FileWriter(filePath, false);
myWriter.write(file);
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.err.println("An error occurred.");
e.printStackTrace();
}
}
private static void writeFileFromPath(Map<Double, List<Knoten>> isoLines,
String filePath, int width, int height){
try {
StringBuilder stringBuilder = new StringBuilder("<svg xmlns=\"http://www.w3.org/2000/svg\" ")
.append("version=\"1.1\" x=\"1\" y=\"1\" ")
.append("width=\"").append(width*scale)
.append("\" height=\"").append(height*scale)
.append("\"> \n");
Set<Double> h = isoLines.keySet();
Double[] heights = new Double[h.size()];
h.toArray(heights);
Arrays.sort(heights);
int i = 0;
int p = 0;
for(Double d: heights) { // iterate through each line height
double thickness = SVGConverter.thickness;
String color = ColorGenerator.arrayToColorString(ColorGenerator.lineColor(isoLines.size(), i));
i++;
//set of all paths
Set<LinkedList<Spot>> paths = new HashSet<>();
for (Knoten k : isoLines.get(d)) {
paths.addAll(k.getPaths());
}
for (LinkedList<Spot> spots: paths) {
stringBuilder.append("\t<path d=\"");
p++;
Spot currentElement;
if(!curvedMode || spots.size() <= 2) {
currentElement = spots.pop();
stringBuilder
.append("M ")
.append(rounder.roundToString(currentElement.getX() * scale))
.append(",")
.append(rounder.roundToString(currentElement.getY() * scale))
.append(" ");
while (!spots.isEmpty()) {
currentElement = spots.pop();
stringBuilder
.append("L ")
.append(rounder.roundToString(currentElement.getX() * scale))
.append(",")
.append(rounder.roundToString(currentElement.getY() * scale))
.append(" ");
}
}
else {
spots = Curves.getControlPoints(spots);
currentElement = spots.pop();
stringBuilder
.append("M ")
.append(rounder.roundToString(currentElement.getX() * scale))
.append(",")
.append(rounder.roundToString(currentElement.getY() * scale))
.append(" ");
while (!spots.isEmpty()) {
currentElement = spots.pop();
stringBuilder
.append("C ")
.append(rounder.roundToString(currentElement.getX() * scale)).append(" ")
.append(rounder.roundToString(currentElement.getY() * scale)).append(", ");
currentElement = spots.pop();
stringBuilder
.append(rounder.roundToString(currentElement.getX() * scale)).append(" ")
.append(rounder.roundToString(currentElement.getY() * scale)).append(", ");
currentElement = spots.pop();
stringBuilder
.append(rounder.roundToString(currentElement.getX() * scale)).append(" ")
.append(rounder.roundToString(currentElement.getY() * scale)).append(" ");
}
}
stringBuilder
.append("\" stroke=\"").append(color)
.append("\" stroke-width=\"").append(rounder.roundToString(scale* thickness)).append("\" fill=\"none\" /> \n");
if (visualizePaths) {
thickness = thickness + 0.02;
if (thickness >= SVGConverter.thickness+0.1) {
thickness-=0.1;
}
}
}
}
stringBuilder.append("</svg>");
String file = stringBuilder.toString();
FileWriter myWriter = new FileWriter(filePath, false);
myWriter.write(file);
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.err.println("An error occurred.");
e.printStackTrace();
}
}
public static double getThickness() {
return thickness;
}
public static void setThickness(double thickness) {
SVGConverter.thickness = thickness;
}
public static boolean isCurvedMode() {
return curvedMode;
}
public static void setCurvedMode(boolean curvedMode) {
SVGConverter.curvedMode = curvedMode;
}
public static boolean isVisualizePaths() {
return visualizePaths;
}
public static void setVisualizePaths(boolean visualizePaths) {
SVGConverter.visualizePaths = visualizePaths;
}
public static double getScale() {
return scale;
}
public static void setScale(double scale) {
SVGConverter.scale = scale;
}
private static void writeFileLines2(List<LineSegment> segments,
String filePath, int width, int height){
try {
StringBuilder stringBuilder = new StringBuilder("<svg xmlns=\"http://www.w3.org/2000/svg\" ")
.append("version=\"1.1\" x=\"1\" y=\"1\" ")
.append("width=\"").append(width)
.append("\" height=\"").append(height)
.append("\"> \n");
for (LineSegment lS: segments){
stringBuilder
.append("<line ")
.append("x1=\"").append(lS.p1x).append("\" y1=\"").append(lS.p1y).append("\" ")
.append("x2=\"").append(lS.p2x).append("\" y2=\"").append(lS.p2y).append("\" ")
.append("stroke=\"black\" stroke-width=\"1\"/> \n");
}
stringBuilder.append("</svg>");
String file = stringBuilder.toString();
FileWriter myWriter = new FileWriter(filePath, false);
myWriter.write(file);
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.err.println("An error occurred.");
e.printStackTrace();
}
}
public static Rounder getRounder() {
return rounder;
}
public static void setRounder(Rounder rounder) {
SVGConverter.rounder = rounder;
}
}