Subversion Repositories gelsvn

Rev

Rev 391 | 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>
388 jab 42
#include <HMesh/obj_save.h>
387 jab 43
#include <HMesh/mesh_optimization.h>
388 jab 44
#include <HMesh/triangulate.h>
45
#include <HMesh/close_holes.h>
46
#include <HMesh/caps_and_needles.h>
47
#include <HMesh/refine_edges.h>
48
#include <HMesh/subdivision.h>
386 jab 49
 
50
#include <GLConsole/GLConsole.h>
394 jab 51
#include <Util/Timer.h>
386 jab 52
#include "harmonics.h"
53
#include "wireframe.h"
54
 
391 jab 55
using namespace std;
56
using namespace HMesh;
57
using namespace Geometry;
58
using namespace GLGraphics;
59
using namespace CGLA;
60
using namespace Util;
61
using namespace LinAlg;
386 jab 62
 
391 jab 63
int WINX=800, WINY=800;
64
 
65
class VisObj
66
{
67
	string file;
68
	GLViewController view_ctrl;
69
	GLuint display_list;
70
	bool create_display_list;
71
	Manifold mani;
72
	Harmonics* harmonics;
73
 
74
public:
75
 
76
	Manifold& mesh() {return mani;}
77
	GLViewController& view_control() {return view_ctrl;}
78
 
79
	bool reload(string _file)
80
	{
81
		if(_file != "") file = _file;
82
		mani.clear();
83
		if(!load(file, mani))
84
			return false;
85
		Vec3f c(0,0,0);
86
		float r = 5;
87
		mani.get_bsphere(c,r);
88
		view_ctrl.set_centre(c);
89
		view_ctrl.set_eye_dist(2*r);
90
		return true;
91
	}
92
 
93
	VisObj():
94
	file(""), view_ctrl(WINX,WINY, Vec3f(0), 1.0), display_list(glGenLists(1)), create_display_list(true), harmonics(0) 
95
	{
96
	}
97
 
98
	void display(bool wire, bool harm, bool flat)
99
	{
394 jab 100
/*		static Timer tim;
101
		tim.start();*/
391 jab 102
		if(create_display_list)
103
		{
104
			create_display_list = false;
105
 
106
			glNewList(display_list,GL_COMPILE);
107
			if(wire)
108
			{
109
				enable_wireframe();
394 jab 110
				draw(mani,false);
391 jab 111
				glUseProgram(0);	
112
			}
113
			else if(harm)
114
				harmonics->draw();
115
			else 
116
				draw(mani,!flat);
117
			glEndList();
118
		}
119
		view_ctrl.set_gl_modelview();
120
		glCallList(display_list);
394 jab 121
		// tim.get_secs();
391 jab 122
	}
123
 
124
 
125
	void post_create_display_list()
126
	{
127
		create_display_list = true;
128
	}
129
 
130
	void harmonics_analyze_mesh()
131
	{
132
		delete harmonics;
133
		harmonics = new Harmonics(mani);
134
	}
135
 
136
	void harmonics_reset_shape()
137
	{
138
		if(harmonics)
139
			harmonics->reset_shape();
140
	}
141
 
142
	void harmonics_parse_key(unsigned char key)
143
	{
144
		harmonics->parse_key(key);
145
	}
146
 
147
	void harmonics_partial_reconstruct(int eig0, int eig1, float scale)
148
	{
149
		if(harmonics)
150
			harmonics->partial_reconstruct(eig0, eig1, scale);
151
	}
152
 
153
};
154
 
155
inline VisObj& get_vis_obj(int i)
156
{
157
	static VisObj vo[9];
158
	return vo[i];
159
}
160
 
161
inline VisObj& avo()
162
{
163
	static CVar<int> active("active_mesh",0);
164
	return get_vis_obj(active);
165
}
166
 
167
inline Manifold& active_mesh()
168
{
169
	return avo().mesh();
170
}
171
 
172
inline GLViewController& active_view_control()
173
{
174
	return avo().view_control();
175
}
176
 
386 jab 177
// Single global instance so glut can get access
178
GLConsole theConsole;
179
 
