
#include "cyclestate.h"

#include "globalstate.h"
#include "reconmap.h"
#include "glpic.h"

CycleState::CycleState(void) {

        _max_subd_level = DEFAULT_MAX_SUBD_LEVEL;
	_foveal_sampling = DEFAULT_FOVEAL_SAMPLING;

        _x_winsize = _y_winsize = 0;

	_frame_id = 0;
	_sampled_all_pixels = 0;
	_finished_frame_req = 0;
	_interpolate_disabled = 1; //on the first frame we have no work for reconstruct to do!
	_use_hardware_interpolation = 0;

	_sample_list = new SampleList();
	_our_samplebuffer = _samplebuffer = new ReconMap();
	_our_pixelbuffer = _pixelbuffer= new GLPic();
	_our_plane_cache = _plane_cache = new PlaneCache();
	_num_jobs_per_frame = 0;
	ClearErrors();
	ClearStats();
}

CycleState::~CycleState() {
  _sample_list->Clear();

  delete _sample_list;
  delete _our_samplebuffer;
  delete _our_pixelbuffer;
  delete _our_plane_cache;

}

CycleState& CycleState::CopyFrom(const CycleState &) {

	cerr << "Error: CycleState::CopyFrom() called" << endl;
	exit(-1);

	return *this;
}

/*
ostream& operator<<(ostream &co, const CycleState &C) {

	return co;
}

istream& operator>>(istream &ci, CycleState &C) {

	return ci;
}
*/

void CycleState::AddErrorRay (const Ray &ray, float t)
{
  if (_num_error_rays < NUM_ERROR_RAYS)
    {
    _error_rays [_num_error_rays] = ray;
    _error_rays [_num_error_rays++].set_t (t);
    }
}

void CycleState::AddErrorBbox (const Bounds3d &box)
{
  if (_num_error_bboxes < NUM_ERROR_BBOXES)
    _error_bboxes [_num_error_bboxes++] = box;
}

void CycleState::DrawErrorRays (void)
{
  glColor3f (.8, .5, .8);
  for (int i = 0; i < _num_error_rays; i++)
    {
      _error_rays[i].DrawT1T2NoColor ( 0., _error_rays[i].t());

    }
}
void CycleState::DrawErrorBboxes (void)
{
  glColor3f (.6, 0., .6);
  for (int i = 0; i < _num_error_bboxes; i++)
    {
      _error_bboxes[i].drawWireFrame();
    }
}

void CycleState::ClearStats (void)
{
  _num_traversals_avoided = 0;
  _num_intersections_avoided = 0;
  ClearErrors();
  _planes_tested = _planes_calced = _plane_tests_requested =0;
  _objs_classified = 0;
  _full_shadow_tests = _cache_hit_shadow_tests = _gratuitous_full_shadow_tests = 0;
  _objs_checked.Clear(); 
  _cells_entered.Clear();
  _objs_intersected.Clear();
  _num_classified_straddling = 0;
  _num_classified_to_one_side = 0;
  _num_on_one_side_of_kids = _num_straddles_kids = 0;
}
