temp_rts_glFilamentNetwork.cpp 11.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
#include "temp_rts_glFilamentNetwork.h"

void rts_glFilamentNetwork::RenderEdges()
{
	//for each edge
	vector<netEdge*>::iterator i;
	int a, b;
	point3D<double> v_a, v_b;
	glBegin(GL_LINES);						//start rendering lines
	for(i=m_edge_list.begin(); i!=m_edge_list.end(); i++)
	{
		v_a = (*i)->vertex_a->position;			//get the positions of the two vertex edges
		v_b = (*i)->vertex_b->position;
		glVertex3f(v_a.x, v_a.y, v_a.z);	//draw the vertices
		glVertex3f(v_b.x, v_b.y, v_b.z);
	}
	glEnd();	//finish drawing
}

void rts_glFilamentNetwork::RenderBranches()
{
	//for each vertex
	vector<netVertex*>::iterator i;
	point3D<double> v;
	glBegin(GL_POINTS);
	for(i=m_vertex_list.begin(); i!=m_vertex_list.end(); i++)
	{
		if((*i)->v_neighbors.size() > 2)
		{
			v = (*i)->position;
			glVertex3f(v.x, v.y, v.z);
		}
	}
	glEnd();
}

void rts_glFilamentNetwork::RenderFilaments()
{
	vector<netFilament*>::iterator i;
	int num_edges;
	point3D<double> v_a, v_b;
	int e;
	glBegin(GL_LINES);
	for(i=m_filament_list.begin(); i!=m_filament_list.end(); i++)	//for each filament
	{
		num_edges = (*i)->edges.size();
		for(e=0; e<num_edges; e++)
		{
			v_a = (*i)->edges[e]->vertex_a->position;
			v_b = (*i)->edges[e]->vertex_b->position;
			glVertex3f(v_a.x, v_a.y, v_a.z);
			glVertex3f(v_b.x, v_b.y, v_b.z);
		}
	}
	glEnd();
}

void rts_glFilamentNetwork::RenderSelectedFilaments()
{
	vector<unsigned int>::iterator f;
	int num_edges;
	point3D<double> v_a, v_b;
	int e;
	glBegin(GL_LINES);
	for(f=m_selected_list.begin(); f!=m_selected_list.end(); f++)
	{
		num_edges = m_filament_list[(*f)]->edges.size();
		for(e=0; e<num_edges; e++)
		{
			v_a = m_filament_list[(*f)]->edges[e]->vertex_a->position;
			v_b = m_filament_list[(*f)]->edges[e]->vertex_b->position;
			glVertex3f(v_a.x, v_a.y, v_a.z);
			glVertex3f(v_b.x, v_b.y, v_b.z);
		}
	}
	glEnd();
}

void rts_glFilamentNetwork::RenderOrientationColor(double x, double y, double z, double r, double g, double b)
{
	vector<netFilament*>::iterator i;
	int num_edges;
	point3D<double> v_a, v_b;

	//orientation variables
	vector3D<double> orientation;
	double cos_a;
	vector3D<double> A(x, y, z);
	A.Normalize();

	int e;
	glBegin(GL_LINES);
	for(i=m_filament_list.begin(); i!=m_filament_list.end(); i++)	//for each filament
	{
		//set the color of the filament based on orientation
		v_a = (*i)->vertex_a->position;
		v_b = (*i)->vertex_b->position;
		

		num_edges = (*i)->edges.size();
		for(e=0; e<num_edges; e++)
		{
			v_a = (*i)->edges[e]->vertex_a->position;
			v_b = (*i)->edges[e]->vertex_b->position;

			//determine color
			orientation = (v_a - v_b).Normalize();
			cos_a = fabs(orientation*A);
			glColor3f(r*cos_a, g*cos_a, b*cos_a);

			glVertex3f(v_a.x, v_a.y, v_a.z);
			glVertex3f(v_b.x, v_b.y, v_b.z);
		}
	}
	glEnd();
}

void rts_glFilamentNetwork::RenderEdgeDistanceColor(float r, float g, float b, float cutoff)
{
	vector<netEdge*>::iterator e;
	glBegin(GL_LINES);
	point3D<double> p;
	double d;
	double exp = 3;
	for(e=m_edge_list.begin(); e!=m_edge_list.end(); e++)
	{
		d = (*e)->distance;
		if(d<cutoff)
		{
			if(d<0.0) d=0.0;
			if(d>1.0) d=1.0;

			glColor3f(pow((1.0-d),exp)*r, pow((1.0-d),exp)*g, pow((1.0-d),exp)*b);
			p = (*e)->vertex_a->position;
			glVertex3f(p.x, p.y, p.z);
			p = (*e)->vertex_b->position;
			glVertex3f(p.x, p.y, p.z);
		}
	}
	glEnd();
}

