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

SCM Repository

[diderot] View of /branches/lamont/src/include/Diderot/grid.h
ViewVC logotype

View of /branches/lamont/src/include/Diderot/grid.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2162 - (download) (as text) (annotate)
Thu Feb 21 04:59:17 2013 UTC (7 years, 3 months ago) by lamonts
File size: 4783 byte(s)
Fixed bugs in my grid.h from new spatial implementation
/*! \file grid.h
 *
 * \author Lamont Samuels
 *
 * This is includes the structure of the uniform grid on the CPU and functions for calculating cell indicies for strand positions.
 */

/*
 * COPYRIGHT (c) 2010 The Diderot Project (http://diderot-language.cs.uchicago.edu)
 * All rights reserved.
 */

#ifndef _GRID_H_
#define _GRID_H_

#ifndef _DIDEROT_TYPES_H_
#include "Diderot/types.h"
#endif

#define UMAD(a, b, c)  ( (a) * (b) + (c) )

#define XMIN 0 
#define XMAX 1 
#define YMIN 2
#define YMAX 3 

//StrandItem_t -> Strand Id linked list node
typedef struct strandItem{
    uint32_t strandId;             //the unique identification number for a strand  
    struct strandItem * next;      //the next strand item in the list 
    struct strandItem * prev;      //the previous strand item in the list
}StrandItem_t;

//GridContext -> Information about the grid 
typedef struct GridCtx2D { 
   Diderot_vec2_t * gridSize;            // the size(s) of the spatial grid structure
   uint32_t numberOfStrands;               // The nuber of strands in execution
   Diderot_vec2_t * cellSize;             // The size of each cell in the grid
   Diderot_vec4_t  * window;              // The window dimensions (XMin,XMax,YMin,YMax) 
   StrandItem_t ** grid;                   // the number of strands in each grid cell.  
} GridContext2D;

typedef struct GridCtx3D {
    Diderot_vec3_t * gridSize;    // the size(s) of the spatial grid structure
    uint32_t numberOfStrands;      // The nuber of strands in execution
    Diderot_vec3_t * cellSize;     // The size of each cell in the grid
    Diderot_vec6_t * window;       // The window dimensions (XMin,XMax,YMin,YMax,ZMin,ZMax)
    StrandItem_t ** grid;          // the number of strands in each grid cell.
} GridContext3D;




STATIC_INLINE bool isWithinRadius2D( Diderot_vec2_t  particle,  Diderot_vec2_t neighbor, int radius) {
    return dist2(particle,neighbor) <= radius;
}
STATIC_INLINE bool isWithinRadius3D( Diderot_vec3_t  particle,  Diderot_vec3_t neighbor, int radius) {
    return dist3(particle,neighbor) <= radius;
}

STATIC_INLINE Diderot_int_t clamp_int(Diderot_int_t x, Diderot_int_t a, Diderot_int_t b)
{
    return x < a ? (a + 1) : (x > b ? (b + 1) : x);
}
STATIC_INLINE Diderot_ivec2_t clampToGrid2D(GridContext2D * ctx, Diderot_ivec2_t cell) {

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

    return vec2i(clamp_int(u.i[0],0,(Diderot_int_t)v.r[0] -1),
                 clamp_int(u.i[1],0,(Diderot_int_t)v.r[1] -1));
}
STATIC_INLINE Diderot_vec2_t worldToGrid2D(Diderot_vec2_t p, GridContext2D * ctx){

    Diderot_union2_t pos; 
    pos.v = p; 

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

    Diderot_union4_t winSize; 
    winSize.v = *(ctx->window); 

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

    Diderot_real_t gridWidth = cellSize.r[0] * gridSize.r[0]; 
    Diderot_real_t gridHeight = cellSize.r[1] * gridSize.r[1]; 

    Diderot_real_t x = (pos.r[0] - winSize.r[XMIN]) * gridWidth / (winSize.r[XMAX] - winSize.r[XMIN]); 
    Diderot_real_t y = (pos.r[1] - winSize.r[YMAX]) * gridHeight / (winSize.r[YMAX] - winSize.r[YMIN]); 

    return vec2(x,y); 
} 
STATIC_INLINE Diderot_ivec2_t worldToCell(Diderot_vec2_t p, GridContext2D * ctx){

    Diderot_union2_t u; 
    u.v = worldToGrid2D(p,ctx);

    Diderot_union2_t v; 
    v.v = *(ctx->cellSize); 
 
   return clampToGrid2D(ctx,vec2rtoi(vec2(floor(u.r[0] /v.r[0]),floor(u.r[1]/v.r[1]))));
} 
STATIC_INLINE uint32_t worldToGridIdx(Diderot_vec2_t p, GridContext2D * ctx){

    Diderot_ivec2_t cell = worldToCell(p,ctx);

    Diderot_iunion2_t u;
    u.v = cell;

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

   return UMAD(u.i[1],(Diderot_int_t)gridSize.r[1],u.i[0]);
} 
/* FIXME: Need to update the 3D functions to work once the 2D versions work */ 
STATIC_INLINE Diderot_ivec3_t worldToGrid3D(Diderot_vec3_t p, GridContext3D * ctx){
   
    Diderot_union3_t u;
    u.v = p;

    Diderot_union3_t v; 
    v.v = *(ctx->cellSize);     

    return vec3rtoi(vec3(floor(u.r[0] /v.r[0]),
                    floor(u.r[1]/v.r[1]),
                    floor(u.r[2] /v.r[2])));
}
STATIC_INLINE  Diderot_ivec3_t  clampToGrid3D(GridContext3D * ctx,  Diderot_ivec3_t  cell) {

    Diderot_iunion3_t u;
    u.v = cell;
    
    Diderot_union3_t v;
    v.v = *(ctx->gridSize);       

    Diderot_int_t x = clamp_int(u.i[0],0,(Diderot_int_t)v.r[0] -1); 
    Diderot_int_t y = clamp_int(u.i[1],0,(Diderot_int_t)v.r[1] -1);
    Diderot_int_t z = clamp_int(u.i[2],0,(Diderot_int_t)v.r[2] -1);

    return __extension__ (Diderot_ivec3_t){
            x,
            y,
            z,
            0
        };

}

#endif /* _GRID_H_ */ 

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