Subversion Repositories gelsvn

Rev

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