Subversion Repositories gelsvn

Rev

Rev 650 | Rev 657 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
647 janba 1
//
2
//  MeshEditor.cpp
3
//  GEL
4
//
5
//  Created by J. Andreas Bærentzen on 09/10/13.
6
//
7
//
8
 
9
#include <GL/glew.h>
10
#include <functional>
11
#include "MeshEditor.h"
12
#include <string>
13
#include <iostream>
14
#include <vector>
15
#include <algorithm>
16
#include <queue>
17
 
18
#include <GLGraphics/Console.h>
19
 
20
#include <CGLA/eigensolution.h>
21
#include <CGLA/Vec2d.h>
22
#include <CGLA/Vec3d.h>
23
#include <CGLA/Mat3x3d.h>
24
#include <CGLA/Mat2x2d.h>
25
#include <CGLA/Mat2x3d.h>
26
#include <CGLA/Mat4x4d.h>
27
 
28
#include <LinAlg/Matrix.h>
29
#include <LinAlg/Vector.h>
30
#include <LinAlg/LapackFunc.h>
31
 
32
#include <HMesh/Manifold.h>
33
#include <HMesh/AttributeVector.h>
34
#include <HMesh/mesh_optimization.h>
35
#include <HMesh/curvature.h>
36
#include <HMesh/triangulate.h>
37
#include <HMesh/flatten.h>
38
#include <HMesh/dual.h>
39
#include <HMesh/load.h>
40
#include <HMesh/quadric_simplify.h>
41
#include <HMesh/smooth.h>
42
#include <HMesh/x3d_save.h>
43
#include <HMesh/obj_save.h>
44
#include <HMesh/off_save.h>
45
#include <HMesh/mesh_optimization.h>
46
#include <HMesh/triangulate.h>
47
#include <HMesh/cleanup.h>
48
#include <HMesh/cleanup.h>
49
#include <HMesh/refine_edges.h>
50
#include <HMesh/subdivision.h>
51
#include <HMesh/harmonics.h>
52
 
53
#include <Util/Timer.h>
54
 
55
#include "VisObj.h"
56
 
57
using namespace std;
58
using namespace CGLA;
59
using namespace HMesh;
60
using namespace Util;
61
 
