-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIsolinien.java
411 lines (340 loc) · 15.3 KB
/
Isolinien.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
package com.wtv.processing;
import com.wtv.converter.CSVReader;
import com.wtv.converter.ObjConverter;
import com.wtv.converter.SVGConverter;
import com.wtv.structures.*;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
public class Isolinien {
public static Rounder rounder = new Rounder();
public static void main(String[] args) throws Exception {
int anzLines = 15;
int scale = 1;
List<String> x = new ArrayList<>();
x.add("map01_res3.csv");
x.add("hill_res50.csv");
x.add("map03.csv");
x.add("saddle_res50.csv");
x.add("tilt_D_res50.csv");
x.add("srtm_de_il.csv");
x.add("test.csv");
for(String s : x){
s = "examples/" + s;
printIsoByPathNodes(s,",", anzLines);
ObjConverter.objCreateTriangles(triangles(scale(CSVReader.dateiLesen2D(s, ","), scale)), s);
}
/*
String file = "examples/map01_res3.csv";
double[][] d = CSVReader.dateiLesenDyn(file, ",");
ObjConverter.objCreateTriangles(triangles(d), file);
printIsoByPath(file, ",", 5);
*/
}
//Via LineSegments
public static void printIso(String file, String spaltentrenner, int amount) throws IOException{
double[][] d = CSVReader.dateiLesen2D(file, spaltentrenner);
Map<Double, List<LineSegment>> segments = getIsolinesForArray(d, amount);
SVGConverter.SVGCreateIsoFromSegments(segments, file, d.length-1, d[0].length -1);
}
public static Map<Double, List<LineSegment>> getIsolinesForArray(double[][] values, int amount){
double min = min(values);
double max = max(values);
double[] heights = lineHeights(amount, min, max);
Map<Double, List<LineSegment>> result = new HashMap<>();
List<Triangle> triangles = triangles(values);
for(double h: heights) {
result.put(h, getIsoLine(triangles,h));
}
return result;
}
//Via PathNodes
public static void printIsoByPathNodes(String file, String spaltentrenner, int amount) throws Exception {
double[][] d = CSVReader.dateiLesen2D(file, spaltentrenner);
Map<Double, List<PathNode>> paths = getPathNodesOfAllIsolines(d, amount);
SVGConverter.SVGCreateIsoFromPathNodes(paths, file, d.length-1, d[0].length -1);
}
public static Map<Double, List<PathNode>> getPathNodesOfAllIsolines(double[][] values, int amount){
double min = min(values);
double max = max(values);
double[] heights = lineHeights(amount, min, max);
List<Triangle> triangles = triangles(values);
Map<Double, List<PathNode>> linePaths = new HashMap<>();
for(double h: heights){
linePaths.put(h, getPathNodesOfIsoline(triangles, h));
}
return linePaths;
}
public static List<PathNode> getPathNodesOfIsoline(List<Triangle> triangles, Double lineHeight){
List<PathNode> result = new ArrayList<>();
List<LineSegment> segments = getIsoLine(triangles, lineHeight);
LineSegment current;
PathNode node;
while(segments.size() > 0){
current = segments.get(0);
node = new PathNode(current.p1x, current.p1y);
result.add(node);
segments = getPathNodesOfIsolineRecursive(node, segments);
}
return result;
}
private static List<LineSegment> getPathNodesOfIsolineRecursive(PathNode node, List<LineSegment> segments){
Set<PathNode> following;
Set<LineSegment> nextSegments = segments.stream()
.filter(s -> (s.p1x==node.getX() && s.p1y==node.getY()))
.collect(Collectors.toSet());
segments.removeAll(nextSegments);
following = nextSegments.stream()
.map(s -> new PathNode(s.p2x, s.p2y))
.collect(Collectors.toSet());
nextSegments = segments.stream()
.filter(s -> s.p2x==node.getX() && s.p2y==node.getY())
.collect(Collectors.toSet());
segments.removeAll(nextSegments);
following.addAll(nextSegments.stream()
.map(s -> new PathNode(s.p1x, s.p1y))
.collect(Collectors.toSet()));
node.setFollowing(following);
for(PathNode n: following){
segments = getPathNodesOfIsolineRecursive(n, segments);
}
return segments;
}
//Via Path in "Ordered Lists"
// A path/"Pfad" is a LinkedList of Spots
// A node/"Knoten" is a List of paths
// A path always has a node at each end
public static void printIsoByPath(String file, String spaltentrenner, int amount) throws IOException {
double[][] d = CSVReader.dateiLesen2D(file, spaltentrenner);
d = CSVReader.invertForSvg(d);
Map<Double, List<Knoten>> paths = getIsoLinesAsPaths(d, amount);
SVGConverter.SVGCreateIsoFromPath(paths, file, d.length-1, d[0].length -1);
}
private static Map<Double, List<Knoten>> getIsoLinesAsPaths(double[][] values, int amount) {
Map<Double, List<Knoten>> result = new HashMap<>();
double min = min(values);
double max = max(values);
double[] heights = lineHeights(amount, min, max);
for (double lineHeight: heights) { // iterate through line Heights
List<Knoten> knots = new ArrayList<>();
List<Triangle> triangles = triangles(values);
List<LineSegment> segments = getIsoLine(triangles, lineHeight);
// creates new paths
while (segments.size() > 0) {
LinkedList<Spot> newPath = new LinkedList<>(); // the new Path
LineSegment currSegment = segments.get(0);
newPath.add(new Spot(currSegment.p1x, currSegment.p1y));
newPath.addLast(new Spot(currSegment.p2x, currSegment.p2y));
segments.remove(currSegment);
// build path backwards
while (true) {
Spot currSpot = new Spot(newPath.getFirst().getX(), newPath.getFirst().getY());
// If there is a knot here, thats the end of our path.
LineSegment s = subroutine(knots, currSpot, newPath, segments);
if(s==null) break;
newPath.addFirst(new Spot(s.p2x, s.p2y));
segments.remove(s);
}
// build path forewards
while (true) {
Spot currSpot = new Spot(newPath.getLast().getX(), newPath.getLast().getY());
LineSegment s = subroutine(knots, currSpot, newPath, segments);
if(s==null) break;
newPath.addLast(new Spot(s.p2x, s.p2y));
segments.remove(s);
}
}
result.put(lineHeight, knots);
}
return result;
}
private static LineSegment subroutine(List<Knoten> knots, Spot currSpot, LinkedList<Spot> newPath, List<LineSegment> segments) {
// If there is a knot here, thats the end of our path.
Knoten localKnot = knots.stream().filter(knoten -> knoten.getSpot().equals(currSpot)).findFirst().orElse(null);
if(localKnot != null) {
localKnot.add(newPath);
return null;
}
/* Find all segments that start or end at the first spot
* Also modify them so that they all start there
*/
List<LineSegment> localSegments = segments.stream()
.filter(segment -> (currSpot.getX() == segment.p1x) && currSpot.getY() == segment.p1y)
.collect(Collectors.toList());
localSegments.addAll(
segments.stream()
.filter(segment -> (currSpot.getX() == segment.p2x) && currSpot.getY() == segment.p2y)
.peek(LineSegment::swap)
.collect(Collectors.toList())
);
if (localSegments.size() != 1) {
Knoten k = new Knoten(currSpot);
k.add(newPath);
knots.add(k);
return null;
}
return localSegments.get(0);
}
//General Use
public static double max(double[][] values) throws NoSuchElementException{
//noinspection OptionalGetWithoutIsPresent
return Arrays.stream(values) // -> double[] stream
.map(innerList -> Arrays.stream(innerList).max()) // -> OptionalDouble Stream
.map(OptionalDouble::getAsDouble) // -> Double stream
.max(Double::compareTo) // -> Optional<Double>
.get(); // -> Double
}
public static double min(double[][] values) throws NoSuchElementException{
//noinspection OptionalGetWithoutIsPresent
return Arrays.stream(values) // -> double[] stream
.map(innerList -> Arrays.stream(innerList).min()) // -> OptionalDouble Stream
.map(OptionalDouble::getAsDouble) // -> Double stream
.min(Double::compareTo) // -> Optional<Double>
.get(); // -> Double
}
public static double[] lineHeights(int amount, double min, double max) {
double[] result = new double[amount];
double intervall = max - min;
for(int i = 0; i < amount; i++){
result[i] = min + (intervall/(amount+1)) * (i+1);
}
return result;
}
public static List<Triangle> triangles(double[][] values){
List<Triangle> triangles = new ArrayList<>();
/*
The Idea of the following loop is to go through all squares within the table of values and add the 3
corresponding triangles for each.
The (x,y) is always the top left corner of the square
*/
for(int x = 0; x < values.length - 1; x++){ //iterate through y coordinates
for (int y = 0; y < values[x].length - 1; y++){ //iterate through x coordinates
double midPoint = middlePoint(values,x,y);
triangles.add(
new Triangle(
new Point(x, y, values[x][y]),
new Point(x+1, y, values[x+1][y]),
new Point(x + 0.5,y + 0.5, midPoint)));
triangles.add(
new Triangle(
new Point(x+1, y, values[x+1][y]),
new Point(x+1, y+1, values[x+1][y+1]),
new Point(x + 0.5,y + 0.5, midPoint)));
triangles.add(
new Triangle(
new Point(x+1, y+1, values[x+1][y+1]),
new Point(x, y+1, values[x][y+1]),
new Point(x + 0.5,y + 0.5, midPoint)));
triangles.add(
new Triangle(
new Point(x, y, values[x][y]),
new Point(x, y+1, values[x][y+1]),
new Point(x + 0.5,y + 0.5, midPoint)));
}
}
for (Triangle t: triangles) {
t.a.z = rounder.round(t.a.z);
t.b.z = rounder.round(t.b.z);
t.c.z = rounder.round(t.c.z);
}
return triangles;
}
public static double middlePoint(double[][] values, int x, int y){
return ((values[x][y] + values[x+1][y] + values[x][y+1] + values[x+1][y+1]) / 4.0);
}
public static List<LineSegment> getIsoLine(List<Triangle> triangles, double lineHeight) {
List<LineSegment> result = new ArrayList<>();
for(Triangle t: triangles){
LineSegment s = intersectionOfTriangle(t, lineHeight);
s = rounder.round(s, 13); //workaround for some rounding errors in double values
if(s != null){
//s = rounder.round(s);
if (!result.contains(s)) { // prevent duplicates
result.add(s);
}
}
}
return result;
}
/**
*
* @param a first point
* @param b second point
* @param lineHeight the z value
* @return Returns the point at witch the line between the two points reaches a certain height.
* Returns zero if the intersection is not a point
*/
public static Point intersectionOfLine(Point a, Point b, double lineHeight) {
double factor = (lineHeight - a.z) / (b.z - a.z); //=NaN when dividing by zero
Point p = null;
if(0 <= factor && factor <=1) {
p = new Point(
a.x + (factor * (b.x-a.x)),
a.y + (factor * (b.y-a.y)),
a.z + (factor * (b.z-a.z)));
}
return p;
}
public static double[][] move(double[][] values, double move) {
for(int i = 0; i < values.length; i++) {
for(int j = 0; j < values[i].length; j++) {
values[i][j] = values[i][j] + move;
}
}
return values;
}
public static double[][] scale(double[][] values, double scale) {
for(int i = 0; i < values.length; i++) {
for(int j = 0; j < values[i].length; j++) {
values[i][j] = values[i][j] * scale;
}
}
return values;
}
/*
// I have no idea what this was supposed to be...
public static Pair<Map<String, LineSegment>, Map<String, LineSegment>> getIsoLineMaps(List<Triangle> triangles,
double lineHeight) {
Map<String, LineSegment> pointMap1 = new HashMap<>();
Map<String, LineSegment> pointMap2 = new HashMap<>();
for(Triangle t: triangles){
LineSegment s = intersectionOfTriangle(t, lineHeight);
if(s != null){
pointMap1.put(s.p1x + "," + s.p1y, s);
pointMap1.put(s.p2x + "," + s.p2y, s);
}
}
return new Pair<>(pointMap1, pointMap2);
}
*/
private static LineSegment intersectionOfTriangle(Triangle triangle, double lineHeight) {
List<Point> points = new ArrayList<>();
Point a = intersectionOfLine(triangle.a, triangle.b, lineHeight);
if(a != null) {
points.add(a);
}
a = intersectionOfLine(triangle.b, triangle.c, lineHeight);
if(a != null) {
points.add(a);
}
a = intersectionOfLine(triangle.c, triangle.a, lineHeight);
if(a != null) {
points.add(a);
}
points = new ArrayList<>(new HashSet<>(points));
if(points.size() == 2 &&
(points.get(0).x != points.get(1).x ||
points.get(0).y != points.get(1).y)){
return new LineSegment(
points.get(0).x, points.get(0).y,
points.get(1).x, points.get(1).y);
}
return null;
}
public static Rounder getRounder() {
return rounder;
}
public static void setRounder(Rounder rounder) {
Isolinien.rounder = rounder;
}
}