MeshSelectionT.cc 37 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
Jan Möbius's avatar
 
Jan Möbius committed
72 73
void selectVertices(MeshT* _mesh, std::vector< int >& _vertices) {
  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
Jan Möbius's avatar
 
Jan Möbius committed
86 87
void unselectVertices(MeshT* _mesh, std::vector< int >& _vertices) {
  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 117 118 119 120 121
      _mesh->status(v_it).set_selected(false);
}

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

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 128 129 130 131
      _mesh->status(v_it).set_selected( ! _mesh->status(v_it).selected());
}

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

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 142 143 144 145 146 147
      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);
      }
}

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

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 166 167 168 169 170
         for ( typename MeshT::VertexVertexIter vv_it(*_mesh,v_it); vv_it; ++vv_it)
            if ( ! _mesh->property(temp_shrink,vv_it) ){
                _mesh->status(v_it).set_selected( false );
                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; ++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 )
206
    if ( _mesh->status(v_it).selected() )
Jan Möbius's avatar
 
Jan Möbius committed
207
      selection.push_back( v_it.handle().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 222 223
  for ( typename MeshT::VertexIter v_it= _mesh->vertices_begin() ; v_it != _mesh->vertices_end() ; ++v_it )
    if ( _mesh->status(v_it).selected() )
      ++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 )
231
    if ( _mesh->status(v_it).selected() ^ _invert )
Jan Möbius's avatar
 
Jan Möbius committed
232
      selection.push_back( v_it.handle().idx() );
233

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


template< typename MeshT >
239
inline
Jan Möbius's avatar
 
Jan Möbius committed
240
void selectBoundaryVertices(MeshT* _mesh, typename MeshT::VertexHandle& _vh){
241

Jan Möbius's avatar
 
Jan Möbius committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
  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)
    _mesh->property(visited, v_it) = false;

  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)
      if ( _mesh->is_boundary( _mesh->edge_handle( voh_it.handle() ) ) )
        stack.push( _mesh->to_vertex_handle(voh_it) );

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

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

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

278
    typename MeshT::VertexHandle vh(*v);