180
////////////////////////////////////////////////////////////////////////////////
181
char* ConsoleHelp(std::vector<std::string> &args)
182
{
183
    theConsole.Printf("");
184
    theConsole.Printf("----------------- HELP -----------------");
391 jab 185
    theConsole.Printf("Press ESC key to open and close console");
386 jab 186
    theConsole.Printf("Press TAB to see the available commands and functions");
187
    theConsole.Printf("Functions are shown in green and variables in yellow");
188
    theConsole.Printf("Setting a value: [command] = value");
189
    theConsole.Printf("Getting a value: [command]");
190
    theConsole.Printf("Functions: [function] [arg1] [arg2] ...");
391 jab 191
    theConsole.Printf("Entering arg1=? or arg1=help will give a description.");
386 jab 192
    theConsole.Printf("History: Up and Down arrow keys move through history.");
193
    theConsole.Printf("Tab Completion: TAB does tab completion and makes suggestions.");
391 jab 194
    theConsole.Printf("");
195
    theConsole.Printf("Keyboard commands (when console is not active):");
196
    theConsole.Printf("w   : toggle wireframe");
197
    theConsole.Printf("f   : toggle flatshading");
198
    theConsole.Printf("1-9 : switch between active meshes.");
394 jab 199
    theConsole.Printf("d   : (display.show_harmonics = 1) diffuse light on and off");
200
    theConsole.Printf("h   : (display.show_harmonics = 1) highlight on and off ");
201
    theConsole.Printf("+/- : (display.show_harmonics = 1) which eigenvector to show");
391 jab 202
    theConsole.Printf("q   : quit program");
203
    theConsole.Printf("ESC : open console");
204
    theConsole.Printf("");
205
    theConsole.Printf("Mouse: Left button rotates, middle zooms, right pans");
386 jab 206
    theConsole.Printf("----------------- HELP -----------------");
207
    theConsole.Printf("");
208
    return "";
209
}
210
 
391 jab 211
bool wantshelp(std::vector<std::string> &args)
212
{
213
	if(args.size()==0) return false;
214
	string str = args[0];
215
	if(str=="help" || str=="HELP" || str=="Help" || str=="?") return true;
216
	return false;
217
}
386 jab 218
 
391 jab 219
/// Function that aligns two meshes.
220
char* console_align(std::vector<std::string> &args)
221
{
222
	if(wantshelp(args)) 
223
		{
224
			theConsole.Printf("usage: align <dest> <src>");
225
			theConsole.Printf("This function aligns dest mesh with src");
226
			theConsole.Printf("In practice the GLViewController of src is copied to dst.");
227
			theConsole.Printf("both arguments are mandatory and must be numbers between 1 and 9.");
228
			theConsole.Printf("Note that results might be unexpexted if the meshes are not on the same scale");
229
			return "";
230
		}
386 jab 231
 
391 jab 232
	int dest = 0;
233
	if(args.size()>0)
234
	{
235
		istringstream a0(args[0]);
236
		a0 >> dest;
237
		--dest;
238
		if(dest <0 || dest>8) return "dest mesh out of range (1-9)";
239
	}
240
	else return "neither source nor destination mesh?!";
241
	int src = 0;
242
	if(args.size()>1)
243
	{
244
		istringstream a1(args[1]);
245
		a1 >> src;
246
		--src;
247
		if(src <0 || src>8) return "src mesh out of range (1-9)";
248
	}	
249
	else return "no src mesh?";
386 jab 250
 
391 jab 251
	get_vis_obj(dest).view_control() = get_vis_obj(src).view_control();
252
 
253
	return "";
386 jab 254
}
255
 
391 jab 256
 
388 jab 257
char* console_save(std::vector<std::string> &args)
258
{
391 jab 259
	if(wantshelp(args)) 
260
		{
261
			theConsole.Printf("usage: save <name.x3d|name.obj> ");
262
			return "";
263
		}
388 jab 264
	string& file_name = args[0];
265
	if(args.size() == 1)
266
	{
267
		if(file_name.substr(file_name.length()-4,file_name.length())==".obj")
268
		{
391 jab 269
			obj_save(file_name, active_mesh());
388 jab 270
			return "";
271
		}
272
		else if(file_name.substr(file_name.length()-4,file_name.length())==".x3d")
273
		{
391 jab 274
			x3d_save(file_name, active_mesh());
388 jab 275
			return "";
276
		}
277
		return "unknown format";
278
	}
279
	return "usage: save <name.x3d|name.obj> ";
280
}
386 jab 281
 
388 jab 282
char* console_refine_edges(std::vector<std::string> &args)
283
{
391 jab 284
	if(wantshelp(args)) 
285
		{
286
			theConsole.Printf("usage: refine.split_edges <length>");
287
			theConsole.Printf("splits edges longer than <length>; default is 0.5 times average length");
288
			return "";
289
		}
290
 
388 jab 291
	float thresh = 0.5;
292
	if(args.size()>0)
293
	{
294
		istringstream a0(args[0]);
295
		a0 >> thresh;
296
	}
391 jab 297
	float avg_length = average_edge_length(active_mesh());
298
	refine_edges(active_mesh(), thresh * avg_length);
388 jab 299
	return "";
391 jab 300
 
388 jab 301
}
302
 
