00001 package edu.tum.cs.vis.model.parser;
00002
00003 import java.io.BufferedInputStream;
00004 import java.io.BufferedOutputStream;
00005 import java.io.File;
00006 import java.io.FileOutputStream;
00007 import java.io.IOException;
00008 import java.util.Enumeration;
00009 import java.util.HashMap;
00010 import java.util.Iterator;
00011 import java.util.LinkedList;
00012 import java.util.zip.ZipEntry;
00013 import java.util.zip.ZipFile;
00014
00015 import javax.vecmath.Point3f;
00016
00017 import processing.core.PApplet;
00018 import edu.tum.cs.vis.model.util.Line;
00019 import edu.tum.cs.vis.model.util.Triangle;
00020
00027 public abstract class ModelParser {
00028
00032 protected LinkedList<Triangle> triangles = new LinkedList<Triangle>();
00036 protected LinkedList<Line> lines = new LinkedList<Line>();
00037
00041 protected Float minX = null;
00042 protected Float maxX = null;
00043 protected Float minY = null;
00044 protected Float maxY = null;
00045 protected Float minZ = null;
00046 protected Float maxZ = null;
00047
00052 private Point3f currentPosition = null;
00053
00058 private static final HashMap<String,Class<? extends ModelParser>> extensionAssignment = new HashMap<String,Class<? extends ModelParser>>();
00059 static {
00060
00061
00062
00063 extensionAssignment.put("dae", ColladaParser.class);
00064 extensionAssignment.put("kmz", ColladaParser.class);
00065 }
00066
00072 protected static String getExtension(String filename)
00073 {
00074 return filename.substring(filename.lastIndexOf('.') + 1);
00075 }
00076
00082 protected boolean isValidExtension(String filename)
00083 {
00084 if (extensionAssignment.get(getExtension(filename)) == this.getClass())
00085 return true;
00086 return false;
00087 }
00088
00093 protected boolean checkExtension(String filename) {
00094 if (!isValidExtension(filename))
00095 {
00096 System.out.println("Unknown file extension for class: " + this.getClass().getName() + " " + filename + "\n Must be one of: " + getValidExtensions());
00097 return false;
00098 } else {
00099 return true;
00100 }
00101 }
00102
00103
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
00126 protected void drawTriangles(PApplet applet) {
00127
00128
00129 for (Triangle tri : triangles) {
00130
00131 tri.draw(applet);
00132 }
00133 }
00134
00141 protected void drawLines(PApplet applet) {
00142
00143
00144 for (Line line : lines) {
00145
00146 line.draw(applet);
00147 }
00148 }
00149
00156 public void drawBoundingBox(PApplet applet) {
00157
00158 applet.pushMatrix();
00159 applet.translate(currentPosition.x, currentPosition.y,
00160 currentPosition.z);
00161 applet.box(getModelWidth(), getModelHeight(), getModelDepth());
00162
00163
00164 applet.popMatrix();
00165 }
00166
00171 protected void centerModel() {
00172
00173 getModelWidth();
00174 getModelHeight();
00175 getModelDepth();
00176 currentPosition = new Point3f(minX + (getModelWidth() / 2), minY
00177 + (getModelHeight() / 2), minZ + (getModelDepth() / 2));
00178 setModelPosition(new Point3f(0, 0, 0));
00179 }
00180
00187 protected void scaleModel(float factor) {
00188 for (Triangle tri : triangles) {
00189
00190 tri.scale(factor);
00191 }
00192 for (Line line : lines) {
00193
00194 line.scale(factor);
00195 }
00196 minX *= factor;
00197 maxX *= factor;
00198 minY *= factor;
00199 maxY *= factor;
00200 minZ *= factor;
00201 maxZ *= factor;
00202 }
00203
00209 protected void setModelPosition(Point3f pos) {
00210 if (currentPosition == null)
00211 centerModel();
00212 for (Triangle tri : triangles) {
00213
00214 tri.translate(pos.x - currentPosition.x, pos.y - currentPosition.y,
00215 pos.z - currentPosition.z);
00216 }
00217 for (Line line : lines) {
00218
00219 line.translate(pos.x - currentPosition.x,
00220 pos.y - currentPosition.y, pos.z - currentPosition.z);
00221 }
00222 currentPosition = pos;
00223 }
00224
00229 public abstract void draw(PApplet applet);
00230
00235 public abstract boolean loadModel(String filename);
00236
00242 public static Class<? extends ModelParser> findParser(String filename)
00243 {
00244 return extensionAssignment.get(getExtension(filename));
00245 }
00246
00253 public float getModelHeight() {
00254 if (minY != null && maxY != null)
00255 return Math.abs(maxY - minY);
00256 minY = Float.MAX_VALUE;
00257 maxY = Float.MIN_VALUE;
00258
00259 for (Triangle tri : triangles) {
00260 for (int v = 0; v < 3; v++) {
00261 minY = Math.min(tri.position[v].y, minY);
00262 maxY = Math.max(tri.position[v].y, maxY);
00263 }
00264 }
00265 return maxY - minY;
00266 }
00267
00274 public float getModelWidth() {
00275 if (minX != null && maxX != null)
00276 return Math.abs(maxX - minX);
00277 minX = Float.MAX_VALUE;
00278 maxX = Float.MIN_VALUE;
00279
00280 for (Triangle tri : triangles) {
00281 for (int v = 0; v < 3; v++) {
00282 minX = Math.min(tri.position[v].x, minX);
00283 maxX = Math.max(tri.position[v].x, maxX);
00284 }
00285 }
00286 return maxX - minX;
00287 }
00288
00295 public float getModelDepth() {
00296 if (minZ != null && maxZ != null)
00297 return Math.abs(maxZ - minZ);
00298 minZ = Float.MAX_VALUE;
00299 maxZ = Float.MIN_VALUE;
00300
00301 for (Triangle tri : triangles) {
00302 for (int v = 0; v < 3; v++) {
00303 minZ = Math.min(tri.position[v].z, minZ);
00304 maxZ = Math.max(tri.position[v].z, maxZ);
00305 }
00306 }
00307 return maxZ - minZ;
00308 }
00309
00316 public static boolean Unzip(String zipFile, String outputDirectory) {
00317 if (!outputDirectory.endsWith("/") && !outputDirectory.endsWith("\\"))
00318 outputDirectory += "/";
00319
00320 BufferedOutputStream dest = null;
00321 BufferedInputStream is = null;
00322 int BUFFER = 2048;
00323 ZipEntry entry;
00324 ZipFile zipfile;
00325 try {
00326 zipfile = new ZipFile(zipFile);
00327 Enumeration<? extends ZipEntry> e = zipfile.entries();
00328 while (e.hasMoreElements()) {
00329 entry = (ZipEntry) e.nextElement();
00330 if (entry.isDirectory()) {
00331 (new File(outputDirectory + entry.getName())).mkdir();
00332 continue;
00333 }
00334
00335 String filename = outputDirectory + entry.getName();
00336 String filePath = filename.substring(0,
00337 filename.lastIndexOf(File.separator));
00338
00339
00340 if (!(new File(filePath)).exists()) {
00341 (new File(filePath)).mkdirs();
00342 }
00343 is = new BufferedInputStream(zipfile.getInputStream(entry));
00344 int count;
00345 byte data[] = new byte[BUFFER];
00346 FileOutputStream fos = new FileOutputStream(filename);
00347 dest = new BufferedOutputStream(fos, BUFFER);
00348 while ((count = is.read(data, 0, BUFFER)) != -1) {
00349 dest.write(data, 0, count);
00350 }
00351 dest.flush();
00352 dest.close();
00353 is.close();
00354 }
00355 } catch (IOException e1) {
00356
00357 System.err.println("Couldn't unzip file: " + zipFile);
00358 e1.printStackTrace();
00359 return false;
00360 }
00361 return true;
00362 }
00363
00369 public static File createTempDirectory() throws IOException {
00370 final File temp;
00371
00372 temp = File.createTempFile("temp", Long.toString(System.nanoTime()));
00373
00374 if (!(temp.delete())) {
00375 throw new IOException("Could not delete temp file: "
00376 + temp.getAbsolutePath());
00377 }
00378
00379 if (!(temp.mkdir())) {
00380 throw new IOException("Could not create temp directory: "
00381 + temp.getAbsolutePath());
00382 }
00383
00384 return (temp);
00385 }
00386
00387 }