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-22 10:00:22
|
Update of /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv25359/src/net/sourceforge/bprocessor/gui/actions Modified Files: FileImportActionListener.java ImportFileReader.java Log Message: Added scale and basis for obj import Index: FileImportActionListener.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions/FileImportActionListener.java,v retrieving revision 1.8 retrieving revision 1.9 diff -C2 -d -r1.8 -r1.9 *** FileImportActionListener.java 12 Oct 2007 10:20:35 -0000 1.8 --- FileImportActionListener.java 22 Oct 2007 10:00:17 -0000 1.9 *************** *** 16,25 **** --- 16,28 ---- import java.io.InputStreamReader; + import javax.swing.BoxLayout; import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JLabel; import javax.swing.JPanel; + import javax.swing.JTextField; import javax.swing.filechooser.FileFilter; + import net.sourceforge.bprocessor.gui.GUI; import net.sourceforge.bprocessor.model.Project; import net.sourceforge.bprocessor.model.Space; *************** *** 37,40 **** --- 40,54 ---- private CVSinfo info = null; + + private FileFilter objFilter; + + private FileFilter csvFilter; + + /** The xy axis is the basis plane */ + public static final int XY = 0; + /** The zx axis is the basis plane */ + public static final int ZX = 1; + /** The yz axis is the basis plane */ + public static final int YZ = 2; /** *************** *** 45,63 **** info = new CVSinfo(loadChooser); loadChooser.setAccessory(info); ! loadChooser.setFileFilter(new FileFilter() { @Override public boolean accept(File f) { ! String name = f.getName(); ! if (f.isDirectory() || name.endsWith(".csv") || name.endsWith(".obj")) { ! return true; ! } ! return false; } @Override public String getDescription() { ! return "Legal import formats"; } ! }); } --- 59,87 ---- info = new CVSinfo(loadChooser); loadChooser.setAccessory(info); ! objFilter = new FileFilter() { @Override public boolean accept(File f) { ! return f.isDirectory() || f.getName().endsWith(".obj"); } @Override public String getDescription() { ! return "Obj - wavefront format"; } ! }; ! csvFilter = new FileFilter() { ! @Override ! public boolean accept(File f) { ! return f.isDirectory() || f.getName().endsWith(".csv"); ! } ! ! @Override ! public String getDescription() { ! return "Csv - Landmålergården file"; ! } ! }; ! loadChooser.addChoosableFileFilter(csvFilter); ! loadChooser.addChoosableFileFilter(objFilter); ! loadChooser.setFileFilter(csvFilter); } *************** *** 82,86 **** try { Space s = Project.getInstance().world().createUnion(lfile.getName()); ! ImportFileReader.importObjFile(lfile, s); Project.getInstance().world().add(s); } catch (Exception ex) { --- 106,111 ---- try { Space s = Project.getInstance().world().createUnion(lfile.getName()); ! ImportFileReader.importObjFile(lfile, s, info.objPanel.getScale(), ! info.objPanel.getDirection()); Project.getInstance().world().add(s); } catch (Exception ex) { *************** *** 88,91 **** --- 113,244 ---- } } + loadChooser.revalidate(); + loadChooser.repaint(); + } + } + + private class OBJJPanel extends JPanel implements ActionListener { + private JComboBox basis; + private JTextField scale; + + public OBJJPanel() { + this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); + this.add(new JLabel("Baseplane")); + basis = new JComboBox(new String[]{"X-Y", "Z-X", "Y-Z"}); + this.add(basis); + this.add(new JLabel("Scale")); + scale = new JTextField("1:1", 5); + scale.addActionListener(this); + this.add(scale); + } + + public void actionPerformed(ActionEvent e) { + if (e.getSource() instanceof JTextField) { + JTextField tf = (JTextField)e.getSource(); + log.info(e); + String txt = tf.getText(); + if (!txt.matches("1:\\d+")) { + GUI.getInstance().alert("String did not match format [1:digit]", tf.getLocation()); + } + } + } + + /** + * Return the scale for the model + * @return The scale + */ + public double getScale() { + return 1.0 / Integer.parseInt(scale.getText().substring(2)); + } + + /** + * Return a int beeing one of the static finals XY, ZX, YZ which + * is the coords that should be treated as XY plane when importing + * @return either XY, ZX or YZ + */ + public int getDirection() { + return basis.getSelectedIndex(); + } + } + + private class CSVJPanel extends JPanel implements ActionListener { + private int selectedFloor; + private JComboBox floor; + private JLabel linesLabel; + + public CSVJPanel() { + this.setMinimumSize(new Dimension(100, 50)); + this.setMaximumSize(new Dimension(100, 200)); + this.setPreferredSize(new Dimension(100, 150)); + this.add(new JLabel("Floor:")); + floor = new JComboBox(); + floor.addItem("All"); + floor.setSelectedIndex(0); + floor.addActionListener(this); + this.add(floor); + linesLabel = new JLabel("Lines:"); + this.add(linesLabel); + } + + /** + * {@inheritDoc} + */ + public void actionPerformed(ActionEvent e) { + if (isShowing()) { + if (floor.getSelectedIndex() > 0) { + selectedFloor = Integer.parseInt((String)floor.getSelectedItem()); + } else { + selectedFloor = -1; + } + } + } + + /** + * Reset all info in view + */ + private void resetView() { + linesLabel.setText("Lines:"); + floor.removeAllItems(); + floor.addItem("All"); + } + + public void fileSelected(File file) { + if (file.canRead()) { + try { + BufferedReader bf; + FileInputStream fis = new FileInputStream(file); + InputStreamReader isr = new InputStreamReader(fis); + bf = new BufferedReader(isr); + int lines = 0; + long floors = 0; + while (bf.ready()) { + String line = bf.readLine(); + if (line.charAt(0) == '#') { + //skip comments + continue; + } else { + lines++; + //read info from file + String[] tokens = line.split(";"); + if (tokens.length < 15) { + throw new Exception("Wrong file format"); + } + long f = Long.parseLong(tokens[2]); //read out the floor + floors = floors | (long)Math.pow(2, f); + } + } + resetView(); + + for (int i = 0; i < 64; i++) { + long val = (floors >> i) & 1; + if (val == 1) { + this.floor.addItem("" + i); + } + } + this.linesLabel.setText("Lines: " + lines); + } catch (Exception exc) { + log.error(exc); + } + } } } *************** *** 95,108 **** * for the users pleasure */ ! private class CVSinfo extends JPanel ! implements PropertyChangeListener, ActionListener { private File file = null; - private JComboBox floor; - - private JLabel linesLabel; - - private int selectedFloor; - /** * The constructor --- 248,256 ---- * for the users pleasure */ ! private class CVSinfo extends JPanel implements PropertyChangeListener { private File file = null; + private CSVJPanel csvPanel; + private OBJJPanel objPanel; /** * The constructor *************** *** 111,121 **** private CVSinfo(JFileChooser fc) { setPreferredSize(new Dimension(100, 50)); ! floor = new JComboBox(); ! floor.addItem("All"); ! floor.setSelectedIndex(0); ! floor.addActionListener(this); ! this.add(floor); ! linesLabel = new JLabel("Lines:"); ! this.add(linesLabel); fc.addPropertyChangeListener(this); } --- 259,264 ---- private CVSinfo(JFileChooser fc) { setPreferredSize(new Dimension(100, 50)); ! csvPanel = new CSVJPanel(); ! objPanel = new OBJJPanel(); fc.addPropertyChangeListener(this); } *************** *** 125,129 **** */ private int getSelectedFloor() { ! return selectedFloor; } --- 268,272 ---- */ private int getSelectedFloor() { ! return csvPanel.selectedFloor; } *************** *** 135,219 **** String prop = e.getPropertyName(); - //If the directory changed, don't show an image. if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) { file = null; update = true; - - //If a file became selected, find out which one. } else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) { file = (File) e.getNewValue(); update = true; } if (update && file != null) { if (file.getName().endsWith(".csv")) { ! linesLabel.setEnabled(true); ! if (file.canRead()) { ! try { ! BufferedReader bf; ! FileInputStream fis = new FileInputStream(file); ! InputStreamReader isr = new InputStreamReader(fis); ! bf = new BufferedReader(isr); ! int lines = 0; ! long floors = 0; ! while (bf.ready()) { ! String line = bf.readLine(); ! if (line.charAt(0) == '#') { ! //skip comments ! continue; ! } else { ! lines++; ! //read info from file ! String[] tokens = line.split(";"); ! if (tokens.length < 15) { ! throw new Exception("Wrong file format"); ! } ! long f = Long.parseLong(tokens[2]); //read out the floor ! floors = floors | (long)Math.pow(2, f); ! } ! } ! ! resetView(); ! ! for (int i = 0; i < 64; i++) { ! long val = (floors >> i) & 1; ! if (val == 1) { ! floor.addItem("" + i); ! } ! } ! linesLabel.setText("Lines: " + lines); ! } catch (Exception exc) { ! log.error(exc); ! } ! } ! } else { ! linesLabel.setEnabled(false); } } if (isShowing()) { repaint(); - } else { - resetView(); - } - } - - /** - * Reset all info in view - */ - private void resetView() { - linesLabel.setText("Lines:"); - floor.removeAllItems(); - floor.addItem("All"); - } - - /** - * {@inheritDoc} - */ - public void actionPerformed(ActionEvent e) { - if (isShowing()) { - if (floor.getSelectedIndex() > 0) { - selectedFloor = Integer.parseInt((String)floor.getSelectedItem()); - } else { - selectedFloor = -1; - } } } --- 278,306 ---- String prop = e.getPropertyName(); if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) { file = null; update = true; } else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) { file = (File) e.getNewValue(); update = true; } + + if (JFileChooser.FILE_FILTER_CHANGED_PROPERTY.equals(prop)) { + this.removeAll(); + if (e.getNewValue() == objFilter) { + this.add(objPanel); + } else if (e.getNewValue() == csvFilter) { + this.add(csvPanel); + } + this.revalidate(); + } + if (update && file != null) { if (file.getName().endsWith(".csv")) { ! csvPanel.fileSelected(file); } } if (isShowing()) { repaint(); } } Index: ImportFileReader.java =================================================================== RCS file: /cvsroot/bprocessor/gui/src/net/sourceforge/bprocessor/gui/actions/ImportFileReader.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** ImportFileReader.java 18 Oct 2007 07:58:12 -0000 1.4 --- ImportFileReader.java 22 Oct 2007 10:00:17 -0000 1.5 *************** *** 40,71 **** /** ! * 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 * @throws IOException a possible file read error */ ! public static void importObjFile(File f, Space into) throws IOException { if (f.exists()) { FileInputStream stream = new FileInputStream(f); --- 40,75 ---- /** ! * Importer for the obj format that is as follows<br> ! * # some text<br> ! * Line is a comment until the end of the line<br> ! * v float float float<br> ! * A single vertex's geometric position in space. The first vertex listed in <br> ! * the file has index 1, and subsequent vertices are numbered sequentially.<br> ! * vn float float float<br> ! * A normal. The first normal in the file is index 1, and subsequent normals <br> ! * are numbered sequentially.<br> ! * vt float float<br> ! * A texture coordinate. The first texture coordinate in the file is index 1, <br> ! * and subsequent textures are numbered sequentially.<br> ! * f int int int ...<br> ! * or<br> ! * f int/int int/int int/int . . .<br> ! * or<br> ! * f int/int/int int/int/int int/int/int ...<br> ! * A polygonal face. The numbers are indexes into the arrays of vertex positions, <br> ! * texture coordinates, and normals respectively. A number may be omitted if, for <br> ! * example, texture coordinates are not being defined in the model.<br> ! * There is no maximum number of vertices that a single polygon may contain. <br> ! * The .obj file specification says that each face must be flat and convex. <br> ! * In JavaView polygonal face may be triangulated. <br> * @param f The file to load * @param into the space to paste it into + * @param scale The scale to import in + * @param basisPlane the plane to use as xy plane. Use one of the static final ints in + * net.sourceforge.bprocessor.gui.actions.FileImportActionListener * @throws IOException a possible file read error */ ! public static void importObjFile(File f, Space into, double scale, int basisPlane) ! throws IOException { if (f.exists()) { FileInputStream stream = new FileInputStream(f); *************** *** 73,84 **** 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>>(); List<Space> groups = new LinkedList<Space>(); Map<String, Material> materialMap = new HashMap<String, Material>(); Space currentGroup = into.createConstructionSpace("Object"); ! Material currentMaterial = new Material("Object", new float[]{0f, 0.9f, 0f}); ! materialMap.put("default", currentMaterial); groups.add(currentGroup); int lineNum = 0; --- 77,90 ---- BufferedReader br = new BufferedReader(reader); LinkedList<Vertex> verts = new LinkedList<Vertex>(); + LinkedList<Vertex> vertexNormals = new LinkedList<Vertex>(); + LinkedList<Vertex> vertexTexture = 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>>(); + Map<Vertex, Vertex> v2normal = new HashMap<Vertex, Vertex>(); List<Space> groups = new LinkedList<Space>(); Map<String, Material> materialMap = new HashMap<String, Material>(); Space currentGroup = into.createConstructionSpace("Object"); ! Material currentMaterial = null; groups.add(currentGroup); int lineNum = 0; *************** *** 91,105 **** double y = Double.parseDouble(lineContent[2]); double z = Double.parseDouble(lineContent[3]); ! double factor = 0.1; ! Vertex v = new Vertex(z * factor, x * factor, y * factor); 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); --- 97,159 ---- double y = Double.parseDouble(lineContent[2]); double z = Double.parseDouble(lineContent[3]); ! switch (basisPlane) { ! case FileImportActionListener.YZ: ! double tmpy = y; ! y = x; ! x = z; ! z = tmpy; ! break; ! case FileImportActionListener.ZX: ! double tmpx = x; ! x = z; ! z = y; ! y = tmpx; ! break; ! } ! Vertex v = new Vertex(x * scale, y * scale, z * scale); verts.add(v); v2es.put(v, new HashSet<Edge>()); + } else if (lineContent[0].equals("vn")) { + double x = Double.parseDouble(lineContent[1]); + double y = Double.parseDouble(lineContent[2]); + double z = Double.parseDouble(lineContent[3]); + Vertex v = new Vertex(z, x, y); + vertexNormals.add(v); + v2es.put(v, new HashSet<Edge>()); + } else if (lineContent[0].equals("vt")) { + double x = Double.parseDouble(lineContent[1]); + double y = Double.parseDouble(lineContent[2]); + Vertex v = new Vertex(x, y, 0); + vertexTexture.add(v); + v2es.put(v, new HashSet<Edge>()); } else if (lineContent[0].equals("f")) { LinkedList<Edge> edge = new LinkedList<Edge>(); ! int index = 0; ! String[] vert = lineContent[1].split("/"); ! Vertex first = verts.get(Integer.parseInt(vert[index]) - 1); ! Vertex cur = first; ! if (!vertexTexture.isEmpty()) { ! index++; ! } ! if (!vertexNormals.isEmpty()) { ! index++; ! Vertex normal = ! vertexNormals.get(Integer.parseInt(vert[index]) - 1); ! v2normal.put(cur, normal); ! } Vertex prev = first; for (int i = 2; i < lineContent.length; i++) { ! index = 0; ! vert = lineContent[i].split("/"); ! cur = verts.get(Integer.parseInt(vert[index]) - 1); ! if (!vertexTexture.isEmpty()) { ! index++; ! } ! if (!vertexNormals.isEmpty()) { ! index++; ! Vertex normal = ! vertexNormals.get(Integer.parseInt(vert[index]) - 1); ! v2normal.put(cur, normal); ! } Edge e = findEdge(prev, cur, v2es); edge.add(e); *************** *** 110,113 **** --- 164,171 ---- surfaces.add(s); s.setBackDomain(currentGroup); + if (currentMaterial == null) { + currentMaterial = new Material("Object", new float[]{0f, 0.9f, 0f}); + materialMap.put("default", currentMaterial); + } s.setFrontMaterial(currentMaterial); edges.addAll(edge); *************** *** 148,178 **** /** ! * Read a obj material file as ! * Comments begin with a '#' character in column 1. Blank lines may be inserted for clarity. ! * Otherwise, the file consists of a sequence of newmtl statements, followed by a definition ! * of various properties for that material. ! * The quantities that may be defined for a material include: ! * Ka r g b ! * defines the ambient color of the material to be (r,g,b). The default is (0.2,0.2,0.2); ! * Kd r g b ! * defines the diffuse color of the material to be (r,g,b). The default is (0.8,0.8,0.8); ! * Ks r g b ! * defines the specular color of the material to be (r,g,b). This color shows up in ! * highlights. The default is (1.0,1.0,1.0); ! * d alpha ! * defines the transparency of the material to be alpha. ! * The default is 1.0 (not transparent at all) Some formats use Tr instead of d; ! * Tr alpha ! * defines the transparency of the material to be alpha. ! * The default is 1.0 (not transparent at all). Some formats use d instead of Tr; ! * Ns s ! * defines the shininess of the material to be s. The default is 0.0; ! * illum n ! * denotes the illumination model used by the material. ! * illum = 1 indicates a flat material with no specular highlights, so the value of Ks ! * is not used. illum = 2 denotes the presence of specular highlights, and so a specification ! * for Ks is required. ! * map_Ka filename ! * names a file containing a texture map, which should just be an ASCII dump of RGB values; * @param file the material file * @param materialMap A map from name to material --- 206,236 ---- /** ! * Read a obj material file as<br> ! * Comments begin with a '#' character in column 1. Blank lines may be inserted for clarity. <br> ! * Otherwise, the file consists of a sequence of newmtl statements, followed by a definition <br> ! * of various properties for that material.<br> ! * The quantities that may be defined for a material include:<br> ! * Ka r g b<br> ! * defines the ambient color of the material to be (r,g,b). The default is (0.2,0.2,0.2); <br> ! * Kd r g b<br> ! * defines the diffuse color of the material to be (r,g,b). The default is (0.8,0.8,0.8);<br> ! * Ks r g b<br> ! * defines the specular color of the material to be (r,g,b). This color shows up in ! * highlights. The default is (1.0,1.0,1.0); <br> ! * d alpha<br> ! * defines the transparency of the material to be alpha. ! * The default is 1.0 (not transparent at all) Some formats use Tr instead of d; <br> ! * Tr alpha<br> ! * defines the transparency of the material to be alpha. ! * The default is 1.0 (not transparent at all). Some formats use d instead of Tr; <br> ! * Ns s<br> ! * defines the shininess of the material to be s. The default is 0.0; <br> ! * illum n<br> ! * denotes the illumination model used by the material. ! * illum = 1 indicates a flat material with no specular highlights, so the value of Ks ! * is not used. illum = 2 denotes the presence of specular highlights, and so a ! * specification for Ks is required. <br> ! * map_Ka filename<br> ! * names a file containing a texture map, which should just be an ASCII dump of RGB values; * @param file the material file * @param materialMap A map from name to material |