Search in sources :

Example 31 with ReaderTokenizer

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

the class AnsysReader method readElemFileOld.

protected static LinkedHashMap<Integer, ArrayList<Integer>> readElemFileOld(Reader elemReader, boolean useAnsysNum) throws IOException {
    LinkedHashMap<Integer, ArrayList<Integer>> elemPositions = new LinkedHashMap<Integer, ArrayList<Integer>>();
    ReaderTokenizer rtok = new ReaderTokenizer(new BufferedReader(elemReader));
    rtok.eolIsSignificant(true);
    int offset = useAnsysNum ? 0 : -1;
    int elemId = 0;
    while (rtok.nextToken() != ReaderTokenizer.TT_EOF) {
        rtok.pushBack();
        ArrayList<Integer> curLine = new ArrayList<Integer>();
        ArrayList<Integer> elemNumList;
        int nextToken = rtok.nextToken();
        while (nextToken != ReaderTokenizer.TT_EOL && nextToken != ReaderTokenizer.TT_EOF) {
            curLine.add((int) rtok.nval);
            nextToken = rtok.nextToken();
        }
        if (curLine.size() == 14) {
            elemNumList = new ArrayList<Integer>();
            for (int i = 0; i < 8; i++) {
                elemNumList.add(curLine.get(i) + offset);
            }
            for (int i = 8; i < 13; i++) {
                elemNumList.add(0, curLine.get(i));
            }
            elemId = curLine.get(13);
        } else {
            elemNumList = elemPositions.get(elemId + offset);
            for (int i = 0; i < curLine.size(); i++) {
                elemNumList.add(curLine.get(i) + offset);
            }
        }
        elemPositions.put(elemId + offset, elemNumList);
    }
    return elemPositions;
}
Also used : ArrayList(java.util.ArrayList) ReaderTokenizer(maspack.util.ReaderTokenizer) BufferedReader(java.io.BufferedReader) LinkedHashMap(java.util.LinkedHashMap)

Example 32 with ReaderTokenizer

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

the class TextTest method build.

