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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2201 - (view) (download)

1 : lamonts 2191 //! \brief allocate memory for the grid context
2 :     //! \param gridSize the dimensions of the grid
3 :     //! \param numberOfStrands the number of strands
4 :     //! \param cellSize the size of the cells
5 :     //! \param window the viewing window (i.e. [XMIN,XMAX,YMIN,YMAX])
6 :     //! \return the address of the grid context
7 :     GridContext * Diderot_GridAllocGrid2D(Diderot_vec2_t * gridSize,
8 :     uint32_t numberOfStrands,
9 :     Diderot_vec2_t * cellSize,
10 :     Diderot_vec4_t * window)
11 :     {
12 : lamonts 2162
13 : lamonts 2191 GridContext * newGridContext = CheckedAlloc (sizeof(GridContext));
14 : lamonts 2094
15 : lamonts 2191 newGridContext->gridSize = gridSize;
16 :     newGridContext->numberOfStrands = numberOfStrands;
17 :     newGridContext->cellSize = cellSize;
18 :     newGridContext->window = window;
19 : lamonts 2094
20 : lamonts 2191 Diderot_union2_t v;
21 :     v.v = *(gridSize);
22 : lamonts 2094
23 : lamonts 2191 newGridContext->grid = CheckedAlloc(sizeof(GridCell_t)*((Diderot_int_t)v.r[0] + 2) * ((Diderot_int_t)v.r[1] + 2));
24 :     newGridContext->gridPool.poolSize = numberOfStrands;
25 :     newGridContext->gridPool.handle = 0;
26 :     newGridContext->gridPool.pool = CheckedAlloc(sizeof(GridItem_t)*numberOfStrands);
27 :    
28 :     return newGridContext;
29 :     }
30 :     void Diderot_GridAdd2D(Diderot_vec2_t pos, uint32_t id, GridContext * ctx)
31 : lamonts 2162 {
32 : lamonts 2191 uint32_t gridIdx = worldToGridIdx2D(pos,ctx);
33 :     uint32_t newPoolIdx = 0;
34 : lamonts 2160
35 : lamonts 2191 GridItem_t * newItem = newGridItem(ctx,&newPoolIdx);
36 :     newItem->strandId = id;
37 : lamonts 2160
38 : lamonts 2191 if(ctx->grid[gridIdx].poolIdx != -1)
39 :     newItem->next = ctx->grid[gridIdx].poolIdx;
40 :     else
41 :     newItem->next = -1;
42 : lamonts 2160
43 : lamonts 2191 ctx->grid[gridIdx].poolIdx = newPoolIdx;
44 :     }
45 : lamonts 2201 void Diderot_GridBuild2D(@STRANDTY@ ** strands, GridContext * ctx)
46 : lamonts 2191 {
47 : lamonts 2201 Diderot_GridClear(ctx);
48 : lamonts 2191
49 :     for(int id = 0; id < ctx->numberOfStrands; id++) {
50 :     Diderot_GridAdd2D(strands[id]->pos,strands[id]->id,ctx);
51 :     }
52 :     }
53 : lamonts 2094 /************* Query Functions *******************/
54 : lamonts 2160 Diderot_DynSeq_t * spherical_query2D(@STRANDTY@ ** strands,
55 : lamonts 2191 @STRANDTY@ * qStrand,
56 :     GridContext * ctx,
57 :     QueryPool_t * queryPool,
58 :     Diderot_real_t radius)
59 :     {
60 : lamonts 2094 Diderot_vec2_t strandPosition = qStrand->pos;
61 :    
62 : lamonts 2191 Diderot_union2_t gridSize;
63 : lamonts 2162 gridSize.v = *(ctx->gridSize);
64 : lamonts 2094
65 : lamonts 2191 Diderot_union2_t cellSize;
66 : lamonts 2162 cellSize.v = *(ctx->cellSize);
67 :    
68 : lamonts 2191 Diderot_iunion2_t gridPos;
69 :     gridPos.v = worldToCell(strandPosition, ctx);
70 : lamonts 2162
71 : lamonts 2191 Diderot_iunion2_t xBounds;
72 :     xBounds.v = vec2i(clamp_int((gridPos.i[0] - ceil(radius/(float)cellSize.r[0])) - 1,
73 : lamonts 2094 0,
74 : lamonts 2191 gridSize.r[0]),
75 :     clamp_int((gridPos.i[0] + ceil(radius/(float)cellSize.r[0])) - 1,
76 : lamonts 2094 0,
77 : lamonts 2191 gridSize.r[0]));
78 : lamonts 2094
79 :    
80 : lamonts 2201 Diderot_iunion2_t yBounds;
81 : lamonts 2191 yBounds.v = vec2i(clamp_int((gridPos.i[1] - ceil(radius/(float)cellSize.r[1])) - 1,
82 : lamonts 2162 0,
83 : lamonts 2191 gridSize.r[1]),
84 :     clamp_int((gridPos.i[1] + ceil(radius/(float)cellSize.r[1])) - 1,
85 : lamonts 2094 0,
86 : lamonts 2191 gridSize.r[1]));
87 : lamonts 2094
88 : lamonts 2201 int neighborCount = 0;
89 : lamonts 2094
90 : lamonts 2191 Diderot_QueryClearPool(queryPool);
91 :    
92 :     for(int x = xBounds.i[0]; x <= xBounds.i[1]; x++){
93 :     for(int y = yBounds.i[0]; y <= yBounds.i[1]; y++) {
94 :     int neighborCellIndx = UMAD(y,gridSize.r[1] + 2,x);
95 : lamonts 2201 GridItem_t * neighborCellStrands = Diderot_GridFindCellStart(neighborCellIndx,ctx);
96 : lamonts 2160 while(neighborCellStrands != 0) {
97 : lamonts 2094 if(isWithinRadius2D(strandPosition,strands[neighborCellStrands->strandId]->pos,radius)){
98 :     if(neighborCellStrands->strandId != qStrand->strandId) {
99 : lamonts 2191 Diderot_QueryAddToPool(strands[neighborCellStrands->strandId],queryPool);
100 : lamonts 2160 neighborCount++;
101 : lamonts 2094 }
102 :     }
103 : lamonts 2191 if(neighborCellStrands->next == -1)
104 :     break;
105 :     else
106 : lamonts 2201 neighborCellStrands = Diderot_GridFindItemInCell(neighborCellStrands->next,ctx);
107 : lamonts 2094 }
108 :     }
109 :     }
110 : lamonts 2191 Diderot_DynSeq_t * seq = Diderot_DynSeqAlloc(sizeof(@STRANDTY@ *),neighborCount);
111 :     seq->data = queryPool->pool;
112 :    
113 : lamonts 2160 return seq;
114 :     }

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