CityGMLViewer.java 18.2 KB
Newer Older
Matthias Betz's avatar
Matthias Betz committed
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
/*-
 * Copyright 2021 Hochschule für Technik Stuttgart
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.hft.stuttgart.citygml.viewer;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
26
27
28
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Matthias Betz's avatar
Matthias Betz committed
29
30
import java.util.ArrayList;
import java.util.List;
Matthias Betz's avatar
Matthias Betz committed
31
32
33
34
35
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
Matthias Betz's avatar
Matthias Betz committed
36
37
38
39
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
40
import javax.swing.JOptionPane;
Matthias Betz's avatar
Matthias Betz committed
41
import javax.xml.namespace.QName;
Matthias Betz's avatar
Matthias Betz committed
42
43
import javax.xml.parsers.ParserConfigurationException;

Matthias Betz's avatar
Matthias Betz committed
44
45
46
47
48
49
import org.citygml4j.core.model.core.AbstractFeature;
import org.citygml4j.core.util.CityGMLConstants;
import org.citygml4j.xml.CityGMLContext;
import org.citygml4j.xml.CityGMLContextException;
import org.citygml4j.xml.reader.ChunkOptions;
import org.citygml4j.xml.reader.CityGMLChunk;
Matthias Betz's avatar
Matthias Betz committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import org.citygml4j.xml.reader.CityGMLInputFactory;
import org.citygml4j.xml.reader.CityGMLReadException;
import org.citygml4j.xml.reader.CityGMLReader;
import org.lwjgl.PointerBuffer;
import org.lwjgl.glfw.Callbacks;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWMouseButtonCallback;
import org.lwjgl.glfw.GLFWScrollCallback;
import org.lwjgl.glfw.GLFWWindowSizeCallbackI;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import org.lwjgl.opengl.GL30;
64
65
import org.lwjgl.opengl.GLUtil;
import org.lwjgl.system.Callback;
Matthias Betz's avatar
Matthias Betz committed
66
67
68
69
70
71
72
73
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;
import org.lwjgl.util.nfd.NativeFileDialog;
import org.xml.sax.SAXException;

import com.jogamp.opengl.GLException;

import de.hft.stuttgart.citygml.viewer.datastructure.BoundingBox;
Matthias Betz's avatar
Matthias Betz committed
74
import de.hft.stuttgart.citygml.viewer.datastructure.Polygon;
Matthias Betz's avatar
Matthias Betz committed
75
76
import de.hft.stuttgart.citygml.viewer.math.Vector3d;
import de.hft.stuttgart.citygml.viewer.parser.CityGMLParser;
77
import de.hft.stuttgart.citygml.viewer.parser.ColorHandler;
Matthias Betz's avatar
Matthias Betz committed
78
79
80
import de.hft.stuttgart.citygml.viewer.parser.FeatureMapper;
import de.hft.stuttgart.citygml.viewer.parser.ObservedInputStream;
import de.hft.stuttgart.citygml.viewer.parser.ParserConfiguration;
81
import de.hft.stuttgart.citygml.viewer.parser.PolygonColorMapper;
Matthias Betz's avatar
Matthias Betz committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

public class CityGMLViewer {

	private static final Logger log = Logger.getLogger(CityGMLViewer.class.getName());

	private static Camera camera;

	private static boolean mouse1Down = false;
	private static boolean mouse2Down = false;

	private static double x;
	private static double y;

	private static int width = 1024;
	private static int height = 768;

	private static List<PolygonViewInformation> viewing = new ArrayList<>();
	private static float cameraViewDistance = 10000f;

	private static long windowId;

	private static ProgressBar bar;

Matthias Betz's avatar
Matthias Betz committed
105
106
107
	private static final String CITY_OBJECT_MEMBER = "cityObjectMember";
	private static List<QName> chunkProperties = new ArrayList<>();

108
109
110
111
	private static PolygonColorMapper colorMapper = null;
	private static ColorHandler colorHandler = null;
	private static boolean useDebug = false;

Matthias Betz's avatar
Matthias Betz committed
112
113
114
115
116
117
	static {
		chunkProperties.add(new QName(CityGMLConstants.CITYGML_1_0_CORE_NAMESPACE, CITY_OBJECT_MEMBER));
		chunkProperties.add(new QName(CityGMLConstants.CITYGML_2_0_CORE_NAMESPACE, CITY_OBJECT_MEMBER));
		chunkProperties.add(new QName(CityGMLConstants.CITYGML_3_0_CORE_NAMESPACE, CITY_OBJECT_MEMBER));
	}

Matthias Betz's avatar
Matthias Betz committed
118
119
	public static void main(String[] args) {
		File f = null;
120
121
		f = parseArguments(args, f);
		
Matthias Betz's avatar
Matthias Betz committed
122
		try {
123
			setupWindow(f, useDebug);
124
		} catch (Exception e) {
125
			String message = e.getClass().getSimpleName() + ": " + e.getMessage();
126
			if (e.getCause() != null) {
127
				message += "\nCause: " + e.getCause().getClass().getSimpleName() + ": " + e.getCause().getMessage();
128
			}
129
			showErrorDialog(message);
Matthias Betz's avatar
Matthias Betz committed
130
131
132
133
134
135
136
		} finally {
			for (PolygonViewInformation view : viewing) {
				view.destroy();
			}
		}
	}

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
	private static File parseArguments(String[] args, File f) {
		Path colorsPath = null;
		Path mappingPath = null;
		boolean nextColorsFile = false;
		boolean nextMappingsFile = false;
		for (String arg : args) {
			if (nextColorsFile) {
				nextColorsFile = false;
				String file = arg;
				Path path = Paths.get(file);
				if (!Files.exists(path) || !Files.isRegularFile(path)) {
					showErrorDialog("Name specified for colors is not a file");
					System.exit(1);
				}
				colorsPath = path;
			} else if (nextMappingsFile) {
				nextMappingsFile = false;
				String file = arg;
				Path path = Paths.get(file);
				if (!Files.exists(path) || !Files.isRegularFile(path)) {
					showErrorDialog("Name specified for mapping is not a file");
					System.exit(2);
				}
				mappingPath = path;
			} else if (arg.equals("-debug")) {
				useDebug = true;
			} else if (arg.equals("-colors")) {
				// next argument must be a file for the color properties
				nextColorsFile = true;
			} else if (arg.equals("-mapping")) {
				nextMappingsFile = true;
			} else {
				// this must be the citygml file
				String file = arg;
				Path path = Paths.get(file);
				if (!Files.exists(path) || !Files.isRegularFile(path)) {
					showErrorDialog("CityGML file specified does is not a file or does not exist");
					System.exit(3);
				}
			}
		}
		
		// default values for missing parameters
		if (colorsPath == null) {
			colorsPath = Paths.get("color.properties");
		}
		if (mappingPath == null) {
			mappingPath = Paths.get("colorMappings.csv");
		}
		if (f == null) {
			f = showFileChooserDialog();
		}
		
		colorMapper = new PolygonColorMapper(mappingPath);
		colorHandler = new ColorHandler(colorsPath);
		return f;
	}

	public static void showErrorDialog(String msg) {
		JOptionPane.showMessageDialog(null, msg, "Error", JOptionPane.ERROR_MESSAGE);
	}

Matthias Betz's avatar
Matthias Betz committed
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
	private static File showFileChooserDialog() {
		File f = null;
		try (MemoryStack stack = MemoryStack.stackPush()) {
			File defaultPath = new File("bla");
			PointerBuffer pathPointer = stack.mallocPointer(1);
			int success = NativeFileDialog.NFD_OpenDialog("gml,xml", defaultPath.getAbsolutePath(), pathPointer);
			if (success == NativeFileDialog.NFD_OKAY) {
				String filePath = pathPointer.getStringUTF8(0);
				f = new File(filePath);
			} else {
				System.exit(0);
			}
		}
		return f;
	}

	private static void screenShot() {
		// Creating an rbg array of total pixels
		int[] pixels = new int[width * height];
		int bindex;
		// allocate space for RBG pixels
220
		ByteBuffer fb = MemoryUtil.memAlloc(width * height * 3);
Matthias Betz's avatar
Matthias Betz committed
221
222
223
224
225
226
227
228
229
230

		// grab a copy of the current frame contents as RGB
		GL11.glReadPixels(0, 0, width, height, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, fb);

		BufferedImage imageIn = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		// convert RGB data in ByteBuffer to integer array
		for (int i = 0; i < pixels.length; i++) {
			bindex = i * 3;
			pixels[i] = (fb.get(bindex) << 16) + (fb.get(bindex + 1) << 8) + (fb.get(bindex + 2));
		}
231
		MemoryUtil.memFree(fb);
Matthias Betz's avatar
Matthias Betz committed
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
		// Allocate colored pixel to buffered Image
		imageIn.setRGB(0, 0, width, height, pixels, 0, width);

		// Creating the transformation direction (horizontal)
		AffineTransform at = AffineTransform.getScaleInstance(1, -1);
		at.translate(0, -imageIn.getHeight(null));

		// Applying transformation
		AffineTransformOp opRotated = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
		BufferedImage imageOut = opRotated.filter(imageIn, null);

		try {
			int nr = 0;
			File f = new File("screen_" + nr + ".png");
			while (f.exists()) {
				nr++;
				f = new File("screen_" + nr + ".png");
			}
			ImageIO.write(imageOut, "png", f);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

256
257
258
259
	private static void setupWindow(File f, boolean useDebug) {
		GLFW.glfwSetErrorCallback(GLFWErrorCallback.create((code, desc) -> {
			log.severe("Encountered OpenGL error with code: " + code);
		}));
Matthias Betz's avatar
Matthias Betz committed
260
261
262
		if (!GLFW.glfwInit()) {
			throw new IllegalStateException("Unable to initialize GLFW");
		}
Matthias Betz's avatar
Matthias Betz committed
263
		String title = "CityGMLViewer" + f.getName();
Matthias Betz's avatar
Matthias Betz committed
264
265

		GLFW.glfwDefaultWindowHints(); // Loads GLFW's default window settings
266
267
268
		if (useDebug) {
			GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_DEBUG_CONTEXT, GLFW.GLFW_TRUE);
		}
Matthias Betz's avatar
Matthias Betz committed
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
		GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE); // Sets window to be visible
		GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_TRUE); // Sets whether the window is resizable
		GLFW.glfwWindowHint(GLFW.GLFW_SAMPLES, 2);

		windowId = GLFW.glfwCreateWindow(width, height, title, 0, 0);
		if (windowId == 0) {
			GLFW.glfwTerminate();
			throw new IllegalStateException("Failed to create window");
		}

		GLFW.glfwSetWindowSizeCallback(windowId, new GLFWWindowSizeCallbackI() {

			@Override
			public void invoke(long window, int width, int height) {
				CityGMLViewer.width = width;
				CityGMLViewer.height = height;
				if (camera != null) {
					camera.reshape(width, height, cameraViewDistance);
				}
				if (bar != null) {
					bar.updateWindowSize(width, height);
				}
				GL11.glViewport(0, 0, width, height);
			}
		});

		GLFW.glfwSetKeyCallback(windowId, (window, key, scancode, action, mods) -> {
			if (key == GLFW.GLFW_KEY_ESCAPE && action == GLFW.GLFW_PRESS) {
				GLFW.glfwSetWindowShouldClose(window, true);
			} else if (key == GLFW.GLFW_KEY_F9 && action == GLFW.GLFW_PRESS) {
				screenShot();
			}
		});

		GLFW.glfwSetMouseButtonCallback(windowId, new GLFWMouseButtonCallback() {

			@Override
			public void invoke(long window, int button, int action, int mods) {
				if (button == GLFW.GLFW_MOUSE_BUTTON_1 && action == GLFW.GLFW_PRESS) {
					mouse1Down = true;
					try (MemoryStack stack = MemoryStack.stackPush()) {
						DoubleBuffer xBuffer = stack.mallocDouble(1);
						DoubleBuffer yBuffer = stack.mallocDouble(1);
						GLFW.glfwGetCursorPos(windowId, xBuffer, yBuffer);
						x = xBuffer.get(0);
						y = yBuffer.get(0);
					}
				} else if (button == GLFW.GLFW_MOUSE_BUTTON_1 && action == GLFW.GLFW_RELEASE) {
					mouse1Down = false;
				} else if (button == GLFW.GLFW_MOUSE_BUTTON_2 && action == GLFW.GLFW_PRESS) {
					mouse2Down = true;
					try (MemoryStack stack = MemoryStack.stackPush()) {
						DoubleBuffer xBuffer = stack.mallocDouble(1);
						DoubleBuffer yBuffer = stack.mallocDouble(1);
						GLFW.glfwGetCursorPos(windowId, xBuffer, yBuffer);
						x = xBuffer.get(0);
						y = yBuffer.get(0);
					}
				} else if (button == GLFW.GLFW_MOUSE_BUTTON_2 && action == GLFW.GLFW_RELEASE) {
					mouse2Down = false;
				}
			}
		});

		GLFW.glfwSetScrollCallback(windowId, new GLFWScrollCallback() {

			@Override
			public void invoke(long window, double xoffset, double yoffset) {
				camera.scroll((int) (-yoffset));
			}
		});

		GLFW.glfwSetCursorPosCallback(windowId, (window, xPos, yPos) -> {
			if (mouse1Down) {
				try (MemoryStack stack = MemoryStack.stackPush()) {
					double dragDiffX = xPos - x;
					double dragDiffY = yPos - y;
					camera.drag(dragDiffX, dragDiffY);
					x = xPos;
					y = yPos;
				}
			} else if (mouse2Down) {
				try (MemoryStack stack = MemoryStack.stackPush()) {
					double dragDiffX = xPos - x;
					double dragDiffY = yPos - y;
					camera.move(dragDiffX, dragDiffY);
					x = xPos;
					y = yPos;
				}
			}
		});

		GLFW.glfwMakeContextCurrent(windowId); // glfwSwapInterval needs a context on the calling thread, otherwise will
												// cause
		// NO_CURRENT_CONTEXT error
		org.lwjgl.opengl.GLCapabilities caps = GL.createCapabilities(); // Will let lwjgl know we want to use this
																		// context as the context to draw with
366
367
368
369
370
		Callback cb = null;
		if (useDebug) {
			cb = GLUtil.setupDebugMessageCallback();
		}

Matthias Betz's avatar
Matthias Betz committed
371
372
		if (!caps.OpenGL30) {
			log.warning("OpenGL 3.0 is not supported");
Matthias Betz's avatar
Matthias Betz committed
373
374
375
376
377
378
379
380
381
382
383
		}
		GLFW.glfwSwapInterval(1); // How many draws to swap the buffer

		GL11.glEnable(GL11.GL_DEPTH_TEST);
		GL11.glEnable(GL13.GL_MULTISAMPLE);
		GL11.glDisable(GL11.GL_CULL_FACE);

		GL11.glViewport(0, 0, width, height);
		GLFW.glfwShowWindow(windowId); // Shows the window

		if (log.isLoggable(Level.INFO)) {
Matthias Betz's avatar
Matthias Betz committed
384
			log.fine("OpenGL error code after displaying the window: " + GL11.glGetError());
Matthias Betz's avatar
Matthias Betz committed
385
386
387
388
389
390
		}

		FloatBuffer clearColor = null;
		FloatBuffer clearDepth = null;

		try {
391
			Shader program = new Shader("vertex.vert", "fragment.frag", useDebug);
Matthias Betz's avatar
Matthias Betz committed
392
			program.use();
Matthias Betz's avatar
Matthias Betz committed
393
			log.fine(() -> "OpenGL error code after shader loading: " + GL11.glGetError());
Matthias Betz's avatar
Matthias Betz committed
394
395
396
397
398
			camera = new Camera(program);
			camera.setOrtho(true);
			camera.reshape(width, height, cameraViewDistance);
			bar = new ProgressBar(windowId, width, height);
			bar.drawProgress(0);
Matthias Betz's avatar
Matthias Betz committed
399
			log.fine(() -> "OpenGL error code after drawing initial progress bar: " + GL11.glGetError());
Matthias Betz's avatar
Matthias Betz committed
400
401

			loadGmlFile(f);
Matthias Betz's avatar
Matthias Betz committed
402
			log.fine(() -> "OpenGL error code after loading GML file: " + GL11.glGetError());
Matthias Betz's avatar
Matthias Betz committed
403
404
			bar.free();
			bar = null;
Matthias Betz's avatar
Matthias Betz committed
405

Matthias Betz's avatar
Matthias Betz committed
406
407
408
409
410
411
412
413
			camera.setOrtho(false);
			camera.reshape(width, height, cameraViewDistance);

			clearColor = MemoryUtil.memAllocFloat(4);
			clearColor.put(0, 0.9411765f).put(1, 1f).put(2, 1f).put(3, 1f);
			clearDepth = MemoryUtil.memAllocFloat(1);
			clearDepth.put(0, 1f);

Matthias Betz's avatar
Matthias Betz committed
414
			log.fine(() -> "OpenGL error code before loop: " + GL11.glGetError());
Matthias Betz's avatar
Matthias Betz committed
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
			while (!GLFW.glfwWindowShouldClose(windowId)) {
				/* Do something */
				GL30.glClearBufferfv(GL11.GL_COLOR, 0, clearColor);
				GL30.glClearBufferfv(GL11.GL_DEPTH, 0, clearDepth);

				for (PolygonViewInformation view : viewing) {
					view.draw();
				}

				Thread.sleep(15);

				GLFW.glfwSwapBuffers(windowId);
				GLFW.glfwPollEvents();
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
			Thread.currentThread().interrupt();
		} finally {
			MemoryUtil.memFree(clearColor);
			MemoryUtil.memFree(clearDepth);
			if (camera != null) {
				camera.free();
			}
		}

