Subversion Repositories gelsvn

Rev

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

Rev 596 Rev 600
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
 
11
 
12
#include <string>
12
#include <string>
13
#include <iostream>
13
#include <iostream>
14
#include <vector>
14
#include <vector>
15
#include <algorithm>
15
#include <algorithm>
16
 
16
 
17
#include <GL/glew.h>
17
#include <GL/glew.h>
18
 
18
 
19
#include <GLGraphics/Console.h>
19
#include <GLGraphics/Console.h>
20
 
20
 
21
 
21
 
22
#include <CGLA/eigensolution.h>
22
#include <CGLA/eigensolution.h>
23
#include <CGLA/Vec2d.h>
23
#include <CGLA/Vec2d.h>
24
#include <CGLA/Vec3d.h>
24
#include <CGLA/Vec3d.h>
25
#include <CGLA/Mat3x3d.h>
25
#include <CGLA/Mat3x3d.h>
26
#include <CGLA/Mat2x2d.h>
26
#include <CGLA/Mat2x2d.h>
27
#include <CGLA/Mat2x3d.h>
27
#include <CGLA/Mat2x3d.h>
28
 
28
 
29
#include <LinAlg/Matrix.h>
29
#include <LinAlg/Matrix.h>
30
#include <LinAlg/Vector.h>
30
#include <LinAlg/Vector.h>
31
#include <LinAlg/LapackFunc.h>
31
#include <LinAlg/LapackFunc.h>
32
 
32
 
33
#include <GLGraphics/gel_glut.h>
33
#include <GLGraphics/gel_glut.h>
34
 
34
 
35
#include <HMesh/Manifold.h>
35
#include <HMesh/Manifold.h>
36
#include <HMesh/AttributeVector.h>
36
#include <HMesh/AttributeVector.h>
37
#include <HMesh/mesh_optimization.h>
37
#include <HMesh/mesh_optimization.h>
38
#include <HMesh/curvature.h>
38
#include <HMesh/curvature.h>
39
#include <HMesh/triangulate.h>
39
#include <HMesh/triangulate.h>
40
#include <HMesh/flatten.h>
40
#include <HMesh/flatten.h>
41
#include <HMesh/dual.h>
41
#include <HMesh/dual.h>
42
#include <HMesh/load.h>
42
#include <HMesh/load.h>
43
#include <HMesh/quadric_simplify.h>
43
#include <HMesh/quadric_simplify.h>
44
#include <HMesh/smooth.h>
44
#include <HMesh/smooth.h>
45
#include <HMesh/x3d_save.h>
45
#include <HMesh/x3d_save.h>
46
#include <HMesh/obj_save.h>
46
#include <HMesh/obj_save.h>
47
#include <HMesh/off_save.h>
47
#include <HMesh/off_save.h>
48
#include <HMesh/mesh_optimization.h>
48
#include <HMesh/mesh_optimization.h>
49
#include <HMesh/triangulate.h>
49
#include <HMesh/triangulate.h>
50
#include <HMesh/close_holes.h>
50
#include <HMesh/cleanup.h>
51
#include <HMesh/caps_and_needles.h>
51
#include <HMesh/cleanup.h>
52
#include <HMesh/refine_edges.h>
52
#include <HMesh/refine_edges.h>
53
#include <HMesh/subdivision.h>
53
#include <HMesh/subdivision.h>
54
 
54
 
55
#include <Util/Timer.h>
55
#include <Util/Timer.h>
56
#include <Util/ArgExtracter.h>
56
#include <Util/ArgExtracter.h>
57
 
57
 
58
#include "polarize.h"
58
#include "polarize.h"
59
#include "harmonics.h"
59
#include "harmonics.h"
60
#include "VisObj.h"
60
#include "VisObj.h"
61
 
61
 
62
using namespace std;
62
using namespace std;
63
using namespace HMesh;
63
using namespace HMesh;
64
using namespace Geometry;
64
using namespace Geometry;
65
using namespace GLGraphics;
65
using namespace GLGraphics;
66
using namespace CGLA;
66
using namespace CGLA;
67
using namespace Util;
67
using namespace Util;
68
using namespace LinAlg;
68
using namespace LinAlg;
69
 
69
 
70
// Single global instance so glut can get access
70
// Single global instance so glut can get access
71
Console theConsole;
71
Console theConsole;
72
bool console_visible = false;
72
bool console_visible = false;
73
 
73
 
74
 
74
 
75
inline VisObj& get_vis_obj(int i)
75
inline VisObj& get_vis_obj(int i)
76
{
76
{
77
    static VisObj vo[9];
77
    static VisObj vo[9];
78
    return vo[i];
78
    return vo[i];
79
}
79
}
80
 
80
 
81
Console::variable<int> active(0);
81
Console::variable<int> active(0);
82
 
82
 
83
inline VisObj& avo()
83
inline VisObj& avo()
84
{
84
{
85
    return get_vis_obj(active);
85
    return get_vis_obj(active);
86
}
86
}
87
 
87
 
88
inline Manifold& active_mesh()
88
inline Manifold& active_mesh()
89
{
89
{
90
    return avo().mesh();
90
    return avo().mesh();
91
}
91
}
92
 
92
 
93
inline GLViewController& active_view_control()
93
inline GLViewController& active_view_control()
94
{
94
{
95
    return avo().view_control();
95
    return avo().view_control();
96
}
96
}
97
 
97
 
98
 
98
 
99
 
99
 
100
////////////////////////////////////////////////////////////////////////////////
100
////////////////////////////////////////////////////////////////////////////////
101
bool MyConsoleHelp(const std::vector<std::string> & args)
101
bool MyConsoleHelp(const std::vector<std::string> & args)
102
{
102
{
103
    theConsole.printf("");
103
    theConsole.printf("");
104
    theConsole.printf("----------------- HELP -----------------");
104
    theConsole.printf("----------------- HELP -----------------");
105
    theConsole.printf("Press ESC key to open and close console");
105
    theConsole.printf("Press ESC key to open and close console");
106
    theConsole.printf("Press TAB to see the available commands and functions");
106
    theConsole.printf("Press TAB to see the available commands and functions");
107
    theConsole.printf("Functions are shown in green and variables in yellow");
107
    theConsole.printf("Functions are shown in green and variables in yellow");
108
    theConsole.printf("Setting a value: [command] = value");
108
    theConsole.printf("Setting a value: [command] = value");
109
    theConsole.printf("Getting a value: [command]");
109
    theConsole.printf("Getting a value: [command]");
110
    theConsole.printf("Functions: [function] [arg1] [arg2] ...");
110
    theConsole.printf("Functions: [function] [arg1] [arg2] ...");
111
    theConsole.printf("Entering arg1=? or arg1=help will give a description.");
111
    theConsole.printf("Entering arg1=? or arg1=help will give a description.");
112
    theConsole.printf("History: Up and Down arrow keys move through history.");
112
    theConsole.printf("History: Up and Down arrow keys move through history.");
113
    theConsole.printf("Tab Completion: TAB does tab completion and makes suggestions.");
113
    theConsole.printf("Tab Completion: TAB does tab completion and makes suggestions.");
114
    theConsole.printf("");
114
    theConsole.printf("");
115
    theConsole.printf("Keyboard commands (when console is not active):");
115
    theConsole.printf("Keyboard commands (when console is not active):");
116
    theConsole.printf("w   : switch to display.render_mode = wireframe");
116
    theConsole.printf("w   : switch to display.render_mode = wireframe");
117
    theConsole.printf("i   : switch to display.render_mode = isophotes");
117
    theConsole.printf("i   : switch to display.render_mode = isophotes");
118
    theConsole.printf("r   : switch to display.render_mode = reflection");
118
    theConsole.printf("r   : switch to display.render_mode = reflection");
119
    theConsole.printf("m   : switch to display.render_mode = metallic");
119
    theConsole.printf("m   : switch to display.render_mode = metallic");
120
    theConsole.printf("g   : switch to display.render_mode = glazed");
120
    theConsole.printf("g   : switch to display.render_mode = glazed");
121
    theConsole.printf("n   : switch to display.render_mode = normal");
121
    theConsole.printf("n   : switch to display.render_mode = normal");
122
    theConsole.printf("h   : switch to display.render_mode = harmonics");
122
    theConsole.printf("h   : switch to display.render_mode = harmonics");
123
    theConsole.printf("f   : toggle smooth/flat shading");
123
    theConsole.printf("f   : toggle smooth/flat shading");
124
    theConsole.printf("1-9 : switch between active meshes.");
124
    theConsole.printf("1-9 : switch between active meshes.");
125
    theConsole.printf("d   : (display.render_mode = harmonics) diffuse light on and off");
125
    theConsole.printf("d   : (display.render_mode = harmonics) diffuse light on and off");
126
    theConsole.printf("h   : (display.render_mode = harmonics) highlight on and off ");
126
    theConsole.printf("h   : (display.render_mode = harmonics) highlight on and off ");
127
    theConsole.printf("+/- : (display.render_mode = harmonics) which eigenvector to show");
127
    theConsole.printf("+/- : (display.render_mode = harmonics) which eigenvector to show");
128
    theConsole.printf("q   : quit program");
128
    theConsole.printf("q   : quit program");
129
    theConsole.printf("ESC : open console");
129
    theConsole.printf("ESC : open console");
130
    theConsole.printf("");
130
    theConsole.printf("");
131
    theConsole.printf("Mouse: Left button rotates, middle zooms, right pans");
131
    theConsole.printf("Mouse: Left button rotates, middle zooms, right pans");
132
    theConsole.printf("----------------- HELP -----------------");
132
    theConsole.printf("----------------- HELP -----------------");
133
    theConsole.printf("");
133
    theConsole.printf("");
134
    return true;
134
    return true;
135
}
135
}
136
 
136
 
137
bool wantshelp(const std::vector<std::string> & args)
137
bool wantshelp(const std::vector<std::string> & args)
138
{
138
{
139
    if(args.size() == 0) 
139
    if(args.size() == 0) 
140
        return false;
140
        return false;
141
	
141
	
142
    string str = args[0];
142
    string str = args[0];
143
	
143
	
144
    if(str=="help" || str=="HELP" || str=="Help" || str=="?") 
144
    if(str=="help" || str=="HELP" || str=="Help" || str=="?") 
145
        return true;
145
        return true;
146
	
146
	
147
    return false;
147
    return false;
148
}
148
}
149
 
149
 
