-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpatialHash.h
193 lines (168 loc) · 4.27 KB
/
SpatialHash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifndef SPATIALHASH_H
#define SPATIALHASH_H
#include <cstddef>
#include <algorithm>
#include "vecmath.h"
#include "HashNode.h"
#define P1 73856093
#define P2 19349663
#define P3 83492791
class SpatialHash {
public:
SpatialHash(int num_particles, float cellSize) {
m_cellSize = cellSize;
m_size = next_prime(num_particles * 2);
table = new HashNode* [m_size] ();
printf("Table Size: %d", m_size);
};
~SpatialHash() {
// destroy all buckets one by one
for (int i = 0; i < m_size; ++i) {
HashNode *entry = table[i];
while (entry != NULL) {
HashNode *prev = entry;
entry = entry->getNext();
delete prev;
}
table[i] = NULL;
}
// destroy the hash table
delete [] table;
};
vector<int> rhat(Vector3f pos) {
int r0 = floor(pos[0] / m_cellSize);
int r1 = floor(pos[1] / m_cellSize);
int r2 = floor(pos[2] / m_cellSize);
vector<int> result = vector<int>();
result.push_back(r0);
result.push_back(r1);
result.push_back(r2);
return result;
};
int hash(Vector3f &pos) {
vector<int> r = rhat(pos);
return hash(r[0], r[1], r[2]);
};
int hash(int i, int j, int k) {
if (i < 0 || j < 0 || k <0) {printf("NEGATIVE NUMBAS");}
int result = ( ( (i * P1) ^ (j * P2) ^ (k * P3)) )% m_size;
if (result < 0) {printf("ERROR: %d NEGATIVE KEY!!!\n", result);}
if (result >= m_size) {printf("ERROR: OUT OF BOUNDS!!!\n");}
return result;
};
//Takes in the particle index and position
void insert(int index, Vector3f pos) {
unsigned int key = hash(pos);
HashNode* node = new HashNode(key, index);
if (table[key] != NULL) {
node->setNext(table[key]);
}
table[key] = node;
};
void insert(vector<Vector3f> &points) {
for (int i=0;i< points.size();i+=2) {
int key = hash(points[i]);
HashNode* n = new HashNode(key, i/2);
if (table[key] != NULL) {
n->setNext(table[key]);
}
table[key] = n;
}
};
void overview() {
int count = 0;
for (int i=0;i<m_size;++i) {
printf("KEY: %d | ", i);
HashNode* n = table[i];
while (n != NULL) {
printf("%d ", n->getValue());
count ++;
n = n->getNext();
}
printf("\n");
}
printf("COUNT: %d\n", count);
};
void remove(int key, int index) {
HashNode* node = table[key];
if(node->getValue() == index) {
table[key] = node->getNext();
delete node;
node = table[key];
}
while (node->getNext() != NULL) {
HashNode* next = node->getNext();
if (next->getKey() == index) {
node->setNext(next->getNext());
delete next;
} else {
node = node->getNext();
}
}
};
void clear() {
//Only clear buckets but not table
for (int i = 0; i < m_size; ++i) {
HashNode *entry = table[i];
while (entry != NULL) {
HashNode *prev = entry;
entry = entry->getNext();
delete prev;
}
table[i] = NULL;
}
};
vector<int> findNeighbours(Vector3f pos) {
vector<int> r = rhat(pos);
//printf("RHAT: %d %d %d\n", r[0], r[1], r[2]);
vector<int> results = vector<int>();
for (int i=r[0]-1;i<=r[0]+1;++i) {
for (int j=r[1]-1;j<=r[1]+1;++j) {
for (int k=r[2]-1;k<=r[2]+1;++k) {
//printf("Hashing: %d %d %d\n", i, j, k);
int key = hash(max(0,i),max(0,j),max(0,k));
if (key < 0) {continue;}
if (key >= m_size) {printf("ERROR: OUT OF BOUNDS %d", key); continue;}
//printf("find hash value %d ", key);
HashNode* n = table[key];
while (n != NULL) {
int v = n->getValue();
//printf(" %d ", v);
if (std::find(results.begin(), results.end(), v) == results.end()) {
//printf("Pushing, ");
results.push_back(v);
}
n= n->getNext();
}
//printf("results is %d long\n", results.size());
}
}
}
return results;
};
bool is_prime(std::size_t x) {
for (std::size_t i = 3; true; i += 2)
{
std::size_t q = x / i;
if (q < i)
return true;
if (x == q * i)
return false;
}
return true;
};
std::size_t next_prime(std::size_t x) {
if (x <= 2)
return 2;
if (!(x & 1))
++x;
for (; !is_prime(x); x += 2)
;
return x;
};
protected:
int m_size;
float m_cellSize;
HashNode ** table;
};
#endif