279 280 281 282 283 284 285 286 287 288 289 290
    typename MeshT::VertexOHalfedgeIter ohe_iter = _mesh->voh_iter(vh);

    for (; ohe_iter; ++ohe_iter) {
      // test if both incident vertices are in _vertices
      typename MeshT::VertexHandle ovh = _mesh->to_vertex_handle(ohe_iter.handle());
      // search for ovh in _vertices
      for(std::vector<int>::iterator it = _vertices.begin(); it != _vertices.end(); ++it) {
        if((*it) == ovh.idx()) {
          _mesh->status(_mesh->edge_handle(ohe_iter.handle())).set_selected(true);
          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

302 303
    if ( _mesh->status( v_it ).selected() ) {
      typename MeshT::VertexOHalfedgeIter ohe_iter = _mesh->voh_iter(v_it);
304

305 306 307 308 309 310
      for (; ohe_iter; ++ohe_iter) {    
        // test if both incident vertices are in _vertices
        typename MeshT::VertexHandle ovh = _mesh->to_vertex_handle(ohe_iter.handle());
        if (_mesh->status(ovh).selected())
          _mesh->status(_mesh->edge_handle(ohe_iter.handle())).set_selected(true);
      }
311
    }
312
  }
313 314
}

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

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

    typename MeshT::VertexHandle vh(*v);
322 323 324 325 326 327 328 329 330 331 332 333 334
    typename MeshT::VertexOHalfedgeIter ohe_iter = _mesh->voh_iter(vh);

    for (; ohe_iter; ++ohe_iter) {
      // test if both incident vertices are in _vertices
      typename MeshT::VertexHandle ovh = _mesh->to_vertex_handle(ohe_iter.handle());
      // search for ovh in _vertices
      for(std::vector<int>::iterator it = _vertices.begin(); it != _vertices.end(); ++it) {
        if((*it) == ovh.idx()) {
          _mesh->status(ohe_iter.handle()).set_selected(true);
          _mesh->status(_mesh->opposite_halfedge_handle(ohe_iter.handle())).set_selected(true);
          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

347
    if ( _mesh->status( v_it ).selected() ) {
348

349
      typename MeshT::VertexOHalfedgeIter ohe_iter = _mesh->voh_iter(v_it);
350

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

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

367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
  for(typename MeshT::FaceIter f_it = _mesh->faces_begin(); f_it != _mesh->faces_end(); ++f_it) {
    typename MeshT::FaceVertexIter fv_it = _mesh->fv_iter(f_it);
    // go over each vertex of each face and test if it's selected
    bool allfound = true;
    for(; fv_it; ++fv_it) {
      // search fv_it in _vertices
      bool onefound = false;
      for(std::vector<int>::iterator it = _vertices.begin(); it != _vertices.end(); ++it) {
        if((*it) == fv_it.handle().idx()) { onefound = true; break; }
      }
      if(!onefound) {
        allfound = false;
        break;
      }
    }
    if(allfound) {
      // all incident vertices are selected -> select face
      _mesh->status(f_it).set_selected(true);
    }
  }
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 397 398 399 400 401 402 403 404
  for (f_it = _mesh->faces_begin(); f_it != f_end; ++f_it) {

    typename MeshT::FaceVertexIter fv_it = _mesh->fv_iter(f_it);
    // test if all incident vertices are selected
    bool allfound = true;
    for(; fv_it; ++fv_it) {
      if(!_mesh->status(fv_it.handle()).selected()) {
        allfound = false;
        break;
      }
405
    }
406 407 408
    if(allfound)
      _mesh->status(f_it).set_selected(true);
  }
409 410
}

411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
template< typename MeshT >
inline
void convertVertexSelectionToFeatureVertices(MeshT* _mesh) {

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

        if (_mesh->status(v_it).selected()) {

            _mesh->status(v_it).set_feature(true);
        } else {
            _mesh->status(v_it).set_feature(false);
        }
    }
}

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

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

        if (_mesh->status(v_it).feature()) {

            _mesh->status(v_it).set_selected(true);
        } else {
            _mesh->status(v_it).set_selected(false);
        }
    }
}

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

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

        _mesh->status(v_it).set_feature(false);
    }
}

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

template< typename MeshT >
456
inline
Jan Möbius's avatar
 
Jan Möbius committed
457 458 459
void setArea(MeshT* _mesh, std::vector< int >& _vertices , unsigned int _type, bool _state) {
  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 472 473 474
    _mesh->status(v_it).change_bit(_type,  _state);
}

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 )
480
    if ( _mesh->status(v_it).is_bit_set( _type ) )
Jan Möbius's avatar
 
Jan Möbius committed
481
      selection.push_back( v_it.handle().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 494 495
  for ( typename MeshT::VertexIter v_it= _mesh->vertices_begin() ; v_it != _mesh->vertices_end() ; ++v_it )
    if ( _mesh->status(v_it).is_bit_set( _type ) )
      ++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 )
503
    if ( _mesh->status(v_it).is_bit_set( _type ) ^ _invert )
Jan Möbius's avatar
 