150
/// Function that aligns two meshes.
150
/// Function that aligns two meshes.
151
void console_align(const std::vector<std::string> & args)
151
void console_align(const std::vector<std::string> & args)
152
{
152
{
153
    if(wantshelp(args)) {
153
    if(wantshelp(args)) {
154
        theConsole.printf("usage: align <dest> <src>");
154
        theConsole.printf("usage: align <dest> <src>");
155
        theConsole.printf("This function aligns dest mesh with src");
155
        theConsole.printf("This function aligns dest mesh with src");
156
        theConsole.printf("In practice the GLViewController of src is copied to dst.");
156
        theConsole.printf("In practice the GLViewController of src is copied to dst.");
157
        theConsole.printf("both arguments are mandatory and must be numbers between 1 and 9.");
157
        theConsole.printf("both arguments are mandatory and must be numbers between 1 and 9.");
158
        theConsole.printf("Note that results might be unexpexted if the meshes are not on the same scale");
158
        theConsole.printf("Note that results might be unexpexted if the meshes are not on the same scale");
159
    }
159
    }
160
	
160
	
161
    int dest = 0;
161
    int dest = 0;
162
	
162
	
163
    if(args.size()>0){
163
    if(args.size()>0){
164
        istringstream a0(args[0]);
164
        istringstream a0(args[0]);
165
        a0 >> dest;
165
        a0 >> dest;
166
        --dest;
166
        --dest;
167
		
167
		
168
        if(dest <0 || dest>8)
168
        if(dest <0 || dest>8)
169
        {
169
        {
170
            theConsole.printf("dest mesh out of range (1-9)");
170
            theConsole.printf("dest mesh out of range (1-9)");
171
            return;
171
            return;
172
        }
172
        }
173
    }
173
    }
174
    else
174
    else
175
    {
175
    {
176
        theConsole.printf("neither source nor destination mesh?!");
176
        theConsole.printf("neither source nor destination mesh?!");
177
        return;
177
        return;
178
    }
178
    }
179
	
179
	
180
    int src = 0;
180
    int src = 0;
181
    if(args.size()>1){
181
    if(args.size()>1){
182
        istringstream a1(args[1]);
182
        istringstream a1(args[1]);
183
        a1 >> src;
183
        a1 >> src;
184
        --src;
184
        --src;
185
		
185
		
186
        if(src <0 || src>8)
186
        if(src <0 || src>8)
187
        {
187
        {
188
            theConsole.printf("src mesh out of range (1-9)");
188
            theConsole.printf("src mesh out of range (1-9)");
189
            return;
189
            return;
190
        }
190
        }
191
    }
191
    }
192
    else
192
    else
193
    {
193
    {
194
        theConsole.printf("no src mesh?");
194
        theConsole.printf("no src mesh?");
195
        return;
195
        return;
196
    }
196
    }
197
    get_vis_obj(dest).view_control() = get_vis_obj(src).view_control();
197
    get_vis_obj(dest).view_control() = get_vis_obj(src).view_control();
198
}
198
}
199
 
199
 
200
void console_polarize(const std::vector<std::string> & args)
200
void console_polarize(const std::vector<std::string> & args)
201
{
201
{
202
    if(wantshelp(args)) {
202
    if(wantshelp(args)) {
203
        theConsole.printf("usage: polarize");
203
        theConsole.printf("usage: polarize");
204
        return;
204
        return;
205
    }
205
    }
206
    int divisions = 10;
206
    int divisions = 10;
207
	
207
	
208
    if(args.size() > 0){
208
    if(args.size() > 0){
209
        istringstream a0(args[0]);
209
        istringstream a0(args[0]);
210
        a0 >> divisions;
210
        a0 >> divisions;
211
    }
211
    }
212
 
212
 
213
    avo().save_old();
213
    avo().save_old();
214
 
214
 
215
	double vmin, vmax;
215
	double vmin, vmax;
216
    VertexAttributeVector<double> fun;
216
    VertexAttributeVector<double> fun;
217
    VertexAttributeVector<double> par;
217
    VertexAttributeVector<double> par;
218
    make_height_fun(active_mesh(), fun, vmin, vmax);
218
    make_height_fun(active_mesh(), fun, vmin, vmax);
219
    polarize_mesh(active_mesh(), fun, vmin, vmax, divisions, par);
219
    polarize_mesh(active_mesh(), fun, vmin, vmax, divisions, par);
220
}
220
}
221
 
221
 
222
void console_flatten(const std::vector<std::string> & args)
222
void console_flatten(const std::vector<std::string> & args)
223
{
223
{
224
    if(wantshelp(args)) {
224
    if(wantshelp(args)) {
225
        theConsole.printf("usage: flatten <floater|harmonic|barycentric>");
225
        theConsole.printf("usage: flatten <floater|harmonic|barycentric>");
226
        theConsole.printf("This function flattens a meshs with a simple boundary. It is mostly for showing mesh");
226
        theConsole.printf("This function flattens a meshs with a simple boundary. It is mostly for showing mesh");
227
        theConsole.printf("parametrization methods. The current mesh MUST have a SINGLE boundary loop");
227
        theConsole.printf("parametrization methods. The current mesh MUST have a SINGLE boundary loop");
228
        theConsole.printf("This loop is mapped to the unit circle in a regular fashion (equal angle intervals).");
228
        theConsole.printf("This loop is mapped to the unit circle in a regular fashion (equal angle intervals).");
229
        theConsole.printf("All non boundary vertices are placed at the origin. Then the system is relaxed iteratively");
229
        theConsole.printf("All non boundary vertices are placed at the origin. Then the system is relaxed iteratively");
230
        theConsole.printf("using the weight scheme given as argument.");
230
        theConsole.printf("using the weight scheme given as argument.");
231
        return;
231
        return;
232
    }
232
    }
233
	
233
	
234
    avo().save_old();
234
    avo().save_old();
235
	
235
	
236
    WeightScheme ws = BARYCENTRIC_W;
236
    WeightScheme ws = BARYCENTRIC_W;
237
    if(args.size()>0){
237
    if(args.size()>0){
238
        if(args[0] == "floater")
238
        if(args[0] == "floater")
239
            ws = FLOATER_W;
239
            ws = FLOATER_W;
240
        else if(args[0] == "harmonic")
240
        else if(args[0] == "harmonic")
241
            ws = HARMONIC_W;
241
            ws = HARMONIC_W;
242
        else if(args[0] == "lscm")
242
        else if(args[0] == "lscm")
243
            ws = LSCM_W;
243
            ws = LSCM_W;
244
    }
244
    }
245
    else
245
    else
246
        return;
246
        return;
247
	
247
	
248
    flatten(active_mesh(), ws);
248
    flatten(active_mesh(), ws);
249
	
249
	
250
    return;
250
    return;
251
}
251
}
252
 
252
 
253
void console_save(const std::vector<std::string> & args)
253
void console_save(const std::vector<std::string> & args)
254
{
254
{
255
    if(wantshelp(args)) {
255
    if(wantshelp(args)) {
256
        theConsole.printf("usage: save <name.x3d|name.obj> ");
256
        theConsole.printf("usage: save <name.x3d|name.obj> ");
257
		
257
		
258
        return;
258
        return;
259
    }
259
    }
260
    const string& file_name = args[0];
260
    const string& file_name = args[0];
261
    if(args.size() == 1){
261
    if(args.size() == 1){
262
        if(file_name.substr(file_name.length()-4,file_name.length())==".obj"){
262
        if(file_name.substr(file_name.length()-4,file_name.length())==".obj"){
263
            obj_save(file_name, active_mesh());
263
            obj_save(file_name, active_mesh());
264
			
264
			
265
            return;
265
            return;
266
        }
266
        }
267
        else if(file_name.substr(file_name.length()-4,file_name.length())==".off"){
267
        else if(file_name.substr(file_name.length()-4,file_name.length())==".off"){
268
            off_save(file_name, active_mesh());
268
            off_save(file_name, active_mesh());
269
			
269
			
270
            return;
270
            return;
271
        }
271
        }
272
        else if(file_name.substr(file_name.length()-4,file_name.length())==".x3d"){
272
        else if(file_name.substr(file_name.length()-4,file_name.length())==".x3d"){
273
            x3d_save(file_name, active_mesh());
273
            x3d_save(file_name, active_mesh());
274
			
274
			
275
            return;
275
            return;
276
        }
276
        }
277
        theConsole.printf("unknown format");
277
        theConsole.printf("unknown format");
278
        return; 
278
        return; 
279
    }
279
    }
280
    theConsole.printf("usage: save <name.x3d|name.obj> ");
280
    theConsole.printf("usage: save <name.x3d|name.obj> ");
281
}
281
}
282
 
282
 
283
 
283
 
284
void console_refine_edges(const std::vector<std::string> & args)
284
void console_refine_edges(const std::vector<std::string> & args)
285
{
285
{
286
    if(wantshelp(args)) {
286
    if(wantshelp(args)) {
287
        theConsole.printf("usage: refine.split_edges <length>");
287
        theConsole.printf("usage: refine.split_edges <length>");
288
        theConsole.printf("splits edges longer than <length>; default is 0.5 times average length");
288
        theConsole.printf("splits edges longer than <length>; default is 0.5 times average length");
289
        return;
289
        return;
290
    }
290
    }
291
	
291
	
292
    avo().save_old();
292
    avo().save_old();
293
	
293
	
294
    float thresh = 0.5f;
294
    float thresh = 0.5f;
295
	
295
	
296
    if(args.size() > 0){
296
    if(args.size() > 0){
297
        istringstream a0(args[0]);
297
        istringstream a0(args[0]);
298
        a0 >> thresh;
298
        a0 >> thresh;
299
    }
299
    }
300
	
300
	
301
    float avg_length = average_edge_length(active_mesh());
301
    float avg_length = average_edge_length(active_mesh());
302
	
302
	
303
    refine_edges(active_mesh(), thresh * avg_length);
303
    refine_edges(active_mesh(), thresh * avg_length);
304
	
304
	
305
    return;
305
    return;
306
	
306
	
307
}
307
}
308
 
308
 
309
void console_refine_faces(const std::vector<std::string> & args)
309
void console_refine_faces(const std::vector<std::string> & args)
310
{
310
{
311
    if(wantshelp(args)) {
311
    if(wantshelp(args)) {
312
        theConsole.printf("usage: refine.split_faces ");
312
        theConsole.printf("usage: refine.split_faces ");
313
        theConsole.printf("usage:  Takes no arguments. Inserts a vertex at the centre of each face.");
313
        theConsole.printf("usage:  Takes no arguments. Inserts a vertex at the centre of each face.");
314
		
314
		
315
        return;
315
        return;
316
    }
316
    }
317
    avo().save_old();
317
    avo().save_old();
318
	
318
	
319
    triangulate_by_vertex_face_split(active_mesh());
319
    triangulate_by_vertex_face_split(active_mesh());
320
	
320
	
321
    return;
321
    return;
322
	
322
	
323
}
323
}
324
 
324
 
325
void console_cc_subdivide(const std::vector<std::string> & args)
325
void console_cc_subdivide(const std::vector<std::string> & args)
326
{
326
{
327
    if(wantshelp(args)) {
327
    if(wantshelp(args)) {
328
        theConsole.printf("usage: refine.catmull_clark ");
328
        theConsole.printf("usage: refine.catmull_clark ");
329
        theConsole.printf("Does one step of Catmull-Clark subdivision");
329
        theConsole.printf("Does one step of Catmull-Clark subdivision");
330
		
330
		
331
        return;
331
        return;
332
    }
332
    }
333
    avo().save_old();
333
    avo().save_old();
334
	
334
	
335
    cc_split(active_mesh(),active_mesh());
335
    cc_split(active_mesh(),active_mesh());
336
    cc_smooth(active_mesh());
336
    cc_smooth(active_mesh());
337
	
337
	
338
    return;
338
    return;
339
}
339
}
340
 
