Subversion Repositories gelsvn

Rev

Rev 386 | Rev 388 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
386 jab 1
/*
2
 *  MeshEdit is a small application which allows you to load and edit a mesh.
3
 *  The mesh will be stored in GEL's half edge based Manifold data structure.
4
 *  A number of editing operations are supported. Most of these are accessible from the 
5
 *  console that pops up when you hit 'esc'.
6
 *
7
 *  Created by J. Andreas Bærentzen on 15/08/08.
8
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
9
 *
10
 */
11
#include <iostream>
12
#include <CGLA/eigensolution.h>
13
#include <CGLA/Vec2d.h>
14
#include <CGLA/Vec3d.h>
15
#include <CGLA/Mat3x3d.h>
16
#include <CGLA/Mat2x2d.h>
17
#include <CGLA/Mat2x3d.h>
18
 
19
#include <LinAlg/Matrix.h>
20
#include <LinAlg/Vector.h>
21
#include <LinAlg/LapackFunc.h>
22
 
23
#include <Util/Timer.h>
24
#include <Util/ArgExtracter.h>
25
 
26
#include <GL/glew.h>
27
#include <GLGraphics/gel_glut.h>
28
#include <GLGraphics/draw.h>
29
#include <GLGraphics/glsl_shader.h>
30
#include <GLGraphics/GLViewController.h>
31
 
32
#include <HMesh/Manifold.h>
33
#include <HMesh/VertexCirculator.h>
34
#include <HMesh/FaceCirculator.h>
35
#include <HMesh/build_manifold.h>
36
#include <HMesh/mesh_optimization.h>
37
#include <HMesh/triangulate.h>
38
#include <HMesh/load.h>
387 jab 39
#include <HMesh/quadric_simplify.h>
40
#include <HMesh/smooth.h>
386 jab 41
#include <HMesh/x3d_save.h>
387 jab 42
#include <HMesh/mesh_optimization.h>
386 jab 43
 
44
#include <GLConsole/GLConsole.h>
45
 
46
#include "harmonics.h"
47
#include "wireframe.h"
48
 
49
 
50
// Single global instance so glut can get access
51
GLConsole theConsole;
52
 
53
////////////////////////////////////////////////////////////////////////////////
54
char* ConsoleHelp(std::vector<std::string> &args)
55
{
56
    theConsole.Printf("");
57
    theConsole.Printf("----------------- HELP -----------------");
58
    theConsole.Printf("Press ~ key to open and close console");
59
    theConsole.Printf("Press TAB to see the available commands and functions");
60
    theConsole.Printf("Functions are shown in green and variables in yellow");
61
    theConsole.Printf("Setting a value: [command] = value");
62
    theConsole.Printf("Getting a value: [command]");
63
    theConsole.Printf("Functions: [function] [arg1] [arg2] ...");
64
    theConsole.Printf("Entering just the function name will give a description.");
65
    theConsole.Printf("History: Up and Down arrow keys move through history.");
66
    theConsole.Printf("Tab Completion: TAB does tab completion and makes suggestions.");
67
    theConsole.Printf("----------------- HELP -----------------");
68
    theConsole.Printf("");
69
    return "";
70
}
71
 
72
using namespace std;
73
using namespace HMesh;
74
using namespace Geometry;
75
using namespace GLGraphics;
76
using namespace CGLA;
77
using namespace Util;
78
using namespace LinAlg;
79
 
80
 
81
 
82
namespace 
83
{
84
	GLViewController* view_ctrl;
85
	int WINX=800, WINY=800;
86
 
87
	Manifold mani;	
88
	bool create_display_list = true;
89
}
90
 
91
 
387 jab 92
char* console_minimize_curvature(std::vector<std::string> &args)
93
{
94
	bool anneal=false;
95
	if(args.size()>0)
96
	{
97
		istringstream a0(args[0]);
98
		a0 >> anneal;
99
	}
386 jab 100
 
387 jab 101
	minimize_curvature(mani, anneal);
102
	return "";
103
}
104
 
