Blame view

main.cpp 9.06 KB
6bf618a1   David Mayerich   initial commit
1
2
3
4
5
6
  #include <iostream>
  
  #include <GL/glut.h>
  
  #include <stim/visualization/camera.h>
  #include <stim/parser/arguments.h>
f8a38243   David Mayerich   made changes to s...
7
8
  #include <stim/visualization/obj.h>
  #include <stim/visualization/gl_spharmonics.h>
6bf618a1   David Mayerich   initial commit
9
10
11
  
  #define theta_scale		0.01
  #define phi_scale		0.01
f8a38243   David Mayerich   made changes to s...
12
  #define zoom_scale		0.1
6bf618a1   David Mayerich   initial commit
13
14
15
16
17
  
  //create a global camera that will specify the viewport
  stim::camera cam;
  int mx, my;			//mouse coordinates in the window space
  
f8a38243   David Mayerich   made changes to s...
18
  stim::gl_spharmonics<double> S;
6bf618a1   David Mayerich   initial commit
19
20
21
22
23
24
25
  
  float d = 1.5;		//initial distance between the camera and the sphere
  
  bool rotate_zoom = true;	//sets the current camera mode (rotation = true, zoom = false)
  
  stim::arglist args;			//class for processing command line arguments
  
f8a38243   David Mayerich   made changes to s...
26
  bool zaxis = false;			//render the z-axis (set via a command line flag)
