File: advancing_front.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 (573 lines) | stat: -rw-r--r-- 17,147 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
/****************************************************************************
* 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 MLS_ADVANCE_H
#define MLS_ADVANCE_H

#include <iostream>
#include <list>

namespace vcg {
  namespace tri {

/* An active edge on the advancing front.
 * belong to a triangle (v0,v1,v2)
 * v0, v1 the active edge
 * v2     internal vertex
*/
class FrontEdge {
 public:
  int v0, v1, v2;   //v0, v1 represent the FrontEdge, v2 the other vertex
                    //in the face this FrontEdge belongs to
  bool active; //keep tracks of wether it is in front or in deads

  //the loops in the front are mantained as a double linked list
  std::list<FrontEdge>::iterator next;
  std::list<FrontEdge>::iterator previous;

  FrontEdge() {}
  FrontEdge(int _v0, int _v1, int _v2):
             v0(_v0), v1(_v1), v2(_v2), active(true) {
               assert(v0 != v1 && v1 != v2 && v0 != v2);
  }

    bool operator==(const FrontEdge& f) const
    {
        return ((v0 == f.v0) && (v1 == f.v1) && (v2 == f.v2) );
    }
};

template <class MESH> class AdvancingFront {
 public:

  typedef typename MESH::VertexType     VertexType;
  typedef typename MESH::FaceType       FaceType;
  typedef typename MESH::FaceIterator       FaceIterator;
  typedef typename MESH::ScalarType     ScalarType;
  typedef typename MESH::VertexType::CoordType   Point3x;

  //class FrontEdgeLists
  //{

  //};


// protected:
  std::list<FrontEdge> front;
  std::list<FrontEdge> deads;
  std::vector<int> nb; //number of fronts a vertex is into,
                       //this is used for the Visited and Border flags
                       //but adding topology may not be needed anymore

 public:

  MESH &mesh;           //this structure will be filled by the algorithm

  AdvancingFront(MESH &_mesh): mesh(_mesh) {


    UpdateFlags<MESH>::FaceBorderFromNone(mesh);
    UpdateFlags<MESH>::VertexBorderFromFaceBorder(mesh);

    nb.clear();
    nb.resize(mesh.vert.size(), 0);

    CreateLoops();
  }
  virtual ~AdvancingFront() {}

  void BuildMesh(CallBackPos call = NULL, int interval = 512)
  {
    float finalfacesext = mesh.vert.size() * 2.0f;
	if (call) (*call)(0, "Advancing front");
    while(1) {

      for(int i = 0; i < interval; i++) {
        if(!front.size() && !SeedFace()) return;
        AddFace();
        if(call)
        {
            float rap = float(mesh.face.size()) / finalfacesext;
            int perc = (int) (100.0f * rap);
            (*call)(perc,"Adding Faces");
        }
      }
    }
  }

protected:
  //Implement these functions in your subclass
  enum ListID {FRONT,DEADS};
  typedef std::pair< ListID,std::list<FrontEdge>::iterator > ResultIterator;
  virtual bool Seed(int &v0, int &v1, int &v2) = 0;
  // This function must find a vertex to be added to edge 'e'.
  // return -1 in case of failure
  virtual int Place(FrontEdge &e, ResultIterator &touch) = 0;

  //create the FrontEdge loops from seed faces
  void CreateLoops()
  {
    for(size_t i = 0; i < mesh.face.size(); i++)
    {
      FaceType &f = mesh.face[i];
      if(f.IsD()) continue;

      for(int k = 0; k < 3; k++) {
        if(f.IsB(k)) {
          addNewEdge(FrontEdge(tri::Index(mesh,f.V0(k)),tri::Index(mesh,f.V1(k)),tri::Index(mesh,f.V2(k))) );
          nb[tri::Index(mesh,f.V0(k))]++;
        }
      }
    }

    for(std::list<FrontEdge>::iterator s = front.begin(); s != front.end(); s++) {
      (*s).previous = front.end();
      (*s).next = front.end();
    }
    //now create loops:
    for(std::list<FrontEdge>::iterator s = front.begin(); s != front.end(); s++) {
      for(std::list<FrontEdge>::iterator j = front.begin(); j != front.end(); j++) {
        if(s == j) continue;
        if((*s).v1 != (*j).v0) continue;
        if((*j).previous != front.end()) continue;
        (*s).next = j;
        (*j).previous = s;
        break;
      }
    }
    for(std::list<FrontEdge>::iterator s = front.begin(); s != front.end(); s++) {
      assert((*s).next != front.end());
      assert((*s).previous != front.end());
    }
  }

  bool SeedFace() {
    int v[3];
    bool success = Seed(v[0], v[1], v[2]);
    if(!success) return false;

    nb.resize(mesh.vert.size(), 0);

     //create the border of the first face
    std::list<FrontEdge>::iterator e = front.end();
    std::list<FrontEdge>::iterator last = e;
    std::list<FrontEdge>::iterator first;

    for(int i = 0; i < 3; i++) {
      int v0 = v[i];
      int v1 = v[((i+1)%3)];
      int v2 = v[((i+2)%3)];

      mesh.vert[v0].SetB();
      nb[v[i]]++;

      e = front.insert(front.begin(), FrontEdge(v0, v1, v2));
      if(i != 0) {
        (*last).next = e;
        (*e).previous = last;
      } else
        first = e;

      last = e;
    }
    //connect last and first
    (*last).next = first;
    (*first).previous = last;

    AddFace(v[0], v[1], v[2]);
    return true;
  }

public:
  bool AddFace() {
    if(!front.size()) return false;

    std::list<FrontEdge>::iterator ei = front.begin();
    FrontEdge &current = *ei;
    FrontEdge &previous = *current.previous;
    FrontEdge &next = *current.next;

    int v0 = current.v0, v1 = current.v1;
    assert(nb[v0] < 10 && nb[v1] < 10);

    ResultIterator touch;
    touch.first = FRONT;
    touch.second = front.end();
    int v2 = Place(current, touch);

    if(v2 == -1) {
      KillEdge(ei);
      return false;
    }

    assert(v2 != v0 && v2 != v1);

    if ( ( (touch.first == FRONT) && (touch.second != front.end()) ) ||
         ( (touch.first == DEADS) && (touch.second != deads.end()) )    )

    {
      //check for orientation and manifoldness

      //touch == current.previous?
      if(v2 == previous.v0) {
        if(!CheckEdge(v2, v1)) {
          KillEdge(ei);
          return false;
        }
          /*touching previous FrontEdge  (we reuse previous)
                                    next
             ------->v2 -----> v1------>
                      \       /
                       \     /
               previous \   / current
                         \ /
                          v0           */

        Detach(v0);

        std::list<FrontEdge>::iterator up = addNewEdge(FrontEdge(v2, v1, v0));
        MoveFront(up);
        (*up).previous = previous.previous;
        (*up).next = current.next;
        (*previous.previous).next = up;
        next.previous = up;
        Erase(current.previous);
        Erase(ei);
        Glue(up);

      //touch == (*current.next).next
      } else if(v2 == next.v1) {
        if(!CheckEdge(v0, v2)) {
          KillEdge(ei);
          return false;
        }
        /*touching next FrontEdge  (we reuse next)
          previous
             ------->v0 -----> v2------>
                      \       /
                       \     /
                        \   / next
                         \ /
                          v1           */

        Detach(v1);
        std::list<FrontEdge>::iterator up = addNewEdge(FrontEdge(v0, v2, v1));
        MoveFront(up);
        (*up).previous = current.previous;
        (*up).next = (*current.next).next;
        previous.next = up;
        (*next.next).previous = up;
        Erase(current.next);
        Erase(ei);
        Glue(up);
      } else {
        if(!CheckEdge(v0, v2) || !CheckEdge(v2, v1)) {
          KillEdge(ei);
          return false;
        }
      //touching some loop: split (or merge it is local does not matter.
      //like this
      /*
                  left        right
                <--------v2-<------
                          /|\
                         /   \
                     up /     \ down
                       /       \
                      /         V
                 ----v0 - - - > v1---------
                        current                         */
        std::list<FrontEdge>::iterator left = touch.second;
        std::list<FrontEdge>::iterator right = (*touch.second).previous;

        //this would be a really bad join
        if(v1 == (*right).v0 || v0 == (*left).v1) {
          KillEdge(ei);
          return false;
        }

        nb[v2]++;

        std::list<FrontEdge>::iterator down = addNewEdge(FrontEdge(v2, v1, v0));
        std::list<FrontEdge>::iterator up = addNewEdge(FrontEdge(v0, v2, v1));

        (*right).next = down;
        (*down).previous = right;

        (*down).next = current.next;
        next.previous = down;

        (*left).previous = up;
        (*up).next = left;

        (*up).previous = current.previous;
        previous.next = up;
        Erase(ei);
      }


    }
    else if (((touch.first == FRONT) && (touch.second == front.end())) ||
             ((touch.first == DEADS) && (touch.second == deads.end()))    )
    {
//        assert(CheckEdge(v0, v2));
//        assert(CheckEdge(v2, v1));
        /*  adding a new vertex

                           v2
                          /|\
                         /   \
                     up /     \ down
                       /       \
                      /         V
                 ----v0 - - - > v1--------- */
        assert(!mesh.vert[v2].IsB()); //fatal error! a new point is already a border?
        nb[v2]++;
        mesh.vert[v2].SetB();

        std::list<FrontEdge>::iterator down = addNewEdge(FrontEdge(v2, v1, v0));
        std::list<FrontEdge>::iterator up = addNewEdge(FrontEdge(v0, v2, v1));

        (*down).previous = up;
        (*up).next = down;
        (*down).next = current.next;
        next.previous = down;
        (*up).previous = current.previous;
        previous.next = up;
        Erase(ei);
      }

      AddFace(v0, v2, v1);
      return false;
  }

protected:
  void AddFace(int v0, int v1, int v2) {
    FaceIterator fi = vcg::tri::Allocator<MESH>::AddFace(mesh,v0,v1,v2);
    if (FaceType::HasNormal())
      fi->N() = TriangleNormal(*fi).Normalize();
    if(tri::HasVFAdjacency(mesh))
    {
      for(int j=0;j<3;++j)
      {
        (*fi).VFp(j) = (*fi).V(j)->VFp();
        (*fi).VFi(j) = (*fi).V(j)->VFi();
        (*fi).V(j)->VFp() = &(*fi);
        (*fi).V(j)->VFi() = j;
      }
    }
  }

  void AddVertex(VertexType &vertex) {
    VertexType *oldstart = NULL;
    if(mesh.vert.size()) oldstart = &*mesh.vert.begin();
    mesh.vert.push_back(vertex);
    mesh.vn++;
    VertexType *newstart = &*mesh.vert.begin();
    if(oldstart && oldstart != newstart) {
      for(int i = 0; i < mesh.face.size(); i++) {
        FaceType &face = mesh.face[i];
        for(int k = 0; k < 3; k++)
          face.V(k) = newstart + (face.V(k) - oldstart);
      }
    }
    nb.push_back(0);
  }

  // Given a possible new edge v0-v1
  // it checks that:
  // 1) the orientation is consistent (all the faces with vertex v0 and v1 have the edge in the opposite way)
  // 2) the edge appears at least once

  bool CheckEdge(int v0, int v1) {
    int tot = 0;
    VertexType *vv0 = &(mesh.vert[v0]);
    VertexType *vv1 = &(mesh.vert[v1]);
    if(tri::HasVFAdjacency(mesh))
    {
      face::VFIterator<FaceType> vfi(vv0);
      for (;!vfi.End();++vfi)
      {
        FaceType *f = vfi.F();
        for(int k = 0; k < 3; k++) {
          if(vv0 == f->V0(k) && vv1 == f->V1(k))  //orientation non constistent
             return false;
          else if(vv1 == f->V0(k) && vv0 == f->V1(k)) ++tot;
        }
      }
      return true;
    }
    for(int i = 0; i < (int)mesh.face.size(); i++) {
      FaceType &f = mesh.face[i];
      for(int k = 0; k < 3; k++) {
        if(vv0 == f.V0(k) && vv1 == f.V1(k))  //orientation non constistent
           return false;
        else if(vv1 == f.V0(k) && vv0 == f.V1(k)) ++tot;
      }
      if(tot >= 2) { //non manifold
        return false;
      }
    }
    return true;
  }
  //front management:

  //Add a new FrontEdge to the back of the queue
  std::list<FrontEdge>::iterator addNewEdge(FrontEdge e) {
    return front.insert(front.end(), e);
  }

  //move an Edge among the dead ones
  void KillEdge(std::list<FrontEdge>::iterator e)
  {
    if (e->active)
    {
        (*e).active = false;
        //std::list<FrontEdge>::iterator res = std::find(front.begin(),front.end(),e);
        FrontEdge tmp = *e;
        deads.splice(deads.end(), front, e);
        std::list<FrontEdge>::iterator newe = std::find(deads.begin(),deads.end(),tmp);
        tmp.previous->next = newe;
        tmp.next->previous = newe;
    }
  }

  void Erase(std::list<FrontEdge>::iterator e) {
    if((*e).active) front.erase(e);
    else deads.erase(e);
  }

  //move an FrontEdge to the back of the queue
  void MoveBack(std::list<FrontEdge>::iterator e) {
    front.splice(front.end(), front, e);
  }

  void MoveFront(std::list<FrontEdge>::iterator e) {
    front.splice(front.begin(), front, e);
  }

  //check if e can be sewed with one of oits neighbours
  bool Glue(std::list<FrontEdge>::iterator e) {
    return Glue((*e).previous, e) || Glue(e, (*e).next);
  }

  //Glue toghether a and b (where a.next = b
  bool Glue(std::list<FrontEdge>::iterator a, std::list<FrontEdge>::iterator b) {
    if((*a).v0 != (*b).v1) return false;

    std::list<FrontEdge>::iterator previous = (*a).previous;
    std::list<FrontEdge>::iterator next = (*b).next;
    (*previous).next = next;
    (*next).previous = previous;
    Detach((*a).v1);
    Detach((*a).v0);
    Erase(a);
    Erase(b);
    return true;
  }

  void Detach(int v) {
    assert(nb[v] > 0);
    if(--nb[v] == 0) {
      mesh.vert[v].ClearB();
    }
  }
};