105
char* console_minimize_dihedral(std::vector<std::string> &args)
106
{
107
	int iter = 1000;
108
	if(args.size()>0)
109
	{
110
		istringstream a0(args[0]);
111
		a0 >> iter;
112
	}
113
 
114
	bool anneal = false;
115
	if(args.size()>1)
116
	{
117
		istringstream a0(args[0]);
118
		a0 >> anneal;
119
	}
120
 
121
	bool use_alpha = true;
122
	if(args.size()>22)
123
	{
124
		istringstream a0(args[0]);
125
		a0 >> use_alpha;
126
	}
127
 
128
	float gamma = 4.0;
129
	if(args.size()>3)
130
	{
131
		istringstream a0(args[0]);
132
		a0 >> gamma;
133
	}
134
 
135
 
136
	minimize_dihedral_angle(mani, iter, anneal, use_alpha, gamma);
137
	return "";
138
}
139
 
140
char* console_optimize_valency(std::vector<std::string> &args)
141
{
142
	bool anneal=false;
143
	if(args.size()>0)
144
	{
145
		istringstream a0(args[0]);
146
		a0 >> anneal;
147
	}
148
	optimize_valency(mani, anneal);
149
	return "";
150
}
151
 
386 jab 152
char* console_partial_reconstruct(std::vector<std::string> &args)
153
{
154
	int E0,E1;
155
	float scale;
156
	istringstream a0(args[0]);
157
	a0 >> E0;
158
	istringstream a1(args[1]);
159
	a1 >> E1;
160
	istringstream a2(args[2]);
161
	a2 >> scale;
162
	partial_reconstruct(mani, E0,E1,scale);
163
	return "";
164
}
165
 
166
char* console_reset_shape(std::vector<std::string> &args)
167
{
168
	reset_shape(mani);
169
	return "";
170
}
171
 
172
void reshape(int W, int H)
173
{
174
	view_ctrl->reshape(W,H);
175
}
176
 
177
 
387 jab 178
char* console_simplify(std::vector<std::string> &args)
179
{
180
	float keep_fraction;
181
	if(args.size()==0) return "you must specify fraction of vertices to keep";
182
	istringstream a0(args[0]);
183
	a0 >> keep_fraction;
386 jab 184
 
387 jab 185
	Vec3f p0, p7;
186
	mani.get_bbox(p0, p7);
187
	Vec3f d = p7-p0;
188
	float s = 1.0/d.max_coord();
189
	Vec3f pcentre = (p7+p0)/2.0;
190
	for(VertexIter vi = mani.vertices_begin(); vi != mani.vertices_end(); ++vi)
191
		vi->pos = (vi->pos - pcentre) * s;
192
	quadric_simplify(mani,keep_fraction,0.0001f,true);
193
	for(VertexIter vi = mani.vertices_begin(); vi != mani.vertices_end(); ++vi)
194
		vi->pos = vi->pos*d.max_coord() + pcentre;
195
	return "";
196
}
197
 
198
char* console_laplacian_smooth(std::vector<std::string> &args)
199
{
200
	float t=1.0;
201
	if(args.size()>0)
202
	{
203
		istringstream a0(args[0]);
204
		a0 >> t;
205
	}
206
	/// Simple laplacian smoothing with an optional weight.
207
	laplacian_smooth(mani, t);
208
	return "";
209
}
210
 
211
char* console_taubin_smooth(std::vector<std::string> &args)
212
{
213
	int iter=1;
214
	if(args.size()>0)
215
	{
216
		istringstream a0(args[0]);
217
		a0 >> iter;
218
	}
219
 
220
	/// Taubin smoothing is similar to laplacian smoothing but reduces shrinkage
221
	taubin_smooth(mani,  iter);
222
	return "";
223
}
224
 