303
char* console_refine_faces(std::vector<std::string> &args)
304
{
391 jab 305
	if(wantshelp(args)) 
306
		{
307
			theConsole.Printf("usage: refine.split_faces ");
308
			theConsole.Printf("usage:  Takes no arguments. Inserts a vertex at the centre of each face.");
309
			return "";
310
		}
311
 
312
	safe_triangulate(active_mesh());
388 jab 313
	return "";
391 jab 314
 
388 jab 315
}
316
 
317
char* console_cc_subdivide(std::vector<std::string> &args)
318
{
391 jab 319
	if(wantshelp(args)) 
320
		{
321
			theConsole.Printf("usage: refine.catmull_clark ");
322
			theConsole.Printf("Splits each polygon into four (Catmull Clark style)");
323
			return "";
324
		}
325
	cc_split(active_mesh(),active_mesh());
388 jab 326
	return "";
327
}
328
 
394 jab 329
char* console_dual(std::vector<std::string> &args)
330
{
331
	if(wantshelp(args)) 
332
	{
333
		theConsole.Printf("usage: dual ");
334
		theConsole.Printf("Produces the dual by converting each face to a vertex placed at the barycenter.");
335
		return "";
336
	}
337
 
338
	Manifold& m = active_mesh();
339
 
340
	// make sure every face knows its number
341
	m.enumerate_faces();
342
 
343
	vector<Vec3f> vertices(m.no_faces());
344
	vector<int> faces(m.no_vertices());
345
	vector<int> indices;
346
 
347
	// Create new vertices. Each face becomes a vertex whose position
348
	// is the centre of the face
349
	int i=0;
350
	for(FaceIter f=m.faces_begin(); f!=m.faces_end(); ++f,++i)
351
		vertices[i] = centre(f);
352
 
353
	// Create new faces. Each vertex is a new face with N=valency of vertex
354
	// edges.
355
	i=0;
356
	for(VertexIter v=m.vertices_begin(); v!= m.vertices_end(); ++v,++i)
357
	{
358
		VertexCirculator vc(v);
359
		vector<int> index_tmp;
360
		for(; !vc.end(); ++vc)
361
			index_tmp.push_back(vc.get_face()->touched);
362
 
363
		// Push vertex indices for this face onto indices vector.
364
		// The circulator moves around the face in a clockwise fashion
365
		// so we just reverse the ordering.
366
		indices.insert(indices.end(), index_tmp.rbegin(), index_tmp.rend());
367
 
368
		// Insert face valency in the face vector.
369
		faces[i] = vc.no_steps();
370
	}
371
 
372
	// Clear the manifold before new geometry is inserted.
373
	m.clear();
374
 
375
	// And build
376
	build_manifold(m, vertices.size(), &vertices[0], faces.size(),
377
				   &faces[0],&indices[0]);
378
 
379
	return "";
380
}
388 jab 381
 
382
 
387 jab 383
char* console_minimize_curvature(std::vector<std::string> &args)
384
{
391 jab 385
	if(wantshelp(args)) 
386
		{
387
			theConsole.Printf("usage: optimize.minimize_curvature <anneal>");
388
			theConsole.Printf("Flip edges to minimize mean curvature.");
389
			theConsole.Printf("If anneal is true, simulated annealing (slow) is used rather than a greedy scheme");
390
			return "";
391
		}
387 jab 392
	bool anneal=false;
393
	if(args.size()>0)
394
	{
395
		istringstream a0(args[0]);
396
		a0 >> anneal;
397
	}
391 jab 398
 
399
	minimize_curvature(active_mesh(), anneal);
400
	avo().post_create_display_list();
387 jab 401
	return "";
402
}
403
 
404
char* console_minimize_dihedral(std::vector<std::string> &args)
405
{
391 jab 406
	if(wantshelp(args)) 
407
		{
408
			theConsole.Printf("usage: optimize.minimize_dihedral <iter> <anneal> <use_alpha> <gamma> ");
409
			theConsole.Printf("Flip edges to minimize dihedral angles.");
410
			theConsole.Printf("Iter is the max number of iterations. anneal tells us whether to use ");
411
			theConsole.Printf("simulated annealing and not greedy optimization. use_alpha (default=true) ");
412
			theConsole.Printf("means to use angle and not cosine of anglegamma (default=4) is the power ");
413
			theConsole.Printf("to which we raise the dihedral angle");
414
			return "";
415
		}
387 jab 416
	int iter = 1000;
417
	if(args.size()>0)
418
	{
419
		istringstream a0(args[0]);
420
		a0 >> iter;
421
	}
391 jab 422
 
387 jab 423
	bool anneal = false;
424
	if(args.size()>1)
425
	{
426
		istringstream a0(args[0]);
427
		a0 >> anneal;
428
	}
391 jab 429
 
387 jab 430
	bool use_alpha = true;
388 jab 431
	if(args.size()>2)
387 jab 432
	{
433
		istringstream a0(args[0]);
434
		a0 >> use_alpha;
435
	}
391 jab 436
 
387 jab 437
	float gamma = 4.0;
438
	if(args.size()>3)
439
	{
440
		istringstream a0(args[0]);
441
		a0 >> gamma;
442
	}
443
 
444
 
391 jab 445
	minimize_dihedral_angle(active_mesh(), iter, anneal, use_alpha, gamma);
387 jab 446
	return "";
447
}
448
 