340
 
341
void console_doosabin_subdivide(const std::vector<std::string> & args)
341
void console_doosabin_subdivide(const std::vector<std::string> & args)
342
{
342
{
343
    if(wantshelp(args)) {
343
    if(wantshelp(args)) {
344
        theConsole.printf("usage: refine.doo_sabin ");
344
        theConsole.printf("usage: refine.doo_sabin ");
345
        theConsole.printf("Does one step of Doo-Sabin Subdivision");
345
        theConsole.printf("Does one step of Doo-Sabin Subdivision");
346
		
346
		
347
        return;
347
        return;
348
    }
348
    }
349
    avo().save_old();
349
    avo().save_old();
350
	
350
	
351
    cc_split(active_mesh(),active_mesh());
351
    cc_split(active_mesh(),active_mesh());
352
    dual(active_mesh());
352
    dual(active_mesh());
353
    
353
    
354
    return;
354
    return;
355
}
355
}
356
 
356
 
357
void console_dual(const std::vector<std::string> & args)
357
void console_dual(const std::vector<std::string> & args)
358
{
358
{
359
    if(wantshelp(args)) 
359
    if(wantshelp(args)) 
360
    {
360
    {
361
        theConsole.printf("usage: dual ");
361
        theConsole.printf("usage: dual ");
362
        theConsole.printf("Produces the dual by converting each face to a vertex placed at the barycenter.");
362
        theConsole.printf("Produces the dual by converting each face to a vertex placed at the barycenter.");
363
        return;
363
        return;
364
    }
364
    }
365
    avo().save_old();
365
    avo().save_old();
366
	
366
	
367
    dual(active_mesh());
367
    dual(active_mesh());
368
	
368
	
369
    return;
369
    return;
370
}
370
}
371
 
371
 
372
 
372
 
373
void console_minimize_curvature(const std::vector<std::string> & args)
373
void console_minimize_curvature(const std::vector<std::string> & args)
374
{
374
{
375
    if(wantshelp(args)) 
375
    if(wantshelp(args)) 
376
    {
376
    {
377
        theConsole.printf("usage: optimize.minimize_curvature <anneal>");
377
        theConsole.printf("usage: optimize.minimize_curvature <anneal>");
378
        theConsole.printf("Flip edges to minimize mean curvature.");
378
        theConsole.printf("Flip edges to minimize mean curvature.");
379
        theConsole.printf("If anneal is true, simulated annealing (slow) is used rather than a greedy scheme");
379
        theConsole.printf("If anneal is true, simulated annealing (slow) is used rather than a greedy scheme");
380
        return;
380
        return;
381
    }
381
    }
382
    avo().save_old();
382
    avo().save_old();
383
	
383
	
384
    bool anneal=false;
384
    bool anneal=false;
385
    if(args.size() > 0)
385
    if(args.size() > 0)
386
    {
386
    {
387
        istringstream a0(args[0]);
387
        istringstream a0(args[0]);
388
        a0 >> anneal;
388
        a0 >> anneal;
389
    }
389
    }
390
	
390
	
391
    minimize_curvature(active_mesh(), anneal);
391
    minimize_curvature(active_mesh(), anneal);
392
    avo().post_create_display_list();
392
    avo().post_create_display_list();
393
    return;
393
    return;
394
}
394
}
395
 
395
 
396
void console_minimize_dihedral(const std::vector<std::string> & args)
396
void console_minimize_dihedral(const std::vector<std::string> & args)
397
{
397
{
398
    if(wantshelp(args))
398
    if(wantshelp(args))
399
    {
399
    {
400
        theConsole.printf("usage: optimize.minimize_dihedral <iter> <anneal> <use_alpha> <gamma> ");
400
        theConsole.printf("usage: optimize.minimize_dihedral <iter> <anneal> <use_alpha> <gamma> ");
401
        theConsole.printf("Flip edges to minimize dihedral angles.");
401
        theConsole.printf("Flip edges to minimize dihedral angles.");
402
        theConsole.printf("Iter is the max number of iterations. anneal tells us whether to use ");
402
        theConsole.printf("Iter is the max number of iterations. anneal tells us whether to use ");
403
        theConsole.printf("simulated annealing and not greedy optimization. use_alpha (default=true) ");
403
        theConsole.printf("simulated annealing and not greedy optimization. use_alpha (default=true) ");
404
        theConsole.printf("means to use angle and not cosine of anglegamma (default=4) is the power ");
404
        theConsole.printf("means to use angle and not cosine of anglegamma (default=4) is the power ");
405
        theConsole.printf("to which we raise the dihedral angle");
405
        theConsole.printf("to which we raise the dihedral angle");
406
        return;
406
        return;
407
    }
407
    }
408
    avo().save_old();
408
    avo().save_old();
409
	
409
	
410
    int iter = 1000;
410
    int iter = 1000;
411
    if(args.size() > 0)
411
    if(args.size() > 0)
412
    {
412
    {
413
        istringstream a0(args[0]);
413
        istringstream a0(args[0]);
414
        a0 >> iter;
414
        a0 >> iter;
415
    }
415
    }
416
	
416
	
417
    bool anneal = false;
417
    bool anneal = false;
418
    if(args.size() > 1)
418
    if(args.size() > 1)
419
    {
419
    {
420
        istringstream a0(args[1]);
420
        istringstream a0(args[1]);
421
        a0 >> anneal;
421
        a0 >> anneal;
422
    }
422
    }
423
	
423
	
424
    bool use_alpha = true;
424
    bool use_alpha = true;
425
    if(args.size() > 2)
425
    if(args.size() > 2)
426
    {
426
    {
427
        istringstream a0(args[2]);
427
        istringstream a0(args[2]);
428
        a0 >> use_alpha;
428
        a0 >> use_alpha;
429
    }
429
    }
430
	
430
	
431
    float gamma = 4.0f;
431
    float gamma = 4.0f;
432
    if(args.size() > 3)
432
    if(args.size() > 3)
433
    {
433
    {
434
        istringstream a0(args[3]);
434
        istringstream a0(args[3]);
435
        a0 >> gamma;
435
        a0 >> gamma;
436
    }
436
    }
437
	
437
	
438
	
438
	
439
    minimize_dihedral_angle(active_mesh(), iter, anneal, use_alpha, gamma);
439
    minimize_dihedral_angle(active_mesh(), iter, anneal, use_alpha, gamma);
440
    return;
440
    return;
441
}
441
}
442
 
442
 
443
void console_maximize_min_angle(const std::vector<std::string> & args)
443
void console_maximize_min_angle(const std::vector<std::string> & args)
444
{
444
{
445
    if(wantshelp(args)) 
445
    if(wantshelp(args)) 
446
    {
446
    {
447
        theConsole.printf("usage: optimize.maximize_min_angle <thresh> <anneal>");
447
        theConsole.printf("usage: optimize.maximize_min_angle <thresh> <anneal>");
448
        theConsole.printf("Flip edges to maximize min angle - to make mesh more Delaunay.");
448
        theConsole.printf("Flip edges to maximize min angle - to make mesh more Delaunay.");
449
        theConsole.printf("If the dot product of the normals between adjacent faces < thresh");
449
        theConsole.printf("If the dot product of the normals between adjacent faces < thresh");
450
        theConsole.printf("no flip will be made. anneal selects simulated annealing rather ");
450
        theConsole.printf("no flip will be made. anneal selects simulated annealing rather ");
451
        theConsole.printf("nthan greedy optimization.");
451
        theConsole.printf("nthan greedy optimization.");
452
        return;
452
        return;
453
    }
453
    }
454
    avo().save_old();
454
    avo().save_old();
455
	
455
	
456
    float thresh = 0.0f;
456
    float thresh = 0.0f;
457
    if(args.size() > 0)
457
    if(args.size() > 0)
458
    {
458
    {
459
        istringstream a0(args[0]);
459
        istringstream a0(args[0]);
460
        a0 >> thresh;
460
        a0 >> thresh;
461
    }
461
    }
462
    bool anneal = false;
462
    bool anneal = false;
463
    if(args.size() > 1)
463
    if(args.size() > 1)
464
    {
464
    {
465
        istringstream a0(args[1]);
465
        istringstream a0(args[1]);
466
        a0 >> anneal;
466
        a0 >> anneal;
467
    }
467
    }
468
    maximize_min_angle(active_mesh(),thresh,anneal);
468
    maximize_min_angle(active_mesh(),thresh,anneal);
469
    return;
469
    return;
470
}
470
}
471
 
471
 
472
 
472
 
473
void console_optimize_valency(const std::vector<std::string> & args)
473
void console_optimize_valency(const std::vector<std::string> & args)
474
{
474
{
475
    if(wantshelp(args)) 
475
    if(wantshelp(args)) 
476
    {
476
    {
477
        theConsole.printf("usage: optimize.valency <anneal> ");
477
        theConsole.printf("usage: optimize.valency <anneal> ");
478
        theConsole.printf("Optimizes valency for triangle meshes. Anneal selects simulated annealing rather than greedy optim.");
478
        theConsole.printf("Optimizes valency for triangle meshes. Anneal selects simulated annealing rather than greedy optim.");
479
        return;
479
        return;
480
    }
480
    }
481
    avo().save_old();
481
    avo().save_old();
482
	
482
	
483
    bool anneal = false;
483
    bool anneal = false;
484
    if(args.size() > 0)
484
    if(args.size() > 0)
485
    {
485
    {
486
        istringstream a0(args[0]);
486
        istringstream a0(args[0]);
487
        a0 >> anneal;
487
        a0 >> anneal;
488
    }
488
    }
489
    optimize_valency(active_mesh(), anneal);
489
    optimize_valency(active_mesh(), anneal);
490
    return;
490
    return;
491
}
491
}
492
 
492
 
493
void console_analyze(const std::vector<std::string> & args)
493
void console_analyze(const std::vector<std::string> & args)
494
{
494
{
495
    if(wantshelp(args)) 
495
    if(wantshelp(args)) 
496
    {
496
    {
497
        theConsole.printf("usage:  harmonics.analyze");
497
        theConsole.printf("usage:  harmonics.analyze");
498
        theConsole.printf("Creates the Laplace Beltrami operator for the mesh and finds all eigensolutions.");
498
        theConsole.printf("Creates the Laplace Beltrami operator for the mesh and finds all eigensolutions.");
499
        theConsole.printf("It also projects the vertices onto the eigenvectors - thus transforming the mesh");
499
        theConsole.printf("It also projects the vertices onto the eigenvectors - thus transforming the mesh");
500
        theConsole.printf("to this basis.");
500
        theConsole.printf("to this basis.");
501
        theConsole.printf("Note that this will stall the computer for a large mesh - as long as we use Lapack.");
501
        theConsole.printf("Note that this will stall the computer for a large mesh - as long as we use Lapack.");
502
        return;
502
        return;
503
    }
503
    }
504
    avo().harmonics_analyze_mesh();
504
    avo().harmonics_analyze_mesh();
505
    return;
505
    return;
506
}
506
}
507
 
