Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ComponentsPluginT.cc
1 /*===========================================================================*\
2 * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40  \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44  * $Revision$ *
45  * $LastChangedBy$ *
46  * $Date$ *
47  * *
48  \*===========================================================================*/
49 
50 #define COMPONENTSPLUGIN_CC
51 
52 #include "ComponentsPlugin.hh"
53 #include <iostream>
54 #include <vector>
55 
57 
58 //------------------------------------------------------------------------------
59 
60 
61 template< class MeshT >
62 void
63 ComponentsPlugin::splitComponent( MeshT* _mesh, MeshT* _copy){
64 
65  //init properties
67 
68  if ( !_mesh->get_property_handle(visited,"visited") )
69  _mesh->add_property(visited, "visited");
70 
71  typename MeshT::FaceIter f_it;
72  typename MeshT::FaceIter f_end = _mesh->faces_end();
73 
74  //init property for all faces
75  for (f_it = _mesh->faces_begin(); f_it != f_end; ++f_it)
76  _mesh->property(visited, *f_it) = false;
77 
78  typename MeshT::FaceHandle fh;
79 
80  //Take the first face in the mesh as we only split one component apart
81  fh = *(_mesh->faces_begin());
82  _mesh->property(visited, fh) = true;
83 
84 
85  //if none was found -> finished
86  if (!fh.is_valid() ) return;
87 
88  std::vector< typename MeshT::FaceHandle > handles;
89  handles.push_back( fh );
90 
91  //grow from face and collect faces in this component
92  while( handles.size() > 0 ){
93 
94  typename MeshT::FaceHandle current = handles.back();
95  handles.pop_back();
96 
97  typename MeshT::FaceFaceIter ff_it;
98 
99  for (ff_it=_mesh->ff_iter( current ); ff_it.is_valid(); ++ff_it)
100  if ( ! _mesh->property(visited, *ff_it) ){
101  _mesh->property(visited, *ff_it) = true;
102  handles.push_back( *ff_it );
103  }
104  }
105 
106  //delete the found component from the original mesh and keep it in the copy
107  for (typename MeshT::FaceIter f_it = _mesh->faces_begin(); f_it != f_end; ++f_it)
108  // -1 means not the component
109  if ( _mesh->property(visited, *f_it) )
110  _mesh->delete_face( *f_it );
111  else
112  _copy->delete_face( *f_it );
113 
114 
115  //remove garbage from the components
116  _copy->garbage_collection();
117  _mesh->garbage_collection();
118 
119  _mesh->remove_property(visited);
120 
121 }
122 
123 template< class MeshT >
124 void
126 
127 
129  if ( !_mesh->get_property_handle(visited,"visited") )
130  _mesh->add_property(visited, "visited");
131 
132  std::vector<typename MeshT::FaceHandle> facesInBiggest;
133 
134  for (typename MeshT::FaceIter fIter = _mesh->faces_begin(); fIter != _mesh->faces_end(); ++fIter)
135  {
136  if (_mesh->property(visited,*fIter))
137  continue;
138 
139  //It is a vertex, which is not visited => new component
140  std::vector<typename MeshT::FaceHandle> componentFaces;
141  componentFaces.push_back(*fIter);
142 
143  for(std::size_t i = 0; i < componentFaces.size(); ++i )
144  {
145  //add all not visited neightbours
146  for (typename MeshT::FaceFaceIter ffIter = _mesh->ff_begin(componentFaces[i]); ffIter.is_valid() ;++ffIter)
147  {
148  if (!ffIter->is_valid())
149  std::cout << "handleId: " << *ffIter << std::endl;
150  if (ffIter->is_valid() && !_mesh->property(visited,*ffIter) )
151  {
152  _mesh->property(visited,*ffIter) = true;
153  componentFaces.push_back(*ffIter);
154  }
155  }
156  }
157 
158  //all faces are found, so compare the components
159  if (facesInBiggest.size() < componentFaces.size())
160  {
161  std::swap(facesInBiggest,componentFaces);
162  }
163  }
164  _mesh->remove_property(visited);
165 
166  //select all faces in the biggest component;
167  for (typename std::vector<typename MeshT::FaceHandle>::iterator iter = facesInBiggest.begin(); iter != facesInBiggest.end(); ++iter)
168  {
169  _mesh->status(*iter).set_selected(true);
170  }
171 }
172 
173 template< class MeshT >
174 void
176 {
177  for (typename MeshT::FaceIter fIter = _mesh->faces_begin(); fIter != _mesh->faces_end(); ++fIter)
178  {
179  if (!_mesh->status(*fIter).selected())
180  {
181  //delete face and isolated vertices
182  _mesh->delete_face(*fIter,true);
183  }
184  else
185  {
186  _mesh->status(*fIter).set_selected(false);
187  }
188  }
189 
190  _mesh->garbage_collection();
191 }
void selectBiggestComponent(MeshT *_mesh)
Select the biggest component of the mesh.
void splitComponent(MeshT *_mesh, MeshT *_copy)
Split mesh into components.
void deleteUnselectedFaces(MeshT *_mesh)
Deletes all faces of a mesh that are not selected.