440
441
442
		if (cb != null) {
			cb.free();
		}
Matthias Betz's avatar
Matthias Betz committed
443
444
445
446
447
448
449
450
451
452
453
454
		GLFW.glfwDestroyWindow(windowId);
		Callbacks.glfwFreeCallbacks(windowId);
		GLFW.glfwTerminate();
	}

	private static void loadGmlFile(File f) {
		try {
			File file = f;
			ParserConfiguration config = new ParserConfiguration();
			CityGMLParser.parseEpsgCodeFromFile(file, config);
			CityGMLContext context = CityGMLContext.newInstance();
			CityGMLInputFactory in = context.createCityGMLInputFactory()
Matthias Betz's avatar
Matthias Betz committed
455
					.withChunking(ChunkOptions.chunkByProperties(chunkProperties).skipCityModel(true));
Matthias Betz's avatar
Matthias Betz committed
456
457
458
			try (ObservedInputStream ois = new ObservedInputStream(file);
					CityGMLReader reader = in.createCityGMLReader(file.getAbsolutePath(), ois)) {
				ois.addListener(p -> bar.drawProgress((int) (p * 82)));
459
460
461

				int threads = Runtime.getRuntime().availableProcessors();
				ExecutorService service = Executors.newFixedThreadPool(threads);
Matthias Betz's avatar
Matthias Betz committed
462
				List<Future<FeatureMapper>> mappers = new ArrayList<>();
Matthias Betz's avatar
Matthias Betz committed
463
				while (reader.hasNext()) {
Matthias Betz's avatar
Matthias Betz committed
464
465
					CityGMLChunk nextChunk = reader.nextChunk();
					mappers.add(service.submit(() -> {
466
						FeatureMapper mapper = new FeatureMapper(config, colorMapper, colorHandler);
Matthias Betz's avatar
Matthias Betz committed
467
468
469
470
						AbstractFeature feature = nextChunk.build();
						feature.accept(mapper);
						return mapper;
					}));
Matthias Betz's avatar
Matthias Betz committed
471
				}
Matthias Betz's avatar
Matthias Betz committed
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
				service.shutdown();
				service.awaitTermination(20, TimeUnit.MINUTES);
				List<Polygon> lod1Polygons = new ArrayList<>();
				List<Polygon> lod2Polygons = new ArrayList<>();
				List<Polygon> lod3Polygons = new ArrayList<>();
				List<Polygon> lod4Polygons = new ArrayList<>();
				List<FeatureMapper> fMappers = mappers.stream().map(t -> {
					try {
						return t.get();
					} catch (InterruptedException e) {
						Thread.currentThread().interrupt();
						log.severe("Could not finish parsing file in 20 minutes, aborting");
						throw new IllegalStateException(e);
					} catch (ExecutionException e) {
						log.severe("Could not parsing file completely, cause: " + e.getCause().getMessage());
						throw new IllegalStateException(e);
					}
				}).toList();
				fMappers.stream().forEach(m -> {
					lod1Polygons.addAll(m.getLod1Polygons());
					lod2Polygons.addAll(m.getLod2Polygons());
					lod3Polygons.addAll(m.getLod3Polygons());
					lod4Polygons.addAll(m.getLod4Polygons());
				});
				BoundingBox bbox = BoundingBox.of(lod1Polygons, lod2Polygons, lod3Polygons, lod4Polygons);
Matthias Betz's avatar
Matthias Betz committed
497
498
				bar.drawProgress(81);
				Vector3d center = bbox.getCenter();
Matthias Betz's avatar
Matthias Betz committed
499
				fMappers.forEach(m -> m.movePolygonsBy(center));
Matthias Betz's avatar
Matthias Betz committed
500
				bar.drawProgress(82);
501
502
				long nrOfPolygons = (long) lod1Polygons.size() + lod2Polygons.size() + lod3Polygons.size()
						+ lod4Polygons.size();
Matthias Betz's avatar
Matthias Betz committed
503
				long[] count = new long[1];
Matthias Betz's avatar
Matthias Betz committed
504
				log.info(() -> "Found " + nrOfPolygons + " polygons");
Matthias Betz's avatar
Matthias Betz committed
505
506
507
508
				PolygonListener l = () -> {
					count[0]++;
					bar.drawProgress(82 + (int) (count[0] * 9 / nrOfPolygons));
				};
Matthias Betz's avatar
Matthias Betz committed
509
510
				if (!lod1Polygons.isEmpty()) {
					PolygonViewInformation lod1ViewInfo = new PolygonViewInformation(lod1Polygons, l);
Matthias Betz's avatar
Matthias Betz committed
511
512
					viewing.add(lod1ViewInfo);
				}
Matthias Betz's avatar
Matthias Betz committed
513
514
				if (!lod2Polygons.isEmpty()) {
					PolygonViewInformation lod2ViewInfo = new PolygonViewInformation(lod2Polygons, l);
Matthias Betz's avatar
Matthias Betz committed
515
516
					viewing.add(lod2ViewInfo);
				}
Matthias Betz's avatar
Matthias Betz committed
517
518
				if (!lod3Polygons.isEmpty()) {
					PolygonViewInformation lod3ViewInfo = new PolygonViewInformation(lod3Polygons, l);
Matthias Betz's avatar
Matthias Betz committed
519
520
					viewing.add(lod3ViewInfo);
				}
Matthias Betz's avatar
Matthias Betz committed
521
522
				if (!lod4Polygons.isEmpty()) {
					PolygonViewInformation lod4ViewInfo = new PolygonViewInformation(lod4Polygons, l);
Matthias Betz's avatar
Matthias Betz committed
523
524
525
526
527
528
529
530
531
					viewing.add(lod4ViewInfo);
				}

				double longestSide = bbox.getDiagonalLength() * 0.2;
				double d = longestSide / Math.tan(Math.toRadians(30) / 2);
				double translateZ = -d;
				camera.setDistance((float) translateZ);
				cameraViewDistance = (float) longestSide * 20f;
				camera.reshape(width, height, cameraViewDistance);
Matthias Betz's avatar
Matthias Betz committed
532
533
534
			} catch (InterruptedException e) {
				Thread.currentThread().interrupt();
				log.severe("Could not finish parsing file in 20 minutes, aborting");
Matthias Betz's avatar
Matthias Betz committed
535
536
537
538
539
540
541
			}
		} catch (GLException | IOException | ParserConfigurationException | SAXException | CityGMLContextException
				| CityGMLReadException e) {
			e.printStackTrace();
		}
	}
}