Search in sources :

Example 21 with ReaderTokenizer

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

the class AmiraLandmarkReader method readSets.

/**
 * Creates an 2D array of 3D points from the landmark data from a Reader
 *
 * @param reader
 * reader from which to read amira landmark data
 * @param scale
 * factor by which node coordinate data should be scaled
 * @return list of points read from file
 * @throws IOException
 * if this is a problem reading the file
 */
public static Point3d[][] readSets(Reader reader, double scale) throws IOException {
    Point3d[][] ptarray = null;
    int numSets = 0, length = 0;
    ReaderTokenizer rtok = new ReaderTokenizer(new BufferedReader(reader));
    rtok.wordChars("./@");
    String prevToken;
    while (rtok.nextToken() != ReaderTokenizer.TT_EOF) {
        if (numSets == 0 || length == 0) {
            if (rtok.ttype == ReaderTokenizer.TT_WORD) {
                prevToken = rtok.sval;
                rtok.nextToken();
                if (rtok.ttype != ReaderTokenizer.TT_NUMBER) {
                    // not at data yet, look for next "@1" word
                    rtok.pushBack();
                    continue;
                } else {
                    if (prevToken.compareTo("Markers") == 0) {
                        length = (int) rtok.nval;
                    } else if (prevToken.compareTo("NumSets") == 0) {
                        numSets = (int) rtok.nval;
                    }
                }
            }
        } else {
            if (ptarray == null) {
                ptarray = new Point3d[numSets][length];
            }
            if (rtok.ttype == ReaderTokenizer.TT_WORD && rtok.sval.matches("@[\\d]+")) {
                int setIdx = Integer.parseInt(rtok.sval.replace("@", "")) - 1;
                rtok.nextToken();
                if (rtok.ttype != ReaderTokenizer.TT_NUMBER) {
                    // not at data yet, look for next "@1" word
                    rtok.pushBack();
                    continue;
                } else {
                    // we have found landmark data - scan into
                    rtok.pushBack();
                    // Point3ds until the next set
                    int count = 0;
                    while (rtok.nextToken() != ReaderTokenizer.TT_EOF && rtok.ttype != ReaderTokenizer.TT_WORD) {
                        rtok.pushBack();
                        Point3d point = new Point3d();
                        point.scan(rtok);
                        if (!point.equals(new Point3d(0, 0, 0))) {
                            point.scale(scale);
                            ptarray[setIdx][count] = point;
                            count++;
                        }
                    }
                    rtok.pushBack();
                }
            }
        }
    }
    return ptarray;
}
Also used : Point3d(maspack.matrix.Point3d) ReaderTokenizer(maspack.util.ReaderTokenizer) BufferedReader(java.io.BufferedReader)

Example 22 with ReaderTokenizer

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

the class ClassAliases method initializeFromFile.

protected static void initializeFromFile(File file) throws IOException {
    try {
        ReaderTokenizer rtok = new ReaderTokenizer(new BufferedReader(new FileReader(file)));
        rtok.commentChar('#');
        rtok.wordChar('/');
        rtok.wordChar('.');
        rtok.wordChar('$');
        rtok.wordChar('_');
        rtok.wordChar('-');
        String alias = null;
        while (rtok.nextToken() != ReaderTokenizer.TT_EOF && rtok.tokenIsWordOrQuotedString('"')) {
            if (alias == null) {
                alias = rtok.sval;
            } else {
                Class<?> cls;
                try {
                    cls = Class.forName(rtok.sval);
                } catch (Exception e) {
                    System.err.println("Error: class \"" + rtok.sval + "\" in file " + file.getAbsolutePath() + " cannot be located");
                    cls = null;
                }
                if (cls != null) {
                    doAddEntry(alias, cls);
                }
            }
        }
        if (rtok.ttype != ReaderTokenizer.TT_EOF) {
            throw new IOException("Warning: unknown token " + rtok.tokenName() + " in file " + file.getAbsolutePath());
        }
    } catch (Exception e) {
        throw new IOException("Warning: error reading file " + file.getAbsolutePath() + "\n" + e.getMessage());
    }
}
Also used : ReaderTokenizer(maspack.util.ReaderTokenizer) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) IOException(java.io.IOException)

