1 package org.textensor.vis; 2 3 import java.io.BufferedReader; 4 import java.io.File; 5 import java.io.FileReader; 6 import java.io.IOException; 7 import java.io.StreamTokenizer; 8 import java.util.ArrayList; 9 10 import org.textensor.report.E; 11 12 public class ElementReader { 13 14 File fsrc; 15 16 ArrayList<VolElt> elts; 17 18 public ElementReader(File f) { 19 fsrc = f; 20 elts = new ArrayList<VolElt>(); 21 } 22 23 public void read() { 24 try { 25 26 BufferedReader br = new BufferedReader(new FileReader(fsrc)); 27 StreamTokenizer tz = new StreamTokenizer(br); 28 tz.eolIsSignificant(false); 29 30 while (br.ready()) { 31 int ielt = nextInt(tz); 32 int nstrip = nextInt(tz); 33 int nvert = nextInt(tz); 34 35 int[] slens = new int[nstrip]; 36 for (int i = 0; i < nstrip; i++) { 37 slens[i] = nextInt(tz); 38 } 39 float[] verts = new float[3 * nvert]; 40 float[] norms = new float[3 * nvert]; 41 42 int ind = 0; 43 for (int i = 0; i < nvert; i++) { 44 verts[ind] = nextFloat(tz); 45 verts[ind+1] = nextFloat(tz); 46 verts[ind+2] = nextFloat(tz); 47 48 norms[ind] = nextFloat(tz); 49 norms[ind+1] = nextFloat(tz); 50 norms[ind+2] = nextFloat(tz); 51 52 ind += 3; 53 } 54 55 VolElt ve = new VolElt(slens, verts, norms); 56 elts.add(ve); 57 } 58 59 } catch (Exception ex) { 60 ex.printStackTrace(); 61 } 62 E.info("loaded " + elts.size() + " curved volume elements"); 63 } 64 65 private int nextInt(StreamTokenizer tz) throws IOException { 66 int itok = tz.nextToken(); 67 int ret = 0; 68 if (tz.ttype == StreamTokenizer.TT_NUMBER) { 69 ret = (int)(Math.round(tz.nval)); 70 71 } else { 72 throw new IOException("need a number but got " + tz); 73 } 74 return ret; 75 } 76 77 78 private float nextFloat(StreamTokenizer tz) throws IOException { 79 int itok = tz.nextToken(); 80 float ret = 0; 81 if (tz.ttype == StreamTokenizer.TT_NUMBER) { 82 ret = (float)(tz.nval); 83 84 } else { 85 throw new IOException("need a number but got " + tz); 86 } 87 return ret; 88 } 89 90 public ArrayList<VolElt> getElements() { 91 return elts; 92 } 93 }