Group.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.io.Serializable;
00011 import java.util.ArrayList;
00012 import java.util.Collection;
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, DrawSettings drawSettings) {
00108                 mesh.drawLines(g, drawSettings);
00109                 mesh.drawTriangles(g, drawSettings);
00110                 for (Group gr : children) {
00111                         gr.draw(g, drawSettings);
00112                 }
00113         }
00114 
00122         public boolean removeTriangle(Triangle t) {
00123                 synchronized (mesh.getTriangles()) {
00124                         if (mesh.getTriangles().remove(t))
00125                                 return true;
00126                 }
00127                 for (Group gr : children) {
00128                         boolean ret = gr.removeTriangle(t);
00129                         if (ret)
00130                                 return ret;
00131                 }
00132                 return false;
00133         }
00134 
00141         public void removeTriangle(Collection<Triangle> t) {
00142                 synchronized (mesh.getTriangles()) {
00143                         mesh.getTriangles().removeAll(t);
00144                 }
00145                 for (Group gr : children) {
00146                         gr.removeTriangle(t);
00147                 }
00148         }
00149 
00159         public void drawBoundingBox(PGraphics gr, boolean recursive) {
00160                 mesh.drawBoundingBox(gr);
00161                 if (recursive) {
00162                         for (Group g : children) {
00163                                 g.drawBoundingBox(gr, recursive);
00164                         }
00165                 }
00166         }
00167 
00174         public void addTriangle(Triangle t) {
00175                 synchronized (mesh.getTriangles()) {
00176                         mesh.getTriangles().add(t);
00177                 }
00178                 synchronized (model.getTriangles()) {
00179                         model.getTriangles().add(t);
00180                 }
00181         }
00182 
00188         public ArrayList<Group> getChildren() {
00189                 return children;
00190         }
00191 
00204         public void getIntersectedTriangles(final Point3f rayStart, final Point3f rayEnd,
00205                         final ArrayList<IntersectedTriangle> intersectedTriangles) {
00206                 mesh.getIntersectedTriangles(rayStart, rayEnd, intersectedTriangles);
00207 
00208                 for (Group g : children) {
00209                         g.getIntersectedTriangles(rayStart, rayEnd, intersectedTriangles);
00210                 }
00211 
00212         }
00213 
00221         public void getAllTriangles(final Collection<Triangle> triangles) {
00222                 mesh.getAllTriangles(triangles);
00223 
00224                 for (Group g : children) {
00225                         g.getAllTriangles(triangles);
00226                 }
00227         }
00228 
00235         public Float getMaxX() {
00236                 if (maxX == null)
00237                         getTotalWidth();
00238                 return maxX;
00239         }
00240 
00247         public Float getMaxY() {
00248                 if (maxY == null)
00249                         getTotalHeight();
00250                 return maxY;
00251         }
00252 
00259         public Float getMaxZ() {
00260                 if (maxZ == null)
00261                         getTotalDepth();
00262                 return maxZ;
00263         }
00264 
00270         public Mesh getMesh() {
00271                 return mesh;
00272         }
00273 
00280         public Float getMinX() {
00281                 if (minX == null)
00282                         getTotalWidth();
00283                 return minX;
00284         }
00285 
00292         public Float getMinY() {
00293                 if (minY == null)
00294                         getTotalHeight();
00295                 return minY;
00296         }
00297 
00304         public Float getMinZ() {
00305                 if (minZ == null)
00306                         getTotalDepth();
00307                 return minZ;
00308         }
00309 
00315         public Model getModel() {
00316                 return model;
00317         }
00318 
00324         public String getName() {
00325                 return name;
00326         }
00327 
00334         public float getTotalDepth() {
00335                 if (minZ == null || maxZ == null) {
00336                         minZ = mesh.getMinZ();
00337                         maxZ = mesh.getMaxZ();
00338 
00339                         for (Group g : children) {
00340                                 minZ = Math.min(minZ, g.getMinZ());
00341                                 maxZ = Math.max(maxZ, g.getMaxZ());
00342 
00343                         }
00344                 }
00345                 return maxZ - minZ;
00346         }
00347 
00354         public float getTotalHeight() {
00355                 if (minY == null || maxY == null) {
00356                         minY = mesh.getMinY();
00357                         maxY = mesh.getMaxY();
00358 
00359                         for (Group g : children) {
00360                                 minY = Math.min(minY, g.getMinY());
00361                                 maxY = Math.max(maxY, g.getMaxY());
00362 
00363                         }
00364                 }
00365                 return maxY - minY;
00366         }
00367 
00374         public float getTotalWidth() {
00375                 if (minX == null || maxX == null) {
00376                         minX = mesh.getMinX();
00377                         maxX = mesh.getMaxX();
00378 
00379                         for (Group g : children) {
00380                                 minX = Math.min(minX, g.getMinX());
00381                                 maxX = Math.max(maxX, g.getMaxX());
00382 
00383                         }
00384                 }
00385                 return maxX - minX;
00386         }
00387 
00396         public void initialize(String textureBasePath) {
00397                 initialize(textureBasePath, true);
00398         }
00399 
00409         private void initialize(String textureBasePath, boolean isRoot) {
00410                 // Initialize min and max values of mesh
00411                 mesh.setTextureBasePath(textureBasePath);
00412                 mesh.getWidth();
00413                 mesh.getHeight();
00414                 mesh.getDepth();
00415                 for (Group g : children) {
00416                         g.initialize(textureBasePath, false);
00417                 }
00418                 getTotalWidth();
00419                 getTotalHeight();
00420                 getTotalDepth();
00421                 if (isRoot) {
00422                         Vector3f translation = new Vector3f((minX + (getTotalWidth() / 2)) * (-1),
00423                                         (minY + (getTotalHeight() / 2)) * (-1), (minZ + (getTotalDepth() / 2)) * (-1));
00424                         for (Vertex v : model.getVertices())
00425                                 v.add(translation);
00426                 }
00427         }
00428 
00433         public void resetMinMaxValues() {
00434                 mesh.resetMinMaxValues();
00435 
00436                 minX = maxX = minY = maxY = minZ = maxZ = null;
00437 
00438                 for (Group g : children) {
00439                         g.resetMinMaxValues();
00440                 }
00441         }
00442 
00449         public void setChildren(ArrayList<Group> children) {
00450                 this.children = children;
00451         }
00452 
00459         public void setMesh(Mesh mesh) {
00460                 this.mesh = mesh;
00461         }
00462 
00469         public void setName(String name) {
00470                 this.name = name;
00471         }
00472 
00473 }


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