Jan Möbius committed
504
      selection.push_back( v_it.handle().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
Jan Möbius's avatar
 
Jan Möbius committed
516 517
void selectEdges(MeshT* _mesh, std::vector< int >& _edges) {
  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
Jan Möbius's avatar
 
Jan Möbius committed
530 531
void unselectEdges(MeshT* _mesh, std::vector< int >& _edges) {
  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 582
  for (e_it = _mesh->edges_begin(); e_it != e_end ; ++e_it)
    if ( _mesh->is_boundary( _mesh->halfedge_handle(e_it,0) ) ||
Jan Möbius's avatar
 
Jan Möbius committed
583 584 585 586
         _mesh->is_boundary( _mesh->halfedge_handle(e_it,1) ) )
      _mesh->status(e_it).set_selected( true );
}

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 )
595
    if ( _mesh->status(e_it).selected() )
Jan Möbius's avatar
 
Jan Möbius committed
596
      selection.push_back( e_it.handle().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 611 612
  for ( typename MeshT::VertexIter e_it= _mesh->edges_begin() ; e_it != _mesh->edges_end() ; ++e_it )
    if ( _mesh->status(e_it).selected() )
      ++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 )
620
    if ( _mesh->status(e_it).selected() ^ _invert )
Jan Möbius's avatar
 
Jan Möbius committed
621
      selection.push_back( e_it.handle().idx() );
622

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

626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
template< typename MeshT >
inline
void convertEdgeToVertexSelection(MeshT* _mesh, std::vector< int >& _edges) {

	for (std::vector<int>::iterator e = _edges.begin(); e != _edges.end(); ++e) {

		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 649 650 651 652 653 654 655 656 657 658 659 660
template< typename MeshT >
inline
void convertEdgeToVertexSelection(MeshT* _mesh) {

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

      typename MeshT::HalfedgeHandle heh0 = _mesh->halfedge_handle(e_it.handle(), 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);
    }
}

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

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

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

691 692 693 694 695 696 697 698 699 700 701 702
  typename MeshT::FaceIter f_it, f_end = _mesh->faces_end();
  
  for (f_it = _mesh->faces_begin(); f_it != f_end; ++f_it) {

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

709 710 711 712 713 714 715 716 717 718 719 720 721
template< typename MeshT >
inline
void convertEdgeToHalfedgeSelection(MeshT* _mesh) {

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

      _mesh->status(_mesh->halfedge_handle(e_it, 0)).set_selected(true);
      _mesh->status(_mesh->halfedge_handle(e_it, 1)).set_selected(true);
    }
}

722 723
template< typename MeshT >
inline
724
void convertEdgeSelectionToFeatureEdges(MeshT* _mesh) {
725 726 727 728 729 730 731 732 733 734 735 736 737 738

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

        if (_mesh->status(e_it).selected()) {

            _mesh->status(e_it).set_feature(true);
        } else {
            _mesh->status(e_it).set_feature(false);
        }
    }
}

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

743 744 745 746 747 748 749 750 751 752 753
        if (_mesh->status(e_it).feature()) {

            _mesh->status(e_it).set_selected(true);
        } else {
            _mesh->status(e_it).set_selected(false);
        }
    }
}

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

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

        _mesh->status(e_it).set_feature(false);
    }
}
David Bommes's avatar
David Bommes committed
761 762 763 764 765 766 767 768 769 770 771

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

template< typename MeshT >
inline
void selectHalfedges(MeshT* _mesh, std::vector< int >& _halfedges) {
  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 782 783 784 785
      typename MeshT::HalfedgeHandle heh(_halfedges[i]);
      _mesh->status(heh).set_selected(true);
    }
}

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

template< typename MeshT >
inline
void unselectHalfedges(MeshT* _mesh, std::vector< int >& _halfedges) {
  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 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
      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)
  _mesh->status(he_it).set_selected(true);
}

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

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)
    _mesh->status(he_it).set_selected(false);
}

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

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)
    _mesh->status(he_it).set_selected( ! _mesh->status(he_it).selected());
}

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


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)
    if ( _mesh->is_boundary( he_it))
      _mesh->status(he_it).set_selected( true );
}

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

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 )
    if ( _mesh->status(he_it).selected() )
      selection.push_back( he_it.handle().idx() );

  return selection;
}

852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
template< typename MeshT >
inline
void convertHalfedgeToVertexSelection(MeshT* _mesh) {
    
    for ( typename MeshT::HalfedgeIter he_it= _mesh->halfedges_begin() ; he_it != _mesh->halfedges_end() ; ++he_it ) {
        
        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);
        }
    }
}

