Go to the documentation of this file.00001 #ifndef OPENTISSUE_COLLISION_SPATIAL_HASHING_HASH_FUNCTIONS_SPATIAL_HASHING_GRID_FUNCTION_H
00002 #define OPENTISSUE_COLLISION_SPATIAL_HASHING_HASH_FUNCTIONS_SPATIAL_HASHING_GRID_FUNCTION_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <cmath>
00013 #include <cassert>
00014
00015 namespace OpenTissue
00016 {
00017 namespace spatial_hashing
00018 {
00025 class GridHashFunction
00026 {
00027 protected:
00028
00029 size_t m_size;
00030 int m_dx;
00031
00032 public:
00033
00034 GridHashFunction() { resize(1000); }
00035 GridHashFunction(size_t size) { resize(size); }
00036
00037 public:
00038
00039 size_t operator()( int i,int j, int k )
00040 {
00041 while(i<0) i += m_dx;
00042 while(j<0) j += m_dx;
00043 while(k<0) k += m_dx;
00044 if(i >= m_dx) i = i % m_dx;
00045 if(j >= m_dx) j = j % m_dx;
00046 if(k >= m_dx) k = k % m_dx;
00047
00048 int hash_key = (i*m_dx + j)*m_dx + k;
00049 hash_key = hash_key % m_size;
00050
00051 assert( hash_key >= 0 );
00052 assert( static_cast<size_t>(hash_key) < m_size );
00053 return hash_key;
00054 }
00055
00056 void resize(size_t new_size)
00057 {
00058 using std::floor;
00059
00060 float b = static_cast<float>(new_size);
00061 float a = pow( b , 1.0f/3.0f);
00062 m_dx = static_cast<int>( floor(a + 0.5f) );
00063 if(m_dx < 2) m_dx = 2;
00064 m_size = m_dx*m_dx*m_dx;
00065 }
00066
00067 size_t size()const { return m_size; }
00068 };
00069
00070 }
00071
00072 }
00073
00074
00075 #endif