Example 23 with ReaderTokenizer

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

the class VariableVectorField method textToValue.

public Object textToValue(String text, BooleanHolder corrected, StringHolder errMsg) {
    corrected.value = false;
    if (isBlank(text)) {
        return setVoidIfPossible(errMsg);
    }
    ReaderTokenizer rtok = new ReaderTokenizer(new StringReader(text));
    VectorNd tmp = new VectorNd(myVectorSize);
    int idx = 0;
    try {
        while (rtok.nextToken() != ReaderTokenizer.TT_EOF) {
            if (idx == tmp.size()) {
                tmp.adjustSize(5);
            }
            if (rtok.ttype == ReaderTokenizer.TT_NUMBER) {
                tmp.set(idx++, rtok.nval);
            } else {
                return illegalValue("Malformed number for element " + idx, errMsg);
            }
        }
    } catch (Exception e) {
        return illegalValue("Error parsing text", errMsg);
    }
    tmp.setSize(idx);
    return validValue(tmp, errMsg);
}
Also used : ReaderTokenizer(maspack.util.ReaderTokenizer) StringReader(java.io.StringReader) VectorNd(maspack.matrix.VectorNd)

Example 24 with ReaderTokenizer

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

the class VectorField method textToValue.

public Object textToValue(String text, BooleanHolder corrected, StringHolder errMsg) {
    corrected.value = false;
    if (isBlank(text)) {
        return setVoidIfPossible(errMsg);
    }
    ReaderTokenizer rtok = new ReaderTokenizer(new StringReader(text));
    VectorNd tmp = new VectorNd(myVectorSize);
    try {
        for (int i = 0; i < myVectorSize; i++) {
            if (rtok.nextToken() != ReaderTokenizer.TT_NUMBER) {
                if (rtok.ttype == ReaderTokenizer.TT_EOF) {
                    return illegalValue("Missing elements (there should be " + myVectorSize + ")", errMsg);
                } else {
                    return illegalValue("Malformed number for element " + i, errMsg);
                }
            }
            tmp.set(i, rtok.nval);
        }
        if (rtok.nextToken() != ReaderTokenizer.TT_EOF) {
            if (rtok.ttype == ReaderTokenizer.TT_NUMBER) {
                return illegalValue("Too many elements (there should be " + myVectorSize + ")", errMsg);
            } else {
                return illegalValue("Extra characters after last element", errMsg);
            }
        }
        return validValue(tmp, errMsg);
    } catch (Exception e) {
        return illegalValue("Improperly formed vector", errMsg);
    }
}
Also used : ReaderTokenizer(maspack.util.ReaderTokenizer) StringReader(java.io.StringReader) VectorNd(maspack.matrix.VectorNd)

Example 25 with ReaderTokenizer

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

the class VectoriField method textToValue.

public Object textToValue(String text, BooleanHolder corrected, StringHolder errMsg) {
    corrected.value = false;
    if (isBlank(text)) {
        return setVoidIfPossible(errMsg);
    }
    ReaderTokenizer rtok = new ReaderTokenizer(new StringReader(text));
    VectorNi tmp = new VectorNi(myVectorSize);
    try {
        for (int i = 0; i < myVectorSize; i++) {
            if (rtok.nextToken() == ReaderTokenizer.TT_EOF) {
                return illegalValue("Missing elements (there should be " + myVectorSize + ")", errMsg);
            } else if (!rtok.tokenIsInteger()) {
                return illegalValue("Malformed integer for element " + i, errMsg);
            }
            tmp.set(i, (int) rtok.nval);
        }
        if (rtok.nextToken() != ReaderTokenizer.TT_EOF) {
            if (rtok.tokenIsInteger()) {
                return illegalValue("Too many elements (there should be " + myVectorSize + ")", errMsg);
            } else {
                return illegalValue("Extra characters after last element", errMsg);
            }
        }
        return validValue(tmp, errMsg);
    } catch (Exception e) {
        return illegalValue("Improperly formed vector", errMsg);
    }
}
Also used : ReaderTokenizer(maspack.util.ReaderTokenizer) StringReader(java.io.StringReader) VectorNi(maspack.matrix.VectorNi)

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