Skip to content
Snippets Groups Projects
simulation.cpp 2.18 KiB
Newer Older
  • Learn to ignore specific revisions
  • mingf2's avatar
    mingf2 committed
    #include <fstream>
    #include "simulation.h"
    
    mingf2's avatar
    mingf2 committed
    
    
    mingf2's avatar
    mingf2 committed
    Cone createCone(const Setup* const config){
    
    mingf2's avatar
    mingf2 committed
        // static Vector3D fakeSource(config.R, 0, 0);
    
        // randomly pick up two pixels
    
    mingf2's avatar
    mingf2 committed
        int i = std::rand() % (config->pixels.size() / 2);
    
    mingf2's avatar
    mingf2 committed
        int j = -1;
        while (true)
        {
    
    mingf2's avatar
    mingf2 committed
            j = std::rand() % (config->pixels.size() / 2);
    
    mingf2's avatar
    mingf2 committed
            if (j != i)
            {
                break;
            }
        }
    
    mingf2's avatar
    mingf2 committed
        Vector3D site1 = config->pixels[i];
        Vector3D site2 = config->pixels[j];
    
    mingf2's avatar
    mingf2 committed
        // randomly select z coordinates of two interations
    
    mingf2's avatar
    mingf2 committed
        site1.Z = (2 * float(std::rand()) / RAND_MAX - 1) * config->pitchZ / 2.0;
        site2.Z = (2 * float(std::rand()) / RAND_MAX - 1) * config->pitchZ / 2.0;
    
    mingf2's avatar
    mingf2 committed
        // calculate angle
        Vector3D axis = site2 - site1;
    
    mingf2's avatar
    mingf2 committed
        double cosAngle = getCosAngle(site1 - config->trueSource, axis);
    
    mingf2's avatar
    mingf2 committed
        return Cone(site1, axis, cosAngle, 0, 0);
    
    mingf2's avatar
    mingf2 committed
    }
    
    void * Worker::handle(void *ptr) {
        while(1) {
            //some work to get data (read board or file)
            for (int i = 0; i < config->chuckSize; i++)
            {
                coneQueue->enqueue(createCone(config));
            }
            counts += config->chuckSize;
    
    mingf2's avatar
    mingf2 committed
            while (coneQueue->size_approx() >= config->capacity)
    
    mingf2's avatar
    mingf2 committed
            {
                gSystem->Sleep(50);
            }
            
            // printf("You are in worker Thread. Total counts is: %lld\n", counts);
        }
    
    mingf2's avatar
    mingf2 committed
    }
    
    void * Worker::reader(void *ptr) {
        std::ifstream conefile;
        std::string fpath("/media/ming/DATA/projects/Imager/polimi/cones.txt");
        conefile.open(fpath, std::ios::in);
        std::string line;
        // skip header (first line)
        std::getline(conefile, line);
        
        while(1) {
            //some work to get data (read board or file)
            int i(0);
            while (i < config->chuckSize && std::getline(conefile, line))
            {
                coneQueue->enqueue(Cone(line));
                i++;
            }
            counts += i;
            if (conefile.eof())
            {
                // TODO
                // emit processing finished signal to maain thread
            }
            
            while (coneQueue->size_approx() >= config->capacity)
            {
                gSystem->Sleep(50);
            }
            
            // printf("You are in worker Thread. Total counts is: %lld\n", counts);
        }
        conefile.close();
    
    mingf2's avatar
    mingf2 committed
    }