-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMesh.pde
More file actions
235 lines (205 loc) · 5.92 KB
/
Copy pathMesh.pde
File metadata and controls
235 lines (205 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Mesh Class
class Mesh {
ArrayList Triangles;
float bx1,by1,bz1,bx2,by2,bz2; //bounding box
boolean Valid;
float Sink;
//Mesh Loading routine
Mesh(String FileName)
{
Valid = false;
println ("loading file " + FileName + "...");
Triangles = new ArrayList();
Sink=0;
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]));
}
}
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;
for(int i = Triangles.size()-1;i>=0;i--)
{
Triangle tri = (Triangle) Triangles.get(i);
tri.Scale(Factor);
}
CalculateBoundingBox();
}
void Translate(float tx, float ty, float tz)
{
for(int i = Triangles.size()-1;i>=0;i--)
{
Triangle tri = (Triangle) Triangles.get(i);
tri.Translate(tx,ty,tz);
}
CalculateBoundingBox();
}
void RotateX(float Angle)
{
if(Float.isNaN(Angle))return;
for(int i = Triangles.size()-1;i>=0;i--)
{
Triangle tri = (Triangle) Triangles.get(i);
tri.RotateX(Angle);
}
CalculateBoundingBox();
}
void CalculateBoundingBox()
{
bx1 = 10000;
bx2 = -10000;
by1 = 10000;
by2 = -10000;
bz1 = 10000;
bz2 = -10000;
for(int i = Triangles.size()-1;i>=0;i--)
{
Triangle tri = (Triangle) Triangles.get(i);
if(tri.x1<bx1)bx1=tri.x1;
if(tri.x2<bx1)bx1=tri.x2;
if(tri.x3<bx1)bx1=tri.x3;
if(tri.x1>bx2)bx2=tri.x1;
if(tri.x2>bx2)bx2=tri.x2;
if(tri.x3>bx2)bx2=tri.x3;
if(tri.y1<by1)by1=tri.y1;
if(tri.y2<by1)by1=tri.y2;
if(tri.y3<by1)by1=tri.y3;
if(tri.y1>by2)by2=tri.y1;
if(tri.y2>by2)by2=tri.y2;
if(tri.y3>by2)by2=tri.y3;
if(tri.z1<bz1)bz1=tri.z1;
if(tri.z2<bz1)bz1=tri.z2;
if(tri.z3<bz1)bz1=tri.z3;
if(tri.z1>bz2)bz2=tri.z1;
if(tri.z2>bz2)bz2=tri.z2;
if(tri.z3>bz2)bz2=tri.z3;
}
}
}