388 jab 449
char* console_maximize_min_angle(std::vector<std::string> &args)
450
{
391 jab 451
	if(wantshelp(args)) 
452
		{
453
			theConsole.Printf("usage: optimize.maximize_min_angle <thresh> <anneal>");
454
			theConsole.Printf("Flip edges to maximize min angle - to make mesh more Delaunay.");
455
			theConsole.Printf("If the dot product of the normals between adjacent faces < thresh");
456
			theConsole.Printf("no flip will be made. anneal selects simulated annealing rather ");
457
			theConsole.Printf("nthan greedy optimization.");
458
			return "";
459
		}
388 jab 460
	float thresh=0.0;
461
	if(args.size()>0)
462
	{
463
		istringstream a0(args[0]);
464
		a0 >> thresh;
465
	}
466
	bool anneal=false;
467
	if(args.size()>1)
468
	{
469
		istringstream a0(args[0]);
470
		a0 >> anneal;
471
	}
391 jab 472
	maximize_min_angle(active_mesh(),thresh,anneal);
388 jab 473
	return "";
474
}
475
 
476
 
387 jab 477
char* console_optimize_valency(std::vector<std::string> &args)
478
{
391 jab 479
	if(wantshelp(args)) 
480
		{
481
			theConsole.Printf("usage: optimize.valency <anneal> ");
482
			theConsole.Printf("Optimizes valency for triangle meshes. Anneal selects simulated annealing rather than greedy optim.");
483
			return "";
484
		}
387 jab 485
	bool anneal=false;
486
	if(args.size()>0)
487
	{
488
		istringstream a0(args[0]);
489
		a0 >> anneal;
490
	}
391 jab 491
	optimize_valency(active_mesh(), anneal);
387 jab 492
	return "";
493
}
494
 
388 jab 495
char* console_analyze(std::vector<std::string> &args)
496
{
391 jab 497
	if(wantshelp(args)) 
498
		{
499
			theConsole.Printf("usage:  harmonics.analyze");
500
			theConsole.Printf("Creates the Laplace Beltrami operator for the mesh and finds all eigensolutions.");
501
			theConsole.Printf("It also projects the vertices onto the eigenvectors - thus transforming the mesh");
502
			theConsole.Printf("to this basis.");
503
			theConsole.Printf("Note that this will stall the computer for a large mesh - as long as we use Lapack.");
504
			return "";
505
		}
506
	avo().harmonics_analyze_mesh();
388 jab 507
	return "";
508
}
509
 
510
 
386 jab 511
char* console_partial_reconstruct(std::vector<std::string> &args)
512
{
391 jab 513
	if(wantshelp(args)) 
514
		{
515
			theConsole.Printf("usage: haramonics.partial_reconstruct <e0> <e1> <s>");
516
			theConsole.Printf("Reconstruct from projections onto eigenvectors. The two first arguments indicate");
517
			theConsole.Printf("the eigenvector interval that we reconstruct from. The last argument is the ");
518
			theConsole.Printf("scaling factor. Thus, for a vertex, v, the formula for computing the position, p, is:");
519
			theConsole.Printf("for (i=e0; i<=e1;++i) p += proj[i] * Q[i][v] * s;");
520
			theConsole.Printf("where proj[i] is the 3D vector containing the x, y, and z projections of the mesh onto");
521
			theConsole.Printf("eigenvector i. Q[i][v] is the v'th coordinate of the i'th eigenvector.");
522
			theConsole.Printf("Note that if vertex coordinates are not first reset, the result is probably unexpected.");
523
			return "";
524
		}
386 jab 525
	int E0,E1;
526
	float scale;
527
	istringstream a0(args[0]);
528
	a0 >> E0;
529
	istringstream a1(args[1]);
530
	a1 >> E1;
531
	istringstream a2(args[2]);
532
	a2 >> scale;
391 jab 533
	avo().harmonics_partial_reconstruct(E0,E1,scale);
386 jab 534
	return "";
535
}
536
 
