StatusAttrib.cc 8.49 KB
Newer Older
1
2
3
4
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
33
34
35
36
37
38
39
40
41
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*===========================================================================*\
 *                                                                           *
 *                            OpenVolumeMesh                                 *
 *        Copyright (C) 2011 by Computer Graphics Group, RWTH Aachen         *
 *                        www.openvolumemesh.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenVolumeMesh.                                     *
 *                                                                           *
 *  OpenVolumeMesh 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.                        *
 *                                                                           *
 *  OpenVolumeMesh 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 OpenVolumeMesh.  If not,                              *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                         *
 *   $Date$                    *
 *   $LastChangedBy$                                                *
 *                                                                           *
\*===========================================================================*/

#include "StatusAttrib.hh"

#include "../Core/TopologyKernel.hh"
#include "../Core/PropertyDefines.hh"

namespace OpenVolumeMesh {

StatusAttrib::StatusAttrib(TopologyKernel& _kernel) :
kernel_(_kernel),
v_status_(_kernel.request_vertex_property<OpenVolumeMeshStatus>("vertex_status")),
e_status_(_kernel.request_edge_property<OpenVolumeMeshStatus>("edge_status")),
he_status_(_kernel.request_halfedge_property<OpenVolumeMeshStatus>("halfedge_status")),
f_status_(_kernel.request_face_property<OpenVolumeMeshStatus>("face_status")),
hf_status_(_kernel.request_halfface_property<OpenVolumeMeshStatus>("halfface_status")),
c_status_(_kernel.request_cell_property<OpenVolumeMeshStatus>("cell_status")),
m_status_(_kernel.request_mesh_property<OpenVolumeMeshStatus>("mesh_status")) {

}

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

StatusAttrib::~StatusAttrib() {

}

void StatusAttrib::garbage_collection(bool _preserveManifoldness) {

    /*
     * Perform these steps from vertex to cell:
     * ===========================================
     * 1. Delete entity with handle h from vector
     * 2. Delete all higher dimensional entities
     *    containing a handle to the delete entity (set delete flag)
     * 3. Replace all handle indices h_i > h with (h_i - 1) in all
     *    higher dimensional entities (steps 2 and 3 can be combined)
     * 4. Delete property and status data of h
     * ===========================================
     * 5. Call update_adjacencies() (if required)
     * ===========================================
     * 6. Preserve manifoldness
     *    (optional, requires bottom-up adjacencies)
     */

    for(VertexIter v_it = kernel_.vertices_begin(); v_it != kernel_.vertices_end();) {

        if(!v_status_[v_it->idx()].deleted()) {
            ++v_it;
        } else {
            v_it = kernel_.delete_vertex(*v_it);
        }
    }

    for(EdgeIter e_it = kernel_.edges_begin(); e_it != kernel_.edges_end();) {

        if(!e_status_[e_it->idx()].deleted()) {
            ++e_it;
        } else {
            e_it = kernel_.delete_edge(*e_it);
        }
    }

    for(FaceIter f_it = kernel_.faces_begin(); f_it != kernel_.faces_end();) {

        if(!f_status_[f_it->idx()].deleted()) {
            ++f_it;
        } else {
            f_it = kernel_.delete_face(*f_it);
        }
    }

    for(CellIter c_it = kernel_.cells_begin(); c_it != kernel_.cells_end();) {

        if(!c_status_[c_it->idx()].deleted()) {
            ++c_it;
        } else {
            c_it = kernel_.delete_cell(*c_it);
        }
    }

    // Step 5
    if(kernel_.has_bottom_up_adjacencies()) {
        kernel_.update_adjacencies();
    }

    // Step 6
    if(_preserveManifoldness) {
        if(kernel_.has_bottom_up_adjacencies()) {

            // Go over all faces and find those
            // that are not incident to any cell
            for(FaceIter f_it = kernel_.faces_begin(); f_it != kernel_.faces_end();) {

                // Get half-faces
                HalfFaceHandle hf0 = kernel_.halfface_handle(*f_it, 0);
                HalfFaceHandle hf1 = kernel_.halfface_handle(*f_it, 1);

                // If neither of the half-faces is incident to a cell, delete face
                if(kernel_.incident_cell(hf0) == TopologyKernel::InvalidCellHandle &&
                        kernel_.incident_cell(hf1) == TopologyKernel::InvalidCellHandle) {

                    f_it = kernel_.delete_face(*f_it);

                    kernel_.update_face_adjacencies();

                } else {
                    ++f_it;
                }
            }

            kernel_.update_edge_adjacencies();

            // Go over all edges and find those
            // whose half-edges are not incident to any half-face
            for(EdgeIter e_it = kernel_.edges_begin(); e_it != kernel_.edges_end();) {

                // Get half-edges
                HalfEdgeHandle he0 = kernel_.halfedge_handle(*e_it, 0);
                HalfEdgeHandle he1 = kernel_.halfedge_handle(*e_it, 1);

                // If neither of the half-edges is incident to a half-face, delete edge
                HalfEdgeHalfFaceIter he0hf_it = kernel_.hehf_iter(he0);
                HalfEdgeHalfFaceIter he1hf_it = kernel_.hehf_iter(he1);

                if(!he0hf_it.valid() && !he1hf_it.valid()) {

                    e_it = kernel_.delete_edge(*e_it);

                    kernel_.update_edge_adjacencies();

                } else {
                     ++e_it;
                }
            }

            // Vertex caches have to be re-computed because the face/half-face
            // indices have changed since the last deletions
            kernel_.update_vertex_adjacencies();

            // Go over all vertices and find those
            // that are not incident to any edge
            for(VertexIter v_it = kernel_.vertices_begin(); v_it != kernel_.vertices_end();) {

                // If neither of the half-edges is incident to a half-face, delete edge
                VertexOHalfEdgeIter voh_it = kernel_.voh_iter(*v_it);

                if(!voh_it.valid()) {

                    v_it = kernel_.delete_vertex(*v_it);

                    kernel_.update_vertex_adjacencies();

                } else {
                     ++v_it;
                }
            }

        } else {
            std::cerr << "Preservation of three-manifoldness in garbage_collection() "
                    << "requires bottom-up adjacencies!" << std::endl;
            return;
        }
    }
}

} // Namespace OpenVolumeMesh