Developer Documentation
ComponentsPluginT_impl.hh
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 #define COMPONENTSPLUGIN_CC
43 
44 #include "ComponentsPlugin.hh"
45 #include <iostream>
46 #include <vector>
47 
49 
50 //------------------------------------------------------------------------------
51 
52 
53 template< class MeshT >
54 void
55 ComponentsPlugin::splitComponent( MeshT* _mesh, MeshT* _copy){
56 
57  //init properties
59 
60  if ( !_mesh->get_property_handle(visited,"visited") )
61  _mesh->add_property(visited, "visited");
62 
63  typename MeshT::FaceIter f_it;
64  typename MeshT::FaceIter f_end = _mesh->faces_end();
65 
66  //init property for all faces
67  for (f_it = _mesh->faces_begin(); f_it != f_end; ++f_it)
68  _mesh->property(visited, *f_it) = false;
69 
70  typename MeshT::FaceHandle fh;
71 
72  //Take the first face in the mesh as we only split one component apart
73  fh = *(_mesh->faces_begin());
74  _mesh->property(visited, fh) = true;
75 
76 
77  //if none was found -> finished
78  if (!fh.is_valid() ) return;
79 
80  std::vector< typename MeshT::FaceHandle > handles;
81  handles.push_back( fh );
82 
83  //grow from face and collect faces in this component
84  while( handles.size() > 0 ){
85 
86  typename MeshT::FaceHandle current = handles.back();
87  handles.pop_back();
88 
89  typename MeshT::FaceFaceIter ff_it;
90 
91  for (ff_it=_mesh->ff_iter( current ); ff_it.is_valid(); ++ff_it)
92  if ( ! _mesh->property(visited, *ff_it) ){
93  _mesh->property(visited, *ff_it) = true;
94  handles.push_back( *ff_it );
95  }
96  }
97 
98  //delete the found component from the original mesh and keep it in the copy
99  for (typename MeshT::FaceIter f_it = _mesh->faces_begin(); f_it != f_end; ++f_it)
100  // -1 means not the component
101  if ( _mesh->property(visited, *f_it) )
102  _mesh->delete_face( *f_it );
103  else
104  _copy->delete_face( *f_it );
105 
106 
107  //remove garbage from the components
108  _copy->garbage_collection();
109  _mesh->garbage_collection();
110 
111  _mesh->remove_property(visited);
112 
113 }
114 
115 template< class MeshT >
116 void
118 
119 
121  if ( !_mesh->get_property_handle(visited,"visited") )
122  _mesh->add_property(visited, "visited");
123 
124  std::vector<typename MeshT::FaceHandle> facesInBiggest;
125 
126  for (typename MeshT::FaceIter fIter = _mesh->faces_begin(); fIter != _mesh->faces_end(); ++fIter)
127  {
128  if (_mesh->property(visited,*fIter))
129  continue;
130 
131  //It is a vertex, which is not visited => new component
132  std::vector<typename MeshT::FaceHandle> componentFaces;
133  componentFaces.push_back(*fIter);
134 
135  for(std::size_t i = 0; i < componentFaces.size(); ++i )
136  {
137  //add all not visited neightbours
138  for (typename MeshT::FaceFaceIter ffIter = _mesh->ff_begin(componentFaces[i]); ffIter.is_valid() ;++ffIter)
139  {
140  if (!ffIter->is_valid())
141  std::cout << "handleId: " << *ffIter << std::endl;
142  if (ffIter->is_valid() && !_mesh->property(visited,*ffIter) )
143  {
144  _mesh->property(visited,*ffIter) = true;
145  componentFaces.push_back(*ffIter);
146  }
147  }
148  }
149 
150  //all faces are found, so compare the components
151  if (facesInBiggest.size() < componentFaces.size())
152  {
153  std::swap(facesInBiggest,componentFaces);
154  }
155  }
156  _mesh->remove_property(visited);
157 
158  //select all faces in the biggest component;
159  for (typename std::vector<typename MeshT::FaceHandle>::iterator iter = facesInBiggest.begin(); iter != facesInBiggest.end(); ++iter)
160  {
161  _mesh->status(*iter).set_selected(true);
162  }
163 }
164 
165 template< class MeshT >
166 void
168 {
169  for (typename MeshT::FaceIter fIter = _mesh->faces_begin(); fIter != _mesh->faces_end(); ++fIter)
170  {
171  if (!_mesh->status(*fIter).selected())
172  {
173  //delete face and isolated vertices
174  _mesh->delete_face(*fIter,true);
175  }
176  else
177  {
178  _mesh->status(*fIter).set_selected(false);
179  }
180  }
181 
182  _mesh->garbage_collection();
183 }
void splitComponent(MeshT *_mesh, MeshT *_copy)
Split mesh into components.
void selectBiggestComponent(MeshT *_mesh)
Select the biggest component of the mesh.
void deleteUnselectedFaces(MeshT *_mesh)
Deletes all faces of a mesh that are not selected.