/** * @file InSerialize.java * @author Robert S. Laramee * @version 1.0 * @see DrawCanvas.java, OutSerialize.java *
* start date Friday 27 Nov 98 * "finish" date Thursday 03 Nov 98 * *
description This is the class that reads the solid object's * definition specification and instances. */ import java.awt.*; // for Color class import java.io.*; // for the streams import java.util.*; // for the Vector class //import matrix.*; // for Point4, Matrix4 class public class InSerialize { private BSPTree tree; private FileInputStream input; private StreamTokenizer stream; private String fileName; private SolidObject solidObject; private SolidObjectList SOlist; private String name; private Face face; private Color color; /** * constructor */ public InSerialize(BSPTree t, SolidObjectList sol) { tree = t; SOlist = sol; input = null; stream = null; fileName = null; solidObject = null; name = null; face = null; color = Color.white; // default } /** * The run() function starts the file parser. */ public void run() { fileName = new String("solid.dat"); openFile(fileName); try { parseFile(); System.err.println ("Done parsing file: " + fileName); } catch ( IOException e ) { System.err.println ("** Error parsing file"); System.err.println ("** IOException: " + e + " aborting..."); return; } } /** * This function opens the file specified by filename * @param filename -name of file to open */ public void openFile(String fileName) { try { input = new FileInputStream ( fileName ); System.out.println("Opening file: " + fileName); } catch ( IOException e ) { System.err.println ("** Error opening file: " + fileName ); System.err.println ("** IOException: " + e + " exiting..."); System.exit(0); } stream = new StreamTokenizer(new InputStreamReader(input)); /* Let's adjust some StreamTokenizer settings: */ stream.parseNumbers(); //stream.ordinaryChar('''); stream.commentChar('#'); stream.ordinaryChar('"'); stream.ordinaryChar('_'); //stream.eolIsSignificant(true); } /** * This method parses the input file * @throws IOException */ public void parseFile() throws IOException { while(stream.nextToken() != StreamTokenizer.TT_EOF) { if (stream.ttype == StreamTokenizer.TT_EOF) { System.out.println("End of file reached"); } else if (stream.ttype == StreamTokenizer.TT_EOL) { System.out.println("End of line reached"); } else if (stream.ttype == StreamTokenizer.TT_NUMBER) { // System.out.println("TT_NUMBER = " + stream.nval); } else if (stream.ttype == StreamTokenizer.TT_WORD) { processCommand(stream.sval, stream); } else { System.out.print("** Error: unknown input type."); } } // end while() return; } /** * This method reads and delegates the commands. * @param s * @param stream * @throws IOException */ private void processCommand(String s, StreamTokenizer stream) throws IOException { if (s.equals("D")) { // parse name parseName(stream); } else if (s.equals("f")) { // parse face parseFace(stream); } else if (s.equals("d")) { // parse darken ratio stream.nextToken(); // get the number if (face != null) face.setDarkenRatio(stream.nval); } else if (s.equals("e")) { // parse end parseEnd(stream); } else if (s.equals("c")) { // parse color parseColor(stream); } else if (s.equals("i")) { // parse instance parseInstance(stream); } else { System.out.println("** Error: command- " + stream.sval + " not recognized"); return; } } /** * This method parses the name of a face. A new name means a new * definition, and therefore a new solid object. We'll also add * the name to the Shape Menu. * @param stream * @throws IOException */ private void parseName(StreamTokenizer stream) throws IOException { stream.nextToken(); name = new String(stream.sval); solidObject = new SolidObject(name); SOlist.add(solidObject); Swing3d.getApp().addUserShape(name); } /** * This method parses the definition of a face * @param stream * @throws IOException */ private void parseFace(StreamTokenizer stream) throws IOException { int numVertices; double x,y,z; Vector points = new Vector(); stream.nextToken(); // parse number of vertices numVertices = (int)stream.nval; for(int i = 0; i < numVertices; i++) { // parse vertices stream.nextToken(); x = stream.nval; stream.nextToken(); y = stream.nval; stream.nextToken(); z = stream.nval; points.addElement(new Point4(x, y, z)); } if (points.size() != numVertices) { System.out.println("** Error: invalid number of vertices: " + numVertices + " != " + points.size()); } else { if (solidObject != null) { face = new Face(color, 1.0, points); solidObject.getFaces().addElement(face); } } return; } /** * This method reads and creates an instance of a solid object * @param stream * @throws IOException */ private void parseInstance(StreamTokenizer stream) throws IOException { stream.nextToken(); // parse name name = new String(stream.sval); Matrix4 xform = new Matrix4(); for(int i = 0; i < 4; i++) { // parse matrix for (int j = 0; j < 4; j++) { stream.nextToken(); xform.put(i, j, stream.nval); } } if (name.equals("cube")) { new Cube(xform, color, tree); } else if (name.equals("tetrahedron")) { new Tetrahedron(xform, color, tree); } else if (solidObject != null) { solidObject.addToTree((new Instance(name, xform, color)), tree); } else { System.out.println("** Error: no object definition for instance"); } } /** * A method to parse and set the colors * @param stream -a handle to the input stream */ private void parseColor(StreamTokenizer stream) throws IOException { int red, green, blue; stream.nextToken(); red = (int)stream.nval; stream.nextToken(); green = (int)stream.nval; stream.nextToken(); blue = (int)stream.nval; color = new Color(red, green, blue); } /** * a short method to parse the word after an end ('e') command * @throws IOException */ private void parseEnd(StreamTokenizer stream) throws IOException { stream.nextToken(); String s = stream.sval; if ((s.equals("D")) || // name command (s.equals("f")) || // face command (s.equals("d")) || // darken ratio command (s.equals("e")) || // end command (s.equals("c")) || // color command (s.equals("i")) ) { // parse instance stream.pushBack(); } } /** * a short method to handle (or ignore rather) comments */ private void handleComment() { // System.out.println("comment line"); try { while (stream.nextToken() != StreamTokenizer.TT_EOL) { // just move to the end of the line } } catch ( IOException e ) { System.err.println ("** Error reading next token"); System.err.println ("** IOException: "+ e +" exiting..."); System.exit(0); } } }