Blame view

main.cu 39.3 KB
9191c39e   Jiaming Guo   first version of ...
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
  #include <stdlib.h>
  #include <string>
  #include <fstream>
  #include <algorithm>
  
  // CUDA include
  #ifdef __CUDACC__
  #include "device_launch_parameters.h"
  #include <cuda.h>
  #include <cuda_runtime_api.h>
  #include "cuda_runtime.h"
  #endif
  
  // OPENGL include
  #include <GL/glut.h>
  #include <GL/freeglut.h>
  
  #include "flow.h"
  
  // STIM include
  #include <stim/visualization/gl_aaboundingbox.h>
  #include <stim/parser/arguments.h>
  #include <stim/visualization/camera.h>
  #include <stim/visualization/colormap.h>
  #include <stim/cuda/cudatools/error.h>
b61addd8   Jiaming Guo   add adjustment fe...
26
  #include <stim/grids/image_stack.h>
9191c39e   Jiaming Guo   first version of ...
27
28
29
30
  
  
  //********************parameter setting********************
  // overall parameters
b61addd8   Jiaming Guo   add adjustment fe...
31
  std::string units;										// units used in this program
9191c39e   Jiaming Guo   first version of ...
32
33
34
35
36
37
38
39
40
  int vX, vY;
  float dx, dy, dz;										// x, y and z image scaling(units/pixel)
  std::string stackdir = "";								// directory where image stacks will be stored
  stim::arglist args;										// create an instance of arglist
  stim::gl_aaboundingbox<float> bb;						// axis-aligned bounding box object
  stim::camera cam;										// camera object
  unsigned num_edge;										// number of edges in the network
  unsigned num_vertex;									// number of vertex in the network
  std::vector<unsigned> pendant_vertex;					// list of pendant vertex index in GT
b61addd8   Jiaming Guo   add adjustment fe...
41
  std::vector<std::string> menu_option = { "simulation", "build inlet/outlet", "manufacture", "adjustment" };
9191c39e   Jiaming Guo   first version of ...
42
  stim::flow<float> flow;									// flow object
8f6c2057   Jiaming Guo   fixed index marki...
43
  stim::flow<float> backup;								// flow backup
9191c39e   Jiaming Guo   first version of ...
44
45
46
47
48
49
50
51
  float move_pace;										// camera moving parameter
  float u;												// viscosity
  float rou;												// density
  float max_v;
  float min_v;
  int mods;												// special keyboard input
  std::vector<unsigned char> color;						// velocity color map
  std::vector<int> velocity_bar;							// velocity bar
6ea69c93   Jiaming Guo   change bus size
52
  float length = 40.0f;									// cuboid length
ce1a6f5e   Jiaming Guo   add functions for...
53
  float scale = 1.0f;										// scale factor
b61addd8   Jiaming Guo   add adjustment fe...
54
55
56
57
58
59
60
  bool image_stack = false;								// flag indicates an image stack been loaded
  stim::image_stack<unsigned char, float> S;				// image stack
  float binary_threshold = 128;							// threshold for binary transformation
  float in = 0.0f;										// total input volume flow rate
  float out = 0.0f;
  float Rt = 0.0f;										// total resistance
  float Qn = 0.0f;										// required input total volume flow rate
ce1a6f5e   Jiaming Guo   add functions for...
61
62
  GLint dlist;											// simulation display list
  bool undo = false;										// delete display list
9191c39e   Jiaming Guo   first version of ...
63
64
65
66
67
68
69
70
  
  // hard-coded parameters
  float camera_factor = 1.2f;			// start point of the camera as a function of X and Y size
  float orbit_factor = 0.01f;			// degrees per pixel used to orbit the camera
  float zoom_factor = 10.0f;			// zooming factor
  float border_factor = 20.0f;		// border
  float radii_factor = 1.0f;			// radii changing factor
  GLint subdivision = 20;				// slices and stacks
9e60c6a7   Jiaming Guo   fixed minor bug i...
71
  float default_radius = 5.0f;		// default radii of network vertex
9191c39e   Jiaming Guo   first version of ...
72
73
74
  float delta = 0.01f;				// small discrepancy
  float eps = 20.0f;					// epsilon threshold
  float max_pressure = 0.0f;			// maximum pressure that the channel can bear
8334680a   Jiaming Guo   add square wave c...
75
76
  float height_threshold = 100.0f;	// connection height constraint
  float fragment_ratio = 0.0f;		// fragment ratio
9191c39e   Jiaming Guo   first version of ...
77
78
79
80
  
  // glut event parameters
  int mouse_x;						// window x-coordinate
  int mouse_y;						// window y-coordinate
f4105b89   Jiaming Guo   add new function:...
81
82
  int picked_x;						// picked window x-coordinate
  int picked_y;						// picked window y-coordinate
8f6c2057   Jiaming Guo   fixed index marki...
83
  bool LTbutton = false;				// true means down while false means up		
9191c39e   Jiaming Guo   first version of ...
84
85
  
  // simulation parameters
9e60c6a7   Jiaming Guo   fixed minor bug i...
86
  bool render_direction = false;		// flag indicates rendering flow direction for one edge
9191c39e   Jiaming Guo   first version of ...
87
88
89
  bool simulation = false;			// flag indicates simulation mode
  bool color_bound = false;			// flag indicates velocity color map bound
  bool to_select_pressure = false;	// flag indicates having selected a vertex to modify pressure
057ea93b   Jiaming Guo   add flow velocity...
90
  bool mark_index = true;				// flag indicates marking the index near the vertex
ce1a6f5e   Jiaming Guo   add functions for...
91
92
  bool glyph_mode = false;			// flag indicates rendering glyph for flow velocity field
  bool frame_mode = false;			// flag indicates rendering filament framing structrues
8f6c2057   Jiaming Guo   fixed index marki...
93
  bool subdivided = false;			// flag indicates subdivision status
9191c39e   Jiaming Guo   first version of ...
94
  unsigned pressure_index;			// the index of vertex that is clicked
6332d1e7   Jiaming Guo   fixed warnings
95
  unsigned direction_index = UINT_MAX;// the index of edge that is pointed at
8f6c2057   Jiaming Guo   fixed index marki...
96
  std::vector<stim::vec3<float> > back_vertex;	// vertex back up for marking indices
9191c39e   Jiaming Guo   first version of ...
97
98
99
100
  
  // build inlet/outlet parameters
  bool build_inlet_outlet = false;	// flag indicates building inlets and outlets
  bool modified_bridge = false;		// flag indicates having modified inlet/outlet connection
8334680a   Jiaming Guo   add square wave c...
101
102
  bool hilbert_curve = false;			// flag indicates enabling hilbert curves constructions
  bool change_fragment = false;		// flag indicates changing fragment for square wave connections
f4105b89   Jiaming Guo   add new function:...
103
104
105
106
  bool picked_connection = false;		// flag indicates picked one connection
  bool render_new_connection = false;	// flag indicates rendering new line connection in trasparency
  bool redisplay = false;				// flag indicates redisplay rendering
  bool connection_done = false;		// flag indicates finishing connections
ce1a6f5e   Jiaming Guo   add functions for...
107
  bool render_flow_rate = false;		// flag indicates rendering total volume flow rate
6332d1e7   Jiaming Guo   fixed warnings
108
  unsigned connection_index = UINT_MAX;// the index of connection that is picked
f4105b89   Jiaming Guo   add new function:...
109
110
111
  unsigned port_index = 0;			// inlet (0) or outlet (1)
  stim::vec3<float> tmp_v1, tmp_v2;	// temp vertex
  int coef;							// computational coefficient factor
