00001 
00002 
00003 
00004 
00005 
00006 #ifndef __UIUC_CHARM_COLLIDE_UTIL_H
00007 #define __UIUC_CHARM_COLLIDE_UTIL_H
00008 
00009 #include "pup.h"
00010 #include <math.h>
00011 #include "ckvector3d.h"
00012 typedef CkVector3d vector3d;
00013 typedef double real;
00014 #include "bbox.h"
00015 #include "collide_cfg.h" 
00016 #include "collide_buffers.h" 
00017 
00018 
00019 struct CollideObjID {
00020     int chunk;
00021     int number;
00022     int prio; 
00023     int pe; 
00024     CollideObjID(int chunk_,int number_,int prio_,int pe_) 
00025         :chunk(chunk_),number(number_),prio(prio_),pe(pe_) {}
00026     
00031     inline int shouldCollide(const CollideObjID &b) const {
00032         if (prio<b.prio) return 1; 
00033         return 0;
00034     }
00035 };
00036 
00037 
00038 class Collision {
00039 public:
00040     CollideObjID A,B;
00041     Collision(const CollideObjID &A_, const CollideObjID &B_) :A(A_),B(B_) { }
00042 };
00043 
00044 class CollisionList : public growableBufferT<Collision> {
00045 public:
00046     CollisionList() {}
00047     void add(const CollideObjID &A, const CollideObjID &B) {
00048         push_back(Collision(A,B));
00049     }
00050 };
00051 
00052 
00053 
00054 struct CollideObjRec {
00055     CollideObjID id;
00056     bbox3d box;
00057     CollideObjRec(const CollideObjID &id_,const bbox3d &box_)
00058         :id(id_),box(box_) {}
00059 
00060     const bbox3d &getBbox(void) const {return box;}
00061 };
00062 
00063 
00064 class CollideLoc3d {
00065     
00066     static inline int cls(int x,unsigned int n) {
00067         const unsigned int intBits=8*sizeof(int);
00068         n&=(intBits-1);
00069         return (x<<n)|(x>>(intBits-n));
00070     }
00071 public:
00072     int x,y,z;
00073     CollideLoc3d(int Nx,int Ny,int Nz) {x=Nx;y=Ny;z=Nz;}
00074     CollideLoc3d() {}
00075     int getX() const {return x;}
00076     int getY() const {return y;}
00077     int getZ() const {return z;}
00078     inline unsigned int  hash(void) const {
00079         return cls(x,6)+cls(y,17)+cls(z,28);
00080         
00081     }
00082     static unsigned int staticHash(const void *key,size_t ignored) {
00083         return ((const CollideLoc3d *)key)->hash();
00084     }
00085     inline int compare(const CollideLoc3d &b) const {
00086         return x==b.x && y==b.y && z==b.z;
00087     }
00088     static int staticCompare(const void *k1,const void *k2,size_t ignored) {
00089         return ((const CollideLoc3d *)k1)->compare(*(const CollideLoc3d *)k2);
00090     }
00091 };
00092 
00093 
00095 class CollideGrid3d {
00096     vector3d origin,scales,sizes;
00097     #if COLLIDE_USE_FLOAT_HACK
00098     double hakShift[3]; 
00099     int hakStart[3];
00100     #endif
00101 public:
00102     CollideGrid3d() {}
00103     CollideGrid3d(const vector3d &Norigin,const vector3d &desiredSize) {
00104         init(Norigin,desiredSize);
00105     }
00106     void pup(PUP::er &p);
00107     
00108     void init(const vector3d &Norigin,
00109         const vector3d &desiredSize);
00110     
00111     real world2grid(int axis,real x) const {
00112         return (x-origin[axis])*scales[axis];
00113     }
00114     iSeg1d world2grid(int axis,const rSeg1d &s) const {
00115     #if COLLIDE_USE_FLOAT_HACK
00116      
00117         float fl=(float)(hakShift[axis]+s.getMin());
00118         int lo=*(int *)&fl;
00119         float fh=(float)(hakShift[axis]+s.getMax());
00120         int hi=1+*(int *)&fh;
00121     #else
00122         int lo=(int)floor(world2grid(axis,s.getMin()));
00123         int hi=(int)ceil(world2grid(axis,s.getMax()));
00124     #endif
00125         return iSeg1d(lo,hi);
00126     }
00127     
00128     real grid2world(int axis,real val) const {
00129     #if COLLIDE_USE_FLOAT_HACK
00130         return (val-hakStart[axis])
00131     #else
00132         return val
00133     #endif
00134           *sizes[axis]+origin[axis];
00135     }
00136     rSeg1d grid2world(int axis,rSeg1d src) const {
00137         return rSeg1d(grid2world(axis,src.getMin()),
00138                       grid2world(axis,src.getMax()));
00139     }
00140     
00141     void print(const CollideLoc3d &g);
00142 };
00143 
00144 
00145 #endif