Skip to content
Snippets Groups Projects
simulation.cpp 2.25 KiB
#include <fstream>
#include "simulation.h"

Cone createCone(const Setup* const config){
    // static Vector3D fakeSource(config.R, 0, 0);

    // randomly pick up two pixels
    int i = std::rand() % (config->pixels.size() / 2);
    int j = -1;
    while (true)
    {
        j = std::rand() % (config->pixels.size() / 2);
        if (j != i)
        {
            break;
        }
    }
    Vector3D site1 = config->pixels[i];
    Vector3D site2 = config->pixels[j];
    // randomly select z coordinates of two interations
    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;
    // calculate angle
    Vector3D axis = site2 - site1;
    double cosAngle = getCosAngle(site1 - config->trueSource, axis);
    return Cone(site1, axis, cosAngle, 0, 0);
}

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;
        while (coneQueue->size_approx() >= config->capacity)
        {
            gSystem->Sleep(50);
        }
        
        // printf("You are in worker Thread. Total counts is: %lld\n", counts);
    }
}

void * Worker::reader(void *ptr) {
    std::ifstream conefile;
    std::string fpath("Data/cones.txt");
    conefile.open(fpath, std::ios::in);
    if (!conefile.good())
    {
        throw std::invalid_argument("Cannot find file: " + fpath);
    }
    
    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();
}