537
char* console_reset_shape(std::vector<std::string> &args)
538
{
391 jab 539
	if(wantshelp(args)) 
540
		{
541
			theConsole.Printf("usage: harmonics.reset_shape ");
542
			theConsole.Printf("Simply sets all vertices to 0,0,0. Call this before doing partial_reconstruct");
543
			theConsole.Printf("unless you know what you are doing.");
544
			return "";
545
		}
546
	avo().harmonics_reset_shape();
386 jab 547
	return "";
548
}
549
 
550
 
388 jab 551
char* console_close_holes(std::vector<std::string> &args)
552
{
391 jab 553
	if(wantshelp(args)) 
554
		{
555
			theConsole.Printf("usage: cleanup.close_holes");
556
			theConsole.Printf("This function closes holes. It simply follows the loop of halfvectors which");
557
			theConsole.Printf("enclose the hole and add a face to which they all point.");
558
			return "";
559
		}
560
	close_holes(active_mesh());
388 jab 561
	return "";
562
}
386 jab 563
 
388 jab 564
char* console_reload(std::vector<std::string> &args)
565
{
391 jab 566
	if(wantshelp(args)) 
567
		{
568
			theConsole.Printf("usage:  reload <file>");
569
			theConsole.Printf("Reloads the current file if no argument is given, but");
570
			theConsole.Printf("if an argument is given, then that becomes the current file");
571
			return "";
572
		}
573
	if(!avo().reload(args.size()>0 ? args[0]:""))
574
		return "failed to load";
388 jab 575
	return "";
576
}
577
 
578
 
387 jab 579
char* console_simplify(std::vector<std::string> &args)
580
{
391 jab 581
	if(wantshelp(args)) 
582
		{
583
			theConsole.Printf("usage: simplify <fraction> ");
584
			theConsole.Printf("Performs Garland Heckbert (quadric based) mesh simplification.");
585
			theConsole.Printf("The only argument is the fraction of vertices to keep.");
586
			return "";
587
		}
387 jab 588
	float keep_fraction;
589
	if(args.size()==0) return "you must specify fraction of vertices to keep";
590
	istringstream a0(args[0]);
591
	a0 >> keep_fraction;
391 jab 592
 
387 jab 593
	Vec3f p0, p7;
391 jab 594
	active_mesh().get_bbox(p0, p7);
387 jab 595
	Vec3f d = p7-p0;
596
	float s = 1.0/d.max_coord();
597
	Vec3f pcentre = (p7+p0)/2.0;
391 jab 598
	for(VertexIter vi = active_mesh().vertices_begin(); vi != active_mesh().vertices_end(); ++vi)
387 jab 599
		vi->pos = (vi->pos - pcentre) * s;
391 jab 600
	quadric_simplify(active_mesh(),keep_fraction,0.0001f,true);
601
	for(VertexIter vi = active_mesh().vertices_begin(); vi != active_mesh().vertices_end(); ++vi)
387 jab 602
		vi->pos = vi->pos*d.max_coord() + pcentre;
603
	return "";
604
}
605
 
391 jab 606
char* console_vertex_noise(std::vector<std::string> &args)
607
{
608
	if(wantshelp(args)) 
609
		{
610
			theConsole.Printf("usage: noise.perturb_vertices <amplitude>");
611
			theConsole.Printf("adds a random vector to each vertex. To ensure uniformness, the vector must lie in the");
612
			theConsole.Printf("unit sphere. The length of the vector is multiplied by the average edge length and then amplitude");
613
			return "";
614
		}
615
	float avg_length = average_edge_length(active_mesh());
616
 
617
	float noise_amplitude = 0.5;
618
	if(args.size()>0) 
619
	{
620
		istringstream a0(args[0]);
621
		a0 >> noise_amplitude;
622
	}
623
 
624
	srand(0);
625
	for(VertexIter vi = active_mesh().vertices_begin(); vi != active_mesh().vertices_end(); ++vi)
626
	{
627
		Vec3f v;
628
		do {
629
			v = Vec3f(rand(),rand(),rand());
630
			v /= RAND_MAX;
631
		} while(sqr_length(v) > 1.0);
632
		v -= Vec3f(0.5);
633
		v *= 2.0;
634
		v *= noise_amplitude;
635
		v *= avg_length;
636
		vi->pos += v;
637
	}		
638
	return "";
639
}
640
 
