$search
00001 /******************************************************************************* 00002 * Copyright (c) 2012 Stefan Profanter. All rights reserved. This program and the accompanying 00003 * materials are made available under the terms of the GNU Public License v3.0 which accompanies 00004 * this distribution, and is available at http://www.gnu.org/licenses/gpl.html 00005 * 00006 * Contributors: Stefan Profanter - initial API and implementation, Year: 2012 00007 ******************************************************************************/ 00008 package edu.tum.cs.vis.model.parser; 00009 00010 import java.io.BufferedReader; 00011 import java.io.DataInputStream; 00012 import java.io.FileInputStream; 00013 import java.io.InputStreamReader; 00014 00015 import edu.tum.cs.vis.model.util.Group; 00016 import edu.tum.cs.vis.model.util.Mesh; 00017 import edu.tum.cs.vis.model.util.Triangle; 00018 import edu.tum.cs.vis.model.util.Vertex; 00019 00041 public class CustomParser extends ModelParser { 00042 00043 /* (non-Javadoc) 00044 * @see edu.tum.cs.vis.model.parser.ModelParser#loadModel(java.lang.String) 00045 */ 00046 @Override 00047 protected boolean loadModel(String filename) { 00048 try { 00049 // Open the file that is the first 00050 // command line parameter 00051 FileInputStream fstream = new FileInputStream(filename); 00052 // Get the object of DataInputStream 00053 DataInputStream in = new DataInputStream(fstream); 00054 BufferedReader br = new BufferedReader(new InputStreamReader(in)); 00055 String strLine; 00056 00057 Group g = new Group(model); 00058 model.setGroup(g); 00059 Mesh m = new Mesh(); 00060 g.setMesh(m); 00061 00062 boolean isTriangles = false; 00063 // Read File Line By Line 00064 while ((strLine = br.readLine()) != null) { 00065 if (strLine.compareTo("#") == 0) 00066 isTriangles = true; 00067 else { 00068 String[] parts = strLine.split("\t"); 00069 00070 if (!isTriangles) { 00071 if (parts.length != 5) 00072 throw new RuntimeException("Vertex: invalid number of data: " 00073 + parts.length); 00074 00075 float x = Float.valueOf(parts[0]); 00076 float y = Float.valueOf(parts[1]); 00077 float z = Float.valueOf(parts[2]); 00078 00079 model.getVertices().add(new Vertex(x, y, z)); 00080 } else { 00081 if (parts.length != 3) 00082 throw new RuntimeException("Triangle: invalid number of data: " 00083 + parts.length); 00084 int p1 = Integer.valueOf(parts[0]); 00085 int p2 = Integer.valueOf(parts[1]); 00086 int p3 = Integer.valueOf(parts[2]); 00087 00088 Triangle t = new Triangle(); 00089 t.getPosition()[0] = model.getVertices().get(p1); 00090 t.getPosition()[1] = model.getVertices().get(p2); 00091 t.getPosition()[2] = model.getVertices().get(p3); 00092 00093 t.updateCentroid(); 00094 m.getTriangles().add(t); 00095 } 00096 } 00097 00098 } 00099 // Close the input stream 00100 in.close(); 00101 00102 } catch (Exception e) {// Catch exception if any 00103 System.err.println("Error: " + e.getMessage()); 00104 return false; 00105 } 00106 00107 return true; 00108 00109 } 00110 }