rts_glRenderableDTGrid.cpp 7.22 KB
#include "rts_glRenderableDTGrid.h"


/*ITERATOR INITIALIZATION*/
void rts_glRenderableDTGrid::init_iter_ppp()
{
	m_iterator = begin();
	m_iter_type = PPP;
}
void rts_glRenderableDTGrid::init_iter_nnn()
{
	m_iterator = last();
	m_iter_type = NNN;
}
void rts_glRenderableDTGrid::init_iter_ppn()
{
	m_iterator = begin();	//put the iterator at the beginning of the data set

	if(m_iterator.grid->acc.size() > 1)	//if there is more than one pcolumn
	{
		m_iterator.loc.iv = m_iterator.grid->proj2D.value[1].to_value - 1;
		m_iterator.loc.z = zCoord[m_iterator.grid->proj2D.value[1].to_coord - 1];
	}
	else
	{
		m_iterator.loc.iv = value.size() - 1;
		m_iterator.loc.z = zCoord.back();
	}
	m_iter_type = PPN;
}

void rts_glRenderableDTGrid::init_iter_npp()
{
	//set the 1D iterator location
	m_iterator.Iterator2D.Iterator1D.loc.iv = proj2D.proj1D.value.size()-1;	//start at the last 1D p-column
	m_iterator.Iterator2D.Iterator1D.loc.ic = proj2D.proj1D.xCoord.size() - 2;
	m_iterator.Iterator2D.Iterator1D.loc.x = proj2D.proj1D.xCoord[m_iterator.Iterator2D.Iterator1D.loc.ic+1];
	m_iterator.Iterator2D.Iterator1D.value = proj2D.proj1D.value[m_iterator.Iterator2D.Iterator1D.loc.iv];
	m_iterator.Iterator2D.Iterator1D.grid = &(proj2D.proj1D);

	//set the 2D iterator location
	m_iterator.Iterator2D.loc.iv = proj2D.proj1D.value[m_iterator.Iterator2D.Iterator1D.loc.iv].to_value;
	m_iterator.Iterator2D.loc.ic = proj2D.proj1D.value[m_iterator.Iterator2D.Iterator1D.loc.iv].to_coord;
	m_iterator.Iterator2D.loc.y = proj2D.yCoord[m_iterator.Iterator2D.loc.ic];
	m_iterator.Iterator2D.value = proj2D.value[m_iterator.Iterator2D.loc.iv];
	m_iterator.Iterator2D.grid = &(proj2D);

	//set the 3D iterator location
	m_iterator.loc.iv = proj2D.value[m_iterator.Iterator2D.loc.iv].to_value;
	m_iterator.loc.ic = proj2D.value[m_iterator.Iterator2D.loc.iv].to_coord;
	m_iterator.loc.z = zCoord[m_iterator.loc.ic];
	m_iterator.value = value[m_iterator.loc.iv];
	m_iterator.grid = this;

	m_iter_type = NPP;
}

void rts_glRenderableDTGrid::iter_next_npp()
{	
	//if we are at the end of a p-column
	if(m_iterator.loc.iv == value.size() - 1 ||	//if we are at the end of the last p-column or
		(m_iterator.Iterator2D.loc.iv != m_iterator.grid->proj2D.value.size() - 1 &&
		m_iterator.loc.iv == proj2D.value[m_iterator.Iterator2D.loc.iv + 1].to_value - 1)) //we are at the end of a p-column
	{
		iter_next_npx(m_iterator.Iterator2D);	//increment the 2D iterator
		m_iterator.loc.iv = proj2D.value[m_iterator.Iterator2D.loc.iv].to_value;
		m_iterator.loc.ic = proj2D.value[m_iterator.Iterator2D.loc.iv].to_coord;
		m_iterator.loc.z = zCoord[m_iterator.loc.ic];
	}
	//if we are at the end of a connected component
	else if(m_iterator.loc.z == zCoord[m_iterator.loc.ic+1])
	{
		m_iterator.loc.iv++;
		m_iterator.loc.ic+=2;
		m_iterator.loc.z = zCoord[m_iterator.loc.ic];
	}
	//else if we are in a p-column
	else
	{
		m_iterator.loc.iv++;
		m_iterator.loc.z++;
	}
	m_iterator.value = value[m_iterator.loc.iv];
}

