Skip to content
Snippets Groups Projects
Commit 83324be7 authored by Marek Bařinka's avatar Marek Bařinka
Browse files

Update MeshObjLoader.java

Exceptions redone.
Changed returning null to throwing exception.
parent ca41dcd5
No related branches found
No related tags found
No related merge requests found
...@@ -35,41 +35,30 @@ public class MeshObjLoader { ...@@ -35,41 +35,30 @@ public class MeshObjLoader {
/** /**
* Loads data into MeshModel * Loads data into MeshModel
* @param is Input data stream with model * @param is Input data stream with model
* @return Complete MeshModel or null if there is problem with data * @return Complete MeshModel
* @throws IOException There was problem reading data stream * @throws IOException Data are corrupted
*/ */
public static MeshModel read(InputStream is) throws IOException { public static MeshModel read(InputStream is) throws IOException {
MeshModel meshModel = null; final IOBJParser parser = new OBJParser();
try { final OBJModel model = parser.parse(is);
final IOBJParser parser = new OBJParser();
final OBJModel model = parser.parse(is); if (model.getObjects().isEmpty()) {
throw new IOException("File doesn't contain any model");
if (model.getObjects().isEmpty()) {
return null;
}
OBJObject object = model.getObjects().get(0);
meshModel = parseObjectToModel(model, object);
} catch (IOException ex) {
throw new IOException("Cannot read requested stream", ex);
} }
return meshModel; OBJObject object = model.getObjects().get(0);
return parseObjectToModel(model, object);
} }
/** /**
* Opens file and loads data into MeshModel * Opens file and loads data into MeshModel
* @param file File containing face to load into a MeshModel * @param file File containing face to load into a MeshModel
* @return Complete MeshModel or null if there is problem with data * @return Complete MeshModel
* @throws FileNotFoundException Requested file was not found * @throws FileNotFoundException Requested file was not found
* @throws IOException There was problem with reading the file * @throws IOException There was problem with reading the file
*/ */
public static MeshModel read(File file) throws FileNotFoundException, IOException { public static MeshModel read(File file) throws FileNotFoundException, IOException {
try (InputStream is = new FileInputStream(file)) { try (InputStream is = new FileInputStream(file)) {
return read(is); return read(is);
} catch (FileNotFoundException ex) {
throw new FileNotFoundException("File " + file.getName() + " not found");
} catch (IOException ex) {
throw new IOException("Cannot open file" + file.getName(), ex);
} }
} }
...@@ -77,16 +66,14 @@ public class MeshObjLoader { ...@@ -77,16 +66,14 @@ public class MeshObjLoader {
* Parse OBJObject into MeshModel * Parse OBJObject into MeshModel
* @param model Model is needed in future. It's holding data pools * @param model Model is needed in future. It's holding data pools
* @param object Object to parse. It corespond to our MeshModel * @param object Object to parse. It corespond to our MeshModel
* @return Returns complete model or null if there is problem with data * @return Returns complete model
* @throws IOException Data are corrupted
*/ */
private static MeshModel parseObjectToModel(OBJModel model, OBJObject object) { private static MeshModel parseObjectToModel(OBJModel model, OBJObject object) throws IOException {
MeshModel meshModel = new MeshModel(); MeshModel meshModel = new MeshModel();
// Our facet = loader mesh, create and fill all facets // Our facet = loader mesh, create and fill all facets
for (OBJMesh mesh : object.getMeshes()) { for (OBJMesh mesh : object.getMeshes()) {
MeshFacet meshFacet = parseMeshToFacet(model, mesh); MeshFacet meshFacet = parseMeshToFacet(model, mesh);
if (meshFacet == null) {
return null;
}
meshModel.addChild(meshFacet); meshModel.addChild(meshFacet);
} }
return meshModel; return meshModel;
...@@ -96,16 +83,14 @@ public class MeshObjLoader { ...@@ -96,16 +83,14 @@ public class MeshObjLoader {
* Parse OBJMesh into MeshFacet * Parse OBJMesh into MeshFacet
* @param model Model is needed in future. It's holding data pools * @param model Model is needed in future. It's holding data pools
* @param mesh Mesh to parse. It corespond to our MeshMacet * @param mesh Mesh to parse. It corespond to our MeshMacet
* @return Returns complete facet or null if there is problem with data * @return Returns complete facet
* @throws IOException Data are corrupted
*/ */
private static MeshFacet parseMeshToFacet(OBJModel model, OBJMesh mesh) { private static MeshFacet parseMeshToFacet(OBJModel model, OBJMesh mesh) throws IOException {
MeshFacet meshFacet = new MeshFacet(); MeshFacet meshFacet = new MeshFacet();
for (OBJFace face : mesh.getFaces()) { for (OBJFace face : mesh.getFaces()) {
MeshFace meshFace = parseFaceToFace(model, face, meshFacet); MeshFace meshFace = parseFaceToFace(model, face, meshFacet);
if (meshFace == null) {
return null;
}
meshFacet.addChild(meshFace); meshFacet.addChild(meshFace);
} }
return meshFacet; return meshFacet;
...@@ -116,14 +101,15 @@ public class MeshObjLoader { ...@@ -116,14 +101,15 @@ public class MeshObjLoader {
* @param model Model is needed in future. It's holding data pools * @param model Model is needed in future. It's holding data pools
* @param face Face to parse. It corespond to our MeshFace * @param face Face to parse. It corespond to our MeshFace
* @param meshFacet Facet is holding our data pools * @param meshFacet Facet is holding our data pools
* @return Returns complete face or null if there is problem with data * @return Returns complete face
* @throws IOException Data are corrupted
*/ */
private static MeshFace parseFaceToFace(OBJModel model, OBJFace face, MeshFacet meshFacet) { private static MeshFace parseFaceToFace(OBJModel model, OBJFace face, MeshFacet meshFacet) throws IOException {
List<MeshPoint> facePoints = getPointsFromFace(model, face, meshFacet); List<MeshPoint> facePoints = getPointsFromFace(model, face, meshFacet);
MeshFace meshFace = new MeshFace(); MeshFace meshFace = new MeshFace();
if (facePoints.size() != 3) { if (facePoints.size() != 3) {
return null; throw new IOException("Data contains non-triangular face");
} }
// Create edges // Create edges
for (int i = 1; i <= facePoints.size(); i++) { for (int i = 1; i <= facePoints.size(); i++) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment