CustomParser.java
Go to the documentation of this file.
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; Andrei Stoica -
00007  * minor refactor during the Google Summer of Code 2014
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         /* (non-Javadoc)
00045          * @see edu.tum.cs.vis.model.parser.ModelParser#loadModel(java.lang.String)
00046          */
00047         @Override
00048         protected boolean loadModel(String filename) {
00049                 try {
00050                         // Open the file that is the first
00051                         // command line parameter
00052                         FileInputStream fstream = new FileInputStream(filename);
00053                         // Get the object of DataInputStream
00054                         DataInputStream in = new DataInputStream(fstream);
00055                         @SuppressWarnings("resource")
00056                         // necessary to close resources at the outer layer only
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                         // Read File Line By Line
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                         // Close the input stream
00104                         in.close();
00105 
00106                 } catch (Exception e) {// Catch exception if any
00107                         System.err.println("Error: " + e.getMessage());
00108                         return false;
00109                 }
00110 
00111                 return true;
00112 
00113         }
00114 }


knowrob_cad_parser
Author(s): Stefan Profanter
autogenerated on Mon Oct 6 2014 01:29:56