225
char* console_fvm_smooth(std::vector<std::string> &args)
226
{	
227
	int iter=1;
228
	if(args.size()>0)
229
	{
230
		istringstream a0(args[0]);
231
		a0 >> iter;
232
	}
233
	/** Fuzzy vector median smoothing is effective when it comes to
234
	 preserving sharp edges. */
235
	fvm_smooth(mani,  iter);
236
	return "";
237
 
238
}
239
 
386 jab 240
void display() 
241
{
242
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
243
 
244
	static CVar<int> do_wireframe("display.wireframe",0);
245
 
246
	glPushMatrix();
247
 
248
	view_ctrl->set_gl_modelview();
249
 
250
	if(create_display_list)
251
	{
252
		create_display_list = false;
253
 
254
		glNewList(1,GL_COMPILE);
255
		if(do_wireframe)
256
			{
257
				enable_wireframe();
258
				draw(mani);
259
			}
260
		else
261
			draw_eigenvalues(mani);
262
		glEndList();
263
	}
264
	glCallList(1);
265
 
266
	glPopMatrix();
267
 
268
	glUseProgram(0);
269
	theConsole.RenderConsole();
270
 
271
	glutSwapBuffers();
272
}
273
 
274
void animate() 
275
{	
276
	usleep( (int)1e4 );
277
	view_ctrl->try_spin();
278
	glutPostRedisplay();
279
}
280
 
281
 
282
void mouse(int button, int state, int x, int y) 
283
{
284
	Vec2i pos(x,y);
285
	if (state==GLUT_DOWN) 
286
	{
287
		if (button==GLUT_LEFT_BUTTON) 
288
			view_ctrl->grab_ball(ROTATE_ACTION,pos);
289
		else if (button==GLUT_MIDDLE_BUTTON) 
290
			view_ctrl->grab_ball(ZOOM_ACTION,pos);
291
		else if (button==GLUT_RIGHT_BUTTON) 
292
			view_ctrl->grab_ball(PAN_ACTION,pos);
293
	}
294
	else if (state==GLUT_UP)
295
		view_ctrl->release_ball();
296
}
297
 
298
void motion(int x, int y) {
299
	Vec2i pos(x,y);
300
	view_ctrl->roll_ball(Vec2i(x,y));
301
}
302
 
303
void keyboard_spec(int key, int x, int y)
304
{
305
   int mod = glutGetModifiers();
306
 
307
    if( theConsole.isOpen() ) {
308
        // If shift held, scroll the console
309
        if( mod == GLUT_ACTIVE_SHIFT ) {
310
            switch (key){
311
                case GLUT_KEY_UP:
312
                    theConsole.ScrollDownLine();
313
                    break;
314
                case GLUT_KEY_DOWN: 
315
                    theConsole.ScrollUpLine();
316
                    break;
317
            }
318
        } else {
319
            theConsole.StandardKeyBindings( key );
320
        }
321
    }
322
}
323
 
324
template<typename T>
325
T& get_CVar_ref(const std::string& s)
326
{
327
	return *reinterpret_cast<T*> (GetCVarData(s));
328
}
329
 
