Developer Documentation
PolyConnectivity.cc
1 /* ========================================================================= *
2  * *
3  * OpenMesh *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openmesh.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenMesh. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40  * ========================================================================= */
41 
42 
43 
44 //== IMPLEMENTATION ==========================================================
45 #include <OpenMesh/Core/Mesh/PolyConnectivity.hh>
46 #include <set>
47 
48 namespace OpenMesh {
49 
50 const PolyConnectivity::VertexHandle PolyConnectivity::InvalidVertexHandle;
51 const PolyConnectivity::HalfedgeHandle PolyConnectivity::InvalidHalfedgeHandle;
52 const PolyConnectivity::EdgeHandle PolyConnectivity::InvalidEdgeHandle;
53 const PolyConnectivity::FaceHandle PolyConnectivity::InvalidFaceHandle;
54 
55 //-----------------------------------------------------------------------------
56 
58 {
59  assert(_start_vh.is_valid() && _end_vh.is_valid());
60 
61  for (ConstVertexOHalfedgeIter voh_it = cvoh_iter(_start_vh); voh_it.is_valid(); ++voh_it)
62  if (to_vertex_handle(*voh_it) == _end_vh)
63  return *voh_it;
64 
65  return make_smart(InvalidHalfedgeHandle, this);
66 }
67 
68 
69 bool PolyConnectivity::is_boundary(FaceHandle _fh, bool _check_vertex) const
70 {
71  for (ConstFaceEdgeIter cfeit = cfe_iter( _fh ); cfeit.is_valid(); ++cfeit)
72  if (is_boundary( *cfeit ) )
73  return true;
74 
75  if (_check_vertex)
76  {
77  for (ConstFaceVertexIter cfvit = cfv_iter( _fh ); cfvit.is_valid(); ++cfvit)
78  if (is_boundary( *cfvit ) )
79  return true;
80  }
81  return false;
82 }
83 
85 {
86  /* The vertex is non-manifold if more than one gap exists, i.e.
87  more than one outgoing boundary halfedge. If (at least) one
88  boundary halfedge exists, the vertex' halfedge must be a
89  boundary halfedge. If iterating around the vertex finds another
90  boundary halfedge, the vertex is non-manifold. */
91 
92  ConstVertexOHalfedgeIter vh_it(*this, _vh);
93  if (vh_it.is_valid())
94  for (++vh_it; vh_it.is_valid(); ++vh_it)
95  if (is_boundary(*vh_it))
96  return false;
97  return true;
98 }
99 
100 //-----------------------------------------------------------------------------
101 
103 {
104  for (ConstVertexOHalfedgeIter vh_it=cvoh_iter(_vh); vh_it.is_valid(); ++vh_it)
105  {
106  if (is_boundary(*vh_it))
107  {
108  set_halfedge_handle(_vh, *vh_it);
109  break;
110  }
111  }
112 }
113 
114 //-----------------------------------------------------------------------------
115 
117 PolyConnectivity::add_face(const VertexHandle* _vertex_handles, size_t _vhs_size)
118 {
119  VertexHandle vh;
120  size_t i, ii, n(_vhs_size);
121  HalfedgeHandle inner_next, inner_prev,
122  outer_next, outer_prev,
123  boundary_next, boundary_prev,
124  patch_start, patch_end;
125 
126 
127  // Check sufficient working storage available
128  if (edgeData_.size() < n)
129  {
130  edgeData_.resize(n);
131  next_cache_.resize(6*n);
132  }
133 
134  size_t next_cache_count = 0;
135 
136  // don't allow degenerated faces
137  assert (n > 2);
138 
139  // test for topological errors
140  for (i=0, ii=1; i<n; ++i, ++ii, ii%=n)
141  {
142  if ( !is_boundary(_vertex_handles[i]) )
143  {
144  omerr() << "PolyMeshT::add_face: complex vertex\n";
145  return make_smart(InvalidFaceHandle, this);
146  }
147 
148  // Initialise edge attributes
149  edgeData_[i].halfedge_handle = find_halfedge(_vertex_handles[i],
150  _vertex_handles[ii]);
151  edgeData_[i].is_new = !edgeData_[i].halfedge_handle.is_valid();
152  edgeData_[i].needs_adjust = false;
153 
154  if (!edgeData_[i].is_new && !is_boundary(edgeData_[i].halfedge_handle))
155  {
156  omerr() << "PolyMeshT::add_face: complex edge\n";
157  return make_smart(InvalidFaceHandle, this);
158  }
159  }
160 
161  // re-link patches if necessary
162  for (i=0, ii=1; i<n; ++i, ++ii, ii%=n)
163  {
164  if (!edgeData_[i].is_new && !edgeData_[ii].is_new)
165  {
166  inner_prev = edgeData_[i].halfedge_handle;
167  inner_next = edgeData_[ii].halfedge_handle;
168 
169 
170  if (next_halfedge_handle(inner_prev) != inner_next)
171  {
172  // here comes the ugly part... we have to relink a whole patch
173 
174  // search a free gap
175  // free gap will be between boundary_prev and boundary_next
176  outer_prev = opposite_halfedge_handle(inner_next);
177  outer_next = opposite_halfedge_handle(inner_prev);
178  boundary_prev = outer_prev;
179  do
180  boundary_prev =
182  while (!is_boundary(boundary_prev));
183  boundary_next = next_halfedge_handle(boundary_prev);
184 
185  // ok ?
186  if (boundary_prev == inner_prev)
187  {
188  omerr() << "PolyMeshT::add_face: patch re-linking failed\n";
189  return make_smart(InvalidFaceHandle, this);
190  }
191 
192  assert(is_boundary(boundary_prev));
193  assert(is_boundary(boundary_next));
194 
195  // other halfedges' handles
196  patch_start = next_halfedge_handle(inner_prev);
197  patch_end = prev_halfedge_handle(inner_next);
198 
199  assert(boundary_prev.is_valid());
200  assert(patch_start.is_valid());
201  assert(patch_end.is_valid());
202  assert(boundary_next.is_valid());
203  assert(inner_prev.is_valid());
204  assert(inner_next.is_valid());
205 
206  // relink
207  next_cache_[next_cache_count++] = std::make_pair(boundary_prev, patch_start);
208  next_cache_[next_cache_count++] = std::make_pair(patch_end, boundary_next);
209  next_cache_[next_cache_count++] = std::make_pair(inner_prev, inner_next);
210  }
211  }
212  }
213 
214  // create missing edges
215  for (i=0, ii=1; i<n; ++i, ++ii, ii%=n)
216  if (edgeData_[i].is_new)
217  edgeData_[i].halfedge_handle = new_edge(_vertex_handles[i], _vertex_handles[ii]);
218 
219  // create the face
220  FaceHandle fh(new_face());
221  set_halfedge_handle(fh, edgeData_[n-1].halfedge_handle);
222 
223  // setup halfedges
224  for (i=0, ii=1; i<n; ++i, ++ii, ii%=n)
225  {
226  vh = _vertex_handles[ii];
227 
228  inner_prev = edgeData_[i].halfedge_handle;
229  inner_next = edgeData_[ii].halfedge_handle;
230  assert(inner_prev.is_valid());
231  assert(inner_next.is_valid());
232 
233  size_t id = 0;
234  if (edgeData_[i].is_new) id |= 1;
235  if (edgeData_[ii].is_new) id |= 2;
236 
237 
238  if (id)
239  {
240  outer_prev = opposite_halfedge_handle(inner_next);
241  outer_next = opposite_halfedge_handle(inner_prev);
242  assert(outer_prev.is_valid());
243  assert(outer_next.is_valid());
244 
245  // set outer links
246  switch (id)
247  {
248  case 1: // prev is new, next is old
249  boundary_prev = prev_halfedge_handle(inner_next);
250  assert(boundary_prev.is_valid());
251  next_cache_[next_cache_count++] = std::make_pair(boundary_prev, outer_next);
252  set_halfedge_handle(vh, outer_next);
253  break;
254 
255  case 2: // next is new, prev is old
256  boundary_next = next_halfedge_handle(inner_prev);
257  assert(boundary_next.is_valid());
258  next_cache_[next_cache_count++] = std::make_pair(outer_prev, boundary_next);
259  set_halfedge_handle(vh, boundary_next);
260  break;
261 
262  case 3: // both are new
263  if (!halfedge_handle(vh).is_valid())
264  {
265  set_halfedge_handle(vh, outer_next);
266  next_cache_[next_cache_count++] = std::make_pair(outer_prev, outer_next);
267  }
268  else
269  {
270  boundary_next = halfedge_handle(vh);
271  boundary_prev = prev_halfedge_handle(boundary_next);
272  assert(boundary_prev.is_valid());
273  assert(boundary_next.is_valid());
274  next_cache_[next_cache_count++] = std::make_pair(boundary_prev, outer_next);
275  next_cache_[next_cache_count++] = std::make_pair(outer_prev, boundary_next);
276  }
277  break;
278  }
279 
280  // set inner link
281  next_cache_[next_cache_count++] = std::make_pair(inner_prev, inner_next);
282  }
283  else edgeData_[ii].needs_adjust = (halfedge_handle(vh) == inner_next);
284 
285 
286  // set face handle
287  set_face_handle(edgeData_[i].halfedge_handle, fh);
288  }
289 
290  // process next halfedge cache
291  for (i = 0; i < next_cache_count; ++i)
292  set_next_halfedge_handle(next_cache_[i].first, next_cache_[i].second);
293 
294 
295  // adjust vertices' halfedge handle
296  for (i=0; i<n; ++i)
297  if (edgeData_[i].needs_adjust)
298  adjust_outgoing_halfedge(_vertex_handles[i]);
299 
300  return make_smart(fh, this);
301 }
302 
303 //-----------------------------------------------------------------------------
304 
306 {
307  // Check if we have been given a degenerate configuration (2 vertices are identical)
308  assert(_vh0!=_vh1);
309  assert(_vh0!=_vh2);
310  assert(_vh1!=_vh2);
311  assert(_vh0!=_vh3);
312  assert(_vh1!=_vh3);
313  assert(_vh2!=_vh3);
314 
315  VertexHandle vhs[4] = { _vh0, _vh1, _vh2, _vh3 };
316  return add_face(vhs, 4);
317 }
318 
319 //-----------------------------------------------------------------------------
320 
322 {
323  // Check if we have been given a degenerate configuration (2 vertices are identical)
324  assert(_vh0!=_vh1);
325  assert(_vh0!=_vh2);
326  assert(_vh1!=_vh2);
327 
328  VertexHandle vhs[3] = { _vh0, _vh1, _vh2 };
329  return add_face(vhs, 3);
330 }
331 
332 //-----------------------------------------------------------------------------
333 
334 SmartFaceHandle PolyConnectivity::add_face(const std::vector<VertexHandle>& _vhandles)
335 { return add_face(&_vhandles.front(), _vhandles.size()); }
336 
337 //-----------------------------------------------------------------------------
338 
339 SmartFaceHandle PolyConnectivity::add_face(const std::vector<SmartVertexHandle>& _vhandles)
340 {
341  std::vector<VertexHandle> vhandles(_vhandles.begin(), _vhandles.end());
342  return add_face(&vhandles.front(), vhandles.size());
343 }
344 
345 
346 //-----------------------------------------------------------------------------
348 {
349  //is edge already deleteed?
350  if (status(edge_handle(v0v1)).deleted())
351  {
352  return false;
353  }
354 
356  VertexHandle v0(to_vertex_handle(v1v0));
357  VertexHandle v1(to_vertex_handle(v0v1));
358 
359  bool v0v1_triangle = false;
360  bool v1v0_triangle = false;
361 
362  if (!is_boundary(v0v1))
363  v0v1_triangle = valence(face_handle(v0v1)) == 3;
364 
365  if (!is_boundary(v1v0))
366  v1v0_triangle = valence(face_handle(v1v0)) == 3;
367 
368  //in a quadmesh we dont have the "next" or "previous" vhandle, so we need to look at previous and next on both sides
369  //VertexHandle v_01_p = from_vertex_handle(prev_halfedge_handle(v0v1));
370  VertexHandle v_01_n = to_vertex_handle(next_halfedge_handle(v0v1));
371 
372  //VertexHandle v_10_p = from_vertex_handle(prev_halfedge_handle(v1v0));
373  VertexHandle v_10_n = to_vertex_handle(next_halfedge_handle(v1v0));
374 
375  //are the vertices already deleted ?
376  if (status(v0).deleted() || status(v1).deleted())
377  {
378  return false;
379  }
380 
381  //the edges v1-vl and vl-v0 must not be both boundary edges
382  //this test makes only sense in a polymesh if the side face is a triangle
383  VertexHandle vl;
384  if (!is_boundary(v0v1))
385  {
386  if (v0v1_triangle)
387  {
390 
391  vl = to_vertex_handle(h1);
392 
394  return false;
395  }
396  }
397 
398  //the edges v0-vr and vr-v1 must not be both boundary edges
399  //this test makes only sense in a polymesh if the side face is a triangle
400  VertexHandle vr;
401  if (!is_boundary(v1v0))
402  {
403  if (v1v0_triangle)
404  {
407 
408  vr = to_vertex_handle(h1);
409 
411  return false;
412  }
413  }
414 
415  // if vl and vr are equal and valid (e.g. triangle case) -> fail
416  if ( vl.is_valid() && (vl == vr)) return false;
417 
418  // edge between two boundary vertices should be a boundary edge
419  if ( is_boundary(v0) && is_boundary(v1) && !is_boundary(v0v1) && !is_boundary(v1v0))
420  return false;
421 
422  VertexVertexIter vv_it;
423  // test intersection of the one-rings of v0 and v1
424  for (vv_it = vv_iter(v0); vv_it.is_valid(); ++vv_it)
425  {
426  status(*vv_it).set_tagged(false);
427  }
428 
429  for (vv_it = vv_iter(v1); vv_it.is_valid(); ++vv_it)
430  {
431  status(*vv_it).set_tagged(true);
432  }
433 
434  for (vv_it = vv_iter(v0); vv_it.is_valid(); ++vv_it)
435  {
436  if (status(*vv_it).tagged() &&
437  !(*vv_it == v_01_n && v0v1_triangle) &&
438  !(*vv_it == v_10_n && v1v0_triangle)
439  )
440  {
441  return false;
442  }
443  }
444 
445  //test for a face on the backside/other side that might degenerate
446  if (v0v1_triangle)
447  {
448  HalfedgeHandle one, two;
449  one = next_halfedge_handle(v0v1);
450  two = next_halfedge_handle(one);
451 
452  one = opposite_halfedge_handle(one);
453  two = opposite_halfedge_handle(two);
454 
455  if (face_handle(one) == face_handle(two) && valence(face_handle(one)) != 3)
456  {
457  return false;
458  }
459  }
460 
461  if (v1v0_triangle)
462  {
463  HalfedgeHandle one, two;
464  one = next_halfedge_handle(v1v0);
465  two = next_halfedge_handle(one);
466 
467  one = opposite_halfedge_handle(one);
468  two = opposite_halfedge_handle(two);
469 
470  if (face_handle(one) == face_handle(two) && valence(face_handle(one)) != 3)
471  {
472  return false;
473  }
474  }
475 
476  if (status(*vv_it).tagged() && v_01_n == v_10_n && v0v1_triangle && v1v0_triangle)
477  {
478  return false;
479  }
480 
481  // passed all tests
482  return true;
483 }
484 
485 //-----------------------------------------------------------------------------
486 
487 void PolyConnectivity::delete_vertex(VertexHandle _vh, bool _delete_isolated_vertices)
488 {
489  // store incident faces
490  std::vector<FaceHandle> face_handles;
491  face_handles.reserve(8);
492  for (VFIter vf_it(vf_iter(_vh)); vf_it.is_valid(); ++vf_it)
493  face_handles.push_back(*vf_it);
494 
495 
496  // delete collected faces
497  std::vector<FaceHandle>::iterator fh_it(face_handles.begin()),
498  fh_end(face_handles.end());
499 
500  for (; fh_it!=fh_end; ++fh_it)
501  delete_face(*fh_it, _delete_isolated_vertices);
502 
503  status(_vh).set_deleted(true);
504 }
505 
506 //-----------------------------------------------------------------------------
507 
508 void PolyConnectivity::delete_edge(EdgeHandle _eh, bool _delete_isolated_vertices)
509 {
510  FaceHandle fh0(face_handle(halfedge_handle(_eh, 0)));
511  FaceHandle fh1(face_handle(halfedge_handle(_eh, 1)));
512 
513  if (fh0.is_valid()) delete_face(fh0, _delete_isolated_vertices);
514  if (fh1.is_valid()) delete_face(fh1, _delete_isolated_vertices);
515 
516  // If there is no face, we delete the edge
517  // here
518  if ( ! fh0.is_valid() && !fh1.is_valid()) {
519  // mark edge deleted if the mesh has a edge status
520  if ( has_edge_status() )
521  status(_eh).set_deleted(true);
522 
523  // mark corresponding halfedges as deleted
524  // As the deleted edge is boundary,
525  // all corresponding halfedges will also be deleted.
526  if ( has_halfedge_status() ) {
527  status(halfedge_handle(_eh, 0)).set_deleted(true);
528  status(halfedge_handle(_eh, 1)).set_deleted(true);
529  }
530  }
531 }
532 
533 //-----------------------------------------------------------------------------
534 
535 void PolyConnectivity::delete_face(FaceHandle _fh, bool _delete_isolated_vertices)
536 {
537  assert(_fh.is_valid() && !status(_fh).deleted());
538 
539  // mark face deleted
540  status(_fh).set_deleted(true);
541 
542 
543  // this vector will hold all boundary edges of face _fh
544  // these edges will be deleted
545  std::vector<EdgeHandle> deleted_edges;
546  deleted_edges.reserve(3);
547 
548 
549  // this vector will hold all vertices of face _fh
550  // for updating their outgoing halfedge
551  std::vector<VertexHandle> vhandles;
552  vhandles.reserve(3);
553 
554 
555  // for all halfedges of face _fh do:
556  // 1) invalidate face handle.
557  // 2) collect all boundary halfedges, set them deleted
558  // 3) store vertex handles
559  HalfedgeHandle hh;
560  for (FaceHalfedgeIter fh_it(fh_iter(_fh)); fh_it.is_valid(); ++fh_it)
561  {
562  hh = *fh_it;
563 
564  set_boundary(hh);//set_face_handle(hh, InvalidFaceHandle);
565 
567  deleted_edges.push_back(edge_handle(hh));
568 
569  vhandles.push_back(to_vertex_handle(hh));
570  }
571 
572 
573  // delete all collected (half)edges
574  // these edges were all boundary
575  // delete isolated vertices (if _delete_isolated_vertices is true)
576  if (!deleted_edges.empty())
577  {
578  std::vector<EdgeHandle>::iterator del_it(deleted_edges.begin()),
579  del_end(deleted_edges.end());
580  HalfedgeHandle h0, h1, next0, next1, prev0, prev1;
581  VertexHandle v0, v1;
582 
583  for (; del_it!=del_end; ++del_it)
584  {
585  h0 = halfedge_handle(*del_it, 0);
586  v0 = to_vertex_handle(h0);
587  next0 = next_halfedge_handle(h0);
588  prev0 = prev_halfedge_handle(h0);
589 
590  h1 = halfedge_handle(*del_it, 1);
591  v1 = to_vertex_handle(h1);
592  next1 = next_halfedge_handle(h1);
593  prev1 = prev_halfedge_handle(h1);
594 
595  // adjust next and prev handles
596  set_next_halfedge_handle(prev0, next1);
597  set_next_halfedge_handle(prev1, next0);
598 
599  // mark edge deleted if the mesh has a edge status
600  if ( has_edge_status() )
601  status(*del_it).set_deleted(true);
602 
603 
604  // mark corresponding halfedges as deleted
605  // As the deleted edge is boundary,
606  // all corresponding halfedges will also be deleted.
607  if ( has_halfedge_status() ) {
608  status(h0).set_deleted(true);
609  status(h1).set_deleted(true);
610  }
611 
612  // update v0
613  if (halfedge_handle(v0) == h1)
614  {
615  // isolated ?
616  if (next0 == h1)
617  {
618  if (_delete_isolated_vertices)
619  status(v0).set_deleted(true);
620  set_isolated(v0);
621  }
622  else set_halfedge_handle(v0, next0);
623  }
624 
625  // update v1
626  if (halfedge_handle(v1) == h0)
627  {
628  // isolated ?
629  if (next1 == h0)
630  {
631  if (_delete_isolated_vertices)
632  status(v1).set_deleted(true);
633  set_isolated(v1);
634  }
635  else set_halfedge_handle(v1, next1);
636  }
637  }
638  }
639 
640  // update outgoing halfedge handles of remaining vertices
641  std::vector<VertexHandle>::iterator v_it(vhandles.begin()),
642  v_end(vhandles.end());
643  for (; v_it!=v_end; ++v_it)
645 }
646 
647 
648 //-----------------------------------------------------------------------------
650 {
651  HalfedgeHandle h0 = _hh;
655 
656  // remove edge
657  collapse_edge(h0);
658 
659  // remove loops
663  collapse_loop(o1);
664 }
665 
666 //-----------------------------------------------------------------------------
668 {
669  HalfedgeHandle h = _hh;
672 
676 
677  FaceHandle fh = face_handle(h);
678  FaceHandle fo = face_handle(o);
679 
680  VertexHandle vh = to_vertex_handle(h);
681  VertexHandle vo = to_vertex_handle(o);
682 
683 
684 
685  // halfedge -> vertex
686  for (VertexIHalfedgeIter vih_it(vih_iter(vo)); vih_it.is_valid(); ++vih_it)
687  set_vertex_handle(*vih_it, vh);
688 
689 
690  // halfedge -> halfedge
691  set_next_halfedge_handle(hp, hn);
692  set_next_halfedge_handle(op, on);
693 
694 
695  // face -> halfedge
696  if (fh.is_valid()) set_halfedge_handle(fh, hn);
697  if (fo.is_valid()) set_halfedge_handle(fo, on);
698 
699 
700  // vertex -> halfedge
701  if (halfedge_handle(vh) == o) set_halfedge_handle(vh, hn);
703  set_isolated(vo);
704 
705  // delete stuff
706  status(edge_handle(h)).set_deleted(true);
707  status(vo).set_deleted(true);
708  if (has_halfedge_status())
709  {
710  status(h).set_deleted(true);
711  status(o).set_deleted(true);
712  }
713 }
714 
715 //-----------------------------------------------------------------------------
717 {
718  HalfedgeHandle h0 = _hh;
720 
723 
724  VertexHandle v0 = to_vertex_handle(h0);
725  VertexHandle v1 = to_vertex_handle(h1);
726 
727  FaceHandle fh = face_handle(h0);
728  FaceHandle fo = face_handle(o0);
729 
730 
731 
732  // is it a loop ?
733  assert ((next_halfedge_handle(h1) == h0) && (h1 != o0));
734 
735 
736  // halfedge -> halfedge
737  set_next_halfedge_handle(h1, next_halfedge_handle(o0));
738  set_next_halfedge_handle(prev_halfedge_handle(o0), h1);
739 
740 
741  // halfedge -> face
742  set_face_handle(h1, fo);
743 
744 
745  // vertex -> halfedge
746  set_halfedge_handle(v0, h1); adjust_outgoing_halfedge(v0);
747  set_halfedge_handle(v1, o1); adjust_outgoing_halfedge(v1);
748 
749 
750  // face -> halfedge
751  if (fo.is_valid() && halfedge_handle(fo) == o0)
752  {
753  set_halfedge_handle(fo, h1);
754  }
755 
756  // delete stuff
757  if (fh.is_valid())
758  {
759  set_halfedge_handle(fh, InvalidHalfedgeHandle);
760  status(fh).set_deleted(true);
761  }
762  status(edge_handle(h0)).set_deleted(true);
763  if (has_halfedge_status())
764  {
765  status(h0).set_deleted(true);
766  status(o0).set_deleted(true);
767  }
768 }
769 
770 //-----------------------------------------------------------------------------
772 {
773  HalfedgeHandle heh0 = halfedge_handle(_eh, 0);
774  HalfedgeHandle heh1 = halfedge_handle(_eh, 1);
775 
776  //FaceHandle fh0 = face_handle(heh0);//fh0 or fh1 might be a invalid,
777  FaceHandle fh1 = face_handle(heh1);//i.e., representing the boundary
778 
779  HalfedgeHandle next_heh = next_halfedge_handle(heh0);
780  while (next_heh != heh0)
781  {//check if there are no other edges shared between fh0 & fh1
782  if (opposite_face_handle(next_heh) == fh1)
783  {
784  return false;
785  }
786  next_heh = next_halfedge_handle(next_heh);
787  }
788  return true;
789 }
790 
791 //-----------------------------------------------------------------------------
793 {
794  std::set<FaceHandle> nb_fhs;
795  for (ConstFaceFaceIter cff_it = cff_iter(_fh); cff_it.is_valid(); ++cff_it)
796  {
797  if (nb_fhs.find(*cff_it) == nb_fhs.end())
798  {
799  nb_fhs.insert(*cff_it);
800  }
801  else
802  {//there is more than one link
803  return false;
804  }
805  }
806  return true;
807 }
808 
809 //-----------------------------------------------------------------------------
812 {
813  //don't allow "dangling" vertices and edges
814  assert(!status(_eh).deleted() && is_simple_link(_eh));
815 
816  HalfedgeHandle heh0 = halfedge_handle(_eh, 0);
817  HalfedgeHandle heh1 = halfedge_handle(_eh, 1);
818 
819  //deal with the faces
820  FaceHandle rem_fh = face_handle(heh0), del_fh = face_handle(heh1);
821  if (!del_fh.is_valid())
822  {//boundary case - we must delete the rem_fh
823  std::swap(del_fh, rem_fh);
824  }
825  assert(del_fh.is_valid());
826 /* for (FaceHalfedgeIter fh_it = fh_iter(del_fh); fh_it; ++fh_it)
827  {//set the face handle of the halfedges of del_fh to point to rem_fh
828  set_face_handle(fh_it, rem_fh);
829  } */
830  //fix the halfedge relations
831  HalfedgeHandle prev_heh0 = prev_halfedge_handle(heh0);
832  HalfedgeHandle prev_heh1 = prev_halfedge_handle(heh1);
833 
834  HalfedgeHandle next_heh0 = next_halfedge_handle(heh0);
835  HalfedgeHandle next_heh1 = next_halfedge_handle(heh1);
836 
837  set_next_halfedge_handle(prev_heh0, next_heh1);
838  set_next_halfedge_handle(prev_heh1, next_heh0);
839  //correct outgoing vertex handles for the _eh vertices (if needed)
840  VertexHandle vh0 = to_vertex_handle(heh0);
841  VertexHandle vh1 = to_vertex_handle(heh1);
842 
843  if (halfedge_handle(vh0) == heh1)
844  {
845  set_halfedge_handle(vh0, next_heh0);
846  }
847  if (halfedge_handle(vh1) == heh0)
848  {
849  set_halfedge_handle(vh1, next_heh1);
850  }
851 
852  //correct the hafledge handle of rem_fh if needed and preserve its first vertex
853  if (halfedge_handle(rem_fh) == heh0)
854  {//rem_fh is the face at heh0
855  set_halfedge_handle(rem_fh, prev_heh1);
856  }
857  else if (halfedge_handle(rem_fh) == heh1)
858  {//rem_fh is the face at heh1
859  set_halfedge_handle(rem_fh, prev_heh0);
860  }
861  for (FaceHalfedgeIter fh_it = fh_iter(rem_fh); fh_it.is_valid(); ++fh_it)
862  {//set the face handle of the halfedges of del_fh to point to rem_fh
863  set_face_handle(*fh_it, rem_fh);
864  }
865 
866  status(_eh).set_deleted(true);
867  status(del_fh).set_deleted(true);
868  return rem_fh;//returns the remaining face handle
869 }
870 
871 //-----------------------------------------------------------------------------
873 {
874  //this does not work without prev_halfedge_handle
875  assert_compile(sizeof(Halfedge) == sizeof(Halfedge_with_prev));
876  //shoudl be deleted
877  assert(status(_eh).deleted());
878  status(_eh).set_deleted(false);
879 
880  HalfedgeHandle heh0 = halfedge_handle(_eh, 0);
881  HalfedgeHandle heh1 = halfedge_handle(_eh, 1);
882  FaceHandle rem_fh = face_handle(heh0), del_fh = face_handle(heh1);
883  if (!del_fh.is_valid())
884  {//boundary case - we must delete the rem_fh
885  std::swap(del_fh, rem_fh);
886  }
887  assert(status(del_fh).deleted());
888  status(del_fh).set_deleted(false);
889 
890  //restore halfedge relations
891  HalfedgeHandle prev_heh0 = prev_halfedge_handle(heh0);
892  HalfedgeHandle prev_heh1 = prev_halfedge_handle(heh1);
893 
894  HalfedgeHandle next_heh0 = next_halfedge_handle(heh0);
895  HalfedgeHandle next_heh1 = next_halfedge_handle(heh1);
896 
897  set_next_halfedge_handle(prev_heh0, heh0);
898  set_prev_halfedge_handle(next_heh0, heh0);
899 
900  set_next_halfedge_handle(prev_heh1, heh1);
901  set_prev_halfedge_handle(next_heh1, heh1);
902 
903  for (FaceHalfedgeIter fh_it = fh_iter(del_fh); fh_it.is_valid(); ++fh_it)
904  {//reassign halfedges to del_fh
905  set_face_handle(*fh_it, del_fh);
906  }
907 
908  if (face_handle(halfedge_handle(rem_fh)) == del_fh)
909  {//correct the halfedge handle of rem_fh
910  if (halfedge_handle(rem_fh) == prev_heh0)
911  {//rem_fh is the face at heh1
912  set_halfedge_handle(rem_fh, heh1);
913  }
914  else
915  {//rem_fh is the face at heh0
916  assert(halfedge_handle(rem_fh) == prev_heh1);
917  set_halfedge_handle(rem_fh, heh0);
918  }
919  }
920 }
921 
922 //-----------------------------------------------------------------------------
925 {
926  assert(face_handle(_prev_heh) == face_handle(_next_heh));//only the manifold case
927  assert(next_halfedge_handle(_prev_heh) != _next_heh);//this can not be done
928  VertexHandle vh0 = to_vertex_handle(_prev_heh);
929  VertexHandle vh1 = from_vertex_handle(_next_heh);
930  //create the link between vh0 and vh1
931  HalfedgeHandle heh0 = new_edge(vh0, vh1);
933  HalfedgeHandle next_prev_heh = next_halfedge_handle(_prev_heh);
934  HalfedgeHandle prev_next_heh = prev_halfedge_handle(_next_heh);
935  set_next_halfedge_handle(_prev_heh, heh0);
936  set_next_halfedge_handle(heh0, _next_heh);
937  set_next_halfedge_handle(prev_next_heh, heh1);
938  set_next_halfedge_handle(heh1, next_prev_heh);
939 
940  //now set the face handles - the new face is assigned to heh0
941  FaceHandle new_fh = new_face();
942  set_halfedge_handle(new_fh, heh0);
943  for (FaceHalfedgeIter fh_it = fh_iter(new_fh); fh_it.is_valid(); ++fh_it)
944  {
945  set_face_handle(*fh_it, new_fh);
946  }
947  FaceHandle old_fh = face_handle(next_prev_heh);
948  set_face_handle(heh1, old_fh);
949  if (old_fh.is_valid() && face_handle(halfedge_handle(old_fh)) == new_fh)
950  {//fh pointed to one of the halfedges now assigned to new_fh
951  set_halfedge_handle(old_fh, heh1);
952  }
955  return heh0;
956 }
957 
958 //-----------------------------------------------------------------------------
960 {
961  /*
962  Split an arbitrary face into triangles by connecting
963  each vertex of fh after its second to vh.
964 
965  - fh will remain valid (it will become one of the
966  triangles)
967  - the halfedge handles of the new triangles will
968  point to the old halfedges
969  */
970 
971  HalfedgeHandle base_heh(halfedge_handle(_fh));
972  VertexHandle start_vh = from_vertex_handle(base_heh);
973  HalfedgeHandle prev_heh(prev_halfedge_handle(base_heh));
974  HalfedgeHandle next_heh(next_halfedge_handle(base_heh));
975 
976  while (to_vertex_handle(next_halfedge_handle(next_heh)) != start_vh)
977  {
978  HalfedgeHandle next_next_heh(next_halfedge_handle(next_heh));
979 
980  FaceHandle new_fh = new_face();
981  set_halfedge_handle(new_fh, base_heh);
982 
983  HalfedgeHandle new_heh = new_edge(to_vertex_handle(next_heh), start_vh);
984 
985  set_next_halfedge_handle(base_heh, next_heh);
986  set_next_halfedge_handle(next_heh, new_heh);
987  set_next_halfedge_handle(new_heh, base_heh);
988 
989  set_face_handle(base_heh, new_fh);
990  set_face_handle(next_heh, new_fh);
991  set_face_handle(new_heh, new_fh);
992 
993  copy_all_properties(prev_heh, new_heh, true);
994  copy_all_properties(prev_heh, opposite_halfedge_handle(new_heh), true);
995  copy_all_properties(_fh, new_fh, true);
996 
997  base_heh = opposite_halfedge_handle(new_heh);
998  next_heh = next_next_heh;
999  }
1000  set_halfedge_handle(_fh, base_heh); //the last face takes the handle _fh
1001 
1002  set_next_halfedge_handle(base_heh, next_heh);
1003  set_next_halfedge_handle(next_halfedge_handle(next_heh), base_heh);
1004 
1005  set_face_handle(base_heh, _fh);
1006 }
1007 
1008 //-----------------------------------------------------------------------------
1010 {
1011  /* The iterators will stay valid, even though new faces are added,
1012  because they are now implemented index-based instead of
1013  pointer-based.
1014  */
1015  FaceIter f_it(faces_begin()), f_end(faces_end());
1016  for (; f_it!=f_end; ++f_it)
1017  triangulate(*f_it);
1018 }
1019 
1020 //-----------------------------------------------------------------------------
1022 {
1023  HalfedgeHandle hend = halfedge_handle(fh);
1025 
1026  HalfedgeHandle hold = new_edge(to_vertex_handle(hend), vh);
1027 
1028  set_next_halfedge_handle(hend, hold);
1029  set_face_handle(hold, fh);
1030 
1031  hold = opposite_halfedge_handle(hold);
1032 
1033  while (hh != hend) {
1034 
1036 
1037  FaceHandle fnew = new_face();
1038  set_halfedge_handle(fnew, hh);
1039 
1040  HalfedgeHandle hnew = new_edge(to_vertex_handle(hh), vh);
1041 
1042  set_next_halfedge_handle(hnew, hold);
1043  set_next_halfedge_handle(hold, hh);
1044  set_next_halfedge_handle(hh, hnew);
1045 
1046  set_face_handle(hnew, fnew);
1047  set_face_handle(hold, fnew);
1048  set_face_handle(hh, fnew);
1049 
1050  hold = opposite_halfedge_handle(hnew);
1051 
1052  hh = hnext;
1053  }
1054 
1055  set_next_halfedge_handle(hold, hend);
1056  set_next_halfedge_handle(next_halfedge_handle(hend), hold);
1057 
1058  set_face_handle(hold, fh);
1059 
1060  set_halfedge_handle(vh, hold);
1061 }
1062 
1063 
1065 
1066  // Split the given face (fh will still be valid)
1067  split(fh, vh);
1068 
1069  // Copy the property of the original face to all new faces
1070  for(VertexFaceIter vf_it = vf_iter(vh); vf_it.is_valid(); ++vf_it)
1071  copy_all_properties(fh, *vf_it, true);
1072 }
1073 
1074 //-----------------------------------------------------------------------------
1076 {
1077  uint count(0);
1078  for (ConstVertexVertexIter vv_it=cvv_iter(_vh); vv_it.is_valid(); ++vv_it)
1079  ++count;
1080  return count;
1081 }
1082 
1083 //-----------------------------------------------------------------------------
1085 {
1086  uint count(0);
1087  for (ConstFaceVertexIter fv_it=cfv_iter(_fh); fv_it.is_valid(); ++fv_it)
1088  ++count;
1089  return count;
1090 }
1091 
1092 //-----------------------------------------------------------------------------
1094 {
1095  HalfedgeHandle h0 = halfedge_handle(_eh, 0);
1096  HalfedgeHandle h1 = halfedge_handle(_eh, 1);
1097 
1098  VertexHandle vfrom = from_vertex_handle(h0);
1099 
1101  //HalfedgeHandle ph1 = prev_halfedge_handle(h1);
1102 
1103  //HalfedgeHandle nh0 = next_halfedge_handle(h0);
1105 
1106  bool boundary0 = is_boundary(h0);
1107  bool boundary1 = is_boundary(h1);
1108 
1109  //add the new edge
1110  HalfedgeHandle new_e = new_edge(from_vertex_handle(h0), _vh);
1111 
1112  //fix the vertex of the opposite halfedge
1113  set_vertex_handle(h1, _vh);
1114 
1115  //fix the halfedge connectivity
1116  set_next_halfedge_handle(new_e, h0);
1117  set_next_halfedge_handle(h1, opposite_halfedge_handle(new_e));
1118 
1119  set_next_halfedge_handle(ph0, new_e);
1120  set_next_halfedge_handle(opposite_halfedge_handle(new_e), nh1);
1121 
1122 // set_prev_halfedge_handle(new_e, ph0);
1123 // set_prev_halfedge_handle(opposite_halfedge_handle(new_e), h1);
1124 
1125 // set_prev_halfedge_handle(nh1, opposite_halfedge_handle(new_e));
1126 // set_prev_halfedge_handle(h0, new_e);
1127 
1128  if (!boundary0)
1129  {
1130  set_face_handle(new_e, face_handle(h0));
1131  }
1132  else
1133  {
1134  set_boundary(new_e);
1135  }
1136 
1137  if (!boundary1)
1138  {
1139  set_face_handle(opposite_halfedge_handle(new_e), face_handle(h1));
1140  }
1141  else
1142  {
1143  set_boundary(opposite_halfedge_handle(new_e));
1144  }
1145 
1146  set_halfedge_handle( _vh, h0 );
1147  adjust_outgoing_halfedge( _vh );
1148 
1149  if (halfedge_handle(vfrom) == h0)
1150  {
1151  set_halfedge_handle(vfrom, new_e);
1152  adjust_outgoing_halfedge( vfrom );
1153  }
1154 }
1155 
1156 //-----------------------------------------------------------------------------
1157 
1159 {
1160  // Split the edge (handle is kept!)
1161  split_edge(_eh, _vh);
1162 
1163  // Navigate to the new edge
1165 
1166  // Copy the property from the original to the new edge
1167  copy_all_properties(_eh, eh0, true);
1168 }
1169 
1170 } // namespace OpenMesh
1171 
Handle for a edge entity.
Definition: Handles.hh:134
Handle for a face entity.
Definition: Handles.hh:141
bool tagged() const
is tagged ?
Definition: Status.hh:133
bool is_manifold(VertexHandle _vh) const
Is (the mesh at) vertex _vh two-manifold ?
void split_copy(FaceHandle _fh, VertexHandle _vh)
Face split (= 1-to-n split).
SmartHalfedgeHandle next_halfedge_handle(SmartHalfedgeHandle _heh) const
returns the face handle of the opposite halfedge
void set_tagged(bool _b)
set tagged
Definition: Status.hh:135
void collapse(HalfedgeHandle _heh)
void adjust_outgoing_halfedge(VertexHandle _vh)
void delete_vertex(VertexHandle _vh, bool _delete_isolated_vertices=true)
SmartHalfedgeHandle prev_halfedge_handle(SmartHalfedgeHandle _heh) const
returns the face handle of the opposite halfedge
Handle for a halfedge entity.
Definition: Handles.hh:127
void split_edge(EdgeHandle _eh, VertexHandle _vh)
FaceHandle remove_edge(EdgeHandle _eh)
VertexVertexIter vv_iter(VertexHandle _vh)
vertex - vertex circulator
FaceIter faces_begin()
Begin iterator for faces.
FaceHalfedgeIter fh_iter(FaceHandle _fh)
face - halfedge circulator
ConstVertexOHalfedgeIter cvoh_iter(VertexHandle _vh) const
const vertex - outgoing halfedge circulator
VertexFaceIter vf_iter(VertexHandle _vh)
vertex - face circulator
void collapse_loop(HalfedgeHandle _hh)
Helper for halfedge collapse.
FaceIter faces_end()
End iterator for faces.
VertexIHalfedgeIter vih_iter(VertexHandle _vh)
vertex - incoming halfedge circulator
bool is_simple_link(EdgeHandle _eh) const
Handle for a vertex entity.
Definition: Handles.hh:120
ConstVertexVertexIter cvv_iter(VertexHandle _vh) const
const vertex circulator
const StatusInfo & status(VertexHandle _vh) const
Status Query API.
Definition: ArrayKernel.hh:501
SmartEdgeHandle edge_handle(SmartHalfedgeHandle _heh) const
returns the face handle of the opposite halfedge
bool is_valid() const
The handle is valid iff the index is not negative.
Definition: Handles.hh:72
void split(FaceHandle _fh, VertexHandle _vh)
Face split (= 1-to-n split).
void delete_edge(EdgeHandle _eh, bool _delete_isolated_vertices=true)
HalfedgeHandle insert_edge(HalfedgeHandle _prev_heh, HalfedgeHandle _next_heh)
SmartHalfedgeHandle opposite_halfedge_handle(SmartHalfedgeHandle _heh) const
returns the face handle of the opposite halfedge
SmartHalfedgeHandle find_halfedge(VertexHandle _start_vh, VertexHandle _end_vh) const
Find halfedge from _vh0 to _vh1. Returns invalid handle if not found.
SmartVertexHandle make_smart(VertexHandle _vh, const PolyConnectivity *_mesh)
Creats a SmartVertexHandle from a VertexHandle and a Mesh.
void delete_face(FaceHandle _fh, bool _delete_isolated_vertices=true)
void reinsert_edge(EdgeHandle _eh)
ConstFaceFaceIter cff_iter(FaceHandle _fh) const
const face - face circulator
SmartFaceHandle opposite_face_handle(HalfedgeHandle _heh) const
returns the face handle of the opposite halfedge
void set_deleted(bool _b)
set deleted
Definition: Status.hh:105
bool is_boundary(HalfedgeHandle _heh) const
Check if the halfedge is at the boundary.
void triangulate()
triangulate the entire mesh
bool is_collapse_ok(HalfedgeHandle _he)
FaceHalfedgeIter fh_end(FaceHandle _fh)
face - halfedge circulator
static const VertexHandle InvalidVertexHandle
Invalid handle.
void collapse_edge(HalfedgeHandle _hh)
Helper for halfedge collapse.
SmartFaceHandle face_handle(SmartHalfedgeHandle _heh) const
returns the face handle of the opposite halfedge
uint valence(VertexHandle _vh) const
Vertex valence.
void split_edge_copy(EdgeHandle _eh, VertexHandle _vh)
SmartFaceHandle add_face(const std::vector< VertexHandle > &_vhandles)
Add and connect a new face.
static const HalfedgeHandle InvalidHalfedgeHandle
Invalid handle.
static const EdgeHandle InvalidEdgeHandle
Invalid handle.
bool deleted() const
is deleted ?
Definition: Status.hh:103
void copy_all_properties(VertexHandle _vh_from, VertexHandle _vh_to, bool _copyBuildIn=false)
Definition: BaseKernel.hh:511
static const FaceHandle InvalidFaceHandle
Invalid handle.
ConstFaceEdgeIter cfe_iter(FaceHandle _fh) const
const face - edge circulator
ConstFaceVertexIter cfv_iter(FaceHandle _fh) const
const face - vertex circulator
bool is_simply_connected(FaceHandle _fh) const
SmartHalfedgeHandle halfedge_handle(SmartEdgeHandle _eh, unsigned int _i) const
returns the face handle of the opposite halfedge