@Override
public void build(String[] args) throws IOException {
    super.build(args);
    final HashMap<String, Font> fontMap = new HashMap<>();
    for (Font font : GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts()) {
        fontMap.put(font.getName(), font);
    }
    TextComponent3d text = new TextComponent3d("hello world");
    text.setFont(new Font(Font.SANS_SERIF, 0, 54));
    text.setText("Hello world! And goodnight to all.");
    text.setPosition(new Point3d(0.4f, 0.3f, 0.3f));
    text.setTextSize(0.25);
    text.setTextColor(Color.WHITE);
    text.setFollowEye(false);
    RenderProps.setFaceStyle(text, FaceStyle.FRONT_AND_BACK);
    addRenderable(text);
    text = new TextComponent3d("alphabet");
    text.setFont(new Font(Font.SANS_SERIF, 0, 54));
    text.setText("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
    text.setPosition(new Point3d(-2f, 0f, 0f));
    text.setTextSize(0.25);
    text.setTextColor(Color.ORANGE);
    text.setFollowEye(false);
    RenderProps.setFaceStyle(text, FaceStyle.FRONT_AND_BACK);
    addRenderable(text);
    Font comic = fontMap.get("Comic Sans MS");
    if (comic == null) {
        comic = new Font(Font.MONOSPACED, Font.BOLD, 32);
    } else {
        comic = comic.deriveFont(Font.BOLD, 32);
    }
    text = new TextComponent3d("mikey");
    text.setFont(new Font(Font.SANS_SERIF, 0, 54));
    text.setText("Cowabunga");
    text.setPosition(new Point3d(0, 0.5f, 0f));
    text.setTextSize(0.25);
    text.setTextColor(Color.CYAN);
    text.setHorizontalAlignment(HorizontalAlignment.CENTRE);
    text.setFollowEye(false);
    RenderProps.setFaceStyle(text, FaceStyle.FRONT_AND_BACK);
    RenderProps.setShading(text, Shading.NONE);
    addRenderable(text);
    text = new TextComponent3d("flip");
    text.setFont(new Font(Font.SERIF, 0, 64));
    text.setText("Flip me!");
    text.setPosition(new Point3d(0, -0.44f, 0f));
    text.setOrientation(new AxisAngle(0, 1, 0, Math.toRadians(30)));
    text.setTextSize(0.3);
    text.setTextColor(Color.MAGENTA);
    text.setHorizontalAlignment(HorizontalAlignment.CENTRE);
    text.setFollowEye(false);
    RenderProps.setFaceStyle(text, FaceStyle.FRONT);
    addRenderable(text);
    text = new TextComponent3d("flip2");
    text.setFont(new Font(Font.SERIF, 0, 64));
    text.setText("Flip me!");
    text.setPosition(new Point3d(0, -0.44f, 0f));
    text.setOrientation(new AxisAngle(0, 1, 0, Math.toRadians(210)));
    text.setTextSize(0.3);
    text.setTextColor(Color.MAGENTA);
    text.setHorizontalAlignment(HorizontalAlignment.CENTRE);
    text.setFollowEye(false);
    RenderProps.setFaceStyle(text, FaceStyle.FRONT);
    addRenderable(text);
    PrintWriter writer = new PrintWriter(ArtisynthPath.getTempDir().getAbsolutePath() + "/texttest.txt");
    write(writer, new NumberFormat("%g"), null);
    writer.close();
    ArrayDeque<ScanToken> tokens = new ArrayDeque<>();
    ReaderTokenizer rtok = new ReaderTokenizer(new FileReader(ArtisynthPath.getTempDir().getAbsolutePath() + "/texttest.txt"));
    scan(rtok, tokens);
    rtok.close();
}
Also used : HashMap(java.util.HashMap) Font(java.awt.Font) ArrayDeque(java.util.ArrayDeque) TextComponent3d(artisynth.core.renderables.TextComponent3d) AxisAngle(maspack.matrix.AxisAngle) ScanToken(artisynth.core.util.ScanToken) Point3d(maspack.matrix.Point3d) ReaderTokenizer(maspack.util.ReaderTokenizer) FileReader(java.io.FileReader) PrintWriter(java.io.PrintWriter) NumberFormat(maspack.util.NumberFormat)

Example 33 with ReaderTokenizer

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

the class MayaToWavefront method doFiberExport.

public static void doFiberExport(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 OBJ file from '" + inputFile + "', " + dateFormat.format(date));
    out.println();
    int nVertices = 0;
    try {
        while (rtok.ttype != ReaderTokenizer.TT_EOF) {
            String line = readLine(rtok);
            // find nurbsCurve
            if (line.contains("createNode nurbsCurve")) {
                String curveName = getMayaName(line);
                // throw away lines starting with setAttr
                rtok.nextToken();
                while (rtok.ttype == ReaderTokenizer.TT_WORD || rtok.ttype == ReaderTokenizer.TT_EOL) {
                    toEOL(rtok);
                    if (rtok.ttype == ReaderTokenizer.TT_EOL) {
                        rtok.nextToken();
                    }
                }
                // throw away next line (form: 1 17 0 no 3)
                toEOL(rtok);
                // read indexing
                int nSkip = (int) (rtok.scanNumber());
                int skipped = 0;
                // throw away numbers
                while (skipped < nSkip) {
                    rtok.nextToken();
                    if (rtok.ttype == ReaderTokenizer.TT_NUMBER) {
                        skipped++;
                    } else if (rtok.ttype != ReaderTokenizer.TT_EOL) {
                        System.err.println("Error: unexpected token " + rtok.tokenName() + " on line " + rtok.lineno());
                        return;
                    }
                }
                // read entire curve
                int nPoints = (int) (scanNumber(rtok));
                ArrayList<Point3d> fiber = new ArrayList<Point3d>();
                double[] pnt = new double[3];
                for (int i = 0; i < nPoints; i++) {
                    int nRead = scanNumbers(rtok, pnt, 3);
                    if (nRead < 3) {
                        System.err.println("Error: cannot read coordinate on line " + rtok.lineno());
                        return;
                    }
                    fiber.add(new Point3d(pnt));
                }
                // print fiber to file
                String fiberVertices = "l";
                if (curveName != null) {
                    out.println("# " + curveName);
                }
                for (Point3d vtx : fiber) {
                    nVertices++;
                    fiberVertices += " " + nVertices;
                    out.printf("v %f %f %f\n", vtx.x, vtx.y, vtx.z);
                }
                out.println(fiberVertices);
                out.println();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        out.close();
        return;
    }
    if (out != System.out) {
        out.close();
    }
}
Also used : PrintStream(java.io.PrintStream) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Date(java.util.Date) Point3d(maspack.matrix.Point3d) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ReaderTokenizer(maspack.util.ReaderTokenizer) FileReader(java.io.FileReader) SimpleDateFormat(java.text.SimpleDateFormat)

Example 34 with ReaderTokenizer

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

the class MayaAsciiReader method doRead.

public static void doRead(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));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return;
    }
    MayaAsciiReader reader = new MayaAsciiReader();
    try {
        reader.read(rtok);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        rtok.close();
    }
    out.print(reader.tree.toString());
    if (out != System.out) {
        out.close();
    }
}
Also used : PrintStream(java.io.PrintStream) ReaderTokenizer(maspack.util.ReaderTokenizer) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IOException(java.io.IOException)