507
 
508
 
508
 
509
void console_partial_reconstruct(const std::vector<std::string> & args)
509
void console_partial_reconstruct(const std::vector<std::string> & args)
510
{
510
{
511
    if(args.size() != 3)
511
    if(args.size() != 3)
512
        theConsole.printf("usage: haramonics.partial_reconstruct <e0> <e1> <s>");
512
        theConsole.printf("usage: haramonics.partial_reconstruct <e0> <e1> <s>");
513
	
513
	
514
    if(wantshelp(args)) {
514
    if(wantshelp(args)) {
515
        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");
516
        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 ");
517
        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:");
518
        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;");
519
        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");
520
        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.");
521
        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.");
522
    }
522
    }
523
    avo().save_old();
523
    avo().save_old();
524
	
524
	
525
    if(args.size() != 3)
525
    if(args.size() != 3)
526
        return;
526
        return;
527
	
527
	
528
    int E0,E1;
528
    int E0,E1;
529
    float scale;
529
    float scale;
530
    istringstream a0(args[0]);
530
    istringstream a0(args[0]);
531
    a0 >> E0;
531
    a0 >> E0;
532
    istringstream a1(args[1]);
532
    istringstream a1(args[1]);
533
    a1 >> E1;
533
    a1 >> E1;
534
    istringstream a2(args[2]);
534
    istringstream a2(args[2]);
535
    a2 >> scale;
535
    a2 >> scale;
536
    avo().harmonics_partial_reconstruct(E0,E1,scale);
536
    avo().harmonics_partial_reconstruct(E0,E1,scale);
537
    return;
537
    return;
538
}
538
}
539
 
539
 
540
void console_reset_shape(const std::vector<std::string> & args)
540
void console_reset_shape(const std::vector<std::string> & args)
541
{
541
{
542
    if(wantshelp(args)) 
542
    if(wantshelp(args)) 
543
    {
543
    {
544
        theConsole.printf("usage: harmonics.reset_shape ");
544
        theConsole.printf("usage: harmonics.reset_shape ");
545
        theConsole.printf("Simply sets all vertices to 0,0,0. Call this before doing partial_reconstruct");
545
        theConsole.printf("Simply sets all vertices to 0,0,0. Call this before doing partial_reconstruct");
546
        theConsole.printf("unless you know what you are doing.");
546
        theConsole.printf("unless you know what you are doing.");
547
        return;
547
        return;
548
    }
548
    }
549
    avo().save_old();
549
    avo().save_old();
550
    avo().harmonics_reset_shape();
550
    avo().harmonics_reset_shape();
551
    return;
551
    return;
552
}
552
}
553
 
553
 
554
 
554
 
555
void console_close_holes(const std::vector<std::string> & args)
555
void console_close_holes(const std::vector<std::string> & args)
556
{
556
{
557
    if(wantshelp(args)) 
557
    if(wantshelp(args)) 
558
    {
558
    {
559
        theConsole.printf("usage: cleanup.close_holes");
559
        theConsole.printf("usage: cleanup.close_holes");
560
        theConsole.printf("This function closes holes. It simply follows the loop of halfvectors which");
560
        theConsole.printf("This function closes holes. It simply follows the loop of halfvectors which");
561
        theConsole.printf("enclose the hole and add a face to which they all point.");
561
        theConsole.printf("enclose the hole and add a face to which they all point.");
562
        return;
562
        return;
563
    }
563
    }
564
    avo().save_old();
564
    avo().save_old();
565
	
565
	
566
    close_holes(active_mesh());
566
    close_holes(active_mesh());
567
    return;
567
    return;
568
}
568
}
569
 
569
 
570
void console_reload(const std::vector<std::string> & args)
570
void console_reload(const std::vector<std::string> & args)
571
{
571
{
572
    if(wantshelp(args)) 
572
    if(wantshelp(args)) 
573
    {
573
    {
574
        theConsole.printf("usage:  load <file>");
574
        theConsole.printf("usage:  load <file>");
575
        theConsole.printf("(Re)loads the current file if no argument is given, but");
575
        theConsole.printf("(Re)loads the current file if no argument is given, but");
576
        theConsole.printf("if an argument is given, then that becomes the current file");
576
        theConsole.printf("if an argument is given, then that becomes the current file");
577
        return;
577
        return;
578
    }
578
    }
579
    avo().save_old();
579
    avo().save_old();
580
	
580
	
581
    if(!avo().reload(args.size() > 0 ? args[0]:""))
581
    if(!avo().reload(args.size() > 0 ? args[0]:""))
582
        theConsole.printf("failed to load");
582
        theConsole.printf("failed to load");
583
	
583
	
584
    return;
584
    return;
585
}
585
}
586
 
586
 
587
 
587
 
588
void console_add_mesh(const std::vector<std::string> & args)
588
void console_add_mesh(const std::vector<std::string> & args)
589
{
589
{
590
    if(wantshelp(args)) 
590
    if(wantshelp(args)) 
591
    {
591
    {
592
        theConsole.printf("usage:  add_mesh <file>");
592
        theConsole.printf("usage:  add_mesh <file>");
593
        theConsole.printf("Loads the file but without clearing the mesh. Thus, the loaded mesh is added to the");
593
        theConsole.printf("Loads the file but without clearing the mesh. Thus, the loaded mesh is added to the");
594
        theConsole.printf("current model.");
594
        theConsole.printf("current model.");
595
        return;
595
        return;
596
    }
596
    }
597
    avo().save_old();
597
    avo().save_old();
598
	
598
	
599
    if(!avo().add_mesh(args.size() > 0 ? args[0]:""))
599
    if(!avo().add_mesh(args.size() > 0 ? args[0]:""))
600
        theConsole.printf("failed to load");
600
        theConsole.printf("failed to load");
601
	
601
	
602
    return;
602
    return;
603
}
603
}
604
void console_valid(const std::vector<std::string> & args)
604
void console_valid(const std::vector<std::string> & args)
605
{
605
{
606
    if(wantshelp(args)) 
606
    if(wantshelp(args)) 
607
    {
607
    {
608
        theConsole.printf("usage:  validity");
608
        theConsole.printf("usage:  validity");
609
        theConsole.printf("Tests validity of Manifold");
609
        theConsole.printf("Tests validity of Manifold");
610
        return;
610
        return;
611
    }
611
    }
612
	if(valid(active_mesh()))
612
	if(valid(active_mesh()))
613
		theConsole.printf("Mesh is valid");
613
		theConsole.printf("Mesh is valid");
614
	else
614
	else
615
		theConsole.printf("Mesh is invalid - check console output");
615
		theConsole.printf("Mesh is invalid - check console output");
616
	return;
616
	return;
617
}
617
}
618
 
618
 
619
void console_info(const std::vector<std::string> & args)
619
void console_info(const std::vector<std::string> & args)
620
{
620
{
621
    if(wantshelp(args)) 
621
    if(wantshelp(args)) 
622
    {
622
    {
623
        theConsole.printf("usage:  info");
623
        theConsole.printf("usage:  info");
624
        theConsole.printf("Provides information about mesh.");
624
        theConsole.printf("Provides information about mesh.");
625
        return;
625
        return;
626
    }
626
    }
627
    Vec3d p0, p7;
627
    Vec3d p0, p7;
628
    bbox(active_mesh(), p0, p7);
628
    bbox(active_mesh(), p0, p7);
629
    stringstream bbox_corners;
629
    stringstream bbox_corners;
630
    bbox_corners << p0 << " - " << p7 << endl;
630
    bbox_corners << p0 << " - " << p7 << endl;
631
	theConsole.printf("Bounding box corners : %s", bbox_corners.str().c_str());
631
	theConsole.printf("Bounding box corners : %s", bbox_corners.str().c_str());
632
    map<int,int> val_hist;
632
    map<int,int> val_hist;
633
    
633
    
634
    for(VertexIDIterator vi = active_mesh().vertices_begin(); vi != active_mesh().vertices_end(); ++vi)
634
    for(VertexIDIterator vi = active_mesh().vertices_begin(); vi != active_mesh().vertices_end(); ++vi)
635
    {
635
    {
636
        int val = valency(active_mesh(), *vi);
636
        int val = valency(active_mesh(), *vi);
637
        if(val_hist.find(val) == val_hist.end())
637
        if(val_hist.find(val) == val_hist.end())
638
            val_hist[val] = 0;
638
            val_hist[val] = 0;
639
        ++val_hist[val];
639
        ++val_hist[val];
640
    }
640
    }
641
    
641
    
642
    theConsole.printf("Valency histogam");
642
    theConsole.printf("Valency histogam");
643
    for(map<int,int>::iterator iter = val_hist.begin(); iter != val_hist.end(); ++iter)
643
    for(map<int,int>::iterator iter = val_hist.begin(); iter != val_hist.end(); ++iter)
644
    {
644
    {
645
        stringstream vhl;
645
        stringstream vhl;
646
        vhl << iter->first << ", " << iter->second;
646
        vhl << iter->first << ", " << iter->second;
647
        theConsole.printf("%d, %d", iter->first, iter->second);
647
        theConsole.printf("%d, %d", iter->first, iter->second);
648
    }
648
    }
649
 
649
 
650
	theConsole.printf("Mesh contains %d faces", active_mesh().no_faces());
650
	theConsole.printf("Mesh contains %d faces", active_mesh().no_faces());
651
	theConsole.printf("Mesh contains %d halfedges", active_mesh().no_halfedges());
651
	theConsole.printf("Mesh contains %d halfedges", active_mesh().no_halfedges());
652
	theConsole.printf("Mesh contains %d vertices", active_mesh().no_vertices());
652
	theConsole.printf("Mesh contains %d vertices", active_mesh().no_vertices());
653
	return;
653
	return;
654
}
654
}
655
 
655
 
656
 
656
 