template< typename MeshT >
inline
void convertHalfedgeToEdgeSelection(MeshT* _mesh) {
    
    for ( typename MeshT::HalfedgeIter he_it= _mesh->halfedges_begin() ; he_it != _mesh->halfedges_end() ; ++he_it ) {
        
        if(_mesh->status(he_it).selected()) {
            _mesh->status(_mesh->edge_handle(he_it)).set_selected(true);
        }
    }
}

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 886 887 888 889 890 891
    for ( typename MeshT::HalfedgeIter he_it= _mesh->halfedges_begin() ; he_it != _mesh->halfedges_end() ; ++he_it ) {
        
        if(_mesh->status(he_it).selected()) {
            _mesh->status(_mesh->face_handle(he_it)).set_selected(true);
        }
    }
}

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, 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, IdList& _faces) {
Jan Möbius's avatar
 
Jan Möbius committed
913
  const int n_faces = (int)_mesh->n_faces();
914 915

  for ( uint i = 0 ; i < _faces.size() ; ++i )
916
    if ( (_faces[i] >= 0) && ( _faces[i] < n_faces ) )  {
Jan Möbius's avatar
 
Jan Möbius committed
917 918 919
      typename MeshT::FaceHandle fh(_faces[i]);
      _mesh->status(fh).set_selected(false);
    }
920 921
}

Jan Möbius's avatar
 
Jan Möbius committed
922 923 924 925 926 927
//=========================================================

template< typename MeshT >
inline
void selectAllFaces(MeshT* _mesh) {
   typename MeshT::FaceIter f_it, f_end=_mesh->faces_end();
928 929

   for (f_it = _mesh->faces_begin(); f_it != f_end ; ++f_it)
Jan Möbius's avatar
 
Jan Möbius committed
930 931 932 933 934 935 936 937 938 939
      _mesh->status(f_it).set_selected(true);
}

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


template< typename MeshT >
inline
void clearFaceSelection(MeshT* _mesh) {
   typename MeshT::FaceIter f_it, f_end=_mesh->faces_end();
940 941

   for (f_it = _mesh->faces_begin(); f_it != f_end ; ++f_it)
Jan Möbius's avatar
 
Jan Möbius committed
942 943 944 945 946 947 948
      _mesh->status(f_it).set_selected(false);
}

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


template< typename MeshT >
949
inline
Jan Möbius's avatar
 
Jan Möbius committed
950 951
void invertFaceSelection(MeshT* _mesh) {
   typename MeshT::FaceIter f_it, f_end=_mesh->faces_end();
952 953

   for (f_it = _mesh->faces_begin(); f_it != f_end ; ++f_it)
Jan Möbius's avatar
 
Jan Möbius committed
954 955 956 957 958 959 960 961 962
      _mesh->status(f_it).set_selected( ! _mesh->status(f_it).selected());
}

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

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

  for (he_it = _mesh->halfedges_begin(); he_it != he_end ; ++he_it)
Jan Möbius's avatar
 
Jan Möbius committed
965 966 967 968 969 970 971 972 973 974 975 976
    if (_mesh->is_boundary(he_it) ) {
        for (typename MeshT::VertexFaceIter vf_it(*_mesh ,_mesh->to_vertex_handle(he_it) ) ; vf_it ; ++vf_it)
          _mesh->status(vf_it).set_selected(true);
        for (typename MeshT::VertexFaceIter vf_it(*_mesh ,_mesh->from_vertex_handle(he_it) ) ; vf_it ; ++vf_it)
          _mesh->status(vf_it).set_selected(true);
    }
}

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


template< typename MeshT >
977
inline
Jan Möbius's avatar
 
