PolyConnectivity.cc 38.9 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/* ========================================================================= *
Jan Möbius's avatar
Jan Möbius committed
2
3
 *                                                                           *
 *                               OpenMesh                                    *
Jan Möbius's avatar
Jan Möbius committed
4
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
Jan Möbius's avatar
Typo    
Jan Möbius committed
5
 *           Department of Computer Graphics and Multimedia                  *
Jan Möbius's avatar
Jan Möbius committed
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
 *                          All rights reserved.                             *
 *                            www.openmesh.org                               *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenMesh.                                            *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39
40
 *                                                                           *
 * ========================================================================= */
41
42
43
44
45

/*===========================================================================*\
 *                                                                           *             
 *   $Revision$                                                         *
 *   $Date$                   *
Jan Möbius's avatar
Jan Möbius committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
 *                                                                           *
\*===========================================================================*/

//== IMPLEMENTATION ==========================================================
#include <OpenMesh/Core/Mesh/PolyConnectivity.hh>
#include <set>

namespace OpenMesh {

const PolyConnectivity::VertexHandle    PolyConnectivity::InvalidVertexHandle;
const PolyConnectivity::HalfedgeHandle  PolyConnectivity::InvalidHalfedgeHandle;
const PolyConnectivity::EdgeHandle      PolyConnectivity::InvalidEdgeHandle;
const PolyConnectivity::FaceHandle      PolyConnectivity::InvalidFaceHandle;

//-----------------------------------------------------------------------------
61

Jan Möbius's avatar
Jan Möbius committed
62
63
64
65
66
PolyConnectivity::HalfedgeHandle
PolyConnectivity::find_halfedge(VertexHandle _start_vh, VertexHandle _end_vh ) const
{
  assert(_start_vh.is_valid() && _end_vh.is_valid());

67
68
69
  for (ConstVertexOHalfedgeIter voh_it = cvoh_iter(_start_vh); voh_it.is_valid(); ++voh_it)
    if (to_vertex_handle(*voh_it) == _end_vh)
      return *voh_it;
Jan Möbius's avatar
Jan Möbius committed
70
71
72
73
74
75
76

  return InvalidHalfedgeHandle;
}


bool PolyConnectivity::is_boundary(FaceHandle _fh, bool _check_vertex) const
{
Jan Möbius's avatar
Jan Möbius committed
77
78
  for (ConstFaceEdgeIter cfeit = cfe_iter( _fh ); cfeit.is_valid(); ++cfeit)
      if (is_boundary( *cfeit ) )
Jan Möbius's avatar
Jan Möbius committed
79
80
81
82
        return true;

  if (_check_vertex)
  {
Jan Möbius's avatar
Jan Möbius committed
83
84
      for (ConstFaceVertexIter cfvit = cfv_iter( _fh ); cfvit.is_valid(); ++cfvit)
        if (is_boundary( *cfvit ) )
Jan Möbius's avatar
Jan Möbius committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
            return true;
  }
  return false;
}

bool PolyConnectivity::is_manifold(VertexHandle _vh) const
{
  /* The vertex is non-manifold if more than one gap exists, i.e.
    more than one outgoing boundary halfedge. If (at least) one
    boundary halfedge exists, the vertex' halfedge must be a
    boundary halfedge. If iterating around the vertex finds another
    boundary halfedge, the vertex is non-manifold. */

  ConstVertexOHalfedgeIter vh_it(*this, _vh);
Jan Möbius's avatar
Jan Möbius committed
99
100
101
  if (vh_it.is_valid())
    for (++vh_it; vh_it.is_valid(); ++vh_it)
        if (is_boundary(*vh_it))
Jan Möbius's avatar
Jan Möbius committed
102
103
104
105
106
107
108
          return false;
  return true;
}

//-----------------------------------------------------------------------------
void PolyConnectivity::adjust_outgoing_halfedge(VertexHandle _vh)
{
Jan Möbius's avatar
Jan Möbius committed
109
  for (ConstVertexOHalfedgeIter vh_it=cvoh_iter(_vh); vh_it.is_valid(); ++vh_it)
Jan Möbius's avatar
Jan Möbius committed
110
  {
Jan Möbius's avatar
Jan Möbius committed
111
    if (is_boundary(*vh_it))
Jan Möbius's avatar
Jan Möbius committed
112
    {
Jan Möbius's avatar
Jan Möbius committed
113
      set_halfedge_handle(_vh, *vh_it);
Jan Möbius's avatar
Jan Möbius committed
114
115
116
117
118
119
120
121
      break;
    }
  }
}

//-----------------------------------------------------------------------------

PolyConnectivity::FaceHandle
Jan Möbius's avatar
Jan Möbius committed
122
PolyConnectivity::add_face(const VertexHandle* _vertex_handles, size_t _vhs_size)
Jan Möbius's avatar
Jan Möbius committed
123
124
{
  VertexHandle                   vh;
Jan Möbius's avatar
Jan Möbius committed
125
  size_t                         i, ii, n(_vhs_size);
Jan Möbius's avatar
Jan Möbius committed
126
127
128
129
130
131
  HalfedgeHandle                 inner_next, inner_prev,
                                 outer_next, outer_prev,
                                 boundary_next, boundary_prev,
                                 patch_start, patch_end;


132
133
134
135
136
137
  // Check sufficient working storage available
  if (edgeData_.size() < n)
  {
    edgeData_.resize(n);
    next_cache_.resize(6*n);
  }
Jan Möbius's avatar
Jan Möbius committed
138
139

  size_t next_cache_count = 0;
140

Jan Möbius's avatar
Jan Möbius committed
141
142
143
144
145
146
147
148
149
150
151
152
  // don't allow degenerated faces
  assert (n > 2);

  // test for topological errors
  for (i=0, ii=1; i<n; ++i, ++ii, ii%=n)
  {
    if ( !is_boundary(_vertex_handles[i]) )
    {
      omerr() << "PolyMeshT::add_face: complex vertex\n";
      return InvalidFaceHandle;
    }

153
154
155
156
157
    // Initialise edge attributes
    edgeData_[i].halfedge_handle = find_halfedge(_vertex_handles[i],
                                                 _vertex_handles[ii]);
    edgeData_[i].is_new = !edgeData_[i].halfedge_handle.is_valid();
    edgeData_[i].needs_adjust = false;
Jan Möbius's avatar
Jan Möbius committed
158

159
    if (!edgeData_[i].is_new && !is_boundary(edgeData_[i].halfedge_handle))
Jan Möbius's avatar
Jan Möbius committed
160
161
162
163
164
165
166
167
168
    {
      omerr() << "PolyMeshT::add_face: complex edge\n";
      return InvalidFaceHandle;
    }
  }

  // re-link patches if necessary
  for (i=0, ii=1; i<n; ++i, ++ii, ii%=n)
  {
169
    if (!edgeData_[i].is_new && !edgeData_[ii].is_new)
Jan Möbius's avatar
Jan Möbius committed
170
    {
171
172
173
      inner_prev = edgeData_[i].halfedge_handle;
      inner_next = edgeData_[ii].halfedge_handle;

Jan Möbius's avatar
Jan Möbius committed
174
175
176
177
178
179
180
181
182
183
184
185
186

      if (next_halfedge_handle(inner_prev) != inner_next)
      {
        // here comes the ugly part... we have to relink a whole patch

        // search a free gap
        // free gap will be between boundary_prev and boundary_next
        outer_prev = opposite_halfedge_handle(inner_next);
        outer_next = opposite_halfedge_handle(inner_prev);
        boundary_prev = outer_prev;
        do
          boundary_prev =
            opposite_halfedge_handle(next_halfedge_handle(boundary_prev));
187
        while (!is_boundary(boundary_prev));
Jan Möbius's avatar
Jan Möbius committed
188
        boundary_next = next_halfedge_handle(boundary_prev);
189

Jan Möbius's avatar
Jan Möbius committed
190
        // ok ?
191
        if (boundary_prev == inner_prev)
Jan Möbius's avatar
Jan Möbius committed
192
193
194
195
196
        {
          omerr() << "PolyMeshT::add_face: patch re-linking failed\n";
          return InvalidFaceHandle;
        }

197
198
199
        assert(is_boundary(boundary_prev));
        assert(is_boundary(boundary_next));

Jan Möbius's avatar
Jan Möbius committed
200
201
202
203
        // other halfedges' handles
        patch_start = next_halfedge_handle(inner_prev);
        patch_end   = prev_halfedge_handle(inner_next);

204
205
206
207
208
209
210
        assert(boundary_prev.is_valid());
        assert(patch_start.is_valid());
        assert(patch_end.is_valid());
        assert(boundary_next.is_valid());
        assert(inner_prev.is_valid());
        assert(inner_next.is_valid());

Jan Möbius's avatar
Jan Möbius committed
211
        // relink
212
213
214
        next_cache_[next_cache_count++] = std::make_pair(boundary_prev, patch_start);
        next_cache_[next_cache_count++] = std::make_pair(patch_end, boundary_next);
        next_cache_[next_cache_count++] = std::make_pair(inner_prev, inner_next);
Jan Möbius's avatar
Jan Möbius committed
215
216
217
218
219
220
      }
    }
  }

  // create missing edges
  for (i=0, ii=1; i<n; ++i, ++ii, ii%=n)
221
222
    if (edgeData_[i].is_new)
      edgeData_[i].halfedge_handle = new_edge(_vertex_handles[i], _vertex_handles[ii]);
Jan Möbius's avatar
Jan Möbius committed
223
224
225

  // create the face
  FaceHandle fh(new_face());
226
  set_halfedge_handle(fh, edgeData_[n-1].halfedge_handle);
Jan Möbius's avatar
Jan Möbius committed
227
228
229
230
231

  // setup halfedges
  for (i=0, ii=1; i<n; ++i, ++ii, ii%=n)
  {
    vh         = _vertex_handles[ii];
232
233
234

    inner_prev = edgeData_[i].halfedge_handle;
    inner_next = edgeData_[ii].halfedge_handle;
235
236
    assert(inner_prev.is_valid());
    assert(inner_next.is_valid());
Jan Möbius's avatar
Jan Möbius committed
237

Jan Möbius's avatar
Jan Möbius committed
238
    size_t id = 0;
239
240
241
    if (edgeData_[i].is_new)  id |= 1;
    if (edgeData_[ii].is_new) id |= 2;

Jan Möbius's avatar
Jan Möbius committed
242
243
244
245
246

    if (id)
    {
      outer_prev = opposite_halfedge_handle(inner_next);
      outer_next = opposite_halfedge_handle(inner_prev);
247
248
      assert(outer_prev.is_valid());
      assert(outer_next.is_valid());
Jan Möbius's avatar
Jan Möbius committed
249
250
251
252
253
254

      // set outer links
      switch (id)
      {
        case 1: // prev is new, next is old
          boundary_prev = prev_halfedge_handle(inner_next);
255
          assert(boundary_prev.is_valid());
256
          next_cache_[next_cache_count++] = std::make_pair(boundary_prev, outer_next);
Jan Möbius's avatar
Jan Möbius committed
257
258
259
260
261
          set_halfedge_handle(vh, outer_next);
          break;

        case 2: // next is new, prev is old
          boundary_next = next_halfedge_handle(inner_prev);
262
          assert(boundary_next.is_valid());
263
          next_cache_[next_cache_count++] = std::make_pair(outer_prev, boundary_next);
Jan Möbius's avatar
Jan Möbius committed
264
265
266
267
268
269
270
          set_halfedge_handle(vh, boundary_next);
          break;

        case 3: // both are new
          if (!halfedge_handle(vh).is_valid())
          {
            set_halfedge_handle(vh, outer_next);
271
            next_cache_[next_cache_count++] = std::make_pair(outer_prev, outer_next);
Jan Möbius's avatar
Jan Möbius committed
272
273
274
275
276
          }
          else
          {
            boundary_next = halfedge_handle(vh);
            boundary_prev = prev_halfedge_handle(boundary_next);
277
278
            assert(boundary_prev.is_valid());
            assert(boundary_next.is_valid());
279
280
            next_cache_[next_cache_count++] = std::make_pair(boundary_prev, outer_next);
            next_cache_[next_cache_count++] = std::make_pair(outer_prev, boundary_next);
Jan Möbius's avatar
Jan Möbius committed
281
282
283
284
285
          }
          break;
      }

      // set inner link
286
      next_cache_[next_cache_count++] = std::make_pair(inner_prev, inner_next);
Jan Möbius's avatar
Jan Möbius committed
287
    }
288
289
    else edgeData_[ii].needs_adjust = (halfedge_handle(vh) == inner_next);

Jan Möbius's avatar
Jan Möbius committed
290
291

    // set face handle
292
    set_face_handle(edgeData_[i].halfedge_handle, fh);
Jan Möbius's avatar
Jan Möbius committed
293
294
295
  }

  // process next halfedge cache
296
  for (i = 0; i < next_cache_count; ++i)
297
298
    set_next_halfedge_handle(next_cache_[i].first, next_cache_[i].second);

Jan Möbius's avatar
Jan Möbius committed
299
300
301

  // adjust vertices' halfedge handle
  for (i=0; i<n; ++i)
302
    if (edgeData_[i].needs_adjust)
Jan Möbius's avatar
Jan Möbius committed
303
304
305
306
307
      adjust_outgoing_halfedge(_vertex_handles[i]);

  return fh;
}

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//-----------------------------------------------------------------------------

FaceHandle PolyConnectivity::add_face(VertexHandle _vh0, VertexHandle _vh1, VertexHandle _vh2, VertexHandle _vh3)
{
  VertexHandle vhs[4] = { _vh0, _vh1, _vh2, _vh3 };
  return add_face(vhs, 4);
}

//-----------------------------------------------------------------------------

FaceHandle PolyConnectivity::add_face(VertexHandle _vh0, VertexHandle _vh1, VertexHandle _vh2)
{
  VertexHandle vhs[3] = { _vh0, _vh1, _vh2 };
  return add_face(vhs, 3);
}

//-----------------------------------------------------------------------------

FaceHandle PolyConnectivity::add_face(const std::vector<VertexHandle>& _vhandles)
{ return add_face(&_vhandles.front(), _vhandles.size()); }
328
329
330
331
332


//-----------------------------------------------------------------------------
bool PolyConnectivity::is_collapse_ok(HalfedgeHandle v0v1)
{
333
334
335
336
337
338
  //is edge already deleteed?
  if (status(edge_handle(v0v1)).deleted())
  {
    return false;
  }

339
340
341
342
343
344
345
346
347
348
349
350
351
352
  HalfedgeHandle v1v0(opposite_halfedge_handle(v0v1));
  VertexHandle v0(to_vertex_handle(v1v0));
  VertexHandle v1(to_vertex_handle(v0v1));  

  bool v0v1_triangle = false;
  bool v1v0_triangle = false;
  
  if (!is_boundary(v0v1))
    v0v1_triangle = valence(face_handle(v0v1)) == 3;
  
  if (!is_boundary(v1v0))
    v1v0_triangle = valence(face_handle(v1v0)) == 3;

  //in a quadmesh we dont have the "next" or "previous" vhandle, so we need to look at previous and next on both sides
Jan Möbius's avatar
Jan Möbius committed
353
  //VertexHandle v_01_p = from_vertex_handle(prev_halfedge_handle(v0v1));
354
355
  VertexHandle v_01_n = to_vertex_handle(next_halfedge_handle(v0v1));  

Jan Möbius's avatar
Jan Möbius committed
356
  //VertexHandle v_10_p = from_vertex_handle(prev_halfedge_handle(v1v0));
357
358
359
360
361
362
363
364
365
366
367
368
369
370
  VertexHandle v_10_n = to_vertex_handle(next_halfedge_handle(v1v0));

  //are the vertices already deleted ?
  if (status(v0).deleted() || status(v1).deleted())
  {
    return false;
  }

  //the edges v1-vl and vl-v0 must not be both boundary edges
  //this test makes only sense in a polymesh if the side face is a triangle
  if (!is_boundary(v0v1))
  {
    if (v0v1_triangle)
    {
Jan Möbius's avatar
Jan Möbius committed
371
      //VertexHandle vl = to_vertex_handle(next_halfedge_handle(v0v1));
372
373
374
375

      HalfedgeHandle h1 = next_halfedge_handle(v0v1);
      HalfedgeHandle h2 = next_halfedge_handle(h1);
      if (is_boundary(opposite_halfedge_handle(h1)) && is_boundary(opposite_halfedge_handle(h2)))
Jan Möbius's avatar
Jan Möbius committed
376
        return false;
377
378
379
380
381
382
383
384
385
    }
  }

  //the edges v0-vr and vr-v1 must not be both boundary edges
  //this test makes only sense in a polymesh if the side face is a triangle
  if (!is_boundary(v1v0))
  {
    if (v1v0_triangle)
    {
Jan Möbius's avatar
Jan Möbius committed
386
      //VertexHandle vr = to_vertex_handle(next_halfedge_handle(v1v0));
387
388
389
390

      HalfedgeHandle h1 = next_halfedge_handle(v1v0);
      HalfedgeHandle h2 = next_halfedge_handle(h1);
      if (is_boundary(opposite_halfedge_handle(h1)) && is_boundary(opposite_halfedge_handle(h2)))
Jan Möbius's avatar
Jan Möbius committed
391
        return false;
392
393
394
395
396
397
398
399
400
    }
  }

  // edge between two boundary vertices should be a boundary edge
  if ( is_boundary(v0) && is_boundary(v1) && !is_boundary(v0v1) && !is_boundary(v1v0))
    return false;
  
  VertexVertexIter vv_it;
  // test intersection of the one-rings of v0 and v1
Jan Möbius's avatar
Jan Möbius committed
401
  for (vv_it = vv_iter(v0); vv_it.is_valid(); ++vv_it)
402
  {
Jan Möbius's avatar
Jan Möbius committed
403
    status(*vv_it).set_tagged(false);
404
405
  }

Jan Möbius's avatar
Jan Möbius committed
406
  for (vv_it = vv_iter(v1); vv_it.is_valid(); ++vv_it)
407
  {
Jan Möbius's avatar
Jan Möbius committed
408
    status(*vv_it).set_tagged(true);
409
410
  }

Jan Möbius's avatar
Jan Möbius committed
411
  for (vv_it = vv_iter(v0); vv_it.is_valid(); ++vv_it)
412
  {
Jan Möbius's avatar
Jan Möbius committed
413
    if (status(*vv_it).tagged() &&
414
415
      !(*vv_it == v_01_n && v0v1_triangle) &&
      !(*vv_it == v_10_n && v1v0_triangle)
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
      )
    {
      return false;
    }
  }
  
  //test for a face on the backside/other side that might degenerate
  if (v0v1_triangle)
  {
    HalfedgeHandle one, two;
    one = next_halfedge_handle(v0v1);
    two = next_halfedge_handle(one);
    
    one = opposite_halfedge_handle(one);
    two = opposite_halfedge_handle(two);
    
    if (face_handle(one) == face_handle(two) && valence(face_handle(one)) != 3)
    {
      return false;
    }
  }
  
  if (v1v0_triangle)
  {
    HalfedgeHandle one, two;
    one = next_halfedge_handle(v1v0);
    two = next_halfedge_handle(one);
    
    one = opposite_halfedge_handle(one);
    two = opposite_halfedge_handle(two);
    
    if (face_handle(one) == face_handle(two) && valence(face_handle(one)) != 3)
    {
      return false;
    }
  }

Jan Möbius's avatar
Jan Möbius committed
453
  if (status(*vv_it).tagged() && v_01_n == v_10_n && v0v1_triangle && v1v0_triangle)
454
455
456
457
458
459
460
461
  {
    return false;
  }

  // passed all tests
  return true;
}

Jan Möbius's avatar
Jan Möbius committed
462
463
464
465
466
467
468
//-----------------------------------------------------------------------------

void PolyConnectivity::delete_vertex(VertexHandle _vh, bool _delete_isolated_vertices)
{
  // store incident faces
  std::vector<FaceHandle> face_handles;
  face_handles.reserve(8);
Jan Möbius's avatar
Jan Möbius committed
469
470
  for (VFIter vf_it(vf_iter(_vh)); vf_it.is_valid(); ++vf_it)
    face_handles.push_back(*vf_it);
Jan Möbius's avatar
Jan Möbius committed
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491


  // delete collected faces
  std::vector<FaceHandle>::iterator fh_it(face_handles.begin()),
                                    fh_end(face_handles.end());

  for (; fh_it!=fh_end; ++fh_it)
    delete_face(*fh_it, _delete_isolated_vertices);

  status(_vh).set_deleted(true);
}

//-----------------------------------------------------------------------------

void PolyConnectivity::delete_edge(EdgeHandle _eh, bool _delete_isolated_vertices)
{
  FaceHandle fh0(face_handle(halfedge_handle(_eh, 0)));
  FaceHandle fh1(face_handle(halfedge_handle(_eh, 1)));

  if (fh0.is_valid())  delete_face(fh0, _delete_isolated_vertices);
  if (fh1.is_valid())  delete_face(fh1, _delete_isolated_vertices);
492

493
494
495
496
497
498
499
500
501
502
503
504
505
506
  // If there is no face, we delete the edge
  // here
  if ( ! fh0.is_valid() && !fh1.is_valid()) {
    // mark edge deleted if the mesh has a edge status
    if ( has_edge_status() )
      status(_eh).set_deleted(true);

    // mark corresponding halfedges as deleted
    // As the deleted edge is boundary,
    // all corresponding halfedges will also be deleted.
    if ( has_halfedge_status() ) {
      status(halfedge_handle(_eh, 0)).set_deleted(true);
      status(halfedge_handle(_eh, 1)).set_deleted(true);
    }
507
  }
Jan Möbius's avatar
Jan Möbius committed
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
}

//-----------------------------------------------------------------------------

void PolyConnectivity::delete_face(FaceHandle _fh, bool _delete_isolated_vertices)
{
  assert(_fh.is_valid() && !status(_fh).deleted());

  // mark face deleted
  status(_fh).set_deleted(true);


  // this vector will hold all boundary edges of face _fh
  // these edges will be deleted
  std::vector<EdgeHandle> deleted_edges;
  deleted_edges.reserve(3);


  // this vector will hold all vertices of face _fh
  // for updating their outgoing halfedge
  std::vector<VertexHandle>  vhandles;
  vhandles.reserve(3);


  // for all halfedges of face _fh do:
  //   1) invalidate face handle.
  //   2) collect all boundary halfedges, set them deleted
  //   3) store vertex handles
  HalfedgeHandle hh;
Jan Möbius's avatar
Jan Möbius committed
537
  for (FaceHalfedgeIter fh_it(fh_iter(_fh)); fh_it.is_valid(); ++fh_it)
Jan Möbius's avatar
Jan Möbius committed
538
  {
Jan Möbius's avatar
Jan Möbius committed
539
    hh = *fh_it;
Jan Möbius's avatar
Jan Möbius committed
540
541
542
543
544
545
546
547
548
549
550

    set_boundary(hh);//set_face_handle(hh, InvalidFaceHandle);

    if (is_boundary(opposite_halfedge_handle(hh)))
        deleted_edges.push_back(edge_handle(hh));

    vhandles.push_back(to_vertex_handle(hh));
  }


  // delete all collected (half)edges
551
  // these edges were all boundary
Jan Möbius's avatar
Jan Möbius committed
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
  // delete isolated vertices (if _delete_isolated_vertices is true)
  if (!deleted_edges.empty())
  {
    std::vector<EdgeHandle>::iterator del_it(deleted_edges.begin()),
                                      del_end(deleted_edges.end());
    HalfedgeHandle h0, h1, next0, next1, prev0, prev1;
    VertexHandle   v0, v1;

    for (; del_it!=del_end; ++del_it)
    {
      h0    = halfedge_handle(*del_it, 0);
      v0    = to_vertex_handle(h0);
      next0 = next_halfedge_handle(h0);
      prev0 = prev_halfedge_handle(h0);

      h1    = halfedge_handle(*del_it, 1);
      v1    = to_vertex_handle(h1);
      next1 = next_halfedge_handle(h1);
      prev1 = prev_halfedge_handle(h1);

      // adjust next and prev handles
      set_next_halfedge_handle(prev0, next1);
      set_next_halfedge_handle(prev1, next0);

576
577
578
      // mark edge deleted if the mesh has a edge status
      if ( has_edge_status() )
    	 status(*del_it).set_deleted(true);
Jan Möbius's avatar
Jan Möbius committed
579

580
581
582

      // mark corresponding halfedges as deleted
      // As the deleted edge is boundary,
583
      // all corresponding halfedges will also be deleted.
584
585
586
587
588
      if ( has_halfedge_status() ) {
        status(h0).set_deleted(true);
        status(h1).set_deleted(true);
      }

Jan Möbius's avatar
Jan Möbius committed
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
      // update v0
      if (halfedge_handle(v0) == h1)
      {
        // isolated ?
        if (next0 == h1)
        {
          if (_delete_isolated_vertices)
            status(v0).set_deleted(true);
          set_isolated(v0);
        }
        else set_halfedge_handle(v0, next0);
      }

      // update v1
      if (halfedge_handle(v1) == h0)
      {
        // isolated ?
        if (next1 == h0)
        {
          if (_delete_isolated_vertices)
            status(v1).set_deleted(true);
          set_isolated(v1);
        }
        else  set_halfedge_handle(v1, next1);
      }
    }
  }

  // update outgoing halfedge handles of remaining vertices
  std::vector<VertexHandle>::iterator v_it(vhandles.begin()),
                                      v_end(vhandles.end());
  for (; v_it!=v_end; ++v_it)
    adjust_outgoing_halfedge(*v_it);
}

624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
//-----------------------------------------------------------------------------
PolyConnectivity::VertexIter PolyConnectivity::vertices_begin()
{
  return VertexIter(*this, VertexHandle(0));
}

//-----------------------------------------------------------------------------
PolyConnectivity::ConstVertexIter PolyConnectivity::vertices_begin() const
{
  return ConstVertexIter(*this, VertexHandle(0));
}

//-----------------------------------------------------------------------------
PolyConnectivity::VertexIter PolyConnectivity::vertices_end()
{
Jan Möbius's avatar
Jan Möbius committed
639
  return VertexIter(*this, VertexHandle( int(n_vertices() ) ));
640
641
642
643
644
}

//-----------------------------------------------------------------------------
PolyConnectivity::ConstVertexIter PolyConnectivity::vertices_end() const
{
Jan Möbius's avatar
Jan Möbius committed
645
  return ConstVertexIter(*this, VertexHandle( int(n_vertices()) ));
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
}

//-----------------------------------------------------------------------------
PolyConnectivity::HalfedgeIter PolyConnectivity::halfedges_begin()
{
  return HalfedgeIter(*this, HalfedgeHandle(0));
}

//-----------------------------------------------------------------------------
PolyConnectivity::ConstHalfedgeIter PolyConnectivity::halfedges_begin() const
{
  return ConstHalfedgeIter(*this, HalfedgeHandle(0));
}

//-----------------------------------------------------------------------------
PolyConnectivity::HalfedgeIter PolyConnectivity::halfedges_end()
{
Jan Möbius's avatar
Jan Möbius committed
663
  return HalfedgeIter(*this, HalfedgeHandle(int(n_halfedges())));
664
665
666
667
668
}

//-----------------------------------------------------------------------------
PolyConnectivity::ConstHalfedgeIter PolyConnectivity::halfedges_end() const
{
Jan Möbius's avatar
Jan Möbius committed
669
  return ConstHalfedgeIter(*this, HalfedgeHandle(int(n_halfedges())));
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
}

//-----------------------------------------------------------------------------
PolyConnectivity::EdgeIter PolyConnectivity::edges_begin()
{
  return EdgeIter(*this, EdgeHandle(0));
}

//-----------------------------------------------------------------------------
PolyConnectivity::ConstEdgeIter PolyConnectivity::edges_begin() const
{
  return ConstEdgeIter(*this, EdgeHandle(0));
}

//-----------------------------------------------------------------------------
PolyConnectivity::EdgeIter PolyConnectivity::edges_end()
{
Jan Möbius's avatar
Jan Möbius committed
687
  return EdgeIter(*this, EdgeHandle(int(n_edges())));
688
689
690
691
692
}

//-----------------------------------------------------------------------------
PolyConnectivity::ConstEdgeIter PolyConnectivity::edges_end() const
{
Jan Möbius's avatar
Jan Möbius committed
693
  return ConstEdgeIter(*this, EdgeHandle(int(n_edges())));
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
}

//-----------------------------------------------------------------------------
PolyConnectivity::FaceIter PolyConnectivity::faces_begin()
{
  return FaceIter(*this, FaceHandle(0));
}

//-----------------------------------------------------------------------------
PolyConnectivity::ConstFaceIter PolyConnectivity::faces_begin() const
{
  return ConstFaceIter(*this, FaceHandle(0));
}

//-----------------------------------------------------------------------------
PolyConnectivity::FaceIter PolyConnectivity::faces_end()
{
Jan Möbius's avatar
Jan Möbius committed
711
  return FaceIter(*this, FaceHandle(int(n_faces())));
712
713
714
715
716
}

//-----------------------------------------------------------------------------
PolyConnectivity::ConstFaceIter PolyConnectivity::faces_end() const
{
Jan Möbius's avatar
Jan Möbius committed
717
  return ConstFaceIter(*this, FaceHandle(int(n_faces())));
718
719
}

Jan Möbius's avatar
Jan Möbius committed
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
//-----------------------------------------------------------------------------
void PolyConnectivity::collapse(HalfedgeHandle _hh)
{
  HalfedgeHandle h0 = _hh;
  HalfedgeHandle h1 = next_halfedge_handle(h0);
  HalfedgeHandle o0 = opposite_halfedge_handle(h0);
  HalfedgeHandle o1 = next_halfedge_handle(o0);

  // remove edge
  collapse_edge(h0);

  // remove loops
  if (next_halfedge_handle(next_halfedge_handle(h1)) == h1)
    collapse_loop(next_halfedge_handle(h1));
  if (next_halfedge_handle(next_halfedge_handle(o1)) == o1)
    collapse_loop(o1);
}

//-----------------------------------------------------------------------------
void PolyConnectivity::collapse_edge(HalfedgeHandle _hh)
{
  HalfedgeHandle  h  = _hh;
  HalfedgeHandle  hn = next_halfedge_handle(h);
  HalfedgeHandle  hp = prev_halfedge_handle(h);

  HalfedgeHandle  o  = opposite_halfedge_handle(h);
  HalfedgeHandle  on = next_halfedge_handle(o);
  HalfedgeHandle  op = prev_halfedge_handle(o);

  FaceHandle      fh = face_handle(h);
  FaceHandle      fo = face_handle(o);

  VertexHandle    vh = to_vertex_handle(h);
  VertexHandle    vo = to_vertex_handle(o);



  // halfedge -> vertex
Jan Möbius's avatar
Jan Möbius committed
758
759
  for (VertexIHalfedgeIter vih_it(vih_iter(vo)); vih_it.is_valid(); ++vih_it)
    set_vertex_handle(*vih_it, vh);
Jan Möbius's avatar
Jan Möbius committed
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837


  // halfedge -> halfedge
  set_next_halfedge_handle(hp, hn);
  set_next_halfedge_handle(op, on);


  // face -> halfedge
  if (fh.is_valid())  set_halfedge_handle(fh, hn);
  if (fo.is_valid())  set_halfedge_handle(fo, on);


  // vertex -> halfedge
  if (halfedge_handle(vh) == o)  set_halfedge_handle(vh, hn);
  adjust_outgoing_halfedge(vh);
  set_isolated(vo);

  // delete stuff
  status(edge_handle(h)).set_deleted(true);
  status(vo).set_deleted(true);
}

//-----------------------------------------------------------------------------
void PolyConnectivity::collapse_loop(HalfedgeHandle _hh)
{
  HalfedgeHandle  h0 = _hh;
  HalfedgeHandle  h1 = next_halfedge_handle(h0);

  HalfedgeHandle  o0 = opposite_halfedge_handle(h0);
  HalfedgeHandle  o1 = opposite_halfedge_handle(h1);

  VertexHandle    v0 = to_vertex_handle(h0);
  VertexHandle    v1 = to_vertex_handle(h1);

  FaceHandle      fh = face_handle(h0);
  FaceHandle      fo = face_handle(o0);



  // is it a loop ?
  assert ((next_halfedge_handle(h1) == h0) && (h1 != o0));


  // halfedge -> halfedge
  set_next_halfedge_handle(h1, next_halfedge_handle(o0));
  set_next_halfedge_handle(prev_halfedge_handle(o0), h1);


  // halfedge -> face
  set_face_handle(h1, fo);


  // vertex -> halfedge
  set_halfedge_handle(v0, h1);  adjust_outgoing_halfedge(v0);
  set_halfedge_handle(v1, o1);  adjust_outgoing_halfedge(v1);


  // face -> halfedge
  if (fo.is_valid() && halfedge_handle(fo) == o0)
  {
    set_halfedge_handle(fo, h1);
  }

  // delete stuff
  if (fh.is_valid())  
  {
    set_halfedge_handle(fh, InvalidHalfedgeHandle);
    status(fh).set_deleted(true);
  }
  status(edge_handle(h0)).set_deleted(true);
}

//-----------------------------------------------------------------------------
bool PolyConnectivity::is_simple_link(EdgeHandle _eh) const
{
  HalfedgeHandle heh0 = halfedge_handle(_eh, 0);
  HalfedgeHandle heh1 = halfedge_handle(_eh, 1);
  
Jan Möbius's avatar
Jan Möbius committed
838
  //FaceHandle fh0 = face_handle(heh0);//fh0 or fh1 might be a invalid,
Jan Möbius's avatar
Jan Möbius committed
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
  FaceHandle fh1 = face_handle(heh1);//i.e., representing the boundary
  
  HalfedgeHandle next_heh = next_halfedge_handle(heh0);
  while (next_heh != heh0)
  {//check if there are no other edges shared between fh0 & fh1
    if (opposite_face_handle(next_heh) == fh1)
    {
      return false;
    }
    next_heh = next_halfedge_handle(next_heh);
  }
  return true;
}

//-----------------------------------------------------------------------------
bool PolyConnectivity::is_simply_connected(FaceHandle _fh) const
{
  std::set<FaceHandle> nb_fhs;
Jan Möbius's avatar
Jan Möbius committed
857
  for (ConstFaceFaceIter cff_it = cff_iter(_fh); cff_it.is_valid(); ++cff_it)
Jan Möbius's avatar
Jan Möbius committed
858
  {
Jan Möbius's avatar
Jan Möbius committed
859
    if (nb_fhs.find(*cff_it) == nb_fhs.end())
Jan Möbius's avatar
Jan Möbius committed
860
    {
Jan Möbius's avatar
Jan Möbius committed
861
      nb_fhs.insert(*cff_it);
Jan Möbius's avatar
Jan Möbius committed
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
    }
    else
    {//there is more than one link
      return false;
    }
  }
  return true;
}
  
//-----------------------------------------------------------------------------
PolyConnectivity::FaceHandle 
PolyConnectivity::remove_edge(EdgeHandle _eh)
{
  //don't allow "dangling" vertices and edges
  assert(!status(_eh).deleted() && is_simple_link(_eh));
  
  HalfedgeHandle heh0 = halfedge_handle(_eh, 0);
  HalfedgeHandle heh1 = halfedge_handle(_eh, 1);
  
  //deal with the faces
  FaceHandle rem_fh = face_handle(heh0), del_fh = face_handle(heh1);
  if (!del_fh.is_valid())
  {//boundary case - we must delete the rem_fh
    std::swap(del_fh, rem_fh);
  }
  assert(del_fh.is_valid());
/*  for (FaceHalfedgeIter fh_it = fh_iter(del_fh); fh_it; ++fh_it)
  {//set the face handle of the halfedges of del_fh to point to rem_fh
    set_face_handle(fh_it, rem_fh);  
  } */
  //fix the halfedge relations
  HalfedgeHandle prev_heh0 = prev_halfedge_handle(heh0);
  HalfedgeHandle prev_heh1 = prev_halfedge_handle(heh1);

  HalfedgeHandle next_heh0 = next_halfedge_handle(heh0);
  HalfedgeHandle next_heh1 = next_halfedge_handle(heh1);
  
  set_next_halfedge_handle(prev_heh0, next_heh1);
  set_next_halfedge_handle(prev_heh1, next_heh0);
  //correct outgoing vertex handles for the _eh vertices (if needed)
  VertexHandle vh0 = to_vertex_handle(heh0);
  VertexHandle vh1 = to_vertex_handle(heh1);
  
  if (halfedge_handle(vh0) == heh1)
  {
    set_halfedge_handle(vh0, next_heh0);
  }
  if (halfedge_handle(vh1) == heh0)
  {
    set_halfedge_handle(vh1, next_heh1);
  }
  
  //correct the hafledge handle of rem_fh if needed and preserve its first vertex
  if (halfedge_handle(rem_fh) == heh0)
  {//rem_fh is the face at heh0
    set_halfedge_handle(rem_fh, prev_heh1);
  }
  else if (halfedge_handle(rem_fh) == heh1)
  {//rem_fh is the face at heh1
    set_halfedge_handle(rem_fh, prev_heh0);
  }
Jan Möbius's avatar
Jan Möbius committed
923
  for (FaceHalfedgeIter fh_it = fh_iter(rem_fh); fh_it.is_valid(); ++fh_it)
Jan Möbius's avatar
Jan Möbius committed
924
  {//set the face handle of the halfedges of del_fh to point to rem_fh
Jan Möbius's avatar
Jan Möbius committed
925
    set_face_handle(*fh_it, rem_fh);
Jan Möbius's avatar
Jan Möbius committed
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
  } 
  
  status(_eh).set_deleted(true);  
  status(del_fh).set_deleted(true);  
  return rem_fh;//returns the remaining face handle
}

//-----------------------------------------------------------------------------
void PolyConnectivity::reinsert_edge(EdgeHandle _eh)
{
  //this does not work without prev_halfedge_handle
  assert_compile(sizeof(Halfedge) == sizeof(Halfedge_with_prev));
  //shoudl be deleted  
  assert(status(_eh).deleted());
  status(_eh).set_deleted(false);  
  
  HalfedgeHandle heh0 = halfedge_handle(_eh, 0);
  HalfedgeHandle heh1 = halfedge_handle(_eh, 1);
  FaceHandle rem_fh = face_handle(heh0), del_fh = face_handle(heh1);
  if (!del_fh.is_valid())
  {//boundary case - we must delete the rem_fh
    std::swap(del_fh, rem_fh);
  }
  assert(status(del_fh).deleted());
  status(del_fh).set_deleted(false); 
  
  //restore halfedge relations
  HalfedgeHandle prev_heh0 = prev_halfedge_handle(heh0);
  HalfedgeHandle prev_heh1 = prev_halfedge_handle(heh1);

  HalfedgeHandle next_heh0 = next_halfedge_handle(heh0);
  HalfedgeHandle next_heh1 = next_halfedge_handle(heh1);
  
  set_next_halfedge_handle(prev_heh0, heh0);
  set_prev_halfedge_handle(next_heh0, heh0);
  
  set_next_halfedge_handle(prev_heh1, heh1);
  set_prev_halfedge_handle(next_heh1, heh1);
  
Jan Möbius's avatar
Jan Möbius committed
965
  for (FaceHalfedgeIter fh_it = fh_iter(del_fh); fh_it.is_valid(); ++fh_it)
Jan Möbius's avatar
Jan Möbius committed
966
  {//reassign halfedges to del_fh  
Jan Möbius's avatar
Jan Möbius committed
967
    set_face_handle(*fh_it, del_fh);
Jan Möbius's avatar
Jan Möbius committed
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
  }
   
  if (face_handle(halfedge_handle(rem_fh)) == del_fh)
  {//correct the halfedge handle of rem_fh
    if (halfedge_handle(rem_fh) == prev_heh0)
    {//rem_fh is the face at heh1
      set_halfedge_handle(rem_fh, heh1);
    }
    else
    {//rem_fh is the face at heh0
      assert(halfedge_handle(rem_fh) == prev_heh1);
      set_halfedge_handle(rem_fh, heh0);
    }
  }
}

//-----------------------------------------------------------------------------
PolyConnectivity::HalfedgeHandle
PolyConnectivity::insert_edge(HalfedgeHandle _prev_heh, HalfedgeHandle _next_heh)
{
  assert(face_handle(_prev_heh) == face_handle(_next_heh));//only the manifold case
  assert(next_halfedge_handle(_prev_heh) != _next_heh);//this can not be done
  VertexHandle vh0 = to_vertex_handle(_prev_heh);
  VertexHandle vh1 = from_vertex_handle(_next_heh);
  //create the link between vh0 and vh1
  HalfedgeHandle heh0 = new_edge(vh0, vh1);
  HalfedgeHandle heh1 = opposite_halfedge_handle(heh0);
  HalfedgeHandle next_prev_heh = next_halfedge_handle(_prev_heh);
  HalfedgeHandle prev_next_heh = prev_halfedge_handle(_next_heh);
  set_next_halfedge_handle(_prev_heh, heh0);
  set_next_halfedge_handle(heh0, _next_heh);
  set_next_halfedge_handle(prev_next_heh, heh1);
  set_next_halfedge_handle(heh1, next_prev_heh);
  
  //now set the face handles - the new face is assigned to heh0
  FaceHandle new_fh = new_face();
  set_halfedge_handle(new_fh, heh0);
Jan Möbius's avatar
Jan Möbius committed
1005
  for (FaceHalfedgeIter fh_it = fh_iter(new_fh); fh_it.is_valid(); ++fh_it)
Jan Möbius's avatar
Jan Möbius committed
1006
  {
Jan Möbius's avatar
Jan Möbius committed
1007
    set_face_handle(*fh_it, new_fh);
Jan Möbius's avatar
Jan Möbius committed
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
  }  
  FaceHandle old_fh = face_handle(next_prev_heh);
  set_face_handle(heh1, old_fh);   
  if (old_fh.is_valid() && face_handle(halfedge_handle(old_fh)) == new_fh)
  {//fh pointed to one of the halfedges now assigned to new_fh
    set_halfedge_handle(old_fh, heh1);
  }
  adjust_outgoing_halfedge(vh0);  
  adjust_outgoing_halfedge(vh1);  
  return heh0;
}

//-----------------------------------------------------------------------------
void PolyConnectivity::triangulate(FaceHandle _fh)
{
  /*
    Split an arbitrary face into triangles by connecting
    each vertex of fh after its second to vh.

    - fh will remain valid (it will become one of the
      triangles)
    - the halfedge handles of the new triangles will
      point to the old halfedges
  */

  HalfedgeHandle base_heh(halfedge_handle(_fh));
  VertexHandle start_vh = from_vertex_handle(base_heh);
  HalfedgeHandle next_heh(next_halfedge_handle(base_heh));

  while (to_vertex_handle(next_halfedge_handle(next_heh)) != start_vh)
  {
    HalfedgeHandle next_next_heh(next_halfedge_handle(next_heh));

    FaceHandle new_fh = new_face();
    set_halfedge_handle(new_fh, base_heh);

    HalfedgeHandle new_heh = new_edge(to_vertex_handle(next_heh), start_vh);

    set_next_halfedge_handle(base_heh, next_heh);
    set_next_halfedge_handle(next_heh, new_heh);
    set_next_halfedge_handle(new_heh, base_heh);

    set_face_handle(base_heh, new_fh);
    set_face_handle(next_heh, new_fh);
    set_face_handle(new_heh,  new_fh);

    base_heh = opposite_halfedge_handle(new_heh);
    next_heh = next_next_heh;
  }
  set_halfedge_handle(_fh, base_heh);  //the last face takes the handle _fh

  set_next_halfedge_handle(base_heh, next_heh);
  set_next_halfedge_handle(next_halfedge_handle(next_heh), base_heh);

  set_face_handle(base_heh, _fh);
}

//-----------------------------------------------------------------------------
void PolyConnectivity::triangulate()
{
  /* The iterators will stay valid, even though new faces are added,
     because they are now implemented index-based instead of
     pointer-based.
  */
  FaceIter f_it(faces_begin()), f_end(faces_end());
  for (; f_it!=f_end; ++f_it)
Jan Möbius's avatar
Jan Möbius committed
1074
    triangulate(*f_it);
Jan Möbius's avatar
Jan Möbius committed
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
}

//-----------------------------------------------------------------------------
void PolyConnectivity::split(FaceHandle fh, VertexHandle vh)
{
  HalfedgeHandle hend = halfedge_handle(fh);
  HalfedgeHandle hh   = next_halfedge_handle(hend);

  HalfedgeHandle hold = new_edge(to_vertex_handle(hend), vh);

  set_next_halfedge_handle(hend, hold);
  set_face_handle(hold, fh);

  hold = opposite_halfedge_handle(hold);

  while (hh != hend) {

    HalfedgeHandle hnext = next_halfedge_handle(hh);

    FaceHandle fnew = new_face();
    set_halfedge_handle(fnew, hh);

    HalfedgeHandle hnew = new_edge(to_vertex_handle(hh), vh);

    set_next_halfedge_handle(hnew, hold);
    set_next_halfedge_handle(hold, hh);
    set_next_halfedge_handle(hh, hnew);

    set_face_handle(hnew, fnew);
    set_face_handle(hold, fnew);
    set_face_handle(hh,   fnew);

    hold = opposite_halfedge_handle(hnew);

    hh = hnext;
  }

  set_next_halfedge_handle(hold, hend);
  set_next_halfedge_handle(next_halfedge_handle(hend), hold);

  set_face_handle(hold, fh);

  set_halfedge_handle(vh, hold);
}

1120
1121
1122
1123
1124
1125
1126

void PolyConnectivity::split_copy(FaceHandle fh, VertexHandle vh) {

  // Split the given face (fh will still be valid)
  split(fh, vh);

  // Copy the property of the original face to all new faces
Jan Möbius's avatar
Jan Möbius committed
1127
1128
  for(VertexFaceIter vf_it =  vf_iter(vh); vf_it.is_valid(); ++vf_it)
    copy_all_properties(fh, *vf_it);
1129
1130
}

Jan Möbius's avatar
Jan Möbius committed
1131
1132
1133
1134
//-----------------------------------------------------------------------------
uint PolyConnectivity::valence(VertexHandle _vh) const
{
  uint count(0);
Jan Möbius's avatar
Jan Möbius committed
1135
  for (ConstVertexVertexIter vv_it=cvv_iter(_vh); vv_it.is_valid(); ++vv_it)
Jan Möbius's avatar
Jan Möbius committed
1136
1137
1138
1139
1140
1141
1142
1143
    ++count;
  return count;
}

//-----------------------------------------------------------------------------
uint PolyConnectivity::valence(FaceHandle _fh) const
{
  uint count(0);
Jan Möbius's avatar
Jan Möbius committed
1144
  for (ConstFaceVertexIter fv_it=cfv_iter(_fh); fv_it.is_valid(); ++fv_it)
Jan Möbius's avatar
Jan Möbius committed
1145
1146
1147
1148
    ++count;
  return count;
}

1149
1150
1151
1152
1153
1154
1155
1156
1157
//-----------------------------------------------------------------------------
void PolyConnectivity::split_edge(EdgeHandle _eh, VertexHandle _vh)
{
  HalfedgeHandle h0 = halfedge_handle(_eh, 0);
  HalfedgeHandle h1 = halfedge_handle(_eh, 1);
  
  VertexHandle vfrom = from_vertex_handle(h0);

  HalfedgeHandle ph0 = prev_halfedge_handle(h0);
Jan Möbius's avatar
Jan Möbius committed
1158
  //HalfedgeHandle ph1 = prev_halfedge_handle(h1);
1159
  
Jan Möbius's avatar
Jan Möbius committed
1160
  //HalfedgeHandle nh0 = next_halfedge_handle(h0);
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
  HalfedgeHandle nh1 = next_halfedge_handle(h1);
  
  bool boundary0 = is_boundary(h0);
  bool boundary1 = is_boundary(h1);
  
  //add the new edge
  HalfedgeHandle new_e = new_edge(from_vertex_handle(h0), _vh);
  
  //fix the vertex of the opposite halfedge
  set_vertex_handle(h1, _vh);
  
  //fix the halfedge connectivity
  set_next_halfedge_handle(new_e, h0);
  set_next_halfedge_handle(h1, opposite_halfedge_handle(new_e));
  
  set_next_halfedge_handle(ph0, new_e);
  set_next_halfedge_handle(opposite_halfedge_handle(new_e), nh1);
  
1179
1180
//  set_prev_halfedge_handle(new_e, ph0);
//  set_prev_halfedge_handle(opposite_halfedge_handle(new_e), h1);
1181
  
1182
1183
//  set_prev_halfedge_handle(nh1, opposite_halfedge_handle(new_e));
//  set_prev_halfedge_handle(h0, new_e);
1184
1185
1186
1187
  
  if (!boundary0)
  {
    set_face_handle(new_e, face_handle(h0));
1188
1189
  }
  else
1190
1191
1192
1193
1194
1195
1196
1197
  {
    set_boundary(new_e);
  }
  
  if (!boundary1)
  {
    set_face_handle(opposite_halfedge_handle(new_e), face_handle(h1));
  }
1198
  else
1199
1200
1201
  {
    set_boundary(opposite_halfedge_handle(new_e));
  }
1202
1203
1204

  set_halfedge_handle( _vh, h0 );
  adjust_outgoing_halfedge( _vh );
1205
1206
1207
1208
  
  if (halfedge_handle(vfrom) == h0)
  {
    set_halfedge_handle(vfrom, new_e);
1209
    adjust_outgoing_halfedge( vfrom );
1210
1211
1212
  }
}

1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
//-----------------------------------------------------------------------------

void PolyConnectivity::split_edge_copy(EdgeHandle _eh, VertexHandle _vh)
{
  // Split the edge (handle is kept!)
  split_edge(_eh, _vh);

  // Navigate to the new edge
  EdgeHandle eh0 = edge_handle( next_halfedge_handle( halfedge_handle(_eh, 1) ) );

  // Copy the property from the original to the new edge
  copy_all_properties(_eh, eh0);
}

} // namespace OpenMesh
Jan Möbius's avatar
Jan Möbius committed
1228