9191c39e   Jiaming Guo   first version of ...
112
113
114
115
116
117
118
119
120
  
  // manufacture parameters
  bool manufacture = false;			// flag indicates manufacture mode
  
  
  //********************helper function*********************
  // get the network basic information
  inline void get_background() {
  
ce1a6f5e   Jiaming Guo   add functions for...
121
  	pendant_vertex = flow.get_pendant_vertex();
9191c39e   Jiaming Guo   first version of ...
122
123
124
125
126
  	num_edge = flow.edges();
  	num_vertex = flow.vertices();
  
  	// set the initial radii
  	flow.init(num_edge, num_vertex);			// initialize flow object
9e60c6a7   Jiaming Guo   fixed minor bug i...
127
128
129
130
131
  	
  	// if no radius information laoded
  	if (!flow.get_radius(0, 0))
  		for (unsigned i = 0; i < num_edge; i++)
  			flow.set_r(i, default_radius);
9191c39e   Jiaming Guo   first version of ...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
  }
  
  // convert from window coordinates to world coordinates
  inline void window_to_world(GLdouble &x, GLdouble &y, GLdouble &z) {
  
  	GLint    viewport[4];
  	GLdouble modelview[16];
  	GLdouble projection[16];
  	GLdouble winX, winY;
  	GLfloat  winZ;
  
  	glGetIntegerv(GL_VIEWPORT, viewport);
  	glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
  	glGetDoublev(GL_PROJECTION_MATRIX, projection);
  
  	winX = (GLdouble)mouse_x;
  	winY = viewport[3] - (GLdouble)mouse_y;
  	glReadPixels((GLint)winX, (GLint)winY, (GLsizei)1, (GLsizei)1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
  	gluUnProject(winX, winY, winZ, modelview, projection, viewport, &x, &y, &z);
  }
  
b61addd8   Jiaming Guo   add adjustment fe...
153
154
155
  // convert current image stack into a binary mask
  #ifdef __CUDACC__
  template <typename T, typename F>
6332d1e7   Jiaming Guo   fixed warnings
156
  __global__ void binary_transform(size_t N, T* ptr, F threshold) {
b61addd8   Jiaming Guo   add adjustment fe...
157
  
6332d1e7   Jiaming Guo   fixed warnings
158
  	size_t ix = blockDim.x * blockIdx.x + threadIdx.x;
b61addd8   Jiaming Guo   add adjustment fe...
159
160
161
  	if (ix >= N) return;					// avoid seg-fault
  
  	if (ptr[ix] >= threshold)				// binary transformation
b61addd8   Jiaming Guo   add adjustment fe...
162
  		ptr[ix] = 0;
ce1a6f5e   Jiaming Guo   add functions for...
163
164
  	else
  		ptr[ix] = 255;
b61addd8   Jiaming Guo   add adjustment fe...
165
166
167
  }
  #endif
  
9191c39e   Jiaming Guo   first version of ...
168
169
170
171
172
  
  //********************simulation function**********************
  // initialize flow object
  void flow_initialize() {
  
8334680a   Jiaming Guo   add square wave c...
173
  	flow.set = true;
9191c39e   Jiaming Guo   first version of ...
174
  	stim::vec3<float> center = bb.center();
8f6c2057   Jiaming Guo   fixed index marki...
175
176
  	flow.P.clear();
  	flow.P.resize(num_vertex, 0);									// clear up initialized pressure
9191c39e   Jiaming Guo   first version of ...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
  
  	for (unsigned i = 0; i < pendant_vertex.size(); i++) {
  		if (flow.get_vertex(pendant_vertex[i])[0] <= center[0])
  			flow.P[pendant_vertex[i]] = max_pressure - i * delta;	// should set minor discrepancy
  		else
  			flow.P[pendant_vertex[i]] = (i + 1) * delta;			// algorithm treat 0 as no initial pressure
  	}
  }
  
  // find the stable flow state
  void flow_stable_state() {
  	
  	flow.solve_flow(u);
  	flow.get_color_map(max_v, min_v, color, pendant_vertex);
  	color_bound = true;
  
  	velocity_bar.resize(num_edge);
  	for (unsigned i = 0; i < num_edge; i++)
  		velocity_bar[i] = i;
  	std::sort(velocity_bar.begin(), velocity_bar.end(), [&](int x, int y) {return abs(flow.v[x]) < abs(flow.v[y]); });
  }
  
b61addd8   Jiaming Guo   add adjustment fe...
199
200
201
202
203
204
205
206
207
208
  // adjustment on input volume flow rate and corresponding flow simulation
  void adjustment() {
  
  	system("CLS");							// clear up console box
  	std::cout << "Please enter the input total volume flow rate: " << std::endl;
  	std::cin >> Qn;
  
  	flow.adjust(in, out, Rt, Qn, u);
  }
  
9191c39e   Jiaming Guo   first version of ...
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
  
  //********************glut function********************
  // dynamically set menu
  // @param num: number of current menu options
  // @param range: range of option to be set from menu_option list
  void glut_set_menu(int num, int range) {
  
  	// remove last time menu options
  	for (int i = 1; i < num + 1; i++)
  		glutRemoveMenuItem(1);
  
  	// set new menu options
  	std::string menu_name;
  	for (int i = 1; i < range + 1; i++) {
  		menu_name = menu_option[i - 1];
  		glutAddMenuEntry(menu_name.c_str(), i);
  	}
  }
  
  // set up the squash transform to whole screen
  void glut_projection() {
  
  	glMatrixMode(GL_PROJECTION);					// load the projection matrix for editing
  	glLoadIdentity();								// start with the identity matrix
  	vX = glutGet(GLUT_WINDOW_WIDTH);				// use the whole screen for rendering
  	vY = glutGet(GLUT_WINDOW_HEIGHT);
  	glViewport(0, 0, vX, vY);						// specify a viewport for the entire window
  	float aspect = (float)vX / (float)vY;			// calculate the aspect ratio
  	gluPerspective(60, aspect, 0.1, 1000000);		// set up a perspective projection
  }
  
  // translate camera to origin
  void glut_modelview() {
  
  	glMatrixMode(GL_MODELVIEW);						// load the modelview matrix for editing
  	glLoadIdentity();								// start with the identity matrix
  	stim::vec3<float> eye = cam.getPosition();		// get the camera position (eye point)
  	stim::vec3<float> focus = cam.getLookAt();		// get the camera focal point
  	stim::vec3<float> up = cam.getUp();				// get the camera "up" orientation
  
  	gluLookAt(eye[0], eye[1], eye[2], focus[0], focus[1], focus[2], up[0], up[1], up[2]);	// set up the OpenGL camera
  }
  
  // glut render function
  void glut_render() {
  
  	glEnable(GL_DEPTH_TEST);
  	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
8334680a   Jiaming Guo   add square wave c...
257
  	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
9191c39e   Jiaming Guo   first version of ...
258
259
260
261
  	glut_projection();
  	glut_modelview();
  
  	if (!simulation && !build_inlet_outlet || manufacture) {
9e60c6a7   Jiaming Guo   fixed minor bug i...
262
  		glColor3f(0.0f, 0.0f, 0.0f);
ce1a6f5e   Jiaming Guo   add functions for...
263
  		flow.glCylinder0(scale, undo);
9191c39e   Jiaming Guo   first version of ...
264
  	}
ce1a6f5e   Jiaming Guo   add functions for...
265
266
267
268
269
270
271
272
273
274
275
276
277
278
  	else {	
  		flow.bounding_box();		// bounding box
  		if (num_vertex > 100) {						// if the network is big enough (say 100), use display list
  			if (undo) {								// undo rendering list
  				undo = false;
  				glDeleteLists(dlist, 1);
  			}							
  			if (!glIsList(dlist)) {
  				dlist = glGenLists(1);
  				glNewList(dlist, GL_COMPILE);
  				// render network
  				if (!glyph_mode) {
  					flow.glSolidSphere(max_pressure, subdivision, scale);
  					if (mark_index)
8f6c2057   Jiaming Guo   fixed index marki...
279
  						flow.mark_vertex(back_vertex, scale);
ce1a6f5e   Jiaming Guo   add functions for...
280
281
282
283
284
285
286
287
288
289
  					//flow.glSolidCone(subdivision);
  					flow.glSolidCylinder(direction_index, color, subdivision, scale);
  				}
  				// render glyphs
  				else
  					flow.glyph(color, subdivision, scale, frame_mode);
  				
  				glEndList();
  			}
  			glCallList(dlist);
057ea93b   Jiaming Guo   add flow velocity...
290
  		}
ce1a6f5e   Jiaming Guo   add functions for...
291
292
293
294
295
  		else {										// small network
  			// render network
  			if (!glyph_mode) {
  				flow.glSolidSphere(max_pressure, subdivision, scale);
  				if (mark_index) {
8f6c2057   Jiaming Guo   fixed index marki...
296
  					flow.mark_vertex(back_vertex, scale);
ce1a6f5e   Jiaming Guo   add functions for...
297
298
299
300
301
302
303
304
305
306
  					//flow.mark_edge();
  				}
  				//flow.glSolidCone(subdivision);
  				flow.glSolidCylinder(direction_index, color, subdivision, scale);
  			}
  			// render glyphs
  			else
  				flow.glyph(color, subdivision, scale, frame_mode);
  		}
  		flow.glSolidCuboid(subdivision, manufacture, length);		// render bus source
8f6c2057   Jiaming Guo   fixed index marki...
307
  		if (render_direction && !glyph_mode)						// render the flow direction of the vertex pointed
ce1a6f5e   Jiaming Guo   add functions for...
308
  			flow.glSolidCone(direction_index, subdivision, scale);
9191c39e   Jiaming Guo   first version of ...
309
310
  	}
  
0224d2ef   Jiaming Guo   fixed square wave...
311
  	if (build_inlet_outlet)
ce1a6f5e   Jiaming Guo   add functions for...
312
  		flow.line_bridge(redisplay);
0224d2ef   Jiaming Guo   fixed square wave...
313
  
9191c39e   Jiaming Guo   first version of ...
314
  	if (manufacture) {
ce1a6f5e   Jiaming Guo   add functions for...
315
316
  		flow.glSolidCuboid(subdivision, manufacture, length);
  		flow.tube_bridge(redisplay, subdivision, scale);
9191c39e   Jiaming Guo   first version of ...
317
  	}
f4105b89   Jiaming Guo   add new function:...
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
  
  	if (picked_connection && render_new_connection) {
  		glEnable(GL_BLEND);
  		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  		glColor4f(0.0f, 0.0f, 0.0f, 0.4f);
  		glBegin(GL_LINE_STRIP);
  		if (!port_index) {
  			glVertex3f(flow.inlet[connection_index].V[1][0], flow.inlet[connection_index].V[1][1], flow.inlet[connection_index].V[1][2]);
  			glVertex3f(tmp_v1[0], tmp_v1[1], tmp_v1[2]);
  			glVertex3f(tmp_v2[0], tmp_v2[1], tmp_v2[2]);
  			glVertex3f(flow.inlet[connection_index].V[2][0], flow.inlet[connection_index].V[2][1], flow.inlet[connection_index].V[2][2]);
  		}
  		else {
  			glVertex3f(flow.outlet[connection_index].V[1][0], flow.outlet[connection_index].V[1][1], flow.outlet[connection_index].V[1][2]);
  			glVertex3f(tmp_v1[0], tmp_v1[1], tmp_v1[2]);
  			glVertex3f(tmp_v2[0], tmp_v2[1], tmp_v2[2]);
  			glVertex3f(flow.outlet[connection_index].V[2][0], flow.outlet[connection_index].V[2][1], flow.outlet[connection_index].V[2][2]);
  		}
  		glEnd();
  		glFlush();
  		glDisable(GL_BLEND);
  	}
9191c39e   Jiaming Guo   first version of ...
340
  	
9191c39e   Jiaming Guo   first version of ...
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
  	// render bars
  	// bring up a pressure bar on left
  	if (to_select_pressure) {
  		
  		glMatrixMode(GL_PROJECTION);									// set up the 2d viewport for mode text printing
  		glPushMatrix();
  		glLoadIdentity();
  		vX = glutGet(GLUT_WINDOW_WIDTH);								// get the current window width
  		vY = glutGet(GLUT_WINDOW_HEIGHT);								// get the current window height
  		glViewport(0, 0, vX, vY);										// locate to left bottom corner
  		gluOrtho2D(0, vX, 0, vY);										// define othogonal aspect
  
  		glMatrixMode(GL_MODELVIEW);
  		glPushMatrix();
  		glLoadIdentity();
  
  		glLineWidth(border_factor);
  		glBegin(GL_LINES);
  		glColor3f(0.0, 0.0, 1.0);										// blue to red
  		glVertex2f(border_factor, border_factor);
  		glColor3f(1.0, 0.0, 0.0);
  		glVertex2f(border_factor, (vY - 2.0f * border_factor));
  		glEnd();
  		glFlush();
  
  		// pressure bar text
8334680a   Jiaming Guo   add square wave c...
367
  		glColor3f(0.0f, 0.0f, 0.0f);
9191c39e   Jiaming Guo   first version of ...
368
369
370
371
372
373
374
375
376
377
378
379
  		glRasterPos2f(0.0f, vY - border_factor);
  		std::stringstream ss_p;
  		ss_p << "Pressure Bar";
  		glutBitmapString(GLUT_BITMAP_HELVETICA_18, (const unsigned char*)(ss_p.str().c_str()));
  
  		// pressure range text
  		float step = vY - 3.0f * border_factor;
  		step /= 10;
  		for (unsigned i = 0; i < 11; i++) {
  			glRasterPos2f((border_factor * 1.5f), (border_factor + i * step));
  			std::stringstream ss_n;
  			ss_n << (float)i * max_pressure / 10;
8334680a   Jiaming Guo   add square wave c...
380
  			glutBitmapString(GLUT_BITMAP_HELVETICA_18, (const unsigned char*)(ss_n.str().c_str()));
9191c39e   Jiaming Guo   first version of ...
381
382
383
384
385
386
387
  		}
  		glPopMatrix();
  		glMatrixMode(GL_PROJECTION);
  		glPopMatrix();
  	}
  
  	// bring up a velocity bar on left
b94dd1e0   Jiaming Guo   add render functi...
388
  	if ((simulation || build_inlet_outlet) && !to_select_pressure && !change_fragment) {
9191c39e   Jiaming Guo   first version of ...
389
390
391
392
393
394
395
396
397
398
399
400
401
402
  		
  		glMatrixMode(GL_PROJECTION);									// set up the 2d viewport for mode text printing
  		glPushMatrix();
  		glLoadIdentity();
  		vX = glutGet(GLUT_WINDOW_WIDTH);								// get the current window width
  		vY = glutGet(GLUT_WINDOW_HEIGHT);								// get the current window height
  		glViewport(0, 0, vX, vY);										// locate to left bottom corner
  		gluOrtho2D(0, vX, 0, vY);										// define othogonal aspect
  
  		glMatrixMode(GL_MODELVIEW);
  		glPushMatrix();
  		glLoadIdentity();
  
  		float step = (vY - 3 * border_factor);
f4105b89   Jiaming Guo   add new function:...
403
404
  		step /= BREWER_CTRL_PTS - 1;
  		for (unsigned i = 0; i < BREWER_CTRL_PTS - 1; i++) {
9191c39e   Jiaming Guo   first version of ...
405
406
  			glLineWidth(border_factor);
  			glBegin(GL_LINES);
f4105b89   Jiaming Guo   add new function:...
407
  			glColor3f(BREWERCP[i * 4 + 0], BREWERCP[i * 4 + 1], BREWERCP[i * 4 + 2]);
9191c39e   Jiaming Guo   first version of ...
408
  			glVertex2f(border_factor, border_factor + i * step);
f4105b89   Jiaming Guo   add new function:...
409
  			glColor3f(BREWERCP[(i + 1) * 4 + 0], BREWERCP[(i + 1) * 4 + 1], BREWERCP[(i + 1) * 4 + 2]);
9191c39e   Jiaming Guo   first version of ...
410
411
412
413
414
415
  			glVertex2f(border_factor, border_factor + (i + 1) * step);
  			glEnd();
  		}
  		glFlush();
  
  		// pressure bar text
8334680a   Jiaming Guo   add square wave c...
416
  		glColor3f(0.0f, 0.0f, 0.0f);
9191c39e   Jiaming Guo   first version of ...
417
418
419
420
421
422
423
424
425
426
427
428
  		glRasterPos2f(0.0f, vY - border_factor);
  		std::stringstream ss_p;
  		ss_p << "Velocity range";
  		glutBitmapString(GLUT_BITMAP_HELVETICA_18, (const unsigned char*)(ss_p.str().c_str()));
  
  		// pressure range text
  		step = vY - 3 * border_factor;
  		step /= 10;
  		for (unsigned i = 0; i < 11; i++) {
  			glRasterPos2f(border_factor * 1.5f, border_factor + i * step);
  			std::stringstream ss_n;
  			ss_n << min_v + i * (max_v - min_v) / 10;
8334680a   Jiaming Guo   add square wave c...
429
  			glutBitmapString(GLUT_BITMAP_HELVETICA_18, (const unsigned char*)(ss_n.str().c_str()));
9191c39e   Jiaming Guo   first version of ...
430
431
432
433
434
  		}
  		glPopMatrix();
  		glMatrixMode(GL_PROJECTION);
  		glPopMatrix();
  	}
8334680a   Jiaming Guo   add square wave c...
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
  
  	// bring up a ratio bar on the left
  	if (change_fragment) {
  		
  		glMatrixMode(GL_PROJECTION);									// set up the 2d viewport for mode text printing
  		glPushMatrix();
  		glLoadIdentity();
  		vX = glutGet(GLUT_WINDOW_WIDTH);								// get the current window width
  		vY = glutGet(GLUT_WINDOW_HEIGHT);								// get the current window height
  		glViewport(0, 0, vX, vY);										// locate to left bottom corner
  		gluOrtho2D(0, vX, 0, vY);										// define othogonal aspect
  
  		glMatrixMode(GL_MODELVIEW);
  		glPushMatrix();
  		glLoadIdentity();
  
  		glLineWidth(border_factor);
  		glBegin(GL_LINES);
  		glColor3f(0.0, 0.0, 1.0);										// blue to red
  		glVertex2f(border_factor, border_factor);
  		glColor3f(1.0, 0.0, 0.0);
  		glVertex2f(border_factor, (vY - 2.0f * border_factor));
  		glEnd();
  		glFlush();
  
ce1a6f5e   Jiaming Guo   add functions for...
460
  		// ratio bar text
8334680a   Jiaming Guo   add square wave c...
461
462
463
464
465
466
  		glColor3f(0.0f, 0.0f, 0.0f);
  		glRasterPos2f(0.0f, vY - border_factor);
  		std::stringstream ss_p;
  		ss_p << "Ratio bar";
  		glutBitmapString(GLUT_BITMAP_HELVETICA_18, (const unsigned char*)(ss_p.str().c_str()));
  
ce1a6f5e   Jiaming Guo   add functions for...
467
  		// ratio range text
8334680a   Jiaming Guo   add square wave c...
468
469
470
471
472
473
474
475
476
477
478
479
480
  		float step = vY - 3.0f * border_factor;
  		step /= 10;
  		for (unsigned i = 0; i < 11; i++) {
  			glRasterPos2f((border_factor * 1.5f), (border_factor + i * step));
  			std::stringstream ss_n;
  			ss_n << (float)i * 1.0f / 10;
  			glutBitmapString(GLUT_BITMAP_HELVETICA_18, (const unsigned char*)(ss_n.str().c_str()));
  		}
  		glPopMatrix();
  		glMatrixMode(GL_PROJECTION);
  		glPopMatrix();
  	}
  
b61addd8   Jiaming Guo   add adjustment fe...
481
  	if (build_inlet_outlet)
ce1a6f5e   Jiaming Guo   add functions for...
482
483
  		if (render_flow_rate)
  			flow.display_flow_rate(in, out);
b61addd8   Jiaming Guo   add adjustment fe...
484
  
9191c39e   Jiaming Guo   first version of ...
485
486
487
488
489
490
491
492
493
494
  	glutSwapBuffers();
  }
  
  // register glut menu options
  void glut_menu(int value) {
  
  	int num = glutGet(GLUT_MENU_NUM_ITEMS);
  	if (value == 1) {
  		simulation = true;
  		build_inlet_outlet = false;
ce1a6f5e   Jiaming Guo   add functions for...
495
  		render_flow_rate = false;
9191c39e   Jiaming Guo   first version of ...
496
497
  		manufacture = false;
  		modified_bridge = false;
ce1a6f5e   Jiaming Guo   add functions for...
498
  		change_fragment = false;
f4105b89   Jiaming Guo   add new function:...
499
  		connection_done = false;
6ea69c93   Jiaming Guo   change bus size
500
  		// first time
b61addd8   Jiaming Guo   add adjustment fe...
501
  		if (!flow.set) {						// only first time simulation called "simulation", ^_^
ce1a6f5e   Jiaming Guo   add functions for...
502
  			get_background();					// get the graph information
8f6c2057   Jiaming Guo   fixed index marki...
503
  			back_vertex = flow.back_vertex();	// vertex back up for marking indices
ce1a6f5e   Jiaming Guo   add functions for...
504
  			flow_initialize();					// initialize flow condition
6ea69c93   Jiaming Guo   change bus size
505
506
507
508
  			menu_option[0] = "resimulation";
  		}
  
  		// simulation / resimulation
9191c39e   Jiaming Guo   first version of ...
509
510
511
  		flow_stable_state();					// main function of solving the linear system
  		flow.print_flow();
  		
ce1a6f5e   Jiaming Guo   add functions for...
512
  		if (!glyph_mode)
0224d2ef   Jiaming Guo   fixed square wave...
513
  			glut_set_menu(num, 2);
9191c39e   Jiaming Guo   first version of ...
514
515
516
517
518
519
  	}
  
  	if (value == 2) {
  		simulation = false;
  		build_inlet_outlet = true;
  		manufacture = false;
f4105b89   Jiaming Guo   add new function:...
520
  		if (!modified_bridge && !connection_done) {
9191c39e   Jiaming Guo   first version of ...
521
  			flow.set_main_feeder();
8334680a   Jiaming Guo   add square wave c...
522
  			flow.build_synthetic_connection(u, default_radius);
f4105b89   Jiaming Guo   add new function:...
523
524
525
526
527
528
529
  			flow.check_direct_connection();	// check whether direct connections intersect each other
  			connection_done = true;
  		}
  		else if (modified_bridge) {
  			modified_bridge = false;
  			redisplay = true;
  			flow.clear_synthetic_connection();
9191c39e   Jiaming Guo   first version of ...
530
531
  		}
  
b61addd8   Jiaming Guo   add adjustment fe...
532
  		glut_set_menu(num, 4);
9191c39e   Jiaming Guo   first version of ...
533
534
535
536
537
538
  	}
  
  	if (value == 3) {
  		simulation = false;
  		build_inlet_outlet = false;
  		manufacture = true;
ce1a6f5e   Jiaming Guo   add functions for...
539
  		glyph_mode = false;			// manufacuture mode doesn't need flow direction
15fc312f   Jiaming Guo   fixed modes conflict
540
  		redisplay = true;
9191c39e   Jiaming Guo   first version of ...
541
542
  	}
  
b61addd8   Jiaming Guo   add adjustment fe...
543
544
545
  	if (value == 4) {
  		simulation = true;
  		build_inlet_outlet = false;
ce1a6f5e   Jiaming Guo   add functions for...
546
  		render_flow_rate = false;
b61addd8   Jiaming Guo   add adjustment fe...
547
548
549
550
551
552
553
  		manufacture = false;
  		
  		adjustment();					// adjust network flow accordingly
  
  		glut_set_menu(num, 1);
  	}
  
9191c39e   Jiaming Guo   first version of ...
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
  	glutPostRedisplay();
  }
  
  // defines camera motion based on mouse dragging
  void glut_motion(int x, int y) {
  
  	mods = glutGetModifiers();
  	if (LTbutton && mods == 0) {
  
  		float theta = orbit_factor * (mouse_x - x);		// determine the number of degrees along the x-axis to rotate
  		float phi = orbit_factor * (y - mouse_y);		// number of degrees along the y-axis to rotate
  
  		cam.OrbitFocus(theta, phi);						// rotate the camera around the focal point
  	}
  	mouse_x = x;										// update the mouse position
  	mouse_y = y;
  
  	glutPostRedisplay();								// re-draw the visualization
  }
  
9e60c6a7   Jiaming Guo   fixed minor bug i...
574
575
576
  // defines passive mouse motion function
  void glut_passive_motion(int x, int y) {
  
f4105b89   Jiaming Guo   add new function:...
577
  	mods = glutGetModifiers();
9e60c6a7   Jiaming Guo   fixed minor bug i...
578
579
580
581
582
  
  	// check whether the mouse point near to an edge
  	GLdouble posX, posY, posZ;
  	window_to_world(posX, posY, posZ);			// get the world coordinates
  
f4105b89   Jiaming Guo   add new function:...
583
  	if (simulation || build_inlet_outlet && !mods) {
9e60c6a7   Jiaming Guo   fixed minor bug i...
584
  		bool flag = flow.epsilon_edge((float)posX, (float)posY, (float)posZ, eps, direction_index);
ce1a6f5e   Jiaming Guo   add functions for...
585
  		if (flag && !glyph_mode)
9e60c6a7   Jiaming Guo   fixed minor bug i...
586
  			render_direction = true;
ce1a6f5e   Jiaming Guo   add functions for...
587
  		else if (!flag && !glyph_mode) {
9e60c6a7   Jiaming Guo   fixed minor bug i...
588
  			if (render_direction)				// if the direction is displaying currently, do a short delay
f4105b89   Jiaming Guo   add new function:...
589
  				Sleep(300);
9e60c6a7   Jiaming Guo   fixed minor bug i...
590
591
592
  			render_direction = false;
  			direction_index = -1;
  		}
8f6c2057   Jiaming Guo   fixed index marki...
593
  		undo = true;
9e60c6a7   Jiaming Guo   fixed minor bug i...
594
595
  	}
  
f4105b89   Jiaming Guo   add new function:...
596
597
  	if (mods == GLUT_ACTIVE_SHIFT && picked_connection) {
  		render_new_connection = true;
6332d1e7   Jiaming Guo   fixed warnings
598
  		size_t i;
f4105b89   Jiaming Guo   add new function:...
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
  		if (!port_index) {
  			tmp_v1 = stim::vec3<float>(flow.inlet[connection_index].V[1][0], flow.inlet[connection_index].V[1][1] + (float)(picked_y - y), flow.inlet[connection_index].V[1][2]);
  			tmp_v2 = stim::vec3<float>(flow.inlet[connection_index].V[2][0], flow.inlet[connection_index].V[2][1] + (float)(picked_y - y), flow.inlet[connection_index].V[2][2]);
  			i = flow.inlet[connection_index].V.size();
  			if (coef * tmp_v1[1] < coef * flow.inlet[connection_index].V[i - 1][1]) {
  				tmp_v1[1] = flow.inlet[connection_index].V[i - 1][1];
  				tmp_v2[1] = flow.inlet[connection_index].V[i - 1][1];
  			}
  		}
  		else {
  			tmp_v1 = stim::vec3<float>(flow.outlet[connection_index].V[1][0], flow.outlet[connection_index].V[1][1] + (float)(picked_y - y), flow.outlet[connection_index].V[1][2]);
  			tmp_v2 = stim::vec3<float>(flow.outlet[connection_index].V[2][0], flow.outlet[connection_index].V[2][1] + (float)(picked_y - y), flow.outlet[connection_index].V[2][2]);
  			i = flow.outlet[connection_index].V.size();
  			if (coef * tmp_v1[1] < coef * flow.outlet[connection_index].V[i - 1][1]) {
  				tmp_v1[1] = flow.outlet[connection_index].V[i - 1][1];
  				tmp_v2[1] = flow.outlet[connection_index].V[i - 1][1];
  			}
  		}	
  	}
  	else if (mods == GLUT_ACTIVE_CTRL && picked_connection) {
  		render_new_connection = true;
  		if (!port_index) {
  			tmp_v1 = stim::vec3<float>(flow.inlet[connection_index].V[0][0] + (float)(x - picked_x), flow.inlet[connection_index].V[0][1], flow.inlet[connection_index].V[0][2]);
  			tmp_v2 = stim::vec3<float>(flow.inlet[connection_index].V[1][0] + (float)(x - picked_x), flow.inlet[connection_index].V[1][1], flow.inlet[connection_index].V[1][2]);
  			if (tmp_v1[0] < flow.main_feeder[port_index][0] - length / 2) {
  				tmp_v1[0] = flow.main_feeder[port_index][0] - length / 2;
  				tmp_v2[0] = flow.main_feeder[port_index][0] - length / 2;
  			}
  			else if (tmp_v1[0] > flow.main_feeder[port_index][0] + length / 2) {
  				tmp_v1[0] = flow.main_feeder[port_index][0] + length / 2;
  				tmp_v2[0] = flow.main_feeder[port_index][0] + length / 2;
  			}
  		}
  		else {
  			tmp_v1 = stim::vec3<float>(flow.outlet[connection_index].V[0][0] + (float)(x - picked_x), flow.outlet[connection_index].V[0][1], flow.outlet[connection_index].V[0][2]);
  			tmp_v2 = stim::vec3<float>(flow.outlet[connection_index].V[1][0] + (float)(x - picked_x), flow.outlet[connection_index].V[1][1], flow.outlet[connection_index].V[1][2]);
  			if (tmp_v1[0] > flow.main_feeder[port_index][0] + length / 2) {
  				tmp_v1[0] = flow.main_feeder[port_index][0] + length / 2;
  				tmp_v2[0] = flow.main_feeder[port_index][0] + length / 2;
  			}
  			else if (tmp_v1[0] < flow.main_feeder[port_index][0] - length / 2) {
  				tmp_v1[0] = flow.main_feeder[port_index][0] - length / 2;
  				tmp_v2[0] = flow.main_feeder[port_index][0] - length / 2;
  			}
  		}
  	}
  	else
  		render_new_connection = false;
  
  	mouse_x = x;
  	mouse_y = y;
  
  	glutPostRedisplay();							// re-draw the visualization
9e60c6a7   Jiaming Guo   fixed minor bug i...
652
653
  }
  
9191c39e   Jiaming Guo   first version of ...
654
655
656
  // get click window coordinates
  void glut_mouse(int button, int state, int x, int y) {
  
f4105b89   Jiaming Guo   add new function:...
657
658
  	mods = glutGetModifiers();						// get special keyboard input
  
9191c39e   Jiaming Guo   first version of ...
659
660
  	mouse_x = x;
  	mouse_y = y;
f4105b89   Jiaming Guo   add new function:...
661
662
663
664
  	if (!mods) {
  		picked_connection = false;
  		render_new_connection = false;
  	}
9191c39e   Jiaming Guo   first version of ...
665
666
667
668
669
  	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
  		LTbutton = true;
  	else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
  		LTbutton = false;
  
f4105b89   Jiaming Guo   add new function:...
670
  	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && !mods && simulation && !to_select_pressure) {
9191c39e   Jiaming Guo   first version of ...
671
672
673
  		GLdouble posX, posY, posZ;
  		window_to_world(posX, posY, posZ);			// get the world coordinates
  
6ea69c93   Jiaming Guo   change bus size
674
  		bool flag = flow.epsilon_vertex((float)posX, (float)posY, (float)posZ, eps, scale, pressure_index);
9191c39e   Jiaming Guo   first version of ...
675
676
677
678
679
680
  		if (flag) {
  			std::vector<unsigned>::iterator it = std::find(pendant_vertex.begin(), pendant_vertex.end(), pressure_index);
  			if (it != pendant_vertex.end()) 		// if it is dangle vertex
  				to_select_pressure = true;
  		}
  	}
f4105b89   Jiaming Guo   add new function:...
681
  	else if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && !mods && simulation && to_select_pressure) {
b61addd8   Jiaming Guo   add adjustment fe...
682
  		if (y >= 2 * border_factor && y <= vY - border_factor) {	// within the pressure bar range
9191c39e   Jiaming Guo   first version of ...
683
  			to_select_pressure = false;
b61addd8   Jiaming Guo   add adjustment fe...
684
  			float tmp_pressure = (float)(vY - y - border_factor) / ((float)vY - 3.0f * border_factor) * max_pressure;
9191c39e   Jiaming Guo   first version of ...
685
  			flow.set_pressure(pressure_index, tmp_pressure);
6ea69c93   Jiaming Guo   change bus size
686
687
  			//flow_stable_state();									// main function of solving the linear system
  			//flow.print_flow();
9191c39e   Jiaming Guo   first version of ...
688
689
  		}
  	}