657
void console_simplify(const std::vector<std::string> & args)
657
void console_simplify(const std::vector<std::string> & args)
658
{
658
{
659
    if(wantshelp(args)) 
659
    if(wantshelp(args)) 
660
    {
660
    {
661
        theConsole.printf("usage: simplify <fraction> ");
661
        theConsole.printf("usage: simplify <fraction> ");
662
        theConsole.printf("Performs Garland Heckbert (quadric based) mesh simplification.");
662
        theConsole.printf("Performs Garland Heckbert (quadric based) mesh simplification.");
663
        theConsole.printf("The only argument is the fraction of vertices to keep.");
663
        theConsole.printf("The only argument is the fraction of vertices to keep.");
664
        return;
664
        return;
665
    }
665
    }
666
    avo().save_old();
666
    avo().save_old();
667
	
667
	
668
    float keep_fraction;
668
    float keep_fraction;
669
    if(args.size() == 0)
669
    if(args.size() == 0)
670
    {
670
    {
671
        theConsole.print("you must specify fraction of vertices to keep");
671
        theConsole.print("you must specify fraction of vertices to keep");
672
        return;
672
        return;
673
    }
673
    }
674
    istringstream a0(args[0]);
674
    istringstream a0(args[0]);
675
    a0 >> keep_fraction;
675
    a0 >> keep_fraction;
676
	
676
	
677
    Vec3d p0, p7;
677
    Vec3d p0, p7;
678
    bbox(active_mesh(), p0, p7);
678
    bbox(active_mesh(), p0, p7);
679
    Vec3d d = p7-p0;
679
    Vec3d d = p7-p0;
680
    float s = 1.0/d.max_coord();
680
    float s = 1.0/d.max_coord();
681
    Vec3d pcentre = (p7+p0)/2.0;
681
    Vec3d pcentre = (p7+p0)/2.0;
682
    for(VertexIDIterator vi = active_mesh().vertices_begin(); vi != active_mesh().vertices_end(); ++vi){
682
    for(VertexIDIterator vi = active_mesh().vertices_begin(); vi != active_mesh().vertices_end(); ++vi){
683
        active_mesh().pos(*vi) = (active_mesh().pos(*vi) - pcentre) * s;
683
        active_mesh().pos(*vi) = (active_mesh().pos(*vi) - pcentre) * s;
684
    }
684
    }
685
    cout << "Timing the Garland Heckbert (quadric based) mesh simplication..." << endl;
685
    cout << "Timing the Garland Heckbert (quadric based) mesh simplication..." << endl;
686
    Timer timer;
686
    Timer timer;
687
    timer.start();
687
    timer.start();
688
	
688
	
689
    //simplify
689
    //simplify
690
    quadric_simplify(active_mesh(),keep_fraction,0.0001f,true);
690
    quadric_simplify(active_mesh(),keep_fraction,0.0001f,true);
691
	
691
	
692
    cout << "Simplification complete, process time: " << timer.get_secs() << " seconds" << endl;
692
    cout << "Simplification complete, process time: " << timer.get_secs() << " seconds" << endl;
693
	
693
	
694
    //clean up the mesh, a lot of edges were just collapsed 
694
    //clean up the mesh, a lot of edges were just collapsed 
695
    active_mesh().cleanup();
695
    active_mesh().cleanup();
696
	
696
	
697
    for(VertexIDIterator vi = active_mesh().vertices_begin(); vi != active_mesh().vertices_end(); ++vi)
697
    for(VertexIDIterator vi = active_mesh().vertices_begin(); vi != active_mesh().vertices_end(); ++vi)
698
        active_mesh().pos(*vi) = active_mesh().pos(*vi)*d.max_coord() + pcentre;
698
        active_mesh().pos(*vi) = active_mesh().pos(*vi)*d.max_coord() + pcentre;
699
    return;
699
    return;
700
}
700
}
701
 
701
 
702
void console_vertex_noise(const std::vector<std::string> & args)
702
void console_vertex_noise(const std::vector<std::string> & args)
703
{
703
{
704
    if(wantshelp(args)) 
704
    if(wantshelp(args)) 
705
    {
705
    {
706
        theConsole.printf("usage: noise.perturb_vertices <amplitude>");
706
        theConsole.printf("usage: noise.perturb_vertices <amplitude>");
707
        theConsole.printf("adds a random vector to each vertex. A random vector in the unit cube is generated and");
707
        theConsole.printf("adds a random vector to each vertex. A random vector in the unit cube is generated and");
708
        theConsole.printf("to ensure an isotropic distribution, vectors outside the unit ball are discarded.");
708
        theConsole.printf("to ensure an isotropic distribution, vectors outside the unit ball are discarded.");
709
        theConsole.printf("The vector is multiplied by the average edge length and then by the amplitude specified.");
709
        theConsole.printf("The vector is multiplied by the average edge length and then by the amplitude specified.");
710
        theConsole.printf("If no amplitude is specified, the default (0.5) is used.");
710
        theConsole.printf("If no amplitude is specified, the default (0.5) is used.");
711
        return;
711
        return;
712
    }
712
    }
713
    avo().save_old();
713
    avo().save_old();
714
	
714
	
715
    float avg_length = average_edge_length(active_mesh());
715
    float avg_length = average_edge_length(active_mesh());
716
	
716
	
717
    float noise_amplitude = 0.5f;
717
    float noise_amplitude = 0.5f;
718
    if(args.size() > 0) {
718
    if(args.size() > 0) {
719
        istringstream a0(args[0]);
719
        istringstream a0(args[0]);
720
        a0 >> noise_amplitude;
720
        a0 >> noise_amplitude;
721
    }
721
    }
722
	
722
	
723
    gel_srand(0);
723
    gel_srand(0);
724
    for(VertexIDIterator vi = active_mesh().vertices_begin(); vi != active_mesh().vertices_end(); ++vi){
724
    for(VertexIDIterator vi = active_mesh().vertices_begin(); vi != active_mesh().vertices_end(); ++vi){
725
        Vec3d v;
725
        Vec3d v;
726
        do{
726
        do{
727
            v = Vec3d(gel_rand(),gel_rand(),gel_rand());
727
            v = Vec3d(gel_rand(),gel_rand(),gel_rand());
728
            v /= (float)(GEL_RAND_MAX);
728
            v /= (float)(GEL_RAND_MAX);
729
        } 
729
        } 
730
        while(sqr_length(v) > 1.0);
730
        while(sqr_length(v) > 1.0);
731
		
731
		
732
        v -= Vec3d(0.5);
732
        v -= Vec3d(0.5);
733
        v *= 2.0;
733
        v *= 2.0;
734
        v *= noise_amplitude;
734
        v *= noise_amplitude;
735
        v *= avg_length;
735
        v *= avg_length;
736
        active_mesh().pos(*vi) += v;
736
        active_mesh().pos(*vi) += v;
737
    }		
737
    }		
738
    return;
738
    return;
739
}
739
}
740
 
740
 
741
void console_perpendicular_vertex_noise(const std::vector<std::string> & args)
741
void console_perpendicular_vertex_noise(const std::vector<std::string> & args)
742
{
742
{
743
    if(wantshelp(args)) {
743
    if(wantshelp(args)) {
744
        theConsole.printf("usage: noise.perturb_vertices_perpendicular <amplitude>");
744
        theConsole.printf("usage: noise.perturb_vertices_perpendicular <amplitude>");
745
        theConsole.printf("adds the normal times a random scalar times amplitude times");
745
        theConsole.printf("adds the normal times a random scalar times amplitude times");
746
        theConsole.printf("times average edge length to the vertex. (default amplitude=0.5)");
746
        theConsole.printf("times average edge length to the vertex. (default amplitude=0.5)");
747
        return;
747
        return;
748
    }
748
    }
749
    avo().save_old();
749
    avo().save_old();
750
	
750
	
751
    float avg_length = average_edge_length(active_mesh());
751
    float avg_length = average_edge_length(active_mesh());
752
	
752
	
753
    float noise_amplitude = 0.5;
753
    float noise_amplitude = 0.5;
754
    if(args.size() > 0) 
754
    if(args.size() > 0) 
755
    {
755
    {
756
        istringstream a0(args[0]);
756
        istringstream a0(args[0]);
757
        a0 >> noise_amplitude;
757
        a0 >> noise_amplitude;
758
    }
758
    }
759
	
759
	
760
    VertexAttributeVector<Vec3d> normals(active_mesh().allocated_vertices());
760
    VertexAttributeVector<Vec3d> normals(active_mesh().allocated_vertices());
761
    for(VertexIDIterator vi = active_mesh().vertices_begin(); vi != active_mesh().vertices_end(); ++vi)
761
    for(VertexIDIterator vi = active_mesh().vertices_begin(); vi != active_mesh().vertices_end(); ++vi)
762
        normals[*vi] = normal(active_mesh(), *vi);
762
        normals[*vi] = normal(active_mesh(), *vi);
763
	
763
	
764
    gel_srand(0);
764
    gel_srand(0);
765
    for(VertexIDIterator vi = active_mesh().vertices_begin(); vi != active_mesh().vertices_end(); ++vi)
765
    for(VertexIDIterator vi = active_mesh().vertices_begin(); vi != active_mesh().vertices_end(); ++vi)
766
    {
766
    {
767
        float rval = 0.5-gel_rand() / float(GEL_RAND_MAX);
767
        float rval = 0.5-gel_rand() / float(GEL_RAND_MAX);
768
        active_mesh().pos(*vi) += normals[*vi]*rval*noise_amplitude*avg_length*2.0;
768
        active_mesh().pos(*vi) += normals[*vi]*rval*noise_amplitude*avg_length*2.0;
769
    }
769
    }
770
    return;
770
    return;
771
}
771
}
772
 
772
 
773
void console_noisy_flips(const std::vector<std::string> & args)
773
void console_noisy_flips(const std::vector<std::string> & args)
774
{
774
{
775
    if(wantshelp(args)){
775
    if(wantshelp(args)){
776
        theConsole.printf("usage:  noise.perturb_topology <iter>");
776
        theConsole.printf("usage:  noise.perturb_topology <iter>");
777
        theConsole.printf("Perform random flips. iter (default=1) is the number of iterations.");
777
        theConsole.printf("Perform random flips. iter (default=1) is the number of iterations.");
778
        theConsole.printf("mostly for making nasty synthetic test cases.");
778
        theConsole.printf("mostly for making nasty synthetic test cases.");
779
        return;
779
        return;
780
    }
780
    }
781
    avo().save_old();
781
    avo().save_old();
782
	
782
	
783
    int iter = 1;
783
    int iter = 1;
784
    if(args.size() > 0){
784
    if(args.size() > 0){
785
        istringstream a0(args[0]);
785
        istringstream a0(args[0]);
786
        a0 >> iter;
786
        a0 >> iter;
787
    }
787
    }
788
	
788
	
789
    randomize_mesh(active_mesh(),  iter);
789
    randomize_mesh(active_mesh(),  iter);
790
    return;
790
    return;
791
}
791
}
792
 
792
 
793
void console_laplacian_smooth(const std::vector<std::string> & args)
793
void console_laplacian_smooth(const std::vector<std::string> & args)
794
{
794
{
795
    if(wantshelp(args)) {
795
    if(wantshelp(args)) {
796
        theConsole.printf("usage:  smooth.laplacian <weight> <iter>");
796
        theConsole.printf("usage:  smooth.laplacian <weight> <iter>");
797
        theConsole.printf("Perform Laplacian smoothing. weight is the scaling factor for the Laplacian.");
797
        theConsole.printf("Perform Laplacian smoothing. weight is the scaling factor for the Laplacian.");
798
        theConsole.printf("default weight = 1.0. Default number of iterations = 1");
798
        theConsole.printf("default weight = 1.0. Default number of iterations = 1");
799
        return;
799
        return;
800
    }
800
    }
801
    avo().save_old();
801
    avo().save_old();
802
	
802
	
803
    float t=1.0;
803
    float t=1.0;
804
    if(args.size() > 0){
804
    if(args.size() > 0){
805
        istringstream a0(args[0]);
805
        istringstream a0(args[0]);
806
        a0 >> t;
806
        a0 >> t;
807
    }
807
    }
808
    int iter = 1;
808
    int iter = 1;
809
    if(args.size()>1){
809
    if(args.size()>1){
810
        istringstream a0(args[1]);
810
        istringstream a0(args[1]);
811
        a0 >> iter;
811
        a0 >> iter;
812
    }
812
    }
813
    /// Simple laplacian smoothing with an optional weight.
813
    /// Simple laplacian smoothing with an optional weight.
814
    for(int i=0;i<iter;++i) 
814
    for(int i=0;i<iter;++i) 
815
        laplacian_smooth(active_mesh(), t);
815
        laplacian_smooth(active_mesh(), t);
816
    return;
816
    return;
817
}
817
}
818
 
