MeshSelectionT.cc 37.3 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
2
3
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
Jan Möbius's avatar
Jan Möbius committed
33
34
35
\*===========================================================================*/

/*===========================================================================*\
Jan Möbius's avatar
Jan Möbius committed
36
37
38
39
40
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
Jan Möbius's avatar
Jan Möbius committed
41
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66




//=============================================================================
//
//  IMPLEMENTATION
//
//=============================================================================

#define MESHSELECTION_C

//== INCLUDES =================================================================

#include "MeshSelectionT.hh"
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>

#include <stack>
//== NAMESPACES ===============================================================

namespace MeshSelection {

//== IMPLEMENTATION ==========================================================

//=========================================================
67
68
69
//== Vertex Selection =====================================
//=========================================================

Jan Möbius's avatar
 
Jan Möbius committed
70
template< typename MeshT >
71
inline
72
void selectVertices(MeshT* _mesh, const std::vector< int >& _vertices) {
Jan Möbius's avatar
 
Jan Möbius committed
73
  const int n_vertices = (int)_mesh->n_vertices();
74
75

  for ( uint i = 0 ; i < _vertices.size() ; ++i )
76
    if ( (_vertices[i] >= 0) && ( _vertices[i] < n_vertices ) )  {
Jan Möbius's avatar
 
Jan Möbius committed
77
78
79
80
      typename MeshT::VertexHandle vh(_vertices[i]);
      _mesh->status(vh).set_selected(true);
    }
}
81

Jan Möbius's avatar
 
Jan Möbius committed
82
//=========================================================
83

Jan Möbius's avatar
 
Jan Möbius committed
84
template< typename MeshT >
85
inline
86
void unselectVertices(MeshT* _mesh, const std::vector< int >& _vertices) {
Jan Möbius's avatar
 
Jan Möbius committed
87
  const int n_vertices = (int)_mesh->n_vertices();
88
89

  for ( uint i = 0 ; i < _vertices.size() ; ++i )
90
    if ( (_vertices[i] >= 0) && ( _vertices[i] < n_vertices ) )  {
Jan Möbius's avatar
 
Jan Möbius committed
91
92
93
94
95
96
      typename MeshT::VertexHandle vh(_vertices[i]);
      _mesh->status(vh).set_selected(false);
    }
}

//=========================================================
97

Jan Möbius's avatar
 
Jan Möbius committed
98
template< typename MeshT >
99
inline
Jan Möbius's avatar
 
Jan Möbius committed
100
101
void selectAllVertices(MeshT* _mesh) {
   typename MeshT::VertexIter v_it, v_end=_mesh->vertices_end();
102
103

   for (v_it = _mesh->vertices_begin(); v_it != v_end ; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
104
      _mesh->status(*v_it).set_selected(true);
105
106

}
Jan Möbius's avatar
 
Jan Möbius committed
107
108

//=========================================================
109

Jan Möbius's avatar
 
Jan Möbius committed
110
111
112
113
template< typename MeshT >
inline
void clearVertexSelection(MeshT* _mesh) {
   typename MeshT::VertexIter v_it, v_end=_mesh->vertices_end();
114
115

   for (v_it = _mesh->vertices_begin(); v_it != v_end ; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
116
      _mesh->status(*v_it).set_selected(false);
Jan Möbius's avatar
 
Jan Möbius committed
117
118
119
120
121
}

//=========================================================

template< typename MeshT >
122
inline
Jan Möbius's avatar
 
Jan Möbius committed
123
124
void invertVertexSelection(MeshT* _mesh) {
   typename MeshT::VertexIter v_it, v_end=_mesh->vertices_end();
125
126

   for (v_it = _mesh->vertices_begin(); v_it != v_end ; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
127
      _mesh->status(*v_it).set_selected( ! _mesh->status(*v_it).selected());
Jan Möbius's avatar
 
Jan Möbius committed
128
129
130
131
}

//=========================================================

132

Jan Möbius's avatar
 
Jan Möbius committed
133
134
135
136
template< typename MeshT >
inline
void selectBoundaryVertices(MeshT* _mesh) {
   typename MeshT::HalfedgeIter he_it, he_end=_mesh->halfedges_end();
137
138

   for (he_it = _mesh->halfedges_begin(); he_it != he_end ; ++he_it)
Jan Möbius's avatar
Jan Möbius committed
139
140
141
      if (_mesh->is_boundary(*he_it) ) {
         _mesh->status(_mesh->to_vertex_handle(*he_it)).set_selected(true);
         _mesh->status(_mesh->from_vertex_handle(*he_it)).set_selected(true);
Jan Möbius's avatar
 
Jan Möbius committed
142
143
144
145
146
147
      }
}

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

template< typename MeshT >
148
inline
Jan Möbius's avatar
 
Jan Möbius committed
149
150
void shrinkVertexSelection(MeshT* _mesh) {
   OpenMesh::VPropHandleT< bool > temp_shrink;
151

Jan Möbius's avatar
 
Jan Möbius committed
152
   _mesh->add_property( temp_shrink, "Temp property for Vertex selection shrinking" );
153

Jan Möbius's avatar
 
Jan Möbius committed
154
   typename MeshT::VertexIter v_it, v_end=_mesh->vertices_end();
155

Jan Möbius's avatar
 
Jan Möbius committed
156
   // initialize property ( copy status to new property )
157
   for (v_it = _mesh->vertices_begin(); v_it != v_end ; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
158
      _mesh->property(temp_shrink,*v_it) = _mesh->status(*v_it).selected();
159

Jan Möbius's avatar
 
Jan Möbius committed
160
   // update selection
161
   for (v_it = _mesh->vertices_begin(); v_it != v_end ; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
162
163
      if ( _mesh->property(temp_shrink,*v_it) ) {
         _mesh->status(*v_it).set_selected( true );
164

Jan Möbius's avatar
Jan Möbius committed
165
         for ( typename MeshT::VertexVertexIter vv_it(*_mesh,*v_it); vv_it.is_valid(); ++vv_it)
Jan Möbius's avatar
Jan Möbius committed
166
167
            if ( ! _mesh->property(temp_shrink,*vv_it) ){
                _mesh->status(*v_it).set_selected( false );
Jan Möbius's avatar
 
Jan Möbius committed
168
169
170
                break;
            }
      }
171

Jan Möbius's avatar
 
Jan Möbius committed
172
173
174
   _mesh->remove_property(temp_shrink);
}

175
//=========================================================
Jan Möbius's avatar
 
Jan Möbius committed
176
177

template< typename MeshT >
178
inline
Jan Möbius's avatar
 
Jan Möbius committed
179
180
void growVertexSelection(MeshT* _mesh) {
   OpenMesh::VPropHandleT< bool > temp_grow;
181

Jan Möbius's avatar
 
Jan Möbius committed
182
   _mesh->add_property( temp_grow, "Temp property for Vertex selection growing" );
183

Jan Möbius's avatar
 
Jan Möbius committed
184
185
   // initialize property ( copy status to new property )
   typename MeshT::VertexIter v_it, v_end=_mesh->vertices_end();
186
   for (v_it = _mesh->vertices_begin(); v_it != v_end ; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
187
      _mesh->property(temp_grow,*v_it) = _mesh->status(*v_it).selected();
188

Jan Möbius's avatar
 
Jan Möbius committed
189
   // update selection
190
   for (v_it = _mesh->vertices_begin(); v_it != v_end ; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
191
192
193
      if ( _mesh->property(temp_grow,*v_it) )
         for ( typename MeshT::VertexVertexIter vv_it(*_mesh,*v_it); vv_it.is_valid(); ++vv_it)
            _mesh->status(*vv_it).set_selected( true );
194

Jan Möbius's avatar
 
Jan Möbius committed
195
196
197
   _mesh->remove_property(temp_grow);
}

198
//=========================================================
Jan Möbius's avatar
 
Jan Möbius committed
199
200

template< typename MeshT >
201
inline
Jan Möbius's avatar
 
Jan Möbius committed
202
203
std::vector< int > getVertexSelection(MeshT* _mesh) {
  std::vector< int > selection;
204

Jan Möbius's avatar
 
Jan Möbius committed
205
  for ( typename MeshT::VertexIter v_it= _mesh->vertices_begin() ; v_it != _mesh->vertices_end() ; ++v_it )
Jan Möbius's avatar
Jan Möbius committed
206
207
    if ( _mesh->status(*v_it).selected() )
      selection.push_back( v_it->idx() );
208

Jan Möbius's avatar
 
Jan Möbius committed
209
210
211
  return selection;
}

212
//=========================================================
Jan Möbius's avatar
 
Jan Möbius committed
213
214

template< typename MeshT >
215
inline
Jan Möbius's avatar
 
Jan Möbius committed
216
217
std::vector< int > getVertexSelection(MeshT* _mesh, bool& _invert) {
  std::vector< int > selection;
218

Jan Möbius's avatar
 
Jan Möbius committed
219
  int count = 0;
220

Jan Möbius's avatar
 
Jan Möbius committed
221
  for ( typename MeshT::VertexIter v_it= _mesh->vertices_begin() ; v_it != _mesh->vertices_end() ; ++v_it )
Jan Möbius's avatar
Jan Möbius committed
222
    if ( _mesh->status(*v_it).selected() )
Jan Möbius's avatar
 
Jan Möbius committed
223
      ++count;
224

Jan Möbius's avatar
 
Jan Möbius committed
225
226
  if ( count > (int)( _mesh->n_vertices() / 2) )
    _invert = true;
227
  else
Jan Möbius's avatar
 
Jan Möbius committed
228
    _invert = false;
229

Jan Möbius's avatar
 
Jan Möbius committed
230
  for ( typename MeshT::VertexIter v_it= _mesh->vertices_begin() ; v_it != _mesh->vertices_end() ; ++v_it )
Jan Möbius's avatar
Jan Möbius committed
231
232
    if ( _mesh->status(*v_it).selected() ^ _invert )
      selection.push_back( v_it->idx() );
233

Jan Möbius's avatar
 
Jan Möbius committed
234
235
236
237
238
  return selection;
}


template< typename MeshT >
239
inline
240
void selectBoundaryVertices(MeshT* _mesh, const typename MeshT::VertexHandle& _vh){
241

Jan Möbius's avatar
 
Jan Möbius committed
242
243
244
245
246
  OpenMesh::VPropHandleT< bool > visited;
  _mesh->add_property(visited, "Visited Vertices");

  typename MeshT::VertexIter v_it, v_end = _mesh->vertices_end();
  for (v_it = _mesh->vertices_begin(); v_it != v_end; ++v_it)
Jan Möbius's avatar
Jan Möbius committed
247
    _mesh->property(visited, *v_it) = false;
Jan Möbius's avatar
 
Jan Möbius committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261

  std::stack< typename MeshT::VertexHandle > stack;
  stack.push( _vh );

  while (!stack.empty()){

    typename MeshT::VertexHandle vh = stack.top();
    stack.pop();

    if (_mesh->property(visited,vh))
      continue;

    //find outgoing boundary-edges
    for (typename MeshT::VertexOHalfedgeIter voh_it(*_mesh,vh); voh_it; ++voh_it)
Jan Möbius's avatar
Jan Möbius committed
262
263
      if ( _mesh->is_boundary( _mesh->edge_handle( *voh_it ) ) )
        stack.push( _mesh->to_vertex_handle(*voh_it) );
Jan Möbius's avatar
 
Jan Möbius committed
264
265
266
267
268
269
270
271

    //select vertex
    _mesh->property(visited,vh) = true;
    _mesh->status( vh ).set_selected(true);
  }
  _mesh->remove_property(visited);
}

272
273
template< typename MeshT >
inline
274
void convertVertexToEdgeSelection(MeshT* _mesh, const std::vector< int >& _vertices) {
275

276
  for ( std::vector<int>::const_iterator v = _vertices.begin(); v != _vertices.end(); ++v) {
277

278
    typename MeshT::VertexHandle vh(*v);
279
280
    typename MeshT::VertexOHalfedgeIter ohe_iter = _mesh->voh_iter(vh);

Jan Möbius's avatar
Jan Möbius committed
281
    for (; ohe_iter.is_valid(); ++ohe_iter) {
282
      // test if both incident vertices are in _vertices
Jan Möbius's avatar
Jan Möbius committed
283
      typename MeshT::VertexHandle ovh = _mesh->to_vertex_handle(*ohe_iter);
284
      // search for ovh in _vertices
285
      for(std::vector<int>::const_iterator it = _vertices.begin(); it != _vertices.end(); ++it) {
286
        if((*it) == ovh.idx()) {
Jan Möbius's avatar
Jan Möbius committed
287
          _mesh->status(_mesh->edge_handle(*ohe_iter)).set_selected(true);
288
289
290
          break;
        }
      }
291
292
293
294
295
296
297
298
299
    }
  }
}

template< typename MeshT >
inline
void convertVertexToEdgeSelection(MeshT* _mesh) {

  typename MeshT::VertexIter v_it, v_end = _mesh->vertices_end();
300
  for (v_it = _mesh->vertices_begin(); v_it != v_end; ++v_it) {
301

Jan Möbius's avatar
Jan Möbius committed
302
    if ( _mesh->status( *v_it ).selected() ) {
Jan Möbius's avatar
Jan Möbius committed
303
      typename MeshT::VertexOHalfedgeIter ohe_iter = _mesh->voh_iter(*v_it);
304

Jan Möbius's avatar
Jan Möbius committed
305
      for (; ohe_iter.is_valid(); ++ohe_iter) {
306
        // test if both incident vertices are in _vertices
Jan Möbius's avatar
Jan Möbius committed
307
        typename MeshT::VertexHandle ovh = _mesh->to_vertex_handle(*ohe_iter);
308
        if (_mesh->status(ovh).selected())
Jan Möbius's avatar
Jan Möbius committed
309
          _mesh->status(_mesh->edge_handle(*ohe_iter)).set_selected(true);
310
      }
311
    }
312
  }
313
314
}

315
316
template< typename MeshT >
inline
317
void convertVertexToHalfedgeSelection(MeshT* _mesh, const std::vector< int >& _vertices) {
318

319
  for (std::vector<int>::const_iterator v = _vertices.begin(); v != _vertices.end(); ++v) {
320
321

    typename MeshT::VertexHandle vh(*v);
322
323
    typename MeshT::VertexOHalfedgeIter ohe_iter = _mesh->voh_iter(vh);

Jan Möbius's avatar
Jan Möbius committed
324
    for (; ohe_iter.is_valid(); ++ohe_iter) {
325
      // test if both incident vertices are in _vertices
Jan Möbius's avatar
Jan Möbius committed
326
      typename MeshT::VertexHandle ovh = _mesh->to_vertex_handle(*ohe_iter);
327
      // search for ovh in _vertices
328
      for(std::vector<int>::const_iterator it = _vertices.begin(); it != _vertices.end(); ++it) {
329
        if((*it) == ovh.idx()) {
Jan Möbius's avatar
Jan Möbius committed
330
331
          _mesh->status(*ohe_iter).set_selected(true);
          _mesh->status(_mesh->opposite_halfedge_handle(*ohe_iter)).set_selected(true);
332
333
334
          break;
        }
      }
335
336
337
338
339
340
341
342
343
344
    }
  }
}

template< typename MeshT >
inline
void convertVertexToHalfedgeSelection(MeshT* _mesh) {

  typename MeshT::VertexIter v_it, v_end = _mesh->vertices_end();
  
345
  for (v_it = _mesh->vertices_begin(); v_it != v_end; ++v_it) {
346

Jan Möbius's avatar
Jan Möbius committed
347
    if ( _mesh->status( *v_it ).selected() ) {
348

Jan Möbius's avatar
Jan Möbius committed
349
      typename MeshT::VertexOHalfedgeIter ohe_iter = _mesh->voh_iter(*v_it);
350

Jan Möbius's avatar
Jan Möbius committed
351
      for (; ohe_iter.is_valid(); ++ohe_iter) {
352
        // test if both incident vertices are in _vertices
Jan Möbius's avatar
Jan Möbius committed
353
        typename MeshT::VertexHandle ovh = _mesh->to_vertex_handle(*ohe_iter);
354
        if (_mesh->status(ovh).selected()) {
Jan Möbius's avatar
Jan Möbius committed
355
356
          _mesh->status(*ohe_iter).set_selected(true);
          _mesh->status(_mesh->opposite_halfedge_handle(*ohe_iter)).set_selected(true);
357
        }
358
359
      }
    }
360
  }
361
362
}

363
364
template< typename MeshT >
inline
365
void convertVertexToFaceSelection(MeshT* _mesh, const std::vector< int >& _vertices) {
366

367
  for(typename MeshT::FaceIter f_it = _mesh->faces_begin(); f_it != _mesh->faces_end(); ++f_it) {
Jan Möbius's avatar
Jan Möbius committed
368
    typename MeshT::FaceVertexIter fv_it = _mesh->fv_iter(*f_it);
369
370
    // go over each vertex of each face and test if it's selected
    bool allfound = true;
Jan Möbius's avatar
Jan Möbius committed
371
    for(; fv_it.is_valid(); ++fv_it) {
372
373
      // search fv_it in _vertices
      bool onefound = false;
374
      for(std::vector<int>::const_iterator it = _vertices.begin(); it != _vertices.end(); ++it) {
Jan Möbius's avatar
Jan Möbius committed
375
        if((*it) == fv_it->idx()) { onefound = true; break; }
376
377
378
379
380
381
382
383
      }
      if(!onefound) {
        allfound = false;
        break;
      }
    }
    if(allfound) {
      // all incident vertices are selected -> select face
Jan Möbius's avatar
Jan Möbius committed
384
      _mesh->status(*f_it).set_selected(true);
385
386
    }
  }
387
388
}

389
390
391
392
template< typename MeshT >
inline
void convertVertexToFaceSelection(MeshT* _mesh) {

393
  typename MeshT::FaceIter f_it, f_end = _mesh->faces_end();
394
  
395
396
  for (f_it = _mesh->faces_begin(); f_it != f_end; ++f_it) {

Jan Möbius's avatar
Jan Möbius committed
397
    typename MeshT::FaceVertexIter fv_it = _mesh->fv_iter(*f_it);
398
399
    // test if all incident vertices are selected
    bool allfound = true;
Jan Möbius's avatar
Jan Möbius committed
400
401
    for(; fv_it.is_valid(); ++fv_it) {
      if(!_mesh->status(*fv_it).selected()) {
402
403
404
        allfound = false;
        break;
      }
405
    }
406
    if(allfound)
Jan Möbius's avatar
Jan Möbius committed
407
      _mesh->status(*f_it).set_selected(true);
408
  }
409
410
}

411
412
413
414
415
416
template< typename MeshT >
inline
void convertVertexSelectionToFeatureVertices(MeshT* _mesh) {

    for (typename MeshT::VertexIter v_it = _mesh->vertices_begin(); v_it != _mesh->vertices_end(); ++v_it) {

Jan Möbius's avatar
Jan Möbius committed
417
        if (_mesh->status(*v_it).selected()) {
418

Jan Möbius's avatar
Jan Möbius committed
419
            _mesh->status(*v_it).set_feature(true);
420
        } else {
Jan Möbius's avatar
Jan Möbius committed
421
            _mesh->status(*v_it).set_feature(false);
422
423
424
425
426
427
428
429
430
431
        }
    }
}

template< typename MeshT >
inline
void convertFeatureVerticesToVertexSelection(MeshT* _mesh) {

    for (typename MeshT::VertexIter v_it = _mesh->vertices_begin(); v_it != _mesh->vertices_end(); ++v_it) {

Jan Möbius's avatar
Jan Möbius committed
432
        if (_mesh->status(*v_it).feature()) {
433

Jan Möbius's avatar
Jan Möbius committed
434
            _mesh->status(*v_it).set_selected(true);
435
        } else {
Jan Möbius's avatar
Jan Möbius committed
436
            _mesh->status(*v_it).set_selected(false);
437
438
439
440
441
442
443
444
445
446
        }
    }
}

template< typename MeshT >
inline
void clearFeatureVertices(MeshT* _mesh) {

    for (typename MeshT::VertexIter v_it = _mesh->vertices_begin(); v_it != _mesh->vertices_end(); ++v_it) {

Jan Möbius's avatar
Jan Möbius committed
447
        _mesh->status(*v_it).set_feature(false);
448
449
450
    }
}

451
452
//=========================================================
//== Modeling Regions =====================================
Jan Möbius's avatar
 
Jan Möbius committed
453
454
455
//=========================================================

template< typename MeshT >
456
inline
457
void setArea(MeshT* _mesh, const std::vector< int >& _vertices , unsigned int _type, bool _state) {
Jan Möbius's avatar
 
Jan Möbius committed
458
459
  for ( uint i = 0 ; i < _vertices.size() ; ++i ) {
    if ( _vertices[i] > (int)_mesh->n_vertices() )
460
461
      continue;

Jan Möbius's avatar
 
Jan Möbius committed
462
463
464
465
466
467
    typename MeshT::VertexHandle vh(_vertices[i]);
    _mesh->status(vh).change_bit(_type, _state);
  }
}

template< typename MeshT >
468
inline
Jan Möbius's avatar
 
Jan Möbius committed
469
void setArea(MeshT* _mesh , unsigned int _type, bool _state) {
470
  for ( typename MeshT::VertexIter v_it= _mesh->vertices_begin() ; v_it != _mesh->vertices_end() ; ++v_it )
Jan Möbius's avatar
Jan Möbius committed
471
    _mesh->status(*v_it).change_bit(_type,  _state);
Jan Möbius's avatar
 
Jan Möbius committed
472
473
474
}

template< typename MeshT >
475
inline
Jan Möbius's avatar
 
Jan Möbius committed
476
477
std::vector< int > getArea(MeshT* _mesh, unsigned int _type) {
  std::vector< int > selection;
478

Jan Möbius's avatar
 
Jan Möbius committed
479
  for ( typename MeshT::VertexIter v_it= _mesh->vertices_begin() ; v_it != _mesh->vertices_end() ; ++v_it )
Jan Möbius's avatar
Jan Möbius committed
480
481
    if ( _mesh->status(*v_it).is_bit_set( _type ) )
      selection.push_back( v_it->idx() );
482
483

  return selection;
Jan Möbius's avatar
 
Jan Möbius committed
484
485
486
}

template< typename MeshT >
487
inline
Jan Möbius's avatar
 
Jan Möbius committed
488
489
std::vector< int > getArea(MeshT* _mesh, unsigned int _type , bool& _invert) {
  std::vector< int > selection;
490

Jan Möbius's avatar
 
Jan Möbius committed
491
  int count = 0;
492

Jan Möbius's avatar
 
Jan Möbius committed
493
  for ( typename MeshT::VertexIter v_it= _mesh->vertices_begin() ; v_it != _mesh->vertices_end() ; ++v_it )
Jan Möbius's avatar
Jan Möbius committed
494
    if ( _mesh->status(*v_it).is_bit_set( _type ) )
Jan Möbius's avatar
 
Jan Möbius committed
495
      ++count;
496

Jan Möbius's avatar
 
Jan Möbius committed
497
498
  if ( count > (int)( _mesh->n_vertices() / 2) )
    _invert = true;
499
  else
Jan Möbius's avatar
 
Jan Möbius committed
500
    _invert = false;
501

Jan Möbius's avatar
 
Jan Möbius committed
502
  for ( typename MeshT::VertexIter v_it= _mesh->vertices_begin() ; v_it != _mesh->vertices_end() ; ++v_it )
Jan Möbius's avatar
Jan Möbius committed
503
504
    if ( _mesh->status(*v_it).is_bit_set( _type ) ^ _invert )
      selection.push_back( v_it->idx() );
505

Jan Möbius's avatar
 
Jan Möbius committed
506
507
508
509
510
  return selection;
}


//=========================================================
511
512
513
//== Edge Selection =====================================
//=========================================================

Jan Möbius's avatar
 
Jan Möbius committed
514
template< typename MeshT >
515
inline
516
void selectEdges(MeshT* _mesh, const std::vector< int >& _edges) {
Jan Möbius's avatar
 
Jan Möbius committed
517
  const int n_edges = (int)_mesh->n_edges();
518
519

  for ( uint i = 0 ; i < _edges.size() ; ++i )
520
    if ( (_edges[i] >= 0) && ( _edges[i] < n_edges ) )  {
Jan Möbius's avatar
 
Jan Möbius committed
521
522
523
524
      typename MeshT::EdgeHandle eh(_edges[i]);
      _mesh->status(eh).set_selected(true);
    }
}
525

Jan Möbius's avatar
 
Jan Möbius committed
526
//=========================================================
527

Jan Möbius's avatar
 
Jan Möbius committed
528
template< typename MeshT >
529
inline
530
void unselectEdges(MeshT* _mesh, const std::vector< int >& _edges) {
Jan Möbius's avatar
 
Jan Möbius committed
531
  const int n_edges = (int)_mesh->n_edges();
532
533

  for ( uint i = 0 ; i < _edges.size() ; ++i )
534
    if ( (_edges[i] >= 0) && ( _edges[i] < n_edges ) )  {
Jan Möbius's avatar
 
Jan Möbius committed
535
536
537
      typename MeshT::EdgeHandle eh(_edges[i]);
      _mesh->status(eh).set_selected(false);
    }
538
539
}

Jan Möbius's avatar
 
Jan Möbius committed
540
//=========================================================
541

Jan Möbius's avatar
 
Jan Möbius committed
542
template< typename MeshT >
543
inline
Jan Möbius's avatar
 
Jan Möbius committed
544
545
void selectAllEdges(MeshT* _mesh) {
  typename MeshT::EdgeIter e_it, e_end=_mesh->edges_end();
546
547

  for (e_it = _mesh->edges_begin(); e_it != e_end ; ++e_it)
Jan Möbius's avatar
Jan Möbius committed
548
  _mesh->status(*e_it).set_selected(true);
549
}
Jan Möbius's avatar
 
Jan Möbius committed
550
551
552
553

//=========================================================

template< typename MeshT >
554
inline
Jan Möbius's avatar
 
Jan Möbius committed
555
556
557
void clearEdgeSelection(MeshT* _mesh) {
  typename MeshT::EdgeIter e_it, e_end=_mesh->edges_end();

558
  for (e_it = _mesh->edges_begin(); e_it != e_end ; ++e_it)
Jan Möbius's avatar
Jan Möbius committed
559
    _mesh->status(*e_it).set_selected(false);
560
}
Jan Möbius's avatar
 
Jan Möbius committed
561
562
563
564

//=========================================================

template< typename MeshT >
565
inline
Jan Möbius's avatar
 
Jan Möbius committed
566
567
568
void invertEdgeSelection(MeshT* _mesh) {
  typename MeshT::EdgeIter e_it, e_end=_mesh->edges_end();

569
  for (e_it = _mesh->edges_begin(); e_it != e_end ; ++e_it)
Jan Möbius's avatar
Jan Möbius committed
570
    _mesh->status(*e_it).set_selected( ! _mesh->status(*e_it).selected());
571
}
Jan Möbius's avatar
 
Jan Möbius committed
572
573
574

//=========================================================

575

Jan Möbius's avatar
 
Jan Möbius committed
576
577
578
579
580
template< typename MeshT >
inline
void selectBoundaryEdges(MeshT* _mesh) {
  typename MeshT::EdgeIter e_it, e_end=_mesh->edges_end();

581
  for (e_it = _mesh->edges_begin(); e_it != e_end ; ++e_it)
Jan Möbius's avatar
Jan Möbius committed
582
583
584
    if ( _mesh->is_boundary( _mesh->halfedge_handle(*e_it,0) ) ||
         _mesh->is_boundary( _mesh->halfedge_handle(*e_it,1) ) )
      _mesh->status(*e_it).set_selected( true );
Jan Möbius's avatar
 
Jan Möbius committed
585
586
}

587
//=========================================================
Jan Möbius's avatar
 
Jan Möbius committed
588
589

template< typename MeshT >
590
inline
Jan Möbius's avatar
 
Jan Möbius committed
591
592
std::vector< int > getEdgeSelection(MeshT* _mesh) {
  std::vector< int > selection;
593

Jan Möbius's avatar
 
Jan Möbius committed
594
  for ( typename MeshT::EdgeIter e_it= _mesh->edges_begin() ; e_it != _mesh->edges_end() ; ++e_it )
Jan Möbius's avatar
Jan Möbius committed
595
596
    if ( _mesh->status(*e_it).selected() )
      selection.push_back( e_it->idx() );
597

Jan Möbius's avatar
 
Jan Möbius committed
598
599
600
  return selection;
}

601
//=========================================================
Jan Möbius's avatar
 
Jan Möbius committed
602
603

template< typename MeshT >
604
inline
Jan Möbius's avatar
 
Jan Möbius committed
605
606
std::vector< int > getEdgeSelection(MeshT* _mesh, bool& _invert) {
  std::vector< int > selection;
607

Jan Möbius's avatar
 
Jan Möbius committed
608
  int count = 0;
609

Jan Möbius's avatar
 
Jan Möbius committed
610
  for ( typename MeshT::VertexIter e_it= _mesh->edges_begin() ; e_it != _mesh->edges_end() ; ++e_it )
Jan Möbius's avatar
Jan Möbius committed
611
    if ( _mesh->status(*e_it).selected() )
Jan Möbius's avatar
 
Jan Möbius committed
612
      ++count;
613

Jan Möbius's avatar
 
Jan Möbius committed
614
615
  if ( count > (int)( _mesh->n_vertices() / 2) )
    _invert = true;
616
  else
Jan Möbius's avatar
 
Jan Möbius committed
617
    _invert = false;
618

Jan Möbius's avatar
 
Jan Möbius committed
619
  for ( typename MeshT::VertexIter e_it= _mesh->edges_begin() ; e_it != _mesh->edges_end() ; ++e_it )
Jan Möbius's avatar
Jan Möbius committed
620
621
    if ( _mesh->status(*e_it).selected() ^ _invert )
      selection.push_back( e_it->idx() );
622

Jan Möbius's avatar
 
Jan Möbius committed
623
624
625
  return selection;
}

626
627
template< typename MeshT >
inline
628
void convertEdgeToVertexSelection(MeshT* _mesh, const std::vector< int >& _edges) {
629

630
	for (std::vector<int>::const_iterator e = _edges.begin(); e != _edges.end(); ++e) {
631
632
633
634
635
636
637
638
639
640
641
642

		typename MeshT::EdgeHandle eh(*e);
		typename MeshT::HalfedgeHandle heh0 = _mesh->halfedge_handle(eh, 0);

		typename MeshT::VertexHandle vh0 = _mesh->to_vertex_handle(heh0);
		typename MeshT::VertexHandle vh1 = _mesh->from_vertex_handle(heh0);

		_mesh->status(vh0).set_selected(true);
		_mesh->status(vh1).set_selected(true);
	}
}

643
644
645
646
647
648
template< typename MeshT >
inline
void convertEdgeToVertexSelection(MeshT* _mesh) {

  for ( typename MeshT::EdgeIter e_it= _mesh->edges_begin() ; e_it != _mesh->edges_end() ; ++e_it )
    
Jan Möbius's avatar
Jan Möbius committed
649
    if ( _mesh->status(*e_it).selected() ){
650

Jan Möbius's avatar
Jan Möbius committed
651
      typename MeshT::HalfedgeHandle heh0 = _mesh->halfedge_handle(*e_it, 0);
652
653
654
655
656
657
658
659
660

      typename MeshT::VertexHandle vh0 = _mesh->to_vertex_handle(heh0);
      typename MeshT::VertexHandle vh1 = _mesh->from_vertex_handle(heh0);

      _mesh->status(vh0).set_selected(true);
      _mesh->status(vh1).set_selected(true);
    }
}

661
662
template< typename MeshT >
inline
663
void convertEdgeToFaceSelection(MeshT* _mesh, const std::vector< int >& _edges) {
664

665
  for(typename MeshT::FaceIter f_it = _mesh->faces_begin(); f_it != _mesh->faces_end(); ++f_it) {
Jan Möbius's avatar
Jan Möbius committed
666
    typename MeshT::FaceEdgeIter fe_it = _mesh->fe_iter(*f_it);
667
668
    // go over each edge of each face and test if it's selected
    bool allfound = true;
Jan Möbius's avatar
Jan Möbius committed
669
    for(; fe_it.is_valid(); ++fe_it) {
670
671
      // search fe_it in _edges
      bool onefound = false;
672
      for(std::vector<int>::const_iterator it = _edges.begin(); it != _edges.end(); ++it) {
Jan Möbius's avatar
Jan Möbius committed
673
        if((*it) == fe_it->idx()) { onefound = true; break; }
674
675
676
677
678
679
680
681
      }
      if(!onefound) {
        allfound = false;
        break;
      }
    }
    if(allfound) {
      // all incident vertices are selected -> select face
Jan Möbius's avatar
Jan Möbius committed
682
      _mesh->status(*f_it).set_selected(true);
683
684
    }
  }
685
686
}

687
688
689
690
template< typename MeshT >
inline
void convertEdgeToFaceSelection(MeshT* _mesh) {

691
692
693
694
  typename MeshT::FaceIter f_it, f_end = _mesh->faces_end();
  
  for (f_it = _mesh->faces_begin(); f_it != f_end; ++f_it) {

Jan Möbius's avatar
Jan Möbius committed
695
    typename MeshT::FaceEdgeIter fe_it = _mesh->fe_iter(*f_it);
696
697
    // test if all incident edges are selected
    bool allfound = true;
Jan Möbius's avatar
Jan Möbius committed
698
699
    for(; fe_it.is_valid(); ++fe_it) {
      if(!_mesh->status(*fe_it).selected()) {
700
701
702
        allfound = false;
        break;
      }
703
    }
704
    if(allfound)
Jan Möbius's avatar
Jan Möbius committed
705
      _mesh->status(*f_it).set_selected(true);
706
  }
707
}
Jan Möbius's avatar
 
Jan Möbius committed
708

709
710
711
712
713
714
template< typename MeshT >
inline
void convertEdgeToHalfedgeSelection(MeshT* _mesh) {

  for ( typename MeshT::EdgeIter e_it= _mesh->edges_begin() ; e_it != _mesh->edges_end() ; ++e_it )
    
Jan Möbius's avatar
Jan Möbius committed
715
    if ( _mesh->status(*e_it).selected() ){
716

Jan Möbius's avatar
Jan Möbius committed
717
718
      _mesh->status(_mesh->halfedge_handle(*e_it, 0)).set_selected(true);
      _mesh->status(_mesh->halfedge_handle(*e_it, 1)).set_selected(true);
719
720
721
    }
}

722
723
template< typename MeshT >
inline
724
void convertEdgeSelectionToFeatureEdges(MeshT* _mesh) {
725
726
727

    for (typename MeshT::EdgeIter e_it = _mesh->edges_begin(); e_it != _mesh->edges_end(); ++e_it) {

Jan Möbius's avatar
Jan Möbius committed
728
        if (_mesh->status(*e_it).selected()) {
729

Jan Möbius's avatar
Jan Möbius committed
730
            _mesh->status(*e_it).set_feature(true);
731
        } else {
Jan Möbius's avatar
Jan Möbius committed
732
            _mesh->status(*e_it).set_feature(false);
733
734
735
736
737
738
        }
    }
}

template< typename MeshT >
inline
739
void convertFeatureEdgesToEdgeSelection(MeshT* _mesh) {
740
741

    for (typename MeshT::EdgeIter e_it = _mesh->edges_begin(); e_it != _mesh->edges_end(); ++e_it) {
David Bommes's avatar
David Bommes committed
742

Jan Möbius's avatar
Jan Möbius committed
743
        if (_mesh->status(*e_it).feature()) {
744

Jan Möbius's avatar
Jan Möbius committed
745
            _mesh->status(*e_it).set_selected(true);
746
        } else {
Jan Möbius's avatar
Jan Möbius committed
747
            _mesh->status(*e_it).set_selected(false);
748
749
750
751
752
753
        }
    }
}

template< typename MeshT >
inline
754
void clearFeatureEdges(MeshT* _mesh) {
755
756
757

    for (typename MeshT::EdgeIter e_it = _mesh->edges_begin(); e_it != _mesh->edges_end(); ++e_it) {

Jan Möbius's avatar
Jan Möbius committed
758
        _mesh->status(*e_it).set_feature(false);
759
760
    }
}
David Bommes's avatar
David Bommes committed
761
762
763
764
765
766
767

//=========================================================
//== Halfedge Selection =====================================
//=========================================================

template< typename MeshT >
inline
768
void selectHalfedges(MeshT* _mesh, const std::vector< int >& _halfedges) {
David Bommes's avatar
David Bommes committed
769
770
771
  const int n_halfedges = (int)_mesh->n_halfedges();

  for ( uint i = 0 ; i < _halfedges.size() ; ++i )
772
    if ( (_halfedges[i] >= 0) && ( _halfedges[i] < n_halfedges ) )  {
David Bommes's avatar
David Bommes committed
773
774
775
776
777
778
779
780
781
      typename MeshT::HalfedgeHandle heh(_halfedges[i]);
      _mesh->status(heh).set_selected(true);
    }
}

//=========================================================

template< typename MeshT >
inline
782
void unselectHalfedges(MeshT* _mesh, const std::vector< int >& _halfedges) {
David Bommes's avatar
David Bommes committed
783
784
785
  const int n_halfedges = (int)_mesh->n_halfedges();

  for ( uint i = 0 ; i < _halfedges.size() ; ++i )
786
    if ( (_halfedges[i] >= 0) && ( _halfedges[i] < n_halfedges ) )  {
David Bommes's avatar
David Bommes committed
787
788
789
790
791
792
793
794
795
796
797
798
799
      typename MeshT::HalfedgeHandle heh(_halfedges[i]);
      _mesh->status(heh).set_selected(false);
    }
}

//=========================================================

template< typename MeshT >
inline
void selectAllHalfedges(MeshT* _mesh) {
  typename MeshT::HalfedgeIter he_it, he_end=_mesh->halfedges_end();

  for (he_it = _mesh->halfedges_begin(); he_it != he_end ; ++he_it)
Jan Möbius's avatar
Jan Möbius committed
800
  _mesh->status(*he_it).set_selected(true);
David Bommes's avatar
David Bommes committed
801
802
803
804
805
806
807
808
809
810
}

//=========================================================

template< typename MeshT >
inline
void clearHalfedgeSelection(MeshT* _mesh) {
  typename MeshT::HalfedgeIter he_it, he_end=_mesh->halfedges_end();

  for (he_it = _mesh->halfedges_begin(); he_it != he_end ; ++he_it)
Jan Möbius's avatar
Jan Möbius committed
811
    _mesh->status(*he_it).set_selected(false);
David Bommes's avatar
David Bommes committed
812
813
814
815
816
817
818
819
820
821
}

//=========================================================

template< typename MeshT >
inline
void invertHalfedgeSelection(MeshT* _mesh) {
  typename MeshT::HalfedgeIter he_it, he_end=_mesh->halfedges_end();

  for (he_it = _mesh->halfedges_begin(); he_it != he_end ; ++he_it)
Jan Möbius's avatar
Jan Möbius committed
822
    _mesh->status(*he_it).set_selected( ! _mesh->status(*he_it).selected());
David Bommes's avatar
David Bommes committed
823
824
825
826
827
828
829
830
831
832
833
}

//=========================================================


template< typename MeshT >
inline
void selectBoundaryHalfedges(MeshT* _mesh) {
  typename MeshT::HalfedgeIter he_it, he_end=_mesh->halfedges_end();

  for (he_it = _mesh->halfedges_begin(); he_it != he_end ; ++he_it)
Jan Möbius's avatar
Jan Möbius committed
834
835
    if ( _mesh->is_boundary( *he_it))
      _mesh->status(*he_it).set_selected( true );
David Bommes's avatar
David Bommes committed
836
837
838
839
840
841
842
843
844
845
}

//=========================================================

template< typename MeshT >
inline
std::vector< int > getHalfedgeSelection(MeshT* _mesh) {
  std::vector< int > selection;

  for ( typename MeshT::HalfedgeIter he_it= _mesh->halfedges_begin() ; he_it != _mesh->halfedges_end() ; ++he_it )
Jan Möbius's avatar
Jan Möbius committed
846
847
    if ( _mesh->status(*he_it).selected() )
      selection.push_back( he_it->idx() );
David Bommes's avatar
David Bommes committed
848
849
850
851

  return selection;
}

852
853
854
855
856
857
template< typename MeshT >
inline
void convertHalfedgeToVertexSelection(MeshT* _mesh) {
    
    for ( typename MeshT::HalfedgeIter he_it= _mesh->halfedges_begin() ; he_it != _mesh->halfedges_end() ; ++he_it ) {
        
Jan Möbius's avatar
Jan Möbius committed
858
859
860
        if(_mesh->status(*he_it).selected()) {
            _mesh->status(_mesh->to_vertex_handle(*he_it)).set_selected(true);
            _mesh->status(_mesh->from_vertex_handle(*he_it)).set_selected(true);
861
862
863
864
865
866
867
868
869
870
        }
    }
}

template< typename MeshT >
inline
void convertHalfedgeToEdgeSelection(MeshT* _mesh) {
    
    for ( typename MeshT::HalfedgeIter he_it= _mesh->halfedges_begin() ; he_it != _mesh->halfedges_end() ; ++he_it ) {
        
Jan Möbius's avatar
Jan Möbius committed
871
872
        if(_mesh->status(*he_it).selected()) {
            _mesh->status(_mesh->edge_handle(*he_it)).set_selected(true);
873
874
875
876
877
878
879
        }
    }
}

template< typename MeshT >
inline
void convertHalfedgeToFaceSelection(MeshT* _mesh) {
880
881
882
883
    // Note: A face is not only selected
    // iff all incident halfedges are selected but
    // at least one of them. This is, however,
    // desired in some cases.
884
885
    for ( typename MeshT::HalfedgeIter he_it= _mesh->halfedges_begin() ; he_it != _mesh->halfedges_end() ; ++he_it ) {
        
Jan Möbius's avatar
Jan Möbius committed
886
887
        if(_mesh->status(*he_it).selected()) {
            _mesh->status(_mesh->face_handle(*he_it)).set_selected(true);
888
889
890
891
        }
    }
}

Jan Möbius's avatar
 
Jan Möbius committed
892
893
894
895
896
//=========================================================
//== Face Selection =======================================
//=========================================================

template< typename MeshT >
897
inline
898
void selectFaces(MeshT* _mesh, const IdList& _faces) {
Jan Möbius's avatar
 
Jan Möbius committed
899
  const int n_faces = (int)_mesh->n_faces();
900
901

  for ( uint i = 0 ; i < _faces.size() ; ++i )
902
    if ( (_faces[i] >= 0) && ( _faces[i] < n_faces ) )  {
Jan Möbius's avatar
 
Jan Möbius committed
903
904
905
906
      typename MeshT::FaceHandle fh(_faces[i]);
      _mesh->status(fh).set_selected(true);
    }
}
907

Jan Möbius's avatar
 
Jan Möbius committed
908
//=========================================================
909

Jan Möbius's avatar
 
Jan Möbius committed
910
template< typename MeshT >
911
inline
912
void unselectFaces(MeshT* _mesh, const IdList& _faces) {