Mesh.java
Go to the documentation of this file.
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.util;
00009 
00010 import java.awt.Image;
00011 import java.awt.image.BufferedImage;
00012 import java.io.File;
00013 import java.io.IOException;
00014 import java.io.Serializable;
00015 import java.util.ArrayList;
00016 import java.util.Arrays;
00017 import java.util.Collection;
00018 import java.util.HashMap;
00019 import java.util.HashSet;
00020 import java.util.List;
00021 import java.util.Set;
00022 
00023 import javax.imageio.ImageIO;
00024 import javax.vecmath.Point3f;
00025 
00026 import processing.core.PGraphics;
00027 import processing.core.PImage;
00028 import edu.tum.cs.ias.knowrob.utils.FileUtil;
00029 
00039 public class Mesh implements Serializable {
00040 
00044         private static final long       serialVersionUID        = -2761386921853967131L;
00045 
00049         protected Float                         minX                            = null;
00053         protected Float                         maxX                            = null;
00057         protected Float                         minY                            = null;
00061         protected Float                         maxY                            = null;
00065         protected Float                         minZ                            = null;
00069         protected Float                         maxZ                            = null;
00074         private boolean                         texturesInitialized     = false;
00075 
00079         private String                          textureBasePath;
00083         private ArrayList<Triangle>     triangles                       = new ArrayList<Triangle>();
00084 
00088         private ArrayList<Line>         lines                           = new ArrayList<Line>();
00089 
00096         public void drawBoundingBox(PGraphics g) {
00097                 if (triangles.size() == 0 && lines.size() == 0)
00098                         return;
00099                 // Save current translation
00100                 g.pushMatrix();
00101                 g.translate(getMaxX() - getWidth() / 2f, getMaxY() - getHeight() / 2f, getMaxZ()
00102                                 - getDepth() / 2f);
00103                 g.box(getWidth(), getHeight(), getDepth());
00104 
00105                 // Restore last translation
00106                 g.popMatrix();
00107         }
00108 
00118         public void drawLines(PGraphics g, DrawSettings drawSettings) {
00119                 for (Line line : lines) {
00120 
00121                         line.draw(g, drawSettings);
00122                 }
00123         }
00124 
00134         public void drawTriangles(PGraphics g, DrawSettings drawSettings) {
00135                 if (!texturesInitialized)
00136                         setTextureImage();
00137                 synchronized (triangles) {
00138                         for (Triangle tri : triangles) {
00139                                 tri.draw(g, drawSettings);
00140                         }
00141                 }
00142         }
00143 
00150         public float getDepth() {
00151                 if (minZ != null && maxZ != null)
00152                         return Math.abs(maxZ - minZ);
00153                 minZ = Float.MAX_VALUE;
00154                 maxZ = Float.MIN_VALUE;
00155                 if (triangles.size() > 0) {
00156 
00157                         for (Triangle tri : triangles) {
00158                                 for (int v = 0; v < 3; v++) {
00159                                         minZ = Math.min(tri.position[v].z, minZ);
00160                                         maxZ = Math.max(tri.position[v].z, maxZ);
00161                                 }
00162                         }
00163                         for (Line line : lines) {
00164                                 for (int v = 0; v < 2; v++) {
00165                                         minZ = Math.min(line.position[v].z, minZ);
00166                                         maxZ = Math.max(line.position[v].z, maxZ);
00167                                 }
00168                         }
00169                 }
00170                 return maxZ - minZ;
00171         }
00172 
00179         public float getHeight() {
00180                 if (minY != null && maxY != null)
00181                         return Math.abs(maxY - minY);
00182                 minY = Float.MAX_VALUE;
00183                 maxY = Float.MIN_VALUE;
00184                 if (triangles.size() > 0) {
00185 
00186                         for (Triangle tri : triangles) {
00187                                 for (int v = 0; v < 3; v++) {
00188                                         minY = Math.min(tri.position[v].y, minY);
00189                                         maxY = Math.max(tri.position[v].y, maxY);
00190                                 }
00191                         }
00192                         for (Line line : lines) {
00193                                 for (int v = 0; v < 2; v++) {
00194                                         minY = Math.min(line.position[v].y, minY);
00195                                         maxY = Math.max(line.position[v].y, maxY);
00196                                 }
00197                         }
00198                 }
00199                 return maxY - minY;
00200         }
00201 
00214         public void getIntersectedTriangles(final Point3f rayStart, final Point3f rayEnd,
00215                         final ArrayList<IntersectedTriangle> intersectedTriangles) {
00216 
00217                 for (Triangle tri : triangles) {
00218 
00219                         Point3f intersect = new Point3f();
00220                         if (tri.intersectsRay(rayStart, rayEnd, intersect))
00221                                 intersectedTriangles.add(new IntersectedTriangle(tri, intersect));
00222                 }
00223 
00224         }
00225 
00231         public ArrayList<Line> getLines() {
00232                 return lines;
00233         }
00234 
00241         public Float getMaxX() {
00242                 if (maxX == null)
00243                         getWidth();
00244                 return maxX;
00245         }
00246 
00253         public Float getMaxY() {
00254                 if (maxY == null)
00255                         getHeight();
00256                 return maxY;
00257         }
00258 
00265         public Float getMaxZ() {
00266                 if (maxZ == null)
00267                         getDepth();
00268                 return maxZ;
00269         }
00270 
00277         public Float getMinX() {
00278                 if (minX == null)
00279                         getWidth();
00280                 return minX;
00281         }
00282 
00289         public Float getMinY() {
00290                 if (minY == null)
00291                         getHeight();
00292                 return minY;
00293         }
00294 
00301         public Float getMinZ() {
00302                 if (minZ == null)
00303                         getDepth();
00304                 return minZ;
00305         }
00306 
00312         public String getTextureBasePath() {
00313                 return textureBasePath;
00314         }
00315 
00321         public List<Triangle> getTriangles() {
00322                 return triangles;
00323         }
00324 
00330         public Set<Vertex> getVertices() {
00331                 Set<Vertex> v = new HashSet<Vertex>(triangles.size() * 2);
00332                 for (Triangle t : triangles)
00333                         v.addAll(Arrays.asList(t.getPosition()));
00334                 return v;
00335         }
00336 
00343         public float getWidth() {
00344                 if (minX != null && maxX != null)
00345                         return Math.abs(maxX - minX);
00346 
00347                 minX = Float.MAX_VALUE;
00348                 maxX = Float.MIN_VALUE;
00349                 if (triangles.size() > 0) {
00350 
00351                         for (Triangle tri : triangles) {
00352                                 for (int v = 0; v < 3; v++) {
00353                                         minX = Math.min(tri.position[v].x, minX);
00354                                         maxX = Math.max(tri.position[v].x, maxX);
00355                                 }
00356                         }
00357                         for (Line line : lines) {
00358                                 for (int v = 0; v < 2; v++) {
00359                                         minX = Math.min(line.position[v].x, minX);
00360                                         maxX = Math.max(line.position[v].x, maxX);
00361                                 }
00362                         }
00363                 }
00364                 return maxX - minX;
00365         }
00366 
00371         public void resetMinMaxValues() {
00372                 minX = maxX = minY = maxY = minZ = maxZ = null;
00373         }
00374 
00381         public void setLines(ArrayList<Line> lines) {
00382                 this.lines = lines;
00383         }
00384 
00391         public void setTextureBasePath(String textureBasePath) {
00392                 this.textureBasePath = textureBasePath;
00393         }
00394 
00400         private void setTextureImage() {
00401                 if (textureBasePath == null)
00402                         return;
00403                 // load all Texture-Images only once (memory efficiency)
00404                 HashMap<String, PImage> pictures = new HashMap<String, PImage>();
00405                 HashSet<String> alreadyLoaded = new HashSet<String>();// if error reading file,
00406                                                                                                                                 // pictures<path> is null. So avoid
00407                                                                                                                                 // trying again
00408                 synchronized (triangles) {
00409                         for (Triangle tri : triangles) {
00410                                 if (tri.appearance.getImageFileName() == null)
00411                                         continue;
00412                                 String texfile = FileUtil.getAbsoluteFilePath(textureBasePath,
00413                                                 tri.appearance.getImageFileName());
00414                                 if (pictures.get(texfile) == null && !alreadyLoaded.contains(texfile)) {
00415 
00416                                         alreadyLoaded.add(texfile);
00417                                         BufferedImage bimg = null;
00418                                         try {
00419                                                 bimg = ImageIO.read(new File(texfile));
00420                                                 // Convert BufferedImage to Image otherwise PImage constructor will fail!!
00421 
00422                                                 Image i = bimg.getScaledInstance(bimg.getWidth(), bimg.getHeight(), 0);
00423 
00424                                                 PImage pImg = new PImage(i);
00425 
00426                                                 pictures.put(texfile, pImg);
00427                                         } catch (IOException e) {
00428                                                 System.err.println("Couldn't read file: " + texfile);
00429                                         }
00430 
00431                                 }
00432                         }
00433 
00434                 }
00435 
00436                 // Now remove offset of texture coordinates because there is a bug with P3D when texture
00437                 // should repeated
00438                 synchronized (triangles) {
00439                         for (Triangle tri : triangles) {
00440 
00441                                 if (tri.appearance.getImageFileName() == null)
00442                                         continue;
00443 
00444                                 String texfile = FileUtil.getAbsoluteFilePath(textureBasePath,
00445                                                 tri.appearance.getImageFileName());
00446                                 // PImage tex = applet.loadImage(texfile);
00447                                 PImage tex = pictures.get(texfile);
00448                                 if (tex == null)
00449                                         continue;
00450 
00451                                 double xMin = Double.MAX_VALUE;
00452                                 double yMin = Double.MAX_VALUE;
00453 
00454                                 for (int i = 0; i < 3; i++) {
00455 
00456                                         double x = tri.texPosition[i].x * tex.width;
00457                                         double y = tex.height - tri.texPosition[i].y * tex.height;
00458                                         tri.texPosition[i].x = (float) x;
00459                                         tri.texPosition[i].y = (float) y;
00460                                         xMin = Math.min(x, xMin);
00461                                         yMin = Math.min(y, yMin);
00462                                 }
00463 
00464                                 // Remove offset of texture coordinate if all coordinates are greater than texture
00465                                 xMin = Math.floor(xMin / tex.width);
00466                                 yMin = Math.floor(yMin / tex.height);
00467 
00468                                 for (int i = 0; i < 3; i++) {
00469                                         tri.texPosition[i].x -= xMin * tex.width;
00470                                         tri.texPosition[i].y -= yMin * tex.height;
00471                                 }
00472 
00473                                 tri.appearance.setImageReference(tex);
00474                         }
00475                 }
00476                 texturesInitialized = true;
00477         }
00478 
00485         public void setTriangles(ArrayList<Triangle> triangles) {
00486                 this.triangles = triangles;
00487         }
00488 
00495         public void getAllTriangles(Collection<Triangle> triangles2) {
00496                 triangles2.addAll(triangles);
00497 
00498         }
00499 
00500 }


knowrob_cad_parser
Author(s): Stefan Profanter
autogenerated on Sat Dec 28 2013 17:09:45