File: halfedge_quad_clean.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (734 lines) | stat: -rwxr-xr-x 26,240 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004-2016                                           \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *   
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program is distributed in the hope that it will be useful,           *
* but WITHOUT ANY WARRANTY; without even the implied warranty of            *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/
#ifndef HALFEDGEQUADCLEAN_H
#define HALFEDGEQUADCLEAN_H

#include <vcg/complex/algorithms/update/halfedge_topology.h>
#include <queue>
#include <set>
#include<vcg/math/base.h>
#include<valarray>
#include<cmath>


namespace vcg
{
    namespace tri
    {
        /*!
          * \brief The class provides methods for detecting doublets and singlets and removing them
          *
          */

        template<class MeshType> class HalfedgeQuadClean
        {

        protected:

            typedef typename MeshType::VertexPointer VertexPointer;
            typedef typename MeshType::EdgePointer EdgePointer;
            typedef typename MeshType::HEdgePointer HEdgePointer;
            typedef typename MeshType::FacePointer FacePointer;

            typedef typename MeshType::VertexIterator VertexIterator;
            typedef typename MeshType::EdgeIterator EdgeIterator;
            typedef typename MeshType::HEdgeIterator HEdgeIterator;
            typedef typename MeshType::FaceIterator FaceIterator;

            /*! Adds to a queue all faces on the 1-ring of a given vertex
              *
              * \param q Queue of faces
              * \param vp Pointer to the vertex
              *
              */
            static void add_faces(queue<FacePointer> &q, VertexPointer vp)
            {
                vector<FacePointer> faces = HalfEdgeTopology<MeshType>::get_incident_faces(vp);

                for(typename vector<FacePointer>::iterator fi = faces.begin(); fi != faces.end(); ++fi)
                {
                    if(*fi)
                        q.push(*fi);
                }
            }

            /*! Removes doublets from all the faces into a queue until the queue empties
              *
              * \param m Mesh
              * \param faces Set of all faces modified by the removals
              * \param q Queue of faces to clean
              *
              */
            static void remove_doublets(MeshType &m, set<FacePointer> &faces, queue<FacePointer> &q)
            {

                while(!q.empty())
                {
                    FacePointer fp = q.front();
                    q.pop();

                    if( !fp->IsD() )
                    {
                        faces.insert(remove_doublet(m,fp, &q));
                    }
                }
            }

            /*! Removes a doublet and all the other ones that the removal may have generated
              *
              * \param m Mesh
              * \param fp Pointer to the face to clean from doublets
              * \param Queue of faces to check for new doublets
              *
              * \return The new face generated after doublet removal
              */
            static FacePointer remove_doublet(MeshType &m, FacePointer fp, queue<FacePointer> *q = NULL)
            {
                vector<HEdgePointer> hedges = HalfEdgeTopology<MeshType>::find_doublet_hedges_quad(fp);

                assert(hedges.size() <= 4);

                switch(hedges.size())
                {
                    // No doublets
                    case 0:
                        return NULL;

                    // A single doublet
                    case 1:
                        if(q)
                        {
                            add_faces(*q, hedges[0]->HNp()->HVp());

                            add_faces(*q, hedges[0]->HPp()->HVp());
                        }

                        return HalfEdgeTopology<MeshType>::doublet_remove_quad(m, hedges[0]->HVp());

                    // Two doublets on the same face
                    case 2:
                        {

                            if(q)
                            {
                                add_faces(*q, hedges[0]->HNp()->HVp());

                                add_faces(*q, hedges[0]->HPp()->HVp());
                            }

                           FacePointer fp1 = HalfEdgeTopology<MeshType>::doublet_remove_quad(m, hedges[0]->HVp());

                           // Removal of the doublet may generate a singlet
                           if(HalfEdgeTopology<MeshType>::is_singlet_quad(fp1))
                           {

                                HEdgePointer hp = HalfEdgeTopology<MeshType>::singlet_remove_quad(m, fp1);

                                if(hp)
                                {
                                    if(q)
                                    {
                                        if(hp->HFp())
                                            q->push(hp->HFp());
                                        if(hp->HOp()->HFp())
                                            q->push(hp->HOp()->HFp());
                                    }

                                    int valence1, valence2;

                                    valence1 = HalfEdgeTopology<MeshType>::vertex_valence(hp->HVp());
                                    valence2 = HalfEdgeTopology<MeshType>::vertex_valence(hp->HOp()->HVp());

                                    // Check if the removal of the singlet generated other singlets, then iteratively remove them
                                    while(valence1 == 1 || valence2 == 1)
                                    {

                                        assert(! (valence1 == 1 && valence2 == 1));

                                        FacePointer singlet_pointer;

                                        if(valence1 == 1 )
                                            singlet_pointer = hp->HFp();
                                        else
                                            singlet_pointer = hp->HOp()->HFp();

                                        hp = HalfEdgeTopology<MeshType>::singlet_remove_quad(m, singlet_pointer);

                                        if(!hp)
                                            break;

                                        if(q)
                                        {
                                            if(hp->HFp())
                                                q->push(hp->HFp());
                                            if(hp->HOp()->HFp())
                                                q->push(hp->HOp()->HFp());
                                        }

                                        valence1 = HalfEdgeTopology<MeshType>::vertex_valence(hp->HVp());
                                        valence2 = HalfEdgeTopology<MeshType>::vertex_valence(hp->HOp()->HVp());

                                    }
                                }
                           }

                           return fp1;
                       }

                     // Four doublets: simply remove one of the two faces
                    case 4:
                        HalfEdgeTopology<MeshType>::remove_face(m,fp->FHp()->HOp()->HFp());
                        return fp;

                    default:
                        assert(0);
                  }
            }




        public:

            /*! Removes doublets from all the faces on the 1-ring of the given vertices
              *
              * \param m Mesh
              * \param Set of all faces modified by the removals
              * \param vertices Vector of vertices that will be
              *
              */
            static void remove_doublets(MeshType &m, set<FacePointer> &faces, vector<VertexPointer> vertices)
            {
                queue<FacePointer> q;

                for(typename vector<VertexPointer>::iterator vi = vertices.begin(); vi != vertices.end(); ++vi)
                {
                    vector<FacePointer> inc_faces = HalfEdgeTopology<MeshType>::get_incident_faces(*vi);

                    for(typename vector<FacePointer>::iterator fi = inc_faces.begin(); fi != inc_faces.end(); ++fi)
                    {
                        if(*fi)
                            if( !((*fi)->IsD()) )
                                q.push(*fi);
                    }
                }

                remove_doublets(m, faces, q);
            }

            /*! Removes doublets from all the faces on the 1-ring of the given vertices
              *
              * \param m Mesh
              * \param vertices Vector of vertices that will be
              *
              */
            static void remove_doublets(MeshType &m, vector<VertexPointer> vertices)
            {
                set<FacePointer> faces;

                remove_doublets(m,faces,vertices);
            }

            /*! Removes doublets from a set of faces
              *
              * \param m Mesh
              * \param set of faces to clean
              *
              */
            static void remove_doublets(MeshType &m, set<FacePointer> &faces)
            {

                queue<FacePointer> q;

                for(typename set<FacePointer>::iterator fi = faces.begin(); fi != faces.end(); ++fi)
                {
                    if(*fi)
                        if( !((*fi)->IsD()) )
                            q.push(*fi);
                }

                remove_doublets(m, faces, q);

            }


            /*! Removes all doublets from a mesh
              *
              * \param m Mesh
              *
              * \return Number of doublets removed
              */
            static int remove_doublets(MeshType &m)
            {
                int count;
                int removed = 0;
                do
                {
                    count = 0;
                    for(FaceIterator fi = m.face.begin(); fi != m.face.end(); ++fi)
                    {
                        if( !((*fi).IsD()) )
                        {
                            if(remove_doublet(m, &(*fi)))
                                count++;
                        }
                    }

                    removed += count;

                }while(count != 0);

                return removed;
            }

            /*! Removes all singlets from a mesh
              *
              * \param m Mesh
              *
              * \return Number of singlets removed
              */
            static int remove_singlets(MeshType &m)
            {
                int removed = 0;
                int count;
                do
                {
                    count = 0;
                    for(FaceIterator fi = m.face.begin(); fi != m.face.end(); ++fi)
                    {
                        if( !((*fi).IsD()) )
                        {
                            if( HalfEdgeTopology<MeshType>::is_singlet_quad(&(*fi)) )
                            {
                                HalfEdgeTopology<MeshType>::singlet_remove_quad(m, &(*fi));
                                count++;
                            }
                        }
                    }

                    removed += count;

                }while(count != 0);

                return removed;
            }

            /*! Checks if a mesh has singlets
              *
              * \param m Mesh
              *
              * \return Value indicating whether mesh has singlets
              */
            static bool has_singlets(MeshType &m)
            {

                for(FaceIterator fi = m.face.begin(); fi != m.face.end(); ++fi)
                {
                    if( !((*fi).IsD()) )
                    {
                        if( HalfEdgeTopology<MeshType>::is_singlet_quad(&(*fi)) )
                            return true;
                    }
                }

                return false;
            }

            /*! Checks if a mesh has doublets
              *
              * \param m Mesh
              *
              * \return Value indicating whether mesh has doublets
              */
            static bool has_doublets(MeshType &m)
            {

                for(FaceIterator fi = m.face.begin(); fi != m.face.end(); ++fi)
                {
                    if( !((*fi).IsD()) )
                    {
                        if( HalfEdgeTopology<MeshType>::has_doublet_quad(&(*fi)) )
                            return true;
                    }
                }

                return false;
            }

            /*! Performs rotation of selected edges computing the best rotation
              *
              *
              * \param m Mesh
              * \param hedges Vector of halfedges representing the edges to rotate
              * \param faces Set of modified faces (every modified face will be inserted into this set)
              *
              */
            template<class PriorityType>
            static void flip_edges(MeshType &m, vector<HEdgePointer> &hedges, set<FacePointer> &faces)
            {

                for(typename vector<HEdgePointer>::iterator hi = hedges.begin(); hi != hedges.end(); ++hi)
                {
                    // edge must be shared by two faces
                    if((*hi)->HFp() && (*hi)->HOp()->HFp())
                    {
                        if(!(*hi)->IsD() && !(*hi)->HFp()->IsD() && !(*hi)->HOp()->HFp()->IsD())
                        {
                            typename PriorityType::FlipType fliptype = PriorityType::best_flip( *hi );

                            if(fliptype != PriorityType::NO_FLIP)
                            {

                                vector<VertexPointer> vertices;

                                // Record old vertices for future removal of doublets
                                vertices.push_back((*hi)->HVp());
                                vertices.push_back((*hi)->HOp()->HVp());

                                // Add modified faces into the set of faces
                                faces.insert((*hi)->HFp());
                                faces.insert((*hi)->HOp()->HFp());

                                bool cw = (fliptype == PriorityType::CW_FLIP);
                                HalfEdgeTopology<MeshType>::edge_rotate_quad((*hi),cw);

                                // After a rotation doublets may have generated
                                remove_doublets(m, faces, vertices);

                            }
                        }
                    }
                }
            }

            /*! Performs edge rotations on the entire mesh computing the best rotation for each edge
              *
              *
              * \param m Mesh
              *
              */
            template <class PriorityType>
            static int flip_edges(MeshType &m)
            {
                int count;
                int ret=0;
                do
                {
                    count = 0;
                    for(typename MeshType::EdgeIterator ei = m.edge.begin(); ei != m.edge.end(); ++ei)
                    {

                        if( !(ei->IsD()) )
                        {
                            HEdgePointer hp = ei->EHp();

                            if(hp->HFp() && hp->HOp()->HFp())
                            {
                                typename PriorityType::FlipType fliptype = PriorityType::best_flip( hp );

                                if(fliptype != PriorityType::NO_FLIP)
                                {
                                    vector<VertexPointer> vertices;

                                    // Record old vertices for future removal of doublets
                                    vertices.push_back(hp->HVp());
                                    vertices.push_back(hp->HOp()->HVp());

                                    bool cw = (fliptype == PriorityType::CW_FLIP);
                                    HalfEdgeTopology<MeshType>::edge_rotate_quad(hp,cw);

                                    // After a rotation doublets may have generated
                                    remove_doublets(m, vertices);

                                    count++;
                                }
                            }

                        }
                    }

                    ret+=count;

                }while(count != 0);

                return ret;
            }

        };