818
 
819
 
819
 
820
void console_mean_curvature_smooth(const std::vector<std::string> & args){
820
void console_mean_curvature_smooth(const std::vector<std::string> & args){
821
    if(wantshelp(args)) {
821
    if(wantshelp(args)) {
822
        theConsole.printf("usage:  smooth.mean_curvature <weight> <iter>");
822
        theConsole.printf("usage:  smooth.mean_curvature <weight> <iter>");
823
        theConsole.printf("Perform mean curvature smoothing. weight is the scaling factor for the");
823
        theConsole.printf("Perform mean curvature smoothing. weight is the scaling factor for the");
824
        theConsole.printf("mean curvature vector which has been normalized by dividing by edge lengths");
824
        theConsole.printf("mean curvature vector which has been normalized by dividing by edge lengths");
825
        theConsole.printf("this allows for larger steps as suggested by Desbrun et al.");
825
        theConsole.printf("this allows for larger steps as suggested by Desbrun et al.");
826
        theConsole.printf("default weight = 1.0. Default number of iterations = 1");
826
        theConsole.printf("default weight = 1.0. Default number of iterations = 1");
827
        return;
827
        return;
828
    }
828
    }
829
    avo().save_old();
829
    avo().save_old();
830
	
830
	
831
    double t=1.0;
831
    double t=1.0;
832
    if(args.size() > 0){
832
    if(args.size() > 0){
833
        istringstream a0(args[0]);
833
        istringstream a0(args[0]);
834
        a0 >> t;
834
        a0 >> t;
835
    }
835
    }
836
    int iter=1;
836
    int iter=1;
837
    if(args.size() > 1){
837
    if(args.size() > 1){
838
        istringstream a0(args[1]);
838
        istringstream a0(args[1]);
839
        a0 >> iter;
839
        a0 >> iter;
840
    }	
840
    }	
841
    VertexAttributeVector<Vec3d> new_pos(active_mesh().allocated_vertices());
841
    VertexAttributeVector<Vec3d> new_pos(active_mesh().allocated_vertices());
842
    for(int j = 0; j < iter; ++j){
842
    for(int j = 0; j < iter; ++j){
843
        for(VertexIDIterator v = active_mesh().vertices_begin(); v != active_mesh().vertices_end(); ++v) {
843
        for(VertexIDIterator v = active_mesh().vertices_begin(); v != active_mesh().vertices_end(); ++v) {
844
            Vec3d m;
844
            Vec3d m;
845
            double w_sum;
845
            double w_sum;
846
            unnormalized_mean_curvature_normal(active_mesh(), *v, m, w_sum);
846
            unnormalized_mean_curvature_normal(active_mesh(), *v, m, w_sum);
847
            new_pos[*v] = Vec3d(active_mesh().pos(*v))  + (t * m/w_sum);
847
            new_pos[*v] = Vec3d(active_mesh().pos(*v))  + (t * m/w_sum);
848
        }
848
        }
849
        for(VertexIDIterator v = active_mesh().vertices_begin(); v != active_mesh().vertices_end(); ++v)
849
        for(VertexIDIterator v = active_mesh().vertices_begin(); v != active_mesh().vertices_end(); ++v)
850
            active_mesh().pos(*v) = new_pos[*v];
850
            active_mesh().pos(*v) = new_pos[*v];
851
    }
851
    }
852
    return;
852
    return;
853
}
853
}
854
 
854
 
855
void console_taubin_smooth(const std::vector<std::string> & args)
855
void console_taubin_smooth(const std::vector<std::string> & args)
856
{
856
{
857
    if(wantshelp(args)){
857
    if(wantshelp(args)){
858
        theConsole.printf("usage:  smooth.taubin <iter>");
858
        theConsole.printf("usage:  smooth.taubin <iter>");
859
        theConsole.printf("Perform Taubin smoothing. iter (default=1) is the number of iterations.");
859
        theConsole.printf("Perform Taubin smoothing. iter (default=1) is the number of iterations.");
860
        return;
860
        return;
861
    }
861
    }
862
    avo().save_old();
862
    avo().save_old();
863
	
863
	
864
    int iter = 1;
864
    int iter = 1;
865
    if(args.size() > 0){
865
    if(args.size() > 0){
866
        istringstream a0(args[0]);
866
        istringstream a0(args[0]);
867
        a0 >> iter;
867
        a0 >> iter;
868
    }
868
    }
869
    /// Taubin smoothing is similar to laplacian smoothing but reduces shrinkage
869
    /// Taubin smoothing is similar to laplacian smoothing but reduces shrinkage
870
    taubin_smooth(active_mesh(),  iter);
870
    taubin_smooth(active_mesh(),  iter);
871
	
871
	
872
    return;
872
    return;
873
}
873
}
874
 
874
 
875
void console_fvm_smooth(const std::vector<std::string> & args)
875
void console_fvm_smooth(const std::vector<std::string> & args)
876
{	
876
{	
877
    if(wantshelp(args)){
877
    if(wantshelp(args)){
878
        theConsole.printf("usage: smooth.fuzzy_vector_median <iter>");
878
        theConsole.printf("usage: smooth.fuzzy_vector_median <iter>");
879
        theConsole.printf("Smooth normals using fuzzy vector median smoothing. iter (default=1) is the number of iterations");
879
        theConsole.printf("Smooth normals using fuzzy vector median smoothing. iter (default=1) is the number of iterations");
880
        theConsole.printf("This function does a very good job of preserving sharp edges.");
880
        theConsole.printf("This function does a very good job of preserving sharp edges.");
881
        return;
881
        return;
882
    }
882
    }
883
    avo().save_old();
883
    avo().save_old();
884
	
884
	
885
    int iter=1;
885
    int iter=1;
886
    if(args.size() > 0){
886
    if(args.size() > 0){
887
        istringstream a0(args[0]);
887
        istringstream a0(args[0]);
888
        a0 >> iter;
888
        a0 >> iter;
889
    }
889
    }
890
    // Fuzzy vector median smoothing is effective when it comes to preserving sharp edges. 
890
    // Fuzzy vector median smoothing is effective when it comes to preserving sharp edges. 
891
    fvm_smooth(active_mesh(),  iter);
891
    fvm_smooth(active_mesh(),  iter);
892
	
892
	
893
    return;
893
    return;
894
}
894
}
895
 
895
 
896
void console_triangulate(const std::vector<std::string> & args)
896
void console_triangulate(const std::vector<std::string> & args)
897
{	
897
{	
898
    if(wantshelp(args)) {
898
    if(wantshelp(args)) {
899
        theConsole.printf("usage:  triangulate");
899
        theConsole.printf("usage:  triangulate");
900
        theConsole.printf("This function triangulates all non triangular faces of the mesh.");
900
        theConsole.printf("This function triangulates all non triangular faces of the mesh.");
901
        theConsole.printf("you may want to call it after hole closing. For a polygon it simply connects");
901
        theConsole.printf("you may want to call it after hole closing. For a polygon it simply connects");
902
        theConsole.printf("the two closest vertices in a recursive manner until only triangles remain");
902
        theConsole.printf("the two closest vertices in a recursive manner until only triangles remain");
903
        return;
903
        return;
904
    }
904
    }
905
    avo().save_old();
905
    avo().save_old();
906
	
906
	
907
    shortest_edge_triangulate(active_mesh());
907
    shortest_edge_triangulate(active_mesh());
908
    active_mesh().cleanup();
908
    active_mesh().cleanup();
909
	valid(active_mesh());
909
	valid(active_mesh());
910
    return;
910
    return;
911
}
911
}
912
 
912
 
-
 
913
void console_remove_faces(const std::vector<std::string> & args)
-
 
914
{
-
 
915
    avo().save_old();
-
 
916
    
-
 
917
    gel_srand(0);
-
 
918
 
-
 
919
//    for (FaceIDIterator f= active_mesh().faces_begin(); f != active_mesh().faces_end(); ++f) {
-
 
920
//        if(gel_rand() < 0.5 * GEL_RAND_MAX)
-
 
921
//        {
-
 
922
//            active_mesh().remove_face(*f);
-
 
923
//        }
-
 
924
//    }
-
 
925
 
-
 
926
//    for (VertexIDIterator v= active_mesh().vertices_begin(); v != active_mesh().vertices_end(); ++v) {
-
 
927
//        if(gel_rand() < 0.005 * GEL_RAND_MAX)
-
 
928
//        {
-
 
929
//            active_mesh().remove_vertex(*v);
-
 
930
//        }
-
 
931
//    }
-
 
932
    for (HalfEdgeIDIterator h= active_mesh().halfedges_begin(); h != active_mesh().halfedges_end(); ++h) {
-
 
933
        if(gel_rand() < 0.005 * GEL_RAND_MAX)
-
 
934
        {
-
 
935
            active_mesh().remove_edge(*h);
-
 
936
        }
-
 
937
    }
-
 
938
 
-
 
939
    active_mesh().cleanup();
-
 
940
    valid(active_mesh());
-
 
941
	
-
 
942
    return;
-
 
943
}
-
 
944
 
913
 
945
 
914
void console_remove_caps(const std::vector<std::string> & args)
946
void console_remove_caps(const std::vector<std::string> & args)
915
{	
947
{	
916
    if(wantshelp(args)) {
948
    if(wantshelp(args)) {
917
        theConsole.printf("usage:  cleanup.remove_caps thresh");
949
        theConsole.printf("usage:  cleanup.remove_caps thresh");
918
        theConsole.printf("Remove caps (triangles with one very big angle). The thresh argument is the fraction of PI to");
950
        theConsole.printf("Remove caps (triangles with one very big angle). The thresh argument is the fraction of PI to");
919
        theConsole.printf("use as threshold for big angle. Default is 0.85. Caps are removed by flipping.");
951
        theConsole.printf("use as threshold for big angle. Default is 0.85. Caps are removed by flipping.");
920
        return;
952
        return;
921
    }
953
    }
922
    avo().save_old();
954
    avo().save_old();
923
	
955
	
924
    float t = 0.85f;
956
    float t = 0.85f;
925
    if(args.size() > 0){
957
    if(args.size() > 0){
926
        istringstream a0(args[0]);
958
        istringstream a0(args[0]);
927
        a0 >> t;
959
        a0 >> t;
928
    }
960
    }
929
    remove_caps(active_mesh(), static_cast<float>(M_PI) *t);
961
    remove_caps(active_mesh(), static_cast<float>(M_PI) *t);
930
    active_mesh().cleanup();
962
    active_mesh().cleanup();
931
	
963
	
932
    return;
964
    return;
933
}
965
}
934
 
