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 2201 - (download) (annotate)
Sun Feb 24 16:02:12 2013 UTC (7 years, 8 months ago) by lamonts
File size: 4474 byte(s)
Refactored the spatail implementation slightly
//! \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; 
}
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 Diderot_GridBuild2D(@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_GridFindCellStart(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_GridFindItemInCell(neighborCellStrands->next,ctx);
                  }
        }
    }   
    Diderot_DynSeq_t * seq = Diderot_DynSeqAlloc(sizeof(@STRANDTY@ *),neighborCount);
    seq->data = queryPool->pool;
 
    return seq;
} 

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