Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/lamont/src/compiler/c-target/fragments/grid2D.in
ViewVC logotype

View of /branches/lamont/src/compiler/c-target/fragments/grid2D.in

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2191 - (download) (annotate)
Sun Feb 24 06:12:43 2013 UTC (6 years, 5 months ago) by lamonts
File size: 5746 byte(s)
Major update to spatial data structure. Fully working now
STATIC_INLINE GridItem_t * Diderot_GridFindItemInPool(uint32_t id, GridContext * ctx)
{ 
    return &(ctx->gridPool.pool[id]);
 
}

GridItem_t * Diderot_GridFindItemInGrid(uint32_t id, GridContext * ctx)
{ 
    if(ctx->grid[id].poolIdx == -1) 
        return 0; 
    else 
        return &(ctx->gridPool.pool[ctx->grid[id].poolIdx]); 
}

//! \brief allocate memory for the grid context 
//! \param gridSize the dimensions of the grid 
//! \param numberOfStrands the number of strands 
//! \param cellSize the size of the cells 
//! \param window the viewing window (i.e. [XMIN,XMAX,YMIN,YMAX]) 
//! \return the address of the grid context 
GridContext * Diderot_GridAllocGrid2D(Diderot_vec2_t * gridSize, 
                                      uint32_t numberOfStrands, 
                                      Diderot_vec2_t * cellSize,    
                                      Diderot_vec4_t  * window) 
{

	GridContext * newGridContext = CheckedAlloc (sizeof(GridContext));

    newGridContext->gridSize = gridSize; 
    newGridContext->numberOfStrands = numberOfStrands; 
    newGridContext->cellSize = cellSize; 
    newGridContext->window = window; 

    Diderot_union2_t v;
    v.v = *(gridSize);   

    newGridContext->grid = CheckedAlloc(sizeof(GridCell_t)*((Diderot_int_t)v.r[0] + 2) * ((Diderot_int_t)v.r[1] + 2)); 
    newGridContext->gridPool.poolSize = numberOfStrands; 
    newGridContext->gridPool.handle = 0; 
    newGridContext->gridPool.pool = CheckedAlloc(sizeof(GridItem_t)*numberOfStrands); 
    
    return newGridContext; 
}
GridItem_t * newGridItem(GridContext * ctx, uint32_t * poolIdx) 
{
   if(ctx->gridPool.handle < ctx->gridPool.poolSize) 
   {
      *poolIdx = (ctx->gridPool.handle)++; 
      return &(ctx->gridPool.pool[*poolIdx]); 
   } else { 
     //FIXME: write code to resize the grid pool!!! 
     return 0; 
   }
}
void Diderot_GridClear(GridContext * ctx) 
{ 
    Diderot_union2_t v;
    v.v = *(ctx->gridSize);  

    ctx->gridPool.handle  = 0;  
 
    uint32_t size = (v.r[0] + 2) * (v.r[1] + 2); 

    for(uint32_t i=0; i < size; i++){ 
       ctx->grid[i].poolIdx = -1; 
    }
} 
void Diderot_GridAdd2D(Diderot_vec2_t pos, uint32_t id, GridContext * ctx) 
{ 
   uint32_t gridIdx = worldToGridIdx2D(pos,ctx);
   uint32_t newPoolIdx = 0; 

   GridItem_t * newItem = newGridItem(ctx,&newPoolIdx);
   newItem->strandId = id; 

   if(ctx->grid[gridIdx].poolIdx != -1) 
     newItem->next = ctx->grid[gridIdx].poolIdx; 
   else 
     newItem->next = -1; 

   ctx->grid[gridIdx].poolIdx = newPoolIdx; 
} 
void buildGrid2D(@STRANDTY@ ** strands, GridContext * ctx) 
{ 
      Diderot_GridClear(ctx); 

    for(int id = 0; id < ctx->numberOfStrands; id++) {
        Diderot_GridAdd2D(strands[id]->pos,strands[id]->id,ctx);
    }  
} 
/************* Query Functions *******************/
 Diderot_DynSeq_t * spherical_query2D(@STRANDTY@ ** strands,
                                      @STRANDTY@  * qStrand,
                                      GridContext * ctx,
                                      QueryPool_t * queryPool,
                                      Diderot_real_t radius) 
{    
    Diderot_vec2_t strandPosition = qStrand->pos;

    Diderot_union2_t gridSize; 
    gridSize.v = *(ctx->gridSize); 

    Diderot_union2_t cellSize; 
    cellSize.v = *(ctx->cellSize); 
 
    Diderot_iunion2_t gridPos; 
    gridPos.v = worldToCell(strandPosition, ctx); 
    
    Diderot_iunion2_t xBounds; 
    xBounds.v = vec2i(clamp_int((gridPos.i[0] - ceil(radius/(float)cellSize.r[0])) - 1,
                                0,
                                gridSize.r[0]),
                      clamp_int((gridPos.i[0] + ceil(radius/(float)cellSize.r[0])) - 1,
                                0,
                                gridSize.r[0]));
 
    
     Diderot_iunion2_t  yBounds;  
    yBounds.v = vec2i(clamp_int((gridPos.i[1] - ceil(radius/(float)cellSize.r[1])) - 1,
                                0,
                                gridSize.r[1]),
                      clamp_int((gridPos.i[1] + ceil(radius/(float)cellSize.r[1])) - 1,
                                 0,
                                 gridSize.r[1]));

    int neighborCount = 0;  

   Diderot_QueryClearPool(queryPool);

   for(int x = xBounds.i[0]; x <= xBounds.i[1]; x++){
        for(int y = yBounds.i[0]; y <= yBounds.i[1]; y++) {
                 int neighborCellIndx = UMAD(y,gridSize.r[1] + 2,x);
                 GridItem_t * neighborCellStrands =  Diderot_GridFindItemInGrid(neighborCellIndx,ctx);
                while(neighborCellStrands != 0) {
                      if(isWithinRadius2D(strandPosition,strands[neighborCellStrands->strandId]->pos,radius)){
                            if(neighborCellStrands->strandId != qStrand->strandId) {
                                Diderot_QueryAddToPool(strands[neighborCellStrands->strandId],queryPool); 
                                neighborCount++; 
                            }
                      }
                     if(neighborCellStrands->next == -1) 
                        break; 
                     else 
                        neighborCellStrands = Diderot_GridFindItemInPool(neighborCellStrands->next,ctx);
                  }
        }
    }   
    Diderot_DynSeq_t * seq = Diderot_DynSeqAlloc(sizeof(@STRANDTY@ *),neighborCount);
    seq->data = queryPool->pool;
 
    return seq;
} 
 Diderot_DynSeq_t * spherical_query(@STRANDTY@ ** strands,
                                    @STRANDTY@ * qStrand,
                                   GridContext * ctx,
                                   QueryPool_t * queryPool,
                                   Diderot_real_t radius) 
{
   return spherical_query2D(strands,qStrand,ctx,queryPool,radius);
}

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0