/* */ import java.Math.*; import java.util.ArrayList; scene = window.getScene(); // display dialogue to get relevant parameters orientList=new BComboBox(); orientList.add("XY"); orientList.add("XZ"); orientList.add("YZ"); scaleField = new ValueField(0.01, ValueField.POSITIVE); sampleField=new ValueField(16,ValueField.POSITIVE+ValueField.INTEGER); comps=new Widget[]{scaleField,sampleField,orientList}; labels=new String[]{ "Scaling Factor", "Number of Points per segment","Orientation of Curve" }; dlg = new ComponentsDialog(window, "Select Parameters for SVG Import",comps,labels); if (!dlg.clickedOk()) return; scalefac=scaleField.getValue(); sampleNumber = sampleField.getValue(); orientChoice=orientList.getSelectedIndex(); // initialise xsum=0.0; ysum=0.0; // display file chooser to select file fc = new BFileChooser(BFileChooser.OPEN_FILE, "Import SVG File"); fc.showDialog(window); f = fc.getSelectedFile(); in = new BufferedReader(new FileReader(f)); bezPoint=new Vec3[100]; smoothness=new float[100]; F=new Vec3(); A=new Vec3(); B=new Vec3(); C=new Vec3(); D=new Vec3(); id = null; eof = false; do { // find start of path data while ((s = in.readLine()).indexOf("")!=-1) return; } // find start of actual data points dataSplit = new StringTokenizer(s); while (dataSplit.hasMoreTokens()) { tok = dataSplit.nextToken(); //System.out.println("tok=" + tok); // if we found the 'd' attribute, advance to the 'M' token within it if (tok.equals("d") || tok.startsWith("d=")) { while (!tok.endsWith("M")) tok = dataSplit.nextToken(); break; } // if we see an id attribute, grab it as it goes past if (tok.equals("id") || tok.startsWith("id=")) { // find value while (tok.indexOf('"') == -1) tok = dataSplit.nextToken(); id = tok.substring(tok.indexOf('"')+1); // find end of value while (id.indexOf('"') == -1) id += " " + dataSplit.nextToken(); id = id.substring(0, id.indexOf('"')); } if (tok.indexOf("/>") != -1) return; // end of line - read next line if (!dataSplit.hasMoreTokens()) { s = in.readLine(); dataSplit = new StringTokenizer(s); } } dataSt=dataSplit.nextToken(); // read first 2 points of data dataStSp=new StringTokenizer(dataSt,","); // parse token separated by ',' Ax=Double.parseDouble(dataStSp.nextToken()); // convert to actual numbers Ay=Double.parseDouble(dataStSp.nextToken()); F.set(Ax,-Ay,0); // remember first point currentPoint=0; type = '\0'; data = true; // parse remaining data points do { // if no more tokens, then get the next line if (!dataSplit.hasMoreTokens()) { s = in.readLine(); if (s.indexOf("") != -1) { eof = true; break; } dataSplit = new StringTokenizer(s); } // get the next token dataSt=dataSplit.nextToken(); // if token is alpha, then it is a new type specifier - advance parser. // Otherwise, type is the same as the previous type if (Character.isLetter(dataSt.charAt(0))) { type = dataSt.charAt(0); dataSt = dataSplit.nextToken(); } switch (type) { // if the next chunk is a "C" then it is a cubic bezier curve case 'C': case 'c': // *** CUBIC BEZIER *** // bezier curves have 3 sets of x,y coordinates dataStSp=new StringTokenizer(dataSt,","); // separate x and y vals Bx=Double.parseDouble(dataStSp.nextToken()); By=Double.parseDouble(dataStSp.nextToken()); dataSt=dataSplit.nextToken(); // read 2nd point of data dataStSp=new StringTokenizer(dataSt,","); // separate x and y vals Cx=Double.parseDouble(dataStSp.nextToken()); Cy=Double.parseDouble(dataStSp.nextToken()); dataSt=dataSplit.nextToken(); // read 3rd point of data dataStSp=new StringTokenizer(dataSt,","); // separate x and y vals Dx=Double.parseDouble(dataStSp.nextToken()); // if token contains '"', then data stops there last=dataStSp.nextToken(); if (last.indexOf('"')!=-1) { data = false; last=last.substring(0,last.indexOf('"')); } Dy=Double.parseDouble(last); // create the vector points A.set(Ax,-Ay,0); B.set(Bx,-By,0); C.set(Cx,-Cy,0); D.set(Dx,-Dy,0); // calculate the curve points for this segment //using cubic bezier equations for (i=0; i") || dataSt.endsWith("\"")) data = false; } while(data); L=new Vec3(Ax,-Ay,0); // remember the last point //System.out.println("cp=" + currentPoint + // "; size=" + (currentPoint*sampleNumber) + // "; len=" + bezPoint.length); // resize arrays to exact size if (bezPoint.length > currentPoint*sampleNumber) { t1 = bezPoint; bezPoint = new Vec3[currentPoint*sampleNumber]; System.arraycopy(t1, 0, bezPoint, 0, bezPoint.length); t2 = smoothness; smoothness = new float[bezPoint.length]; System.arraycopy(t2, 0, smoothness, 0, smoothness.length); } // create the curve - set closed=true if F == L bezier=new Curve(bezPoint,smoothness,Curve.INTERPOLATING,F.equals(L)); // if we haven't already found it, look for the id attribute if (id == null) { do { // end of line - read next line if (!dataSplit.hasMoreTokens()) { s = in.readLine(); dataSplit = new StringTokenizer(s); } tok = dataSplit.nextToken(); //System.out.println("tok=" + tok); // id attribute - grab the value if (tok.equals("id") || tok.startsWith("id=")) { // find the start of the value while (tok.indexOf('"') == -1) tok = dataSplit.nextToken(); id = tok.substring(tok.indexOf('"')+1); // find end of value while (id.indexOf('"') == -1) id += " "+dataSplit.nextToken(); id = id.substring(0, id.indexOf('"')); } if (tok.indexOf("/>") != -1) break; } while (true); // default id, if no attribute found if (id == null) id = "path"; } // orient in YZ plane if (orientChoice==0) window.addObject(bezier, new CoordinateSystem(new Vec3(0,0,0),0,90,0), id, null); // orient in XY plane if (orientChoice==1) window.addObject(bezier, new CoordinateSystem(new Vec3(0,0,0),0,0,0), id, null); // orient in XZ plane if (orientChoice==2) window.addObject(bezier, new CoordinateSystem(new Vec3(0,0,0),90,0,0), id, null); } while (!eof)