62
namespace GLGraphics {
63
 
64
    namespace {
65
 
66
        bool wantshelp(const std::vector<std::string> & args)
67
        {
68
            if(args.size() == 0)
69
                return false;
70
 
71
            string str = args[0];
72
 
73
            if(str=="help" || str=="HELP" || str=="Help" || str=="?")
74
                return true;
75
 
76
            return false;
77
        }
78
 
79
 
80
        bool wantshelp(MeshEditor* me, const std::vector<std::string> & args)
81
        {
82
            if(args.size() == 0)
83
                return false;
84
 
85
            string str = args[0];
86
 
87
            if(str=="help" || str=="HELP" || str=="Help" || str=="?")
88
                return true;
89
 
90
            return false;
91
        }
92
 
93
        /// Function that aligns two meshes.
94
        void console_align(MeshEditor* me, const std::vector<std::string> & args)
95
        {
96
            if(wantshelp(args)) {
97
                me->printf("usage: align <dest> <src>");
98
                me->printf("This function aligns dest mesh with src");
99
                me->printf("In practice the GLViewController of src is copied to dst.");
100
                me->printf("both arguments are mandatory and must be numbers between 1 and 9.");
101
                me->printf("Note that results might be unexpexted if the meshes are not on the same scale");
102
            }
103
 
104
            int dest = 0;
105
 
106
            if(args.size()>0){
107
                istringstream a0(args[0]);
108
                a0 >> dest;
109
                --dest;
110
 
111
                if(dest <0 || dest>8)
112
                {
113
                    me->printf("dest mesh out of range (1-9)");
114
                    return;
115
                }
116
            }
117
            else
118
            {
119
                me->printf("neither source nor destination mesh?!");
120
                return;
121
            }
122
 
123
            int src = 0;
124
            if(args.size()>1){
125
                istringstream a1(args[1]);
126
                a1 >> src;
127
                --src;
128
 
129
                if(src <0 || src>8)
130
                {
131
                    me->printf("src mesh out of range (1-9)");
132
                    return;
133
                }
134
            }
135
            else
136
            {
137
                me->printf("no src mesh?");
138
                return;
139
            }
140
            me->align(src,dest);
141
        }
142
 
143
 
144
 
145
 
146
        void console_ridge_lines(MeshEditor* me, const std::vector<std::string> & args)
147
        {
148
            if(wantshelp(args)) {
149
                me->printf("usage: ridge_lines");
150
                return;
151
            }
152
 
153
            me->save_active_mesh();
154
 
155
            Manifold& mani = me->active_mesh();
156
 
157
            VertexAttributeVector<Mat3x3d> curvature_tensors(mani.allocated_vertices());
158
            VertexAttributeVector<Vec3d> min_curv_direction(mani.allocated_vertices());
159
            VertexAttributeVector<Vec3d> max_curv_direction(mani.allocated_vertices());
160
            VertexAttributeVector<Vec2d> curvature(mani.allocated_vertices());
161
 
162
            //    curvature_tensors_from_edges(mani, curvature_tensors);
163
            //    for(int i=0;i<3; ++i)
164
            //        smooth_curvature_tensors(mani,curvature_tensors);
165
            //    curvature_from_tensors(mani, curvature_tensors,
166
            //                        min_curv_direction,
167
            //                        max_curv_direction,
168
            //                        curvature);
169
 
170
            //    void console_refit_polar(MeshEditor* me, const std::vector<std::string> & args)
171
            //    {
172
            //        if(wantshelp(args)) {
173
            //            me->printf("usage: simplify.polar <mesh 1> <mesh 2> <iter>");
174
            //            return;
175
            //        }
176
            //        int m1=1;
177
            //        int m2=2;
178
            //        int iter=1;
179
            //        int dim = 64;
180
            //        if(args.size() > 0){
181
            //            istringstream a0(args[0]);
182
            //            a0 >> m1;
183
            //        }
184
            //        if(args.size() > 1){
185
            //            istringstream a0(args[1]);
186
            //            a0 >> m2;
187
            //        }
188
            //        if(args.size() > 2){
189
            //            istringstream a0(args[2]);
190
            //            a0 >> iter;
191
            //        }
192
            //
193
            //        if(args.size() > 3){
194
            //            istringstream a0(args[3]);
195
            //            a0 >> dim;
196
            //        }
197
            //
198
            //
199
            //
200
            //        me->save_active_mesh();
201
            //
202
            //        smooth_and_refit(get_vis_obj(m1-1).mesh() , get_vis_obj(m2-1).mesh(), iter, dim);
203
            //    }
204
            //
205
 
206
 
207
            curvature_paraboloids(mani,
208
                                  min_curv_direction,
209
                                  max_curv_direction,
210
                                  curvature);
211
 
212
            for(auto vid : mani.vertices())
213
            {
214
                Vec3d max_curv_dir = normalize(max_curv_direction[vid]);
215
                Vec3d min_curv_dir = normalize(min_curv_direction[vid]);
216
                double vid_min_pc = curvature[vid][0];
217
                double vid_max_pc = curvature[vid][1];
218
                bool ridge = true;
219
                bool ravine = true;
220
                Walker w = mani.walker(vid);
221
                Vec3d r(0);
222
                for(; !w.full_circle();w = w.circulate_vertex_ccw())
223
                {
224
                    Vec3d e = (mani.pos(w.vertex()) - mani.pos(vid));
225
 
226
                    if(abs(dot(min_curv_dir,e)) > abs(dot(max_curv_dir,e)))
227
                    {
228
                        if(curvature[w.vertex()][0]<vid_min_pc+20)
229
                            ravine = false;
230
 
231
                    }
232
                    else
233
                    {
234
                        if(curvature[w.vertex()][1]>vid_max_pc-20)
235
                            ridge = false;
236
                    }
237
                }
238
                DebugRenderer::vertex_colors[vid] = Vec3f(ridge,ravine,0.0);
239
            }
240
            for(auto fid : mani.faces())
241
                DebugRenderer::face_colors[fid] = Vec3f(.3,.3,.6);
242
            for(auto hid : mani.halfedges()) {
243
 
244
                Walker w = mani.walker(hid);
245
                Vec3f c0 = DebugRenderer::vertex_colors[w.opp().vertex()];
246
                Vec3f c1 = DebugRenderer::vertex_colors[w.vertex()];
247
 
248
                DebugRenderer::edge_colors[hid] = (c0==c1) ? c0 : Vec3f(0.1,0.1,0.3);
249
 
250
            }
251
        }
252
 
253
        void transform_mesh(Manifold& mani, const Mat4x4d& m)
254
        {
255
            for(VertexIDIterator vid = mani.vertices_begin(); vid != mani.vertices_end(); ++vid)
256
                mani.pos(*vid) = m.mul_3D_point(mani.pos(*vid));
257
        }
258
 
259
        void console_scale(MeshEditor* me, const std::vector<std::string> & args)
260
        {
261
            if(wantshelp(args)) {
262
                me->printf("usage: scale sx sy sz");
263
                return;
264
            }
265
 
266
            Vec3d s;
267
 
268
            if(args.size() > 0){
269
                istringstream a0(args[0]);
270
                a0 >> s[0];
271
            }
272
            if(args.size() > 1){
273
                istringstream a0(args[0]);
274
                a0 >> s[1];
275
            }
276
            if(args.size() > 2){
277
                istringstream a0(args[0]);
278
                a0 >> s[2];
279
            }
280
 
281
            me->save_active_mesh();
282
            transform_mesh(me->active_mesh(),scaling_Mat4x4d(s));
283
            me->refit();
284
        }
285
 
286
 
287
        void console_flatten(MeshEditor* me, const std::vector<std::string> & args)
288
        {
289
            if(wantshelp(args)) {
290
                me->printf("usage: flatten <floater|harmonic|barycentric>");
291
                me->printf("This function flattens a meshs with a simple boundary. It is mostly for showing mesh");
292
                me->printf("parametrization methods. The current mesh MUST have a SINGLE boundary loop");
293
                me->printf("This loop is mapped to the unit circle in a regular fashion (equal angle intervals).");
294
                me->printf("All non boundary vertices are placed at the origin. Then the system is relaxed iteratively");
295
                me->printf("using the weight scheme given as argument.");
296
                return;
297
            }
298
 
299
            me->save_active_mesh();
300
 
301
            WeightScheme ws = BARYCENTRIC_W;
302
            if(args.size()>0){
303
                if(args[0] == "floater")
304
                    ws = FLOATER_W;
305
                else if(args[0] == "harmonic")
306
                    ws = HARMONIC_W;
307
                else if(args[0] == "lscm")
308
                    ws = LSCM_W;
309
            }
310
            else
311
                return;
312
 
313
            flatten(me->active_mesh(), ws);
314
 
315
            return;
316
        }
317
 
318
        void console_save(MeshEditor* me, const std::vector<std::string> & args)
319
        {
320
            if(wantshelp(args)) {
321
                me->printf("usage: save <name->x3d|name->obj> ");
322
 
323
                return;
324
            }
325
            const string& file_name = args[0];
326
            if(args.size() == 1){
327
                if(file_name.substr(file_name.length()-4,file_name.length())==".obj"){
328
                    obj_save(file_name, me->active_mesh());
329
 
330
                    return;
331
                }
332
                else if(file_name.substr(file_name.length()-4,file_name.length())==".off"){
333
                    off_save(file_name, me->active_mesh());
334
 
335
                    return;
336
                }
337
                else if(file_name.substr(file_name.length()-4,file_name.length())==".x3d"){
338
                    x3d_save(file_name, me->active_mesh());
339
 
340
                    return;
341
                }
342
                me->printf("unknown format");
343
                return;
344
            }
345
            me->printf("usage: save <name->x3d|name->obj> ");
346
        }
347
 
348
 
349
        void console_refine_edges(MeshEditor* me, const std::vector<std::string> & args)
350
        {
351
            if(wantshelp(args)) {
352
                me->printf("usage: refine.split_edges <length>");
353
                me->printf("splits edges longer than <length>; default is 0.5 times average length");
354
                return;
355
            }
356
 
357
            me->save_active_mesh();
358
 
359
            float thresh = 0.5f;
360
 
361
            if(args.size() > 0){
362
                istringstream a0(args[0]);
363
                a0 >> thresh;
364
            }
365
 
366
            float avg_length = average_edge_length(me->active_mesh());
367
 
368
            refine_edges(me->active_mesh(), thresh * avg_length);
369
 
370
            return;
371
 
372
        }
373
 
374
        void console_refine_faces(MeshEditor* me, const std::vector<std::string> & args)
375
        {
376
            if(wantshelp(args)) {
377
                me->printf("usage: refine.split_faces ");
378
                me->printf("usage:  Takes no arguments. Inserts a vertex at the centre of each face.");
379
 
380
                return;
381
            }
382
            me->save_active_mesh();
383
 
384
            triangulate_by_vertex_face_split(me->active_mesh());
385
 
386
            return;
387
 
388
        }
389
 
390
        void console_cc_subdivide(MeshEditor* me, const std::vector<std::string> & args)
391
        {
392
            if(wantshelp(args)) {
393
                me->printf("usage: subdivide.catmull_clark ");
394
                me->printf("Does one step of Catmull-Clark subdivision");
395
 
396
                return;
397
            }
398
            me->save_active_mesh();
399
 
400
            cc_split(me->active_mesh(),me->active_mesh());
401
            cc_smooth(me->active_mesh());
402
 
403
            return;
404
        }
405
 
406
        void console_loop_subdivide(MeshEditor* me, const std::vector<std::string> & args)
407
        {
408
            if(wantshelp(args)) {
409
                me->printf("usage: subdivide.loop");
410
                me->printf("Does one step of Loop subdivision");
411
 
412
                return;
413
            }
414
            me->save_active_mesh();
415
 
416
            loop_split(me->active_mesh(),me->active_mesh());
417
            loop_smooth(me->active_mesh());
418
 
419
            return;
420
        }
421
 
422
        void console_root3_subdivide(MeshEditor* me, const std::vector<std::string> & args)
423
        {
424
            if(wantshelp(args)) {
425
                me->printf("usage: subdivide.root3");
426
                me->printf("Does one step of sqrt(3) subdivision");
427
 
428
                return;
429
            }
430
            me->save_active_mesh();
431
 
432
            root3_subdivide(me->active_mesh(),me->active_mesh());
433
 
434
            return;
435
        }
436
 
437
 
438
        void console_doosabin_subdivide(MeshEditor* me, const std::vector<std::string> & args)
439
        {
440
            if(wantshelp(args)) {
441
                me->printf("usage: subdivide.doo_sabin ");
442
                me->printf("Does one step of Doo-Sabin Subdivision");
443
 
444
                return;
445
            }
446
            me->save_active_mesh();
447
 
448
            cc_split(me->active_mesh(),me->active_mesh());
449
            dual(me->active_mesh());
450
 
451
            return;
452
        }
453
 
454
        void console_butterfly_subdivide(MeshEditor* me, const std::vector<std::string> & args)
455
        {
456
            if(wantshelp(args)) {
457
                me->printf("usage: subdivide.butterfly ");
458
                me->printf("Does one step of Modified Butterfly Subdivision");
459
 
460
                return;
461
            }
462
            me->save_active_mesh();
463
 
464
            butterfly_subdivide(me->active_mesh(),me->active_mesh());
465
 
466
            return;
467
        }
468
 
469
        void console_dual(MeshEditor* me, const std::vector<std::string> & args)
470
        {
471
            if(wantshelp(args))
472
            {
473
                me->printf("usage: dual ");
474
                me->printf("Produces the dual by converting each face to a vertex placed at the barycenter.");
475
                return;
476
            }
477
            me->save_active_mesh();
478
 
479
            dual(me->active_mesh());
480
 
481
            return;
482
        }
483
 
484
 
485
        void console_minimize_curvature(MeshEditor* me, const std::vector<std::string> & args)
486
        {
487
            if(wantshelp(args))
488
            {
489
                me->printf("usage: optimize.minimize_curvature <anneal>");
490
                me->printf("Flip edges to minimize mean curvature.");
491
                me->printf("If anneal is true, simulated annealing (slow) is used rather than a greedy scheme");
492
                return;
493
            }
494
            me->save_active_mesh();
495
 
496
            bool anneal=false;
497
            if(args.size() > 0)
498
            {
499
                istringstream a0(args[0]);
500
                a0 >> anneal;
501
            }
502
 
503
            minimize_curvature(me->active_mesh(), anneal);
504
            me->post_create_display_list();
505
            return;
506
        }
507
 
508
        void console_minimize_dihedral(MeshEditor* me, const std::vector<std::string> & args)
509
        {
510
            if(wantshelp(args))
511
            {
512
                me->printf("usage: optimize.minimize_dihedral <iter> <anneal> <use_alpha> <gamma> ");
513
                me->printf("Flip edges to minimize dihedral angles.");
514
                me->printf("Iter is the max number of iterations. anneal tells us whether to use ");
515
                me->printf("simulated annealing and not greedy optimization. use_alpha (default=true) ");
516
                me->printf("means to use angle and not cosine of anglegamma (default=4) is the power ");
517
                me->printf("to which we raise the dihedral angle");
518
                return;
519
            }
520
            me->save_active_mesh();
521
 
522
            int iter = 1000;
523
            if(args.size() > 0)
524
            {
525
                istringstream a0(args[0]);
526
                a0 >> iter;
527
            }
528
 
529
            bool anneal = false;
530
            if(args.size() > 1)
531
            {
532
                istringstream a0(args[1]);
533
                a0 >> anneal;
534
            }
535
 
536
            bool use_alpha = true;
537
            if(args.size() > 2)
538
            {
539
                istringstream a0(args[2]);
540
                a0 >> use_alpha;
541
            }
542
 
543
            float gamma = 4.0f;
544
            if(args.size() > 3)
545
            {
546
                istringstream a0(args[3]);
547
                a0 >> gamma;
548
            }
549
 
550
 
551
            minimize_dihedral_angle(me->active_mesh(), iter, anneal, use_alpha, gamma);
552
            return;
553
        }
554
 
555
        void console_maximize_min_angle(MeshEditor* me, const std::vector<std::string> & args)
556
        {
557
            if(wantshelp(args))
558
            {
559
                me->printf("usage: optimize.maximize_min_angle <thresh> <anneal>");
560
                me->printf("Flip edges to maximize min angle - to make mesh more Delaunay.");
561
                me->printf("If the dot product of the normals between adjacent faces < thresh");
562
                me->printf("no flip will be made. anneal selects simulated annealing rather ");
563
                me->printf("nthan greedy optimization.");
564
                return;
565
            }
566
            me->save_active_mesh();
567
 
568
            float thresh = 0.0f;
569
            if(args.size() > 0)
570
            {
571
                istringstream a0(args[0]);
572
                a0 >> thresh;
573
            }
574
            bool anneal = false;
575
            if(args.size() > 1)
576
            {
577
                istringstream a0(args[1]);
578
                a0 >> anneal;
579
            }
580
            maximize_min_angle(me->active_mesh(),thresh,anneal);
581
            return;
582
        }
583
 
584
 
585
        void console_optimize_valency(MeshEditor* me, const std::vector<std::string> & args)
586
        {
587
            if(wantshelp(args))
588
            {
589
                me->printf("usage: optimize.valency <anneal> ");
590
                me->printf("Optimizes valency for triangle meshes. Anneal selects simulated annealing rather than greedy optim.");
591
                return;
592
            }
593
            me->save_active_mesh();
594
 
595
            bool anneal = false;
596
            if(args.size() > 0)
597
            {
598
                istringstream a0(args[0]);
599
                a0 >> anneal;
600
            }
601
            optimize_valency(me->active_mesh(), anneal);
602
            return;
603
        }
604
 
605
            void console_analyze(MeshEditor* me, const std::vector<std::string> & args)
606
            {
607
                if(wantshelp(args))
608
                {
609
                    me->printf("usage:  harmonics.analyze");
610
                    me->printf("Creates the Laplace Beltrami operator for the mesh and finds all eigensolutions.");
611
                    me->printf("It also projects the vertices onto the eigenvectors - thus transforming the mesh");
612
                    me->printf("to this basis.");
613
                    me->printf("Note that this will stall the computer for a large mesh - as long as we use Lapack.");
614
                    return;
615
                }
616
                me->harmonics_analyze_mesh();
617
                return;
618
            }
619
 
620
 
621
        //    void console_partial_reconstruct(MeshEditor* me, const std::vector<std::string> & args)
622
        //    {
623
        //        if(args.size() != 3)
624
        //            me->printf("usage: haramonics.partial_reconstruct <e0> <e1> <s>");
625
        //
626
        //        if(wantshelp(args)) {
627
        //            me->printf("Reconstruct from projections onto eigenvectors. The two first arguments indicate");
628
        //            me->printf("the eigenvector interval that we reconstruct from. The last argument is the ");
629
        //            me->printf("scaling factor. Thus, for a vertex, v, the formula for computing the position, p, is:");
630
        //            me->printf("for (i=e0; i<=e1;++i) p += proj[i] * Q[i][v] * s;");
631
        //            me->printf("where proj[i] is the 3D vector containing the x, y, and z projections of the mesh onto");
632
        //            me->printf("eigenvector i. Q[i][v] is the v'th coordinate of the i'th eigenvector.");
633
        //            me->printf("Note that if vertex coordinates are not first reset, the result is probably unexpected.");
634
        //        }
635
        //        me->save_active_mesh();
636
        //
637
        //        if(args.size() != 3)
638
        //            return;
639
        //
640
        //        int E0,E1;
641
        //        float scale;
642
        //        istringstream a0(args[0]);
643
        //        a0 >> E0;
644
        //        istringstream a1(args[1]);
645
        //        a1 >> E1;
646
        //        istringstream a2(args[2]);
647
        //        a2 >> scale;
648
        //        me->harmonics_partial_reconstruct(E0,E1,scale);
649
        //        return;
650
        //    }
651
        //
652
        //    void console_reset_shape(MeshEditor* me, const std::vector<std::string> & args)
653
        //    {
654
        //        if(wantshelp(args))
655
        //        {
656
        //            me->printf("usage: harmonics.reset_shape ");
657
        //            me->printf("Simply sets all vertices to 0,0,0. Call this before doing partial_reconstruct");
658
        //            me->printf("unless you know what you are doing.");
659
        //            return;
660
        //        }
661
        //        me->save_active_mesh();
662
        //        me->harmonics_reset_shape();
663
        //        return;
664
        //    }
665
 
666
 
667
        void console_close_holes(MeshEditor* me, const std::vector<std::string> & args)
668
        {
669
            if(wantshelp(args))
670
            {
671
                me->printf("usage: cleanup.close_holes");
672
                me->printf("This function closes holes. It simply follows the loop of halfvectors which");
673
                me->printf("enclose the hole and add a face to which they all point.");
674
                return;
675
            }
676
            me->save_active_mesh();
677
 
678
            close_holes(me->active_mesh());
679
            return;
680
        }
681
 
682
        void console_reload(MeshEditor* me, const std::vector<std::string> & args)
683
        {
684
            if(wantshelp(args))
685
            {
686
                me->printf("usage:  load <file>");
687
                me->printf("(Re)loads the current file if no argument is given, but");
688
                me->printf("if an argument is given, then that becomes the current file");
689
                return;
690
            }
691
            me->save_active_mesh();
692
 
693
            if(!me->reload_active_from_file(args.size() > 0 ? args[0]:""))
694
                me->printf("failed to load");
695
 
696
            return;
697
        }
698
 
699
 
700
        void console_add_mesh(MeshEditor* me, const std::vector<std::string> & args)
701
        {
702
            if(wantshelp(args))
703
            {
704
                me->printf("usage:  add_mesh <file>");
705
                me->printf("Loads the file but without clearing the mesh. Thus, the loaded mesh is added to the");
706
                me->printf("current model.");
707
                return;
708
            }
709
            me->save_active_mesh();
710
 
711
            if(!me->add_to_active_from_file(args.size() > 0 ? args[0]:""))
712
                me->printf("failed to load");
713
 
714
            return;
715
        }
716
 
717
        void console_valid(MeshEditor* me, const std::vector<std::string> & args)
718
        {
719
            if(wantshelp(args))
720
            {
721
                me->printf("usage:  validity");
722
                me->printf("Tests validity of Manifold");
723
                return;
724
            }
725
            if(valid(me->active_mesh()))
726
                me->printf("Mesh is valid");
727
            else
728
                me->printf("Mesh is invalid - check console output");
729
            return;
730
        }
731
 
732
        void console_Dijkstra(MeshEditor* me, const std::vector<std::string> & args)
733
        {
734
            if(wantshelp(args))
735
            {
736
                me->printf("usage:  Dijkstra");
737
                return;
738
            }
739
 
740
            Manifold& m = me->active_mesh();
741
 
742
 
743
            VertexAttributeVector<double> dist(m.allocated_vertices(), DBL_MAX);
744
            VertexAttributeVector<int> visited(m.allocated_vertices(), 0);
745
            VertexID v = *m.vertices_begin();
746
            dist[v]=0;
747
            priority_queue<pair<double,VertexID>> pq;
748
            pq.push(make_pair(-dist[v], v));
749
            double max_dist;
750
            while(!pq.empty())
751
            {
752
                VertexID v = pq.top().second;
753
                max_dist = dist[v];
754
                pq.pop();
755
 
756
                if(!visited[v]){
757
                    visited[v]=1;
758
 
759
                    for(Walker w = m.walker(v); !w.full_circle(); w = w.circulate_vertex_ccw())
760
                        if(!visited[w.vertex()])
761
                        {
762
                            double d = dist[v] + length(m, w.halfedge());
763
                            if(d<dist[w.vertex()]) {
764
                                dist[w.vertex()] = d;
765
                                pq.push(make_pair(-d, w.vertex()));
766
                            }
767
                        }
768
                }
769
            }
770
 
771
            for(auto vid : m.vertices()) {
772
                DebugRenderer::vertex_colors[vid] = Vec3f(1-dist[vid]/max_dist,0,0);
773
                cout << dist[vid] << endl;
774
            }
775
            for(auto fid : m.faces())
776
                DebugRenderer::face_colors[fid] = Vec3f(0.3);
777
 
778
            for(auto hid : m.halfedges()) {
779
                Walker w = m.walker(hid);
780
                DebugRenderer::edge_colors[hid] = Vec3f(1.0-max(dist[w.vertex()],dist[w.opp().vertex()])/max_dist,0,0);
781
            }
782
            return;
783
        }
784
 
785
        void console_info(MeshEditor* me, const std::vector<std::string> & args)
786
        {
787
            if(wantshelp(args))
788
            {
789
                me->printf("usage:  info");
790
                me->printf("Provides information about mesh.");
791
                return;
792
            }
793
            Vec3d p0, p7;
794
            bbox(me->active_mesh(), p0, p7);
795
            stringstream bbox_corners;
796
            bbox_corners << p0 << " - " << p7 << endl;
797
            me->printf("Bounding box corners : %s", bbox_corners.str().c_str());
798
            map<int,int> val_hist;
799
 
800
            for(VertexIDIterator vi = me->active_mesh().vertices_begin(); vi != me->active_mesh().vertices_end(); ++vi)
801
            {
802
                int val = valency(me->active_mesh(), *vi);
803
                if(val_hist.find(val) == val_hist.end())
804
                    val_hist[val] = 0;
805
                ++val_hist[val];
806
            }
807
 
808
            me->printf("Valency histogam");
809
            for(map<int,int>::iterator iter = val_hist.begin(); iter != val_hist.end(); ++iter)
810
            {
811
                stringstream vhl;
812
                vhl << iter->first << ", " << iter->second;
813
                me->printf("%d, %d", iter->first, iter->second);
814
            }
815
 
816
            me->printf("Mesh contains %d faces", me->active_mesh().no_faces());
817
            me->printf("Mesh contains %d halfedges", me->active_mesh().no_halfedges());
818
            me->printf("Mesh contains %d vertices", me->active_mesh().no_vertices());
819
            return;
820
        }
821
 
822
 
823
        void console_simplify(MeshEditor* me, const std::vector<std::string> & args)
824
        {
825
            if(wantshelp(args))
826
            {
827
                me->printf("usage: simplify <fraction> ");
828
                me->printf("Performs Garland Heckbert (quadric based) mesh simplification.");
829
                me->printf("The only argument is the fraction of vertices to keep.");
830
                return;
831
            }
832
            me->save_active_mesh();
833
 
834
            float keep_fraction;
835
            if(args.size() == 0)
836
            {
837
                me->printf("you must specify fraction of vertices to keep");
838
                return;
839
            }
840
            istringstream a0(args[0]);
841
            a0 >> keep_fraction;
842
 
843
            Vec3d p0, p7;
844
            bbox(me->active_mesh(), p0, p7);
845
            Vec3d d = p7-p0;
846
            float s = 1.0/d.max_coord();
847
            Vec3d pcentre = (p7+p0)/2.0;
848
            for(VertexIDIterator vi = me->active_mesh().vertices_begin(); vi != me->active_mesh().vertices_end(); ++vi){
849
                me->active_mesh().pos(*vi) = (me->active_mesh().pos(*vi) - pcentre) * s;
850
            }
851
            cout << "Timing the Garland Heckbert (quadric based) mesh simplication..." << endl;
852
            Timer timer;
853
            timer.start();
854
 
855
            //simplify
856
            quadric_simplify(me->active_mesh(),keep_fraction,0.0001f,true);
857
 
858
            cout << "Simplification complete, process time: " << timer.get_secs() << " seconds" << endl;
859
 
860
            //clean up the mesh, a lot of edges were just collapsed
861
            me->active_mesh().cleanup();
862
 
863
            for(VertexIDIterator vi = me->active_mesh().vertices_begin(); vi != me->active_mesh().vertices_end(); ++vi)
864
                me->active_mesh().pos(*vi) = me->active_mesh().pos(*vi)*d.max_coord() + pcentre;
865
            return;
866
        }
867
 
868
        void console_vertex_noise(MeshEditor* me, const std::vector<std::string> & args)
869
        {
870
            if(wantshelp(args))
871
            {
872
                me->printf("usage: noise.perturb_vertices <amplitude>");
873
                me->printf("adds a random vector to each vertex. A random vector in the unit cube is generated and");
874
                me->printf("to ensure an isotropic distribution, vectors outside the unit ball are discarded.");
875
                me->printf("The vector is multiplied by the average edge length and then by the amplitude specified.");
876
                me->printf("If no amplitude is specified, the default (0.5) is used.");
877
                return;
878
            }
879
            me->save_active_mesh();
880
 
881
            float avg_length = average_edge_length(me->active_mesh());
882
 
883
            float noise_amplitude = 0.5f;
884
            if(args.size() > 0) {
885
                istringstream a0(args[0]);
886
                a0 >> noise_amplitude;
887
            }
888
 
889
            gel_srand(0);
890
            for(VertexIDIterator vi = me->active_mesh().vertices_begin(); vi != me->active_mesh().vertices_end(); ++vi){
891
                Vec3d v;
892
                do{
893
                    v = Vec3d(gel_rand(),gel_rand(),gel_rand());
894
                    v /= (float)(GEL_RAND_MAX);
895
                    v -= Vec3d(0.5);
896
                    v *= 2.0;
897
                }
898
                while(sqr_length(v) > 1.0);
899
 
900
                v *= noise_amplitude;
901
                v *= avg_length;
902
                me->active_mesh().pos(*vi) += v;
903
            }
904
            return;
905
        }
906
 
907
        void console_perpendicular_vertex_noise(MeshEditor* me, const std::vector<std::string> & args)
908
        {
909
            if(wantshelp(args)) {
910
                me->printf("usage: noise.perturb_vertices_perpendicular <amplitude>");
911
                me->printf("adds the normal times a random scalar times amplitude times");
912
                me->printf("times average edge length to the vertex. (default amplitude=0.5)");
913
                return;
914
            }
915
            me->save_active_mesh();
916
 
917
            float avg_length = average_edge_length(me->active_mesh());
918
 
919
            float noise_amplitude = 0.5;
920
            if(args.size() > 0)
921
            {
922
                istringstream a0(args[0]);
923
                a0 >> noise_amplitude;
924
            }
925
 
926
            VertexAttributeVector<Vec3d> normals(me->active_mesh().allocated_vertices());
927
            for(VertexIDIterator vi = me->active_mesh().vertices_begin(); vi != me->active_mesh().vertices_end(); ++vi)
928
                normals[*vi] = normal(me->active_mesh(), *vi);
929
 
930
            gel_srand(0);
931
            for(VertexIDIterator vi = me->active_mesh().vertices_begin(); vi != me->active_mesh().vertices_end(); ++vi)
932
            {
933
                float rval = 0.5-gel_rand() / float(GEL_RAND_MAX);
934
                me->active_mesh().pos(*vi) += normals[*vi]*rval*noise_amplitude*avg_length*2.0;
935
            }
936
            return;
937
        }
938
 
939
        void console_noisy_flips(MeshEditor* me, const std::vector<std::string> & args)
940
        {
941
            if(wantshelp(args)){
942
                me->printf("usage:  noise.perturb_topology <iter>");
943
                me->printf("Perform random flips. iter (default=1) is the number of iterations.");
944
                me->printf("mostly for making nasty synthetic test cases.");
945
                return;
946
            }
947
            me->save_active_mesh();
948
 
949
            int iter = 1;
950
            if(args.size() > 0){
951
                istringstream a0(args[0]);
952
                a0 >> iter;
953
            }
954
 
955
            randomize_mesh(me->active_mesh(),  iter);
956
            return;
957
        }
958
 
959
        void console_laplacian_smooth(MeshEditor* me, const std::vector<std::string> & args)
960
        {
961
            if(wantshelp(args)) {
962
                me->printf("usage:  smooth.laplacian <weight> <iter>");
963
                me->printf("Perform Laplacian smoothing. weight is the scaling factor for the Laplacian.");
964
                me->printf("default weight = 1.0. Default number of iterations = 1");
965
                return;
966
            }
967
            me->save_active_mesh();
968
 
969
            float t=1.0;
970
            if(args.size() > 0){
971
                istringstream a0(args[0]);
972
                a0 >> t;
973
            }
974
            int iter = 1;
975
            if(args.size()>1){
976
                istringstream a0(args[1]);
977
                a0 >> iter;
978
            }
979
            Util::Timer tim;
980
            tim.start();
981
            /// Simple laplacian smoothing with an optional weight.
982
            laplacian_smooth(me->active_mesh(), t, iter);
983
            cout << "It took "<< tim.get_secs();
984
            return;
985
        }
986
 
987
 
988
        void console_mean_curvature_smooth(MeshEditor* me, const std::vector<std::string> & args){
989
            if(wantshelp(args)) {
990
                me->printf("usage:  smooth.mean_curvature <weight> <iter>");
991
                me->printf("Perform mean curvature smoothing. weight is the scaling factor for the");
992
                me->printf("mean curvature vector which has been normalized by dividing by edge lengths");
993
                me->printf("this allows for larger steps as suggested by Desbrun et al.");
994
                me->printf("default weight = 1.0. Default number of iterations = 1");
995
                return;
996
            }
997
            me->save_active_mesh();
998
 
999
            double t=1.0;
1000
            if(args.size() > 0){
1001
                istringstream a0(args[0]);
1002
                a0 >> t;
1003
            }
1004
            int iter=1;
1005
            if(args.size() > 1){
1006
                istringstream a0(args[1]);
1007
                a0 >> iter;
1008
            }
1009
            VertexAttributeVector<Vec3d> new_pos(me->active_mesh().allocated_vertices());
1010
            for(int j = 0; j < iter; ++j){
1011
                for(VertexIDIterator v = me->active_mesh().vertices_begin(); v != me->active_mesh().vertices_end(); ++v) {
1012
                    Vec3d m;
1013
                    double w_sum;
1014
                    unnormalized_mean_curvature_normal(me->active_mesh(), *v, m, w_sum);
1015
                    new_pos[*v] = Vec3d(me->active_mesh().pos(*v))  + (t * m/w_sum);
1016
                }
1017
                for(VertexIDIterator v = me->active_mesh().vertices_begin(); v != me->active_mesh().vertices_end(); ++v)
1018
                    me->active_mesh().pos(*v) = new_pos[*v];
1019
            }
1020
            return;
1021
        }
1022
 
1023
        void console_taubin_smooth(MeshEditor* me, const std::vector<std::string> & args)
1024
        {
1025
            if(wantshelp(args)){
1026
                me->printf("usage:  smooth.taubin <iter>");
1027
                me->printf("Perform Taubin smoothing. iter (default=1) is the number of iterations.");
1028
                return;
1029
            }
1030
            me->save_active_mesh();
1031
 
1032
            int iter = 1;
1033
            if(args.size() > 0){
1034
                istringstream a0(args[0]);
1035
                a0 >> iter;
1036
            }
1037
            /// Taubin smoothing is similar to laplacian smoothing but reduces shrinkage
1038
            taubin_smooth(me->active_mesh(),  iter);
1039
 
1040
            return;
1041
        }
1042
 
1043
        void console_fvm_anisotropic_smooth(MeshEditor* me, const std::vector<std::string> & args)
1044
        {
1045
            if(wantshelp(args)){
1046
                me->printf("usage: smooth.fuzzy_vector_median <iter>");
1047
                me->printf("Smooth normals using fuzzy vector median smoothing. iter (default=1) is the number of iterations");
1048
                me->printf("This function does a very good job of preserving sharp edges.");
1049
                return;
1050
            }
1051
            me->save_active_mesh();
1052
 
1053
            int iter=1;
1054
            if(args.size() > 0){
1055
                istringstream a0(args[0]);
1056
                a0 >> iter;
1057
            }
1058
            // Fuzzy vector median smoothing is effective when it comes to preserving sharp edges.
1059
            anisotropic_smooth(me->active_mesh(),  iter, FVM_NORMAL_SMOOTH);
1060
 
1061
            return;
1062
        }
1063
 
1064
        void console_bilateral_anisotropic_smooth(MeshEditor* me, const std::vector<std::string> & args)
1065
        {
1066
            if(wantshelp(args)){
1067
                me->printf("usage: smooth.fuzzy_vector_median <iter>");
1068
                me->printf("Smooth normals using fuzzy vector median smoothing. iter (default=1) is the number of iterations");
1069
                me->printf("This function does a very good job of preserving sharp edges.");
1070
                return;
1071
            }
1072
            me->save_active_mesh();
1073
 
1074
            int iter=1;
1075
            if(args.size() > 0){
1076
                istringstream a0(args[0]);
1077
                a0 >> iter;
1078
            }
1079
 
1080
            anisotropic_smooth(me->active_mesh(),  iter, BILATERAL_NORMAL_SMOOTH);
1081
 
1082
            return;
1083
        }
1084
 
1085
        void console_triangulate(MeshEditor* me, const std::vector<std::string> & args)
1086
        {
1087
            if(wantshelp(args)) {
1088
                me->printf("usage:  triangulate");
1089
                me->printf("This function triangulates all non triangular faces of the mesh.");
1090
                me->printf("you may want to call it after hole closing. For a polygon it simply connects");
1091
                me->printf("the two closest vertices in a recursive manner until only triangles remain");
1092
                return;
1093
            }
1094
            me->save_active_mesh();
1095
 
1096
            shortest_edge_triangulate(me->active_mesh());
1097
            me->active_mesh().cleanup();
1098
            valid(me->active_mesh());
1099
            return;
1100
        }
1101
 
1102
 
1103
        void console_remove_caps(MeshEditor* me, const std::vector<std::string> & args)
1104
        {
1105
            if(wantshelp(args)) {
1106
                me->printf("usage:  cleanup.remove_caps thresh");
1107
                me->printf("Remove caps (triangles with one very big angle). The thresh argument is the fraction of PI to");
1108
                me->printf("use as threshold for big angle. Default is 0.85. Caps are removed by flipping.");
1109
                return;
1110
            }
1111
            me->save_active_mesh();
1112
 
1113
            float t = 0.85f;
1114
            if(args.size() > 0){
1115
                istringstream a0(args[0]);
1116
                a0 >> t;
1117
            }
1118
            remove_caps(me->active_mesh(), static_cast<float>(M_PI) *t);
1119
            me->active_mesh().cleanup();
1120
 
1121
            return;
1122
        }
1123
 
1124
        void console_remove_needles(MeshEditor* me, const std::vector<std::string> & args)
1125
        {
1126
            if(wantshelp(args)){
1127
                me->printf("usage: cleanup.remove_needles <thresh>");
1128
                me->printf("Removes very short edges by collapse. thresh is multiplied by the average edge length");
1129
                me->printf("to get the length shorter than which we collapse. Default = 0.1");
1130
                return;
1131
            }
1132
            me->save_active_mesh();
1133
 
1134
            float thresh = 0.1f;
1135
            if(args.size() > 0){
1136
                istringstream a0(args[0]);
1137
                a0 >> thresh;
1138
            }
1139
            float avg_length = average_edge_length(me->active_mesh());
1140
            remove_needles(me->active_mesh(), thresh * avg_length);
1141
            me->active_mesh().cleanup();
1142
 
1143
            return;
1144
        }
1145
 
1146
        void console_undo(MeshEditor* me, const std::vector<std::string> & args)
1147
        {
1148
            if(wantshelp(args)) {
1149
                me->printf("usage:  undo");
1150
                me->printf("This function undoes one operation. Repeated undo does nothing");
1151
                return;
1152
            }
1153
            me->restore_active_mesh();
1154
            return;
1155
        }
1156
 
1157
 
1158
    }
1159
 
1160
 
1161
    void MeshEditor::register_console_function(const std::string& name,
1162
                                               const std::function<void(MeshEditor*, const std::vector<std::string>&)>& con_fun,
1163
                                               const std::string& help_txt)
1164
    {
1165
        std::function<void (const std::vector<std::string>&)> f = bind(con_fun, this, placeholders::_1);
1166
        theConsole.reg_cmdN(name, f, help_txt);
1167
    }
1168
 
1169
    void MeshEditor::keyparse(unsigned short key){
1170
        //toggle console with ESC
1171
        if (key == 27)
1172
        {
1173
            console_visible = !console_visible;
1174
            return;
1175
        }
1176
 
1177
        if (console_visible)
1178
        {
1179
            theConsole.keyboard(key);
1180
            if(key == 13)
1181
            {
1182
                active_visobj().post_create_display_list();
1183
            }
1184
            return;
1185
        }
1186
        else {
1187
 
1188
            switch(key) {
1189
                case 'q': exit(0);
1190
                case '\033':
1191
                    console_visible = false;
1192
                    break;
1193
                case '1':
1194
                case '2':
1195
                case '3':
1196
                case '4':
1197
                case '5':
1198
                case '6':
1199
                case '7':
1200
                case '8':
1201
                case '9':
1202
                {
1203
                    int w[4];
1204
                    glGetIntegerv(GL_VIEWPORT, w);
1205
                    active = key - '1';
1206
                    active_view_control().reshape(w[2],w[3]);
1207
                }
1208
                    break;
1209
                case 'f': display_smooth_shading = !display_smooth_shading; break;
1210
                case 'w':
1211
                    display_render_mode = "wire"; break;
1212
                case 'n':
1213
                    display_render_mode = "normal"; break;
1214
                case 'i':
1215
                    display_render_mode = "isophotes"; break;
1216
                case 'r':
1217
                    display_render_mode = "reflection"; break;
1218
                case 'h':
1219
                    display_render_mode = "harmonics"; break;
1220
                case 't':
1221
                    display_render_mode = "toon"; break;
1222
                case 'g':
1223
                    display_render_mode = "glazed"; break;
1224
                case 'a':
1225
                    display_render_mode = "ambient_occlusion"; break;
1226
                case 'c':
1227
                    display_render_mode = "copper"; break;
1228
                case 'C':
1229
                    display_render_mode = "curvature_lines"; break;
1230
                case 'M':
1231
                    display_render_mode = "mean_curvature"; break;
1232
                case 'G':
1233
                    display_render_mode = "gaussian_curvature"; break;
1234
            }
1235
 
1236
            if(key != '\033') post_create_display_list();
1237
        }
1238
 
1239
    }
1240
 
1241
    void MeshEditor::printf(const char* format, ...)
1242
    {
1243
        //format text
1244
        char buffer[1024];
1245
        va_list args;
1246
        va_start(args, format);
1247
        vsprintf(buffer, format, args);
1248
        va_end(args);
1249
        theConsole.print(buffer);
1250
    }
1251
 
1252
    void MeshEditor::key_up(){theConsole.key_up();}
1253
    void MeshEditor::key_down(){theConsole.key_down();}
1254
    void MeshEditor::key_left(){theConsole.key_left();}
1255
    void MeshEditor::key_right(){theConsole.key_right();}
1256
    void MeshEditor::key_home(){theConsole.key_home();}
1257
    void MeshEditor::key_end(){theConsole.key_end();}
1258
 
1259
    void MeshEditor::grab_ball(TrackBallAction action, const CGLA::Vec2i& pos){
1260
        active_view_control().grab_ball(action, pos);
1261
    }
1262
    void MeshEditor::roll_ball(const CGLA::Vec2i& pos){
1263
        active_view_control().roll_ball(pos);
1264
    }
1265
    void MeshEditor::release_ball(){
1266
        active_view_control().release_ball();
1267
    }
1268
    bool MeshEditor::try_spinning_ball(){
1269
        return active_view_control().try_spin();
1270
    }
1271
 
656 janba 1272
    bool MeshEditor::grab_mesh(const CGLA::Vec2i& pos)
1273
    {
1274
        if(depth_pick(pos[0], pos[1], depth))
1275
        {
1276
            dragging = true;
1277
            mouse_x = pos[0];
1278
            mouse_y = pos[1];
1279
            Vec3d p0 = screen2world(mouse_x, mouse_y, depth);
1280
            Manifold& m = active_mesh();
1281
            active_visobj().save_old();
1282
            Vec3d c;
1283
            float r;
1284
            bsphere(m, c, r);
1285
            for(auto vid : m.vertices())
1286
            {
1287
                double l = sqr_length(p0-m.pos(vid));
1288
                weight_vector[vid] = exp(-l/(0.01*r*r));
1289
            }
1290
            return true;
1291
        }
1292
        return false;
1293
    }
1294
 
1295
    bool MeshEditor::drag_mesh(const CGLA::Vec2i& pos)
1296
    {
1297
        if(dragging)
1298
        {
1299
            Vec3d p0 = screen2world(mouse_x, mouse_y, depth);
1300
            Vec3d p1 = screen2world(pos[0], pos[1], depth);
1301
            Vec3d v = p1-p0;
1302
            Manifold& m = active_mesh();
1303
            for(auto vid : m.vertices())
1304
                m.pos(vid) = active_visobj().mesh_old().pos(vid) + weight_vector[vid] * v;
1305
            post_create_display_list();
1306
            return true;
1307
        }
1308
        return false;
1309
    }
1310
 
1311
    void MeshEditor::release_mesh()
1312
    {
1313
        dragging = false;
1314
    }
647 janba 1315
 
656 janba 1316
 
1317
 
647 janba 1318
    void MeshEditor::display(int scale){
1319
        glClearColor(1, 1, 1, 0);
1320
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1321
        active_visobj().display(display_render_mode, theConsole, display_smooth_shading, display_gamma);
1322
        if(console_visible)
1323
        {
1324
            glUseProgram(0);
1325
            theConsole.display(scale);
1326
        }
1327
 
1328
    }
1329
 
1330
    void MeshEditor::reshape(int w, int h) {
1331
        for(VisObj& v : vo)
1332
            v.view_control().reshape(w, h);
1333
    }
1334
 
1335
    void MeshEditor::init() {
1336
        glewInit();
1337
 
1338
        GLint vp[4];
1339
        glGetIntegerv(GL_VIEWPORT, vp);
1340
        for(VisObj& vis_obj : vo)
1341
            vis_obj.view_control().reshape(vp[2], vp[3]);
1342
 
1343
        glEnable(GL_CULL_FACE);
1344
        glCullFace(GL_BACK);
1345
        glEnable(GL_LIGHTING);
1346
        glEnable(GL_LIGHT0);
1347
        glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
1348
 
1349
        glMatrixMode(GL_MODELVIEW);
1350
        glLoadIdentity();
1351
        glClearColor(1,1,0, 0.f);
1352
        glColor4f(1.0f, 1.0f, 1.0f, 0.f);
1353
        float material[4] = {1,1,1,1};
1354
        glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material);
1355
        glEnable(GL_DEPTH_TEST);
1356
 
1357
        register_console_function("simplify", console_simplify,"");
1358
 
1359
        register_console_function("ridge_lines", console_ridge_lines,"");
1360
 
1361
        register_console_function("smooth.mean_curvature", console_mean_curvature_smooth,"");
1362
        register_console_function("smooth.laplacian", console_laplacian_smooth,"");
1363
        register_console_function("smooth.taubin", console_taubin_smooth,"");
1364
        register_console_function("smooth.fuzzy_vector_median_anisotropic", console_fvm_anisotropic_smooth ,"");
1365
        register_console_function("smooth.bilateral_anisotropic", console_bilateral_anisotropic_smooth ,"");
1366
 
1367
        register_console_function("optimize.valency", console_optimize_valency,"");
1368
        register_console_function("optimize.minimize_dihedral_angles", console_minimize_dihedral,"");
1369
        register_console_function("optimize.minimize_curvature", console_minimize_curvature,"");
1370
        register_console_function("optimize.maximize_min_angle", console_maximize_min_angle,"");
1371
        register_console_function("cleanup.close_holes", console_close_holes,"");
1372
        register_console_function("load_mesh", console_reload,"");
1373
        register_console_function("add_mesh", console_add_mesh,"");
1374
 
1375
        register_console_function("cleanup.remove_caps", console_remove_caps,"");
1376
        register_console_function("cleanup.remove_needles", console_remove_needles,"");
1377
        register_console_function("triangulate", console_triangulate,"");
1378
        register_console_function("refine.split_edges", console_refine_edges,"");
1379
        register_console_function("refine.split_faces", console_refine_faces,"");
1380
        register_console_function("subdivide.catmull_clark", console_cc_subdivide,"");
1381
        register_console_function("subdivide.loop", console_loop_subdivide,"");
1382
        register_console_function("subdivide.root3", console_root3_subdivide,"");
1383
        register_console_function("subdivide.doo_sabin", console_doosabin_subdivide,"");
1384
        register_console_function("subdivide.butterfly", console_butterfly_subdivide,"");
1385
        register_console_function("save_mesh", console_save,"");
1386
        register_console_function("noise.perturb_vertices", console_vertex_noise,"");
1387
        register_console_function("noise.perturb_vertices_perpendicular", console_perpendicular_vertex_noise,"");
1388
        register_console_function("noise.perturb_topology", console_noisy_flips,"");
1389
 
1390
        register_console_function("dual", console_dual,"");
1391
        register_console_function("flatten", console_flatten,"");
1392
 
1393
        register_console_function("align", console_align,"");
1394
        register_console_function("undo", console_undo,"");
1395
 
1396
        register_console_function("validity", console_valid,"");
1397
        register_console_function("info", console_info,"");
1398
 
1399
        //        register_console_function("harmonics.reset_shape", console_reset_shape, "");
1400
        register_console_function("harmonics.analyze", console_analyze, "");
1401
        //        register_console_function("harmonics.partial_reconstruct", console_partial_reconstruct,"");
1402
 
1403
        register_console_function("Dijkstra", console_Dijkstra,"");
1404
 
1405
        register_console_function("transform.scale", console_scale, "Scale mesh");
1406
 
1407
        active.reg(theConsole, "active_mesh", "The active mesh");
1408
        display_render_mode.reg(theConsole, "display.render_mode", "Display render mode");
1409
        display_smooth_shading.reg(theConsole, "display.smooth_shading", "1 for smooth shading 0 for flat");
1410
        display_gamma.reg(theConsole, "display.gamma", "The gamma setting for the display");
1411
 
1412
        theConsole.print("Welcome to MeshEdit");
1413
        theConsole.newline();
1414
    }
1415
 
1416
    bool MeshEditor::add_file(const std::string& str)
1417
    {
1418
        while (active_mesh().no_vertices()>0 && active<NO_MESHES)
1419
            active  = active + 1;
1420
        if(active == NO_MESHES)
1421
            active = 0;
1422
        if(active_visobj().reload(str)) {
1423
            active_visobj().post_create_display_list();
1424
            return true;
1425
        }
1426
        return false;
1427
    }
1428
 
1429
    bool MeshEditor::reload_active_from_file(const std::string& str)
1430
    {
1431
        if(active_visobj().reload(str)) {
1432
            active_visobj().post_create_display_list();
1433
            return true;
1434
        }
1435
        return false;
1436
    }
1437
 
1438
    bool MeshEditor::add_to_active_from_file(const std::string& str)
1439
    {
1440
        if(active_visobj().add_mesh(str)) {
1441
            active_visobj().post_create_display_list();
1442
            return  true;
1443
        }
1444
        return false;
1445
    }
1446
 
1447
 
1448
}