Skip to content

Commit 7c672f8

Browse files
Refac obj file parsing
1 parent 449c4d7 commit 7c672f8

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

pacman-ui-lib/src/main/java/de/amr/pacmanfx/uilib/model3D/Model3D.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
import de.amr.pacmanfx.uilib.objimport.ObjFileImporter;
1010
import javafx.scene.paint.Material;
1111
import javafx.scene.shape.TriangleMesh;
12+
import org.tinylog.Logger;
1213

1314
import java.net.URL;
1415
import java.nio.charset.StandardCharsets;
16+
import java.time.Duration;
17+
import java.time.Instant;
1518
import java.util.List;
1619
import java.util.Map;
1720

@@ -29,7 +32,17 @@ public class Model3D implements Disposable {
2932
*/
3033
public Model3D(URL url) {
3134
requireNonNull(url);
35+
Instant start = Instant.now();
3236
objData = ObjFileImporter.importObjFile(url, StandardCharsets.UTF_8);
37+
Duration duration = Duration.between(start, Instant.now());
38+
Logger.info("OBJ file imported ({} millis). URL: '{}'", duration.toMillis(), url);
39+
for (TriangleMesh mesh : objData.meshMap().values()) {
40+
try {
41+
ObjFileImporter.validateTriangleMesh(mesh);
42+
} catch (AssertionError error) {
43+
Logger.error("Invalid OBJ file data: {}, URL: '{}'", error.getMessage(), url);
44+
}
45+
}
3346
}
3447

3548
@Override

pacman-ui-lib/src/main/java/de/amr/pacmanfx/uilib/objimport/ObjFileImporter.java

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Copyright (c) 2021-2025 Armin Reichert (MIT License)
33
See file LICENSE in repository root directory for details.
44
*/
5-
65
/*
76
* Copyright (c) 2010, 2014, Oracle and/or its affiliates.
87
* All rights reserved. Use is subject to license terms.
@@ -48,8 +47,6 @@
4847
import java.io.InputStreamReader;
4948
import java.net.URL;
5049
import java.nio.charset.Charset;
51-
import java.time.Duration;
52-
import java.time.Instant;
5350
import java.util.ArrayList;
5451
import java.util.HashMap;
5552
import java.util.List;
@@ -58,7 +55,7 @@
5855
import static java.util.Objects.requireNonNull;
5956

6057
/**
61-
* Derived from Oracle's OBJ importer from the 3DViewer sample project.
58+
* Code is based on Oracle's OBJ importer from the 3DViewer sample project.
6259
*
6360
* @see <a href=
6461
* "https://github.com/teamfx/openjfx-10-dev-rt/tree/master/apps/samples/3DViewer/src/main/java/com/javafx/experiments/importers">3DViewer
@@ -117,14 +114,8 @@ public static ObjFileData importObjFile(URL url, Charset charset) {
117114
requireNonNull(charset);
118115
ObjFileImporter importer = new ObjFileImporter(url);
119116
try (InputStream is = url.openStream()) {
120-
Instant start = Instant.now();
121117
var reader = new BufferedReader(new InputStreamReader(is, charset));
122118
importer.parse(reader);
123-
for (TriangleMesh mesh : importer.data.triangleMeshMap.values()) {
124-
validateTriangleMesh(mesh);
125-
}
126-
Duration duration = Duration.between(start, Instant.now());
127-
Logger.info("OBJ file parsed in {} milliseconds; '{}'", duration.toMillis(), url);
128119
}
129120
catch (IOException x) {
130121
Logger.error(x);
@@ -400,25 +391,25 @@ private void commitCurrentMesh() {
400391
mesh.getPoints().setAll(verticesArray);
401392
mesh.getTexCoords().setAll(texCoordsArray);
402393

403-
int[] faces = toIntArray(restOf(data.facesList, facesStart));
394+
final int[] faces = toIntArray(restOf(data.facesList, facesStart));
404395
mesh.getFaces().setAll(faces);
405396

406-
int[] smoothingGroups = useNormals
397+
final int[] smoothingGroups = useNormals
407398
? computeSmoothingGroups(mesh, faces, toIntArray(restOf(data.faceNormalsList, facesNormalStart)), toFloatArray(normalsArray))
408399
: toIntArray(restOf(data.smoothingGroupList, smoothingGroupsStart));
409400
mesh.getFaceSmoothingGroups().setAll(smoothingGroups);
410401

411402
// try specified name, if already used, make unique name using serial number e.g. "my_mesh (3)"
412403
int serialNumber = 2;
413-
String nextMeshName = meshName;
414-
while (data.triangleMeshMap.containsKey(nextMeshName)) {
415-
Logger.info("Mesh name '{}' already exists", nextMeshName);
416-
nextMeshName = "%s (%d)".formatted(meshName, serialNumber);
404+
String unusedMeshName = meshName;
405+
while (data.triangleMeshMap.containsKey(unusedMeshName)) {
406+
Logger.info("Mesh name '{}' already exists", unusedMeshName);
407+
unusedMeshName = "%s (%d)".formatted(meshName, serialNumber);
417408
++serialNumber;
418409
}
419-
data.triangleMeshMap.put(nextMeshName, mesh);
410+
data.triangleMeshMap.put(unusedMeshName, mesh);
420411

421-
Logger.trace("Mesh '{}' added, vertices: {}, uvs: {}, faces: {}, smoothing groups: {}",
412+
Logger.trace("Added mesh '{}', vertices: {}, texture coordinates: {}, faces: {}, smoothing groups: {}",
422413
meshName,
423414
mesh.getPoints().size() / mesh.getPointElementSize(),
424415
mesh.getTexCoords().size() / mesh.getTexCoordElementSize(),

0 commit comments

Comments
 (0)