Blame view

stim/visualization/gl_network.h 24.9 KB
9c97e126   David Mayerich   added an axis-ali...
1
2
3
  #ifndef STIM_GL_NETWORK
  #define STIM_GL_NETWORK
  
0012d210   David Mayerich   fixed compiler er...
4
  #include <GL/glut.h>
9c97e126   David Mayerich   added an axis-ali...
5
  #include <stim/biomodels/network.h>
b3a38641   David Mayerich   added the ability...
6
  #include <stim/visualization/aaboundingbox.h>
9c97e126   David Mayerich   added an axis-ali...
7
8
9
10
11
12
13
14
15
16
  
  namespace stim{
  
  template <typename T>
  class gl_network : public stim::network<T>{
  
  protected:
  	using stim::network<T>::E;
  	using stim::network<T>::V;
  
649a5e34   David Mayerich   implemented displ...
17
18
  	GLuint dlist;
  
9c97e126   David Mayerich   added an axis-ali...
19
20
21
  public:
  
  	/// Default constructor
649a5e34   David Mayerich   implemented displ...
22
23
24
  	gl_network() : stim::network<T>(){
  		dlist = 0;
  	}
9c97e126   David Mayerich   added an axis-ali...
25
26
  
  	/// Constructor creates a gl_network from a stim::network
649a5e34   David Mayerich   implemented displ...
27
28
29
  	gl_network(stim::network<T> N) : stim::network<T>(N){
  		dlist = 0;
  	}
9c97e126   David Mayerich   added an axis-ali...
30
31
32
  
  	/// Fills the parameters with the minimum and maximum spatial positions in the network,
  	///     specifying a bounding box for the network geometry
b3a38641   David Mayerich   added the ability...
33
  	aaboundingbox<T> boundingbox(){
9c97e126   David Mayerich   added an axis-ali...
34
  
b3a38641   David Mayerich   added the ability...
35
  		aaboundingbox<T> bb;								//create a bounding box
9c97e126   David Mayerich   added an axis-ali...
36
37
38
39
40
41
42
43
44
45
46
  
  		//loop through every edge
  		for(unsigned e = 0; e < E.size(); e++){
  			//loop through every point
  			for(unsigned p = 0; p < E[e].size(); p++)
  				bb.expand(E[e][p]);						//expand the bounding box to include the point
  		}
  
  		return bb;								//return the bounding box
  	}
  
6b317c71   Jiaming Guo   add function to r...
47
48
  	///render cylinder based on points from the top/bottom hat
  	///@param C1 set of points from one of the hat
0043e966   Jiaming Guo   fixed normal vect...
49
  	void renderCylinder(std::vector< stim::vec3<T> > C1, std::vector< stim::vec3<T> > C2, stim::vec3<T> P1, stim::vec3<T> P2) {
6b317c71   Jiaming Guo   add function to r...
50
  		glBegin(GL_QUAD_STRIP);
0043e966   Jiaming Guo   fixed normal vect...
51
52
53
54
55
56
  		for (unsigned i = 0; i < C1.size(); i++) {				// for every point on the circle
  			stim::vec3<T> n1 = C1[i] - P1;						// set normal vector for every vertex on the quads
  			stim::vec3<T> n2 = C2[i] - P2;
  			n1 = n1.norm();
  			n2 = n2.norm();
  			glNormal3f(n1[0], n1[1], n1[2]);
6b317c71   Jiaming Guo   add function to r...
57
  			glVertex3f(C1[i][0], C1[i][1], C1[i][2]);
0043e966   Jiaming Guo   fixed normal vect...
58
59
  			glNormal3f(n2[0], n2[1], n2[2]);
  			glVertex3f(C2[i][0], C2[i][1], C2[i][2]);											
6b317c71   Jiaming Guo   add function to r...
60
61
  		}	
  		glEnd();
b019bc5e   Jiaming Guo   add function to r...
62
63
  	}
  
2df16996   Jiaming Guo   add a new way to ...
64
  	///render the vertex as sphere based on glut build-in function
6b317c71   Jiaming Guo   add function to r...
65
66
67
  	///@param x, y, z are the three coordinates of the center point
  	///@param radius is the radius of the sphere
  	///@param subdivisions is the slice/stride along/around z-axis
b019bc5e   Jiaming Guo   add function to r...
68
69
70
71
72
73
74
  	void renderBall(T x, T y, T z, T radius, int subdivisions) {
  		glPushMatrix();
  		glTranslatef(x, y, z);
  		glutSolidSphere(radius, subdivisions, subdivisions);
  		glPopMatrix();
  	}
  
2df16996   Jiaming Guo   add a new way to ...
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  	///render the vertex as sphere based on transformation
  	///@param x, y, z are the three coordinates of the center point
  	///@param radius is the radius of the sphere
  	///@param slice is the number of subdivisions around the z-axis
  	///@param stack is the number of subdivisions along the z-axis
  	void renderBall(T x, T y, T z, T radius, T slice, T stack) {
  		T step_z = stim::PI / slice;					// step angle along z-axis
  		T step_xy = 2 * stim::PI / stack;				// step angle in xy-plane
  		T xx[4], yy[4], zz[4];							// store coordinates
  
  		T angle_z = 0.0;								// start angle
  		T angle_xy = 0.0;
  
  		glBegin(GL_QUADS);
c8864f86   Jiaming Guo   fixed light relevant
89
  		for (unsigned i = 0; i < slice; i++) {			// around the z-axis
2df16996   Jiaming Guo   add a new way to ...
90
91
  			angle_z = i * step_z;						// step step_z each time
  
c8864f86   Jiaming Guo   fixed light relevant
92
  			for (unsigned j = 0; j < stack; j++) {		// along the z-axis
2df16996   Jiaming Guo   add a new way to ...
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  				angle_xy = j * step_xy;					// step step_xy each time, draw floor by floor
  
  				xx[0] = radius * std::sin(angle_z) * std::cos(angle_xy);	// four vertices
  				yy[0] = radius * std::sin(angle_z) * std::sin(angle_xy);
  				zz[0] = radius * std::cos(angle_z);
  
  				xx[1] = radius * std::sin(angle_z + step_z) * std::cos(angle_xy);
  				yy[1] = radius * std::sin(angle_z + step_z) * std::sin(angle_xy);
  				zz[1] = radius * std::cos(angle_z + step_z);
  
  				xx[2] = radius * std::sin(angle_z + step_z) * std::cos(angle_xy + step_xy);
  				yy[2] = radius * std::sin(angle_z + step_z) * std::sin(angle_xy + step_xy);
  				zz[2] = radius * std::cos(angle_z + step_z);
  
  				xx[3] = radius * std::sin(angle_z) * std::cos(angle_xy + step_xy);
  				yy[3] = radius * std::sin(angle_z) * std::sin(angle_xy + step_xy);
  				zz[3] = radius * std::cos(angle_z);
  
c8864f86   Jiaming Guo   fixed light relevant
111
  				for (unsigned k = 0; k < 4; k++) {
2df16996   Jiaming Guo   add a new way to ...
112
113
114
115
116
117
118
  					glVertex3f(x + xx[k], y + yy[k], z + zz[k]);			// draw the floor plane
  				}
  			}
  		}
  		glEnd();
  	}
  
9c97e126   David Mayerich   added an axis-ali...
119
  	/// Render the network centerline as a series of line strips.
fdfdeda0   Jiaming Guo   fix minor mistakes
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  	/// glCenterline0 is for only one input
  	void glCenterline0(){
  		if (!glIsList(dlist)) {					//if dlist isn't a display list, create it
  			dlist = glGenLists(1);				//generate a display list
  			glNewList(dlist, GL_COMPILE);		//start a new display list
  			for (unsigned e = 0; e < E.size(); e++) {				//for each edge in the network
  				glBegin(GL_LINE_STRIP);
  				for (unsigned p = 0; p < E[e].size(); p++) {			//for each point on that edge
  					glVertex3f(E[e][p][0], E[e][p][1], E[e][p][2]);		//set the vertex position based on the current point
  					glTexCoord1f(0);									//set white color
  				}
  				glEnd();
  			}
  			glEndList();						//end the display list
  		}
  		glCallList(dlist);					// render the display list
  	}
b3a38641   David Mayerich   added the ability...
137
  
58674036   Jiaming Guo   fix minor error f...
138
139
  	///render the network centerline as a series of line strips(when loading at least two networks, otherwise using glCenterline0())
  	///colors are based on metric values
0d0ef1a9   David Mayerich   Adapted classes t...
140
  	void glCenterline(){
9c97e126   David Mayerich   added an axis-ali...
141
  
649a5e34   David Mayerich   implemented displ...
142
  		if(!glIsList(dlist)){					//if dlist isn't a display list, create it
b019bc5e   Jiaming Guo   add function to r...
143
  			dlist = glGenLists(1);				//generate a display list
649a5e34   David Mayerich   implemented displ...
144
145
  			glNewList(dlist, GL_COMPILE);		//start a new display list
  			for(unsigned e = 0; e < E.size(); e++){				//for each edge in the network
0d0ef1a9   David Mayerich   Adapted classes t...
146
  				//unsigned errormag_id = E[e].nmags() - 1;
649a5e34   David Mayerich   implemented displ...
147
  				glBegin(GL_LINE_STRIP);
b3a38641   David Mayerich   added the ability...
148
149
  				for(unsigned p = 0; p < E[e].size(); p++){				//for each point on that edge
  					glVertex3f(E[e][p][0], E[e][p][1], E[e][p][2]);		//set the vertex position based on the current point
1599ae65   Jiaming Guo   add swc.h and fun...
150
  					glTexCoord1f(E[e].r(p));							//set the texture coordinate based on the specified magnitude index
649a5e34   David Mayerich   implemented displ...
151
152
  				}
  				glEnd();
9c97e126   David Mayerich   added an axis-ali...
153
  			}
649a5e34   David Mayerich   implemented displ...
154
155
  			glEndList();						//end the display list
  		}		
6b317c71   Jiaming Guo   add function to r...
156
  		glCallList(dlist);						//render the display list
c72184d1   Jiaming Guo   add many function...
157
  	}
9c97e126   David Mayerich   added an axis-ali...
158
  
b7624d0d   Jiaming Guo   add function to h...
159
160
161
  	///render the network cylinder as a series of tubes(when only one network loaded) 
  	void glCylinder0() {
  
5068402b   Jiaming Guo   add function to a...
162
  		float r1, r2;
b7624d0d   Jiaming Guo   add function to h...
163
164
165
  		if (!glIsList(dlist)) {										// if dlist isn't a display list, create it
  			dlist = glGenLists(1);									// generate a display list
  			glNewList(dlist, GL_COMPILE);							// start a new display list
b7624d0d   Jiaming Guo   add function to h...
166
  			for (unsigned e = 0; e < E.size(); e++) {				// for each edge in the network
b7624d0d   Jiaming Guo   add function to h...
167
168
169
  				for (unsigned p = 1; p < E[e].size(); p++) {		// for each point on that edge
  					stim::circle<T> C1 = E[e].circ(p - 1);
  					stim::circle<T> C2 = E[e].circ(p);
5068402b   Jiaming Guo   add function to a...
170
  					r1 = E[e].r(p - 1);
4ea8daa8   Jiaming Guo   add necessary fun...
171
172
173
  					r2 = E[e].r(p);	
  					C1.set_R(r1);									// re-scale the circle to the same
  					C2.set_R(r2);
b7624d0d   Jiaming Guo   add function to h...
174
175
176
177
178
179
180
181
182
183
184
  					std::vector< stim::vec3<T> > Cp1 = C1.glpoints(20);// get 20 points on the circle plane
  					std::vector< stim::vec3<T> > Cp2 = C2.glpoints(20);
  					glBegin(GL_QUAD_STRIP);
  					for (unsigned i = 0; i < Cp1.size(); i++) {
  						glVertex3f(Cp1[i][0], Cp1[i][1], Cp1[i][2]);
  						glVertex3f(Cp2[i][0], Cp2[i][1], Cp2[i][2]);
  					}
  					glEnd();
  				}													// set the texture coordinate based on the specified magnitude index
  			}
  			for (unsigned n = 0; n < V.size(); n++) {
5068402b   Jiaming Guo   add function to a...
185
186
187
188
189
190
191
192
193
194
  				for (unsigned i = 0; i < E.size(); i++) {
  					if (E[i].v[0] == n) {
  						r1 = E[i].r(0);
  						break;
  					}
  					else if (E[i].v[1] == n) {
  						r1 = E[i].r(E[i].size() - 1);
  						break;
  					}
  				}
4ea8daa8   Jiaming Guo   add necessary fun...
195
  				renderBall(V[n][0], V[n][1], V[n][2], r1, 20);
b7624d0d   Jiaming Guo   add function to h...
196
197
198
199
200
201
  			}
  			glEndList();						// end the display list
  		}
  		glCallList(dlist);						// render the display list
  	}
  
58674036   Jiaming Guo   fix minor error f...
202
203
  	///render the network cylinder as a series of tubes
  	///colors are based on metric values 
46a9cc26   Jiaming Guo   fix minor errors ...
204
205
206
207
  	void glCylinder(float sigma, float radius) {
  
  		if (radius != sigma)					// if render radius was changed by user, create a new display list
  			glDeleteLists(dlist, 1);
77281320   David Mayerich   fixed cylinder te...
208
209
210
  		if (!glIsList(dlist)) {										// if dlist isn't a display list, create it
  			dlist = glGenLists(1);									// generate a display list
  			glNewList(dlist, GL_COMPILE);							// start a new display list
46a9cc26   Jiaming Guo   fix minor errors ...
211
  			for (unsigned e = 0; e < E.size(); e++) {				// for each edge in the network
58674036   Jiaming Guo   fix minor error f...
212
213
214
  				for (unsigned p = 1; p < E[e].size(); p++) {		// for each point on that edge
  					stim::circle<T> C1 = E[e].circ(p - 1);
  					stim::circle<T> C2 = E[e].circ(p);
c8864f86   Jiaming Guo   fixed light relevant
215
  					C1.set_R(2*radius);								// re-scale the circle to the same
46a9cc26   Jiaming Guo   fix minor errors ...
216
  					C2.set_R(2*radius);
42d38546   Jiaming Guo   fixed normal vect...
217
218
  					std::vector< stim::vec3<T> > Cp1 = C1.glpoints(20);// get 20 points on the circle plane
  					std::vector< stim::vec3<T> > Cp2 = C2.glpoints(20);
58674036   Jiaming Guo   fix minor error f...
219
  					glBegin(GL_QUAD_STRIP);
0043e966   Jiaming Guo   fixed normal vect...
220
221
222
223
224
  					for (unsigned i = 0; i < Cp1.size(); i++) {
  						stim::vec3<T> n1 = Cp1[i] - E[e][p - 1];	// set normal vector for every vertex on the quads
  						stim::vec3<T> n2 = Cp2[i] - E[e][p];
  						n1 = n1.norm();
  						n2 = n2.norm();
42d38546   Jiaming Guo   fixed normal vect...
225
  
0043e966   Jiaming Guo   fixed normal vect...
226
  						glNormal3f(n1[0], n1[1], n1[2]);
2b28db3e   Jiaming Guo   better render
227
  						glTexCoord1f(E[e].r(p - 1));
58674036   Jiaming Guo   fix minor error f...
228
  						glVertex3f(Cp1[i][0], Cp1[i][1], Cp1[i][2]);
0043e966   Jiaming Guo   fixed normal vect...
229
  						glNormal3f(n2[0], n2[1], n2[2]);
58674036   Jiaming Guo   fix minor error f...
230
  						glTexCoord1f(E[e].r(p));
77281320   David Mayerich   fixed cylinder te...
231
  						glVertex3f(Cp2[i][0], Cp2[i][1], Cp2[i][2]);
58674036   Jiaming Guo   fix minor error f...
232
  					}
58674036   Jiaming Guo   fix minor error f...
233
  					glEnd();
0043e966   Jiaming Guo   fixed normal vect...
234
  				}													// set the texture coordinate based on the specified magnitude index
58674036   Jiaming Guo   fix minor error f...
235
  			}
30b20d4f   Jiaming Guo   make it render be...
236
  			for (unsigned n = 0; n < V.size(); n++) {
46a9cc26   Jiaming Guo   fix minor errors ...
237
238
  				size_t num = V[n].e[0].size();					// store the number of outgoing edge of that vertex
  				if (num != 0) {									// if it has outgoing edge
c8864f86   Jiaming Guo   fixed light relevant
239
  					unsigned idx = V[n].e[0][0];				// find the index of first outgoing edge of that vertex
46a9cc26   Jiaming Guo   fix minor errors ...
240
  					glTexCoord1f(E[idx].r(0));					// bind the texture as metric of first point on that edge
30b20d4f   Jiaming Guo   make it render be...
241
242
  				}					
  				else {
46a9cc26   Jiaming Guo   fix minor errors ...
243
  					unsigned idx = V[n].e[1][0];				// find the index of first incoming edge of that vertex
46a9cc26   Jiaming Guo   fix minor errors ...
244
  					glTexCoord1f(E[idx].r(E[idx].size() - 1));	// bind the texture as metric of last point on that edge
30b20d4f   Jiaming Guo   make it render be...
245
  				}
0043e966   Jiaming Guo   fixed normal vect...
246
  				renderBall(V[n][0], V[n][1], V[n][2], 2*radius, 20);
30b20d4f   Jiaming Guo   make it render be...
247
  			}
46a9cc26   Jiaming Guo   fix minor errors ...
248
  			glEndList();						// end the display list
58674036   Jiaming Guo   fix minor error f...
249
  		}
46a9cc26   Jiaming Guo   fix minor errors ...
250
  		glCallList(dlist);						// render the display list
58674036   Jiaming Guo   fix minor error f...
251
252
  	}
  
2b28db3e   Jiaming Guo   better render
253
254
  	///render a T as a adjoint network of GT in transparancy(darkgreen, overlaid)
  	void glAdjointCylinder(float sigma, float radius) {
46a9cc26   Jiaming Guo   fix minor errors ...
255
  		
2b28db3e   Jiaming Guo   better render
256
  		if (radius != sigma)					// if render radius was changed by user, create a new display list
26e84a59   Jiaming Guo   add adjacency_mat...
257
258
259
  			glDeleteLists(dlist + 4, 1);
  		if (!glIsList(dlist + 4)) {
  			glNewList(dlist + 4, GL_COMPILE);
2b28db3e   Jiaming Guo   better render
260
261
262
263
264
265
266
267
268
269
270
271
  			for (unsigned e = 0; e < E.size(); e++) {				// for each edge in the network
  				for (unsigned p = 1; p < E[e].size(); p++) {		// for each point on that edge
  					stim::circle<T> C1 = E[e].circ(p - 1);
  					stim::circle<T> C2 = E[e].circ(p);
  					C1.set_R(2 * radius);							// scale the circle to the same
  					C2.set_R(2 * radius);
  					std::vector< stim::vec3<T> >Cp1 = C1.glpoints(20);
  					std::vector< stim::vec3<T> >Cp2 = C2.glpoints(20);
  					glBegin(GL_QUAD_STRIP);
  					for (unsigned i = 0; i < Cp1.size(); i++) {		// for every point on the circle(+1 means closing the circle)
  						glVertex3f(Cp1[i][0], Cp1[i][1], Cp1[i][2]);
  						glVertex3f(Cp2[i][0], Cp2[i][1], Cp2[i][2]);
58674036   Jiaming Guo   fix minor error f...
272
  					}
2b28db3e   Jiaming Guo   better render
273
274
  					glEnd();
  				}								// set the texture coordinate based on the specified magnitude index
58674036   Jiaming Guo   fix minor error f...
275
  			}
2b28db3e   Jiaming Guo   better render
276
277
278
279
  			for (unsigned n = 0; n < V.size(); n++) {
  				size_t num = V[n].e[0].size();					// store the number of outgoing edge of that vertex
  				if (num != 0) {									// if it has outgoing edge
  					unsigned idx = V[n].e[0][0];				// find the index of first outgoing edge of that vertex 
58674036   Jiaming Guo   fix minor error f...
280
  				}
2b28db3e   Jiaming Guo   better render
281
282
  				else {
  					unsigned idx = V[n].e[1][0];				// find the index of first incoming edge of that vertex
58674036   Jiaming Guo   fix minor error f...
283
  				}
0043e966   Jiaming Guo   fixed normal vect...
284
  				renderBall(V[n][0], V[n][1], V[n][2], 2 * radius, 20);
58674036   Jiaming Guo   fix minor error f...
285
286
287
  			}
  			glEndList();
  		}
26e84a59   Jiaming Guo   add adjacency_mat...
288
  		glCallList(dlist + 4);
58674036   Jiaming Guo   fix minor error f...
289
290
  	}
  
2b28db3e   Jiaming Guo   better render
291
292
  	///render the network cylinder as series of tubes
  	///@param I is a indicator: 0 -> GT, 1 -> T
6b317c71   Jiaming Guo   add function to r...
293
294
  	///@param map is the mapping relationship between two networks
  	///@param colormap is the random generated color set for render
2b28db3e   Jiaming Guo   better render
295
  	void glRandColorCylinder(int I, std::vector<unsigned> map, std::vector<T> colormap, float sigma, float radius) {
46a9cc26   Jiaming Guo   fix minor errors ...
296
  		
2b28db3e   Jiaming Guo   better render
297
  		if (radius != sigma)									// if render radius was changed by user, create a new display list
26e84a59   Jiaming Guo   add adjacency_mat...
298
299
300
  			glDeleteLists(dlist + 2, 1);
  		if (!glIsList(dlist + 2)) {								// if dlist isn't a display list, create it
  			glNewList(dlist + 2, GL_COMPILE);					// start a new display list
2b28db3e   Jiaming Guo   better render
301
  			for (unsigned e = 0; e < E.size(); e++) {			// for each edge in the network
58674036   Jiaming Guo   fix minor error f...
302
  				if (map[e] != unsigned(-1)) {
c8864f86   Jiaming Guo   fixed light relevant
303
  					if (I == 0) {								// if it is to render GT
2b28db3e   Jiaming Guo   better render
304
  						glColor3f(colormap[e * 3 + 0], colormap[e * 3 + 1], colormap[e * 3 + 2]);
c8864f86   Jiaming Guo   fixed light relevant
305
306
  					}									
  					else {										// if it is to render T
2b28db3e   Jiaming Guo   better render
307
  						glColor3f(colormap[map[e] * 3 + 0], colormap[map[e] * 3 + 1], colormap[map[e] * 3 + 2]);
c8864f86   Jiaming Guo   fixed light relevant
308
  					}
2b28db3e   Jiaming Guo   better render
309
310
  
  					for (unsigned p = 1; p < E[e].size(); p++) {// for each point on that edge
46a9cc26   Jiaming Guo   fix minor errors ...
311
312
  						stim::circle<T> C1 = E[e].circ(p - 1);
  						stim::circle<T> C2 = E[e].circ(p);
c8864f86   Jiaming Guo   fixed light relevant
313
  						C1.set_R(2*radius);						// re-scale the circle to the same
46a9cc26   Jiaming Guo   fix minor errors ...
314
  						C2.set_R(2*radius);
30b20d4f   Jiaming Guo   make it render be...
315
316
  						std::vector< stim::vec3<T> >Cp1 = C1.glpoints(20);
  						std::vector< stim::vec3<T> >Cp2 = C2.glpoints(20);
0043e966   Jiaming Guo   fixed normal vect...
317
  						renderCylinder(Cp1, Cp2, E[e][p - 1], E[e][p]);
58674036   Jiaming Guo   fix minor error f...
318
319
320
  					}
  				}
  				else {
26e84a59   Jiaming Guo   add adjacency_mat...
321
  					glColor3f(1.0, 1.0, 1.0);					// white color for the un-mapping edges
c8864f86   Jiaming Guo   fixed light relevant
322
  					for (unsigned p = 1; p < E[e].size(); p++) {// for each point on that edge
46a9cc26   Jiaming Guo   fix minor errors ...
323
324
325
326
  						stim::circle<T> C1 = E[e].circ(p - 1);
  						stim::circle<T> C2 = E[e].circ(p);
  						C1.set_R(2*radius);						// scale the circle to the same
  						C2.set_R(2*radius);
30b20d4f   Jiaming Guo   make it render be...
327
328
  						std::vector< stim::vec3<T> >Cp1 = C1.glpoints(20);
  						std::vector< stim::vec3<T> >Cp2 = C2.glpoints(20);
0043e966   Jiaming Guo   fixed normal vect...
329
  						renderCylinder(Cp1, Cp2, E[e][p - 1], E[e][p]);
58674036   Jiaming Guo   fix minor error f...
330
331
332
  					}
  				}
  			}
c8864f86   Jiaming Guo   fixed light relevant
333
334
  			for (unsigned n = 0; n < V.size(); n++) {
  				size_t num_edge = V[n].e[0].size() + V[n].e[1].size();
58674036   Jiaming Guo   fix minor error f...
335
  				if (num_edge > 1) {					// if it is the joint vertex
c8864f86   Jiaming Guo   fixed light relevant
336
  					glColor3f(0.3, 0.3, 0.3);		// gray
0043e966   Jiaming Guo   fixed normal vect...
337
  					renderBall(V[n][0], V[n][1], V[n][2], 3*radius, 20);
58674036   Jiaming Guo   fix minor error f...
338
339
  				}
  				else {								// if it is the terminal vertex
58674036   Jiaming Guo   fix minor error f...
340
  					glColor3f(0.6, 0.6, 0.6);		// more white gray
0043e966   Jiaming Guo   fixed normal vect...
341
  					renderBall(V[n][0], V[n][1], V[n][2], 3*radius, 20);
58674036   Jiaming Guo   fix minor error f...
342
343
344
  				}
  			}
  			glEndList();
58674036   Jiaming Guo   fix minor error f...
345
  		}
26e84a59   Jiaming Guo   add adjacency_mat...
346
  		glCallList(dlist + 2);
58674036   Jiaming Guo   fix minor error f...
347
348
  	}
  
2b28db3e   Jiaming Guo   better render
349
350
  	///render the network centerline as a series of line strips in random different color
  	///@param I is a indicator: 0 -> GT, 1 -> T
6b317c71   Jiaming Guo   add function to r...
351
352
  	///@param map is the mapping relationship between two networks
  	///@param colormap is the random generated color set for render
26e84a59   Jiaming Guo   add adjacency_mat...
353
354
355
  	void glRandColorCenterline(int I, std::vector<unsigned> map, std::vector<T> colormap) {
  		if (!glIsList(dlist + 2)) {
  			glNewList(dlist + 2, GL_COMPILE);
6b317c71   Jiaming Guo   add function to r...
356
  			for (unsigned e = 0; e < E.size(); e++) {
2b28db3e   Jiaming Guo   better render
357
358
359
360
361
  				if (map[e] != unsigned(-1)) {					// if it has corresponding edge in another network
  					if (I == 0)									// if it is to render GT
  						glColor3f(colormap[e * 3 + 0], colormap[e * 3 + 1], colormap[e * 3 + 2]);
  					else										// if it is to render T
  						glColor3f(colormap[map[e] * 3 + 0], colormap[map[e] * 3 + 1], colormap[map[e] * 3 + 2]);
c72184d1   Jiaming Guo   add many function...
362
  
c72184d1   Jiaming Guo   add many function...
363
  					glBegin(GL_LINE_STRIP);
6b317c71   Jiaming Guo   add function to r...
364
  					for (unsigned p = 0; p < E[e].size(); p++) {
c72184d1   Jiaming Guo   add many function...
365
366
367
  						glVertex3f(E[e][p][0], E[e][p][1], E[e][p][2]);
  					}
  					glEnd();
c72184d1   Jiaming Guo   add many function...
368
  				}
6b317c71   Jiaming Guo   add function to r...
369
  				else {
26e84a59   Jiaming Guo   add adjacency_mat...
370
  					glColor3f(1.0, 1.0, 1.0);					// white color for the un-mapping edges
c72184d1   Jiaming Guo   add many function...
371
  					glBegin(GL_LINE_STRIP);
6b317c71   Jiaming Guo   add function to r...
372
  					for (unsigned p = 0; p < E[e].size(); p++) {
c72184d1   Jiaming Guo   add many function...
373
374
375
376
377
  						glVertex3f(E[e][p][0], E[e][p][1], E[e][p][2]);
  					}
  					glEnd();
  				}
  			}
c72184d1   Jiaming Guo   add many function...
378
379
  			glEndList();
  		}
26e84a59   Jiaming Guo   add adjacency_mat...
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
  		glCallList(dlist + 2);
  	}
  
  	void glAdjointCenterline() {
  		if (!glIsList(dlist + 4)) {
  			glNewList(dlist + 4, GL_COMPILE);
  			for (unsigned e = 0; e < E.size(); e++) {				//for each edge in the network
  																
  				glBegin(GL_LINE_STRIP);
  				for (unsigned p = 0; p < E[e].size(); p++) {			//for each point on that edge
  					glVertex3f(E[e][p][0], E[e][p][1], E[e][p][2]);		//set the vertex position based on the current point
  					glTexCoord1f(E[e].r(p));							//set the texture coordinate based on the specified magnitude index
  				}
  				glEnd();
  			}
  			glEndList();
  		}
  		glCallList(dlist + 4);
  	}
  
  	// highlight the difference part
  	void glDifferenceCylinder(int I, std::vector<unsigned> map, std::vector<T> colormap, float sigma, float radius) {
  
  		if (radius != sigma)									// if render radius was changed by user, create a new display list
  			glDeleteLists(dlist + 6, 1);
  		if (!glIsList(dlist + 6)) {								// if dlist isn't a display list, create it
  			glNewList(dlist + 6, GL_COMPILE);					// start a new display list
  			for (unsigned e = 0; e < E.size(); e++) {			// for each edge in the network
  				if (map[e] != unsigned(-1)) {
  					glEnable(GL_BLEND);									//enable color blend
  					glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);	//set blend function
  					glDisable(GL_DEPTH_TEST);							//should disable depth
  
  					if (I == 0) {								// if it is to render GT
  						glColor4f(colormap[e * 3 + 0], colormap[e * 3 + 1], colormap[e * 3 + 2], 0.1);
  					}
  					else {										// if it is to render T
  						glColor4f(colormap[map[e] * 3 + 0], colormap[map[e] * 3 + 1], colormap[map[e] * 3 + 2], 0.1);
  					}
  
  					for (unsigned p = 1; p < E[e].size(); p++) {// for each point on that edge
  						stim::circle<T> C1 = E[e].circ(p - 1);
  						stim::circle<T> C2 = E[e].circ(p);
  						C1.set_R(2 * radius);						// re-scale the circle to the same
  						C2.set_R(2 * radius);
  						std::vector< stim::vec3<T> >Cp1 = C1.glpoints(20);
  						std::vector< stim::vec3<T> >Cp2 = C2.glpoints(20);
  						renderCylinder(Cp1, Cp2, E[e][p - 1], E[e][p]);
  					}
  					glDisable(GL_BLEND);
  					glEnable(GL_DEPTH_TEST);
  				}
  				else {
  					glColor3f(1.0, 1.0, 1.0);					// white color for the un-mapping edges
  					for (unsigned p = 1; p < E[e].size(); p++) {// for each point on that edge
  						stim::circle<T> C1 = E[e].circ(p - 1);
  						stim::circle<T> C2 = E[e].circ(p);
  						C1.set_R(2 * radius);						// scale the circle to the same
  						C2.set_R(2 * radius);
  						std::vector< stim::vec3<T> >Cp1 = C1.glpoints(20);
  						std::vector< stim::vec3<T> >Cp2 = C2.glpoints(20);
  						renderCylinder(Cp1, Cp2, E[e][p - 1], E[e][p]);
  					}
  				}
  			}
  			for (unsigned n = 0; n < V.size(); n++) {
  
  				size_t num_edge = V[n].e[0].size() + V[n].e[1].size();
  				if (num_edge > 1) {					// if it is the joint vertex
  					glColor4f(0.3, 0.3, 0.3, 0.1);		// gray
  					renderBall(V[n][0], V[n][1], V[n][2], 3 * radius, 20);
  				}
  				else {								// if it is the terminal vertex
  					glColor4f(0.6, 0.6, 0.6, 0.1);		// more white gray
  					renderBall(V[n][0], V[n][1], V[n][2], 3 * radius, 20);
  				}
  			}
  			glEndList();
  		}
  		glCallList(dlist + 6);
9c97e126   David Mayerich   added an axis-ali...
460
461
  	}
  
6b317c71   Jiaming Guo   add function to r...
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
  	//void glRandColorCenterlineGT(GLuint &dlist1, std::vector<unsigned> map, std::vector<T> colormap){
  	//	if(!glIsList(dlist1)){
  	//		dlist1 = glGenLists(1);
  	//		glNewList(dlist1, GL_COMPILE);
  	//		for(unsigned e = 0; e < E.size(); e++){
  	//			if(map[e] != unsigned(-1)){
  	//				glColor3f(colormap[e * 3 + 0], colormap[e * 3 + 1], colormap[e * 3 + 2]);
  	//				glBegin(GL_LINE_STRIP);
  	//				for(unsigned p = 0; p < E[e].size(); p++){
  	//					glVertex3f(E[e][p][0], E[e][p][1], E[e][p][2]);
  	//				}
  	//				glEnd();
  	//				for (unsigned p = 0; p < E[e].size() - 1; p++) {
  	//					renderCylinder(E[e][p][0], E[e][p][1], E[e][p][2], E[e][p + 1][0], E[e][p + 1][1], E[e][p + 1][2], 10, 20);
  	//				}
  	//			}
  	//			else{
  	//				glColor3f(1.0, 1.0, 1.0);
  	//				glBegin(GL_LINE_STRIP);
  	//				for(unsigned p = 0; p < E[e].size(); p++){
  	//					glVertex3f(E[e][p][0], E[e][p][1], E[e][p][2]);
  	//				}
  	//				glEnd();
  	//			}
  	//		}
  	//		for (unsigned v = 0; v < V.size(); v++) {
  	//			size_t num_edge = V[v].e[0].size() + V[v].e[1].size();
  	//			if (num_edge > 1) {
  	//				glColor3f(0.3, 0.3, 0.3);		// gray color for vertex
  	//				renderBall(V[v][0], V[v][1], V[v][2], 20, 20);
  	//			}
  	//		}
  	//		glEndList();
  	//	}
  	//	glCallList(dlist1);
  	//}
  
  	//void glRandColorCenterlineT(GLuint &dlist2, std::vector<unsigned> map, std::vector<T> colormap){
  	//	if(!glIsList(dlist2)){
  	//		dlist2 = glGenLists(1);
  	//		glNewList(dlist2, GL_COMPILE);
  	//		for(unsigned e = 0; e < E.size(); e++){
  	//			if(map[e] != unsigned(-1)){
  	//				glColor3f(colormap[map[e] * 3 + 0], colormap[map[e] * 3 + 1], colormap[map[e] * 3 + 2]);
  	//				glBegin(GL_LINE_STRIP);
  	//				for(unsigned p = 0; p < E[e].size(); p++){
  	//					glVertex3f(E[e][p][0], E[e][p][1], E[e][p][2]);
  	//				}
  	//				glEnd();
  	//				for (unsigned p = 0; p < E[e].size() - 1; p++) {
  	//					renderCylinder(E[e][p][0], E[e][p][1], E[e][p][2], E[e][p + 1][0], E[e][p + 1][1], E[e][p + 1][2], 10, 20);
  	//				}
  	//			}
  	//			else{
  	//				glColor3f(1.0, 1.0, 1.0);
  	//				glBegin(GL_LINE_STRIP);
  	//				for(unsigned p = 0; p < E[e].size(); p++){
  	//					glVertex3f(E[e][p][0], E[e][p][1], E[e][p][2]);
  	//				}
  	//				glEnd();
  	//			}
  	//		}
  	//		for (unsigned v = 0; v < V.size(); v++) {
  	//			size_t num_edge = V[v].e[0].size() + V[v].e[1].size();
  	//			if (num_edge > 1) {
  	//				glColor3f(0.3, 0.3, 0.3);		// gray color for vertex
  	//				renderBall(V[v][0], V[v][1], V[v][2], 20, 20);
  	//			}
  	//		}
  	//		glEndList();
  	//	}
  	//	glCallList(dlist2);
  	//}
  
58674036   Jiaming Guo   fix minor error f...
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
  
  	//void renderCylinder(T x1, T y1, T z1, T x2, T y2, T z2, T radius, int subdivisions) {
  	//	T dx = x2 - x1;
  	//	T dy = y2 - y1;
  	//	T dz = z2 - z1;
  	//	/// handle the degenerate case with an approximation
  	//	if (dz == 0)
  	//		dz = .00000001;
  	//	T d = sqrt(dx*dx + dy*dy + dz*dz);					
  	//	T ax = 57.2957795*acos(dz / d);						// 180°/pi
  	//	if (dz < 0.0)
  	//		ax = -ax;
  	//	T rx = -dy*dz;
  	//	T ry = dx*dz;
  
  	//	glPushMatrix();
  	//	glTranslatef(x1, y1, z1);
  	//	glRotatef(ax, rx, ry, 0.0);
  
  	//	glutSolidCylinder(radius, d, subdivisions, 1);
  	//	glPopMatrix();
  	//}
  
46a9cc26   Jiaming Guo   fix minor errors ...
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
  
  	/// render the network centerline from swc file as a series of strips in different colors based on the neuronal type
  	/// glCenterline0_swc is for only one input
  	/*void glCenterline0_swc() {
  	if (!glIsList(dlist)) {						// if dlist isn't a display list, create it
  	dlist = glGenLists(1);					// generate a display list
  	glNewList(dlist, GL_COMPILE);			// start a new display list
  	for (unsigned e = 0; e < E.size(); e++) {
  	int type = NT[e];					// get the neuronal type
  	switch (type) {
  	case 0:
  	glColor3f(1.0f, 1.0f, 1.0f);	// white for undefined
  	glBegin(GL_LINE_STRIP);
  	for (unsigned p = 0; p < E[e].size(); p++) {
  	glVertex3f(E[e][p][0], E[e][p][1], E[e][p][2]);
  	}
  	glEnd();
  	break;
  	case 1:
  	glColor3f(1.0f, 0.0f, 0.0f);	// red for soma
  	glBegin(GL_LINE_STRIP);
  	for (unsigned p = 0; p < E[e].size(); p++) {
  	glVertex3f(E[e][p][0], E[e][p][1], E[e][p][2]);
  	}
  	glEnd();
  	break;
  	case 2:
  	glColor3f(1.0f, 0.5f, 0.0f);	// orange for axon
  	glBegin(GL_LINE_STRIP);
  	for (unsigned p = 0; p < E[e].size(); p++) {
  	glVertex3f(E[e][p][0], E[e][p][1], E[e][p][2]);
  	}
  	glEnd();
  	break;
  	case 3:
  	glColor3f(1.0f, 1.0f, 0.0f);	// yellow for undefined
  	glBegin(GL_LINE_STRIP);
  	for (unsigned p = 0; p < E[e].size(); p++) {
  	glVertex3f(E[e][p][0], E[e][p][1], E[e][p][2]);
  	}
  	glEnd();
  	break;
  	case 4:
  	glColor3f(0.0f, 1.0f, 0.0f);	// green for undefined
  	glBegin(GL_LINE_STRIP);
  	for (unsigned p = 0; p < E[e].size(); p++) {
  	glVertex3f(E[e][p][0], E[e][p][1], E[e][p][2]);
  	}
  	glEnd();
  	break;
  	case 5:
  	glColor3f(0.0f, 1.0f, 1.0f);	// verdant for undefined
  	glBegin(GL_LINE_STRIP);
  	for (unsigned p = 0; p < E[e].size(); p++) {
  	glVertex3f(E[e][p][0], E[e][p][1], E[e][p][2]);
  	}
  	glEnd();
  	break;
  	case 6:
  	glColor3f(0.0f, 0.0f, 1.0f);	// blue for undefined
  	glBegin(GL_LINE_STRIP);
  	for (unsigned p = 0; p < E[e].size(); p++) {
  	glVertex3f(E[e][p][0], E[e][p][1], E[e][p][2]);
  	}
  	glEnd();
  	break;
  	case 7:
  	glColor3f(0.5f, 0.0f, 1.0f);	// purple for undefined
  	glBegin(GL_LINE_STRIP);
  	for (unsigned p = 0; p < E[e].size(); p++) {
  	glVertex3f(E[e][p][0], E[e][p][1], E[e][p][2]);
  	}
  	glEnd();
  	break;
  	}
  	}
  	glEndList();						//end the display list
  	}
  	glCallList(dlist);					// render the display list
  	}*/
  
  	///render the network cylinder as a series of tubes
  	///colors are based on metric values 
  	//void glCylinder(float sigma) {
  	//	if (!glIsList(dlist)) {					//if dlist isn't a display list, create it
  	//		dlist = glGenLists(1);				//generate a display list
  	//		glNewList(dlist, GL_COMPILE);		//start a new display list
  	//		for (unsigned e = 0; e < E.size(); e++) {				//for each edge in the network
  	//			for (unsigned p = 1; p < E[e].size() - 1; p++) {	// for each point on that edge
  	//				stim::circle<T> C1 = E[e].circ(p - 1);
  	//				stim::circle<T> C2 = E[e].circ(p);
  	//				C1.set_R(2.5*sigma);							// scale the circle to the same
  	//				C2.set_R(2.5*sigma);
  	//				std::vector< stim::vec3<T> >Cp1 = C1.glpoints(20);
  	//				std::vector< stim::vec3<T> >Cp2 = C2.glpoints(20);
  	//				glBegin(GL_QUAD_STRIP);
  	//				for (unsigned i = 0; i < Cp1.size(); i++) {		// for every point on the circle(+1 means closing the circle)
  	//					glVertex3f(Cp1[i][0], Cp1[i][1], Cp1[i][2]);
  	//					glVertex3f(Cp2[i][0], Cp2[i][1], Cp2[i][2]);
  	//					glTexCoord1f(E[e].r(p));
  	//				}
  	//				glEnd();
  	//			}								//set the texture coordinate based on the specified magnitude index
  	//		}
  	//		for (unsigned n = 0; n < V.size(); n++) {
  	//			size_t num = V[n].e[0].size();					//store the number of outgoing edge of that vertex
  	//			if (num != 0) {									//if it has outgoing edge
  	//				unsigned idx = V[n].e[0][0];				//find the index of first outgoing edge of that vertex 
  	//				glTexCoord1f(E[idx].r(0));					//bind the texture as metric of first point on that edge
  	//			}
  	//			else {
  	//				unsigned idx = V[n].e[1][0];				//find the index of first incoming edge of that vertex
  	//				glTexCoord1f(E[idx].r(E[idx].size() - 1));	//bind the texture as metric of last point on that edge
  	//			}
  	//			renderBall(V[n][0], V[n][1], V[n][2], 2.5*sigma, 20);
  	//		}
  	//		glEndList();						//end the display list
  	//	}
  	//	glCallList(dlist);						//render the display list
  	//}
  
9c97e126   David Mayerich   added an axis-ali...
680
681
682
683
684
685
  };		//end stim::gl_network class
  };		//end stim namespace
  
  
  
  #endif