Example 35 with ReaderTokenizer

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

the class GtsReader method readMesh.

public PolygonalMesh readMesh(PolygonalMesh mesh) throws IOException {
    if (mesh == null) {
        mesh = new PolygonalMesh();
    } else {
        mesh.clear();
    }
    ReaderTokenizer rtok = new ReaderTokenizer(new BufferedReader(new InputStreamReader(myIstream)));
    rtok.commentChar('!');
    rtok.commentChar('#');
    rtok.eolIsSignificant(true);
    int numVerts = rtok.scanInteger();
    int numEdges = rtok.scanInteger();
    int numFaces = rtok.scanInteger();
    while (rtok.nextToken() != ReaderTokenizer.TT_EOL) ;
    ArrayList<Edge> edgeList = new ArrayList<Edge>();
    for (int i = 0; i < numVerts; i++) {
        double x = rtok.scanNumber();
        double y = rtok.scanNumber();
        double z = rtok.scanNumber();
        mesh.addVertex(new Point3d(x, y, z));
        while (rtok.nextToken() != ReaderTokenizer.TT_EOL) ;
    }
    ArrayList<Vertex3d> verts = mesh.getVertices();
    for (int i = 0; i < numEdges; i++) {
        int idx1 = rtok.scanInteger() - 1;
        int idx2 = rtok.scanInteger() - 1;
        if (idx1 < 0 || idx1 >= numVerts) {
            System.out.println("Error: vertex " + idx1 + " out of range, " + rtok);
            System.exit(1);
        }
        if (idx2 < 0 || idx2 >= numVerts) {
            System.out.println("Error: vertex " + idx2 + " out of range, " + rtok);
            System.exit(1);
        }
        edgeList.add(new Edge(verts.get(idx1), verts.get(idx2)));
        while (rtok.nextToken() != ReaderTokenizer.TT_EOL) ;
    }
    for (int i = 0; i < numFaces; i++) {
        ArrayList<Edge> edges = new ArrayList<Edge>();
        while (rtok.nextToken() == ReaderTokenizer.TT_NUMBER) {
            if (!rtok.tokenIsInteger()) {
                System.out.println("Error: edge index expected, " + rtok);
                System.exit(1);
            }
            int idx = (int) rtok.lval - 1;
            if (idx < 0 || idx >= edgeList.size()) {
                System.out.println("Error: edge index " + idx + " out of range, " + rtok);
                System.exit(1);
            }
            edges.add(edgeList.get(idx));
        }
        int[] idxs = getFaceIndices(edges);
        if (idxs == null) {
            System.out.println("Error: face edges are not adjacent, " + rtok);
        }
        mesh.addFace(idxs);
        rtok.pushBack();
        while (rtok.nextToken() != ReaderTokenizer.TT_EOL) ;
    }
    return mesh;
}
Also used : Vertex3d(maspack.geometry.Vertex3d) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) PolygonalMesh(maspack.geometry.PolygonalMesh) Point3d(maspack.matrix.Point3d) ReaderTokenizer(maspack.util.ReaderTokenizer) BufferedReader(java.io.BufferedReader)

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