Go to the documentation of this file.00001 #ifndef OPENTISSUE_COLLISION_SPATIAL_HASHING_HASH_FUNCTIONS_SPATIAL_HASHING_RANDOM_ARRAY_FUNCTION_H
00002 #define OPENTISSUE_COLLISION_SPATIAL_HASHING_HASH_FUNCTIONS_SPATIAL_HASHING_RANDOM_ARRAY_FUNCTION_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/core/math/math_random.h>
00013 #include <vector>
00014 #include <cassert>
00015
00016 namespace OpenTissue
00017 {
00018 namespace spatial_hashing
00019 {
00028 class RandomArrayHashFunction
00029 {
00030 protected:
00031
00032 size_t m_size;
00033
00034 std::vector<size_t> m_random1;
00035 std::vector<size_t> m_random2;
00036 std::vector<size_t> m_random3;
00037
00038 public:
00039
00040 RandomArrayHashFunction() { resize(1000); }
00041 explicit RandomArrayHashFunction(size_t size) { resize(size); }
00042
00043 public:
00044
00045 size_t operator()( int i,int j, int k )
00046 {
00047 if(i<0) i = -i;
00048 if(j<0) j = -j;
00049 if(k<0) k = -k;
00050
00051 size_t ii = i;
00052 size_t jj = j;
00053 size_t kk = k;
00054
00055 if(ii >= m_size) ii = ii % m_size;
00056 if(jj >= m_size) jj = jj % m_size;
00057 if(kk >= m_size) kk = kk % m_size;
00058
00059 size_t hash_key = (
00060 (m_random1[ii] >> (jj&0x0f)) +
00061 (m_random2[jj] >> (kk&0x0f)) +
00062 (m_random3[kk] >> (ii&0x0f))
00063 ) & (m_size-1u);
00064
00065 assert( hash_key >= 0 || !"RandomArrayHashFunction::operator(): hash_key must be non-negative");
00066 assert( hash_key < m_size || !"RandomArrayHashFunction::operator(): hash_key must smaller than size");
00067 return hash_key;
00068 }
00069
00070 void resize(size_t new_size)
00071 {
00072 using std::floor;
00073
00074 math::Random<double> r;
00075
00076 size_t max_val = ~0u;
00077
00078 m_size = new_size ;
00079 m_random1.resize(m_size);
00080 m_random2.resize(m_size);
00081 m_random3.resize(m_size);
00082 for(size_t i=0;i<m_size;++i)
00083 {
00084 m_random1[i] = static_cast<size_t>( floor(max_val * r()) );
00085 m_random2[i] = static_cast<size_t>( floor(max_val * r()) );
00086 m_random3[i] = static_cast<size_t>( floor(max_val * r()) );
00087 }
00088 }
00089
00090 size_t size() const { return m_size; }
00091 };
00092
00093 }
00094
00095 }
00096
00097
00098 #endif