Subversion Repositories gelsvn

Rev

Rev 393 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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