Search in sources :

Example 6 with ReaderTokenizer

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

the class OffReader method read.

public static PolygonalMesh read(PolygonalMesh mesh, Reader reader) throws IOException {
    ReaderTokenizer rtok = new ReaderTokenizer(reader);
    ArrayList<Point3d> nodeList = new ArrayList<Point3d>();
    ArrayList<ArrayList<Integer>> faceList = new ArrayList<ArrayList<Integer>>();
    rtok.eolIsSignificant(false);
    int nVerts = 0;
    int nFaces = 0;
    int nEdges = 0;
    // read first info
    rtok.nextToken();
    if (rtok.ttype != ReaderTokenizer.TT_WORD || !rtok.sval.equalsIgnoreCase("OFF")) {
        throw new IOException("Expected 'OFF' at start of file");
    }
    nVerts = rtok.scanInteger();
    nFaces = rtok.scanInteger();
    nEdges = rtok.scanInteger();
    if (nEdges != 0) {
        System.err.println("Separate edges not supported\n");
    }
    double[] vals = new double[3];
    for (int i = 0; i < nVerts; i++) {
        int nread = rtok.scanNumbers(vals, 3);
        if (nread != 3) {
            throw new IOException("Failed to read vertices");
        }
        Point3d pnt = new Point3d(vals[0], vals[1], vals[2]);
        nodeList.add(pnt);
    }
    for (int i = 0; i < nFaces; i++) {
        int nv = rtok.scanInteger();
        ArrayList<Integer> vtxs = new ArrayList<Integer>(nv);
        for (int j = 0; j < nv; j++) {
            int vIdx = rtok.scanInteger();
            vtxs.add(vIdx);
        }
        faceList.add(vtxs);
    }
    return buildMesh(mesh, nodeList, faceList);
}
Also used : Point3d(maspack.matrix.Point3d) ReaderTokenizer(maspack.util.ReaderTokenizer) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 7 with ReaderTokenizer

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

the class OpenSimAscReader method readMesh.

public PolygonalMesh readMesh(PolygonalMesh mesh) throws IOException {
    if (mesh == null) {
        mesh = new PolygonalMesh();
    } else {
        mesh.clear();
    }
    ReaderTokenizer rtok = new ReaderTokenizer(new InputStreamReader(myIstream));
    rtok.eolIsSignificant(false);
    boolean oldFormat = false;
    int nVertices = 0;
    int nFaces = 0;
    double[] tmp = new double[6];
    int nReadVals = 6;
    // read first symbol, which should be NORM_ASCII or a number of vertices
    if (rtok.nextToken() != ReaderTokenizer.TT_WORD) {
        if (rtok.ttype == ReaderTokenizer.TT_NUMBER) {
            oldFormat = true;
            nReadVals = 3;
            nVertices = (int) rtok.nval;
        } else {
            throw new IOException("Expected " + ReaderTokenizer.TT_WORD + " on line " + rtok.lineno());
        }
    }
    if (!oldFormat) {
        String type = rtok.sval;
        if (type.compareTo("NORM_ASCII") != 0) {
            throw new IOException("Unknown file type: " + type);
        }
        if (rtok.nextToken() != ReaderTokenizer.TT_NUMBER) {
            throw new IOException("Expected " + ReaderTokenizer.TT_NUMBER + " on line " + rtok.lineno());
        }
        nVertices = (int) rtok.nval;
    }
    if (rtok.nextToken() != ReaderTokenizer.TT_NUMBER) {
        throw new IOException("Expected " + ReaderTokenizer.TT_NUMBER + " on line " + rtok.lineno());
    }
    nFaces = (int) rtok.nval;
    if (!oldFormat) {
        // bounding box
        int iRead = rtok.scanNumbers(tmp, 6);
        if (iRead != 6) {
            throw new IOException("Expected bounding box on line " + rtok.lineno());
        }
    }
    // load all vertices now
    for (int i = 0; i < nVertices; i++) {
        int nRead = rtok.scanNumbers(tmp, nReadVals);
        if (nRead != nReadVals) {
            throw new IOException("Expected number of values on line " + rtok.lineno());
        }
        mesh.addVertex(tmp[0], tmp[1], tmp[2]);
    }
    // load all faces
    for (int i = 0; i < nFaces; i++) {
        int nV = (int) rtok.scanNumber();
        int[] face = new int[nV];
        if (scanIntegers(rtok, face, nV) != nV) {
            throw new IOException("Expected number of vertices on line " + rtok.lineno());
        }
        if (oldFormat) {
            for (int j = 0; j < nV; j++) {
                // start indices at zero
                face[j] -= 1;
            }
        }
        mesh.addFace(face);
    }
    return mesh;
}
Also used : InputStreamReader(java.io.InputStreamReader) ReaderTokenizer(maspack.util.ReaderTokenizer) IOException(java.io.IOException) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 8 with ReaderTokenizer

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

the class VectorTest method doScanWrite.

