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

Go to the documentation of this file.
00001 package org.hfbk.vis.visnode;
00002 
00003 import java.util.HashMap;
00004 import java.util.LinkedList;
00005 import java.util.List;
00006 import java.util.Random;
00007 
00008 import org.lwjgl.util.vector.Vector3f;
00009 
00010 
00025 public class VisPartitionNode extends VisNode implements VisUI{
00026         float scale;
00027         boolean dirty=false;
00028         
00029         
00030         VisPartitionNode(Vector3f position) {
00031                 super(null, position);
00032         }
00033 
00034         public void add(VisNode node) {
00035                 super.add(node);
00036                 dirty=true;
00037         }
00038         
00039         
00040         
00042         HashMap<Triple,VisPartitionNode> partition=new HashMap<Triple,VisPartitionNode>();      
00043         
00044         
00045 
00046         void balance(){
00047                 
00048                 if (parent instanceof VisPartitionNode){
00049                         //recenter node but keep childrens word position
00050                         Vector3f.add(position,center(),position);                               
00051                 }
00052                 scale=getExtends()/2;
00053                 if(children.size()>30 && scale>20){
00054                         List<VisPartitionNode> newCells=new LinkedList<VisPartitionNode>();
00055                         for (VisNode node: children){
00056                                 Vector3f p=node.position;
00057                                 
00058                                 Triple pair=new Triple(
00059                                         (int)(p.x/scale), 
00060                                         (int)(p.y/scale), 
00061                                         (int)(p.z/scale)                
00062                                 );              
00063                                 VisPartitionNode cell=partition.get(pair);
00064                                 if (cell==null){
00065                                         cell=new VisPartitionNode(new Vector3f());
00066                                         partition.put(pair, cell );
00067                                         newCells.add(cell);
00068                                 }                               
00069                                 cell.add(node);
00070                         }
00071                         
00072                         children.clear();
00073                         
00074                         for(VisNode n: newCells)
00075                                 super.add(n);
00076                         
00077                 }
00078         }
00079         
00080         final Random hasher=new Random();
00081         
00082         class Triple{
00083                 
00084                 int x,y,z;
00085                 public Triple(int x, int y, int z) {
00086                         this.x=x; this.y=y; this.z=z;
00087                 }               
00088                 public int hashCode() {
00089                         hasher.setSeed(x+y*0x10000L+z*0x100000000L);
00090                         return hasher.nextInt();
00091                 }
00092                 public boolean equals(Object o) {
00093                         return (o instanceof Triple 
00094                                         && ((Triple)o).x==x  
00095                                         && ((Triple)o).y==y  
00096                                         && ((Triple)o).z==z  );                 
00097                 }
00098         }
00099         
00100         void renderSelf() {     
00101                 if (dirty){
00102                         balance();
00103                         balance();
00104                         dirty=false;
00105                 }                               
00106         }
00107         
00108 }

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