Subversion Repositories gelsvn

Rev

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