Subversion Repositories gelsvn

Rev

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