Instantly share code, notes, and snippets.

Embed
What would you like to do?
File Reader for 2d triangle grids with optional vertex and triangle marks vor VRL Studio by Michael Hoffer
package eu.mihosoft.vrl.user;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collectors;
/**
* A simple 2d file reader for VRL-Studio.
*
* # File format:
* # - lines starting with '#' are ignored (just like this comment)
* # - first line contains two entries:
* # the number of vertices and the number of triangles
* # - then follow the vertices and the triangles
* #
* # Now follows the actual content with three vertices and one triangle:
* 3 1
* 0.0 0.0
* 5.0 0.0
* 0.0 5.0
* 0 1 2
*
* @author Michael Hoffer <info@michaelhoffer.de>
*/
@ComponentInfo(name="File2DReader", category="Custom")
public class File2DReader implements java.io.Serializable {
private static final long serialVersionUID=1L;
// ---- add your code here -----
@OutputInfo(name="2df File Content", style="multi-out", options="",
elemTypes=[double[][].class, int[][].class, int[].class, int[].class],
elemNames=["Vertices", "Triangles", "Vertex Marks", "Triangle Marks"],
elemOptions=["serialization=false","serialization=false","serialization=false","serialization=false"])
public Object[] load(
@ParamInfo(name="2df File", style="load-dialog", options="endings=[\".2df\"]; description=\"*.2df-Files\"") File input) {
// read lines and strip comments as well as empty lines
def lines = Files.readAllLines(input.toPath()).
stream().map{l->l.trim()}.
filter{l->!l.startsWith("#")&&!l.isEmpty()}.
collect(Collectors.toList());
// check that we have content
if(lines.size() < 3) {
throw new RuntimeException("File '$input' is invalid: it contains less than 3 lines.")
}
// read num-verts and num-tris
String[] numEntries = lines[0].split("\\s");
if(numEntries.length < 2) {
throw new RuntimeException("File '$input' is invalid: number of verts and/or number of tris unspecified")
}
// read number of vertices
int numVerts = 0;
try{
numVerts = Integer.parseInt(numEntries[0]);
} catch(Exception ex) {
throw new RuntimeException("File '$input' is invalid: number of verts does not specify an integer value")
}
// read number of triangles
int numTris = 0;
try{
numTris = Integer.parseInt(numEntries[1]);
} catch(Exception ex) {
throw new RuntimeException("File '$input' is invalid: number of tris does not specify an integer value")
}
int curLine = 1;
// read vertices (x,y)
double[][] vertices = new double[numVerts][];
for(int i = 0; i < numVerts; i++) {
String[] numEntriesVerts = lines[i + curLine].split("\\s");
if(numEntriesVerts.length < 2) {
throw new RuntimeException("File '$input' is invalid: number of vert coordinates in vertex " + (i-curLine))
}
double x = 0;
try{
x = Double.parseDouble(numEntriesVerts[0]);
} catch(Exception ex) {
throw new RuntimeException("File '$input' is invalid: vertex x coord is invalid in vertex " + (i-curLine))
}
double y = 0;
try{
y = Double.parseDouble(numEntriesVerts[1]);
} catch(Exception ex) {
throw new RuntimeException("File '$input' is invalid: vertex y coord is invalid in vertex " + (i-curLine))
}
vertices[i] = new double[2]
vertices[i][0] = x;
vertices[i][1] = y;
}
curLine += numVerts;
// read triangles (v0,v1,v2)
int[][] triangles = new int[numTris][];
for(int i = 0; i < numTris; i++) {
String[] numEntriesTris = lines[i + curLine].split("\\s");
if(numEntriesTris.length < 3) {
throw new RuntimeException("File '$input' is invalid: wrong number of indices in triangle $i")
}
triangles[i] = new int[3]
for(int j = 0; j < 3; j++) {
try{
triangles[i][j] = Integer.parseInt(numEntriesTris[j]);
} catch(Exception ex) {
throw new RuntimeException("File '$input' is invalid: index $j in triangle $i is not valid")
}
}
}
curLine += numTris;
// read vertex marks (optional)
int[] vrtMarks = new int[numVerts];
for(int i = 0; (i < numVerts) && (i + curLine < lines.size()); i++) {
String[] numEntriesVrtMarks = lines[i + curLine].split("\\s");
if(numEntriesVrtMarks.length == 1) {
try{
vrtMarks[i] = Integer.parseInt(numEntriesVrtMarks[0]);
} catch(Exception ex) {
throw new RuntimeException("File '$input' is invalid: vertex mark $i is not valid")
}
}
if(numEntriesVrtMarks.length > 1) {
throw new RuntimeException("File '$input' is invalid: wrong number of indices in vertex mark $i")
}
}
curLine += numVerts
// read triangle marks (optional)
int[] triMarks = new int[numTris];
for(int i = 0; (i < numTris) && (i + curLine < lines.size()); i++) {
String[] numEntriesTriMarks = lines[i + curLine].split("\\s");
if(numEntriesTriMarks.length == 1) {
try{
triMarks[i] = Integer.parseInt(numEntriesTriMarks[0]);
} catch(Exception ex) {
throw new RuntimeException("File '$input' is invalid: triangle mark $i is not valid")
}
}
if(numEntriesTriMarks.length > 1) {
throw new RuntimeException("File '$input' is invalid: wrong number of indices in triangle mark $i")
}
}
curLine += numVerts
return [vertices, triangles, vrtMarks, triMarks]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment