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

SCM Repository

[diderot] View of /branches/lamont/src/lib/common/grid.c
ViewVC logotype

View of /branches/lamont/src/lib/common/grid.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2228 - (download) (as text) (annotate)
Fri Mar 1 18:02:43 2013 UTC (7 years, 5 months ago) by lamonts
File size: 2367 byte(s)
Adding a grid.c file
/*! \file dyn-seq.c
 *
 * \author Lamont Samuels
 *
 * An implementation of the uniform grid. 
 */

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

#include "Diderot/diderot.h"

Diderot_vec2_t worldToGrid2D(Diderot_vec2_t p, Grid2D_t * 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); 
} 
Diderot_ivec2_t worldToCell(Diderot_vec2_t p, Grid2D_t * 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]))));
} 
uint32_t worldToGridIdx2D(Diderot_vec2_t p, Grid2D_t * 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] + 2),u.i[0]);
} 
/* FIXME: Need to update the 3D functions to work once the 2D versions work */ 
Diderot_ivec3_t worldToGrid3D(Diderot_vec3_t p, Grid3D_t * 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])));
}
Diderot_ivec3_t  clampToGrid3D(Grid3D_t * 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
        };

}

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