template <class MESH> class AdvancingTest: public AdvancingFront<MESH> {
 public:
  typedef typename MESH::VertexType     VertexType;
  typedef typename MESH::VertexIterator VertexIterator;
  typedef typename MESH::FaceType       FaceType;
  typedef typename MESH::FaceIterator   FaceIterator;

  typedef typename MESH::ScalarType     ScalarType;
  typedef typename MESH::VertexType::CoordType   Point3x;

  AdvancingTest(MESH &_mesh): AdvancingFront<MESH>(_mesh) {}

  bool Seed(int &v0, int &v1, int &v2) {
    VertexType v[3];
    v[0].P() = Point3x(0, 0, 0);
    v[1].P() = Point3x(1, 0, 0);
    v[2].P() = Point3x(0, 1, 0);
    v[0].ClearFlags();
    v[1].ClearFlags();
    v[2].ClearFlags();

    v0 = this->mesh.vert.size();
    AddVertex(v[0]);
    v1 = this->mesh.vert.size();
    AddVertex(v[1]);
    v2 = this->mesh.vert.size();
    AddVertex(v[2]);
    return true;
  }

  int Place(FrontEdge &e, typename AdvancingFront<MESH>::ResultIterator &touch)
  {
     Point3f p[3];
     p[0] = this->mesh.vert[e.v0].P();
     p[1] = this->mesh.vert[e.v1].P();
     p[2] = this->mesh.vert[e.v2].P();
     Point3f point = p[0] + p[1] - p[2];

     int vn = this->mesh.vert.size();
     for(int i = 0; i < this->mesh.vert.size(); i++)
     {
       if((this->mesh.vert[i].P() - point).Norm() < 0.1)
       {
         vn = i;
         //find the border
         assert(this->mesh.vert[i].IsB());
         for(std::list<FrontEdge>::iterator k = this->front.begin(); k != this->front.end(); k++)
           if((*k).v0 == i)
           {
             touch.first = AdvancingFront<MESH>::FRONT;
             touch.second = k;
           }

         for(std::list<FrontEdge>::iterator k = this->deads.begin(); k != this->deads.end(); k++)
           if((*k).v0 == i)
             if((*k).v0 == i)
             {
               touch.first = AdvancingFront<MESH>::FRONT;
               touch.second = k;
             }
         break;
       }
     }
     if(vn == this->mesh.vert.size()) {
       VertexType v;
       v.P() = point;
       v.ClearFlags();
       AddVertex(v);
     }
     return vn;
  }
};

}//namespace tri
}//namespace vcg

#endif