Subversion Repositories gelsvn

Rev

Rev 391 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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