Blob Blame Raw

#include <iostream>

#include "simulator.h"


void SimulatorXYZ::simulate(const Track &track, const Tool &tool) {
    points.clear();
    simu_index = 0;
    raster.x0 = raster.x1 = raster.y0 = raster.y1 = raster.min_value = raster.max_value = 0;

    if (!raster.raster.get_data() || track.points.empty()) return;
    
    std::cout << "SimulatorXYZ::simulate" << std::endl;
    std::cout << "SimulatorXYZ::simulate: track points: " << track.points.size() << std::endl;
    
    const TrackPoint &first = track.points.front();
    raster.x0 = raster.x1 = first.position.x;
    raster.y0 = raster.y1 = first.position.y;
    raster.min_value = raster.max_value = first.position.z;
    for(TrackPointList::const_iterator i = track.points.begin(); i != track.points.end(); ++i) {
        if (raster.x0 > i->position.x) raster.x0 = i->position.x;
        if (raster.x1 < i->position.x) raster.x1 = i->position.x;
        if (raster.y0 > i->position.y) raster.y0 = i->position.y;
        if (raster.y1 < i->position.y) raster.y1 = i->position.y;
        if (raster.min_value > i->position.z) raster.min_value = i->position.z;
        if (raster.max_value < i->position.z) raster.max_value = i->position.z;
    }
    
    const Real precision = 1e-5;
    if ( raster.x1 + precision < raster.x0
      || raster.y1 + precision < raster.y0
      || raster.max_value + precision < raster.min_value )
    {
        std::cout << "SimulatorXYZ::simulate: zero bounds" << std::endl;
        return;
    }
    
    raster.raster.fill(raster.max_value);

    Real x0 = raster.x0;
    Real y0 = raster.y0;
    Real dx = raster.x1 - x0;
    Real dy = raster.y1 - y0;
    Real w = raster.raster.get_width();
    Real h = raster.raster.get_height();

    Vector2 scale_to_raster(w/dx , h/dy);
    Vector2 scale_to_tool  (dx/w , dy/h);
    FlatToolPainter painter(&tool, scale_to_tool);

    unsigned long long max_count = 2ull*1024*1024*1024/sizeof(points.front());
    
    int index = 0;
    for(TrackPointList::const_iterator i = track.points.begin(); i != track.points.end(); ++i, ++index) {
        painter.distance = i->position.z;

        Vector2 position(
            scale_to_raster.x * (i->position.x - x0),
            scale_to_raster.y * (i->position.y - y0) );

        raster.raster.touch(painter, position, index, points);
        if (points.size() >= max_count) {
            std::cerr << "SimulatorXYZ::simulate: reached memory limit at time " <<  i->time << std::endl;
            break;
        }
        
        if (index % 1000 == 0)
            std::cout << "." << std::flush;
        if (index % 100000 == 0)
            std::cout << std::endl << std::flush;
    }
    simu_index = (int)points.size();

    std::cout << std::endl;
    std::cout << "SimulatorXYZ::simulate: done" << std::endl;
}

void SimulatorXYZ::scroll_to(int index) {
    while(simu_index < (int)points.size() && points[simu_index].index <= index)
        points[simu_index++].apply_next(raster.raster);
    while(simu_index > 0 && points[simu_index-1].index > index)
        points[--simu_index].apply_prev(raster.raster);
}


void SimulatorXYZA::simulate(const Track &track, const Tool &tool) {
    points.clear();
    simu_index = 0;
    raster.x0 = raster.x1 = raster.min_value = raster.max_value = 0;

    if (!raster.raster.get_data() || track.points.empty()) return;
    
    std::cout << "SimulatorXYZA::simulate" << std::endl;
    std::cout << "SimulatorXYZA::simulate: track points: " << track.points.size() << std::endl;
    
    raster.x0 = raster.x1 = track.points.front().position.x;
    for(TrackPointList::const_iterator i = track.points.begin(); i != track.points.end(); ++i) {
        Real distance = sqrt(i->position.y*i->position.y + i->position.z*i->position.z);
        if (raster.max_value < distance) raster.max_value = distance;
        if (raster.x0 > i->position.x) raster.x0 = i->position.x;
        if (raster.x1 < i->position.x) raster.x1 = i->position.x;
    }
    raster.raster.fill(raster.max_value);
    
    if (raster.x1 + 1e-5 < raster.x0) {
        std::cout << "SimulatorXYZA::simulate: zero bound" << std::endl;
        return;
    }
    
    Real x0 = raster.x0;
    Real dx = raster.x1 - x0;
    Real h = raster.raster.get_height();
    Real w = raster.raster.get_width();

    Vector2 scale_to_raster( w/dx , 1.0/360.0 );
    Vector2 scale_to_tool  ( dx/w , 2*M_PI/h  );
    PolarToolPainter painter(&tool, scale_to_tool, 1);

    unsigned long long max_count = 2ull*1024*1024*1024/sizeof(points.front());
    
    int index = 0;
    for(TrackPointList::const_iterator i = track.points.begin(); i != track.points.end(); ++i, ++index) {
        painter.distance = sqrt(i->position.y*i->position.y + i->position.z*i->position.z);
        if (painter.distance < 1e-5) painter.distance = 1e-5;
        
        Vector2 position(
            scale_to_raster.x * (i->position.x - x0),
            scale_to_raster.y * i->angle );
        position.y = (position.y - floor(position.y))*h;
        Vector2 rxy = painter.get_radius_xy();

        raster.raster.touch(painter, position, index, points);
        if (position.y - rxy.y < 0)
            raster.raster.touch(painter, Vector2(position.x, position.y + h), index, points);
        if (position.y + rxy.y > h)
            raster.raster.touch(painter, Vector2(position.x, position.y - h), index, points);
        if (points.size() >= max_count) {
            std::cerr << "SimulatorXYZA::simulate: reached memory limit at time " <<  i->time << std::endl;
            break;
        }
        
        if (index % 1000 == 0)
            std::cout << "." << std::flush;
        if (index % 100000 == 0)
            std::cout << std::endl << std::flush;
    }
    simu_index = (int)points.size();

    std::cout << std::endl;
    std::cout << "SimulatorXYZA::simulate: done" << std::endl;
}
    
void SimulatorXYZA::scroll_to(int index) {
    while(simu_index < (int)points.size() && points[simu_index].index <= index)
        points[simu_index++].apply_next(raster.raster);
    while(simu_index > 0 && points[simu_index-1].index > index)
        points[--simu_index].apply_prev(raster.raster);
}