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;
}
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());
}
}
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);
}
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);
}
}
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);
}
}
Aggregations