Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009 package edu.tum.cs.vis.model.parser;
00010
00011 import java.io.BufferedReader;
00012 import java.io.DataInputStream;
00013 import java.io.FileInputStream;
00014 import java.io.InputStreamReader;
00015
00016 import edu.tum.cs.vis.model.util.Group;
00017 import edu.tum.cs.vis.model.util.Mesh;
00018 import edu.tum.cs.vis.model.util.Triangle;
00019 import edu.tum.cs.vis.model.util.Vertex;
00020
00042 public class CustomParser extends ModelParser {
00043
00044
00045
00046
00047 @Override
00048 protected boolean loadModel(String filename) {
00049 try {
00050
00051
00052 FileInputStream fstream = new FileInputStream(filename);
00053
00054 DataInputStream in = new DataInputStream(fstream);
00055 @SuppressWarnings("resource")
00056
00057 BufferedReader br = new BufferedReader(new InputStreamReader(in));
00058 String strLine;
00059
00060 Group g = new Group(model);
00061 model.setGroup(g);
00062 Mesh m = new Mesh();
00063 g.setMesh(m);
00064
00065 boolean isTriangles = false;
00066
00067 while ((strLine = br.readLine()) != null) {
00068 if (strLine.compareTo("#") == 0)
00069 isTriangles = true;
00070 else {
00071 String[] parts = strLine.split("\t");
00072
00073 if (!isTriangles) {
00074 if (parts.length != 5)
00075 throw new RuntimeException("Vertex: invalid number of data: "
00076 + parts.length);
00077
00078 float x = Float.valueOf(parts[0]);
00079 float y = Float.valueOf(parts[1]);
00080 float z = Float.valueOf(parts[2]);
00081
00082 model.getVertices().add(new Vertex(x, y, z));
00083 } else {
00084 if (parts.length != 3)
00085 throw new RuntimeException("Triangle: invalid number of data: "
00086 + parts.length);
00087 int p1 = Integer.valueOf(parts[0]);
00088 int p2 = Integer.valueOf(parts[1]);
00089 int p3 = Integer.valueOf(parts[2]);
00090
00091 Triangle t = new Triangle();
00092 t.getPosition()[0] = model.getVertices().get(p1);
00093 t.getPosition()[1] = model.getVertices().get(p2);
00094 t.getPosition()[2] = model.getVertices().get(p3);
00095
00096 t.updateCentroid();
00097 t.updateEdges();
00098 m.getTriangles().add(t);
00099 }
00100 }
00101
00102 }
00103
00104 in.close();
00105
00106 } catch (Exception e) {
00107 System.err.println("Error: " + e.getMessage());
00108 return false;
00109 }
00110
00111 return true;
00112
00113 }
00114 }