Repository navigation
Expand file tree
/
Copy pathSpatialHash.h
More file actions
151 lines (125 loc) · 3.44 KB
/
Copy pathSpatialHash.h
File metadata and controls
151 lines (125 loc) · 3.44 KB
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
///////////////////////////////////////////////////////////////////////////////
// Spatial Hash
// Joseph Bleau (8/30/2012)
// A simple Spatial hash implemention for use in video games where objects need
// to be divided into buckets for processing by their relative Spatial distances.
////////////////////
#ifndef SPATIALHASH_H
#define SPATIALHASH_H
#include <algorithm>
#include <vector>
///////////////////////////////////////////////////////////////////////////////
// SPECIFICATION
////////////////////
template <class T>
class SpatialHash
{
private:
float m_GridWidth;
float m_GridHeight;
float m_CellWidth;
float m_CellHeight;
std::vector<std::vector<std::vector<T* const>>> m_Grid;
public:
void clear();
void insert(T* const item, float x, float y, float w, float h);
void remove(T* const item);
std::vector<T*> getItems(float x, float y, float w, float h);
size_t getNumItems();
SpatialHash(float grid_width, float grid_height, int horiz_divisions, int vert_divisions);
};
///////////////////////////////////////////////////////////////////////////////
// IMPLEMENTATION
////////////////////
template <class T>
void SpatialHash<T>::clear()
{
for(size_t x = 0; x < m_Grid.size(); x++)
{
for(size_t y = 0; y < m_Grid[x].size(); y++)
{
m_Grid[x][y].clear();
}
}
}
template <class T>
void SpatialHash<T>::insert(T* const item, float x, float y, float w, float h)
{
for(float lx = x; lx < x + w; lx += m_CellWidth)
{
for(float ly = y; ly < y + h; ly += m_CellHeight)
{
unsigned int ix = (unsigned int) (lx / m_CellWidth);
unsigned int iy = (unsigned int) (ly / m_CellHeight);
m_Grid[ix][iy].push_back(item);
}
}
}
template <class T>
void SpatialHash<T>::remove(T* const item)
{
for(size_t x = 0; x < m_Grid.size(); x++)
{
for(size_t y = 0; y < m_Grid[x].size(); y++)
{
auto it = std::find(m_Grid[x][y].begin(), m_Grid[x][y].end(), item);
if(it != m_Grid[x][y].end())
{
m_Grid[x][y].erase(it);
}
}
}
}
template <class T>
std::vector<T*> SpatialHash<T>::getItems(float x, float y, float w, float h)
{
// Merge buckets
std::vector<T*> ret;
for(float lx = x; lx < x + w; lx += m_CellHeight)
{
for(float ly = y; ly < y + h; ly += m_CellHeight)
{
unsigned int ix = (unsigned int) (lx / m_CellWidth);
unsigned int iy = (unsigned int) (ly / m_CellHeight);
if(ix >= m_Grid.size())
continue;
if(iy >= m_Grid[ix].size())
continue;
ret.insert(ret.end(), m_Grid[ix][iy].begin(), m_Grid[ix][iy].end());
}
}
// Remove duplicates
std::sort(ret.begin(), ret.end());
ret.erase(std::unique(ret.begin(), ret.end()), ret.end());
return ret;
}
template <class T>
size_t SpatialHash<T>::getNumItems()
{
size_t num = 0;
for(size_t x = 0; x < m_Grid.size(); x++)
{
for(size_t y = 0; y < m_Grid[x].size(); y++)
{
num += m_Grid[x][y].size();
}
}
return num;
}
template <class T>
SpatialHash<T>::SpatialHash(float grid_width, float grid_height, int horiz_divisions, int vert_divisions)
{
m_GridWidth = grid_width;
m_GridHeight = grid_height;
m_CellWidth = m_GridWidth / horiz_divisions;
m_CellHeight = m_GridHeight / vert_divisions;
for(int x = 0; x < horiz_divisions; x++)
{
m_Grid.push_back(std::vector<std::vector<T* const>>());
for(int y = 0; y < vert_divisions; y++)
{
m_Grid[x].push_back(std::vector<T* const>());
}
}
}
#endif