Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
MeshSelectionT.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 
51 
52 
53 //=============================================================================
54 //
55 // IMPLEMENTATION
56 //
57 //=============================================================================
58 
59 #define MESHSELECTION_C
60 
61 //== INCLUDES =================================================================
62 
63 #include "MeshSelectionT.hh"
64 #include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
65 
66 #include <stack>
67 #include <set>
68 //== NAMESPACES ===============================================================
69 
70 namespace MeshSelection {
71 
72 //== IMPLEMENTATION ==========================================================
73 
74 //=========================================================
75 //== Vertex Selection =====================================
76 //=========================================================
77 
78 template< typename MeshT >
79 inline
80 void selectVertices(MeshT* _mesh, const std::vector< int >& _vertices) {
81  const int n_vertices = (int)_mesh->n_vertices();
82 
83  for ( uint i = 0 ; i < _vertices.size() ; ++i )
84  if ( (_vertices[i] >= 0) && ( _vertices[i] < n_vertices ) ) {
85  typename MeshT::VertexHandle vh(_vertices[i]);
86  _mesh->status(vh).set_selected(true);
87  }
88 }
89 
90 //=========================================================
91 
92 template< typename MeshT >
93 inline
94 void unselectVertices(MeshT* _mesh, const std::vector< int >& _vertices) {
95  const int n_vertices = (int)_mesh->n_vertices();
96 
97  for ( uint i = 0 ; i < _vertices.size() ; ++i )
98  if ( (_vertices[i] >= 0) && ( _vertices[i] < n_vertices ) ) {
99  typename MeshT::VertexHandle vh(_vertices[i]);
100  _mesh->status(vh).set_selected(false);
101  }
102 }
103 
104 //=========================================================
105 
106 template< typename MeshT >
107 inline
108 void selectAllVertices(MeshT* _mesh) {
109  typename MeshT::VertexIter v_it, v_end=_mesh->vertices_end();
110 
111  for (v_it = _mesh->vertices_begin(); v_it != v_end ; ++v_it)
112  _mesh->status(*v_it).set_selected(true);
113 
114 }
115 
116 //=========================================================
117 
118 template< typename MeshT >
119 inline
120 void clearVertexSelection(MeshT* _mesh) {
121  typename MeshT::VertexIter v_it, v_end=_mesh->vertices_end();
122 
123  for (v_it = _mesh->vertices_begin(); v_it != v_end ; ++v_it)
124  _mesh->status(*v_it).set_selected(false);
125 }
126 
127 //=========================================================
128 
129 template< typename MeshT >
130 inline
131 void invertVertexSelection(MeshT* _mesh) {
132  typename MeshT::VertexIter v_it, v_end=_mesh->vertices_end();
133 
134  for (v_it = _mesh->vertices_begin(); v_it != v_end ; ++v_it)
135  _mesh->status(*v_it).set_selected( ! _mesh->status(*v_it).selected());
136 }
137 
138 //=========================================================
139 
140 
141 template< typename MeshT >
142 inline
143 void selectBoundaryVertices(MeshT* _mesh) {
144  typename MeshT::HalfedgeIter he_it, he_end=_mesh->halfedges_end();
145 
146  for (he_it = _mesh->halfedges_begin(); he_it != he_end ; ++he_it)
147  if (_mesh->is_boundary(*he_it) ) {
148  _mesh->status(_mesh->to_vertex_handle(*he_it)).set_selected(true);
149  _mesh->status(_mesh->from_vertex_handle(*he_it)).set_selected(true);
150  }
151 }
152 
153 //-----------------------------------------------------------------------------
154 
155 template< typename MeshT >
156 inline
157 void shrinkVertexSelection(MeshT* _mesh) {
158  OpenMesh::VPropHandleT< bool > temp_shrink;
159 
160  _mesh->add_property( temp_shrink, "Temp property for Vertex selection shrinking" );
161 
162  typename MeshT::VertexIter v_it, v_end=_mesh->vertices_end();
163 
164  // initialize property ( copy status to new property )
165  for (v_it = _mesh->vertices_begin(); v_it != v_end ; ++v_it)
166  _mesh->property(temp_shrink,*v_it) = _mesh->status(*v_it).selected();
167 
168  // update selection
169  for (v_it = _mesh->vertices_begin(); v_it != v_end ; ++v_it)
170  if ( _mesh->property(temp_shrink,*v_it) ) {
171  _mesh->status(*v_it).set_selected( true );
172 
173  for ( typename MeshT::VertexVertexIter vv_it(*_mesh,*v_it); vv_it.is_valid(); ++vv_it)
174  if ( ! _mesh->property(temp_shrink,*vv_it) ){
175  _mesh->status(*v_it).set_selected( false );
176  break;
177  }
178  }
179 
180  _mesh->remove_property(temp_shrink);
181 }
182 
183 //=========================================================
184 
185 template< typename MeshT >
186 inline
187 void growVertexSelection(MeshT* _mesh) {
189 
190  _mesh->add_property( temp_grow, "Temp property for Vertex selection growing" );
191 
192  // initialize property ( copy status to new property )
193  typename MeshT::VertexIter v_it, v_end=_mesh->vertices_end();
194  for (v_it = _mesh->vertices_begin(); v_it != v_end ; ++v_it)
195  _mesh->property(temp_grow,*v_it) = _mesh->status(*v_it).selected();
196 
197  // update selection
198  for (v_it = _mesh->vertices_begin(); v_it != v_end ; ++v_it)
199  if ( _mesh->property(temp_grow,*v_it) )
200  for ( typename MeshT::VertexVertexIter vv_it(*_mesh,*v_it); vv_it.is_valid(); ++vv_it)
201  _mesh->status(*vv_it).set_selected( true );
202 
203  _mesh->remove_property(temp_grow);
204 }
205 
206 //=========================================================
207 
208 template< typename MeshT >
209 inline
210 std::vector< int > getVertexSelection(MeshT* _mesh) {
211  std::vector< int > selection;
212 
213  for ( typename MeshT::VertexIter v_it= _mesh->vertices_begin() ; v_it != _mesh->vertices_end() ; ++v_it )
214  if ( _mesh->status(*v_it).selected() )
215  selection.push_back( v_it->idx() );
216 
217  return selection;
218 }
219 
220 //=========================================================
221 
222 template< typename MeshT >
223 inline
224 std::vector< int > getVertexSelection(MeshT* _mesh, bool& _invert) {
225  std::vector< int > selection;
226 
227  int count = 0;
228 
229  for ( typename MeshT::VertexIter v_it= _mesh->vertices_begin() ; v_it != _mesh->vertices_end() ; ++v_it )
230  if ( _mesh->status(*v_it).selected() )
231  ++count;
232 
233  if ( count > (int)( _mesh->n_vertices() / 2) )
234  _invert = true;
235  else
236  _invert = false;
237 
238  for ( typename MeshT::VertexIter v_it= _mesh->vertices_begin() ; v_it != _mesh->vertices_end() ; ++v_it )
239  if ( _mesh->status(*v_it).selected() ^ _invert )
240  selection.push_back( v_it->idx() );
241 
242  return selection;
243 }
244 
245 
246 template< typename MeshT >
247 inline
248 void selectBoundaryVertices(MeshT* _mesh, const typename MeshT::VertexHandle& _vh){
249 
251  _mesh->add_property(visited, "Visited Vertices");
252 
253  typename MeshT::VertexIter v_it, v_end = _mesh->vertices_end();
254  for (v_it = _mesh->vertices_begin(); v_it != v_end; ++v_it)
255  _mesh->property(visited, *v_it) = false;
256 
257  std::stack< typename MeshT::VertexHandle > stack;
258  stack.push( _vh );
259 
260  while (!stack.empty()){
261 
262  typename MeshT::VertexHandle vh = stack.top();
263  stack.pop();
264 
265  if (_mesh->property(visited,vh))
266  continue;
267 
268  //find outgoing boundary-edges
269  for (typename MeshT::VertexOHalfedgeIter voh_it(*_mesh,vh); voh_it.is_valid(); ++voh_it)
270  if ( _mesh->is_boundary( _mesh->edge_handle( *voh_it ) ) )
271  stack.push( _mesh->to_vertex_handle(*voh_it) );
272 
273  //select vertex
274  _mesh->property(visited,vh) = true;
275  _mesh->status( vh ).set_selected(true);
276  }
277  _mesh->remove_property(visited);
278 }
279 
280 template< typename MeshT >
281 inline
282 void convertVertexToEdgeSelection(MeshT* _mesh, const std::vector< int >& _vertices) {
283 
284  for ( std::vector<int>::const_iterator v = _vertices.begin(); v != _vertices.end(); ++v) {
285 
286  typename MeshT::VertexHandle vh(*v);
287  typename MeshT::VertexOHalfedgeIter ohe_iter = _mesh->voh_iter(vh);
288 
289  for (; ohe_iter.is_valid(); ++ohe_iter) {
290  // test if both incident vertices are in _vertices
291  typename MeshT::VertexHandle ovh = _mesh->to_vertex_handle(*ohe_iter);
292  // search for ovh in _vertices
293  for(std::vector<int>::const_iterator it = _vertices.begin(); it != _vertices.end(); ++it) {
294  if((*it) == ovh.idx()) {
295  _mesh->status(_mesh->edge_handle(*ohe_iter)).set_selected(true);
296  break;
297  }
298  }
299  }
300  }
301 }
302 
303 template< typename MeshT >
304 inline
305 void convertVertexToEdgeSelection(MeshT* _mesh) {
306 
307  typename MeshT::VertexIter v_it, v_end = _mesh->vertices_end();
308  for (v_it = _mesh->vertices_begin(); v_it != v_end; ++v_it) {
309 
310  if ( _mesh->status( *v_it ).selected() ) {
311  typename MeshT::VertexOHalfedgeIter ohe_iter = _mesh->voh_iter(*v_it);
312 
313  for (; ohe_iter.is_valid(); ++ohe_iter) {
314  // test if both incident vertices are in _vertices
315  typename MeshT::VertexHandle ovh = _mesh->to_vertex_handle(*ohe_iter);
316  if (_mesh->status(ovh).selected())
317  _mesh->status(_mesh->edge_handle(*ohe_iter)).set_selected(true);
318  }
319  }
320  }
321 }
322 
323 template< typename MeshT >
324 inline
325 void convertVertexToHalfedgeSelection(MeshT* _mesh, const std::vector< int >& _vertices) {
326 
327  for (std::vector<int>::const_iterator v = _vertices.begin(); v != _vertices.end(); ++v) {
328 
329  typename MeshT::VertexHandle vh(*v);
330  typename MeshT::VertexOHalfedgeIter ohe_iter = _mesh->voh_iter(vh);
331 
332  for (; ohe_iter.is_valid(); ++ohe_iter) {
333  // test if both incident vertices are in _vertices
334  typename MeshT::VertexHandle ovh = _mesh->to_vertex_handle(*ohe_iter);
335  // search for ovh in _vertices
336  for(std::vector<int>::const_iterator it = _vertices.begin(); it != _vertices.end(); ++it) {
337  if((*it) == ovh.idx()) {
338  _mesh->status(*ohe_iter).set_selected(true);
339  _mesh->status(_mesh->opposite_halfedge_handle(*ohe_iter)).set_selected(true);
340  break;
341  }
342  }
343  }
344  }
345 }
346 
347 template< typename MeshT >
348 inline
349 void convertVertexToHalfedgeSelection(MeshT* _mesh) {
350 
351  typename MeshT::VertexIter v_it, v_end = _mesh->vertices_end();
352 
353  for (v_it = _mesh->vertices_begin(); v_it != v_end; ++v_it) {
354 
355  if ( _mesh->status( *v_it ).selected() ) {
356 
357  typename MeshT::VertexOHalfedgeIter ohe_iter = _mesh->voh_iter(*v_it);
358 
359  for (; ohe_iter.is_valid(); ++ohe_iter) {
360  // test if both incident vertices are in _vertices
361  typename MeshT::VertexHandle ovh = _mesh->to_vertex_handle(*ohe_iter);
362  if (_mesh->status(ovh).selected()) {
363  _mesh->status(*ohe_iter).set_selected(true);
364  _mesh->status(_mesh->opposite_halfedge_handle(*ohe_iter)).set_selected(true);
365  }
366  }
367  }
368  }
369 }
370 
371 template< typename MeshT >
372 inline
373 void convertVertexToFaceSelection(MeshT* _mesh, const std::vector< int >& _vertices) {
374 
375  for(typename MeshT::FaceIter f_it = _mesh->faces_begin(); f_it != _mesh->faces_end(); ++f_it) {
376  typename MeshT::FaceVertexIter fv_it = _mesh->fv_iter(*f_it);
377  // go over each vertex of each face and test if it's selected
378  bool allfound = true;
379  for(; fv_it.is_valid(); ++fv_it) {
380  // search fv_it in _vertices
381  bool onefound = false;
382  for(std::vector<int>::const_iterator it = _vertices.begin(); it != _vertices.end(); ++it) {
383  if((*it) == fv_it->idx()) { onefound = true; break; }
384  }
385  if(!onefound) {
386  allfound = false;
387  break;
388  }
389  }
390  if(allfound) {
391  // all incident vertices are selected -> select face
392  _mesh->status(*f_it).set_selected(true);
393  }
394  }
395 }
396 
397 template< typename MeshT >
398 inline
399 void convertVertexToFaceSelection(MeshT* _mesh) {
400 
401  typename MeshT::FaceIter f_it, f_end = _mesh->faces_end();
402 
403  for (f_it = _mesh->faces_begin(); f_it != f_end; ++f_it) {
404 
405  typename MeshT::FaceVertexIter fv_it = _mesh->fv_iter(*f_it);
406  // test if all incident vertices are selected
407  bool allfound = true;
408  for(; fv_it.is_valid(); ++fv_it) {
409  if(!_mesh->status(*fv_it).selected()) {
410  allfound = false;
411  break;
412  }
413  }
414  if(allfound)
415  _mesh->status(*f_it).set_selected(true);
416  }
417 }
418 
419 template< typename MeshT >
420 inline
421 void convertVertexSelectionToFeatureVertices(MeshT* _mesh) {
422 
423  for (typename MeshT::VertexIter v_it = _mesh->vertices_begin(); v_it != _mesh->vertices_end(); ++v_it) {
424 
425  if (_mesh->status(*v_it).selected()) {
426 
427  _mesh->status(*v_it).set_feature(true);
428  } else {
429  _mesh->status(*v_it).set_feature(false);
430  }
431  }
432 }
433 
434 template< typename MeshT >
435 inline
436 void convertFeatureVerticesToVertexSelection(MeshT* _mesh) {
437 
438  for (typename MeshT::VertexIter v_it = _mesh->vertices_begin(); v_it != _mesh->vertices_end(); ++v_it) {
439 
440  if (_mesh->status(*v_it).feature()) {
441 
442  _mesh->status(*v_it).set_selected(true);
443  } else {
444  _mesh->status(*v_it).set_selected(false);
445  }
446  }
447 }
448 
449 template< typename MeshT >
450 inline
451 void clearFeatureVertices(MeshT* _mesh) {
452 
453  for (typename MeshT::VertexIter v_it = _mesh->vertices_begin(); v_it != _mesh->vertices_end(); ++v_it) {
454 
455  _mesh->status(*v_it).set_feature(false);
456  }
457 }
458 
459 //=========================================================
460 //== Modeling Regions =====================================
461 //=========================================================
462 
463 template< typename MeshT >
464 inline
465 void setArea(MeshT* _mesh, const std::vector< int >& _vertices , unsigned int _type, bool _state) {
466  for ( uint i = 0 ; i < _vertices.size() ; ++i ) {
467  if ( _vertices[i] > (int)_mesh->n_vertices() )
468  continue;
469 
470  typename MeshT::VertexHandle vh(_vertices[i]);
471  _mesh->status(vh).change_bit(_type, _state);
472  }
473 }
474 
475 template< typename MeshT >
476 inline
477 void setArea(MeshT* _mesh , unsigned int _type, bool _state) {
478  for ( typename MeshT::VertexIter v_it= _mesh->vertices_begin() ; v_it != _mesh->vertices_end() ; ++v_it )
479  _mesh->status(*v_it).change_bit(_type, _state);
480 }
481 
482 template< typename MeshT >
483 inline
484 std::vector< int > getArea(MeshT* _mesh, unsigned int _type) {
485  std::vector< int > selection;
486 
487  for ( typename MeshT::VertexIter v_it= _mesh->vertices_begin() ; v_it != _mesh->vertices_end() ; ++v_it )
488  if ( _mesh->status(*v_it).is_bit_set( _type ) )
489  selection.push_back( v_it->idx() );
490 
491  return selection;
492 }
493 
494 template< typename MeshT >
495 inline
496 std::vector< int > getArea(MeshT* _mesh, unsigned int _type , bool& _invert) {
497  std::vector< int > selection;
498 
499  int count = 0;
500 
501  for ( typename MeshT::VertexIter v_it= _mesh->vertices_begin() ; v_it != _mesh->vertices_end() ; ++v_it )
502  if ( _mesh->status(*v_it).is_bit_set( _type ) )
503  ++count;
504 
505  if ( count > (int)( _mesh->n_vertices() / 2) )
506  _invert = true;
507  else
508  _invert = false;
509 
510  for ( typename MeshT::VertexIter v_it= _mesh->vertices_begin() ; v_it != _mesh->vertices_end() ; ++v_it )
511  if ( _mesh->status(*v_it).is_bit_set( _type ) ^ _invert )
512  selection.push_back( v_it->idx() );
513 
514  return selection;
515 }
516 
517 
518 //=========================================================
519 //== Edge Selection =====================================
520 //=========================================================
521 
522 template< typename MeshT >
523 inline
524 void selectEdges(MeshT* _mesh, const std::vector< int >& _edges) {
525  const int n_edges = (int)_mesh->n_edges();
526 
527  for ( uint i = 0 ; i < _edges.size() ; ++i )
528  if ( (_edges[i] >= 0) && ( _edges[i] < n_edges ) ) {
529  typename MeshT::EdgeHandle eh(_edges[i]);
530  _mesh->status(eh).set_selected(true);
531  }
532 }
533 
534 //=========================================================
535 
536 template< typename MeshT >
537 inline
538 void unselectEdges(MeshT* _mesh, const std::vector< int >& _edges) {
539  const int n_edges = (int)_mesh->n_edges();
540 
541  for ( uint i = 0 ; i < _edges.size() ; ++i )
542  if ( (_edges[i] >= 0) && ( _edges[i] < n_edges ) ) {
543  typename MeshT::EdgeHandle eh(_edges[i]);
544  _mesh->status(eh).set_selected(false);
545  }
546 }
547 
548 //=========================================================
549 
550 template< typename MeshT >
551 inline
552 void selectAllEdges(MeshT* _mesh) {
553  typename MeshT::EdgeIter e_it, e_end=_mesh->edges_end();
554 
555  for (e_it = _mesh->edges_begin(); e_it != e_end ; ++e_it)
556  _mesh->status(*e_it).set_selected(true);
557 }
558 
559 //=========================================================
560 
561 template< typename MeshT >
562 inline
563 void clearEdgeSelection(MeshT* _mesh) {
564  typename MeshT::EdgeIter e_it, e_end=_mesh->edges_end();
565 
566  for (e_it = _mesh->edges_begin(); e_it != e_end ; ++e_it)
567  _mesh->status(*e_it).set_selected(false);
568 }
569 
570 //=========================================================
571 
572 template< typename MeshT >
573 inline
574 void invertEdgeSelection(MeshT* _mesh) {
575  typename MeshT::EdgeIter e_it, e_end=_mesh->edges_end();
576 
577  for (e_it = _mesh->edges_begin(); e_it != e_end ; ++e_it)
578  _mesh->status(*e_it).set_selected( ! _mesh->status(*e_it).selected());
579 }
580 
581 //=========================================================
582 
583 template<typename MeshT>
584 inline
585 void growEdgeSelection(MeshT* _mesh) {
586  std::set<typename MeshT::EdgeHandle> selectedEhs;
587  for (typename MeshT::EdgeIter e_it = _mesh->edges_begin(), e_end = _mesh->edges_end();
588  e_it != e_end; ++e_it) {
589 
590  if (!_mesh->status(*e_it).selected()) continue;
591 
592  const typename MeshT::HalfedgeHandle he = _mesh->halfedge_handle(*e_it, 0);
593  const typename MeshT::VertexHandle vhs[] = { _mesh->from_vertex_handle(he),
594  _mesh->to_vertex_handle(he) };
595 
596  for (int i = 0; i < 2; ++i) {
597  for (typename MeshT::VertexEdgeIter ve_it = _mesh->ve_begin(vhs[i]), ve_end = _mesh->ve_end(vhs[i]);
598  ve_it != ve_end; ++ve_it) {
599 
600  selectedEhs.insert(*ve_it);
601  }
602  }
603 
604  }
605 
606  for (typename std::set<typename MeshT::EdgeHandle>::const_iterator it = selectedEhs.begin(); it != selectedEhs.end(); ++it)
607  _mesh->status(*it).set_selected(true);
608 }
609 
610 //=========================================================
611 
612 
613 template< typename MeshT >
614 inline
615 void selectBoundaryEdges(MeshT* _mesh) {
616  typename MeshT::EdgeIter e_it, e_end=_mesh->edges_end();
617 
618  for (e_it = _mesh->edges_begin(); e_it != e_end ; ++e_it)
619  if ( _mesh->is_boundary( _mesh->halfedge_handle(*e_it,0) ) ||
620  _mesh->is_boundary( _mesh->halfedge_handle(*e_it,1) ) )
621  _mesh->status(*e_it).set_selected( true );
622 }
623 
624 //=========================================================
625 
626 template< typename MeshT >
627 inline
628 std::vector< int > getEdgeSelection(MeshT* _mesh) {
629  std::vector< int > selection;
630 
631  for ( typename MeshT::EdgeIter e_it= _mesh->edges_begin() ; e_it != _mesh->edges_end() ; ++e_it )
632  if ( _mesh->status(*e_it).selected() )
633  selection.push_back( e_it->idx() );
634 
635  return selection;
636 }
637 
638 //=========================================================
639 
640 template< typename MeshT >
641 inline
642 std::vector< int > getEdgeSelection(MeshT* _mesh, bool& _invert) {
643  std::vector< int > selection;
644 
645  int count = 0;
646 
647  for ( typename MeshT::VertexIter e_it= _mesh->edges_begin() ; e_it != _mesh->edges_end() ; ++e_it )
648  if ( _mesh->status(*e_it).selected() )
649  ++count;
650 
651  if ( count > (int)( _mesh->n_vertices() / 2) )
652  _invert = true;
653  else
654  _invert = false;
655 
656  for ( typename MeshT::VertexIter e_it= _mesh->edges_begin() ; e_it != _mesh->edges_end() ; ++e_it )
657  if ( _mesh->status(*e_it).selected() ^ _invert )
658  selection.push_back( e_it->idx() );
659 
660  return selection;
661 }
662 
663 template< typename MeshT >
664 inline
665 void convertEdgeToVertexSelection(MeshT* _mesh, const std::vector< int >& _edges) {
666 
667  for (std::vector<int>::const_iterator e = _edges.begin(); e != _edges.end(); ++e) {
668 
669  typename MeshT::EdgeHandle eh(*e);
670  typename MeshT::HalfedgeHandle heh0 = _mesh->halfedge_handle(eh, 0);
671 
672  typename MeshT::VertexHandle vh0 = _mesh->to_vertex_handle(heh0);
673  typename MeshT::VertexHandle vh1 = _mesh->from_vertex_handle(heh0);
674 
675  _mesh->status(vh0).set_selected(true);
676  _mesh->status(vh1).set_selected(true);
677  }
678 }
679 
680 template< typename MeshT >
681 inline
682 void convertEdgeToVertexSelection(MeshT* _mesh) {
683 
684  for ( typename MeshT::EdgeIter e_it= _mesh->edges_begin() ; e_it != _mesh->edges_end() ; ++e_it )
685 
686  if ( _mesh->status(*e_it).selected() ){
687 
688  typename MeshT::HalfedgeHandle heh0 = _mesh->halfedge_handle(*e_it, 0);
689 
690  typename MeshT::VertexHandle vh0 = _mesh->to_vertex_handle(heh0);
691  typename MeshT::VertexHandle vh1 = _mesh->from_vertex_handle(heh0);
692 
693  _mesh->status(vh0).set_selected(true);
694  _mesh->status(vh1).set_selected(true);
695  }
696 }
697 
698 template< typename MeshT >
699 inline
700 void convertEdgeToFaceSelection(MeshT* _mesh, const std::vector< int >& _edges) {
701 
702  for(typename MeshT::FaceIter f_it = _mesh->faces_begin(); f_it != _mesh->faces_end(); ++f_it) {
703  typename MeshT::FaceEdgeIter fe_it = _mesh->fe_iter(*f_it);
704  // go over each edge of each face and test if it's selected
705  bool allfound = true;
706  for(; fe_it.is_valid(); ++fe_it) {
707  // search fe_it in _edges
708  bool onefound = false;
709  for(std::vector<int>::const_iterator it = _edges.begin(); it != _edges.end(); ++it) {
710  if((*it) == fe_it->idx()) { onefound = true; break; }
711  }
712  if(!onefound) {
713  allfound = false;
714  break;
715  }
716  }
717  if(allfound) {
718  // all incident vertices are selected -> select face
719  _mesh->status(*f_it).set_selected(true);
720  }
721  }
722 }
723 
724 template< typename MeshT >
725 inline
726 void convertEdgeToFaceSelection(MeshT* _mesh) {
727 
728  typename MeshT::FaceIter f_it, f_end = _mesh->faces_end();
729 
730  for (f_it = _mesh->faces_begin(); f_it != f_end; ++f_it) {
731 
732  typename MeshT::FaceEdgeIter fe_it = _mesh->fe_iter(*f_it);
733  // test if all incident edges are selected
734  bool allfound = true;
735  for(; fe_it.is_valid(); ++fe_it) {
736  if(!_mesh->status(*fe_it).selected()) {
737  allfound = false;
738  break;
739  }
740  }
741  if(allfound)
742  _mesh->status(*f_it).set_selected(true);
743  }
744 }
745 
746 template< typename MeshT >
747 inline
748 void convertEdgeToHalfedgeSelection(MeshT* _mesh) {
749 
750  for ( typename MeshT::EdgeIter e_it= _mesh->edges_begin() ; e_it != _mesh->edges_end() ; ++e_it )
751 
752  if ( _mesh->status(*e_it).selected() ){
753 
754  _mesh->status(_mesh->halfedge_handle(*e_it, 0)).set_selected(true);
755  _mesh->status(_mesh->halfedge_handle(*e_it, 1)).set_selected(true);
756  }
757 }
758 
759 template< typename MeshT >
760 inline
761 void convertEdgeSelectionToFeatureEdges(MeshT* _mesh) {
762 
763  for (typename MeshT::EdgeIter e_it = _mesh->edges_begin(); e_it != _mesh->edges_end(); ++e_it) {
764 
765  if (_mesh->status(*e_it).selected()) {
766 
767  _mesh->status(*e_it).set_feature(true);
768  } else {
769  _mesh->status(*e_it).set_feature(false);
770  }
771  }
772 }
773 
774 template< typename MeshT >
775 inline
776 void convertFeatureEdgesToEdgeSelection(MeshT* _mesh) {
777 
778  for (typename MeshT::EdgeIter e_it = _mesh->edges_begin(); e_it != _mesh->edges_end(); ++e_it) {
779 
780  if (_mesh->status(*e_it).feature()) {
781 
782  _mesh->status(*e_it).set_selected(true);
783  } else {
784  _mesh->status(*e_it).set_selected(false);
785  }
786  }
787 }
788 
789 template< typename MeshT >
790 inline
791 void clearFeatureEdges(MeshT* _mesh) {
792 
793  for (typename MeshT::EdgeIter e_it = _mesh->edges_begin(); e_it != _mesh->edges_end(); ++e_it) {
794 
795  _mesh->status(*e_it).set_feature(false);
796  }
797 }
798 
799 //=========================================================
800 //== Halfedge Selection =====================================
801 //=========================================================
802 
803 template< typename MeshT >
804 inline
805 void selectHalfedges(MeshT* _mesh, const std::vector< int >& _halfedges) {
806  const int n_halfedges = (int)_mesh->n_halfedges();
807 
808  for ( uint i = 0 ; i < _halfedges.size() ; ++i )
809  if ( (_halfedges[i] >= 0) && ( _halfedges[i] < n_halfedges ) ) {
810  typename MeshT::HalfedgeHandle heh(_halfedges[i]);
811  _mesh->status(heh).set_selected(true);
812  }
813 }
814 
815 //=========================================================
816 
817 template< typename MeshT >
818 inline
819 void unselectHalfedges(MeshT* _mesh, const std::vector< int >& _halfedges) {
820  const int n_halfedges = (int)_mesh->n_halfedges();
821 
822  for ( uint i = 0 ; i < _halfedges.size() ; ++i )
823  if ( (_halfedges[i] >= 0) && ( _halfedges[i] < n_halfedges ) ) {
824  typename MeshT::HalfedgeHandle heh(_halfedges[i]);
825  _mesh->status(heh).set_selected(false);
826  }
827 }
828 
829 //=========================================================
830 
831 template< typename MeshT >
832 inline
833 void selectAllHalfedges(MeshT* _mesh) {
834  typename MeshT::HalfedgeIter he_it, he_end=_mesh->halfedges_end();
835 
836  for (he_it = _mesh->halfedges_begin(); he_it != he_end ; ++he_it)
837  _mesh->status(*he_it).set_selected(true);
838 }
839 
840 //=========================================================
841 
842 template< typename MeshT >
843 inline
844 void clearHalfedgeSelection(MeshT* _mesh) {
845  typename MeshT::HalfedgeIter he_it, he_end=_mesh->halfedges_end();
846 
847  for (he_it = _mesh->halfedges_begin(); he_it != he_end ; ++he_it)
848  _mesh->status(*he_it).set_selected(false);
849 }
850 
851 //=========================================================
852 
853 template< typename MeshT >
854 inline
855 void invertHalfedgeSelection(MeshT* _mesh) {
856  typename MeshT::HalfedgeIter he_it, he_end=_mesh->halfedges_end();
857 
858  for (he_it = _mesh->halfedges_begin(); he_it != he_end ; ++he_it)
859  _mesh->status(*he_it).set_selected( ! _mesh->status(*he_it).selected());
860 }
861 
862 //=========================================================
863 
864 
865 template< typename MeshT >
866 inline
867 void selectBoundaryHalfedges(MeshT* _mesh) {
868  typename MeshT::HalfedgeIter he_it, he_end=_mesh->halfedges_end();
869 
870  for (he_it = _mesh->halfedges_begin(); he_it != he_end ; ++he_it)
871  if ( _mesh->is_boundary( *he_it))
872  _mesh->status(*he_it).set_selected( true );
873 }
874 
875 //=========================================================
876 
877 template< typename MeshT >
878 inline
879 std::vector< int > getHalfedgeSelection(MeshT* _mesh) {
880  std::vector< int > selection;
881 
882  for ( typename MeshT::HalfedgeIter he_it= _mesh->halfedges_begin() ; he_it != _mesh->halfedges_end() ; ++he_it )
883  if ( _mesh->status(*he_it).selected() )
884  selection.push_back( he_it->idx() );
885 
886  return selection;
887 }
888 
889 template< typename MeshT >
890 inline
891 void convertHalfedgeToVertexSelection(MeshT* _mesh) {
892 
893  for ( typename MeshT::HalfedgeIter he_it= _mesh->halfedges_begin() ; he_it != _mesh->halfedges_end() ; ++he_it ) {
894 
895  if(_mesh->status(*he_it).selected()) {
896  _mesh->status(_mesh->to_vertex_handle(*he_it)).set_selected(true);
897  _mesh->status(_mesh->from_vertex_handle(*he_it)).set_selected(true);
898  }
899  }
900 }
901 
902 template< typename MeshT >
903 inline
904 void convertHalfedgeToEdgeSelection(MeshT* _mesh) {
905 
906  for ( typename MeshT::HalfedgeIter he_it= _mesh->halfedges_begin() ; he_it != _mesh->halfedges_end() ; ++he_it ) {
907 
908  if(_mesh->status(*he_it).selected()) {
909  _mesh->status(_mesh->edge_handle(*he_it)).set_selected(true);
910  }
911  }
912 }
913 
914 template< typename MeshT >
915 inline
916 void convertHalfedgeToFaceSelection(MeshT* _mesh) {
917  // Note: A face is not only selected
918  // iff all incident halfedges are selected but
919  // at least one of them. This is, however,
920  // desired in some cases.
921  for ( typename MeshT::HalfedgeIter he_it= _mesh->halfedges_begin() ; he_it != _mesh->halfedges_end() ; ++he_it ) {
922 
923  if(_mesh->status(*he_it).selected()) {
924  _mesh->status(_mesh->face_handle(*he_it)).set_selected(true);
925  }
926  }
927 }
928 
929 //=========================================================
930 //== Face Selection =======================================
931 //=========================================================
932 
933 template< typename MeshT >
934 inline
935 void selectFaces(MeshT* _mesh, const std::vector< int >& _faces) {
936  const int n_faces = (int)_mesh->n_faces();
937 
938  for ( uint i = 0 ; i < _faces.size() ; ++i )
939  if ( (_faces[i] >= 0) && ( _faces[i] < n_faces ) ) {
940  typename MeshT::FaceHandle fh(_faces[i]);
941  _mesh->status(fh).set_selected(true);
942  }
943 }
944 
945 //=========================================================
946 
947 template< typename MeshT >
948 inline
949 void unselectFaces(MeshT* _mesh, const std::vector< int >& _faces) {
950  const int n_faces = (int)_mesh->n_faces();
951 
952  for ( uint i = 0 ; i < _faces.size() ; ++i )
953  if ( (_faces[i] >= 0) && ( _faces[i] < n_faces ) ) {
954  typename MeshT::FaceHandle fh(_faces[i]);
955  _mesh->status(fh).set_selected(false);
956  }
957 }
958 
959 //=========================================================
960 
961 template< typename MeshT >
962 inline
963 void selectAllFaces(MeshT* _mesh) {
964  typename MeshT::FaceIter f_it, f_end=_mesh->faces_end();
965 
966  for (f_it = _mesh->faces_begin(); f_it != f_end ; ++f_it)
967  _mesh->status(*f_it).set_selected(true);
968 }
969 
970 //=========================================================
971 
972 
973 template< typename MeshT >
974 inline
975 void clearFaceSelection(MeshT* _mesh) {
976  typename MeshT::FaceIter f_it, f_end=_mesh->faces_end();
977 
978  for (f_it = _mesh->faces_begin(); f_it != f_end ; ++f_it)
979  _mesh->status(*f_it).set_selected(false);
980 }
981 
982 //-----------------------------------------------------------------------------
983 
984 
985 template< typename MeshT >
986 inline
987 void invertFaceSelection(MeshT* _mesh) {
988  typename MeshT::FaceIter f_it, f_end=_mesh->faces_end();
989 
990  for (f_it = _mesh->faces_begin(); f_it != f_end ; ++f_it)
991  _mesh->status(*f_it).set_selected( ! _mesh->status(*f_it).selected());
992 }
993 
994 //=========================================================
995 
996 template< typename MeshT >
997 inline
998 void selectBoundaryFaces(MeshT* _mesh) {
999  typename MeshT::HalfedgeIter he_it, he_end=_mesh->halfedges_end();
1000 
1001  for (he_it = _mesh->halfedges_begin(); he_it != he_end ; ++he_it)
1002  if (_mesh->is_boundary(*he_it) ) {
1003  for (typename MeshT::VertexFaceIter vf_it(*_mesh ,_mesh->to_vertex_handle(*he_it) ) ; vf_it.is_valid() ; ++vf_it)
1004  _mesh->status(*vf_it).set_selected(true);
1005  for (typename MeshT::VertexFaceIter vf_it(*_mesh ,_mesh->from_vertex_handle(*he_it) ) ; vf_it.is_valid() ; ++vf_it)
1006  _mesh->status(*vf_it).set_selected(true);
1007  }
1008 }
1009 
1010 //=========================================================
1011 
1012 
1013 template< typename MeshT >
1014 inline
1015 void shrinkFaceSelection(MeshT* _mesh) {
1016  OpenMesh::FPropHandleT< bool > temp_shrink;
1017 
1018  _mesh->add_property( temp_shrink, "Temp property for Face selection shrinking" );
1019 
1020  typename MeshT::FaceIter f_it, f_end=_mesh->faces_end();
1021 
1022  // initialize property ( copy status to new property )
1023  for (f_it = _mesh->faces_begin(); f_it != f_end ; ++f_it)
1024  _mesh->property(temp_shrink,*f_it) = _mesh->status(*f_it).selected();
1025 
1026  // Shrink selection ( deselects all faces which are adjacent to a boundary vertex of the original selection)
1027  for (f_it = _mesh->faces_begin(); f_it != f_end ; ++f_it)
1028  if ( _mesh->property(temp_shrink,*f_it) ) {
1029  bool boundary = false;
1030  for ( typename MeshT::FaceVertexIter fv_it(*_mesh,*f_it); fv_it.is_valid() ; ++fv_it) {
1031  for ( typename MeshT::VertexFaceIter vf_it(*_mesh,*fv_it); vf_it.is_valid() ; ++vf_it) {
1032  if ( ! _mesh->property(temp_shrink,*vf_it) ) {
1033  boundary = true;
1034  }
1035  }
1036  if ( boundary )
1037  break;
1038  }
1039 
1040  _mesh->status(*f_it).set_selected( !boundary );
1041  }
1042 
1043  _mesh->remove_property(temp_shrink);
1044 }
1045 
1046 //=========================================================
1047 
1048 template< typename MeshT >
1049 inline
1050 void growFaceSelection(MeshT* _mesh) {
1052 
1053  _mesh->add_property( temp_grow, "Temp property for Face selection growing" );
1054 
1055  typename MeshT::FaceIter f_it, f_end=_mesh->faces_end();
1056 
1057  // initialize property ( copy status to new property )
1058  for (f_it = _mesh->faces_begin(); f_it != f_end ; ++f_it)
1059  _mesh->property(temp_grow,*f_it) = _mesh->status(*f_it).selected();
1060 
1061  // Grow selection ( selects all faces which are adjacent to a vertex of a already selected face)
1062  for (f_it = _mesh->faces_begin(); f_it != f_end ; ++f_it)
1063  if ( _mesh->property(temp_grow,*f_it) )
1064  for ( typename MeshT::FaceVertexIter fv_it(*_mesh,*f_it); fv_it.is_valid() ; ++fv_it)
1065  for ( typename MeshT::VertexFaceIter vf_it(*_mesh,*fv_it); vf_it.is_valid() ; ++vf_it)
1066  _mesh->status(*vf_it).set_selected( true );
1067 
1068  _mesh->remove_property(temp_grow);
1069 }
1070 
1071 //=========================================================
1072 
1073 template< typename MeshT >
1074 inline
1075 std::vector< int > getFaceSelection(MeshT* _mesh) {
1076  std::vector< int > selection;
1077 
1078  for ( typename MeshT::FaceIter f_it= _mesh->faces_begin() ; f_it != _mesh->faces_end() ; ++f_it )
1079  if ( _mesh->status(*f_it).selected() )
1080  selection.push_back( f_it->idx() );
1081 
1082  return selection;
1083 }
1084 
1085 //=========================================================
1086 
1087 template< typename MeshT >
1088 inline
1089 std::vector< int > getFaceSelection(MeshT* _mesh, bool& _invert) {
1090  std::vector< int > selection;
1091 
1092  int count = 0;
1093 
1094  for ( typename MeshT::FaceIter f_it= _mesh->faces_begin() ; f_it != _mesh->faces_end() ; ++f_it )
1095  if ( _mesh->status(*f_it).selected() )
1096  ++count;
1097 
1098  if ( count > (int)( _mesh->n_vertices() / 2) )
1099  _invert = true;
1100  else
1101  _invert = false;
1102 
1103  for ( typename MeshT::FaceIter f_it= _mesh->faces_begin() ; f_it != _mesh->faces_end() ; ++f_it )
1104  if ( _mesh->status(*f_it).selected() ^ _invert )
1105  selection.push_back( f_it->idx() );
1106 
1107  return selection;
1108 }
1109 
1110 template< typename MeshT >
1111 inline
1112 void convertFaceToVertexSelection(MeshT* _mesh, const std::vector< int >& _faces) {
1113 
1114  for (std::vector<int>::const_iterator f = _faces.begin(); f != _faces.end(); ++f) {
1115 
1116  typename MeshT::FaceHandle fh(*f);
1117 
1118  typename MeshT::FaceVertexIter v_iter = _mesh->fv_iter(fh);
1119 
1120  for (; v_iter.is_valid(); ++v_iter) {
1121  _mesh->status(*v_iter).set_selected(true);
1122  }
1123  }
1124 }
1125 
1126 template< typename MeshT >
1127 inline
1128 void convertFaceToVertexSelection(MeshT* _mesh) {
1129 
1130  for ( typename MeshT::FaceIter f_it= _mesh->faces_begin() ; f_it != _mesh->faces_end() ; ++f_it )
1131 
1132  if ( _mesh->status(*f_it).selected() ){
1133 
1134  typename MeshT::FaceVertexIter v_iter = _mesh->fv_iter(*f_it);
1135 
1136  for (; v_iter.is_valid(); ++v_iter)
1137  _mesh->status(*v_iter).set_selected(true);
1138  }
1139 }
1140 
1141 template< typename MeshT >
1142 inline
1143 void convertFaceToEdgeSelection(MeshT* _mesh, const std::vector< int >& _faces) {
1144 
1145  for (std::vector<int>::const_iterator f = _faces.begin(); f != _faces.end(); ++f) {
1146 
1147  typename MeshT::FaceHandle fh(*f);
1148 
1149  typename MeshT::FaceEdgeIter e_iter = _mesh->fe_iter(fh);
1150 
1151  for (; e_iter.is_valid(); ++e_iter) {
1152  _mesh->status(*e_iter).set_selected(true);
1153  }
1154  }
1155 }
1156 
1157 template< typename MeshT >
1158 inline
1159 void convertFaceToEdgeSelection(MeshT* _mesh) {
1160 
1161  for ( typename MeshT::FaceIter f_it= _mesh->faces_begin() ; f_it != _mesh->faces_end() ; ++f_it )
1162 
1163  if ( _mesh->status(*f_it).selected() ){
1164 
1165  typename MeshT::FaceEdgeIter e_iter = _mesh->fe_iter(*f_it);
1166 
1167  for (; e_iter.is_valid(); ++e_iter)
1168  _mesh->status(*e_iter).set_selected(true);
1169  }
1170 }
1171 
1172 template< typename MeshT >
1173 inline
1174 void convertFaceToHalfedgeSelection(MeshT* _mesh) {
1175 
1176  for ( typename MeshT::FaceIter f_it= _mesh->faces_begin() ; f_it != _mesh->faces_end() ; ++f_it )
1177 
1178  if ( _mesh->status(*f_it).selected() ){
1179 
1180  typename MeshT::FaceHalfedgeIter fh_iter = _mesh->fh_iter(*f_it);
1181 
1182  for (; fh_iter.is_valid(); ++fh_iter)
1183  _mesh->status(*fh_iter).set_selected(true);
1184  }
1185 }
1186 
1187 template< typename MeshT >
1188 inline
1189 void convertFaceSelectionToFeatureFaces(MeshT* _mesh) {
1190 
1191  for (typename MeshT::FaceIter f_it = _mesh->faces_begin(); f_it != _mesh->faces_end(); ++f_it) {
1192 
1193  if (_mesh->status(*f_it).selected()) {
1194 
1195  _mesh->status(*f_it).set_feature(true);
1196  } else {
1197  _mesh->status(*f_it).set_feature(false);
1198  }
1199  }
1200 }
1201 
1202 template< typename MeshT >
1203 inline
1204 void convertFeatureFacesToFaceSelection(MeshT* _mesh) {
1205 
1206  for (typename MeshT::FaceIter f_it = _mesh->faces_begin(); f_it != _mesh->faces_end(); ++f_it) {
1207 
1208  if (_mesh->status(*f_it).feature()) {
1209 
1210  _mesh->status(*f_it).set_selected(true);
1211  } else {
1212  _mesh->status(*f_it).set_selected(false);
1213  }
1214  }
1215 }
1216 
1217 template< typename MeshT >
1218 inline
1219 void clearFeatureFaces(MeshT* _mesh) {
1220 
1221  for (typename MeshT::FaceIter f_it = _mesh->faces_begin(); f_it != _mesh->faces_end(); ++f_it) {
1222 
1223  _mesh->status(*f_it).set_feature(false);
1224  }
1225 }
1226 
1227 //=============================================================================
1228 } // MeshSelection Namespace
1229 //=============================================================================
Functions for selection on a mesh.