EOS 2  1.1.0
Einfache Objektbasierte Sprache
ObjLoader.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.robot.geom3d;
2 
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;
9 
19 public class ObjLoader {
20  public static final Polyhedron loadObj(String path, String filename) {
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);
25  System.exit(-1);
26  return null;
27  }
28  }
29 
30  public static final Polyhedron loadObj(String path, String filename, int hint) {
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);
35  System.exit(-1);
36  return null;
37  }
38  }
39 
40  private static final Polyhedron readPolyhedron(BufferedReader data, String path, int hint)
41  throws IOException, NFaceException {
42  final Polyhedron ph = new Polyhedron();
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);
47  String line;
48  Material mat = null;
49  while ((line = data.readLine()) != null) {
50  if (line.startsWith("#")) {
51  // comment
52  } else if (line.equals("")) {
53  // empty line
54  } else if (line.startsWith("v ")) { // read in vertex data
55  v.add(new Vertice(readFloat(line, 2)));
56  } else if (line.startsWith("vt ")) { // read texture coordinates
57  vt.add(new VerticeTexture(readFloat(line, 3)));
58  } else if (line.startsWith("vn ")) { // read normal coordinates
59  vn.add(new VerticeNormal(readFloat(line, 3)));
60  } else if (line.startsWith("f ")) { // read face data
61  ph.faces.add(readFace(line.substring(2), ph, mat, v, vt, vn));
62  } else if (line.startsWith("mtllib ")) { // load material file
63  loadMtl(path, line.substring(7), ph, matIndex);
64  } else if (line.startsWith("usemtl ")) { // use material
65  mat = matIndex.get(line.substring(7).trim());
66  } else {
67  // one of many tags we do not process
68  // o new object
69  // g new group
70  // s smooth shading
71  // ...
72  }
73  }
74  return ph;
75  }
76 
77  private static final void loadMtl(String path, String filename, Polyhedron ph, Hashtable<String, Material> matIndex)
78  throws IOException {
79  try (BufferedReader data = openStream(path + filename)) {
80  String line;
81  Material mat = new Material();
82  while ((line = data.readLine()) != null) {
83  if (line.startsWith("#")) {
84  // comment
85  } else if (line.equals("")) {
86  // empty line
87  } else if (line.startsWith("newmtl ")) {
88  mat = new Material();
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 ")) {
93  // Ka spectral color
94  mat.setKA(readFloat(line, 3));
95  } else if (line.startsWith("Kd ")) {
96  // Kd diffuse reflectivity
97  mat.setKD(readFloat(line, 3));
98  } else if (line.startsWith("Ks ")) {
99  // Ks spectral reflectivity
100  mat.setKS(readFloat(line, 3));
101  } else {
102  // one of many tags we do not process
103  // Tf transmission filter
104  // d dissolve
105  // illum illumination model
106  // 0. Color on and Ambient off
107  // 1. Color on and Ambient on
108  // 2. Highlight on
109  // 3. Reflection on and Ray trace on
110  // 4. Transparency: Glass on, Reflection: Ray trace on
111  // 5. Reflection: Fresnel on and Ray trace on
112  // 6. Transparency: Refraction on, Reflection: Fresnel off
113  // and Ray trace on
114  // 7. Transparency: Refraction on, Reflection: Fresnel on
115  // and Ray trace on
116  // 8. Reflection on and Ray trace off
117  // 9. Transparency: Glass on, Reflection: Ray trace off
118  // 10. Casts shadows onto invisible surfaces
119  // Ns spectral exponent
120  // Ni optical density
121  // ...
122  }
123  }
124  }
125  }
126 
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++) {
131  try {
132  numbers[i] = Float.parseFloat(values[i]);
133  } catch (Exception e) {
134  System.out.println(line);
135  }
136  }
137  return numbers;
138  }
139 
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);
151  } else {
152  fvt = null;
153  }
154  if (!vert[2].isEmpty()) {
155  fvn[i] = vn.get(Integer.parseInt(vert[2]) - 1);
156  } else {
157  fvn = null;
158  }
159  }
160  return new Face(fv, fvt, fvn, mat);
161  }
162 
163  private static final BufferedReader openStream(String filename) throws IOException {
164  return new BufferedReader(new InputStreamReader(ResourceLoader.getResourceAsStream(filename)));
165  }
166 }
static final Polyhedron loadObj(String path, String filename, int hint)
Definition: ObjLoader.java:30
static final Polyhedron loadObj(String path, String filename)
Definition: ObjLoader.java:20
final LinkedList< Face > faces
Definition: Polyhedron.java:12
Impressum