00001
00004 package org.hfbk.vis;
00005
00006 import java.util.ArrayList;
00007 import java.util.Collection;
00008 import java.util.HashMap;
00009 import java.util.List;
00010
00011 import org.hfbk.vis.visnode.VisNode;
00012 import org.lwjgl.util.vector.Vector3f;
00013
00014 public class Partition extends HashMap<Partition.Triple, List<VisNode>>{
00015
00016 public static class Triple{
00017 int x,y,z;
00018
00019 public Triple(int x, int y, int z) {
00020 this.x=x; this.y=y; this.z=z;
00021 }
00022 public int hashCode() {
00023 return x^y^z;
00024 }
00025
00026 public boolean equals(Object other) {
00027 if (!(other instanceof Triple)) return false;
00028 Triple ot=(Triple)other;
00029 return ot.x==x && ot.y==y && ot.z==z;
00030 }
00031
00032 public Triple[] getHalfNeighbourhood(){
00033 return new Triple[]{
00034 new Triple(x+1, y, z),
00035 new Triple(x, y+1, z),
00036 new Triple(x, y, z+1),
00037 new Triple(x+1, y+1, z),
00038 new Triple(x-1, y+1, z),
00039 new Triple(x, y+1, z+1),
00040 new Triple(x, y-1, z+1),
00041 new Triple(x+1, y, z+1),
00042 new Triple(x-1, y, z+1),
00043 new Triple(x+1, y+1, z+1),
00044 new Triple(x-1, y+1, z+1),
00045 new Triple(x+1, y-1, z+1),
00046 new Triple(x-1, y-1, z+1)
00047 };
00048 }
00049 }
00050
00051 float cellsize;
00052
00053 public Partition(Collection<VisNode> nodes, float cellsize) {
00054 this.cellsize=cellsize;
00055 for(VisNode n : nodes)
00056 add(n);
00057 }
00058
00059 void add(VisNode n){
00060 Vector3f p=n.position;
00061 Triple t=new Triple((int)(p.x/cellsize), (int)(p.y/cellsize), (int)(p.z/cellsize));
00062
00063 List<VisNode> l=get(t);
00064 if (l==null){
00065 l=new ArrayList<VisNode>();
00066 put(t, l);
00067 }
00068 l.add(n);
00069 }
00070
00071 public float getMeanCount(){
00072 int i=0; float count=0;
00073 for (List l: values()){
00074 count+=l.size();
00075 i++;
00076 }
00077 return count/i;
00078 }
00079
00080 public List<List<VisNode>> getHalfNeighbourhood(Triple cell){
00081 Triple[] half=cell.getHalfNeighbourhood();
00082 List<List<VisNode>> nodes=new ArrayList<List<VisNode>>(half.length);
00083
00084 for (Triple t: half)
00085 nodes.add(get(t));
00086 return nodes;
00087 }
00088 }