641
char* console_noisy_flips(std::vector<std::string> &args)
642
{
643
	if(wantshelp(args)) 
644
		{
645
			theConsole.Printf("usage:  noise.perturb_topology <iter>");
646
			theConsole.Printf("Perform random flips. iter (default=1) is the number of iterations.");
647
			theConsole.Printf("mostly for making nasty synthetic test cases.");
648
			return "";
649
		}
650
	int iter=1;
651
	if(args.size()>0)
652
	{
653
		istringstream a0(args[0]);
654
		a0 >> iter;
655
	}
656
 
657
	randomize_mesh(active_mesh(),  iter);
658
	return "";
659
}
660
 
387 jab 661
char* console_laplacian_smooth(std::vector<std::string> &args)
662
{
391 jab 663
	if(wantshelp(args)) 
664
		{
665
			theConsole.Printf("usage:  smooth.laplacian <weight>");
666
			theConsole.Printf("Perform Laplacian smoothing. weight is the scaling factor for the Laplacian.");
667
			return "";
668
		}
387 jab 669
	float t=1.0;
670
	if(args.size()>0)
671
	{
672
		istringstream a0(args[0]);
673
		a0 >> t;
674
	}
675
	/// Simple laplacian smoothing with an optional weight.
391 jab 676
	laplacian_smooth(active_mesh(), t);
387 jab 677
	return "";
678
}
679
 
680
char* console_taubin_smooth(std::vector<std::string> &args)
681
{
391 jab 682
	if(wantshelp(args)) 
683
		{
684
			theConsole.Printf("usage:  smooth.taubin <iter>");
685
			theConsole.Printf("Perform Taubin smoothing. iter (default=1) is the number of iterations.");
686
			return "";
687
		}
387 jab 688
	int iter=1;
689
	if(args.size()>0)
690
	{
691
		istringstream a0(args[0]);
692
		a0 >> iter;
693
	}
694
 
695
	/// Taubin smoothing is similar to laplacian smoothing but reduces shrinkage
391 jab 696
	taubin_smooth(active_mesh(),  iter);
387 jab 697
	return "";
698
}
699
 
700
char* console_fvm_smooth(std::vector<std::string> &args)
701
{	
391 jab 702
	if(wantshelp(args)) 
703
		{
704
			theConsole.Printf("usage: smooth.fuzzy_vector_median <iter>");
705
			theConsole.Printf("Smooth normals using fuzzy vector median smoothing. iter (default=1) is the number of iterations");
706
			theConsole.Printf("This function does a very good job of preserving sharp edges.");
707
			return "";
708
		}
387 jab 709
	int iter=1;
710
	if(args.size()>0)
711
	{
712
		istringstream a0(args[0]);
713
		a0 >> iter;
714
	}
715
	/** Fuzzy vector median smoothing is effective when it comes to
716
	 preserving sharp edges. */
391 jab 717
	fvm_smooth(active_mesh(),  iter);
387 jab 718
	return "";
391 jab 719
 
387 jab 720
}
721
 
388 jab 722
char* console_triangulate(std::vector<std::string> &args)
723
{	
391 jab 724
	if(wantshelp(args)) 
725
		{
726
			theConsole.Printf("usage:  triangulate");
727
			theConsole.Printf("This function triangulates all non triangular faces of the mesh.");
728
			theConsole.Printf("you may want to call it after hole closing. For a polygon it simply connects");
729
			theConsole.Printf("the two closest vertices in a recursive manner until only triangles remain");
730
			return "";
731
		}
732
	shortest_edge_triangulate(active_mesh());
388 jab 733
	return "";
734
}
735
 
736
 
737
char* console_remove_caps(std::vector<std::string> &args)
738
{	
391 jab 739
	if(wantshelp(args)) 
740
		{
741
			theConsole.Printf("usage:  cleanup.remove_caps thresh");
742
			theConsole.Printf("Remove caps (triangles with one very big angle). The thresh argument is the fraction of PI to");
743
			theConsole.Printf("use as threshold for big angle. Default is 0.85. Caps are removed by flipping.");
744
			return "";
745
		}
746
	float t=0.85;
747
	if(args.size()>0)
748
	{
749
		istringstream a0(args[0]);
750
		a0 >> t;
751
	}
752
 
753
	remove_caps_from_trimesh(active_mesh(), static_cast<float>(M_PI) *t);
388 jab 754
	return "";
755
}
756
 
757
char* console_remove_needles(std::vector<std::string> &args)
758
{	
391 jab 759
	if(wantshelp(args)) 
760
		{
761
			theConsole.Printf("usage: cleanup.remove_needles <thresh>");
762
			theConsole.Printf("Removes very short edges by collapse. thresh is multiplied by the average edge length");
763
			theConsole.Printf("to get the length shorter than which we collapse. Default = 0.1");
764
			return "";
765
		}
388 jab 766
	float thresh = 0.1;
767
	if(args.size()>0)
768
	{
769
		istringstream a0(args[0]);
770
		a0 >> thresh;
771
	}
391 jab 772
	float avg_length = average_edge_length(active_mesh());
773
	remove_needles_from_trimesh(active_mesh(), thresh * avg_length);
388 jab 774
	return "";
775
}
776
 
