3 package eu.mihosoft.vrl.v3d.ext.imagej;
12 import java.io.BufferedReader;
14 import java.io.FileInputStream;
15 import java.io.FileNotFoundException;
16 import java.io.FileReader;
17 import java.io.IOException;
18 import java.text.DecimalFormat;
19 import java.text.ParseException;
20 import java.util.ArrayList;
22 import javax.vecmath.Point3f;
61 private ArrayList<Point3f>
vertices =
new ArrayList<>();
64 private Point3f
normal =
new Point3f(0.0f, 0.0f, 0.0f);
67 private FileInputStream
fis;
82 public ArrayList<Point3f>
parse(File f)
throws IOException {
88 BufferedReader br =
new BufferedReader(
new FileReader(f));
89 String line = br.readLine();
90 String[] words = line.trim().split(
"\\s+");
91 if (line.indexOf(
'\0') < 0 && words[0].equalsIgnoreCase(
"solid")) {
99 FileInputStream fs =
new FileInputStream(f);
103 byte[] buffer =
new byte[84];
104 fs.read(buffer, 0, 84);
106 triangles = (int) (((buffer[83] & 0xff) << 24)
107 | ((buffer[82] & 0xff) << 16) | ((buffer[81] & 0xff) << 8) | (buffer[80] & 0xff));
108 if (((f.length() - 84) / 50) ==
triangles) {
113 System.err.println(
"File is not a valid STL");
125 in =
new BufferedReader(
new FileReader(f));
126 }
catch (FileNotFoundException e) {
131 while ((line = in.readLine()) !=
null) {
132 String[] numbers = line.trim().split(
"\\s+");
133 if (numbers[0].equals(
"vertex")) {
137 Point3f vertex =
new Point3f(x, y, z);
139 }
else if (numbers[0].equals(
"facet") && numbers[1].equals(
"normal")) {
146 }
catch (IOException e) {
148 }
catch (ParseException e) {
159 vertices =
new ArrayList<Point3f>();
161 fis =
new FileInputStream(f);
162 for (
int h = 0; h < 84; h++) {
166 byte[] tri =
new byte[50];
167 for (
int tb = 0; tb < 50; tb++) {
168 tri[tb] = (byte)
fis.read();
173 for (
int i = 0; i < 3; i++) {
174 final int j = i * 12 + 12;
178 tri[j + 6], tri[j + 7]);
180 tri[j + 10], tri[j + 11]);
181 Point3f p =
new Point3f(px, py, pz);
186 }
catch (FileNotFoundException e) {
188 }
catch (IOException e) {
208 private float parseFloat(String
string)
throws ParseException {
210 return Float.parseFloat(
string);
223 return Float.intBitsToFloat((((b3 & 0xff) << 24) | ((b2 & 0xff) << 16)
224 | ((b1 & 0xff) << 8) | (b0 & 0xff)));
float parseFloat(String string)
ArrayList< Point3f > vertices
float leBytesToFloat(byte b0, byte b1, byte b2, byte b3)
ArrayList< Point3f > parse(File f)