Skip to content
Browse files

Adding Daves ASCII STL code

  • Loading branch information...
1 parent 64fe85b commit 1e7401e61283ad61b164a410868defa0872f9c41 @MaskedRetriever committed
Showing with 151 additions and 19 deletions.
  1. +151 −19 Mesh.pde
View
170 Mesh.pde
@@ -3,33 +3,169 @@
class Mesh {
ArrayList Triangles;
float bx1,by1,bz1,bx2,by2,bz2; //bounding box
+ boolean Valid;
float Sink;
//Mesh Loading routine
Mesh(String FileName)
{
- byte b[] = loadBytes(FileName);
+ Valid = false;
+
+ println ("loading file " + FileName + "...");
Triangles = new ArrayList();
Sink=0;
- float[] Tri = new float[9];
- //Skip the header
- int offs = 84;
- //Read each triangle out
- while(offs<b.length){
- offs = offs + 12; //skip the normals entirely!
- for(int i = 0; i<9; i++)
- {
- Tri[i]=bin_to_float(b[offs],b[offs+1],b[offs+2],b[offs+3]);
- offs=offs+4;
+
+ if (LoadTextMesh (FileName))
+ {
+ Valid = true;
+ CalculateBoundingBox();
+ }
+ else if (LoadBinaryMesh (FileName))
+ {
+ Valid = true;
+ CalculateBoundingBox();
+ }
+ }
+
+ private boolean LoadBinaryMesh (String FileName)
+ {
+ try
+ {
+ byte b[] = loadBytes(FileName);
+ float[] Tri = new float[9];
+ //Skip the header
+ int offs = 84;
+ //Read each triangle out
+ while(offs<b.length){
+ offs = offs + 12; //skip the normals entirely!
+ for(int i = 0; i<9; i++)
+ {
+ Tri[i]=bin_to_float(b[offs],b[offs+1],b[offs+2],b[offs+3]);
+ offs=offs+4;
+ }
+ offs=offs+2;//Skip the attribute bytes
+ Triangles.add(new Triangle(Tri[0],Tri[1],Tri[2],Tri[3],Tri[4],Tri[5],Tri[6],Tri[7],Tri[8]));
}
- offs=offs+2;//Skip the attribute bytes
- Triangles.add(new Triangle(Tri[0],Tri[1],Tri[2],Tri[3],Tri[4],Tri[5],Tri[6],Tri[7],Tri[8]));
- }
- CalculateBoundingBox();
+ }
+ catch (Exception e)
+ {
+ println ("Unable to load binary STL");
+ return false;
+ }
+
+ return true;
}
+ private boolean LoadTextMesh (String path)
+ {
+ // text format:
+
+ // solid ascii
+ // facet normal -2.242146e-006 -8.944270e-001 -4.472139e-001
+ // outer loop
+ // vertex 5.000000e+001 3.829179e+001 7.236046e+000
+ // vertex 4.048944e+001 3.690984e+001 1.000000e+001
+ // vertex 4.412215e+001 4.190984e+001 -1.963632e-006
+ // endloop
+ // endfacet
+ // facet normal -8.506515e-001 -2.763928e-001 -4.472125e-001
+ // outer loop
+ // .
+ // .
+ // endfacet
+ // endsolid
+
+ BufferedReader reader;
+ String buf;
+
+ try
+ {
+ reader = createReader (path);
+
+ buf = reader.readLine();
+ if (buf.indexOf ("solid") != 0)
+ {
+ // doesn't appear to be a text stl..
+ return false;
+ }
+ }
+ catch (RuntimeException ex)
+ {
+ // this is (more than..) a bit stupid. java complains if you ask it to open
+ // abc.stl when the file name is ABC.stl. figuring out and using then actual
+ // path somewhere above would be good..
+
+ println ("Unable to read from buffered reader. Check to make sure you gave the EXACT pathname (case matters)");
+ return false;
+ }
+ catch (Exception e)
+ {
+ // file doesn't exist or something.. likely won't fail on binary stl
+ println ("unable to read from buffered reader..");
+ return false;
+ }
+
+ try
+ {
+ while (true)
+ {
+ buf = reader.readLine();
+ if (buf == null || buf.indexOf ("endsolid") == 0)
+ {
+ // end of file or last valid line.. good stuff
+ break;
+ }
+
+ if (buf.indexOf ("facet normal") == -1)
+ {
+ // sanity check..
+ return false;
+ }
+ buf = reader.readLine(); // " outer loop"
+
+ String[] floats;
+ int offset;
+
+ // read in the first triangle.. "vertex " followed by 3 floats.. the
+ // regex string split sometimes returns an extra leading entry with
+ // nothing in it (5 elements in the 'floats' array) and sometimes
+ // doesn't (4 elements) so check the length and offset by 1 if needed
+ floats = reader.readLine().split("[\\s,;]+");
+ offset = floats.length == 5 ? 1 : 0;
+ float x1 = Float.parseFloat (floats[1 + offset]);
+ float y1 = Float.parseFloat (floats[2 + offset]);
+ float z1 = Float.parseFloat (floats[3 + offset]);
+
+ // 2nd triangle
+ floats = reader.readLine().split("[\\s,;]+");
+ offset = floats.length == 5 ? 1 : 0;
+ float x2 = Float.parseFloat (floats[1 + offset]);
+ float y2 = Float.parseFloat (floats[2 + offset]);
+ float z2 = Float.parseFloat (floats[3 + offset]);
+
+ // 3rdd triangle
+ floats = reader.readLine().split("[\\s,;]+");
+ offset = floats.length == 5 ? 1 : 0;
+ float x3 = Float.parseFloat (floats[1 + offset]);
+ float y3 = Float.parseFloat (floats[2 + offset]);
+ float z3 = Float.parseFloat (floats[3 + offset]);
+
+ Triangles.add (new Triangle (x1, y1, z1, x2, y2, z2, x3, y3, z3));
+
+ reader.readLine(); // " endloop"
+ reader.readLine(); // " endfacet"
+ }
+ }
+ catch (Exception e)
+ {
+ return false;
+ }
+
+ return true;
+ }
+
void Scale(float Factor)
{
if(Float.isNaN(Factor))return;
@@ -95,9 +231,5 @@ class Mesh {
if(tri.z3>bz2)bz2=tri.z3;
}
}
-
-
-
-
}

0 comments on commit 1e7401e

Please sign in to comment.
Something went wrong with that request. Please try again.