f4105b89   Jiaming Guo   add new function:...
690
  	else if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && !mods && modified_bridge && change_fragment) {
b61addd8   Jiaming Guo   add adjustment fe...
691
692
693
694
695
696
697
698
  		if (y >= 2 * border_factor && y <= vY - border_factor) 		// within the ratio bar range
  			fragment_ratio = (float)(vY - y - border_factor) / ((float)vY - 3.0f * border_factor) * 1.0f;
  		else if (y < 2 * border_factor)
  			fragment_ratio = 1.0f;
  		else if (y > vY - border_factor)
  			fragment_ratio = 0.0f;
  
  		change_fragment = false;
ce1a6f5e   Jiaming Guo   add functions for...
699
  		render_flow_rate = true;
6332d1e7   Jiaming Guo   fixed warnings
700
  		flow.modify_synthetic_connection(u, rou, hilbert_curve, height_threshold, in, out, 2, fragment_ratio, default_radius);
8334680a   Jiaming Guo   add square wave c...
701
  	}
f4105b89   Jiaming Guo   add new function:...
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
  	// move connections along y-axis
  	else if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && mods == GLUT_ACTIVE_SHIFT && !modified_bridge && !picked_connection) {	
  		GLdouble posX, posY, posZ;
  		window_to_world(posX, posY, posZ);			// get the world coordinates
  
  		bool flag = flow.epsilon_edge((float)posX, (float)posY, (float)posZ, eps, connection_index, port_index);
  		if (flag) {
  			picked_connection = true;
  			picked_x = x;
  			picked_y = y;
  			if (!port_index)
  				if (flow.inlet[connection_index].V[2][1] > flow.main_feeder[port_index][1])
  					coef = 1;
  				else
  					coef = -1;
  			else
  				if (flow.outlet[connection_index].V[2][1] > flow.main_feeder[port_index][1])
  					coef = 1;
  				else
  					coef = -1;
  		}
  		else
  			picked_connection = false;
  	}
  	else if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && mods == GLUT_ACTIVE_SHIFT && !modified_bridge && render_new_connection) {
  		float l = 0.0f;
  		std::vector<typename stim::vec3<float> > V;
6332d1e7   Jiaming Guo   fixed warnings
729
  		size_t i;
f4105b89   Jiaming Guo   add new function:...
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
  		if (!port_index) {
  			i = flow.inlet[connection_index].V.size();
  			if (tmp_v2[1] != flow.inlet[connection_index].V[i - 1][1]) {
  				V.resize(4);
  				V[0] = flow.inlet[connection_index].V[0];
  				V[1] = tmp_v1;
  				V[2] = tmp_v2;
  				V[3] = flow.inlet[connection_index].V[i - 1];
  				std::swap(flow.inlet[connection_index].V, V);
  			}
  			else {
  				V.resize(3);
  				V[0] = flow.inlet[connection_index].V[0];
  				V[1] = tmp_v1;
  				V[2] = tmp_v2;
  				std::swap(flow.inlet[connection_index].V, V);
  			}
  			// calculate new length
  			for (unsigned i = 0; i < flow.inlet[connection_index].V.size() - 1; i++) {
  				l += (flow.inlet[connection_index].V[i + 1] - flow.inlet[connection_index].V[i]).len();
  			}
  			flow.inlet[connection_index].l = l;
  		}
  		else {
  			i = flow.outlet[connection_index].V.size();
  			if (tmp_v2[1] != flow.outlet[connection_index].V[i - 1][1]) {
  				V.resize(4);
  				V[0] = flow.outlet[connection_index].V[0];
  				V[1] = tmp_v1;
  				V[2] = tmp_v2;
  				V[3] = flow.outlet[connection_index].V[i - 1];
  				std::swap(flow.outlet[connection_index].V, V);
  			}
  			else {
  				V.resize(3);
  				V[0] = flow.outlet[connection_index].V[0];
  				V[1] = tmp_v1;
  				V[2] = tmp_v2;
  				std::swap(flow.outlet[connection_index].V, V);
  			}
  			// calculate new length
  			for (unsigned i = 0; i < flow.outlet[connection_index].V.size() - 1; i++) {
  				l += (flow.outlet[connection_index].V[i + 1] - flow.outlet[connection_index].V[i]).len();
  			}
  			flow.outlet[connection_index].l = l;
  		}
  
  		redisplay = true;
  		render_new_connection = false;
  		picked_connection = false;
  
  		flow.check_direct_connection();
  		flow.backup();								// back up direct synthetic connections
  	}
  	// move connections along x-axis
  	else if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && mods == GLUT_ACTIVE_CTRL && !modified_bridge && !picked_connection) {
  		GLdouble posX, posY, posZ;
  		window_to_world(posX, posY, posZ);			// get the world coordinates
  
  		bool flag = flow.epsilon_edge((float)posX, (float)posY, (float)posZ, eps, connection_index, port_index);
  		if (flag) {
  			picked_connection = true;
  			picked_x = x;
  			picked_y = y;
  			if (!port_index)
  				coef = 1;
  			else
  				coef = -1;
  		}
  		else
  			picked_connection = false;
  	}
  	else if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && mods == GLUT_ACTIVE_CTRL && !modified_bridge && render_new_connection) {
  		float l = 0.0f;
  		if (!port_index) {
  			flow.inlet[connection_index].V[0] = tmp_v1;
  			flow.inlet[connection_index].V[1] = tmp_v2;
  			// calculate new length
  			for (unsigned i = 0; i < flow.inlet[connection_index].V.size() - 1; i++) {
  				l += (flow.inlet[connection_index].V[i + 1] - flow.inlet[connection_index].V[i]).len();
  			}
  			flow.inlet[connection_index].l = l;
  		}
  		else {
  			flow.outlet[connection_index].V[0] = tmp_v1;
  			flow.outlet[connection_index].V[1] = tmp_v2;
  			// calculate new length
  			for (unsigned i = 0; i < flow.outlet[connection_index].V.size() - 1; i++) {
  				l += (flow.outlet[connection_index].V[i + 1] - flow.outlet[connection_index].V[i]).len();
  			}
  			flow.outlet[connection_index].l = l;
  		}
  
  		redisplay = true;
  		render_new_connection = false;
  		picked_connection = false;
  
  		flow.check_direct_connection();
  		flow.backup();
  	}