void rts_glRenderableDTGrid::iter_next_ppn()
{
	//if we are at the beginning of a p-column
	if(m_iterator.loc.z == zCoord[m_iterator.loc.ic] &&
		m_iterator.loc.ic == proj2D.value[m_iterator.Iterator2D.loc.iv].to_coord)
	{
		m_iterator.Iterator2D.increment();	//move to the next p-column
		if(m_iterator.Iterator2D.loc.iv >= proj2D.value.size())	//return
			return;
		//now update the 3D locator
		if(m_iterator.Iterator2D.loc.iv == proj2D.value.size() - 1)	//if this is the last p-column
		{
			m_iterator.loc.iv = value.size() - 1;
			m_iterator.loc.ic = zCoord.size() - 2;
			m_iterator.loc.z = zCoord.back();
		}
		else
		{
			m_iterator.loc.iv = proj2D.value[m_iterator.Iterator2D.loc.iv+1].to_value - 1;
			m_iterator.loc.ic = proj2D.value[m_iterator.Iterator2D.loc.iv+1].to_coord - 2;
			m_iterator.loc.z = zCoord[m_iterator.loc.ic + 1];
		}
	}
	//if we are at the beginning of a connected component
	else if(m_iterator.loc.z == zCoord[m_iterator.loc.ic])
	{
		m_iterator.loc.iv-=1;
		m_iterator.loc.ic-=2;
		m_iterator.loc.z = zCoord[m_iterator.loc.ic + 1];
	}
	//if we're in the middle of a connected component
	else
	{
		m_iterator.loc.iv--;
		m_iterator.loc.z--;
	}
	m_iterator.value = value[m_iterator.loc.iv];
}

void rts_glRenderableDTGrid::iter_next_npx(rtsDTGrid2D<IndexPair>::iterator &i2d)
{
	//if we are at the end of a p-column
	if(i2d.loc.iv == i2d.grid->value.size() - 1	||	//if this is the last p-column
		(i2d.Iterator1D.loc.iv != i2d.grid->proj1D.value.size() - 1 &&	//we are not in the last p-column and
		i2d.loc.iv == i2d.grid->proj1D.value[i2d.Iterator1D.loc.iv+1].to_value - 1))
	{
		//if(i2d.Iterator1D.loc.iv == 0)	//if it is the first p-column
		//	return;
		i2d.Iterator1D.decrement();		//decrement the 1D iterator
		//update the 2D locator
		i2d.loc.iv = i2d.grid->proj1D.value[i2d.Iterator1D.loc.iv].to_value;
		i2d.loc.ic = i2d.grid->proj1D.value[i2d.Iterator1D.loc.iv].to_coord;
		i2d.loc.y = i2d.grid->yCoord[i2d.loc.ic];
	}
	//if we are at the end of a connected component
	else if(i2d.loc.y == i2d.grid->yCoord[i2d.loc.ic+1])
	{
		i2d.loc.iv++;
		i2d.loc.ic+=2;
		i2d.loc.y = i2d.grid->yCoord[i2d.loc.ic];
	}
	else
	{
		i2d.loc.iv++;
		i2d.loc.y++;
	}
	//set value
	i2d.value = i2d.grid->value[i2d.loc.iv];
}
	

void rts_glRenderableDTGrid::iter_next()
{
	if(m_iter_type == PPP)
		m_iterator.increment();
	if(m_iter_type == NNN)
		m_iterator.decrement();
	if(m_iter_type == PPN)
		iter_next_ppn();
	if(m_iter_type == NPP)
		iter_next_npp();
}
void rts_glRenderableDTGrid::RenderPoints(unsigned int size, float camera_x, float camera_y, float camera_z)
{
	/*This function renders a single point to the screen for each voxel in the
	DT-Grid.
	*/
	//determine which directions to iterate
	if(camera_x > 0)
	{
		if(camera_y > 0)
		{
			if(camera_z > 0)
			{
				//cout<<"+++"<<endl;
				init_iter_ppp();
			}
			else
			{
				//cout<<"++-"<<endl;
				init_iter_ppn();
			}
		}
		else
		{
			if(camera_z > 0)
			{
				//cout<<"+-+"<<endl;
				init_iter_ppp();
			}
			else
			{
				//cout<<"+--"<<endl;
				init_iter_ppp();
			}
		}
	}
	else
	{
		if(camera_y > 0)
		{
			if(camera_z > 0)
			{
				//cout<<"-++"<<endl;
				init_iter_npp();
			}
			else
			{
				//cout<<"-+-"<<endl;
				init_iter_nnn();
			}
		}
		else
		{
			if(camera_z > 0)
			{
				//cout<<"--+"<<endl;
				init_iter_nnn();
			}
			else
			{
				//cout<<"---"<<endl;
				init_iter_nnn();
			}
		}
	}


	//init_iter_npp();


	glPointSize(3);
	glDisable(GL_DEPTH_TEST);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);



	glBegin(GL_POINTS);					//render voxels as points
	unsigned int i;
	unsigned int num_values = value.size();
	for(i=0; i<num_values; i++)
	{
		glColor4f(m_iterator.value.r, m_iterator.value.g, m_iterator.value.b, m_iterator.value.a);
		glVertex3f(m_iterator.getX()/(float)MaxX(), m_iterator.getY()/(float)MaxY(), m_iterator.getZ()/(float)MaxZ());
		//cout<<"("<<m_iterator.getX()<<","<<m_iterator.getY()<<","<<m_iterator.getZ()<<"):"<<m_iterator.value<<endl;
		iter_next();
	}
	//cout<<"---------------------------------------"<<endl;
	i=0;
	glEnd();

	glDisable(GL_BLEND);
	glEnable(GL_DEPTH_TEST);
}