$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.util; 00009 00010 import java.awt.Color; 00011 import java.io.Serializable; 00012 import java.util.ArrayList; 00013 00014 import javax.vecmath.Point3f; 00015 import javax.vecmath.Vector3f; 00016 00017 import processing.core.PGraphics; 00018 import edu.tum.cs.vis.model.Model; 00019 00027 public class Group implements Serializable { 00028 00032 private final Model model; 00033 00037 private static final long serialVersionUID = -3347910663268579041L; 00041 protected Float minX = null; 00045 protected Float maxX = null; 00049 protected Float minY = null; 00053 protected Float maxY = null; 00057 protected Float minZ = null; 00061 protected Float maxZ = null; 00062 00066 private Mesh mesh = new Mesh(); 00067 00071 private String name; 00072 00076 private ArrayList<Group> children = new ArrayList<Group>(); 00077 00084 public Group(Model parent) { 00085 model = parent; 00086 } 00087 00094 public void addChild(Group g) { 00095 children.add(g); 00096 } 00097 00107 public void draw(PGraphics g, Color overrideColor) { 00108 mesh.drawLines(g, overrideColor); 00109 mesh.drawTriangles(g, overrideColor); 00110 for (Group gr : children) { 00111 gr.draw(g, overrideColor); 00112 } 00113 } 00114 00122 public boolean removeTriangle(Triangle t) { 00123 if (mesh.getTriangles().remove(t)) 00124 return true; 00125 for (Group gr : children) { 00126 boolean ret = gr.removeTriangle(t); 00127 if (ret) 00128 return ret; 00129 } 00130 return false; 00131 } 00132 00142 public void drawBoundingBox(PGraphics gr, boolean recursive) { 00143 mesh.drawBoundingBox(gr); 00144 if (recursive) { 00145 for (Group g : children) { 00146 g.drawBoundingBox(gr, recursive); 00147 } 00148 } 00149 } 00150 00157 public void addTriangle(Triangle t) { 00158 synchronized (mesh.getTriangles()) { 00159 mesh.getTriangles().add(t); 00160 } 00161 synchronized (model.getTriangles()) { 00162 model.getTriangles().add(t); 00163 } 00164 } 00165 00171 public ArrayList<Group> getChildren() { 00172 return children; 00173 } 00174 00187 public void getIntersectedTriangles(final Point3f rayStart, final Point3f rayEnd, 00188 final ArrayList<IntersectedTriangle> intersectedTriangles) { 00189 mesh.getIntersectedTriangles(rayStart, rayEnd, intersectedTriangles); 00190 00191 for (Group g : children) { 00192 g.getIntersectedTriangles(rayStart, rayEnd, intersectedTriangles); 00193 } 00194 00195 } 00196 00203 public Float getMaxX() { 00204 if (maxX == null) 00205 getTotalWidth(); 00206 return maxX; 00207 } 00208 00215 public Float getMaxY() { 00216 if (maxY == null) 00217 getTotalHeight(); 00218 return maxY; 00219 } 00220 00227 public Float getMaxZ() { 00228 if (maxZ == null) 00229 getTotalDepth(); 00230 return maxZ; 00231 } 00232 00238 public Mesh getMesh() { 00239 return mesh; 00240 } 00241 00248 public Float getMinX() { 00249 if (minX == null) 00250 getTotalWidth(); 00251 return minX; 00252 } 00253 00260 public Float getMinY() { 00261 if (minY == null) 00262 getTotalHeight(); 00263 return minY; 00264 } 00265 00272 public Float getMinZ() { 00273 if (minZ == null) 00274 getTotalDepth(); 00275 return minZ; 00276 } 00277 00283 public Model getModel() { 00284 return model; 00285 } 00286 00292 public String getName() { 00293 return name; 00294 } 00295 00302 public float getTotalDepth() { 00303 if (minZ == null || maxZ == null) { 00304 minZ = mesh.getMinZ(); 00305 maxZ = mesh.getMaxZ(); 00306 00307 for (Group g : children) { 00308 minZ = Math.min(minZ, g.getMinZ()); 00309 maxZ = Math.max(maxZ, g.getMaxZ()); 00310 00311 } 00312 } 00313 return maxZ - minZ; 00314 } 00315 00322 public float getTotalHeight() { 00323 if (minY == null || maxY == null) { 00324 minY = mesh.getMinY(); 00325 maxY = mesh.getMaxY(); 00326 00327 for (Group g : children) { 00328 minY = Math.min(minY, g.getMinY()); 00329 maxY = Math.max(maxY, g.getMaxY()); 00330 00331 } 00332 } 00333 return maxY - minY; 00334 } 00335 00342 public float getTotalWidth() { 00343 if (minX == null || maxX == null) { 00344 minX = mesh.getMinX(); 00345 maxX = mesh.getMaxX(); 00346 00347 for (Group g : children) { 00348 minX = Math.min(minX, g.getMinX()); 00349 maxX = Math.max(maxX, g.getMaxX()); 00350 00351 } 00352 } 00353 return maxX - minX; 00354 } 00355 00364 public void initialize(String textureBasePath) { 00365 initialize(textureBasePath, true); 00366 } 00367 00373 private void initialize(String textureBasePath, boolean isRoot) { 00374 // Initialize min and max values of mesh 00375 mesh.setTextureBasePath(textureBasePath); 00376 mesh.getWidth(); 00377 mesh.getHeight(); 00378 mesh.getDepth(); 00379 for (Group g : children) { 00380 g.initialize(textureBasePath, false); 00381 } 00382 getTotalWidth(); 00383 getTotalHeight(); 00384 getTotalDepth(); 00385 if (isRoot) { 00386 Vector3f translation = new Vector3f((minX + (getTotalWidth() / 2)) * (-1), 00387 (minY + (getTotalHeight() / 2)) * (-1), (minZ + (getTotalDepth() / 2)) * (-1)); 00388 for (Vertex v : model.getVertices()) 00389 v.add(translation); 00390 } 00391 } 00392 00397 public void resetMinMaxValues() { 00398 mesh.resetMinMaxValues(); 00399 00400 minX = maxX = minY = maxY = minZ = maxZ = null; 00401 00402 for (Group g : children) { 00403 g.resetMinMaxValues(); 00404 } 00405 } 00406 00413 public void setChildren(ArrayList<Group> children) { 00414 this.children = children; 00415 } 00416 00423 public void setMesh(Mesh mesh) { 00424 this.mesh = mesh; 00425 } 00426 00433 public void setName(String name) { 00434 this.name = name; 00435 } 00436 00437 }