9191c39e   Jiaming Guo   first version of ...
830
831
832
833
834
  }
  
  // define camera move based on mouse wheel move
  void glut_wheel(int wheel, int direction, int x, int y) {
  	
6ea69c93   Jiaming Guo   change bus size
835
836
  	mods = glutGetModifiers();
  
9191c39e   Jiaming Guo   first version of ...
837
838
839
840
841
842
  	mouse_x = x;
  	mouse_y = y;
  
  	GLdouble posX, posY, posZ;
  	window_to_world(posX, posY, posZ);			// get the world coordinates
  
4bb615eb   Jiaming Guo   fixed generating ...
843
  	if (!to_select_pressure && (simulation || build_inlet_outlet || manufacture)) {							// check current pixel position only in simualtion and build_inlet_outlet modes
6ea69c93   Jiaming Guo   change bus size
844
  		bool flag = flow.epsilon_vertex((float)posX, (float)posY, (float)posZ, eps, scale, pressure_index);
ce1a6f5e   Jiaming Guo   add functions for...
845
  		if (flag && simulation && !glyph_mode) {
9191c39e   Jiaming Guo   first version of ...
846
847
848
849
850
851
852
853
854
  			float tmp_r;
  			if (direction > 0) {				// increase radii
  				tmp_r = flow.get_radius(pressure_index);
  				tmp_r += radii_factor;
  			}
  			else {
  				tmp_r = flow.get_radius(pressure_index);
  				tmp_r -= radii_factor;
  				if (tmp_r <= 0)
8334680a   Jiaming Guo   add square wave c...
855
  					tmp_r = default_radius;
9191c39e   Jiaming Guo   first version of ...
856
857
  			}
  			flow.set_radius(pressure_index, tmp_r);
ce1a6f5e   Jiaming Guo   add functions for...
858
  			undo = true;									// undo rendering
9191c39e   Jiaming Guo   first version of ...
859
  		}
6ea69c93   Jiaming Guo   change bus size
860
  		else if (!mods) {
9191c39e   Jiaming Guo   first version of ...
861
862
863
864
865
866
867
868
  			if (direction > 0)								// if it is button 3(up), move closer
  				move_pace = zoom_factor;
  			else											// if it is button 4(down), leave farther
  				move_pace = -zoom_factor;
  
  			cam.Push(move_pace);
  		}
  	}
6ea69c93   Jiaming Guo   change bus size
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
  
  	// rescale
  	if (mods == GLUT_ACTIVE_CTRL) {
  		if (direction > 0) {
  			if (scale >= 1)
  				scale += 1.0f;
  			else
  				scale += 0.1f;
  		}
  		else {
  			if (scale > 1)
  				scale -= 1.0f;
  			else if (scale <= 1 && scale > 0.1f)
  				scale -= 0.1f;
  			else
  				scale = 1.0f;
  		}
ce1a6f5e   Jiaming Guo   add functions for...
886
887
  		undo = true;
  		redisplay = true;
6ea69c93   Jiaming Guo   change bus size
888
  	}
9191c39e   Jiaming Guo   first version of ...
889
890
891
892
893
894
895
896
897
  	
  	glutPostRedisplay();
  }
  
  // define keyboard inputs
  void glut_keyboard(unsigned char key, int x, int y) {
  
  	// register different keyboard operation
  	switch (key) {
8f6c2057   Jiaming Guo   fixed index marki...
898
  
b61addd8   Jiaming Guo   add adjustment fe...
899
900
901
  		// saving network flow profile
  	case 's':
  		flow.save_network();
9191c39e   Jiaming Guo   first version of ...
902
  		break;
057ea93b   Jiaming Guo   add flow velocity...
903
  
ce1a6f5e   Jiaming Guo   add functions for...
904
905
906
907
908
909
910
911
912
  		// convert network to binary format (.nwt)
  	case 'c': {
  		std::vector<std::string> tmp = stim::parser::split(args.arg(0), '.');
  		std::stringstream ss;
  		ss << tmp[0] << ".nwt";
  		std::string filename = ss.str();
  		flow.saveNwt(filename);
  		break;
  	}
8f6c2057   Jiaming Guo   fixed index marki...
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
  
  		// subdivide current network for more detailed calculation
  	case 'd': {
  		// subdivide current network due to the limitation of current computation if needed
  		if (!subdivided && simulation && !glyph_mode) {
  			subdivided = true;
  
  			// check whether current network can be subdivided
  			if (flow.check_subdivision()) {
  				flow.subdivision();
  				get_background();
  			}
  
  			flow_initialize();					// initialize flow condition
  			// resimulation
  			flow_stable_state();				// main function of solving the linear system
  			flow.print_flow();
  			undo = true;
  		}
  		else if (subdivided && simulation && !glyph_mode) {
  			subdivided = false;
  			flow = backup;						// load back up
  
  			get_background();
  			flow_initialize();
  			// resimulation
  			flow_stable_state();				// main function of solving the linear system
  			flow.print_flow();
  			undo = true;
  		}
  		break;
  	}
  
057ea93b   Jiaming Guo   add flow velocity...
946
947
  		// flow vector field visualization, Glyphs
  	case 'f':
ce1a6f5e   Jiaming Guo   add functions for...
948
949
950
  		if (glyph_mode && !manufacture && (simulation || build_inlet_outlet)) {
  			glyph_mode = false;
  			frame_mode = false;
0224d2ef   Jiaming Guo   fixed square wave...
951
952
953
954
955
  			redisplay = true;							// lines and arrows rendering use the same display list
  			int num = glutGet(GLUT_MENU_NUM_ITEMS);
  			if (num == 1)
  				glut_set_menu(num, 2);
  		}
ce1a6f5e   Jiaming Guo   add functions for...
956
957
  		else if (!glyph_mode && !manufacture && (simulation || build_inlet_outlet)) {
  			glyph_mode = true;
0224d2ef   Jiaming Guo   fixed square wave...
958
959
960
961
962
  			redisplay = true;
  			int num = glutGet(GLUT_MENU_NUM_ITEMS);
  			if (num == 2)
  				glut_set_menu(num, 1);
  		}
ce1a6f5e   Jiaming Guo   add functions for...
963
964
  		undo = true;
  		break;
8f6c2057   Jiaming Guo   fixed index marki...
965
  
ce1a6f5e   Jiaming Guo   add functions for...
966
967
968
969
970
971
972
973
  		// filaments around arrows
  	case 'g':
  		if (glyph_mode) {
  			if (frame_mode)
  				frame_mode = false;
  			else
  				frame_mode = true;
  		}
8f6c2057   Jiaming Guo   fixed index marki...
974
  		undo = true;
057ea93b   Jiaming Guo   add flow velocity...
975
  		break;
8f6c2057   Jiaming Guo   fixed index marki...
976
  
6ea69c93   Jiaming Guo   change bus size
977
978
979
980
981
982
  		// open/close index marks
  	case 'e':
  		if (mark_index)
  			mark_index = false;
  		else
  			mark_index = true;
ce1a6f5e   Jiaming Guo   add functions for...
983
  		undo = true;
6ea69c93   Jiaming Guo   change bus size
984
985
  		break;
  
9191c39e   Jiaming Guo   first version of ...
986
987
988
989
  		// output image stack
  	case 'm':
  		if (manufacture) {
  #ifdef __CUDACC__
4bb615eb   Jiaming Guo   fixed generating ...
990
  			flow.make_image_stack(S, dx, dy, dz, stackdir, image_stack, default_radius, scale);
b61addd8   Jiaming Guo   add adjustment fe...
991
  
9191c39e   Jiaming Guo   first version of ...
992
993
994
995
996
  #else
  			std::cout << "You need to have a gpu to make image stack, sorry." << std::endl;
  #endif
  		}
  		else if (build_inlet_outlet && !modified_bridge) {
9191c39e   Jiaming Guo   first version of ...
997
  			modified_bridge = true;
8334680a   Jiaming Guo   add square wave c...
998
999
  
  			if (hilbert_curve)
6332d1e7   Jiaming Guo   fixed warnings
1000
  				flow.modify_synthetic_connection(u, rou, hilbert_curve, height_threshold, in, out, 2, fragment_ratio);
8334680a   Jiaming Guo   add square wave c...
1001
1002
  			else
  				change_fragment = true;
9191c39e   Jiaming Guo   first version of ...
1003
1004
1005
  		}
  		break;
  	}
8334680a   Jiaming Guo   add square wave c...
1006
  	
9191c39e   Jiaming Guo   first version of ...
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
  	glutPostRedisplay();
  }
  
  // glut initialization
  void glut_initialize() {
  
  	int myargc = 1;
  	char* myargv[1];
  	myargv[0] = strdup("generate_network_network");
  
  	glutInit(&myargc, myargv);
  	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  	glutInitWindowPosition(100, 100);							// set the initial window position
  	glutInitWindowSize(1000, 1000);
  	glutCreateWindow("3D flow simulation");
  
  	glutDisplayFunc(glut_render);
  	glutMouseFunc(glut_mouse);
  	glutMotionFunc(glut_motion);
9e60c6a7   Jiaming Guo   fixed minor bug i...
1026
  	glutPassiveMotionFunc(glut_passive_motion);
9191c39e   Jiaming Guo   first version of ...
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
  	glutMouseWheelFunc(glut_wheel);
  	glutKeyboardFunc(glut_keyboard);
  
  	glutCreateMenu(glut_menu);					// create a menu object 
  	glut_set_menu(0, 1);
  	glutAttachMenu(GLUT_RIGHT_BUTTON);			// register right mouse to open menu option
  
  	stim::vec3<float> c = bb.center();			// get the center of the network bounding box
  	// place the camera along the z-axis at a distance determined by the network size along x and y
  	cam.setPosition(c + stim::vec<float>(0, 0, camera_factor * std::max(bb.size()[0], bb.size()[1])));
  	cam.LookAt(c[0], c[1], c[2]);
  }
  