6bf618a1   David Mayerich   initial commit
27
28
29
30
31
32
33
34
35
36
37
38
  
  bool init(){
  
  	//set the clear color to white
  	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  
  	//initialize the camera
  	cam.setPosition(d, d, d);
  	cam.LookAt(0, 0, 0, 0, 1, 1);
  	cam.setFOV(40);
  
  	//initialize the texture map stuff	
6bf618a1   David Mayerich   initial commit
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
  	S.glInit(256);
  
  	return true;
  }
  
  //code that is run every time the user changes something
  void display(){
  	//clear the screen
  	glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  
  	//set the projection matrix
  	glMatrixMode(GL_PROJECTION);				//put the projection matrix on the stack
  	glLoadIdentity();							//set it to the identity matrix
  	gluPerspective(cam.getFOV(), 1, 0.001, 1000000);	//set up a perspective projection
  
  
  	//set the model view matrix
  	glMatrixMode(GL_MODELVIEW);					//load the model view matrix to the stack
  	glLoadIdentity();							//set it to the identity matrix
  
  	//get the camera parameters
f8a38243   David Mayerich   made changes to s...
60
61
62
  	stim::vec3<float> p = cam.getPosition();
  	stim::vec3<float> u = cam.getUp();
  	stim::vec3<float> d = cam.getDirection();
6bf618a1   David Mayerich   initial commit
63
64
65
66
67
68
69
  
  	//specify the camera parameters to OpenGL
  	gluLookAt(p[0], p[1], p[2], d[0], d[1], d[2], u[0], u[1], u[2]);
  
  	//draw the sphere
  	S.glRender();
  
f8a38243   David Mayerich   made changes to s...
70
71
72
73
74
75
76
77
78
79
80
81
  	//glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  
  	//draw the z-axis if requested
  	if(zaxis){
  		glDisable(GL_TEXTURE_2D);
  		glColor3f(0.0f, 1.0f, 0.0f);
  		glBegin(GL_LINES);
  			glVertex3f(0.0, 0.0, 0.0);
  			glVertex3f(0.0, 0.0, 100.0);
  		glEnd();
  	}
  
6bf618a1   David Mayerich   initial commit
82
83
84
85
86
87
88
89
90
  	//flush commands on the GPU
  	glutSwapBuffers();
  }
  
  void mouse_press(int button, int state, int x, int y){
  
  	//set the camera motion mode based on the mouse button pressed
  	if(button == GLUT_LEFT_BUTTON)
  		rotate_zoom = true;
f8a38243   David Mayerich   made changes to s...
91
  	else if(button == GLUT_RIGHT_BUTTON)
6bf618a1   David Mayerich   initial commit
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  		rotate_zoom = false;
  
  	//if the mouse is pressed
  	if(state == GLUT_DOWN){
  		//set the current mouse position
  		mx = x;		my = y;
  	}
  }
  
  void mouse_drag(int x, int y){
  
  	//if the camera is in rotation mode, rotate
  	if(rotate_zoom == true){
  		float theta = theta_scale * (mx - x);
  		float phi = -phi_scale * (my - y);
  
  		//if the mouse is dragged
  		cam.OrbitFocus(theta, phi);
  	}
  	//otherwize zoom
  	else{
f8a38243   David Mayerich   made changes to s...
113
  		cam.Push(zoom_scale*(my - y));
6bf618a1   David Mayerich   initial commit
114
115
116
117
118
119
120
121
122
123
124
  	}
  
  	//update the mouse position
  	mx = x;		my = y;
  
  	glutPostRedisplay();
  }
  
  void process_arguments(int argc, char* argv[]){
  
  	args.add("help", "prints this help");
f8a38243   David Mayerich   made changes to s...
125
126
127
128
129
130
  	args.add("rand", "generates a random set of SH coefficients", "", "[N min max]");
  	args.add("sparse", "generates a function based on a set of sparse basis functions", "", "[l0 m0 c0 l1 m1 c1 l2 m2 c2 ...]");
  	args.add("basis", "displays the specified SH basis function", "", "n, or [l m]");
  	args.add("obj", "approximates a geometric object given as a Wavefront OBJ file", "", "filename");
  	args.add("out", "filename for outputting spherical harmonics coefficients", "", "filename");
  	args.add("zaxis", "render the z-axis as a green line");
6bf618a1   David Mayerich   initial commit
131
132
133
134
  
  	//process the command line arguments
  	args.parse(argc, argv);
  
f8a38243   David Mayerich   made changes to s...
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  	//set the z-axis flag
  	if(args["zaxis"].is_set())
  		zaxis = true;
  
  	//if arguments are specified, push them as coefficients
  	if(args.nargs() > 0){
  		//push all of the arguments to the spherical harmonics class as coefficients
  		for(unsigned int a = 0; a < args.nargs(); a++)
  			S.push(atof(args.arg(a).c_str()));
  	}
  
  	//if the user wants to use a random set of SH coefficients
  	else if(args["rand"].is_set()){
  
  		//return an error if the user specifies both fixed and random coefficients
  		if(args.nargs() != 0){
  			std::cout<<"Error: both fixed and random coefficients are specified"<<std::endl;
  			exit(1);
  		}
  
  		//seed the random number generator
  		srand(time(NULL));
  
  		unsigned int N = args["rand"].as_int(0);		//get the number of random coefficients
  		double Cmin = args["rand"].as_float(1);			//get the minimum and maximum coefficient values
  		double Cmax = args["rand"].as_float(2);
  
  		//generate the coefficients
  		for(unsigned int c = 0; c < N; c++){
  
  			double norm = (double) rand() / RAND_MAX;		//calculate a random number in the range [0, 1]
  			double scaled = norm * (Cmax - Cmin) + Cmin;	//scale the random number to [Cmin, Cmax]
  			S.push(scaled);									//push the value as a coefficient
  		}
  	}
  	else if(args["sparse"].is_set()){
  
  		//calculate the number of sparse coefficients
  		unsigned int nC = args["sparse"].nargs() / 3;
  
  		std::vector<unsigned int> C;	//vector of 1D coefficients
  		unsigned int Cmax = 0;			//maximum coefficient provided
  
  		std::vector<double> V;			//vector of 1D coefficient values
  
  		unsigned int c;
  		int l, m;
  		double v;
  		//for each provided coefficient
  		for(unsigned int i = 0; i < nC; i++){
  
  			//load data for a single coefficient from the command line
  			l = args["sparse"].as_int( i * 3 + 0 );
  			m = args["sparse"].as_int( i * 3 + 1 );
  			v = args["sparse"].as_float( i * 3 + 2 );
  
  			//calculate the 1D coefficient
  			c = pow(l + 1, 2) - (l - m) - 1;
  
  			//update the maximum coefficient index
  			if(c > Cmax) Cmax = c;
6bf618a1   David Mayerich   initial commit
196
  
f8a38243   David Mayerich   made changes to s...
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
  			//insert the coefficient and value into vectors
  			C.push_back(c);
  			V.push_back(v);			
  		}
  
  		//set the size of the SH coefficient array
  		S.resize(Cmax + 1);
  		
  		//insert each coefficient
  		for(unsigned int i = 0; i < nC; i++){
  			S.setc(C[i], V[i]);
  		}
  
  	}
  	else if(args["obj"].is_set()){
  
  		std::string filename = args["obj"].as_string(0);
  		unsigned int l = args["obj"].as_int(1);
  
  		//create an obj object
  		stim::obj<double> object(filename);
  
  		//get the centroid of the object
  		stim::vec<double> c = object.centroid();
  
  		//get the number of vertices in the model
  		unsigned int nV = object.numV();
  
  		//for each vertex in the model, create an MC sample
  		std::vector< stim::vec<double> > spherical;
  		stim::vec<float> sample;
  		stim::vec<float> centered;
  		for(unsigned int i = 0; i < nV; i++){
  
  			sample = object.getV(i);			//get a vertex in cartesian coordinates
  			centered = sample - c;
  			spherical.push_back(centered.cart2sph());
  		}
  
  		//generate the spherical PDF
  		stim::spharmonics<double> P;
  		P.pdf(spherical, l, l);
  
  		//begin Monte-Carlo sampling, using the model vertices as samples
  		S.mcBegin(l, l);
  		double theta, phi, fx, px;
  		for(unsigned int i = 0; i < nV; i++){
  			theta = spherical[i][1];
  			phi = spherical[i][2];
  			fx = spherical[i][0];
  			px = P(theta, phi);
  			S.mcSample(theta, phi, fx / px);
  		}
  		S.mcEnd();
  	}
  
  	//if the user specifies an SH basis function
  	else if(args["basis"].is_set()){
  
  		unsigned int n;
  
  		//if the user specifies one index for the basis function
  		if(args["basis"].nargs() == 1)
  			n = args["basis"].as_int(0);
  		else if(args["basis"].nargs() == 2){
  			int l = args["basis"].as_int(0);		//2D indexing (l, m)
  			int m = args["basis"].as_int(1);
  
  			n = pow(l+1, 2) - (l - m) - 1;			//calculate the 1D index
  		}
  
  		//add zeros for the first (n-1) coefficients
  		for(unsigned int c = 0; c < n; c++)
  			S.push(0);
  
  		//add the n'th coefficient
  		S.push(1);
  	}
  
  	//output the spherical harmonics coefficients if requested
  	if(args["out"].is_set()){
  		
  		if(args["out"].nargs() == 0)
  			std::cout<<S.str()<<std::endl;
  		else{
  
  			//open the output file
  			std::ofstream outfile;
  			outfile.open(args["out"].as_string(0).c_str());
  
  			outfile<<S.str();
  
  			outfile.close();
  		}
  	}
  
  
  	
  	
6bf618a1   David Mayerich   initial commit
296
297
298
299
300
  
  	//if the user asks for help, give it and exit
  	if(args["help"].is_set()){
  		std::cout<<"usage: shview c0 c1 c2 c3 ... --option [A B C]"<<std::endl;
  		std::cout<<"examples:"<<std::endl;
f8a38243   David Mayerich   made changes to s...
301
302
303
304
305
306
307
  		std::cout<<"   generate a spherical function with 4 coefficients (l=0 to 2)"<<std::endl;
  		std::cout<<"          shview 1.3 0.2 2.3 1.34"<<std::endl;
  		std::cout<<"   display a spherical function representing the spherical harmonic l = 3, m = -2"<<std::endl;
  		std::cout<<"          shview --basis 3 -2"<<std::endl;
  
  
  
6bf618a1   David Mayerich   initial commit
308
309
310
311
312
313
314
  		std::cout<<args.str();
  		exit(0);
  	}
  }
  
  int main(int argc, char *argv[]){
  
f8a38243   David Mayerich   made changes to s...
315
316
317
318
  #ifdef _WIN32
  	args.set_ansi(false);
  #endif
  
6bf618a1   David Mayerich   initial commit
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
  	//initialize GLUT
  	glutInit(&argc, argv);
  
  	//process arguments
  	process_arguments(argc, argv);
  
  	//set the size of the GLUT window
  	glutInitWindowSize(500, 500);
  
  	glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE);
  
  	//create the GLUT window (and an OpenGL context)
  	glutCreateWindow("Spherical Harmonic Viewport");
  
  	//set the display function (which will be called repeatedly by glutMainLoop)
  	glutDisplayFunc(display);
  
  	//set the mouse press function (called when a mouse button is pressed)
  	glutMouseFunc(mouse_press);
  	//set the mouse motion function (which will be called any time the mouse is dragged)
  	glutMotionFunc(mouse_drag);
  
  	//run the initialization function
  	if(!init())
  		return 1;	//return an error if it fails
  
  
  	//enter the main loop
  	glutMainLoop();
  
  	//return 0 if everything is awesome
  	return 0;
  
  
  
  }