VisClient/org/hfbk/vis/visnode/VisNode.java

Go to the documentation of this file.
00001 /*
00002  * Created on 15.04.2008
00003  */
00004 package org.hfbk.vis.visnode;
00005 
00006 import java.awt.AWTEvent;
00007 import java.util.*;
00008 import java.util.concurrent.CopyOnWriteArrayList;
00009 
00010 import org.dronus.gl.GLUtil;
00011 import org.dronus.graph.Node;
00012 import org.lwjgl.opengl.GL11;
00013 import org.lwjgl.util.vector.Matrix4f;
00014 import org.lwjgl.util.vector.Vector3f;
00015 import org.lwjgl.util.vector.Vector4f;
00016 
00027 public abstract class VisNode {
00028 
00029         public Node node;
00030         
00036         public String url;
00037         
00038         public VisNode parent;
00039         
00041         public List<VisNode> children=new CopyOnWriteArrayList<VisNode>();
00042         
00044         public Vector3f position;
00045         
00046         public Matrix4f currentTransform=new Matrix4f();
00047         
00055         public float radius=10;  
00056         
00058         public boolean layoutLocked=false;
00059         
00061         protected VisNode(Node node, Vector3f position){
00062                 this.node=node;
00063                 this.position=new Vector3f(position);
00064         }
00065         
00071         public void add(VisNode node){
00072                 children.add(node);
00073                 node.parent=this;
00074         } 
00075         
00079         public void remove(VisNode node){
00080                 children.remove(node);
00081                 //node.parent=null;
00082         }
00083         
00087         public VisRoot getRoot(){
00088                 VisNode n=this;
00089                 while (n.parent!=null)
00090                         n=n.parent;
00091                 return (VisRoot)n; 
00092         }
00093         
00112         public Vector3f traverse(VisNode other, Vector3f pos){
00113                 pos=new Vector3f(pos);
00114                 
00115                 Matrix4f xform=new Matrix4f();          
00116                 xform.load(other.currentTransform);
00117                 //invert the transform to get from root space to others local space 
00118                 xform.invert(); 
00119                 Matrix4f.mul(xform, currentTransform, xform);
00120                 
00121                 //transform pos from root space to others local space
00122                 Vector4f newpos=Matrix4f.transform(xform, new Vector4f(pos.x,pos.y,pos.z,1), null);
00123                 
00124                 return new Vector3f(newpos); //return as 3d vector
00125         }
00126         
00135         public void render(List<AWTEvent> events){
00136                 GL11.glPushMatrix();
00137                 
00138                 transform();
00139                 GLUtil.getTransform(currentTransform); //store transform 
00140                                                                                                 //for traverse(.) etc.
00141                 if(GLUtil.showExtents)
00142                         GLUtil.debugDoubleSphere(getExtends(),radius);
00143                 
00144                 //simple sphere vs frustrum culling
00145                 if (radius==Float.POSITIVE_INFINITY || GLUtil.inFrustum(getExtends())){
00146                         
00147                         //handle events here so the handler can retrieve the nodes transformation
00148                         handleEvents(events);
00149                         renderChildren(events);
00150                         renderSelf();
00151                 }
00152                 GL11.glPopMatrix();
00153         }
00154         
00155         void renderChildren(List<AWTEvent> events) {
00156                 for(VisNode n: children) n.render(events);              
00157         }
00158         
00164         float getExtends() {
00165                 float r=radius;
00166                 for(VisNode child: children){
00167                         Vector3f pos=child.position;
00168                         float d=pos.length()+child.getExtends();
00169                         r=Math.max(d,r); //the extends is either maximized by
00170                                                         // the child nodes or by this nodes geometry    
00171                 }
00172                 return r; 
00173         }
00174 
00179         public Vector3f center(){
00180                 Vector3f center=new Vector3f();
00181                 for(VisNode n: children)
00182                         Vector3f.add(center, n.position, center);
00183                 center.scale(1f/children.size());
00184                 for(VisNode n: children)
00185                         Vector3f.sub(n.position, center, n.position);
00186                 return center;
00187         }
00188         
00194         void transform(){
00195                 GL11.glTranslatef(position.x,position.y,position.z);            
00196         }
00197         
00206         abstract void renderSelf(); 
00207         
00213         void handleEvents(List<AWTEvent> events){       }
00214         
00216         void closeSelf(){}
00217         
00219         public void close(){
00220                 for(VisNode n: children) n.close();                     
00221                 closeSelf();
00222         }
00223 
00224         static VisNode findNode (VisNode root, Class<?> cl){
00225                 if (cl.isAssignableFrom(root.getClass())) return root; //hurray!
00226                 for (VisNode n: root.children){
00227                         VisNode found=findNode(n,cl);
00228                         if (found!=null) return found;
00229                 }
00230                 return null;
00231         }
00232         
00234         public VisNode findNode(Class<?> cls) {
00235                 return findNode(this, cls);
00236         }
00237 }

Generated on Tue Apr 7 17:57:21 2009 for visclient by  doxygen 1.5.1