9e60c6a7   Jiaming Guo   fixed minor bug i...
1040
1041
1042
1043
  // output an advertisement for the lab, authors and usage information
  void advertise() {
  	std::cout << std::endl << std::endl;
  	std::cout << " =======================================================================================" << std::endl;
b61addd8   Jiaming Guo   add adjustment fe...
1044
  	std::cout << "|Thank you for using the AFAN tool!                                                     |" << std::endl;
9e60c6a7   Jiaming Guo   fixed minor bug i...
1045
1046
1047
1048
1049
  	std::cout << "|Scalable Tissue Imaging and Modeling (STIM) Lab, University of Houston                 |" << std::endl;
  	std::cout << "|Developers: Jiaming Guo, David Mayerich                                                |" << std::endl;
  	std::cout << "|Source: https://git.stim.ee.uh.edu/Jack/flow3.git									  |" << std::endl;
  	std::cout << " =======================================================================================" << std::endl << std::endl;
  
616c5f99   Jiaming Guo   add advertise
1050
1051
  	std::cout << "Usage(keyboard): e -> open/close indexing" << std::endl;
  	std::cout << "                 m -> build synthetic connections(connection mode)/output augmented network as image stack (manufacture mode)" << std::endl;
ce1a6f5e   Jiaming Guo   add functions for...
1052
1053
1054
1055
  	std::cout << "                 s -> save network flow profiles in profile folder as cvs files" << std::endl;
  	std::cout << "                 c -> convert .obj network to .nwt network and stores in main folder" << std::endl;
  	std::cout << "                 f -> open/close vector field visualization mode" << std::endl;
  	std::cout << "                 g -> render filament frames in vector fiedl visualization mode" << std::endl;
616c5f99   Jiaming Guo   add advertise
1056
  
9e60c6a7   Jiaming Guo   fixed minor bug i...
1057
1058
1059
1060
  	std::cout << args.str();
  }
  
  // main function: parse arguments and initialize GLUT