Jan Möbius committed
978 979
void shrinkFaceSelection(MeshT* _mesh) {
   OpenMesh::FPropHandleT< bool > temp_shrink;
980

Jan Möbius's avatar
 
Jan Möbius committed
981
   _mesh->add_property( temp_shrink, "Temp property for Face selection shrinking" );
982

Jan Möbius's avatar
 
Jan Möbius committed
983
   typename MeshT::FaceIter f_it, f_end=_mesh->faces_end();
984

Jan Möbius's avatar
 
Jan Möbius committed
985
   // initialize property ( copy status to new property )
986
   for (f_it = _mesh->faces_begin(); f_it != f_end ; ++f_it)
Jan Möbius's avatar
 
Jan Möbius committed
987
      _mesh->property(temp_shrink,f_it) = _mesh->status(f_it).selected();
988

Jan Möbius's avatar
 
Jan Möbius committed
989
   // Shrink selection ( deselects all faces which are adjacent to a boundary vertex of the original selection)
990
   for (f_it = _mesh->faces_begin(); f_it != f_end ; ++f_it)
Jan Möbius's avatar
 
Jan Möbius committed
991 992 993 994 995
      if ( _mesh->property(temp_shrink,f_it) ) {
         bool boundary = false;
         for ( typename MeshT::FaceVertexIter fv_it(*_mesh,f_it); fv_it; ++fv_it) {
            for ( typename MeshT::VertexFaceIter vf_it(*_mesh,fv_it); vf_it; ++vf_it) {
               if ( ! _mesh->property(temp_shrink,vf_it) ) {
996
                 boundary = true;
Jan Möbius's avatar
 
Jan Möbius committed
997 998 999 1000 1001
               }
            }
            if ( boundary )
               break;
         }
1002

Jan Möbius's avatar
 
Jan Möbius committed
1003 1004
         _mesh->status(f_it).set_selected( !boundary );
      }
1005

Jan Möbius's avatar
 
Jan Möbius committed
1006 1007 1008 1009 1010 1011
   _mesh->remove_property(temp_shrink);
}

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

template< typename MeshT >
1012
inline
Jan Möbius's avatar
 
Jan Möbius committed
1013 1014
void growFaceSelection(MeshT* _mesh) {
  OpenMesh::FPropHandleT< bool > temp_grow;
1015

Jan Möbius's avatar
 
Jan Möbius committed
1016
  _mesh->add_property( temp_grow, "Temp property for Face selection growing" );
1017

Jan Möbius's avatar
 
Jan Möbius committed
1018
  typename MeshT::FaceIter f_it, f_end=_mesh->faces_end();
1019

Jan Möbius's avatar
 
Jan Möbius committed
1020
  // initialize property ( copy status to new property )
1021
  for (f_it = _mesh->faces_begin(); f_it != f_end ; ++f_it)
Jan Möbius's avatar
 
Jan Möbius committed
1022
    _mesh->property(temp_grow,f_it) = _mesh->status(f_it).selected();
1023

Jan Möbius's avatar
 
Jan Möbius committed
1024
  // Grow selection ( selects all faces which are adjacent to a vertex of a already selected face)
1025
  for (f_it = _mesh->faces_begin(); f_it != f_end ; ++f_it)
Jan Möbius's avatar
 
Jan Möbius committed
1026 1027 1028 1029
    if ( _mesh->property(temp_grow,f_it) )
        for ( typename MeshT::FaceVertexIter fv_it(*_mesh,f_it); fv_it; ++fv_it)
          for ( typename MeshT::VertexFaceIter vf_it(*_mesh,fv_it); vf_it; ++vf_it)
              _mesh->status(vf_it).set_selected( true );
1030

Jan Möbius's avatar
 
Jan Möbius committed
1031 1032 1033
  _mesh->remove_property(temp_grow);
}

1034
//=========================================================
Jan Möbius's avatar
 
Jan Möbius committed
1035 1036

template< typename MeshT >
1037
inline
Jan Möbius's avatar
 
Jan Möbius committed
1038 1039
std::vector< int > getFaceSelection(MeshT* _mesh) {
  std::vector< int > selection;
1040

Jan Möbius's avatar
 
Jan Möbius committed
1041
  for ( typename MeshT::FaceIter f_it= _mesh->faces_begin() ; f_it != _mesh->faces_end() ; ++f_it )