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