391 jab 777
void reshape(int W, int H)
778
{
779
	active_view_control().reshape(W,H);
780
}
781
 
386 jab 782
void display() 
783
{
784
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
785
 
388 jab 786
	static CVar<int> display_wireframe("display.wireframe",0);
394 jab 787
	static CVar<int> display_eigenmodes("display.show_harmonics",0);
388 jab 788
	static CVar<int> display_flat("display.flatshading",0);
386 jab 789
 
790
	glPushMatrix();
791
 
391 jab 792
	avo().display(display_wireframe, display_eigenmodes, display_flat);
386 jab 793
 
794
	glPopMatrix();
795
 
796
	glUseProgram(0);
797
	theConsole.RenderConsole();
798
 
799
	glutSwapBuffers();
800
}
801
 
802
void animate() 
803
{	
394 jab 804
	//usleep( (int)1e4 );
391 jab 805
	active_view_control().try_spin();
386 jab 806
	glutPostRedisplay();
807
}
808
 
809
 
810
void mouse(int button, int state, int x, int y) 
811
{
812
	Vec2i pos(x,y);
813
	if (state==GLUT_DOWN) 
814
	{
815
		if (button==GLUT_LEFT_BUTTON) 
391 jab 816
			active_view_control().grab_ball(ROTATE_ACTION,pos);
386 jab 817
		else if (button==GLUT_MIDDLE_BUTTON) 
391 jab 818
			active_view_control().grab_ball(ZOOM_ACTION,pos);
386 jab 819
		else if (button==GLUT_RIGHT_BUTTON) 
391 jab 820
			active_view_control().grab_ball(PAN_ACTION,pos);
386 jab 821
	}
822
	else if (state==GLUT_UP)
391 jab 823
		active_view_control().release_ball();
386 jab 824
}
825
 
826
void motion(int x, int y) {
827
	Vec2i pos(x,y);
391 jab 828
	active_view_control().roll_ball(Vec2i(x,y));
386 jab 829
}
830
 
831
void keyboard_spec(int key, int x, int y)
832
{
391 jab 833
	int mod = glutGetModifiers();
834
	if( theConsole.isOpen() ) {
835
		// If shift held, scroll the console
836
		if( mod == GLUT_ACTIVE_SHIFT ) {
837
			switch (key){
838
				case GLUT_KEY_UP:
839
					theConsole.ScrollDownLine();
840
					break;
841
				case GLUT_KEY_DOWN: 
842
					theConsole.ScrollUpLine();
843
					break;
844
			}
845
		} else {
846
			theConsole.StandardKeyBindings( key );
847
		}
848
	}
386 jab 849
}
850
 
851
template<typename T>
852
T& get_CVar_ref(const std::string& s)
853
{
854
	return *reinterpret_cast<T*> (GetCVarData(s));
855
}
856
 
857
void keyboard(unsigned char key, int x, int y) 
858
{	
859
	if(theConsole.isOpen())
391 jab 860
	{
386 jab 861
		switch(key) {
862
			case '\033': 
863
				theConsole.ToggleConsole();
864
			default:      
865
				//send keystroke to console
866
				if( theConsole.isOpen() ){
867
					theConsole.EnterCommandCharacter(key);
868
				}
869
				break;
388 jab 870
		}
391 jab 871
		if(key == 13)	avo().post_create_display_list();
872
 
873
	}	
386 jab 874
	else {
875
		int& display_wireframe = get_CVar_ref<int>("display.wireframe");
388 jab 876
		int& display_flat = get_CVar_ref<int>("display.flatshading");
391 jab 877
		int& active  = get_CVar_ref<int>("active_mesh");
386 jab 878
 
391 jab 879
 
386 jab 880
		switch(key) {
881
			case 'q': exit(0);
882
			case '\033':
883
				theConsole.ToggleConsole();
884
				break;
391 jab 885
			case '1':
886
			case '2':
887
			case '3':
888
			case '4':
889
			case '5':
890
			case '6':
891
			case '7':
892
			case '8':
893
			case '9':
894
				active = key - '1'; break;
895
			case 'f': display_flat = !display_flat; break;
386 jab 896
			case 'w':
897
				display_wireframe = !display_wireframe;
898
				break;
899
		}
391 jab 900
 
394 jab 901
		if(get_CVar_ref<int>("display.show_harmonics"))
391 jab 902
			avo().harmonics_parse_key(key);
903
 
904
		avo().post_create_display_list();		
386 jab 905
	}
906
}
907
 
