static inline void hashHandle(cpSpaceHash *hash, cpHandle *hand, cpBB bb) { // Find the dimensions in cell coordinates. cpFloat dim = hash->celldim; int l = floor_int(bb.l/dim); // Fix by ShiftZ int r = floor_int(bb.r/dim); int b = floor_int(bb.b/dim); int t = floor_int(bb.t/dim); int n = hash->numcells; for(int i=l; i<=r; i++){ for(int j=b; j<=t; j++){ int idx = hash_func(i,j,n); cpSpaceHashBin *bin = hash->table[idx]; // 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[idx] = newBin; } } }
int main() { int n; scanf("%d", &n); int i, l[500], w[500], h[500]; for (i = 0; i < n; i++) scanf("%d%d%d", &l[i], &w[i], &h[i]); int m; scanf("%d", &m); int rl[500], rw[500], rc[500]; for (i = 0; i < m; i++) scanf("%d%d%d", &rl[i], &rw[i], &rc[i]); int j, c = 0; for (i = 0; i < n; i++) { int mc = 1000000000; for (j = 0; j < m; j++) { if (rl[j] / h[i] == 0) continue; int cc = floor_int(floor_int(2*l[i] + 2*w[i], rw[j]), rl[j] / h[i]) * rc[j]; if (cc < mc) mc = cc; // printf("i = %d, j = %d, mc = %d\n", i + 1, j + 1, cc); } c += mc; } printf("%d\n", c); return 0; }
// modified from http://playtechs.blogspot.com/2007/03/raytracing-on-grid.html void cpSpaceHashSegmentQuery(cpSpaceHash *hash, void *obj, cpVect a, cpVect b, cpFloat t_exit, cpSpatialIndexSegmentQueryFunc func, void *data) { a = cpvmult(a, 1.0f/hash->celldim); b = cpvmult(b, 1.0f/hash->celldim); int cell_x = floor_int(a.x), cell_y = floor_int(a.y); cpFloat t = 0; int x_inc, y_inc; cpFloat temp_v, temp_h; if (b.x > a.x){ x_inc = 1; temp_h = (cpffloor(a.x + 1.0f) - a.x); } else { x_inc = -1; temp_h = (a.x - cpffloor(a.x)); } if (b.y > a.y){ y_inc = 1; temp_v = (cpffloor(a.y + 1.0f) - a.y); } else { y_inc = -1; temp_v = (a.y - cpffloor(a.y)); } // Division by zero is *very* slow on ARM cpFloat dx = cpfabs(b.x - a.x), dy = cpfabs(b.y - a.y); cpFloat dt_dx = (dx ? 1.0f/dx : INFINITY), dt_dy = (dy ? 1.0f/dy : INFINITY); // fix NANs in horizontal directions cpFloat next_h = (temp_h ? temp_h*dt_dx : dt_dx); cpFloat next_v = (temp_v ? temp_v*dt_dy : dt_dy); int n = hash->numcells; cpSpaceHashBin **table = hash->table; while(t < t_exit){ int idx = hash_func(cell_x, cell_y, n); t_exit = cpfmin(t_exit, segmentQuery_helper(hash, &table[idx], obj, func, data)); if (next_v < next_h){ cell_y += y_inc; t = next_v; next_v += dt_dy; } else { cell_x += x_inc; t = next_h; next_h += dt_dx; } } hash->stamp++; }
static void cpSpaceHashPointQuery(cpSpaceHash *hash, cpVect point, cpSpatialIndexQueryFunc func, void *data) { cpFloat dim = hash->celldim; int idx = hash_func(floor_int(point.x/dim), floor_int(point.y/dim), hash->numcells); // Fix by ShiftZ query_helper(hash, &hash->table[idx], &point, func, data); hash->stamp++; }
void cpSpaceHashPointQuery(cpSpaceHash *hash, cpVect point, cpSpaceHashQueryFunc func, void *data) { cpFloat dim = hash->celldim; int idx = hash_func(floor_int(point.x/dim), floor_int(point.y/dim), hash->numcells); // Fix by ShiftZ query(hash, &hash->table[idx], &point, func, data); // Increment the stamp. // Only one cell is checked, but query() requires it anyway. hash->stamp++; }
// 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 = floor_int(bb.l/dim); int r = floor_int(bb.r/dim); int b = floor_int(bb.b/dim); int t = floor_int(bb.t/dim); cpSpaceHashBin **table = hash->table; for(int i=l; i<=r; i++){ for(int j=b; j<=t; j++){ // // exit the loops if the object has been deleted in func(). // if(!hand->obj) goto break_out; int idx = hash_func(i,j,n); cpSpaceHashBin *bin = table[idx]; if(containsHandle(bin, hand)) continue; cpHandleRetain(hand); // this MUST be done first in case the object is removed in func() query(hash, &bin, obj, func, pair->data); cpSpaceHashBin *newBin = getEmptyBin(hash); newBin->handle = hand; newBin->next = bin; table[idx] = newBin; } } // break_out: // Increment the stamp for each object we hash. hash->stamp++; }
// Hashset iterator func used with cpSpaceHashQueryRehash(). static void queryRehash_helper(cpHandle *hand, queryRehashContext *context) { cpSpaceHash *hash = context->hash; cpSpatialIndexQueryFunc func = context->func; void *data = context->data; cpFloat dim = hash->celldim; int n = hash->numcells; void *obj = hand->obj; cpBB bb = hash->spatialIndex.bbfunc(obj); int l = floor_int(bb.l/dim); int r = floor_int(bb.r/dim); int b = floor_int(bb.b/dim); int t = floor_int(bb.t/dim); cpSpaceHashBin **table = hash->table; for(int i=l; i<=r; i++){ for(int j=b; j<=t; j++){ int idx = hash_func(i,j,n); cpSpaceHashBin *bin = table[idx]; if(containsHandle(bin, hand)) continue; cpHandleRetain(hand); // this MUST be done first in case the object is removed in func() query_helper(hash, &bin, obj, func, data); cpSpaceHashBin *newBin = getEmptyBin(hash); newBin->handle = hand; newBin->next = bin; table[idx] = newBin; } } // Increment the stamp for each object hashed. hash->stamp++; }
static void cpSpaceHashQuery(cpSpaceHash *hash, void *obj, cpBB bb, cpSpatialIndexQueryFunc func, void *data) { // Get the dimensions in cell coordinates. cpFloat dim = hash->celldim; int l = floor_int(bb.l/dim); // Fix by ShiftZ int r = floor_int(bb.r/dim); int b = floor_int(bb.b/dim); int t = floor_int(bb.t/dim); int n = hash->numcells; cpSpaceHashBin **table = hash->table; // Iterate over the cells and query them. for(int i=l; i<=r; i++){ for(int j=b; j<=t; j++){ query_helper(hash, &table[hash_func(i,j,n)], obj, func, data); } } hash->stamp++; }
// Constructors & Destuctors TraversingVolumeAlgorithm(Ray& r) { Length = 0.f; tDelta = glm::vec3(100000); tMax = glm::vec3(100000); sDelta = sign(r.Direction); voxID = floor_int(r.Position); ///////////////////////////////// // Initialisation tDelta ///////////////////////////////// if(r.Direction.x != 0) tDelta.x = 1.f/abs(r.Direction.x); if(r.Direction.y != 0) tDelta.y = 1.f/abs(r.Direction.y); if(r.Direction.z != 0) tDelta.z = 1.f/abs(r.Direction.z); ///////////////////////////////// // Initialise tMax ///////////////////////////////// // * x Initialisation if(r.Direction.x < 0) tMax.x = (voxID.x - r.Position.x) / r.Direction.x; else if(r.Direction.x > 0) tMax.x = (voxID.x + 1.f - r.Position.x) / r.Direction.x; // * y Initialisation if(r.Direction.y < 0) tMax.y = (voxID.y - r.Position.y) / r.Direction.y; else if(r.Direction.y > 0) tMax.y = (voxID.y + 1.f - r.Position.y) / r.Direction.y; // * z Initialisation if(r.Direction.z < 0) tMax.z = (voxID.z - r.Position.z) / r.Direction.z; else if(r.Direction.z > 0) tMax.z = (voxID.z + 1.f - r.Position.z) / r.Direction.z; glm::vec3 DirectionAbs = glm::vec3(abs(r.Direction.x), abs(r.Direction.y), abs(r.Direction.z)); // know the main direction bool xMainDirection =DirectionAbs.x > DirectionAbs.y && DirectionAbs.x > DirectionAbs.z; bool yMainDirection =DirectionAbs.y > DirectionAbs.x && DirectionAbs.y > DirectionAbs.z; bool zMainDirection =DirectionAbs.z > DirectionAbs.x && DirectionAbs.z > DirectionAbs.y; bMainDirection = glm::bvec3(xMainDirection, yMainDirection, zMainDirection); }