1 package de.lathanda.eos.robot.geom3d;
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.util.ArrayList;
7 import java.util.Hashtable;
8 import de.lathanda.eos.base.ResourceLoader;
21 try (BufferedReader data = openStream(path + filename)) {
22 return readPolyhedron(data, path, 100);
23 }
catch (Exception e) {
24 System.err.println(
"missing file "+path+filename);
31 try (BufferedReader data = openStream(path + filename)) {
32 return readPolyhedron(data, path, hint);
33 }
catch (Exception e) {
34 System.err.println(
"missing file "+path+filename);
40 private static final Polyhedron readPolyhedron(BufferedReader data, String path,
int hint)
43 final Hashtable<String, Material> matIndex =
new Hashtable<>();
44 final ArrayList<Vertice> v =
new ArrayList<>(hint);
45 final ArrayList<VerticeTexture> vt =
new ArrayList<>(hint);
46 final ArrayList<VerticeNormal> vn =
new ArrayList<>(hint);
49 while ((line = data.readLine()) !=
null) {
50 if (line.startsWith(
"#")) {
52 }
else if (line.equals(
"")) {
54 }
else if (line.startsWith(
"v ")) {
55 v.add(
new Vertice(readFloat(line, 2)));
56 }
else if (line.startsWith(
"vt ")) {
57 vt.add(
new VerticeTexture(readFloat(line, 3)));
58 }
else if (line.startsWith(
"vn ")) {
59 vn.add(
new VerticeNormal(readFloat(line, 3)));
60 }
else if (line.startsWith(
"f ")) {
61 ph.
faces.add(readFace(line.substring(2), ph, mat, v, vt, vn));
62 }
else if (line.startsWith(
"mtllib ")) {
63 loadMtl(path, line.substring(7), ph, matIndex);
64 }
else if (line.startsWith(
"usemtl ")) {
65 mat = matIndex.get(line.substring(7).trim());
77 private static final void loadMtl(String path, String filename, Polyhedron ph, Hashtable<String, Material> matIndex)
79 try (BufferedReader data = openStream(path + filename)) {
81 Material mat =
new Material();
82 while ((line = data.readLine()) !=
null) {
83 if (line.startsWith(
"#")) {
85 }
else if (line.equals(
"")) {
87 }
else if (line.startsWith(
"newmtl ")) {
89 matIndex.put(line.substring(7).trim(), mat);
90 }
else if (line.startsWith(
"map_Kd ")) {
91 mat.setImage(ResourceLoader.loadResourceImage(path + line.substring(6).trim()));
92 }
else if (line.startsWith(
"Ka ")) {
94 mat.setKA(readFloat(line, 3));
95 }
else if (line.startsWith(
"Kd ")) {
97 mat.setKD(readFloat(line, 3));
98 }
else if (line.startsWith(
"Ks ")) {
100 mat.setKS(readFloat(line, 3));
127 private static final float[] readFloat(String line,
int from) {
128 String[] values = line.substring(from).split(
" ");
129 float[] numbers =
new float[values.length];
130 for (
int i = 0; i < values.length; i++) {
132 numbers[i] = Float.parseFloat(values[i]);
133 }
catch (Exception e) {
134 System.out.println(line);
140 private static final Face readFace(String values, Polyhedron ph, Material mat, ArrayList<Vertice> v,
141 ArrayList<VerticeTexture> vt, ArrayList<VerticeNormal> vn)
throws NFaceException {
142 String[] vertices = values.split(
" ");
143 Vertice[] fv =
new Vertice[vertices.length];
144 VerticeTexture[] fvt =
new VerticeTexture[vertices.length];
145 VerticeNormal[] fvn =
new VerticeNormal[vertices.length];
146 for (
int i = 0; i < vertices.length; i++) {
147 String[] vert = vertices[i].split(
"/");
148 fv[i] = v.get(Integer.parseInt(vert[0]) - 1);
149 if (!vert[1].isEmpty()) {
150 fvt[i] = vt.get(Integer.parseInt(vert[1]) - 1);
154 if (!vert[2].isEmpty()) {
155 fvn[i] = vn.get(Integer.parseInt(vert[2]) - 1);
160 return new Face(fv, fvt, fvn, mat);
163 private static final BufferedReader openStream(String filename)
throws IOException {
164 return new BufferedReader(
new InputStreamReader(ResourceLoader.getResourceAsStream(filename)));
static final Polyhedron loadObj(String path, String filename, int hint)
static final Polyhedron loadObj(String path, String filename)
final LinkedList< Face > faces