MercuryDPM  0.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HGRID.cc
Go to the documentation of this file.
1 //Copyright (c) 2013-2014, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 #include "HGRID.h"
27 #include "Particles/BaseParticle.h"
28 
30 {
31  objectBucket = 0;
32  bucketIsChecked = 0;
33 }
34 
36 HGrid::HGrid(int num_buckets, double cellOverSizeRatio, std::vector<double>& cellSizes)
37 {
38  NUM_BUCKETS = num_buckets;
39  cellOverSizeRatio_=cellOverSizeRatio;
40 
42  bucketIsChecked = new bool[NUM_BUCKETS];
43 
44  for(int i=0;i<cellSizes.size();i++)
45  {
46  cellSizes_.push_back(cellSizes[i]);
47  std::cout<<"Level="<<i<<" Grid size="<<cellSizes_.back()<<std::endl;
48  }
49 }
50 
53  if (objectBucket) { delete [] objectBucket; objectBucket=0; }
54  if (bucketIsChecked) { delete [] bucketIsChecked; bucketIsChecked=0; }
55 }
56 
58 {
59  invCellSizes_.clear();
60  for (unsigned int level = 0; level<cellSizes_.size(); level++)
61  {
62  invCellSizes_.push_back(1.0/cellSizes_[level]);
63  }
64 }
65 
70 {
71  // Find lowest level where object fully fits inside cell, taking RATIO into account
72  Mdouble diameter = obj->get_InteractionRadius() * 2.0 ;
73  unsigned int level=0;
74  while(level<cellSizes_.size()&&cellSizes_[level]<=diameter * cellOverSizeRatio_)
75  {
76  level++;
77  }
78 
79  if (level >= cellSizes_.size())
80  {
81  std::cerr << "ATTENTION !!! Object is larger (d=" << diameter << ", cellOverSizeRatio=" << cellOverSizeRatio_ << ") than largest grid cell allows (" << cellSizes_.back() << ")!" << std::endl;
82  exit(-1);
83  }
84 
85  obj->set_HGRID_Level(level);
86  // indicate level is in use - not levels with no particles no collision detection is performed
87  this->occupiedLevelsMask |= (1 << level);
88 }
89 
90  //-------------------------------------------------
91 
93 int HGrid::ComputeHashBucketIndex(int x, int y, int z, int l)
94 {
95  const int h1 = 0x8da6b343; // Large multiplicative constants;
96  const int h2 = 0xd8163841; // here arbitrarily chosen primes
97  const int h3 = 0xcb1ab31f;
98  const int h4 = 0x165667b1;
99 
100  int n = h1 * x + h2 * y + h3 * z + h4 * l;
101  n = n % NUM_BUCKETS;
102 
103  if (n < 0) n += NUM_BUCKETS;
104 
105  return n;
106 }
107 
108 int HGrid::ComputeHashBucketIndex(int x, int y, int l)
109 {
110  const int h1 = 0x8da6b343; // Large multiplicative constants;
111  const int h2 = 0xd8163841; // here arbitrarily chosen primes
112  const int h4 = 0x165667b1;
113 
114  int n = h1 * x + h2 * y + h4 * l;
115  n = n % NUM_BUCKETS;
116 
117  if (n < 0) n += NUM_BUCKETS;
118 
119  return n;
120 }
121 
122 
124 void HGrid::reset_num_buckets(int new_num_buckets){
125  NUM_BUCKETS=new_num_buckets;
127  bucketIsChecked = (bool*)realloc(bucketIsChecked, NUM_BUCKETS*sizeof(bool));
128 }
129 
void InsertParticleToHgrid(BaseParticle *obj)
This insert a particle given by CParticle in to the HGrid (i.e. it sets up the particle grid properts...
Definition: HGRID.cc:69
Mdouble get_InteractionRadius() const
Mdouble cellOverSizeRatio_
Definition: HGRID.h:84
~HGrid()
constructor: initializes parameters and allocates space for internal variables
Definition: HGRID.cc:52
void reset_num_buckets(int new_num_buckets)
Definition: HGRID.cc:124
int NUM_BUCKETS
Number of buckets used for hashing.
Definition: HGRID.h:47
std::vector< double > invCellSizes_
Definition: HGRID.h:51
std::vector< double > cellSizes_
Definition: HGRID.h:50
HGrid()
Definition: HGRID.cc:29
double Mdouble
Definition: ExtendedMath.h:33
int ComputeHashBucketIndex(int x, int y, int z, int l)
Computes hash bucket index in range [0, NUM_BUCKETS-1].
Definition: HGRID.cc:93
void set_HGRID_Level(const int _new)
void Initialize_inv_size()
Definition: HGRID.cc:57
bool * bucketIsChecked
bucketIsChecked[b] stores if hash bucket b is checked already; initially all zero ...
Definition: HGRID.h:61
int occupiedLevelsMask
l-th bit of occupiedLevelsMask is 1 if level l is contains particles; initially zero (Implies max 32 ...
Definition: HGRID.h:55
BaseParticle ** objectBucket
objectBucket[b] stores pointer to first element in hash bucket b; initially all NULL ...
Definition: HGRID.h:58