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


knowrob_cad_parser
Author(s): Stefan Profanter
autogenerated on Mon Oct 6 2014 01:29:56