#include <iostream>
#include "simulator.h"
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;
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);
}