View Javadoc
1 /***************************************************************************** 2 * Virtual Mockup for Machine Vision 3 * Copyright (C) 2001-2003 Fabio R. de Miranda, João E. Kogler Jr., 4 * Carlos S. Santos. 5 * Virtual Mockup for Machine Vision Project funded by SENAC-SP 6 * 7 * Permission is granted to redistribute and/or modify this 8 * software under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This software is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License (http://www.gnu.org/copyleft/lesser.html) 16 * for more details. 17 * 18 *****************************************************************************/ 19 20 package camera3d.action; 21 22 import javax.media.j3d.*; 23 import java.util.*; 24 import javax.swing.*; 25 import java.io.*; 26 import javax.vecmath.*; 27 import camera3d.VcContent; 28 import camera3d.J3DBase; 29 import camera3d.GUIControl; 30 31 /*** 32 * Action that creates a text file with the list of edges of a VcContent object. 33 * 34 * @author Fábio Roberto de Miranda e Carlos da Silva dos Santos 35 * @version 1.0 36 */ 37 public class DumpEdgesAction extends GUIAction { 38 39 VcContent content; 40 String fileName; 41 StringBuffer output = new StringBuffer(50000); 42 43 StringBuffer vBuffer = new StringBuffer(50000); 44 45 int totalEdges = 0; 46 boolean hasFileName = false; 47 Transform3D currentTransform = new Transform3D(); 48 Point3d tempP3d = new Point3d(); 49 Point3d[] points = new Point3d[4]; 50 51 /*** 52 * GeometrySize: 53 * 4 for quadArrays 54 * 3 for TriangleArrays 55 * 2 for LineArrays 56 */ 57 int geometrySize = 3; 58 59 boolean debugflag = true; 60 61 // For debugging purposes 62 GUIControl guiControl; 63 64 public DumpEdgesAction(VcContent content, String fileName) { 65 this(content); 66 this.fileName = fileName; 67 hasFileName = true; 68 } 69 70 public DumpEdgesAction(VcContent content){ 71 /* Shows a fileChooser asking for output file name */ 72 this.content = content; 73 for (int i = 0; i < points.length; i++) { 74 points[i] = new Point3d(); 75 } 76 77 } 78 79 public void doAction(ActionExecutor executor) { 80 /*** 81 * Show fileChooser and ask for fileName 82 */ 83 System.out.println("__doing dumpAction"); 84 if (!hasFileName){ 85 System.out.println("Asking for filename"); 86 this.fileName = askUserForFile(); 87 } 88 89 this.guiControl = executor.getGUIControl(); 90 91 guiControl.startLineArray(); 92 /***@todo: implement this camera3d.VcAction abstract method*/ 93 Node[] nodes = content.getContent(); 94 for (int i = 0; i < nodes.length; i++) { 95 if (nodes[i] != null) { 96 scan(nodes[i]); 97 } 98 } 99 100 FileOutputStream out = null; 101 PrintStream print = null; 102 103 try { 104 out = new FileOutputStream(fileName); 105 } 106 catch (Exception ex) { 107 System.out.println("Could not find file: "+fileName); 108 ex.printStackTrace(); 109 } 110 111 try { 112 print = new PrintStream(out); 113 } 114 catch (Exception ex) { 115 ex.printStackTrace(); 116 } 117 118 // Counts the edges 119 debugln("Number of edges:"+totalEdges); 120 // Writes the StringBuffer to a file 121 print.println(totalEdges+"\n"); 122 123 debugln("Saving file "+fileName); 124 String s = output.toString(); 125 //debugln(s); 126 print.println(s); 127 debugln("File saved. Closing file."); 128 129 // Over 130 try { 131 print.close(); 132 //out.close(); 133 } 134 catch (Exception ex) { 135 debugln("Could not save output file"); 136 ex.printStackTrace(); 137 } 138 guiControl.finishLineArray(); 139 140 } 141 142 public String askUserForFile(){ 143 String fname = null; 144 JFileChooser chooser = new JFileChooser(); 145 int result = chooser.showSaveDialog(null); 146 if (result == JFileChooser.APPROVE_OPTION){ 147 try { 148 fname = chooser.getSelectedFile().getCanonicalPath(); 149 } catch (IOException ex){ 150 debugln("IOException while trying to obtain path."); 151 } 152 } else { 153 fname = "Arestas.txt"; 154 } 155 debugln("Selected file: "+fname); 156 return fname; 157 } 158 159 public void scan(Node n){ 160 debugln("Scan recursion"); 161 if (n instanceof Group){ 162 Group g = (Group) n; 163 Enumeration enum = g.getAllChildren(); 164 while (enum.hasMoreElements()){ 165 Object obj = enum.nextElement(); 166 if (obj instanceof Node){ 167 scan((Node)obj); 168 } else { 169 debugln("Found an objects that isn't an instance of Node"); 170 } 171 } 172 } else if (n instanceof Shape3D){ 173 traverseGeometry((Shape3D)n); 174 } 175 } 176 177 public void traverseGeometry(Shape3D shape){ 178 debugln("traverseGeometry"); 179 shape.getLocalToVworld(currentTransform); 180 Enumeration enum = shape.getAllGeometries(); 181 while (enum.hasMoreElements()){ 182 Object obj = enum.nextElement(); 183 if (obj instanceof GeometryArray) { 184 printPointReset(); 185 GeometryArray geom = (GeometryArray)obj; 186 if (geom instanceof IndexedGeometryArray){ 187 if (geom instanceof IndexedTriangleArray){ 188 geometrySize = 3; 189 } else if (geom instanceof IndexedQuadArray){ 190 geometrySize = 4; 191 } else if (geom instanceof IndexedLineArray){ 192 geometrySize = 2; 193 } 194 dumpIndexedGeometryArray((IndexedGeometryArray) geom, currentTransform); 195 196 } else if (geom instanceof GeometryArray){ 197 198 GeometryArray array = (GeometryArray)geom; 199 if (array instanceof LineArray){ 200 geometrySize = 2; 201 } else if (array instanceof TriangleArray){ 202 geometrySize = 3; 203 } else if (array instanceof QuadArray){ 204 geometrySize = 4; 205 } 206 dumpGeometryArray(array, currentTransform); 207 } 208 } else { 209 // Write somewhere to somebody that a non-geometryArray piece of geometry was found 210 debugln("DumpEdgesAction: Found non-geometryArray piece of geometry"); 211 } 212 } 213 214 } 215 216 217 public void dumpIndexedGeometryArray(IndexedGeometryArray geom, Transform3D t3D){ 218 debugln("dumpIndexedGeometryArray"); 219 int count = geom.getVertexCount(); 220 int index = geom.getIndexCount(); 221 int valid = geom.getValidIndexCount(); 222 debugln(count +" vertexes "+ index +" repetitions "); 223 double[] vertex = new double[3]; 224 225 for (int i = 0; i < index; i++) { 226 int current = geom.getCoordinateIndex(i); 227 geom.getCoordinate(current, vertex); 228 if (i % 2 == 0){ 229 totalEdges++; 230 } 231 232 tempP3d.set(vertex); 233 t3D.transform(tempP3d); 234 235 printPoint(tempP3d); 236 237 /* 238 tempP3d.get(vertex); 239 for (int j = 0; j < vertex.length; j++) { 240 output.append(vertex[j] + "\n"); 241 } 242 */ 243 244 } 245 } 246 247 public void dumpGeometryArray(GeometryArray geom, Transform3D t3D){ 248 debugln("dumpGeometryArray"); 249 int count = geom.getVertexCount(); 250 int format = geom.getVertexFormat(); 251 debugln(count + "vertexes"); 252 double[] vertex = new double[3]; 253 java.text.DecimalFormat df = new java.text.DecimalFormat("###.###"); 254 //df.setMaximumFractionDigits(3); 255 //df.setMinimumFractionDigits(3); 256 257 // Put some kind of test to allow only real edges objects here 258 for (int i=0; i < count; i++) { 259 geom.getCoordinate(i, vertex); 260 261 // Count the total number of vertexes 262 if (i % 2 == 0){ 263 totalEdges++; 264 } 265 tempP3d.set(vertex); 266 t3D.transform(tempP3d); 267 df.applyPattern("###.###"); 268 269 vBuffer.append(df.format(tempP3d.x)+"; "+df.format(tempP3d.y)+"; "+df.format(tempP3d.z)+";\n"); 270 printPoint(tempP3d); 271 /* 272 tempP3d.get(vertex); 273 for (int j = 0; j < vertex.length; j++) { 274 output.append(vertex[j] + "\n"); 275 } 276 */ 277 } 278 System.out.println(vBuffer); 279 vBuffer.setLength(0); 280 } 281 282 int ptcounter = 0; 283 Point3d firstPoint = new Point3d(); 284 Point3d secondPoint = new Point3d(); 285 286 void printPoint(Point3d point){ 287 if ((ptcounter !=0) && ((ptcounter % geometrySize)== 0)){ 288 // Draw edges 289 for (int i=0; i < geometrySize -1; i++) { 290 /* Not necessary anymore 291 if (GUIControl.debugflag){ 292 guiControl.setColorAlternation(false); 293 guiControl.setGizmoColor((byte)0xff, (byte)0xff, (byte)0xff); 294 guiControl.addLine(new Point3d(points[i]), new Point3d(points[i+1])); 295 } 296 */ 297 298 guiControl.addLineSegment(new Point3d(points[i]), new Point3d(points[i+1])); 299 300 // Write edges to StringBuffer 301 output.append(points[i].x+"\n"); 302 output.append(points[i].y+"\n"); 303 output.append(points[i].z+"\n"); 304 output.append(points[i+1].x+"\n"); 305 output.append(points[i+1].y+"\n"); 306 output.append(points[i+1].z+"\n"); 307 } 308 } 309 points[ptcounter % geometrySize].set(point); 310 ptcounter++; 311 312 } 313 314 315 void printPointReset(){ 316 ptcounter = 0; 317 } 318 319 public void debugln(String s){ 320 System.out.println("DumpEdgesAction: "+s); 321 } 322 323 }

This page was automatically generated by Maven