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

SCM Repository

[diderot] View of /branches/lamont/test/implicit-surface/benchmark/genPosns.c
ViewVC logotype

View of /branches/lamont/test/implicit-surface/benchmark/genPosns.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2261 - (download) (as text) (annotate)
Wed Mar 6 21:42:50 2013 UTC (6 years, 6 months ago) by lamonts
File size: 2096 byte(s)
Adding benchmark directory for unit-circle
/*
 * genPosns.c 
 *
 * Generates random real numbers that represent postions on a unit circle. 
 *
 */

#include <stdlib.h>
#include <math.h>  
#include <stdio.h> 
#include <time.h> 
#include <teem/nrrd.h> 
#include <assert.h>
#include <sys/sysctl.h>
#include <sys/stat.h>

#define PI 3.14159265359
#define HIGH 100.5
#define LO .5

float randomReal(float min, float max){
    return min + (float)rand()/((float)RAND_MAX/(max - min));
}
void generateNrdd(float * particles, const char * nrrdFileName, int numberOfParticles)
{
    Nrrd *nval;
    nval = nrrdNew();
    int status;
    
    status = nrrdWrap_va(nval, particles, nrrdTypeFloat, 1, numberOfParticles);
    
    if(status){
        printf("Error in trying to use nrddWar_va\n");
        exit(1);
    }
    
    status = nrrdSave(nrrdFileName, nval, NULL);

    if(status){
        printf("Error in trying to use nrrdSave\n");
        exit(1);
    }
    
    nrrdNix(nval);
}
void generateTxt(float * particles, const char * txtFilename, int numberOfParticles)
{
    FILE *fh = fopen(txtFilename, "w");
    
    if(fh == NULL){
        printf("Error opening file:%s\n",txtFilename);
        exit(1);
    }
    
    int size = numberOfParticles;
    for(int i =0; i <size; i++)
    {
        fprintf(fh,"%.2f\n",particles[i]);
    }
    fclose(fh);
}
int main(int argc, char * argv[]) 
{ 
    float * strands;
    srand(time(NULL)); 
    int numberOfStrands = 0; 
    char * nrrdFilename;
    
   if(argc == 2) {
        nrrdFilename = "posns.nrrd";
        numberOfStrands = atoi(argv[1]);
        strands = (float *) malloc(sizeof(float) * numberOfStrands * 2);
        
        for(int i = 0; i < (numberOfStrands * 2); i+=2)
        {
            float angle = randomReal(0,2*PI); 
            strands[i] = cos(angle); 
            strands[i+1] = sin(angle); 
        }
       
        generateNrdd(strands,nrrdFilename,numberOfStrands * 2);
        //generateTxt(strands,"posns.txt",numberOfStrands * 2);
    }else { 
        fprintf(stderr, "use genPart: [numberOfStrands]\n");
        exit(1);
    }
 
    return 0; 
} 


    	

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