StatusAttribT.cc 14.7 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
/*===========================================================================*\
 *                                                                           *
 *                            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: 36 $                                                         *
 *   $Date: 2012-01-10 18:00:06 +0100 (Di, 10 Jan 2012) $                    *
 *   $LastChangedBy: kremer $                                                *
 *                                                                           *
\*===========================================================================*/

#define STATUSATTRIBT_CC

#include "StatusAttrib.hh"

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

#include <map>

namespace OpenVolumeMesh {
//========================================================================================

template<typename std_API_Container_VHandlePointer,
         typename std_API_Container_HHandlePointer,
         typename std_API_Container_HFHandlePointer,
         typename std_API_Container_CHandlePointer>
void StatusAttrib::garbage_collection(std_API_Container_VHandlePointer &vh_to_update,
                                      std_API_Container_HHandlePointer &hh_to_update,
                                      std_API_Container_HFHandlePointer &hfh_to_update,
                                      std_API_Container_CHandlePointer &ch_to_update,
                                      bool _preserveManifoldness) {

    /*
     * This is not a real garbage collection in its conventional
     * sense. What happens in this routine are the following steps:
     *
     * 1. If an entity of dimension n is marked to be deleted,
     *    also mark all incident entities of dimension n + 1
     *    for deletion. Do this in a bottom-up fashion.
     * 2. Then delete all entities in top-down manner, so that
     *    no invalid incident higher-dimensional entity is generated.
     * 3. If desired, search for all isolated entities and mark
     *    them deleted in a top-down manner.
     * 4. Delete all entities marked deleted in step 4 in order
     *    to prevent manifoldness.
     */

    // setup tracking so we can update the given handles
    bool track_vh = !vh_to_update.empty();
    bool track_hh = !hh_to_update.empty();
    bool track_hfh = !hfh_to_update.empty();
    bool track_ch = !ch_to_update.empty();
    int offset_vh = 0;
    int offset_hh = 0;
    int offset_hfh = 0;
    int offset_ch = 0;

    std::map<int,int> vh_map;
    std::map<int,int> hh_map;
    std::map<int,int> hfh_map;
    std::map<int,int> ch_map;

    // initialise the maps
    if (track_vh) {
        typename std_API_Container_VHandlePointer::iterator it = vh_to_update.begin();
        typename std_API_Container_VHandlePointer::iterator end = vh_to_update.end();

Mike Kremer's avatar
Mike Kremer committed
100
        for (; it != end; ++it) {
101 102 103 104 105 106 107
            vh_map[(*it)->idx()] = (*it)->idx();
        }
    }
    if (track_hh) {
        typename std_API_Container_HHandlePointer::iterator it = hh_to_update.begin();
        typename std_API_Container_HHandlePointer::iterator end = hh_to_update.end();

Mike Kremer's avatar
Mike Kremer committed
108
        for (; it != end; ++it) {
109 110 111 112 113 114 115
            hh_map[(*it)->idx()] = (*it)->idx();
        }
    }
    if (track_hfh) {
        typename std_API_Container_HFHandlePointer::iterator it = hfh_to_update.begin();
        typename std_API_Container_HFHandlePointer::iterator end = hfh_to_update.end();

Mike Kremer's avatar
Mike Kremer committed
116
        for (; it != end; ++it) {
117 118 119 120 121 122 123
            hfh_map[(*it)->idx()] = (*it)->idx();
        }
    }
    if (track_ch) {
        typename std_API_Container_CHandlePointer::iterator it = ch_to_update.begin();
        typename std_API_Container_CHandlePointer::iterator end = ch_to_update.end();

Mike Kremer's avatar
Mike Kremer committed
124
        for (; it != end; ++it) {
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
            ch_map[(*it)->idx()] = (*it)->idx();
        }
    }

    // Mark all higher-dimensional entities incident to
    // entities marked as deleted from bottom to top
    mark_higher_dim_entities();

    std::vector<int> vertexIndexMap(kernel_.n_vertices(), -1);

    // Turn off bottom-up incidences
    bool v_bu = kernel_.has_vertex_bottom_up_incidences();
    bool e_bu = kernel_.has_edge_bottom_up_incidences();
    bool f_bu = kernel_.has_face_bottom_up_incidences();

    kernel_.enable_bottom_up_incidences(false);

    std::vector<bool> tags(kernel_.n_cells(), false);
    std::vector<bool>::iterator tag_it = tags.begin();

    for(CellIter c_it = kernel_.cells_begin(); c_it != kernel_.cells_end(); ++c_it, ++tag_it) {
        *tag_it = c_status_[c_it->idx()].deleted();

        if (track_ch) {
            if (c_status_[c_it->idx()].deleted()) {
                ++offset_ch;
                if (ch_map.find(c_it->idx()) != ch_map.end())
                    ch_map[c_it->idx()] = -1;
            } else {
                if (ch_map.find(c_it->idx()) != ch_map.end())
                    ch_map[c_it->idx()] = c_it->idx() - offset_ch;
            }
        }
    }
    kernel_.delete_multiple_cells(tags);

    tags.resize(kernel_.n_faces(), false);
    tag_it = tags.begin();

    for(FaceIter f_it = kernel_.faces_begin(); f_it != kernel_.faces_end(); ++f_it, ++tag_it) {
        *tag_it = f_status_[f_it->idx()].deleted();

        if (track_hfh) {
            int halfface_idx = f_it->idx() * 2;
            if (f_status_[f_it->idx()].deleted()) {
                offset_hfh += 2;
                if (hfh_map.find(halfface_idx) != hfh_map.end()) {
                    hfh_map[halfface_idx] = -1;
                }
                if (hfh_map.find(halfface_idx + 1) != hfh_map.end()) {
                    hfh_map[halfface_idx + 1] = -1;
                }
            } else {
                if (hfh_map.find(halfface_idx) != hfh_map.end()) {
                    hfh_map[halfface_idx] = halfface_idx - offset_hfh;
                }
                if (hfh_map.find(halfface_idx + 1) != hfh_map.end()) {
                    hfh_map[halfface_idx + 1] = halfface_idx + 1 - offset_hfh;
                }
            }
        }
    }
    kernel_.delete_multiple_faces(tags);

    tags.resize(kernel_.n_edges(), false);
    tag_it = tags.begin();

    for(EdgeIter e_it = kernel_.edges_begin(); e_it != kernel_.edges_end(); ++e_it, ++tag_it) {
        *tag_it = e_status_[e_it->idx()].deleted();

        if (track_hh) {
            int halfedge_idx = e_it->idx() * 2;
            if (e_status_[e_it->idx()].deleted()) {
                offset_hh += 2;
                if (hh_map.find(halfedge_idx) != hh_map.end()) {
                    hh_map[halfedge_idx] = -1;
                }
                if (hh_map.find(halfedge_idx + 1) != hh_map.end()) {
                    hh_map[halfedge_idx + 1] = -1;
                }
            } else {
                if (hh_map.find(halfedge_idx) != hh_map.end()) {
                    hh_map[halfedge_idx] = halfedge_idx - offset_hh;
                }
                if (hh_map.find(halfedge_idx + 1) != hh_map.end()) {
                    hh_map[halfedge_idx + 1] = halfedge_idx + 1 - offset_hh;
                }
            }
        }
    }
    kernel_.delete_multiple_edges(tags);

    tags.resize(kernel_.n_vertices(), false);
    tag_it = tags.begin();

    for(VertexIter v_it = kernel_.vertices_begin(); v_it != kernel_.vertices_end(); ++v_it, ++tag_it) {
        *tag_it = v_status_[v_it->idx()].deleted();

        if (track_vh) {
            if (v_status_[v_it->idx()].deleted()) {
                if (vh_map.find(v_it->idx()) != vh_map.end()) {
                    ++offset_vh;
                    vh_map[v_it->idx()] = -1;
                }
            } else {
                if (vh_map.find(v_it->idx()) != vh_map.end()) {
                    vh_map[v_it->idx()] = v_it->idx() - offset_vh;
                }
            }
        }
    }
    kernel_.delete_multiple_vertices(tags);

    // update given handles
    if (track_vh) {
        typename std_API_Container_VHandlePointer::iterator it = vh_to_update.begin();
        typename std_API_Container_VHandlePointer::iterator end = vh_to_update.end();

        for (; it != end; ++it) {
            *(*it) = VertexHandle( vh_map[(*it)->idx()] );
        }
    }
    if (track_hh) {
        typename std_API_Container_HHandlePointer::iterator it = hh_to_update.begin();
        typename std_API_Container_HHandlePointer::iterator end = hh_to_update.end();

        for (; it != end; ++it) {
            *(*it) = HalfEdgeHandle( hh_map[(*it)->idx()] );
        }
    }
    if (track_hfh) {
        typename std_API_Container_HFHandlePointer::iterator it = hfh_to_update.begin();
        typename std_API_Container_HFHandlePointer::iterator end = hfh_to_update.end();

        for (; it != end; ++it) {
            *(*it) = HalfFaceHandle( hfh_map[(*it)->idx()] );
        }
    }
    if (track_ch) {
        typename std_API_Container_CHandlePointer::iterator it = ch_to_update.begin();
        typename std_API_Container_CHandlePointer::iterator end = ch_to_update.end();

        for (; it != end; ++it) {
            *(*it) = CellHandle( ch_map[(*it)->idx()] );
        }
    }

    // Todo: Resize props

    if(v_bu) kernel_.enable_vertex_bottom_up_incidences(true);
    if(e_bu) kernel_.enable_edge_bottom_up_incidences(true);
    if(f_bu) kernel_.enable_face_bottom_up_incidences(true);

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

            // 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(); ++f_it) {

                // 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_status_[f_it->idx()].set_deleted(true);
                }
            }

            // 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(); ++e_it) {

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

                // If the half-edge isn't incident to a half-face, delete edge
                HalfEdgeHalfFaceIter hehf_it = kernel_.hehf_iter(he);

                if(!hehf_it.valid()) {

                    e_status_[e_it->idx()].set_deleted(true);

                } else {
                    bool validFace = false;
                    for(; hehf_it.valid(); ++hehf_it) {
                        if(!f_status_[kernel_.face_handle(*hehf_it).idx()].deleted()) {
                            validFace = true;
                            break;
                        }
                    }
                    if(!validFace) {
                        e_status_[e_it->idx()].set_deleted(true);
                    }
                }
            }

            // 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(); ++v_it) {

                // 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_status_[v_it->idx()].set_deleted(true);
                } else {

                    bool validEdge = false;
                    for(; voh_it.valid(); ++voh_it) {
Max Lyon's avatar
Max Lyon committed
340
                        if(!e_status_[kernel_.edge_handle(*voh_it)].deleted()) {
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
                            validEdge = true;
                            break;
                        }
                    }
                    if(!validEdge) {
                        v_status_[v_it->idx()].set_deleted(true);
                    }
                }
            }

            // Recursive call
            garbage_collection(vh_to_update, hh_to_update, hfh_to_update, ch_to_update, false);

        } else {
#ifndef NDEBUG
            std::cerr << "Preservation of three-manifoldness in garbage_collection() "
                    << "requires bottom-up incidences!" << std::endl;
#endif
            return;
        }
    }
}
} // Namespace OpenVolumeMesh