void doScanWrite(Vector vr, String fmt, boolean withBrackets) {
    StringWriter sw = new StringWriter();
    try {
        vr.write(new PrintWriter(sw), new NumberFormat(fmt), withBrackets);
        vr.scan(new ReaderTokenizer(new StringReader(sw.toString())));
    } catch (Exception e) {
        throw new TestException("scan/write error: " + e.getMessage());
    }
}
Also used : TestException(maspack.util.TestException) ReaderTokenizer(maspack.util.ReaderTokenizer) TestException(maspack.util.TestException) NumberFormat(maspack.util.NumberFormat)

Example 9 with ReaderTokenizer

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

the class VectoriTest method doScanWrite.

void doScanWrite(Vectori vr, String fmt, boolean withBrackets) {
    StringWriter sw = new StringWriter();
    try {
        vr.write(new PrintWriter(sw), new NumberFormat(fmt), withBrackets);
        vr.scan(new ReaderTokenizer(new StringReader(sw.toString())));
    } catch (Exception e) {
        throw new TestException("scan/write error: " + e.getMessage());
    }
}
Also used : TestException(maspack.util.TestException) ReaderTokenizer(maspack.util.ReaderTokenizer) TestException(maspack.util.TestException) NumberFormat(maspack.util.NumberFormat)

Example 10 with ReaderTokenizer

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

the class LemkeContactSolverTest method main.

public static void main(String[] args) {
    DoubleHolder epsilon = new DoubleHolder(1e-12);
    StringHolder fileName = new StringHolder("contactTest.txt");
    StringHolder testName = new StringHolder("");
    IntHolder debug = new IntHolder(0);
    IntHolder randomInput = new IntHolder(0);
    BooleanHolder vsize = new BooleanHolder(true);
    BooleanHolder reduce = new BooleanHolder(false);
    BooleanHolder smartDirs = new BooleanHolder(false);
    BooleanHolder timing = new BooleanHolder(false);
    IntHolder timingCnt = new IntHolder(1);
    BooleanHolder tradSolver = new BooleanHolder(false);
    BooleanHolder multiBody = new BooleanHolder(false);
    BooleanHolder tradInc = new BooleanHolder(false);
    BooleanHolder verbose = new BooleanHolder(false);
    ArgParser parser = new ArgParser("java contact.LemkeContactSolverTest");
    parser.addOption("-f %s # test file name", fileName);
    parser.addOption("-D %d # debug code", debug);
    parser.addOption("-vsize %v # variable LCP size", vsize);
    parser.addOption("-reduce %v # reducible LCP size", reduce);
    parser.addOption("-timing %v # time the functions", timing);
    parser.addOption("-timingCnt %d # timing count", timingCnt);
    parser.addOption("-trad %v # use traditional solver", tradSolver);
    parser.addOption("-multiBody %v # use multi-body solver", multiBody);
    parser.addOption("-verbose %v # print solution info", verbose);
    parser.addOption("-tradinc %v # use traditional solver with incremental pivoting", tradInc);
    parser.addOption("-random %d # random input cnt", randomInput);
    parser.addOption("-smartDirs %v # smart friction directions", smartDirs);
    parser.addOption("-eps %f # epsilon", epsilon);
    parser.addOption("-name %s # test name", testName);
    parser.matchAllArgs(args);
    if (tradInc.value) {
        tradSolver.value = true;
    }
    LemkeContactSolverTest tester = new LemkeContactSolverTest(tradSolver.value, multiBody.value);
    if (tradInc.value) {
        tester.testName = testName.value + "Tradinc";
    } else if (tradSolver.value) {
        tester.testName = testName.value + "Trad";
    } else if (vsize.value) {
        tester.testName = testName.value + "Vsize";
    } else {
        tester.testName = testName.value + "Reg";
    }
    tester.solver.setDebug(debug.value);
    tester.solver.setEpsilon(epsilon.value);
    if (!tradSolver.value) {
        // if (multiBody.value)
        // { ContactSolverMbX solverx = (ContactSolverMbX)tester.solver;
        // solverx.setVariableSize(vsize.value);
        // // solverx.setReducibleSize(reduce.value);
        // solverx.setSmartDirections(smartDirs.value);
        // }
        // else
        {
            LemkeContactSolver solverx = (LemkeContactSolver) tester.solver;
            solverx.setVariableSize(vsize.value);
            solverx.setReducibleSize(reduce.value);
            solverx.setSmartDirections(smartDirs.value);
        }
    }
    // else
    // { if (tradInc.value)
    // { if (multiBody.value)
    // { ContactSolverMbO solvero = (ContactSolverMbO)tester.solver;
    // solvero.setIncrementalPivoting (true);
    // }
    // else
    // { ContactSolverO solvero = (ContactSolverO)tester.solver;
    // solvero.setIncrementalPivoting (true);
    // }
    // }
    // }
    tester.doTiming = timing.value;
    tester.timingCnt = timingCnt.value;
    tester.randomInputCnt = randomInput.value;
    int cnt = tester.doTiming ? 2 : 1;
    for (int i = 0; i < cnt; i++) {
        try {
            Reader reader = new FileReader(fileName.value);
            ReaderTokenizer rtok = new ReaderTokenizer(reader);
            rtok.parseNumbers(true);
            rtok.commentChar('#');
            tester.parseInput(rtok);
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }
    System.out.println("\nPassed\n");
}
Also used : ReaderTokenizer(maspack.util.ReaderTokenizer) TestException(maspack.util.TestException)

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