966
 
935
void console_remove_needles(const std::vector<std::string> & args)
967
void console_remove_needles(const std::vector<std::string> & args)
936
{	
968
{	
937
    if(wantshelp(args)){
969
    if(wantshelp(args)){
938
        theConsole.printf("usage: cleanup.remove_needles <thresh>");
970
        theConsole.printf("usage: cleanup.remove_needles <thresh>");
939
        theConsole.printf("Removes very short edges by collapse. thresh is multiplied by the average edge length");
971
        theConsole.printf("Removes very short edges by collapse. thresh is multiplied by the average edge length");
940
        theConsole.printf("to get the length shorter than which we collapse. Default = 0.1");
972
        theConsole.printf("to get the length shorter than which we collapse. Default = 0.1");
941
        return;
973
        return;
942
    }
974
    }
943
    avo().save_old();
975
    avo().save_old();
944
	
976
	
945
    float thresh = 0.1f;
977
    float thresh = 0.1f;
946
    if(args.size() > 0){
978
    if(args.size() > 0){
947
        istringstream a0(args[0]);
979
        istringstream a0(args[0]);
948
        a0 >> thresh;
980
        a0 >> thresh;
949
    }
981
    }
950
    float avg_length = average_edge_length(active_mesh());
982
    float avg_length = average_edge_length(active_mesh());
951
    remove_needles(active_mesh(), thresh * avg_length);
983
    remove_needles(active_mesh(), thresh * avg_length);
952
    active_mesh().cleanup();
984
    active_mesh().cleanup();
953
	
985
	
954
    return;
986
    return;
955
}
987
}
956
 
988
 
957
void console_undo(const std::vector<std::string> & args)
989
void console_undo(const std::vector<std::string> & args)
958
{	
990
{	
959
    if(wantshelp(args)) {
991
    if(wantshelp(args)) {
960
        theConsole.printf("usage:  undo");
992
        theConsole.printf("usage:  undo");
961
        theConsole.printf("This function undoes one operation. Repeated undo does nothing");
993
        theConsole.printf("This function undoes one operation. Repeated undo does nothing");
962
        return;
994
        return;
963
    }
995
    }
964
    avo().restore_old();
996
    avo().restore_old();
965
    return;
997
    return;
966
}
998
}
967
 
999
 
968
 
1000
 
969
void reshape(int W, int H)
1001
void reshape(int W, int H)
970
{
1002
{
971
    active_view_control().reshape(W,H);
1003
    active_view_control().reshape(W,H);
972
}
1004
}
973
 
1005
 
974
Console::variable<string> display_render_mode("normal");
1006
Console::variable<string> display_render_mode("normal");
975
Console::variable<int> display_smooth_shading;
1007
Console::variable<int> display_smooth_shading;
976
Console::variable<float> display_gamma(2.2);
1008
Console::variable<float> display_gamma(2.2);
977
 
1009
 
978
void display()
1010
void display()
979
{
1011
{
980
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1012
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
981
	
1013
	
982
 
1014
 
983
    glPushMatrix();
1015
    glPushMatrix();
984
 
1016
 
985
    avo().display(display_render_mode, theConsole, display_smooth_shading, display_gamma);
1017
    avo().display(display_render_mode, theConsole, display_smooth_shading, display_gamma);
986
	
1018
	
987
    glPopMatrix();
1019
    glPopMatrix();
988
	
1020
	
989
    if(console_visible)
1021
    if(console_visible)
990
    {
1022
    {
991
        glUseProgram(0);
1023
        glUseProgram(0);
992
        theConsole.display();
1024
        theConsole.display();
993
	}
1025
	}
994
    
1026
    
995
    glutSwapBuffers();
1027
    glutSwapBuffers();
996
}
1028
}
997
 
1029
 
998
void animate() 
1030
void animate() 
999
{	
1031
{	
1000
    //usleep( (int)1e4 );
1032
    //usleep( (int)1e4 );
1001
    active_view_control().try_spin();
1033
    active_view_control().try_spin();
1002
    glutPostRedisplay();
1034
    glutPostRedisplay();
1003
}
1035
}
1004
 
1036
 
1005
 
1037
 
1006
void mouse(int button, int state, int x, int y) 
1038
void mouse(int button, int state, int x, int y) 
1007
{
1039
{
1008
    Vec2i pos(x,y);
1040
    Vec2i pos(x,y);
1009
    if (state==GLUT_DOWN) 
1041
    if (state==GLUT_DOWN) 
1010
    {
1042
    {
1011
        if (button==GLUT_LEFT_BUTTON) 
1043
        if (button==GLUT_LEFT_BUTTON && glutGetModifiers() == 0)
1012
            active_view_control().grab_ball(ROTATE_ACTION,pos);
1044
            active_view_control().grab_ball(ROTATE_ACTION,pos);
1013
        else if (button==GLUT_MIDDLE_BUTTON) 
1045
        else if (button==GLUT_MIDDLE_BUTTON || glutGetModifiers() == GLUT_ACTIVE_CTRL) 
1014
            active_view_control().grab_ball(ZOOM_ACTION,pos);
1046
            active_view_control().grab_ball(ZOOM_ACTION,pos);
1015
        else if (button==GLUT_RIGHT_BUTTON) 
1047
        else if (button==GLUT_RIGHT_BUTTON || glutGetModifiers() == GLUT_ACTIVE_ALT)
1016
            active_view_control().grab_ball(PAN_ACTION,pos);
1048
            active_view_control().grab_ball(PAN_ACTION,pos);
1017
    }
1049
    }
1018
    else if (state==GLUT_UP)
1050
    else if (state==GLUT_UP)
1019
        active_view_control().release_ball();
1051
        active_view_control().release_ball();
1020
}
1052
}
1021
 
1053
 
1022
void motion(int x, int y) {
1054
void motion(int x, int y) {
1023
    Vec2i pos(x,y);
1055
    Vec2i pos(x,y);
1024
    active_view_control().roll_ball(Vec2i(x,y));
1056
    active_view_control().roll_ball(Vec2i(x,y));
1025
}
1057
}
1026
 
1058
 
1027
 
1059
 
1028
void keyboard_spec(int key, int x, int y)
1060
void keyboard_spec(int key, int x, int y)
1029
{
1061
{
1030
    if (console_visible)
1062
    if (console_visible)
1031
        theConsole.special(key);
1063
        theConsole.special(key);
1032
    glutPostRedisplay();
1064
    glutPostRedisplay();
1033
}
1065
}
1034
 
1066
 
1035
 
1067
 
1036
void keyboard(unsigned char key, int x, int y) 
1068
void keyboard(unsigned char key, int x, int y) 
1037
{
1069
{
1038
    //toggle console with ESC
1070
    //toggle console with ESC
1039
    if (key == 27)
1071
    if (key == 27)
1040
    {
1072
    {
1041
        console_visible = !console_visible;
1073
        console_visible = !console_visible;
1042
        glutPostRedisplay();
1074
        glutPostRedisplay();
1043
        return;
1075
        return;
1044
    }
1076
    }
1045
    
1077
    
1046
    if (console_visible)
1078
    if (console_visible)
1047
    {
1079
    {
1048
        theConsole.keyboard(key);
1080
        theConsole.keyboard(key);
1049
        if(key == 13)
1081
        if(key == 13)
1050
        {
1082
        {
1051
            avo().post_create_display_list();
1083
            avo().post_create_display_list();
1052
            glutPostRedisplay();
1084
            glutPostRedisplay();
1053
        }
1085
        }
1054
        return;
1086
        return;
1055
    }
1087
    }
1056
    else {		
1088
    else {		
1057
		
1089
		
1058
        switch(key) {
1090
        switch(key) {
1059
			case 'q': exit(0);
1091
			case 'q': exit(0);
1060
			case '\033':
1092
			case '\033':
1061
                console_visible = false;
1093
                console_visible = false;
1062
				break;
1094
				break;
1063
			case '1':
1095
			case '1':
1064
			case '2':
1096
			case '2':
1065
			case '3':
1097
			case '3':
1066
			case '4':
1098
			case '4':
1067
			case '5':
1099
			case '5':
1068
			case '6':
1100
			case '6':
1069
			case '7':
1101
			case '7':
1070
			case '8':
1102
			case '8':
1071
			case '9':
1103
			case '9':
1072
				active = key - '1'; break;
1104
				active = key - '1'; break;
1073
			case 'f': display_smooth_shading = !display_smooth_shading; break;
1105
			case 'f': display_smooth_shading = !display_smooth_shading; break;
1074
			case 'w':
1106
			case 'w':
1075
				display_render_mode = "wire"; break;
1107
				display_render_mode = "wire"; break;
1076
			case 'n':
1108
			case 'n':
1077
				display_render_mode = "normal"; break;
1109
				display_render_mode = "normal"; break;
1078
			case 'i':
1110
			case 'i':
1079
				display_render_mode = "isophotes"; break;
1111
				display_render_mode = "isophotes"; break;
1080
			case 'r':
1112
			case 'r':
1081
				display_render_mode = "reflection"; break;
1113
				display_render_mode = "reflection"; break;
1082
			case 'h':
1114
			case 'h':
1083
				display_render_mode = "harmonics"; break;
1115
				display_render_mode = "harmonics"; break;
1084
			case 't':
1116
			case 't':
1085
				display_render_mode = "toon"; break;
1117
				display_render_mode = "toon"; break;
1086
			case 'g':
1118
			case 'g':
1087
				display_render_mode = "glazed"; break;
1119
				display_render_mode = "glazed"; break;
1088
			case 'a':
1120
			case 'a':
1089
				display_render_mode = "ambient_occlusion"; break;
1121
				display_render_mode = "ambient_occlusion"; break;
1090
			case 'c':
1122
			case 'c':
1091
				display_render_mode = "copper"; break;
1123
				display_render_mode = "copper"; break;
1092
			case 'C':
1124
			case 'C':
1093
				display_render_mode = "curvature_lines"; break;
1125
				display_render_mode = "curvature_lines"; break;
1094
			case 'M':
1126
			case 'M':
1095
				display_render_mode = "mean_curvature"; break;
1127
				display_render_mode = "mean_curvature"; break;
1096
			case 'G':
1128
			case 'G':
1097
				display_render_mode = "gaussian_curvature"; break;
1129
				display_render_mode = "gaussian_curvature"; break;
1098
        }
1130
        }
1099
		
1131
		
1100
        if(string(display_render_mode).substr(0,3) == "har")
1132
        if(string(display_render_mode).substr(0,3) == "har")
1101
            avo().harmonics_parse_key(key);
1133
            avo().harmonics_parse_key(key);
1102
		
1134
		
1103
        if(key != '\033') avo().post_create_display_list();
1135
        if(key != '\033') avo().post_create_display_list();
1104
    }
1136
    }
1105
    
1137
    
1106
    glutPostRedisplay();
1138
    glutPostRedisplay();
1107
}
1139
}
1108
 
1140
 
