-
Notifications
You must be signed in to change notification settings - Fork 20
/
spacehash.go
399 lines (323 loc) · 7.68 KB
/
spacehash.go
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package cp
import (
"math"
"sync"
)
type SpaceHash struct {
*SpatialIndex
numCells int
celldim float64
table []*SpaceHashBin
handleSet *HashSet[*Shape, *Handle]
pooledBins *SpaceHashBin
pooledHandles sync.Pool
stamp uint
}
func NewSpaceHash(celldim float64, num int, bbfunc SpatialIndexBB, staticIndex *SpatialIndex) *SpatialIndex {
spaceHash := &SpaceHash{
celldim: celldim,
numCells: num,
table: make([]*SpaceHashBin, num),
handleSet: NewHashSet[*Shape, *Handle](func(obj *Shape, elt *Handle) bool {
return obj == elt.obj
}),
stamp: 1,
pooledHandles: sync.Pool{New: func() interface{} { return &Handle{} }},
}
for i := 0; i < POOLED_BUFFER_SIZE; i++ {
spaceHash.pooledHandles.Put(&Handle{})
}
spatialIndex := NewSpatialIndex(spaceHash, bbfunc, staticIndex)
spaceHash.SpatialIndex = spatialIndex
return spatialIndex
}
func (hash *SpaceHash) hashHandle(hand *Handle, bb BB) {
dim := hash.celldim
// TODO: chipmunk said floor is slow, use custom floor
l := floor(bb.L / dim)
r := floor(bb.R / dim)
b := floor(bb.B / dim)
t := floor(bb.T / dim)
n := hash.numCells
for i := l; i <= r; i++ {
for j := b; j <= t; j++ {
idx := hashFunc(HashValue(i), HashValue(j), HashValue(n))
bin := hash.table[idx]
if bin.containsHandle(hand) {
continue
}
hand.retain()
newBin := hash.getEmptyBin()
newBin.handle = hand
newBin.next = bin
hash.table[idx] = newBin
}
}
}
func (hash *SpaceHash) Count() int {
return int(hash.handleSet.Count())
}
func (hash *SpaceHash) Each(f SpatialIndexIterator) {
hash.handleSet.Each(func(elt *Handle) {
f(elt.obj)
})
}
func (hash *SpaceHash) Contains(obj *Shape, hashId HashValue) bool {
return hash.handleSet.Find(hashId, obj) != nil
}
func (hash *SpaceHash) Insert(obj *Shape, hashId HashValue) {
hand := hash.handleSet.Insert(hashId, obj, func(obj *Shape) *Handle {
hand := hash.pooledHandles.Get().(*Handle)
hand.Init(obj)
hand.retain()
return hand
})
hash.hashHandle(hand, hash.bbfunc(obj))
}
func (hash *SpaceHash) Remove(obj *Shape, hashId HashValue) {
hand := hash.handleSet.Remove(hashId, obj)
if hand != nil {
hand.obj = nil
hand.release(&hash.pooledHandles)
}
}
func (hash *SpaceHash) Reindex() {
hash.clearTable()
hash.handleSet.Each(func(hand *Handle) {
hash.bbfunc(hand.obj)
})
}
func (hash *SpaceHash) ReindexObject(obj *Shape, hashId HashValue) {
hand := hash.handleSet.Remove(hashId, obj)
if hand != nil {
hand.obj = nil
hand.release(&hash.pooledHandles)
hash.Insert(obj, hashId)
}
}
func (hash *SpaceHash) removeOrphanedHandles(binPtr **SpaceHashBin) {
bin := *binPtr
for bin != nil {
hand := bin.handle
next := bin.next
if hand.obj == nil {
// orphaned handle
*binPtr = bin.next
hash.recycleBin(bin)
hand.release(&hash.pooledHandles)
} else {
binPtr = &bin.next
}
bin = next
}
}
func (hash *SpaceHash) queryHelper(binPtr **SpaceHashBin, obj interface{}, f SpatialIndexQuery, data interface{}) {
restart:
for bin := *binPtr; bin != nil; bin = bin.next {
hand := bin.handle
other := hand.obj
if hand.stamp == hash.stamp || obj == other {
continue
} else if other != nil {
f(obj, other, 0, data)
hand.stamp = hash.stamp
} else {
hash.removeOrphanedHandles(binPtr)
goto restart
}
}
}
func floor(f float64) int {
i := int(f)
if f < 0 && float64(i) != f {
return i - 1
}
return i
}
func (hash *SpaceHash) ReindexQuery(f SpatialIndexQuery, data interface{}) {
hash.clearTable()
hash.handleSet.Each(func(hand *Handle) {
// queryRehash_helper
bb := hash.SpatialIndex.bbfunc(hand.obj)
l := floor(bb.L / hash.celldim)
r := floor(bb.R / hash.celldim)
b := floor(bb.B / hash.celldim)
t := floor(bb.T / hash.celldim)
for i := l; i <= r; i++ {
for j := b; j <= t; j++ {
idx := hashFunc(HashValue(i), HashValue(j), HashValue(hash.numCells))
bin := hash.table[idx]
if bin.containsHandle(hand) {
continue
}
hand.retain()
hash.queryHelper(&bin, hand.obj, f, data)
newBin := hash.getEmptyBin()
newBin.handle = hand
newBin.next = bin
hash.table[idx] = newBin
}
}
hash.stamp++
})
hash.CollideStatic(hash.staticIndex, f, data)
}
func (hash *SpaceHash) Query(obj interface{}, bb BB, f SpatialIndexQuery, data interface{}) {
dim := hash.celldim
l := floor(bb.L / dim)
r := floor(bb.R / dim)
b := floor(bb.B / dim)
t := floor(bb.T / dim)
n := hash.numCells
for i := l; i <= r; i++ {
for j := b; j <= t; j++ {
hash.queryHelper(&hash.table[hashFunc(HashValue(i), HashValue(j), HashValue(n))], obj, f, data)
}
}
hash.stamp++
}
func (hash *SpaceHash) segmentQueryHelper(binPtr **SpaceHashBin, obj interface{}, f SpatialIndexSegmentQuery, data interface{}) float64 {
t := 1.0
restart:
for bin := *binPtr; bin != nil; bin = bin.next {
hand := bin.handle
other := hand.obj
if hand.stamp == hash.stamp {
continue
} else if other != nil {
t = math.Min(t, f(obj, other, data))
hand.stamp = hash.stamp
} else {
hash.removeOrphanedHandles(binPtr)
goto restart
}
}
return t
}
// modified from http://playtechs.blogspot.com/2007/03/raytracing-on-grid.html
func (hash *SpaceHash) SegmentQuery(obj interface{}, a, b Vector, t_exit float64, f SpatialIndexSegmentQuery, data interface{}) {
a = a.Mult(1.0 / hash.celldim)
b = b.Mult(1.0 / hash.celldim)
cellX := int(floor(a.X))
cellY := int(floor(a.Y))
t := 0.0
var xInc, yInc int
var tempV, tempH float64
if b.X > a.X {
xInc = 1
tempH = math.Floor(a.X+1.0) - a.X
} else {
xInc = -1
tempH = a.X - math.Floor(a.X)
}
if b.Y > a.Y {
yInc = 1
tempV = math.Floor(a.Y+1.0) - a.Y
} else {
yInc = -1
tempV = a.Y - math.Floor(a.Y)
}
dx := math.Abs(b.X - a.X)
dy := math.Abs(b.Y - a.Y)
var dtdx, dtdy float64
if dx != 0 {
dtdx = 1.0 / dx
} else {
dtdx = INFINITY
}
if dy != 0 {
dtdy = 1.0 / dy
} else {
dtdy = INFINITY
}
var nextH, nextV float64
if tempH != 0 {
nextH = tempH * dtdx
} else {
nextH = dtdx
}
if tempV != 0 {
nextV = tempV * dtdy
} else {
nextV = dtdy
}
for t < t_exit {
idx := hashFunc(HashValue(cellX), HashValue(cellY), HashValue(hash.numCells))
t_exit = math.Min(t_exit, hash.segmentQueryHelper(&hash.table[idx], obj, f, data))
if nextV < nextH {
cellY += yInc
t = nextV
nextV += dtdy
} else {
cellX += xInc
t = nextH
nextH += dtdx
}
}
hash.stamp++
}
type SpaceHashBin struct {
handle *Handle
next *SpaceHashBin
}
func (bin *SpaceHashBin) containsHandle(hand *Handle) bool {
for item := bin; item != nil; item = item.next {
if item.handle == hand {
return true
}
}
return false
}
func hashFunc(x, y, n HashValue) HashValue {
return (x*1640531513 ^ y*2654435789) % n
}
type Handle struct {
obj *Shape
retains int
stamp uint
}
func (hand *Handle) Init(obj *Shape) {
hand.obj = obj
hand.retains = 0
hand.stamp = 0
}
func (hand *Handle) retain() {
hand.retains++
}
func (hand *Handle) release(pooledHandles *sync.Pool) {
hand.retains--
if hand.retains == 0 {
pooledHandles.Put(hand)
}
}
func (hash *SpaceHash) recycleBin(bin *SpaceHashBin) {
bin.next = hash.pooledBins
hash.pooledBins = bin
}
func (hash *SpaceHash) clearTableCell(idx int) {
bin := hash.table[idx]
for bin != nil {
next := bin.next
bin.handle.release(&hash.pooledHandles)
hash.recycleBin(bin)
bin = next
}
hash.table[idx] = nil
}
func (hash *SpaceHash) clearTable() {
for i := 0; i < hash.numCells; i++ {
hash.clearTableCell(i)
}
}
func (hash *SpaceHash) getEmptyBin() *SpaceHashBin {
bin := hash.pooledBins
if bin != nil {
hash.pooledBins = bin.next
return bin
}
// pool is exhausted, make more
for i := 0; i < POOLED_BUFFER_SIZE; i++ {
hash.recycleBin(&SpaceHashBin{})
}
return &SpaceHashBin{}
}