9191c39e   Jiaming Guo   first version of ...
1061
1062
1063
1064
  int main(int argc, char* argv[]) {
  	
  	// add arguments
  	args.add("help", "prints the help");
b61addd8   Jiaming Guo   add adjustment fe...
1065
  	args.add("units", "string indicating units of length for output measurements (ex. velocity)", "um", "text string");
9191c39e   Jiaming Guo   first version of ...
1066
1067
1068
  	args.add("maxpress", "maximum allowed pressure in g / units / s^2, default 2 is for blood when units = um", "2", "real value > 0");
  	args.add("viscosity", "set the viscosity of the fluid (in g / units / s), default .00001 is for blood when units = um", ".00001", "real value > 0");
  	args.add("rou", "set the desity of the fluid (in g / units^3), default 1.06*10^-12 is for blood when units = um", ".00000000000106", "real value > 0");
6332d1e7   Jiaming Guo   fixed warnings
1069
  	args.add("hilbert", "activate hilbert curves connections");
b61addd8   Jiaming Guo   add adjustment fe...
1070
  	args.add("stack", "load the image stack");
9191c39e   Jiaming Guo   first version of ...
1071
1072
  	args.add("stackres", "spacing between pixel samples in each dimension(in units/pixel)", "1 1 1", "real value > 0");
  	args.add("stackdir", "set the directory of the output image stack", "", "any existing directory (ex. /home/name/network)");
ce1a6f5e   Jiaming Guo   add functions for...
1073
1074
  	args.add("scale", "scale down rendering fibers");
  	args.add("lcc", "extract the largest connected component");
9191c39e   Jiaming Guo   first version of ...
1075
1076
1077
  	
  	args.parse(argc, argv);								// parse the command line
  
9e60c6a7   Jiaming Guo   fixed minor bug i...
1078
1079
1080
1081
1082
  	if (args["help"].is_set()) {
  		advertise();
  		std::exit(1);
  	}
  
9191c39e   Jiaming Guo   first version of ...
1083
  	// load network
6751e826   David Mayerich   fixed 'index out ...
1084
1085
1086
1087
1088
1089
  	if (args.nargs() == 0) {
  		std::cout << "Network file required." << std::endl;
  		return 1;
  	}	
  	else {						// load network from user 
  		std::vector<std::string> tmp = stim::parser::split(args.arg(0), '.');
8f6c2057   Jiaming Guo   fixed index marki...
1090
  		if ("obj" == tmp[1]) {
6751e826   David Mayerich   fixed 'index out ...
1091
  			flow.load_obj(args.arg(0));
8f6c2057   Jiaming Guo   fixed index marki...
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
  			backup.load_obj(args.arg(0));
  		}	
  		else if ("nwt" == tmp[1]) {		// stim network binary format
  			flow.loadNwt(args.arg(0)); 
  			backup.loadNwt(args.arg(0));
  		}		
  		else if ("swc" == tmp[1]) {
  			flow.load_swc(args.arg(0)); 
  			backup.load_swc(args.arg(0));
  		}
9191c39e   Jiaming Guo   first version of ...
1102
1103
1104
1105
1106
  		else {
  			std::cout << "Invalid file type" << std::endl;
  			std::exit(1);
  		}
  	}
ce1a6f5e   Jiaming Guo   add functions for...
1107
1108
  
  	// extract the largest connected component
9191c39e   Jiaming Guo   first version of ...
1109
  
b61addd8   Jiaming Guo   add adjustment fe...
1110
1111
1112
1113
  	// get the units to work on
  	units = args["units"].as_string();
  	flow.set_units(units);
  
9191c39e   Jiaming Guo   first version of ...
1114
1115
  	// blood pressure in capillaries range from 15 - 35 torr
  	// 1 torr = 133.3 Pa
6332d1e7   Jiaming Guo   fixed warnings
1116
  	max_pressure = (float)args["maxpress"].as_float();
9191c39e   Jiaming Guo   first version of ...
1117
1118
1119
  
  	// normal blood viscosity range from 4 - 15 mPa·s(cP)
  	// 1 Pa·s = 1 g / mm / s
6332d1e7   Jiaming Guo   fixed warnings
1120
  	u = (float)args["viscosity"].as_float();			// g / units / s
9191c39e   Jiaming Guo   first version of ...
1121
1122
  
  	// normally the blood density in capillaries: 1060 kg/m^3 = 1.06*10^-12 g/um^3
6332d1e7   Jiaming Guo   fixed warnings
1123
  	rou = (float)args["rou"].as_float();
9191c39e   Jiaming Guo   first version of ...
1124
  
8334680a   Jiaming Guo   add square wave c...
1125
  	// check whether to enable hilbert curves or not
6332d1e7   Jiaming Guo   fixed warnings
1126
  	hilbert_curve = args["hilbert"].is_set();
8334680a   Jiaming Guo   add square wave c...
1127
  
b61addd8   Jiaming Guo   add adjustment fe...
1128
1129
1130
1131
1132
1133
  	// load image stack if provided
  	if (args["stack"].is_set()) {
  		image_stack = true;
  		S.load_images(args["stack"].as_string());
  		// binary transformation
  #ifdef __CUDACC__
6332d1e7   Jiaming Guo   fixed warnings
1134
  		size_t N = S.samples();													// number of pixels loaded
ce1a6f5e   Jiaming Guo   add functions for...
1135
  		unsigned char* d_S;														// image stack stored in device
b61addd8   Jiaming Guo   add adjustment fe...
1136
1137
1138
1139
  		unsigned char* h_S = (unsigned char*)malloc(N * sizeof(unsigned char));	// image stack stored in host
  		cudaMalloc((void**)&d_S, N * sizeof(unsigned char));
  		cudaMemcpy(d_S, S.data(), N * sizeof(unsigned char), cudaMemcpyHostToDevice);
  
6332d1e7   Jiaming Guo   fixed warnings
1140
1141
  		size_t thread = 1024;
  		size_t block = N / thread + 1;
ce1a6f5e   Jiaming Guo   add functions for...
1142
  		binary_transform <<<block, thread>>> (N, d_S, binary_threshold);		// binaryzation
b61addd8   Jiaming Guo   add adjustment fe...
1143
1144
1145
1146
  
  		cudaMemcpy(h_S, d_S, N * sizeof(unsigned char), cudaMemcpyDeviceToHost);
  
  		S.copy(h_S);
b61addd8   Jiaming Guo   add adjustment fe...
1147
1148
1149
  #endif
  	}
  
9191c39e   Jiaming Guo   first version of ...
1150
  	// get the vexel and image stack size
6332d1e7   Jiaming Guo   fixed warnings
1151
1152
1153
  	dx = (float)args["stackres"].as_float(0);
  	dy = (float)args["stackres"].as_float(1);
  	dz = (float)args["stackres"].as_float(2);
9191c39e   Jiaming Guo   first version of ...
1154
1155
1156
1157
1158
  
  	// get the save directory of image stack
  	if (args["stackdir"].is_set())
  		stackdir = args["stackdir"].as_string();
  
ce1a6f5e   Jiaming Guo   add functions for...
1159
1160
  	// get the scale-down factor is provided
  	if (args["scale"].is_set())
6332d1e7   Jiaming Guo   fixed warnings
1161
  		scale = (float)args["scale"].as_float();
ce1a6f5e   Jiaming Guo   add functions for...
1162
  
9191c39e   Jiaming Guo   first version of ...
1163
1164
1165
1166
1167
  	// glut main loop
  	bb = flow.boundingbox();
  	glut_initialize();
  	glutMainLoop();
  }