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 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
/* Copyright (c) 2007 Scott Lembcke
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
#include "chipmunk.h"
#include "prime.h"
static cpHandle*
cpHandleAlloc(void)
{
return (cpHandle *)malloc(sizeof(cpHandle));
}
static cpHandle*
cpHandleInit(cpHandle *hand, void *obj)
{
hand->obj = obj;
hand->retain = 0;
hand->stamp = 0;
return hand;
}
static cpHandle*
cpHandleNew(void *obj)
{
return cpHandleInit(cpHandleAlloc(), obj);
}
static inline void
cpHandleRetain(cpHandle *hand)
{
hand->retain++;
}
static inline void
cpHandleFree(cpHandle *hand)
{
free(hand);
}
static inline void
cpHandleRelease(cpHandle *hand)
{
hand->retain--;
if(hand->retain == 0)
cpHandleFree(hand);
}
cpSpaceHash*
cpSpaceHashAlloc(void)
{
return (cpSpaceHash *)calloc(1, sizeof(cpSpaceHash));
}
// Frees the old table, and allocates a new one.
static void
cpSpaceHashAllocTable(cpSpaceHash *hash, int numcells)
{
free(hash->table);
hash->numcells = numcells;
hash->table = (cpSpaceHashBin **)calloc(numcells, sizeof(cpSpaceHashBin *));
}
// Equality function for the handleset.
static int
handleSetEql(void *obj, void *elt)
{
cpHandle *hand = (cpHandle *)elt;
return (obj == hand->obj);
}
// Transformation function for the handleset.
static void *
handleSetTrans(void *obj, void *unused)
{
cpHandle *hand = cpHandleNew(obj);
cpHandleRetain(hand);
return hand;
}
cpSpaceHash*
cpSpaceHashInit(cpSpaceHash *hash, cpFloat celldim, int numcells, cpSpaceHashBBFunc bbfunc)
{
cpSpaceHashAllocTable(hash, next_prime(numcells));
hash->celldim = celldim;
hash->bbfunc = bbfunc;
hash->bins = NULL;
hash->handleSet = cpHashSetNew(0, &handleSetEql, &handleSetTrans);
hash->stamp = 1;
return hash;
}
cpSpaceHash*
cpSpaceHashNew(cpFloat celldim, int cells, cpSpaceHashBBFunc bbfunc)
{
return cpSpaceHashInit(cpSpaceHashAlloc(), celldim, cells, bbfunc);
}
static inline void
clearHashCell(cpSpaceHash *hash, int index)
{
cpSpaceHashBin *bin = hash->table[index];
while(bin){
cpSpaceHashBin *next = bin->next;
// Release the lock on the handle.
cpHandleRelease(bin->handle);
// Recycle the bin.
bin->next = hash->bins;
hash->bins = bin;
bin = next;
}
hash->table[index] = NULL;
}
// Clear all cells in the hashtable.
static void
clearHash(cpSpaceHash *hash)
{
for(int i=0; i<hash->numcells; i++)
clearHashCell(hash, i);
}
// Free the recycled hash bins.
static void
freeBins(cpSpaceHash *hash)
{
cpSpaceHashBin *bin = hash->bins;
while(bin){
cpSpaceHashBin *next = bin->next;
free(bin);
bin = next;
}
}
// Hashset iterator function to free the handles.
static void
handleFreeWrap(void *elt, void *unused)
{
cpHandle *hand = (cpHandle *)elt;
cpHandleFree(hand);
}
void
cpSpaceHashDestroy(cpSpaceHash *hash)
{
clearHash(hash);
freeBins(hash);
// Free the handles.
cpHashSetEach(hash->handleSet, &handleFreeWrap, NULL);
cpHashSetFree(hash->handleSet);
free(hash->table);
}
void
cpSpaceHashFree(cpSpaceHash *hash)
{
if(!hash) return;
cpSpaceHashDestroy(hash);
free(hash);
}
void
cpSpaceHashResize(cpSpaceHash *hash, cpFloat celldim, int numcells)
{
// Clear the hash to release the old handle locks.
clearHash(hash);
hash->celldim = celldim;
cpSpaceHashAllocTable(hash, next_prime(numcells));
}
// Return true if the chain contains the handle.
static inline int
containsHandle(cpSpaceHashBin *bin, cpHandle *hand)
{
while(bin){
if(bin->handle == hand) return 1;
bin = bin->next;
}
return 0;
}
// Get a recycled or new bin.
static inline cpSpaceHashBin *
getEmptyBin(cpSpaceHash *hash)
{
cpSpaceHashBin *bin = hash->bins;
// Make a new one if necessary.
if(bin == NULL) return (cpSpaceHashBin *)malloc(sizeof(cpSpaceHashBin));
hash->bins = bin->next;
return bin;
}
// The hash function itself.
static inline unsigned int
hash_func(unsigned int x, unsigned int y, unsigned int n)
{
return (x*2185031351ul ^ y*4232417593ul) % n;
}
static inline void
hashHandle(cpSpaceHash *hash, cpHandle *hand, cpBB bb)
{
// Find the dimensions in cell coordinates.
cpFloat dim = hash->celldim;
int l = bb.l/dim;
int r = bb.r/dim;
int b = bb.b/dim;
int t = bb.t/dim;
int n = hash->numcells;
for(int i=l; i<=r; i++){
for(int j=b; j<=t; j++){
int index = hash_func(i,j,n);
cpSpaceHashBin *bin = hash->table[index];
// Don't add an object twice to the same cell.
if(containsHandle(bin, hand)) continue;
cpHandleRetain(hand);
// Insert a new bin for the handle in this cell.
cpSpaceHashBin *newBin = getEmptyBin(hash);
newBin->handle = hand;
newBin->next = bin;
hash->table[index] = newBin;
}
}
}
void
cpSpaceHashInsert(cpSpaceHash *hash, void *obj, unsigned int id, cpBB bb)
{
cpHandle *hand = (cpHandle *)cpHashSetInsert(hash->handleSet, id, obj, NULL);
hashHandle(hash, hand, bb);
}
void
cpSpaceHashRehashObject(cpSpaceHash *hash, void *obj, unsigned int id)
{
cpHandle *hand = (cpHandle *)cpHashSetFind(hash->handleSet, id, obj);
hashHandle(hash, hand, hash->bbfunc(obj));
}
// Hashset iterator function for rehashing the spatial hash. (hash hash hash hash?)
static void
handleRehashHelper(void *elt, void *data)
{
cpHandle *hand = (cpHandle *)elt;
cpSpaceHash *hash = (cpSpaceHash *)data;
hashHandle(hash, hand, hash->bbfunc(hand->obj));
}
void
cpSpaceHashRehash(cpSpaceHash *hash)
{
clearHash(hash);
// Rehash all of the handles.
cpHashSetEach(hash->handleSet, &handleRehashHelper, hash);
}
void
cpSpaceHashRemove(cpSpaceHash *hash, void *obj, unsigned int id)
{
cpHandle *hand = (cpHandle *)cpHashSetRemove(hash->handleSet, id, obj);
hand->obj = NULL;
cpHandleRelease(hand);
}
// Used by the cpSpaceHashEach() iterator.
typedef struct eachPair {
cpSpaceHashIterator func;
void *data;
} eachPair;
// Calls the user iterator function. (Gross I know.)
static void
eachHelper(void *elt, void *data)
{
cpHandle *hand = (cpHandle *)elt;
eachPair *pair = (eachPair *)data;
pair->func(hand->obj, pair->data);
}
// Iterate over the objects in the spatial hash.
void
cpSpaceHashEach(cpSpaceHash *hash, cpSpaceHashIterator func, void *data)
{
// Bundle the callback up to send to the hashset iterator.
eachPair pair = {func, data};
cpHashSetEach(hash->handleSet, &eachHelper, &pair);
}
// Calls the callback function for the objects in a given chain.
static inline void
query(cpSpaceHash *hash, cpSpaceHashBin *bin, void *obj, cpSpaceHashQueryFunc func, void *data)
{
for(; bin; bin = bin->next){
cpHandle *hand = bin->handle;
void *other = hand->obj;
// Skip over certain conditions
if(
// Have we already tried this pair in this query?
hand->stamp == hash->stamp
// Is obj the same as other?
|| obj == other
// Has other been removed since the last rehash?
|| !other
) continue;
func(obj, other, data);
// Stamp that the handle was checked already against this object.
hand->stamp = hash->stamp;
}
}
void
cpSpaceHashQuery(cpSpaceHash *hash, void *obj, cpBB bb, cpSpaceHashQueryFunc func, void *data)
{
// Get the dimensions in cell coordinates.
cpFloat dim = hash->celldim;
int l = bb.l/dim;
int r = bb.r/dim;
int b = bb.b/dim;
int t = bb.t/dim;
int n = hash->numcells;
// Iterate over the cells and query them.
for(int i=l; i<=r; i++){
for(int j=b; j<=t; j++){
int index = hash_func(i,j,n);
query(hash, hash->table[index], obj, func, data);
}
}
// Increment the stamp.
hash->stamp++;
}
// Similar to struct eachPair above.
typedef struct queryRehashPair {
cpSpaceHash *hash;
cpSpaceHashQueryFunc func;
void *data;
} queryRehashPair;
// Hashset iterator func used with cpSpaceHashQueryRehash().
static void
handleQueryRehashHelper(void *elt, void *data)
{
cpHandle *hand = (cpHandle *)elt;
// Unpack the user callback data.
queryRehashPair *pair = (queryRehashPair *)data;
cpSpaceHash *hash = pair->hash;
cpSpaceHashQueryFunc func = pair->func;
cpFloat dim = hash->celldim;
int n = hash->numcells;
void *obj = hand->obj;
cpBB bb = hash->bbfunc(obj);
int l = bb.l/dim;
int r = bb.r/dim;
int b = bb.b/dim;
int t = bb.t/dim;
for(int i=l; i<=r; i++){
for(int j=b; j<=t; j++){
int index = hash_func(i,j,n);
cpSpaceHashBin *bin = hash->table[index];
if(containsHandle(bin, hand)) continue;
query(hash, bin, obj, func, pair->data);
cpHandleRetain(hand);
cpSpaceHashBin *newBin = getEmptyBin(hash);
newBin->handle = hand;
newBin->next = bin;
hash->table[index] = newBin;
}
}
// Increment the stamp for each object we hash.
hash->stamp++;
}
void
cpSpaceHashQueryRehash(cpSpaceHash *hash, cpSpaceHashQueryFunc func, void *data)
{
clearHash(hash);
queryRehashPair pair = {hash, func, data};
cpHashSetEach(hash->handleSet, &handleQueryRehashHelper, &pair);
}
|