$search
00001 /******************************************************************************* 00002 * Copyright (c) 2012 Stefan Profanter. All rights reserved. This program and the accompanying 00003 * materials are made available under the terms of the GNU Public License v3.0 which accompanies 00004 * this distribution, and is available at http://www.gnu.org/licenses/gpl.html 00005 * 00006 * Contributors: Stefan Profanter - initial API and implementation, Year: 2012 00007 ******************************************************************************/ 00008 package edu.tum.cs.vis.model.parser; 00009 00010 import java.io.File; 00011 import java.util.HashMap; 00012 import java.util.Iterator; 00013 00014 import com.sun.j3d.utils.geometry.GeometryInfo; 00015 00016 import edu.tum.cs.ias.knowrob.utils.ResourceRetriever; 00017 import edu.tum.cs.vis.model.Model; 00018 import edu.tum.cs.vis.model.util.Vertex; 00019 00027 public abstract class ModelParser { 00028 00033 private static final HashMap<String, Class<? extends ModelParser>> extensionAssignment = new HashMap<String, Class<? extends ModelParser>>(); 00034 static { 00035 /* 00036 * Add here other parsers with file extension 00037 */ 00038 extensionAssignment.put("dae", ColladaParser.class); 00039 extensionAssignment.put("kmz", ColladaParser.class); 00040 extensionAssignment.put("mesh", CustomParser.class); 00041 extensionAssignment.put("ply", PlyParser.class); 00042 } 00043 00049 private static HashMap<String, Model> modelBuffer = new HashMap<String, Model>(); 00050 00058 public static Class<? extends ModelParser> findParser(String filename) { 00059 return extensionAssignment.get(getExtension(filename)); 00060 } 00061 00069 protected static String getExtension(String filename) { 00070 return filename.substring(filename.lastIndexOf('.') + 1); 00071 } 00072 00076 protected Model model; 00077 00085 protected boolean checkExtension(String filename) { 00086 if (!isValidExtension(filename)) { 00087 System.out.println("Unknown file extension for class: " + this.getClass().getName() 00088 + " " + filename + "\n Must be one of: " + getValidExtensions()); 00089 return false; 00090 } 00091 return true; 00092 } 00093 00099 public Model getModel() { 00100 return model; 00101 } 00102 00108 protected String getValidExtensions() { 00109 Iterator<String> it = extensionAssignment.keySet().iterator(); 00110 String extensions = ""; 00111 while (it.hasNext()) { 00112 String key = it.next(); 00113 Object val = extensionAssignment.get(key); 00114 if (val.getClass() == this.getClass()) 00115 extensions += key + ", "; 00116 } 00117 return extensions; 00118 } 00119 00127 protected boolean isValidExtension(String filename) { 00128 if (extensionAssignment.get(getExtension(filename)) == this.getClass()) 00129 return true; 00130 return false; 00131 } 00132 00140 protected abstract boolean loadModel(String filename); 00141 00150 public boolean parseModel(String filename) { 00151 Model m = modelBuffer.get(filename); 00152 boolean retVal = true; 00153 00154 if (m == null) { 00155 model = new Model(); 00156 // get / download file and determine local file path 00157 String file = ResourceRetriever.retrieve(filename).getAbsolutePath(); 00158 00159 if (!checkExtension(file)) { 00160 return false; 00161 } 00162 00163 if ((new File(file)).exists() == false) { 00164 System.err.println("ERROR: Can't load model. File not found: " + file + "\n"); 00165 return false; 00166 } 00167 00168 retVal = loadModel(file); 00169 if (!retVal) 00170 return false; 00171 modelBuffer.put(filename, model); 00172 model.getGroup().initialize(model.getTextureBasePath()); 00173 } else 00174 model = m; 00175 return retVal; 00176 } 00177 00186 public static int[] polygonTriangulation(Vertex vertices[]) { 00187 float coord[] = new float[vertices.length * 3]; 00188 for (int i = 0; i < vertices.length; i++) { 00189 coord[i * 3] = vertices[i].x; 00190 coord[i * 3 + 1] = vertices[i].y; 00191 coord[i * 3 + 2] = vertices[i].z; 00192 } 00193 00194 GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY); 00195 gi.setCoordinates(coord); 00196 00197 int[] stripCounts = { coord.length / 3 }; 00198 gi.setStripCounts(stripCounts); 00199 gi.convertToIndexedTriangles(); 00200 00201 return gi.getCoordinateIndices(); 00202 } 00203 00204 }