        /*!
          * \brief Generic priority for edge rotations
          *
          */
        template <class MeshType> class EdgeFlipPriority
        {
        public:
            typedef typename MeshType::HEdgePointer HEdgePointer;

            /// Possible types of rotation
            enum FlipType { NO_FLIP, CW_FLIP, CCW_FLIP};

            /*!
              * Computes the best rotation to perform
              *
              * \param hp Pointer to an halfedge representing the edge to rotate
              *
              * \return The best type of rotation
              */
            static FlipType best_flip( HEdgePointer hp);
        };

        /*!
          * \brief Priority based on maximizing vertices regularity
          *
          */
        template <class MeshType> class VertReg: public EdgeFlipPriority<MeshType>
        {

        public:

            typedef typename MeshType::VertexPointer VertexPointer;
            typedef typename MeshType::EdgePointer EdgePointer;
            typedef typename MeshType::HEdgePointer HEdgePointer;
            typedef typename MeshType::FacePointer FacePointer;

            typedef EdgeFlipPriority<MeshType> Base;
            typedef typename Base::FlipType FlipType;

            /// Default Constructor
            VertReg(){}

            ~VertReg(){}

            /*!
              * Computes the best rotation to perform for maximizing vertices regularity
              *
              * \param hp Pointer to an halfedge representing the edge to rotate
              *
              * \return The best type of rotation
              */
            static FlipType best_flip( HEdgePointer hp)
            {
                assert(hp);
                assert(!hp->IsD());

                vector<VertexPointer> vps1 = HalfEdgeTopology<MeshType>::getVertices(hp->HFp(), hp);

                vector<VertexPointer> vps2 = HalfEdgeTopology<MeshType>::getVertices(hp->HOp()->HFp(), hp->HOp());

                valarray<double> valences(6);

                /*
                 Indices of vertices into vector valences:

                  3-------2
                  |       |
                  |  f1   |
                  |       |
                  0-------1
                  |       |
                  |  f2   |
                  |       |
                  4-------5

                  */

                // Compute valencies of vertices
                for(int i=0; i<4; i++)
                    valences[i] = HalfEdgeTopology<MeshType>::vertex_valence(vps1[i]) - 4 ;

                valences[4] = HalfEdgeTopology<MeshType>::vertex_valence(vps2[2]) - 4;
                valences[5] = HalfEdgeTopology<MeshType>::vertex_valence(vps2[3]) - 4;


                // Vector containing sums of the valencies in all the possible cases: no rotation, ccw rotation, cw rotation
                vector<int> sums;

                // positions:
                // sums[0]: now (No rotation)
                // sums[1]: flipping ccw
                // sums[2]: flipping cw

                // No rotation
                sums.push_back( pow(valences, 2.0).sum() );

                // CCW
                valences[0]--;
                valences[1]--;
                valences[2]++;
                valences[4]++;

                sums.push_back( pow(valences, 2.0).sum() );

                // CW
                valences[2]--;
                valences[4]--;
                valences[3]++;
                valences[5]++;

                sums.push_back( pow(valences, 2.0).sum() );


                if( sums[2]<= sums[1] && sums[2]< sums[0] )
                    return Base::CW_FLIP;

                else if( sums[1]< sums[2] && sums[1]< sums[0] )
                    return Base::CCW_FLIP;

                return Base::NO_FLIP;

            }

        };