908
void init_glut(int argc, char** argv)
909
{
910
	glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
911
	glutInitWindowSize(WINX, WINY);
912
	glutInit(&argc, argv);
913
	glutCreateWindow("Shape Harmonics");
914
	glutDisplayFunc(display);
915
	glutKeyboardFunc(keyboard);
916
	glutSpecialFunc(keyboard_spec);
917
	glutReshapeFunc(reshape);
918
	glutMouseFunc(mouse);
919
	glutMotionFunc(motion);
920
	glutIdleFunc(animate);
921
}
922
 
923
void init_gl()
924
{
925
	glewInit();
926
	glEnable(GL_LIGHTING);
927
	glEnable(GL_LIGHT0);
388 jab 928
	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
386 jab 929
 
930
	initialize_wireframe_shaders();
931
 
932
 
933
	// Set the value of a uniform
934
	//glUniform2f(glGetUniformLocation(prog_P0,"WIN_SCALE"), win_size_x/2.0, win_size_y/2.0);
935
 
936
	glMatrixMode(GL_MODELVIEW);
937
	glLoadIdentity();
938
	glClearColor(0.50f, 0.50f, 0.50f, 0.f);
939
	glColor4f(1.0f, 1.0f, 1.0f, 0.f);
940
	glEnable(GL_DEPTH_TEST);
941
 
942
	static CVar<ConsoleFunc> help( "help", ConsoleHelp );
391 jab 943
	static CVar<ConsoleFunc> rs("harmonics.reset_shape", console_reset_shape);
944
	static CVar<ConsoleFunc> ha("harmonics.analyze", console_analyze);
945
	static CVar<ConsoleFunc> pr("harmonics.partial_reconstruct", console_partial_reconstruct);
387 jab 946
	static CVar<ConsoleFunc> simpl("simplify", console_simplify);
391 jab 947
	static CVar<ConsoleFunc> lsmooth("smooth.laplacian", console_laplacian_smooth);
948
	static CVar<ConsoleFunc> tsmooth("smooth.taubin", console_taubin_smooth);
949
	static CVar<ConsoleFunc> fsmooth("smooth.fuzzy_vector_median", console_fvm_smooth);
950
 
951
	static CVar<ConsoleFunc> opt_val("optimize.valency", console_optimize_valency);
952
	static CVar<ConsoleFunc> min_dih("optimize.minimize_dihedral_angles", console_minimize_dihedral);
953
	static CVar<ConsoleFunc> min_curv("optimize.minimize_curvature", console_minimize_curvature);
954
	static CVar<ConsoleFunc> max_min_angle("optimize.maximize_min_angle", console_maximize_min_angle);
955
	static CVar<ConsoleFunc> close_holes_fun("cleanup.close_holes", console_close_holes);
388 jab 956
	static CVar<ConsoleFunc> reload_fun("reload", console_reload);
391 jab 957
 
958
	static CVar<ConsoleFunc> rem_caps_fun("cleanup.remove_caps", console_remove_caps);
959
	static CVar<ConsoleFunc> rem_needles_fun("cleanup.remove_needles", console_remove_needles);
388 jab 960
	static CVar<ConsoleFunc> triangulate_fun("triangulate", console_triangulate);
391 jab 961
	static CVar<ConsoleFunc> refine_fun("refine.split_edges", console_refine_edges);
962
	static CVar<ConsoleFunc> refine_face_fun("refine.split_faces", console_refine_faces);
963
	static CVar<ConsoleFunc> subd_fun("refine.catmull_clark", console_cc_subdivide);
388 jab 964
	static CVar<ConsoleFunc> save_fun("save", console_save);
391 jab 965
	static CVar<ConsoleFunc> noise_fun("noise.perturb_vertices", console_vertex_noise);
966
	static CVar<ConsoleFunc> noise_fun2("noise.perturb_topology", console_noisy_flips);
967
 
394 jab 968
	static CVar<ConsoleFunc> dualize("dual", console_dual);
969
 
391 jab 970
	static CVar<ConsoleFunc> align_fun("align", console_align);
387 jab 971
 
972
 
386 jab 973
}
974
 
975
int main(int argc, char** argv)
976
{
977
	ArgExtracter ae(argc, argv);
391 jab 978
 
979
	init_glut(argc,argv);
980
	init_gl();
981
 
982
	Harmonics::init();
983
 
984
		if(argc>1)
386 jab 985
	{		
391 jab 986
		string file = ae.get_last_arg();
987
		avo().reload(file);
386 jab 988
	}
989
 
990
 
991
	glutMainLoop();
992
}
993
 
994