Search in sources :

Example 41 with ReaderTokenizer

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

the class SimpleFemReader method read.

public FemModel3d read(FemModel3d fem, String fileName, Vector3d scale) throws IOException {
    try {
        ReaderTokenizer rtok = new ReaderTokenizer(new FileReader(fileName));
        fem = read(fem, rtok, scale);
        rtok.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    return fem;
}
Also used : ReaderTokenizer(maspack.util.ReaderTokenizer) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader)

Example 42 with ReaderTokenizer

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

the class TetGenReader method read.

public static FemModel3d read(FemModel3d model, double density, Vector3d scale, Reader nodeReader, Reader elemReader) throws IOException {
    if (model == null) {
        model = new FemModel3d();
    }
    ReaderTokenizer nodeFile = new ReaderTokenizer(new BufferedReader(nodeReader));
    model.setDensity(density);
    nodeFile.nextToken();
    nodeFile.nextToken();
    nodeFile.nextToken();
    nodeFile.nextToken();
    while (nodeFile.nextToken() != ReaderTokenizer.TT_EOF) {
        if (!nodeFile.tokenIsInteger()) {
            throw new IOException("Expecting node index, got " + nodeFile);
        }
        // int index = (int)nodeFile.lval;
        Point3d coords = new Point3d();
        for (int i = 0; i < 3; i++) {
            coords.set(i, nodeFile.scanNumber());
        }
        // System.out.println(coords);
        if (scale != null) {
            coords.x *= scale.x;
            coords.y *= scale.y;
            coords.z *= scale.z;
        }
        model.addNode(new FemNode3d(coords));
    }
    ReaderTokenizer elemFile = new ReaderTokenizer(new BufferedReader(elemReader));
    elemFile.nextToken();
    elemFile.nextToken();
    elemFile.nextToken();
    int indexBase = -1;
    while (elemFile.nextToken() != ReaderTokenizer.TT_EOF) {
        if (!elemFile.tokenIsInteger()) {
            throw new IOException("Expecting element index, got " + elemFile);
        }
        if (indexBase == -1) {
            indexBase = (elemFile.nval == 1.0 ? 1 : 0);
        }
        // int index = (int)elemFile.lval;
        int[] idxs = new int[4];
        for (int i = 0; i < 4; i++) {
            idxs[i] = elemFile.scanInteger() - indexBase;
        // System.out.print(idxs[i] + " ");
        }
        // System.out.println();
        FemNode3d n0 = model.getNode(idxs[0]);
        FemNode3d n1 = model.getNode(idxs[1]);
        FemNode3d n2 = model.getNode(idxs[2]);
        FemNode3d n3 = model.getNode(idxs[3]);
        // check to make sure that the tet is defined so that the
        // first three nodes are arranged clockwise about their face
        TetElement tet;
        if (TetElement.computeVolume(n0, n1, n2, n3) >= 0) {
            tet = new TetElement(n0, n1, n2, n3);
        } else {
            tet = new TetElement(n0, n2, n1, n3);
        }
        model.addElement(tet);
    }
    return model;
}
Also used : Point3d(maspack.matrix.Point3d) ReaderTokenizer(maspack.util.ReaderTokenizer)

Example 43 with ReaderTokenizer

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

the class PlyReader method readMesh.

public MeshBase readMesh(MeshBase mesh) throws IOException {
    DataInputStream is = new DataInputStream(myIstream);
    parseHeader(is);
    ArrayList<Point3d> verts = new ArrayList<Point3d>();
    ArrayList<Vector3d> nrmls = new ArrayList<Vector3d>();
    ArrayList<int[]> faces = new ArrayList<int[]>();
    if (myDataFormat == DataFormat.ASCII) {
        ReaderTokenizer rtok = new ReaderTokenizer(new BufferedReader(new InputStreamReader(myIstream)));
        readVertexInfo(rtok, verts, nrmls);
        readFaceInfo(rtok, faces);
    } else {
        BinaryInputStream bis = new BinaryInputStream(new BufferedInputStream(myIstream));
        if (myDataFormat == DataFormat.BINARY_LITTLE_ENDIAN) {
            bis.setLittleEndian(true);
        }
        readVertexInfo(bis, verts, nrmls);
        readFaceInfo(bis, faces);
    }
    if (mesh == null) {
        if (myNumFaces == 0) {
            mesh = new PointMesh();
        } else {
            mesh = new PolygonalMesh();
        }
    }
    if (mesh instanceof PolygonalMesh) {
        PolygonalMesh pmesh = (PolygonalMesh) mesh;
        int icnt = 0;
        for (Point3d pnt : verts) {
            pmesh.addVertex(pnt);
        }
        for (int[] idxs : faces) {
            pmesh.addFace(idxs);
            icnt += idxs.length;
        }
        if (nrmls.size() > 0) {
            // we have to assume here the there is one normal per vertex,
            // and assign the normal indices accordingly]
            int k = 0;
            int[] normalIndices = new int[icnt];
            for (int i = 0; i < faces.size(); i++) {
                int[] idxs = pmesh.getFaces().get(i).getVertexIndices();
                for (int j = 0; j < idxs.length; j++) {
                    normalIndices[k++] = idxs[j];
                }
            }
            pmesh.setNormals(nrmls, normalIndices);
            pmesh.setHardEdgesFromNormals();
        }
    } else if (mesh instanceof PointMesh) {
        PointMesh pmesh = (PointMesh) mesh;
        pmesh.set(verts.toArray(new Point3d[0]), nrmls.toArray(new Vector3d[0]));
    } else {
        throw new UnsupportedOperationException("Mesh type " + mesh.getClass() + " not supported by this reader");
    }
    return mesh;
}
Also used : InputStreamReader(java.io.InputStreamReader) BinaryInputStream(maspack.util.BinaryInputStream) ArrayList(java.util.ArrayList) DataInputStream(java.io.DataInputStream) PolygonalMesh(maspack.geometry.PolygonalMesh) PointMesh(maspack.geometry.PointMesh) Vector3d(maspack.matrix.Vector3d) BufferedInputStream(java.io.BufferedInputStream) Point3d(maspack.matrix.Point3d) ReaderTokenizer(maspack.util.ReaderTokenizer) BufferedReader(java.io.BufferedReader)

Example 44 with ReaderTokenizer

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

the class VtkAsciiReader method read.

public static PolygonalMesh read(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(false);
    // read until we find a dataset
    while (rtok.nextToken() != ReaderTokenizer.TT_EOF) {
        if (rtok.ttype == ReaderTokenizer.TT_WORD) {
            if (rtok.sval.equalsIgnoreCase("DATASET")) {
                rtok.nextToken();
                String dataType = rtok.sval;
                if (dataType.equalsIgnoreCase("POLYDATA")) {
                    parsePolyData(rtok, nodeList, faceList);
                    return buildMesh(mesh, nodeList, faceList);
                } else {
                    System.err.println("Error: unknown dataset type '" + dataType + "'");
                }
            }
        }
    }
    return null;
}
Also used : 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