        /*!
          * \brief Priority based on minimizing homeometry
          *
          */
        template <class MeshType> class Homeometry: public EdgeFlipPriority<MeshType>
        {

        public:

            typedef typename MeshType::VertexPointer VertexPointer;
            typedef typename MeshType::EdgePointer EdgePointer;
            typedef typename MeshType::HEdgePointer HEdgePointer;
            typedef typename MeshType::FacePointer FacePointer;


            typedef EdgeFlipPriority<MeshType> Base;
            typedef typename Base::FlipType FlipType;

            /// Default Constructor
            Homeometry(){}

            ~Homeometry(){}

            /*!
              * Computes the best rotation to perform for minimizing the distance from homeometry
              *
              * \param hp Pointer to an halfedge representing the edge to rotate
              *
              * \return The best type of rotation
              */
            static FlipType best_flip( HEdgePointer hp)
            {
                assert(hp);
                assert(!hp->IsD());

                vector<VertexPointer> face1 = HalfEdgeTopology<MeshType>::getVertices(hp->HFp(), hp);
                vector<VertexPointer> face2 = HalfEdgeTopology<MeshType>::getVertices(hp->HOp()->HFp(), hp->HOp());

                // Vector containing sums of the valencies in all the possible cases: no rotation, ccw rotation, cw rotation
                vector<int> sums;

                // positions:
                // sums[0]: now (No rotation)
                // sums[1]: flipping ccw
                // sums[2]: flipping cw

                // No rotation
                sums.push_back( distance_from_homeometry(face1, face2, 0) );

                // CCW
                face1[1] = face2[2];
                face2[1] = face1[2];

                sums.push_back( distance_from_homeometry(face1, face2, 1) );

                // CW
                face1[2] = face2[3];
                face2[2] = face1[3];

                sums.push_back( distance_from_homeometry(face1, face2, 2) );


                if( sums[2]<= sums[1] && sums[2]< sums[0] )
                    return Base::CW_FLIP;

                else if( sums[1]< sums[2] && sums[1]< sums[0] )
                    return Base::CCW_FLIP;

                return Base::NO_FLIP;

            }

        protected:

            /*!
              * Computes the area of a quad
              *
              * \param vertices Vector of the four vertices of the quad
              *
              * \return Area of the quad
              */
            static float area(vector<VertexPointer> &vertices)
            {

                assert(vertices.size() == 4);

                float tri1 =  Norm( (vertices[1]->cP() - vertices[0]->cP()) ^ (vertices[2]->cP() - vertices[0]->cP()) );
                float tri2 =  Norm( (vertices[2]->cP() - vertices[0]->cP()) ^ (vertices[3]->cP() - vertices[0]->cP()) );

                return (tri1+tri2) / 2;
            }

            /*!
              * Computes the distance of two faces from being homeometirc
              *
              * \param face1 Vector of vertices belonging to the first face
              * \param face2 Vector of vertices belonging to the second face
              * \param i Index of the edge to compute
              *
              * \return The computed homeometry
              */
            static float distance_from_homeometry(vector<VertexPointer> &face1, vector<VertexPointer> &face2, int i)
            {
                // Ideal edge length
                float mu = sqrt( (area(face1) + area(face2)) / 2 );

                // Length of the i-th edge (the edge changed with a rotation)
                float edge_length = Distance( face1[i]->cP(), face1[i+1]->cP() );

                // Length of the diagonals
                valarray<float> diagonals(4);

                diagonals[0] = Distance( face1[0]->cP(), face1[2]->cP() );
                diagonals[1] = Distance( face1[1]->cP(), face1[3]->cP() );
                diagonals[2] = Distance( face2[0]->cP(), face2[2]->cP() );
                diagonals[3] = Distance( face2[1]->cP(), face2[3]->cP() );

                // Ideal diagonal length
                float ideal_diag_length = SQRT_TWO*mu;

                float sum_diagonals = pow(diagonals - ideal_diag_length, 2.0).sum();

                return  (pow (edge_length - mu , static_cast<float>(2.0)) + sum_diagonals);
            }

        };

    }
}
#endif // HALFEDGEQUADCLEAN_H