void rts_glFilamentNetwork::RenderFilamentDistanceColor(float r, float g, float b, float cutoff)
{
	vector<netFilament*>::iterator f;
	unsigned int e;
	unsigned int num_edges;
	double d;
	point3D<double> v;
	double exp = 3.0;
	for(f = m_filament_list.begin(); f!=m_filament_list.end(); f++)
	{
		d = (*f)->distance;
		if(d<cutoff)
		{
			if(d>1.0) d=1.0;
			if(d<0.0) d=0.0;
			//cout<<"d: "<<d<<endl;
			num_edges = (*f)->edges.size();
			glColor3f(pow((1.0-d), exp)*r, pow((1.0-d), exp)*g, pow((1.0-d), exp)*b);
			glBegin(GL_LINES);
			for(e=0; e<num_edges; e++)
			{
				v = (*f)->edges[e]->vertex_a->position;
				glVertex3f(v.x, v.y, v.z);
				v = (*f)->edges[e]->vertex_b->position;
				glVertex3f(v.x, v.y, v.z);
			}
			glEnd();
		}
	}
		
}


void rts_glFilamentNetwork::RenderRadiusColor(float min_r, float min_g, float min_b,
								   float max_r, float max_g, float max_b,
								   float min_radius, float max_radius)
{
	/*Render each segment with a color based on the radius of the filament
	*/

	vector<netFilament*>::iterator i;
	int e;
	int num_edges;
	point3D<double> v;
	vector3D<float> color_min(min_r, min_g, min_b);
	vector3D<float> color_max(max_r, max_g, max_b);
	max_radius /= m_data_scale;
	min_radius /= m_data_scale;
	float diff = max_radius - min_radius;
	float p;						//parameter value (position between min and max radii)
	vector3D<float> color_p;

	for(i=m_filament_list.begin(); i!=m_filament_list.end(); i++)
	{
		glBegin(GL_LINES);
		num_edges = (*i)->edges.size();
		for(e=0; e<num_edges; e++)
		{
			//determine the color
			p = ((*i)->edges[e]->vertex_a->radius - min_radius)/diff;
			//cout<<"p: "<<p<<endl;
			color_p = (1.0 - p)*color_min + (p)*color_max;
			glColor3f(color_p.x, color_p.y, color_p.z);
			//determine the vertex position
			v = (*i)->edges[e]->vertex_a->position;
			glVertex3f(v.x, v.y, v.z);
			//determine the second color
			p = ((*i)->edges[e]->vertex_b->radius - min_radius)/diff;
			color_p = (1.0 - p)*color_min + (p)*color_max;
			glColor3f(color_p.x, color_p.y, color_p.z);
			//determine the vertex position
			v = (*i)->edges[e]->vertex_b->position;
			glVertex3f(v.x, v.y, v.z);
		}
		glEnd();
	}
}

void rts_glFilamentNetwork::RenderEdgeBoundingBoxes()
{
	glMatrixMode(GL_MODELVIEW);	//switch to the modelview matrix and store it

	vector<netEdge*>::iterator e;
	point3D<double> center;
	vector3D<double> size;
	for(e=m_edge_list.begin(); e!=m_edge_list.end(); e++)
	{
		glPushMatrix();
		size = (*e)->bounding_box.maximum - (*e)->bounding_box.minimum;
		center = (*e)->bounding_box.minimum + 0.5*(size);
		glTranslatef(center.x, center.y, center.z);
		glScalef(size.x,
				size.y,
				size.z);
		glutWireCube(1.0);
		glPopMatrix();
	}

}

void rts_glFilamentNetwork::RenderFilamentBoundingBoxes()
{
	glMatrixMode(GL_MODELVIEW);	//switch to the modelview matrix and store it

	vector<netFilament*>::iterator f;
	point3D<double> center;
	vector3D<double> size;
	for(f=m_filament_list.begin(); f!=m_filament_list.end(); f++)
	{
		glPushMatrix();
		size = (*f)->bounding_box.maximum - (*f)->bounding_box.minimum;
		center = (*f)->bounding_box.minimum + 0.5*size;
		glTranslatef(center.x, center.y, center.z);
		glScalef(size.x,
				size.y,
				size.z);
		glutWireCube(1.0);
		glPopMatrix();
	}
}