330
void keyboard(unsigned char key, int x, int y) 
331
{	
332
	if(theConsole.isOpen())
333
		switch(key) {
334
			case '\033': 
335
				theConsole.ToggleConsole();
336
			default:      
337
				//send keystroke to console
338
				if( theConsole.isOpen() ){
339
					theConsole.EnterCommandCharacter(key);
340
				}
341
				break;
342
		}	
343
	else {
344
		int& display_eigenvalue = get_CVar_ref<int>("display.eigenvalue");
345
		int& display_wireframe = get_CVar_ref<int>("display.wireframe");
346
		int& display_diffuse = get_CVar_ref<int>("display.diffuse");
347
		int& display_highlight = get_CVar_ref<int>("display.highlight");
348
 
349
		switch(key) {
350
			case 'q': exit(0);
351
			case '\033':
352
				theConsole.ToggleConsole();
353
				break;
354
			case '+': 
355
				display_eigenvalue = min(display_eigenvalue+1, MAX_E); 
356
				break;
357
			case '-': 
358
				display_eigenvalue = max(display_eigenvalue-1, 0); 
359
				break;
360
			case '1': E = 1; reconstruct(mani,E); 
361
				break;
362
			case '>': if(E < MAX_E) partial_reconstruct(mani,E,++E); 
363
				break;
364
			case '<': E = max(E-1, 0); reconstruct(mani,E); 
365
				break;
366
			case 'd':	
367
				display_diffuse = !display_diffuse; 
368
				break;
369
			case 'h':
370
				display_highlight = !display_highlight;
371
				break;			
372
			case 'w':
373
				display_wireframe = !display_wireframe;
374
				break;
375
		}
376
	}
377
	create_display_list = true;
378
}
379
 
380
void init_glut(int argc, char** argv)
381
{
382
	glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
383
	glutInitWindowSize(WINX, WINY);
384
	glutInit(&argc, argv);
385
	glutCreateWindow("Shape Harmonics");
386
	glutDisplayFunc(display);
387
	glutKeyboardFunc(keyboard);
388
	glutSpecialFunc(keyboard_spec);
389
	glutReshapeFunc(reshape);
390
	glutMouseFunc(mouse);
391
	glutMotionFunc(motion);
392
	glutIdleFunc(animate);
393
}
394
 
395
void init_gl()
396
{
397
	glewInit();
398
	glEnable(GL_LIGHTING);
399
	glEnable(GL_LIGHT0);
400
	Vec3f c(0,0,0);
401
	float r = 5;
402
	mani.get_bsphere(c,r);
403
	view_ctrl = new GLViewController(WINX,WINY, c,r*2);
404
 
405
	initialize_wireframe_shaders();
406
 
407
 
408
	// Set the value of a uniform
409
	//glUniform2f(glGetUniformLocation(prog_P0,"WIN_SCALE"), win_size_x/2.0, win_size_y/2.0);
410
 
411
	glMatrixMode(GL_MODELVIEW);
412
	glLoadIdentity();
413
	glClearColor(0.50f, 0.50f, 0.50f, 0.f);
414
	glColor4f(1.0f, 1.0f, 1.0f, 0.f);
415
	glEnable(GL_DEPTH_TEST);
416
 
417
	static CVar<ConsoleFunc> help( "help", ConsoleHelp );
418
	static CVar<ConsoleFunc> rs("reset_shape", console_reset_shape);
419
	static CVar<ConsoleFunc> pr("partial_reconstruct", console_partial_reconstruct);
387 jab 420
	static CVar<ConsoleFunc> simpl("simplify", console_simplify);
421
	static CVar<ConsoleFunc> lsmooth("laplacian_smooth", console_laplacian_smooth);
422
	static CVar<ConsoleFunc> tsmooth("taubin_smooth", console_taubin_smooth);
423
	static CVar<ConsoleFunc> fsmooth("fvm_smooth", console_fvm_smooth);
386 jab 424
 
387 jab 425
	static CVar<ConsoleFunc> opt_val("optimize_valency", console_optimize_valency);
426
	static CVar<ConsoleFunc> min_dih("minimize_dihedral", console_minimize_dihedral);
427
	static CVar<ConsoleFunc> min_curv("minimize_curvature", console_minimize_curvature);
428
 
429
 
386 jab 430
}
431
 
432
int main(int argc, char** argv)
433
{
434
	ArgExtracter ae(argc, argv);
435
	ae.extract("-E", E);
436
    if(argc>1)
437
	{		
438
		string file = ae.get_last_arg();
439
		load(file, mani);
440
	}
441
 
442
 
443
	init_glut(argc,argv);
444
	init_gl();
445
	init_harmonics(mani);
446
 
447
	glutMainLoop();
448
}
449
 
450