1109
void init_glut(int argc, char** argv)
1141
void init_glut(int argc, char** argv)
1110
{  
1142
{  
1111
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH|GLUT_ALPHA);
1143
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH|GLUT_ALPHA);
1112
    glutInitWindowSize(WINX, WINY);
1144
    glutInitWindowSize(WINX, WINY);
1113
    glutInit(&argc, argv);
1145
    glutInit(&argc, argv);
1114
    glutCreateWindow("MeshEdit");
1146
    glutCreateWindow("MeshEdit");
1115
    glutDisplayFunc(display);
1147
    glutDisplayFunc(display);
1116
    glutKeyboardFunc(keyboard);
1148
    glutKeyboardFunc(keyboard);
1117
    glutSpecialFunc(keyboard_spec);
1149
    glutSpecialFunc(keyboard_spec);
1118
    glutReshapeFunc(reshape);
1150
    glutReshapeFunc(reshape);
1119
    glutMouseFunc(mouse);
1151
    glutMouseFunc(mouse);
1120
    glutMotionFunc(motion);
1152
    glutMotionFunc(motion);
1121
    glutIdleFunc(animate);
1153
    glutIdleFunc(animate);
1122
}
1154
}
1123
void init_gl()
1155
void init_gl()
1124
{
1156
{
1125
    glewInit();
1157
    glewInit();
1126
    glEnable(GL_LIGHTING);
1158
    glEnable(GL_LIGHTING);
1127
    glEnable(GL_LIGHT0);
1159
    glEnable(GL_LIGHT0);
1128
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
1160
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
1129
	
1161
	
1130
    // Set the value of a uniform
1162
    // Set the value of a uniform
1131
    //glUniform2f(glGetUniformLocation(prog_P0,"WIN_SCALE"), win_size_x/2.0, win_size_y/2.0);
1163
    //glUniform2f(glGetUniformLocation(prog_P0,"WIN_SCALE"), win_size_x/2.0, win_size_y/2.0);
1132
	
1164
	
1133
    glMatrixMode(GL_MODELVIEW);
1165
    glMatrixMode(GL_MODELVIEW);
1134
    glLoadIdentity();
1166
    glLoadIdentity();
1135
    glClearColor(1,1,1, 0.f);
1167
    glClearColor(1,1,1, 0.f);
1136
    glColor4f(1.0f, 1.0f, 1.0f, 0.f);
1168
    glColor4f(1.0f, 1.0f, 1.0f, 0.f);
1137
    float material[4] = {1,1,1,1};
1169
    float material[4] = {1,1,1,1};
1138
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material);
1170
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material);
1139
    glEnable(GL_DEPTH_TEST);
1171
    glEnable(GL_DEPTH_TEST);
1140
	
1172
	
1141
    theConsole.reg_cmdN("harmonics.reset_shape", console_reset_shape, "");
1173
    theConsole.reg_cmdN("harmonics.reset_shape", console_reset_shape, "");
1142
    theConsole.reg_cmdN("harmonics.analyze", console_analyze, "");
1174
    theConsole.reg_cmdN("harmonics.analyze", console_analyze, "");
1143
    theConsole.reg_cmdN("harmonics.partial_reconstruct", console_partial_reconstruct,"");
1175
    theConsole.reg_cmdN("harmonics.partial_reconstruct", console_partial_reconstruct,"");
1144
    theConsole.reg_cmdN("simplify", console_simplify,"");
1176
    theConsole.reg_cmdN("simplify", console_simplify,"");
1145
    theConsole.reg_cmdN("smooth.mean_curvature", console_mean_curvature_smooth,"");
1177
    theConsole.reg_cmdN("smooth.mean_curvature", console_mean_curvature_smooth,"");
1146
    theConsole.reg_cmdN("smooth.laplacian", console_laplacian_smooth,"");
1178
    theConsole.reg_cmdN("smooth.laplacian", console_laplacian_smooth,"");
1147
    theConsole.reg_cmdN("smooth.taubin", console_taubin_smooth,"");
1179
    theConsole.reg_cmdN("smooth.taubin", console_taubin_smooth,"");
1148
    theConsole.reg_cmdN("smooth.fuzzy_vector_median", console_fvm_smooth,"");
1180
    theConsole.reg_cmdN("smooth.fuzzy_vector_median", console_fvm_smooth,"");
1149
	
1181
	
1150
    theConsole.reg_cmdN("optimize.valency", console_optimize_valency,"");
1182
    theConsole.reg_cmdN("optimize.valency", console_optimize_valency,"");
1151
    theConsole.reg_cmdN("optimize.minimize_dihedral_angles", console_minimize_dihedral,"");
1183
    theConsole.reg_cmdN("optimize.minimize_dihedral_angles", console_minimize_dihedral,"");
1152
    theConsole.reg_cmdN("optimize.minimize_curvature", console_minimize_curvature,"");
1184
    theConsole.reg_cmdN("optimize.minimize_curvature", console_minimize_curvature,"");
1153
    theConsole.reg_cmdN("optimize.maximize_min_angle", console_maximize_min_angle,"");
1185
    theConsole.reg_cmdN("optimize.maximize_min_angle", console_maximize_min_angle,"");
1154
    theConsole.reg_cmdN("cleanup.close_holes", console_close_holes,"");
1186
    theConsole.reg_cmdN("cleanup.close_holes", console_close_holes,"");
1155
    theConsole.reg_cmdN("load_mesh", console_reload,"");
1187
    theConsole.reg_cmdN("load_mesh", console_reload,"");
1156
    theConsole.reg_cmdN("add_mesh", console_add_mesh,"");
1188
    theConsole.reg_cmdN("add_mesh", console_add_mesh,"");
1157
	
1189
	
1158
    theConsole.reg_cmdN("cleanup.remove_caps", console_remove_caps,"");
1190
    theConsole.reg_cmdN("cleanup.remove_caps", console_remove_caps,"");
1159
    theConsole.reg_cmdN("cleanup.remove_needles", console_remove_needles,"");
1191
    theConsole.reg_cmdN("cleanup.remove_needles", console_remove_needles,"");
1160
    theConsole.reg_cmdN("triangulate", console_triangulate,"");
1192
    theConsole.reg_cmdN("triangulate", console_triangulate,"");
1161
    theConsole.reg_cmdN("refine.split_edges", console_refine_edges,"");
1193
    theConsole.reg_cmdN("refine.split_edges", console_refine_edges,"");
1162
    theConsole.reg_cmdN("refine.split_faces", console_refine_faces,"");
1194
    theConsole.reg_cmdN("refine.split_faces", console_refine_faces,"");
1163
    theConsole.reg_cmdN("refine.catmull_clark", console_cc_subdivide,"");
1195
    theConsole.reg_cmdN("refine.catmull_clark", console_cc_subdivide,"");
1164
    theConsole.reg_cmdN("refine.doo_sabin", console_doosabin_subdivide,"");
1196
    theConsole.reg_cmdN("refine.doo_sabin", console_doosabin_subdivide,"");
1165
    theConsole.reg_cmdN("save_mesh", console_save,"");
1197
    theConsole.reg_cmdN("save_mesh", console_save,"");
1166
    theConsole.reg_cmdN("noise.perturb_vertices", console_vertex_noise,"");
1198
    theConsole.reg_cmdN("noise.perturb_vertices", console_vertex_noise,"");
1167
    theConsole.reg_cmdN("noise.perturb_vertices_perpendicular", console_perpendicular_vertex_noise,"");
1199
    theConsole.reg_cmdN("noise.perturb_vertices_perpendicular", console_perpendicular_vertex_noise,"");
1168
    theConsole.reg_cmdN("noise.perturb_topology", console_noisy_flips,"");
1200
    theConsole.reg_cmdN("noise.perturb_topology", console_noisy_flips,"");
1169
 
1201
 
-
 
1202
    theConsole.reg_cmdN("remove_faces", console_remove_faces,"");
-
 
1203
 
1170
    theConsole.reg_cmdN("dual", console_dual,"");
1204
    theConsole.reg_cmdN("dual", console_dual,"");
1171
    theConsole.reg_cmdN("flatten", console_flatten,"");
1205
    theConsole.reg_cmdN("flatten", console_flatten,"");
1172
	
1206
	
1173
    theConsole.reg_cmdN("align", console_align,"");
1207
    theConsole.reg_cmdN("align", console_align,"");
1174
	
1208
	
1175
    theConsole.reg_cmdN("undo", console_undo,"");
1209
    theConsole.reg_cmdN("undo", console_undo,"");
1176
	
1210
	
1177
	theConsole.reg_cmdN("validity", console_valid,"");
1211
	theConsole.reg_cmdN("validity", console_valid,"");
1178
	theConsole.reg_cmdN("info", console_info,"");
1212
	theConsole.reg_cmdN("info", console_info,"");
1179
 
1213
 
1180
    theConsole.reg_cmdN("polarize", console_polarize ,"");
1214
    theConsole.reg_cmdN("polarize", console_polarize ,"");
1181
    
1215
    
1182
    active.reg(theConsole, "active_mesh", "The active mesh");
1216
    active.reg(theConsole, "active_mesh", "The active mesh");
1183
    display_render_mode.reg(theConsole, "display.render_mode", "Display render mode");
1217
    display_render_mode.reg(theConsole, "display.render_mode", "Display render mode");
1184
    display_smooth_shading.reg(theConsole, "display.smooth_shading", "1 for smooth shading 0 for flat");
1218
    display_smooth_shading.reg(theConsole, "display.smooth_shading", "1 for smooth shading 0 for flat");
1185
    display_gamma.reg(theConsole, "display.gamma", "The gamma setting for the display");
1219
    display_gamma.reg(theConsole, "display.gamma", "The gamma setting for the display");
1186
 
1220
 
1187
}
1221
}
1188
 
1222
 
1189
int main(int argc, char** argv)
1223
int main(int argc, char** argv)
1190
{
1224
{
1191
    ArgExtracter ae(argc, argv);
1225
    ArgExtracter ae(argc, argv);
1192
	
1226
	
1193
    init_glut(argc, argv);
1227
    init_glut(argc, argv);
1194
    init_gl();
1228
    init_gl();
1195
	
1229
	
1196
    theConsole.print("Welcome to MeshEdit");
1230
    theConsole.print("Welcome to MeshEdit");
1197
    theConsole.newline();
1231
    theConsole.newline();
1198
    
1232
    
1199
    Harmonics::init(theConsole);
1233
    Harmonics::init(theConsole);
1200
	
1234
	
1201
    if(argc>1){		
1235
    if(argc>1){		
1202
        vector<string> files;
1236
        vector<string> files;
1203
		ae.get_all_args(files);
1237
		ae.get_all_args(files);
1204
		for(int i=1;i<files.size();++i)
1238
		for(int i=1;i<files.size();++i)
1205
			get_vis_obj(i-1).reload(files[i]);
1239
			get_vis_obj(i-1).reload(files[i]);
1206
    }
1240
    }
1207
    glutMainLoop();
1241
    glutMainLoop();
1208
    return 0;
1242
    return 0;
1209
}
1243
}
1210
 
1244
 
1211
 
1245
 
1212
 
1246
 
1213
 
1247
 
1214
 
1248
 
1215
 
1249