checkpoint
[anneal.git] / src / edu / berkeley / qfat / stl / StlFileParser.java
1 package edu.berkeley.qfat.stl;
2 import java.io.StreamTokenizer;
3 import java.io.Reader;
4 import java.io.IOException;
5
6 /**
7  * Title:        STL Loader
8  * Description:  STL files loader (Supports ASCII and binary files) for Java3D
9  *               Needs JDK 1.4 due to endian problems
10  * Copyright:    Copyright (c) 2001
11  * Company:      Universidad del Pais Vasco (UPV/EHU)
12  * @author:      Carlos Pedrinaci Godoy
13  * @version 1.0
14  *
15  * Contact : xenicp@yahoo.es
16  *
17  *   STL FILE PARSER:
18  *   Extends StreamTokenizer
19  *   For Ascii files
20  *   Each token returned is checked in StlFile.java
21  *   Format of an ASCII STL file:
22  *
23  *     solid /users/vis/dru/wedge.stl
24  *       facet normal -1 0 0
25  *         outer loop
26  *           vertex 0.005 1 0
27  *           vertex 0 0.543 0
28  *           vertex 0.453 1 1
29  *         endloop
30  *       endfacet
31  *            .
32  *            .
33  *            .
34  *     endsolid /users/vis/dru/wedge.stl
35  *
36  *  That Class is necessary because scientific numbers are not correctly readed by Tokenizer
37  *  we must then extend that class and define another getNumber
38  */
39
40 public class StlFileParser extends StreamTokenizer
41 {
42   /**
43    * Constructor: object creation and setup
44    *
45    * @param r The Reader instance
46    */
47   public StlFileParser(Reader r)
48   {
49     super(r);
50     setup();
51   }
52
53   /**
54    * Method that sets some params of the Tokenizer for reading the file correctly
55    */
56   public void setup()
57   {
58     resetSyntax();
59     eolIsSignificant(true);   // The End Of Line is important
60     lowerCaseMode(true);
61
62     // All printable ascii characters
63     wordChars('!', '~');
64
65     whitespaceChars(' ', ' ');
66     whitespaceChars('\n', '\n');
67     whitespaceChars('\r', '\r');
68     whitespaceChars('\t', '\t');
69   }// End setup
70
71   /**
72    * Gets a number from the stream.  Note that we don't recognize
73    * numbers in the tokenizer automatically because numbers might be in
74    * scientific notation, which isn't processed correctly by
75    * StreamTokenizer.  The number is returned in nval.
76    *
77    * @return boolean.
78    */
79   boolean getNumber()
80   {
81     int t;
82
83     try {
84         nextToken();
85         if (ttype != TT_WORD)
86           throw new IOException("Expected number on line " + lineno());
87         nval =  (Double.valueOf(sval)).doubleValue();
88     }
89     catch (IOException e) {
90       System.err.println(e.getMessage());
91       return false;
92     }
93     catch (NumberFormatException e) {
94       System.err.println(e.getMessage());
95       return false;
96     }
97     return true;
98   } // end of getNumber
99
100 }// End of StlFileParser