Thread: [Bprocessor-commit] gui/src/net/sourceforge/bprocessor/gui/actions FileImportActionListener.java, 1
Status: Pre-Alpha
Brought to you by:
henryml
From: rimestad <rim...@us...> - 2007-10-12 10:20:35
|
Update of /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv27796/src/net/sourceforge/bprocessor/gui/actions Modified Files: FileImportActionListener.java ImportFileReader.java Log Message: Improved on obj readin Index: FileImportActionListener.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions/FileImportActionListener.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** FileImportActionListener.java 12 Oct 2007 08:00:13 -0000 1.7 --- FileImportActionListener.java 12 Oct 2007 10:20:35 -0000 1.8 *************** *** 23,26 **** --- 23,27 ---- import net.sourceforge.bprocessor.model.Project; + import net.sourceforge.bprocessor.model.Space; import org.apache.log4j.Logger; *************** *** 80,84 **** } else if (lfile.getName().endsWith(".obj")) { try { ! ImportFileReader.importObjFile(lfile, Project.getInstance().world()); } catch (Exception ex) { log.error("Could not open file: " + lfile, ex); --- 81,87 ---- } else if (lfile.getName().endsWith(".obj")) { try { ! Space s = Project.getInstance().world().createUnion(lfile.getName()); ! ImportFileReader.importObjFile(lfile, s); ! Project.getInstance().world().add(s); } catch (Exception ex) { log.error("Could not open file: " + lfile, ex); Index: ImportFileReader.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions/ImportFileReader.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** ImportFileReader.java 12 Oct 2007 08:00:14 -0000 1.1 --- ImportFileReader.java 12 Oct 2007 10:20:35 -0000 1.2 *************** *** 14,19 **** --- 14,23 ---- import java.io.InputStreamReader; import java.util.ArrayList; + import java.util.HashMap; + import java.util.HashSet; import java.util.LinkedList; import java.util.List; + import java.util.Map; + import java.util.Set; import java.util.StringTokenizer; *************** *** 33,37 **** private static Logger log = Logger.getLogger(ImportFileReader.class); /** ! * Importer for the obj format * @param f The file to load * @param into the space to paste it into --- 37,63 ---- private static Logger log = Logger.getLogger(ImportFileReader.class); /** ! * Importer for the obj format that is as follows ! * # some text ! * Line is a comment until the end of the line ! * v float float float ! * A single vertex's geometric position in space. The first vertex listed in ! * the file has index 1, and subsequent vertices are numbered sequentially. ! * vn float float float ! * A normal. The first normal in the file is index 1, and subsequent normals ! * are numbered sequentially. ! * vt float float ! * A texture coordinate. The first texture coordinate in the file is index 1, ! * and subsequent textures are numbered sequentially. ! * f int int int ... ! * or ! * f int/int int/int int/int . . . ! * or ! * f int/int/int int/int/int int/int/int ... ! * A polygonal face. The numbers are indexes into the arrays of vertex positions, ! * texture coordinates, and normals respectively. A number may be omitted if, for ! * example, texture coordinates are not being defined in the model. ! * There is no maximum number of vertices that a single polygon may contain. ! * The .obj file specification says that each face must be flat and convex. ! * In JavaView polygonal face may be triangulated. * @param f The file to load * @param into the space to paste it into *************** *** 44,48 **** BufferedReader br = new BufferedReader(reader); LinkedList<Vertex> verts = new LinkedList<Vertex>(); ! LinkedList<Surface> surfaces = new LinkedList<Surface>(); int lineNum = 0; while (br.ready()) { --- 70,76 ---- BufferedReader br = new BufferedReader(reader); LinkedList<Vertex> verts = new LinkedList<Vertex>(); ! Set<Edge> edges = new HashSet<Edge>(); ! Set<Surface> surfaces = new HashSet<Surface>(); ! Map<Vertex, Set<Edge>> v2es = new HashMap<Vertex, Set<Edge>>(); int lineNum = 0; while (br.ready()) { *************** *** 51,78 **** String[] lineContent = line.split(" "); if (lineContent[0].equals("v")) { ! verts.add(new Vertex(Double.parseDouble(lineContent[1]), Double.parseDouble(lineContent[2]), ! Double.parseDouble(lineContent[3]))); } else if (lineContent[0].equals("f")) { ! LinkedList<Edge> edges = new LinkedList<Edge>(); Vertex cur = null; ! Vertex first = verts.get(Integer.parseInt(lineContent[1].split("/")[0])); Vertex prev = first; for (int i = 2; i < lineContent.length; i++) { ! cur = verts.get(Integer.parseInt(lineContent[i].split("/")[0])); ! edges.add(new Edge(prev, cur)); prev = cur; } ! edges.add(new Edge(prev, first)); ! surfaces.add(new Surface(edges)); ! } else if (lineContent[0].equals("#")) { ! continue; ! } else { ! log.error("Did not understand content on line " + lineNum + " containing " + line); } } } } /** * Importer for the csv file format --- 79,136 ---- String[] lineContent = line.split(" "); if (lineContent[0].equals("v")) { ! Vertex v = new Vertex(Double.parseDouble(lineContent[1]), Double.parseDouble(lineContent[2]), ! Double.parseDouble(lineContent[3])); ! verts.add(v); ! v2es.put(v, new HashSet<Edge>()); } else if (lineContent[0].equals("f")) { ! LinkedList<Edge> edge = new LinkedList<Edge>(); Vertex cur = null; ! Vertex first = verts.get(Integer.parseInt(lineContent[1].split("/")[0]) - 1); Vertex prev = first; for (int i = 2; i < lineContent.length; i++) { ! cur = verts.get(Integer.parseInt(lineContent[i].split("/")[0]) - 1); ! Edge e = findEdge(prev, cur, v2es); ! edge.add(e); prev = cur; } ! edge.add(findEdge(prev, first, v2es)); ! surfaces.add(new Surface(edge)); ! edges.addAll(edge); } } + for (Vertex v : verts) { + into.add(v); + } + for (Edge e : edges) { + into.add(e); + } + for (Surface s : surfaces) { + into.add(s); + } + into.changed(); } } + private static Edge findEdge(Vertex prev, Vertex cur, Map<Vertex, Set<Edge>> v2es) { + Edge e = null; + Set<Edge> prevEdges = v2es.get(prev); + Set<Edge> curEdges = v2es.get(cur); + for (Edge edge : prevEdges) { + if (curEdges.contains(edge)) { + if (e != null) { + log.error("Allready found one common " + e + " and now " + edge); + } + e = edge; + } + } + if (e == null) { + e = new Edge(prev, cur); + v2es.get(prev).add(e); + v2es.get(cur).add(e); + } + return e; + } + /** * Importer for the csv file format |