00001 #ifndef OPENTISSUE_COLLISION_SPATIAL_HASHING_HASH_FUNCTIONS_SPATIAL_HASHING_PRIME_NUMBER_FUNCTION_H 00002 #define OPENTISSUE_COLLISION_SPATIAL_HASHING_HASH_FUNCTIONS_SPATIAL_HASHING_PRIME_NUMBER_FUNCTION_H 00003 // 00004 // OpenTissue Template Library 00005 // - A generic toolbox for physics-based modeling and simulation. 00006 // Copyright (C) 2008 Department of Computer Science, University of Copenhagen. 00007 // 00008 // OTTL is licensed under zlib: http://opensource.org/licenses/zlib-license.php 00009 // 00010 #include <OpenTissue/configuration.h> 00011 00012 #include <OpenTissue/core/math/math_prime_numbers.h> 00013 00014 #include <cassert> 00015 00016 namespace OpenTissue 00017 { 00018 namespace spatial_hashing 00019 { 00023 class PrimeNumberHashFunction 00024 { 00025 protected: 00026 00027 size_t m_size; 00028 int m_p1; 00029 int m_p2; 00030 int m_p3; 00031 00032 public: 00033 00034 PrimeNumberHashFunction() 00035 : m_size(1000) 00036 , m_p1( 73856093 ) 00037 , m_p2( 19349663 ) 00038 , m_p3( 83492791 ) 00039 { } 00040 00041 PrimeNumberHashFunction(size_t size) 00042 : m_size(size) 00043 , m_p1( 73856093 ) 00044 , m_p2( 19349663 ) 00045 , m_p3( 83492791 ) 00046 { 00047 assert( m_size > 0 ); 00048 } 00049 00050 public: 00051 00058 size_t operator()( int i,int j, int k ) 00059 { 00060 int hash_key = ( i * m_p1 ^ j * m_p2 ^ k * m_p3 ) % m_size; 00061 if ( hash_key < 0 ) 00062 hash_key += m_size; 00063 assert( hash_key >= 0 ); 00064 assert( static_cast<size_t>(hash_key) < m_size ); 00065 return hash_key; 00066 } 00067 00073 void resize(size_t new_size) 00074 { 00075 m_size = math::prime_search( new_size ); 00076 } 00077 00078 size_t size()const { return m_size; } 00079 }; 00080 00081 } // namespace spatial_hashing 00082 } // namespace OpenTissue 00083 00084 // OPENTISSUE_COLLISION_SPATIAL_HASHING_HASH_FUNCTIONS_SPATIAL_HASHING_PRIME_NUMBER_FUNCTION_H 00085 #endif