void rts_glFilamentNetwork::RenderCellSpheres()
{
	glMatrixMode(GL_MODELVIEW);

	vector<netCell*>::iterator c;
	for(c=m_cell_list.begin(); c!=m_cell_list.end(); c++)
	{
		glPushMatrix();
		glTranslatef((*c)->position.x, (*c)->position.y, (*c)->position.z);
		glutSolidSphere((*c)->radius, 10, 10);
		glPopMatrix();
	}
}
void rts_glFilamentNetwork::RenderSelectedCells()
{
	glMatrixMode(GL_MODELVIEW);

	vector<unsigned int>::iterator f;
	vector<netCell*>::iterator c;
	for(f = m_selected_list.begin(); f!=m_selected_list.end(); f++)
	{
		for(c=m_filament_list[(*f)]->cells.begin(); c!=m_filament_list[(*f)]->cells.end(); c++)
		{
			glPushMatrix();
			glTranslatef((*c)->position.x, (*c)->position.y, (*c)->position.z);
			glutSolidSphere((*c)->radius, 10, 10);
			glPopMatrix();
		}
	}
}

void rts_glFilamentNetwork::p_ProcessPickHits(GLuint num_hits, GLuint* buffer, 
											  unsigned int start_filament, bool append, int max)
{
	//cout<<"hits "<<start_filament<<" to "<<start_filament+63<<": "<<num_hits<<endl;
	/*This structure is kinda complicated.  Extract the selected filaments here and put the indices
	into the selection vector.
	*/
	//cout<<"selected filaments:"<<endl;
	if(num_hits > max)
		num_hits = max;
	unsigned int h, n;
	unsigned int num_names;
	unsigned int name;
	GLuint* ptr = buffer;			//index into buffer array
	for(h=0; h<num_hits; h++)	//for each hit
	{
		num_names = *ptr;	//get the number of names in the hit
		ptr+=3;				//skip to the names
		for(n=0; n<num_names; n++)	//this should really only execute once (since we don't push names)
		{
			name = start_filament + (*ptr);
			if(append)	//if we are appending
			{
				if(!p_IsSelected(name))
					m_selected_list.push_back(name);
			}
			else
				m_selected_list.push_back(name);
			//cout<<start_filament+(*ptr)<<endl;
			ptr++;
		}
	}



}
		
void rts_glFilamentNetwork::PickFilaments(int x_center, int y_center, int width, int height, bool append, int max)
{
	if(!append)
		m_selected_list.clear();			//first empty the selected list to start a new selection
	//set up the selection buffer
	GLuint selection_buffer[1000];
	GLuint hits;

	glSelectBuffer(1000, selection_buffer);
	glRenderMode(GL_SELECT);				//change to selection mode
	glInitNames();			//start naming objects
	glPushName(0);

	//set up projection matrix
	//note that the pick matrix has to be multiplied in first, so we have to store the projection
	//matrix and multiply it in afterwards
	glMatrixMode(GL_PROJECTION);
	GLfloat projection_matrix[16];
	glGetFloatv(GL_PROJECTION_MATRIX, projection_matrix);	//get the current projection matrix
	glPushMatrix();					//store the projection matrix
	glLoadIdentity();				//start from scratch
	GLint viewport[4];
	glGetIntegerv(GL_VIEWPORT, viewport);	//set up the pick matrix
	gluPickMatrix((GLdouble)x_center, (GLdouble)(viewport[3]-y_center), width, height, viewport);
	glMultMatrixf(projection_matrix);


	//run through each filament
	int num_filaments = m_filament_list.size();
	int num_edges, e, f;
	point3D<double> v_a, v_b;
	int name = 0;
	unsigned int total_hits = 0;
	for(f=0; f<num_filaments; f++)		//for each filament
	{
		glLoadName(name);		//push a name for the filament
		glBegin(GL_LINES);	//begin drawing the filament
		num_edges = m_filament_list[f]->edges.size();
		for(e = 0; e<num_edges; e++)
		{
			//draw each edge
			v_a = m_filament_list[f]->edges[e]->vertex_a->position;
			v_b = m_filament_list[f]->edges[e]->vertex_b->position;
			glVertex3f(v_a.x, v_a.y, v_a.z);
			glVertex3f(v_b.x, v_b.y, v_b.z);
		}
		glEnd();
		name++;				//increment the name
		//We have to make sure that the name count doesn't exceed 64
		if(name == 64)		//if the name hits 64, check for and store hits, then reset the name
		{
			hits = glRenderMode(GL_RENDER);
			p_ProcessPickHits(hits, selection_buffer, f-63, append, max - total_hits);	//process the hits (store selected fibers)
			total_hits += hits;
			if(total_hits >= max)		//if we've reached the max, return
			{
				glPopMatrix();
				return;
			}
			name = 0;		//reset the selection queue
			glRenderMode(GL_SELECT);				//change to selection mode
			glInitNames();			//start naming objects
			glPushName(0);			//insert the first name
		}
	}
	glFlush();		//finish rendering
	hits = glRenderMode(GL_RENDER);	//switch back to normal mode, get the number of hits
	p_ProcessPickHits(hits, selection_buffer, f/64, append, max - total_hits);	//get any left-over hits
	total_hits += hits;

	glPopMatrix();
}