-
Notifications
You must be signed in to change notification settings - Fork 3
/
cSpatialHash3D.cls
269 lines (216 loc) · 8.25 KB
/
cSpatialHash3D.cls
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "cSpatialHash3D"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
'https://github.com/matthias-research/pages/blob/master/tenMinutePhysics/11-hashing.html
Private Declare Sub AssignZero Lib "kernel32" Alias "RtlZeroMemory" (pDst As Any, Optional ByVal cb& = 4)
Private Spacing As Long
Attribute Spacing.VB_VarUserMemId = 1073938434
Private TableSize As Long
Attribute TableSize.VB_VarUserMemId = 1073938435
Private cellStart() As Long
Private cellEntries() As Long
Attribute cellEntries.VB_VarUserMemId = 1073938435
Private mQueryIds() As Long
Attribute mQueryIds.VB_VarUserMemId = 1610809344
Public querySize As Long
Attribute querySize.VB_VarUserMemId = 1073938432
Friend Property Get QueryIds(I As Long) As Long
QueryIds = mQueryIds(I)
End Property
Friend Sub constructor(mSpacing As Long, maxNumObjects As Long)
Spacing = mSpacing
TableSize = 2 * maxNumObjects
ReDim cellStart(TableSize + 1)
ReDim cellEntries(maxNumObjects)
ReDim mQueryIds(maxNumObjects)
querySize = 0
End Sub
'hashCoords(xi, yi, zi) {
' var h = (xi * 92837111) ^ (yi * 689287499) ^ (zi * 283923481); // fantasy function
' return Math.abs(h) % this.tableSize;
' }
Friend Function hashCoords(Xi As Single, Yi As Single, Zi As Single) As Long
Dim H As Single
'h = (Xi * 92837111) ^ (Yi * 689287499) ^ (Zi * 283923481) ' // fantasy function
H = (Xi * 0.92837111) * (Yi * 6.89287499) * (Zi * 2.83923481) ' // fantasy function
hashCoords = Abs(H) Mod TableSize
End Function
'intCoord(coord) {
' return Math.floor(coord / this.spacing);
' }
Friend Function intCoord(coord As Single) As Long
intCoord = coord \ Spacing
End Function
' hashPos(pos, nr) {
' return this.hashCoords(
' this.intCoord(pos[3 * nr]),
' this.intCoord(pos[3 * nr + 1]),
' this.intCoord(pos[3 * nr + 2]));
' }
Friend Function hashPos(Pos As tVec3) As Long
hashPos = hashCoords(intCoord(Pos.X), intCoord(Pos.Y), intCoord(Pos.Z))
End Function
'create(pos) {
' var numObjects = Math.min(pos.length / 3, this.cellEntries.length);
'
' // determine cell sizes
'
' this.cellStart.fill(0);
' this.cellEntries.fill(0);
'
' for (var i = 0; i < numObjects; i++) {
' var h = this.hashPos(pos, i);
' this.cellStart[h]++;
' }
'
' // determine cells starts
'
' var start = 0;
' for (var i = 0; i < this.tableSize; i++) {
' start += this.cellStart[i];
' this.cellStart[i] = start;
' }
' this.cellStart[this.tableSize] = start; // guard
'
' // fill in objects ids
'
' for (var i = 0; i < numObjects; i++) {
' var h = this.hashPos(pos, i);
' this.cellStart[h]--;
' this.cellEntries[this.cellStart[h]] = i;
' }
' }
Friend Sub InsertPoints2(X() As Single, Y() As Single, Z() As Single)
Dim numObjects&
Dim I&, Start&, H&
numObjects = UBound(X)
If UBound(cellEntries) < numObjects Then numObjects = UBound(cellEntries)
' // determine cell sizes
' this.cellStart.fill(0);
' this.cellEntries.fill(0);
' ReDim cellStart(TableSize + 1) ' Da cambiare in fill 0
' ReDim cellEntries(UBound(cellEntries)) ' Da cambiare in fill 0
AssignZero cellStart(0), LenB(cellStart(0)) * (TableSize + 1)
AssignZero cellEntries(0), LenB(cellEntries(0)) * (UBound(cellEntries) + 1)
For I = 1 To numObjects
H = hashPos(Vec3(X(I), Y(I), Z(I)))
cellStart(H) = cellStart(H) + 1
Next
'// determine cells starts
Start = 0
For I = 1 To TableSize
Start = Start + cellStart(I)
cellStart(I) = Start
Next
cellStart(TableSize) = Start '; // guard
' // fill in objects ids
For I = 1 To numObjects
H = hashPos(Vec3(X(I), Y(I), Z(I)))
' this.cellStart[h]--;
' this.cellEntries[this.cellStart[h]] = i;
cellStart(H) = cellStart(H) - 1&
cellEntries(cellStart(H)) = I
Next
End Sub
Friend Sub InsertPoints(Pos() As tVec3)
' var numObjects = Math.min(pos.length / 3, this.cellEntries.length);
Dim numObjects&
Dim I&, Start&, H&
numObjects = UBound(Pos)
If UBound(cellEntries) < numObjects Then numObjects = UBound(cellEntries)
' // determine cell sizes
' this.cellStart.fill(0);
' this.cellEntries.fill(0);
' ReDim cellStart(TableSize + 1) ' Da cambiare in fill 0
' ReDim cellEntries(UBound(cellEntries)) ' Da cambiare in fill 0
'Stop
AssignZero cellStart(0), LenB(cellStart(0)) * (TableSize + 1)
AssignZero cellEntries(0), LenB(cellEntries(0)) * (UBound(cellEntries))
For I = 1 To numObjects
H = hashPos(Pos(I))
cellStart(H) = cellStart(H) + 1
Next
'// determine cells starts
Start = 0
For I = 1 To TableSize
Start = Start + cellStart(I)
cellStart(I) = Start
Next
cellStart(TableSize) = Start '; // guard
' // fill in objects ids
For I = 1 To numObjects
H = hashPos(Pos(I))
' this.cellStart[h]--;
' this.cellEntries[this.cellStart[h]] = i;
cellStart(H) = cellStart(H) - 1&
cellEntries(cellStart(H)) = I
Next
End Sub
'query(pos, nr, maxDist) {
' var x0 = this.intCoord(pos[3 * nr] - maxDist);
' var y0 = this.intCoord(pos[3 * nr + 1] - maxDist);
' var z0 = this.intCoord(pos[3 * nr + 2] - maxDist);
'
' var x1 = this.intCoord(pos[3 * nr] + maxDist);
' var y1 = this.intCoord(pos[3 * nr + 1] + maxDist);
' var z1 = this.intCoord(pos[3 * nr + 2] + maxDist);
'
' this.querySize = 0;
'
' for (var xi = x0; xi <= x1; xi++) {
' for (var yi = y0; yi <= y1; yi++) {
' for (var zi = z0; zi <= z1; zi++) {
' var h = this.hashCoords(xi, yi, zi);
' var iStart = this.cellStart[h];
' var end = this.cellStart[h + 1];
'
' for (var i = iStart; i < end; i++) {
' this.mqueryIds[this.querySize] = this.cellEntries[i];
' this.querySize++;
' }
' }
' }
' }
' }
Friend Sub Query(Pos As tVec3, maxDist As Single)
Dim X0&, Y0&, Z0&
Dim X1&, Y1&, Z1&
Dim Xi&, Yi&, Zi&
Dim H&, iStart&, iEnd&
Dim I&
X0 = intCoord(Pos.X - maxDist)
Y0 = intCoord(Pos.Y - maxDist)
Z0 = intCoord(Pos.Z - maxDist)
X1 = intCoord(Pos.X + maxDist)
Y1 = intCoord(Pos.Y + maxDist)
Z1 = intCoord(Pos.Z + maxDist)
querySize = 0
For Xi = X0 To X1
For Yi = Y0 To Y1
For Zi = Z0 To Z1
H = hashCoords(Xi * 1&, Yi * 1&, Zi * 1&)
iStart = cellStart(H)
iEnd = cellStart(H + 1&)
'
' for (var i = iStart; i < end; i++) {
' this.mqueryIds[this.querySize] = this.cellEntries[i];
' this.querySize++;
For I = iStart To iEnd
querySize = querySize + 1
mQueryIds(querySize) = cellEntries(I)
Next
Next
Next
Next
End Sub