Search in sources :

Example 1 with ReaderTokenizer

use of maspack.util.ReaderTokenizer in project artisynth_core by artisynth.

the class MayaJointToMatlab method doJointExport.

public void doJointExport(String inputFile, String outputFile) {
    PrintStream out;
    ReaderTokenizer rtok;
    // output stream
    if (outputFile == null) {
        out = System.out;
    } else {
        try {
            out = new PrintStream(outputFile);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
    }
    // input stream
    try {
        rtok = new ReaderTokenizer(new FileReader(inputFile));
        rtok.eolIsSignificant(true);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return;
    }
    Date date = new Date();
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss z");
    out.println("% Exported matlab file from '" + inputFile + "', " + dateFormat.format(date));
    int iJoint = 0;
    try {
        String line = readLine(rtok);
        while (rtok.ttype != ReaderTokenizer.TT_EOF) {
            // find joint
            if (line.contains("createNode joint")) {
                String jointName = getMayaName(line);
                String jointParent = getMayaParent(line);
                out.println();
                out.println(jointName + ".parent = " + jointParent + ";");
                line = readLine(rtok);
                while (!line.contains("createNode") && rtok.ttype != ReaderTokenizer.TT_EOF) {
                    if (line.contains("setAttr")) {
                        MayaAttribute attr = new MayaAttribute();
                        getAttribute(line, attr);
                        if (!attr.dashed) {
                            out.println(jointName + "." + attr.name + "= " + attr.attr + ";");
                        }
                    }
                    line = readLine(rtok);
                }
                iJoint++;
                out.println("JointArray(" + iJoint + ").joint=" + jointName + ";");
                out.println("JointArray(" + iJoint + ").name=\'" + jointName + "\';");
            } else {
                line = readLine(rtok);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        out.close();
        return;
    }
    if (out != System.out) {
        out.close();
    }
}
Also used : PrintStream(java.io.PrintStream) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ReaderTokenizer(maspack.util.ReaderTokenizer) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IOException(java.io.IOException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 2 with ReaderTokenizer

use of maspack.util.ReaderTokenizer in project artisynth_core by artisynth.

the class StlReader method readASCII.

public static PolygonalMesh readASCII(PolygonalMesh mesh, Reader reader, double tol) throws IOException {
    ReaderTokenizer rtok = new ReaderTokenizer(reader);
    ArrayList<Point3d> nodeList = new ArrayList<Point3d>();
    ArrayList<ArrayList<Integer>> faceList = new ArrayList<ArrayList<Integer>>();
    rtok.eolIsSignificant(true);
    String solidName = "";
    // read until we find "solid"
    while (rtok.nextToken() != ReaderTokenizer.TT_EOF) {
        if (rtok.ttype == ReaderTokenizer.TT_WORD) {
            String word = rtok.sval.toLowerCase();
            if (word.equals("solid")) {
                rtok.nextToken();
                if (rtok.ttype == ReaderTokenizer.TT_WORD) {
                    solidName = rtok.sval;
                }
                toEOL(rtok);
            } else if (word.equals("facet")) {
                ArrayList<Integer> face = readFace(rtok, nodeList, tol);
                if (face != null) {
                    faceList.add(face);
                }
            } else if (word.equals("endsolid") || word.equals("end")) {
                boolean setMeshName = true;
                if (mesh != null) {
                    setMeshName = false;
                }
                mesh = buildMesh(mesh, nodeList, faceList);
                if (setMeshName) {
                    mesh.setName(solidName);
                }
                return mesh;
            }
        }
    }
    return null;
}
Also used : Point3d(maspack.matrix.Point3d) ReaderTokenizer(maspack.util.ReaderTokenizer) ArrayList(java.util.ArrayList)

Example 3 with ReaderTokenizer

use of maspack.util.ReaderTokenizer in project artisynth_core by artisynth.

the class WavefrontReader method parseMaterialFile.

private void parseMaterialFile(String matFileName) throws IOException {
    File parentFile = new File(currPath);
    File matFile = new File(parentFile, matFileName);
    if (!matFile.canRead() && verbose) {
        System.out.println("WavefrontReader warning: can't read material file '" + matFile + "'; ignoring");
    }
    ReaderTokenizer rtok = new ReaderTokenizer(new BufferedReader(new FileReader(matFile)));
    myCurrentProps = null;
    rtok.commentChar('#');
    rtok.eolIsSignificant(true);
    // fast-forward to first definition
    nextToken(rtok);
    RenderProps newProps = null;
    String matName = null;
    while (rtok.ttype != ReaderTokenizer.TT_EOF) {
        if (rtok.ttype == ReaderTokenizer.TT_WORD) {
            if (rtok.sval.equals("newmtl")) {
                break;
            }
        }
        toNextLine(rtok);
    }
    while (rtok.ttype != ReaderTokenizer.TT_EOF) {
        if (rtok.ttype == ReaderTokenizer.TT_WORD) {
            if (rtok.sval.equals("newmtl")) {
                // save old
                if (newProps != null) {
                    // this doesn't work
                    myMaterialMap.put(matName, newProps);
                }
                matName = scanName(rtok);
                newProps = new RenderProps();
            // System.out.println ("found new material '"+matName+"'");
            } else {
                processMaterialLine(rtok, newProps);
            }
            toNextLine(rtok);
        } else {
            if (rtok.ttype != ReaderTokenizer.TT_EOL) {
                throw new IOException("unexpected token " + rtok.ttype + " in material file " + matFile + ", line " + rtok.lineno());
            } else {
                // move to next token
                nextToken(rtok);
            }
        }
    }
    if (newProps != null) {
        // add final render props
        myMaterialMap.put(matName, newProps);
    }
    rtok.close();
}
Also used : ReaderTokenizer(maspack.util.ReaderTokenizer) BufferedReader(java.io.BufferedReader) RenderProps(maspack.render.RenderProps) FileReader(java.io.FileReader) IOException(java.io.IOException) File(java.io.File)

Example 4 with ReaderTokenizer

use of maspack.util.ReaderTokenizer in project artisynth_core by artisynth.

the class WavefrontReader method parse.

public void parse() throws IOException {
    ReaderTokenizer rtok = myRtok;
    boolean savedEol = rtok.getEolIsSignificant();
    int savePound = rtok.getCharSetting('#');
    int saveSlash = rtok.getCharSetting('/');
    rtok.commentChar('#');
    rtok.ordinaryChar('/');
    rtok.eolIsSignificant(true);
    while (nextToken(rtok) != ReaderTokenizer.TT_EOF) {
        if (rtok.ttype == ReaderTokenizer.TT_WORD) {
            if (rtok.sval.equals("EOF")) {
                break;
            }
            if (!processLine(rtok)) {
                // flush to EOL
                toEOL(rtok);
            }
        } else if (rtok.ttype != ReaderTokenizer.TT_EOL) {
            throw new IOException("unexpected token " + rtok.ttype + " , line " + rtok.lineno());
        }
    }
    rtok.eolIsSignificant(savedEol);
    rtok.setCharSetting('#', savePound);
    rtok.setCharSetting('/', saveSlash);
    myInputHasBeenParsed = true;
}
Also used : ReaderTokenizer(maspack.util.ReaderTokenizer) IOException(java.io.IOException)

Example 5 with ReaderTokenizer

use of maspack.util.ReaderTokenizer in project artisynth_core by artisynth.

the class XyzReader method read.

public PointMesh read(PointMesh mesh, InputStream in) throws IOException {
    ReaderTokenizer rtok = new ReaderTokenizer(new BufferedReader(new InputStreamReader(in)));
    if (mesh == null) {
        mesh = new PointMesh();
    }
    mesh.clear();
    ArrayList<Point3d> vtxs = new ArrayList<Point3d>();
    ArrayList<Vector3d> nrms = new ArrayList<Vector3d>();
    boolean done = false;
    while (!done) {
        try {
            double vx = rtok.scanNumber();
            double vy = rtok.scanNumber();
            double vz = rtok.scanNumber();
            double nx = rtok.scanNumber();
            double ny = rtok.scanNumber();
            double nz = rtok.scanNumber();
            vtxs.add(new Point3d(vx, vy, vz));
            nrms.add(new Vector3d(nx, ny, nz));
        } catch (EOFException e) {
            done = true;
        }
    }
    mesh.set(vtxs.toArray(new Point3d[0]), nrms.toArray(new Vector3d[0]));
    return mesh;
}
Also used : PointMesh(maspack.geometry.PointMesh) Vector3d(maspack.matrix.Vector3d) Point3d(maspack.matrix.Point3d) ReaderTokenizer(maspack.util.ReaderTokenizer) ArrayList(java.util.ArrayList)

Aggregations

ReaderTokenizer (maspack.util.ReaderTokenizer)44 IOException (java.io.IOException)19 Point3d (maspack.matrix.Point3d)19 BufferedReader (java.io.BufferedReader)14 ArrayList (java.util.ArrayList)14 FileReader (java.io.FileReader)13 StringReader (java.io.StringReader)7 VectorNd (maspack.matrix.VectorNd)6 FileNotFoundException (java.io.FileNotFoundException)5 PolygonalMesh (maspack.geometry.PolygonalMesh)5 InputStreamReader (java.io.InputStreamReader)4 Vector3d (maspack.matrix.Vector3d)4 NumberFormat (maspack.util.NumberFormat)4 PrintStream (java.io.PrintStream)3 Date (java.util.Date)3 TestException (maspack.util.TestException)3 File (java.io.File)2 PrintWriter (java.io.PrintWriter)2 DateFormat (java.text.DateFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2