TopologyKernel.cc 45.8 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
/*===========================================================================*\
 *                                                                           *
 *                            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$                                                *
 *                                                                           *
\*===========================================================================*/

43
#include <OpenVolumeMesh/System/FunctionalInclude.hh>
44 45
#include <queue>

46 47 48 49 50 51 52 53 54 55 56 57 58
#include "TopologyKernel.hh"

namespace OpenVolumeMesh {

// Initialize constants
const VertexHandle      TopologyKernel::InvalidVertexHandle   = VertexHandle(-1);
const EdgeHandle        TopologyKernel::InvalidEdgeHandle     = EdgeHandle(-1);
const HalfEdgeHandle    TopologyKernel::InvalidHalfEdgeHandle = HalfEdgeHandle(-1);
const FaceHandle        TopologyKernel::InvalidFaceHandle     = FaceHandle(-1);
const HalfFaceHandle    TopologyKernel::InvalidHalfFaceHandle = HalfFaceHandle(-1);
const CellHandle        TopologyKernel::InvalidCellHandle     = CellHandle(-1);

TopologyKernel::TopologyKernel() :
59 60 61 62
    n_vertices_(0u),
    n_edges_(0u),
    n_faces_(0u),
    n_cells_(0u),
63 64
    v_bottom_up_(true),
    e_bottom_up_(true),
65
    f_bottom_up_(true) {
66 67 68 69 70 71 72 73

}

TopologyKernel::~TopologyKernel() {
}

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

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
VertexHandle TopologyKernel::add_vertex() {

    ++n_vertices_;

    // Create item for vertex bottom-up adjacencies
    if(v_bottom_up_) {
        outgoing_hes_per_vertex_.resize(n_vertices_);
    }

    // Resize vertex props
    resize_vprops(n_vertices_);

    // Return 0-indexed handle
    return VertexHandle((int)(n_vertices_ - 1));
}

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

92 93 94 95
/// Add edge
EdgeHandle TopologyKernel::add_edge(const VertexHandle& _fromVertex,
                                    const VertexHandle& _toVertex) {

96
    if((unsigned int)_fromVertex.idx() >= n_vertices() || (unsigned int)_toVertex.idx() >= n_vertices()) {
97 98 99 100 101
        std::cerr << "Vertex handle is out of bounds!" << std::endl;
        return InvalidEdgeHandle;
    }

    // Test if edge does not exist, yet
102
    for(unsigned int i = 0; i < n_edges_; ++i) {
103 104 105 106 107 108 109 110 111 112 113 114
        if(edge(EdgeHandle(i)).from_vertex() == _fromVertex && edge(EdgeHandle(i)).to_vertex() == _toVertex) {
            return EdgeHandle(i);
        } else if(edge(EdgeHandle(i)).from_vertex() == _toVertex && edge(EdgeHandle(i)).to_vertex() == _fromVertex) {
            return EdgeHandle(i);
        }
    }

    // Create edge object
    OpenVolumeMeshEdge e(_fromVertex, _toVertex);

    // Store edge locally
    edges_.push_back(e);
115
    ++n_edges_;
116 117 118 119

    // Resize props
    resize_eprops(n_edges());

120
    EdgeHandle eh((int)n_edges_-1);
121 122 123 124 125

    // Update vertex bottom-up adjacencies
    if(v_bottom_up_) {
        assert(outgoing_hes_per_vertex_.size() > (unsigned int)_fromVertex.idx());
        assert(outgoing_hes_per_vertex_.size() > (unsigned int)_toVertex.idx());
126 127
        outgoing_hes_per_vertex_[_fromVertex.idx()].push_back(halfedge_handle(eh, 0));
        outgoing_hes_per_vertex_[_toVertex.idx()].push_back(halfedge_handle(eh, 1));
128 129 130 131 132 133 134
    }

    // Create item for edge bottom-up adjacencies
    if(e_bottom_up_) {
        incident_hfs_per_he_.resize(n_halfedges());
    }

135
    // Get handle of recently created edge
136
    return eh;
137 138 139 140 141 142 143 144
}

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

/// Add face via incident edges
FaceHandle TopologyKernel::add_face(const std::vector<HalfEdgeHandle>& _halfedges, bool _topologyCheck) {

    // Test if all edges are valid
145 146
    for(std::vector<HalfEdgeHandle>::const_iterator it = _halfedges.begin(),
            end = _halfedges.end(); it != end; ++it) {
147
        if((unsigned int)it->idx() >= n_edges_ * 2u) {
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
            std::cerr << "Halfedge handle out of bounds!" << std::endl;
            return InvalidFaceHandle;
        }
    }

    // Perform topology check
    if(_topologyCheck) {

        /*
         * Test if halfedges are connected
         *
         * The test works as follows:
         * For every edge in the parameter vector
         * put all incident vertices into a
         * set of either "from"-vertices or "to"-vertices,
         * respectively.
         * If and only if all edges are connected,
         * then both sets are identical.
         */

        std::set<VertexHandle> fromVertices;
        std::set<VertexHandle> toVertices;

171 172
        for(std::vector<HalfEdgeHandle>::const_iterator it = _halfedges.begin(),
            end = _halfedges.end(); it != end; ++it) {
173 174 175 176 177

            fromVertices.insert(halfedge(*it).from_vertex());
            toVertices.insert(halfedge(*it).to_vertex());
        }

178 179
        for(std::set<VertexHandle>::const_iterator v_it = fromVertices.begin(),
                v_end = fromVertices.end(); v_it != v_end; ++v_it) {
180 181 182 183 184 185 186 187 188 189 190 191 192
            if(toVertices.count(*v_it) != 1) {
                std::cerr << "The specified halfedges are not connected!" << std::endl;
                return InvalidFaceHandle;
            }
        }

        // The halfedges are now guaranteed to be connected
    }

    // Create face
    OpenVolumeMeshFace face(_halfedges);

    faces_.push_back(face);
193
    ++n_faces_;
194 195

    // Get added face's handle
196
    FaceHandle fh(n_faces_ - 1);
197 198 199 200

    // Resize props
    resize_fprops(n_faces());

201 202 203 204 205 206 207
    // Update edge bottom-up adjacencies
    if(e_bottom_up_) {

        for(std::vector<HalfEdgeHandle>::const_iterator it = _halfedges.begin(),
            end = _halfedges.end(); it != end; ++it) {
            assert(incident_hfs_per_he_.size() > (unsigned int)it->idx());
            assert(incident_hfs_per_he_.size() > (unsigned int)opposite_halfedge_handle(*it).idx());
208 209
            incident_hfs_per_he_[it->idx()].push_back(halfface_handle(fh, 0));
            incident_hfs_per_he_[opposite_halfedge_handle(*it).idx()].push_back(halfface_handle(fh, 1));
210 211 212 213 214 215 216 217
        }
    }

    // Create item for face bottom-up adjacencies
    if(f_bottom_up_) {
        incident_cell_per_hf_.resize(n_halffaces(), InvalidCellHandle);
    }

218 219 220 221 222 223 224 225 226 227 228
    // Return handle of recently created face
    return fh;
}

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

/// Add face via incident vertices
/// Define the _vertices in counter-clockwise order (from the "outside")
FaceHandle TopologyKernel::add_face(const std::vector<VertexHandle>& _vertices) {

    // Test if all vertices exist
229 230
    for(std::vector<VertexHandle>::const_iterator it = _vertices.begin(),
            end = _vertices.end(); it != end; ++it) {
231
        if((unsigned int)it->idx() >= n_vertices()) {
232 233 234 235 236 237 238 239
            std::cerr << "Vertex handle out of bounds!" << std::endl;
            return InvalidFaceHandle;
        }
    }

    // Add edge for each pair of vertices
    std::vector<HalfEdgeHandle> halfedges;
    std::vector<VertexHandle>::const_iterator it = _vertices.begin();
240 241
    std::vector<VertexHandle>::const_iterator end = _vertices.end();
    for(; (it+1) != end; ++it) {
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
        EdgeHandle e_idx = add_edge(*it, *(it+1));

        // Swap halfedge if edge already existed and
        // has been initially defined in reverse orientation
        int swap = 0;
        if(edge(e_idx).to_vertex() == *it) swap = 1;

        halfedges.push_back(halfedge_handle(e_idx, swap));
    }
    EdgeHandle e_idx = add_edge(*it, *_vertices.begin());
    int swap = 0;
    if(edge(e_idx).to_vertex() == *it) swap = 1;
    halfedges.push_back(halfedge_handle(e_idx, swap));

    // Add face
#ifndef NDEBUG
    return add_face(halfedges, true);
#else
    return add_face(halfedges, false);
#endif
}

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

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
void TopologyKernel::reorder_incident_halffaces(const EdgeHandle& _eh) {

    /* Put halffaces in clockwise order via the
     * same cell property which now exists.
     * Note, this only works for manifold configurations though.
     * Proceed as follows: Pick one starting halfface. Assuming
     * that all halfface normals point into the incident cell,
     * we find the adjacent halfface within the incident cell
     * along the considered halfedge. We set the found halfface
     * to be the one to be processed next. If we reach an outside
     * region, we try to go back from the starting halfface in reverse
     * order. If the complex is properly connected (the pairwise
     * intersection of two adjacent 3-dimensional cells is always
     * a 2-dimensional entity, namely a facet), such an ordering
     * always exists and will be found. If not, a correct order
     * can not be given and, as a result, the related iterators
     * will address the related entities in an arbitrary fashion.
     */

    for(unsigned char s = 0; s <= 1; s++) {

        HalfEdgeHandle cur_he = halfedge_handle(_eh, s);
        std::vector<HalfFaceHandle> new_halffaces;
        HalfFaceHandle start_hf = InvalidHalfFaceHandle;
        HalfFaceHandle cur_hf = InvalidHalfFaceHandle;

        // Start with one incident halfface and go
        // into the first direction
        assert(incident_hfs_per_he_.size() > (unsigned int)cur_he.idx());

296
        if(incident_hfs_per_he_[cur_he.idx()].size() != 0) {
297 298

            // Get start halfface
299
            cur_hf = *incident_hfs_per_he_[cur_he.idx()].begin();
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
            start_hf = cur_hf;

            while(cur_hf != InvalidHalfFaceHandle) {

                // Add halfface
                new_halffaces.push_back(cur_hf);

                // Go to next halfface
                cur_hf = adjacent_halfface_in_cell(cur_hf, cur_he);

                if(cur_hf != InvalidHalfFaceHandle)
                    cur_hf = opposite_halfface_handle(cur_hf);

                // End when we're through
                if(cur_hf == start_hf) break;
            }

            // First direction has terminated
            // If new_halffaces has the same size as old (unordered)
            // vector of incident halffaces, we are done here
            // If not, try the other way round
321
            if(new_halffaces.size() != incident_hfs_per_he_[cur_he.idx()].size()) {
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339

                // Get opposite of start halfface
                cur_hf = start_hf;

                 while(cur_hf != InvalidHalfFaceHandle) {

                     cur_hf = opposite_halfface_handle(cur_hf);
                     cur_hf = adjacent_halfface_in_cell(cur_hf, cur_he);

                     if(cur_hf == start_hf) break;

                     if(cur_hf != InvalidHalfFaceHandle)
                         new_halffaces.insert(new_halffaces.begin(), cur_hf);
                     else break;
                }
            }

            // Everything worked just fine, set the new ordered vector
340 341
            if(new_halffaces.size() == incident_hfs_per_he_[cur_he.idx()].size()) {
                incident_hfs_per_he_[cur_he.idx()] = new_halffaces;
342 343 344 345 346 347 348
            }
        }
    }
}

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

349 350 351 352
/// Add cell via incident halffaces
CellHandle TopologyKernel::add_cell(const std::vector<HalfFaceHandle>& _halffaces, bool _topologyCheck) {

    // Test if halffaces have valid indices
353 354
    for(std::vector<HalfFaceHandle>::const_iterator it = _halffaces.begin(),
            end = _halffaces.end(); it != end; ++it) {
355
        if((unsigned int)it->idx() >= n_faces_ * 2u) {
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
            std::cerr << "HalfFace handle is out of bounds!" << std::endl;
            return InvalidCellHandle;
        }
    }

    // Perform topology check
    if(_topologyCheck) {

        /*
         * Test if all halffaces are connected and form a two-manifold
         * => Cell is closed
         *
         * This test is simple: The number of involved half-edges has to be
         * exactly twice the number of involved edges.
         */

        std::set<HalfEdgeHandle> incidentHalfedges;
        std::set<EdgeHandle>     incidentEdges;

375 376
        for(std::vector<HalfFaceHandle>::const_iterator it = _halffaces.begin(),
                end = _halffaces.end(); it != end; ++it) {
377 378

            OpenVolumeMeshFace hface = halfface(*it);
379 380
            for(std::vector<HalfEdgeHandle>::const_iterator he_it = hface.halfedges().begin(),
                    he_end = hface.halfedges().end(); he_it != he_end; ++he_it) {
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
                incidentHalfedges.insert(*he_it);
                incidentEdges.insert(edge_handle(*he_it));
            }
        }

        if(incidentHalfedges.size() != (incidentEdges.size() * 2u)) {
            std::cerr << "The specified halffaces are not connected!" << std::endl;
            return InvalidCellHandle;
        }

        // The halffaces are now guaranteed to form a two-manifold
    }

    // Create new cell
    OpenVolumeMeshCell cell(_halffaces);

    cells_.push_back(cell);
398
    ++n_cells_;
399 400 401 402

    // Resize props
    resize_cprops(n_cells());

403
    CellHandle ch((int)n_cells_-1);
404 405 406 407 408 409 410 411

    // Update face bottom-up adjacencies
    if(f_bottom_up_) {

        std::set<EdgeHandle> cell_edges;
        for(std::vector<HalfFaceHandle>::const_iterator it = _halffaces.begin(),
                end = _halffaces.end(); it != end; ++it) {
            assert(incident_cell_per_hf_.size() > (unsigned int)it->idx());
412
            assert(incident_cell_per_hf_[it->idx()] == InvalidCellHandle);
413
            incident_cell_per_hf_[it->idx()] = ch;
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471

            // Collect all edges of cell
            const std::vector<HalfEdgeHandle> hes = halfface(*it).halfedges();
            for(std::vector<HalfEdgeHandle>::const_iterator he_it = hes.begin(),
                    he_end = hes.end(); he_it != he_end; ++he_it) {
                cell_edges.insert(edge_handle(*he_it));
            }
        }

        if(e_bottom_up_) {

            // Try to reorder all half-faces w.r.t.
            // their incident half-edges such that all
            // half-faces are in cyclic order around
            // a half-edge
            for(std::set<EdgeHandle>::const_iterator e_it = cell_edges.begin(),
                    e_end = cell_edges.end(); e_it != e_end; ++e_it) {
                reorder_incident_halffaces(*e_it);
            }
        }
    }

    return ch;
}

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

/**
 * \brief Delete vertex from mesh
 *
 * After performing this operation, all vertices
 * following vertex _h in the array will be accessible
 * through their old handle decreased by one.
 * This function directly fixes the vertex links
 * in all edges. These steps are performed:
 *
 * 1) Search all incident half-edges HE_v +
 *    Decrease all vertex handles in incident edges
 *    with index > v by 1
 * 2) Delete entry in BU: V -> HF
 * 3) Delete vertex itself (not necessary here since
 *    a vertex is only represented by a number)
 * 4) Delete property entry
 * 5) Delete incident edges
 *
 * @param _h A vertex handle
 */
VertexIter TopologyKernel::delete_vertex(const VertexHandle& _h) {

    assert(_h.idx() < (int)n_vertices());

    // 1)
    std::priority_queue<EdgeHandle> incident_edges;
    if(v_bottom_up_) {

        // Speed-up, because we know the incident edges
        // Get incident edges
        assert(outgoing_hes_per_vertex_.size() > (unsigned int)_h.idx());
472
        const std::vector<HalfEdgeHandle>& inc_hes = outgoing_hes_per_vertex_[_h.idx()];
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
        for(std::vector<HalfEdgeHandle>::const_iterator he_it = inc_hes.begin(),
                he_end = inc_hes.end(); he_it != he_end; ++he_it) {
            incident_edges.push(edge_handle(*he_it));
        }
        // Decrease all vertex handles that are greater than _h in all edge definitions
        for(std::vector<std::vector<HalfEdgeHandle> >::iterator he_it = (outgoing_hes_per_vertex_.begin() + _h.idx() + 1),
                he_end = outgoing_hes_per_vertex_.end(); he_it != he_end; ++he_it) {
            for(std::vector<HalfEdgeHandle>::const_iterator it = he_it->begin(),
                    end = he_it->end(); it != end; ++it) {

                if(it->idx() % 2 == 0) {
                    VertexHandle vh = edge(edge_handle(*it)).from_vertex();
                    if(vh.is_valid())
                        edge(edge_handle(*it)).set_from_vertex(VertexHandle(vh.idx() - 1));
                } else {
                    VertexHandle vh = edge(edge_handle(*it)).to_vertex();
                    if(vh.is_valid())
                        edge(edge_handle(*it)).set_to_vertex(VertexHandle(vh.idx() - 1));
                }
            }
        }
    } else {

        // Iterate over all edges
        for(EdgeIter e_it = edges_begin(), e_end = edges_end();
                e_it != e_end; ++e_it) {

            // Get incident edges
            if(edge(*e_it).from_vertex() == _h ||
                    edge(*e_it).to_vertex() == _h) {
                incident_edges.push(*e_it);
                continue;
            }

            // Decrease all vertex handles in edge definitions that are greater than _h
            if(edge(*e_it).from_vertex() > _h) {
                edge(*e_it).set_from_vertex(VertexHandle(edge(*e_it).from_vertex().idx() - 1));
            }
            if(edge(*e_it).to_vertex() > _h) {
                edge(*e_it).set_to_vertex(VertexHandle(edge(*e_it).to_vertex().idx() - 1));
            }
        }
    }

    // 2)
    if(v_bottom_up_) {
        assert(outgoing_hes_per_vertex_.size() > (unsigned int)_h.idx());
        outgoing_hes_per_vertex_.erase(outgoing_hes_per_vertex_.begin() + _h.idx());
    }

    // 3)
    --n_vertices_;

    // 4)
    vertex_deleted(_h);

    // 5)
    while(!incident_edges.empty()) {
        delete_edge(incident_edges.top());
        incident_edges.pop();
    }

    // Iterator to next element in vertex list
    return (vertices_begin() + _h.idx());
}

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

/**
 * \brief Delete edge from mesh
 *
 * After performing this operation, all edges
 * following edge _h in the array will be accessible
 * through their old handle decreased by one.
 * This function directly fixes the edge links
 * in all faces. These steps are performed:
 *
 * 1) Delete links in BU: V -> HE
 * 2) Search all incident faces +
 *    decrease all half-edge handles > he
 *    in all incident faces
 * 3) Delete item in BU: HE -> HF
 * 4) Decrease all entries > he in BU: V -> HE
 * 5) Delete edge from storage array
 * 6) Delete property item
 * 7) Delete incident faces
 *
 * @param _h An edge handle
 */
EdgeIter TopologyKernel::delete_edge(const EdgeHandle& _h) {

564
    assert(_h.idx() < (int)n_edges_);
565 566 567 568 569 570

    // 1)
    if(v_bottom_up_) {

        VertexHandle v0 = edge(_h).from_vertex();
        VertexHandle v1 = edge(_h).to_vertex();
571
        assert(outgoing_hes_per_vertex_.size() > (unsigned int)std::max(v0.idx(), v1.idx()));
572

573 574 575
        outgoing_hes_per_vertex_[v0.idx()].erase(
                std::remove(outgoing_hes_per_vertex_[v0.idx()].begin(),
                            outgoing_hes_per_vertex_[v0.idx()].end(),
576
                            halfedge_handle(_h, 0)),
577
                            outgoing_hes_per_vertex_[v0.idx()].end());
578

579 580 581
        outgoing_hes_per_vertex_[v1.idx()].erase(
                std::remove(outgoing_hes_per_vertex_[v1.idx()].begin(),
                            outgoing_hes_per_vertex_[v1.idx()].end(),
582
                            halfedge_handle(_h, 1)),
583
                            outgoing_hes_per_vertex_[v1.idx()].end());
584 585 586 587 588 589 590 591
    }

    // 2)
    std::priority_queue<FaceHandle> incident_faces;
    if(e_bottom_up_) {

        // Speed-up, because we already know all incident faces
        // Get incident faces
592
        assert(incident_hfs_per_he_.size() > (unsigned int)halfedge_handle(_h, 0).idx());
593

594
        const std::vector<HalfFaceHandle>& inc_hfs = incident_hfs_per_he_[halfedge_handle(_h, 0).idx()];
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
        for(std::vector<HalfFaceHandle>::const_iterator hf_it = inc_hfs.begin(),
                hf_end = inc_hfs.end(); hf_it != hf_end; ++hf_it) {
            incident_faces.push(face_handle(*hf_it));
        }

        // Decrease all half-edge handles > he in face definitions
        // Get all faces that need updates
        std::set<FaceHandle> update_faces;
        for(std::vector<std::vector<HalfFaceHandle> >::const_iterator iit =
                (incident_hfs_per_he_.begin() + halfedge_handle(_h, 1).idx() + 1),
                iit_end = incident_hfs_per_he_.end(); iit != iit_end; ++iit) {
            for(std::vector<HalfFaceHandle>::const_iterator it = iit->begin(),
                    end = iit->end(); it != end; ++it) {
                update_faces.insert(face_handle(*it));
            }
        }
        // Update respective handles
        HEHandleCorrection cor(halfedge_handle(_h, 1));
        for(std::set<FaceHandle>::iterator f_it = update_faces.begin(),
                f_end = update_faces.end(); f_it != f_end; ++f_it) {

            std::vector<HalfEdgeHandle> hes = face(*f_it).halfedges();

            // Delete current half-edge from face's half-edge list
            hes.erase(std::remove(hes.begin(), hes.end(), halfedge_handle(_h, 0)), hes.end());
            hes.erase(std::remove(hes.begin(), hes.end(), halfedge_handle(_h, 1)), hes.end());

            std::for_each(hes.begin(), hes.end(),
623
                          fun::bind(&HEHandleCorrection::correctValue, &cor, fun::placeholders::_1));
624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
            face(*f_it).set_halfedges(hes);
        }
    } else {

        // Iterate over all faces
        for(FaceIter f_it = faces_begin(), f_end = faces_end();
                f_it != f_end; ++f_it) {

            std::vector<HalfEdgeHandle> hes = face(*f_it).halfedges();
            if(std::find(hes.begin(), hes.end(), halfedge_handle(_h, 0)) != hes.end() ||
                    std::find(hes.begin(), hes.end(), halfedge_handle(_h, 1)) != hes.end()) {
                // Face is incident to current edge
                incident_faces.push(*f_it);
                continue;
            }

            // Delete current half-edge from face's half-edge list
            hes.erase(std::remove(hes.begin(), hes.end(), halfedge_handle(_h, 0)), hes.end());
            hes.erase(std::remove(hes.begin(), hes.end(), halfedge_handle(_h, 1)), hes.end());

            // Decrease all half-edge handles greater than _h in face
            HEHandleCorrection cor(halfedge_handle(_h, 1));
            std::for_each(hes.begin(), hes.end(),
647
                          fun::bind(&HEHandleCorrection::correctValue, &cor, fun::placeholders::_1));
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
            face(*f_it).set_halfedges(hes);
        }
    }

    // 3)
    if(e_bottom_up_) {
        assert(incident_hfs_per_he_.size() > (unsigned int)halfedge_handle(_h, 1).idx());
        incident_hfs_per_he_.erase(incident_hfs_per_he_.begin() + halfedge_handle(_h, 1).idx());
        incident_hfs_per_he_.erase(incident_hfs_per_he_.begin() + halfedge_handle(_h, 0).idx());
    }

    // 4)
    if(v_bottom_up_) {
        HEHandleCorrection cor(halfedge_handle(_h, 1));
        std::for_each(outgoing_hes_per_vertex_.begin(),
                      outgoing_hes_per_vertex_.end(),
664
                      fun::bind(&HEHandleCorrection::correctVecValue, &cor, fun::placeholders::_1));
665 666 667 668
    }

    // 5)
    edges_.erase(edges_.begin() + _h.idx());
669
    --n_edges_;
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708

    // 6)
    edge_deleted(_h);

    // 7)
    while(!incident_faces.empty()) {
        delete_face(incident_faces.top());
        incident_faces.pop();
    }

    // Return iterator to next element in list
    return (edges_begin() + _h.idx());
}

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

/**
 * \brief Delete face from mesh
 *
 * After performing this operation, all faces
 * following face _h in the array will be accessible
 * through their old handle decreased by one.
 * This function directly fixes the face links
 * in all cells. These steps are performed:
 *
 * 1) Delete links in BU: HE -> HF
 * 2) Search all incident cells +
 *    decrease all half-face handles > hf
 *    in all incident cells
 * 3) Delete item in BU: HF -> C
 * 4) Decrease all entries > hf in BU: HE -> HF
 * 5) Delete face from storage array
 * 6) Delete property item
 * 7) Delete incident cells
 *
 * @param _h A face handle
 */
FaceIter TopologyKernel::delete_face(const FaceHandle& _h) {

709
    assert(_h.idx() < (int)n_faces_);
710 711 712 713 714 715 716 717

    // 1)
    if(e_bottom_up_) {

        const std::vector<HalfEdgeHandle>& hes = face(_h).halfedges();
        for(std::vector<HalfEdgeHandle>::const_iterator he_it = hes.begin(),
                he_end = hes.end(); he_it != he_end; ++he_it) {

718
            assert(incident_hfs_per_he_.size() > (unsigned int)std::max(he_it->idx(), opposite_halfedge_handle(*he_it).idx()));
719

720 721 722 723
            incident_hfs_per_he_[he_it->idx()].erase(
                    std::remove(incident_hfs_per_he_[he_it->idx()].begin(),
                                incident_hfs_per_he_[he_it->idx()].end(),
                                halfface_handle(_h, 0)), incident_hfs_per_he_[he_it->idx()].end());
724 725


726 727 728 729
            incident_hfs_per_he_[opposite_halfedge_handle(*he_it).idx()].erase(
                    std::remove(incident_hfs_per_he_[opposite_halfedge_handle(*he_it).idx()].begin(),
                                incident_hfs_per_he_[opposite_halfedge_handle(*he_it).idx()].end(),
                                halfface_handle(_h, 1)), incident_hfs_per_he_[opposite_halfedge_handle(*he_it).idx()].end());
730 731 732 733 734 735 736 737 738
        }
    }

    // 2)
    std::priority_queue<CellHandle> incident_cells;
    if(f_bottom_up_) {

        // Speed-up, since we already know all incident cells
        // Get incident cells for deletion
739
        assert(incident_cell_per_hf_.size() > (unsigned int)halfface_handle(_h, 1).idx());
740 741
        if(incident_cell_per_hf_[halfface_handle(_h, 0).idx()].is_valid()) {
            incident_cells.push(incident_cell_per_hf_[halfface_handle(_h, 0).idx()]);
742
        }
743 744
        if(incident_cell_per_hf_[halfface_handle(_h, 1).idx()].is_valid()) {
            incident_cells.push(incident_cell_per_hf_[halfface_handle(_h, 1).idx()]);
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
        }

        // Decrease all half-face handles > _h in all cells
        std::set<CellHandle> update_cells;
        for(std::vector<CellHandle>::const_iterator c_it = (incident_cell_per_hf_.begin() + halfface_handle(_h, 1).idx() + 1),
                c_end = incident_cell_per_hf_.end(); c_it != c_end; ++c_it) {
            if(!c_it->is_valid()) continue;
            update_cells.insert(*c_it);
        }
        for(std::set<CellHandle>::const_iterator c_it = update_cells.begin(),
                c_end = update_cells.end(); c_it != c_end; ++c_it) {

            std::vector<HalfFaceHandle> hfs = cell(*c_it).halffaces();

            // Delete current half-faces from cell's half-face list
            hfs.erase(std::remove(hfs.begin(), hfs.end(), halfface_handle(_h, 0)), hfs.end());
            hfs.erase(std::remove(hfs.begin(), hfs.end(), halfface_handle(_h, 1)), hfs.end());

            HFHandleCorrection cor(halfface_handle(_h, 1));
            std::for_each(hfs.begin(), hfs.end(),
765
                          fun::bind(&HFHandleCorrection::correctValue, &cor, fun::placeholders::_1));
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
            cell(*c_it).set_halffaces(hfs);
        }

    } else {

        // Iterate over all cells
        for(CellIter c_it = cells_begin(), c_end = cells_end(); c_it != c_end; ++c_it) {

            std::vector<HalfFaceHandle> hfs = cell(*c_it).halffaces();
            if(std::find(hfs.begin(), hfs.end(), halfface_handle(_h, 0)) != hfs.end() ||
                    std::find(hfs.begin(), hfs.end(), halfface_handle(_h, 1)) != hfs.end()) {
                // Delete cell
                incident_cells.push(*c_it);
                continue;
            }
            // Delete current half-faces from cell's half-face list
            hfs.erase(std::remove(hfs.begin(), hfs.end(), halfface_handle(_h, 0)), hfs.end());
            hfs.erase(std::remove(hfs.begin(), hfs.end(), halfface_handle(_h, 1)), hfs.end());

            HFHandleCorrection cor(halfface_handle(_h, 1));
            std::for_each(hfs.begin(), hfs.end(),
787
                          fun::bind(&HFHandleCorrection::correctValue, &cor, fun::placeholders::_1));
788 789 790 791 792 793
            cell(*c_it).set_halffaces(hfs);
        }
    }

    // 3)
    if(f_bottom_up_) {
794
        assert(incident_cell_per_hf_.size() > (unsigned int)halfface_handle(_h, 1).idx());
795 796 797 798 799 800 801 802 803
        incident_cell_per_hf_.erase(incident_cell_per_hf_.begin() + halfface_handle(_h, 1).idx());
        incident_cell_per_hf_.erase(incident_cell_per_hf_.begin() + halfface_handle(_h, 0).idx());
    }

    // 4)
    if(e_bottom_up_) {
        HFHandleCorrection cor(halfface_handle(_h, 1));
        std::for_each(incident_hfs_per_he_.begin(),
                      incident_hfs_per_he_.end(),
804
                      fun::bind(&HFHandleCorrection::correctVecValue, &cor, fun::placeholders::_1));
805 806 807 808
    }

    // 5)
    faces_.erase(faces_.begin() + _h.idx());
809
    --n_faces_;
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842

    // 6)
    face_deleted(_h);

    // 7)
    while(!incident_cells.empty()) {
        delete_cell(incident_cells.top());
        incident_cells.pop();
    }

    // Return iterator to next element in list
    return (faces_begin() + _h.idx());
}

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

/**
 * \brief Delete cell from mesh
 *
 * After performing this operation, all cells
 * following cell _h in the array will be accessible
 * through their old handle decreased by one.
 * These steps are performed:
 *
 * 1) Delete links in BU: HF -> C
 * 2) Decrease all entries > c in BU: HF -> C
 * 3) Delete cell from storage array
 * 4) Delete property item
 *
 * @param _h A cell handle
 */
CellIter TopologyKernel::delete_cell(const CellHandle& _h) {

843
    assert(_h.idx() < (int)n_cells_);
844 845 846 847 848 849 850 851

    // 1)
    if(f_bottom_up_) {
        const std::vector<HalfFaceHandle>& hfs = cell(_h).halffaces();
        for(std::vector<HalfFaceHandle>::const_iterator hf_it = hfs.begin(),
                hf_end = hfs.end(); hf_it != hf_end; ++hf_it) {
            assert(incident_cell_per_hf_.size() > (unsigned int)hf_it->idx());

852
            incident_cell_per_hf_[hf_it->idx()] = InvalidCellHandle;
853 854 855 856 857 858 859 860
        }
    }

    // 2)
    if(f_bottom_up_) {
        CHandleCorrection cor(_h);
        std::for_each(incident_cell_per_hf_.begin(),
                      incident_cell_per_hf_.end(),
861
                      fun::bind(&CHandleCorrection::correctValue, &cor, fun::placeholders::_1));
862 863 864 865
    }

    // 3)
    cells_.erase(cells_.begin() + _h.idx());
866
    --n_cells_;
867 868 869 870 871

    // 4)
    cell_deleted(_h);

    return (cells_begin() + _h.idx());
872 873 874 875 876 877 878 879
}

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

/// Get edge with handle _edgeHandle
const OpenVolumeMeshEdge& TopologyKernel::edge(const EdgeHandle& _edgeHandle) const {

    // Test if edge is valid
880 881
    assert((unsigned int)_edgeHandle.idx() < n_edges_);
    assert(_edgeHandle.idx() >= 0);
882

883
    return edges_[_edgeHandle.idx()];
884 885 886 887 888 889 890 891
}

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

/// Get face with handle _faceHandle
const OpenVolumeMeshFace& TopologyKernel::face(const FaceHandle& _faceHandle) const {

    // Test if face is valid
892 893
    assert((unsigned int)_faceHandle.idx() < n_faces_);
    assert(_faceHandle.idx() >= 0);
894

895
    return faces_[_faceHandle.idx()];
896 897 898 899 900 901 902 903
}

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

/// Get cell with handle _cellHandle
const OpenVolumeMeshCell& TopologyKernel::cell(const CellHandle& _cellHandle) const {

    // Test if cell is valid
904 905
    assert((unsigned int)_cellHandle.idx() < n_cells_);
    assert(_cellHandle.idx() >= 0);
906

907
    return cells_[_cellHandle.idx()];
908 909 910 911 912 913 914 915
}

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

/// Get edge with handle _edgeHandle
OpenVolumeMeshEdge& TopologyKernel::edge(const EdgeHandle& _edgeHandle) {

    // Test if edge is valid
916 917
    assert((unsigned int)_edgeHandle.idx() < n_edges_);
    assert(_edgeHandle.idx() >= 0);
918

919
    return edges_[_edgeHandle.idx()];
920 921 922 923 924 925 926 927
}

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

/// Get face with handle _faceHandle
OpenVolumeMeshFace& TopologyKernel::face(const FaceHandle& _faceHandle) {

    // Test if face is valid
928 929
    assert((unsigned int)_faceHandle.idx() < n_faces_);
    assert(_faceHandle.idx() >= 0);
930

931
    return faces_[_faceHandle.idx()];
932 933 934 935 936 937 938 939
}

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

/// Get cell with handle _cellHandle
OpenVolumeMeshCell& TopologyKernel::cell(const CellHandle& _cellHandle) {

    // Test if cell is valid
940 941
    assert((unsigned int)_cellHandle.idx() < n_cells_);
    assert(_cellHandle.idx() >= 0);
942

943
    return cells_[_cellHandle.idx()];
944 945 946 947 948 949 950 951
}

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

/// Get edge that corresponds to halfedge with handle _halfEdgeHandle
const OpenVolumeMeshEdge TopologyKernel::halfedge(const HalfEdgeHandle& _halfEdgeHandle) const {

    // Is handle in range?
952 953
    assert((unsigned int)_halfEdgeHandle.idx() < (n_edges_ * 2));
    assert(_halfEdgeHandle.idx() >= 0);
954 955 956

    // In case the handle is even, just return the corresponding edge
    /// Otherwise return the opposite halfedge via opposite()
957 958
    if(_halfEdgeHandle.idx() % 2 == 0)
        return edges_[(int)(_halfEdgeHandle.idx() / 2)];
959
    else
960
        return opposite_halfedge(edges_[(int)(_halfEdgeHandle.idx() / 2)]);
961 962 963 964 965 966 967 968
}

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

/// Get face that corresponds to halfface with handle _halfFaceHandle
const OpenVolumeMeshFace TopologyKernel::halfface(const HalfFaceHandle& _halfFaceHandle) const {

    // Is handle in range?
969 970
    assert((unsigned int)_halfFaceHandle.idx() < (n_faces_ * 2));
    assert(_halfFaceHandle.idx() >= 0);
971 972 973

    // In case the handle is not even, just return the corresponding face
    // Otherwise return the opposite halfface via opposite()
974 975
    if(_halfFaceHandle.idx() % 2 == 0)
        return faces_[(int)(_halfFaceHandle.idx() / 2)];
976
    else
977
        return opposite_halfface(faces_[(int)(_halfFaceHandle.idx() / 2)]);
978 979 980 981 982 983 984 985
}

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

/// Get opposite halfedge that corresponds to halfedge with handle _halfEdgeHandle
const OpenVolumeMeshEdge TopologyKernel::opposite_halfedge(const HalfEdgeHandle& _halfEdgeHandle) const {

    // Is handle in range?
986 987
    assert(_halfEdgeHandle.idx() >= 0);
    assert((unsigned int)_halfEdgeHandle.idx() < (n_edges_ * 2));
988 989 990

    // In case the handle is not even, just return the corresponding edge
    // Otherwise return the opposite halfedge via opposite()
991 992
    if(_halfEdgeHandle.idx() % 2 != 0)
        return edges_[(int)(_halfEdgeHandle.idx() / 2)];
993
    else
994
        return opposite_halfedge(edges_[(int)(_halfEdgeHandle.idx() / 2)]);
995 996 997 998 999 1000 1001 1002
}

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

/// Get opposite halfface that corresponds to halfface with handle _halfFaceHandle
const OpenVolumeMeshFace TopologyKernel::opposite_halfface(const HalfFaceHandle& _halfFaceHandle) const {

    // Is handle in range?
1003 1004
    assert(_halfFaceHandle.idx() >= 0);
    assert((unsigned int)_halfFaceHandle.idx() < (n_faces_ * 2));
1005 1006 1007

    // In case the handle is not even, just return the corresponding face
    // Otherwise return the opposite via the first face's opposite() function
1008 1009
    if(_halfFaceHandle.idx() % 2 != 0)
        return faces_[(int)(_halfFaceHandle.idx() / 2)];
1010
    else
1011
        return opposite_halfface(faces_[(int)(_halfFaceHandle.idx() / 2)]);
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
}

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

const HalfEdgeHandle TopologyKernel::halfedge(const VertexHandle& _vh1, const VertexHandle& _vh2) const {

    assert(_vh1.idx() < (int)n_vertices());
    assert(_vh2.idx() < (int)n_vertices());

    for(VertexOHalfEdgeIter voh_it = voh_iter(_vh1); voh_it.valid(); ++voh_it) {
        if(halfedge(*voh_it).to_vertex() == _vh2) {
            return *voh_it;
        }
    }

    return InvalidHalfEdgeHandle;
}

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

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
const HalfFaceHandle TopologyKernel::halfface(const std::vector<VertexHandle>& _vs) const {

    assert(_vs.size() > 2);

    VertexHandle v0 = _vs[0], v1 = _vs[1], v2 = _vs[2];

    assert(v0.is_valid() && v1.is_valid() && v2.is_valid());

    HalfEdgeHandle he0 = halfedge(v0, v1);
    if(!he0.is_valid()) return InvalidHalfFaceHandle;
    HalfEdgeHandle he1 = halfedge(v1, v2);
    if(!he1.is_valid()) return InvalidHalfFaceHandle;

    std::vector<HalfEdgeHandle> hes;
    hes.push_back(he0);
    hes.push_back(he1);

    return halfface(hes);
}

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

const HalfFaceHandle TopologyKernel::halfface(const std::vector<HalfEdgeHandle>& _hes) const {

    assert(_hes.size() >= 2);

    HalfEdgeHandle he0 = _hes[0], he1 = _hes[1];

    assert(he0.is_valid() && he1.is_valid());

    for(HalfEdgeHalfFaceIter hehf_it = hehf_iter(he0); hehf_it.valid(); ++hehf_it) {

        std::vector<HalfEdgeHandle> hes = halfface(*hehf_it).halfedges();
        if(std::find(hes.begin(), hes.end(), he1) != hes.end()) {
            return *hehf_it;
        }
    }

    return InvalidHalfFaceHandle;
}

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

1075 1076
const HalfEdgeHandle TopologyKernel::next_halfedge_in_halfface(const HalfEdgeHandle& _heh, const HalfFaceHandle& _hfh) const {

1077 1078
    assert((unsigned int)_hfh.idx() < n_faces_ * 2u);
    assert((unsigned int)_heh.idx() < n_edges_ * 2u);
1079 1080 1081

    std::vector<HalfEdgeHandle> hes = halfface(_hfh).halfedges();

1082
    for(std::vector<HalfEdgeHandle>::const_iterator it = hes.begin();
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
            it != hes.end(); ++it) {
        if(*it == _heh) {
            if((it + 1) != hes.end()) return *(it + 1);
            else return *hes.begin();
        }
    }

    return InvalidHalfEdgeHandle;
}

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

const HalfEdgeHandle TopologyKernel::prev_halfedge_in_halfface(const HalfEdgeHandle& _heh, const HalfFaceHandle& _hfh) const {

1097 1098
    assert((unsigned int)_hfh.idx() < n_faces_ * 2u);
    assert((unsigned int)_heh.idx() < n_edges_ * 2u);
1099 1100 1101

    std::vector<HalfEdgeHandle> hes = halfface(_hfh).halfedges();

1102
    for(std::vector<HalfEdgeHandle>::const_iterator it = hes.begin();
1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
            it != hes.end(); ++it) {
        if(*it == _heh) {
            if(it != hes.begin()) return *(it - 1);
            else return *(hes.end() - 1);
        }
    }

    return InvalidHalfEdgeHandle;
}

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

1115 1116
HalfFaceHandle
TopologyKernel::adjacent_halfface_in_cell(const HalfFaceHandle& _halfFaceHandle, const HalfEdgeHandle& _halfEdgeHandle) const {
1117

1118
    if((unsigned int)_halfFaceHandle.idx() >= incident_cell_per_hf_.size() || _halfFaceHandle.idx() < 0) {
1119 1120 1121 1122 1123
        return InvalidHalfFaceHandle;
    }
    if(!has_face_bottom_up_adjacencies()) {
        return InvalidHalfFaceHandle;
    }
1124
    if(incident_cell_per_hf_[_halfFaceHandle.idx()] == InvalidCellHandle) {
1125 1126 1127
        // Specified halfface is on the outside of the complex
        return InvalidHalfFaceHandle;
    }
1128

1129
    OpenVolumeMeshCell c = cell(incident_cell_per_hf_[_halfFaceHandle.idx()]);
1130

1131 1132 1133 1134 1135 1136
    // Make sure that _halfFaceHandle is incident to _halfEdgeHandle
    bool skipped = false;
    bool found = false;
    HalfFaceHandle idx = InvalidHalfFaceHandle;
    for(std::vector<HalfFaceHandle>::const_iterator hf_it = c.halffaces().begin();
            hf_it != c.halffaces().end(); ++hf_it) {
1137

1138 1139 1140 1141
        if(*hf_it == _halfFaceHandle) {
            skipped = true;
            continue;
        }
1142

1143 1144 1145
        OpenVolumeMeshFace hf = halfface(*hf_it);
        for(std::vector<HalfEdgeHandle>::const_iterator he_it = hf.halfedges().begin();
            he_it != hf.halfedges().end(); ++he_it) {
1146

1147 1148 1149 1150 1151 1152 1153 1154 1155 1156
            if(edge_handle(*he_it) == edge_handle(_halfEdgeHandle)) {
                found = true;
                idx = *hf_it;
            }
            if(skipped && found) break;
        }
        if(skipped && found) break;
    }
    return ((skipped && found) ? idx : InvalidHalfFaceHandle);
}
1157

1158
//========================================================================================
1159

1160
CellHandle TopologyKernel::incident_cell(const HalfFaceHandle& _halfFaceHandle) const {
1161

1162 1163 1164
    if(!has_face_bottom_up_adjacencies()) {
        return InvalidCellHandle;
    }
1165
    if((unsigned int)_halfFaceHandle.idx() >= incident_cell_per_hf_.size() || _halfFaceHandle.idx() < 0) {
1166
        return InvalidCellHandle;
1167
    }
1168

1169
    return incident_cell_per_hf_[_halfFaceHandle.idx()];
1170 1171 1172 1173
}

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

1174
void TopologyKernel::compute_vertex_bottom_up_adjacencies() {
1175 1176 1177 1178 1179 1180

    // Clear adjacencies
    outgoing_hes_per_vertex_.clear();
    outgoing_hes_per_vertex_.resize(n_vertices());

    // Store outgoing halfedges per vertex
1181
    unsigned int n_vertices = n_edges_;
1182 1183 1184
    for(unsigned int i = 0; i < n_vertices; ++i) {

        VertexHandle from = edges_[i].from_vertex();
1185
        if((unsigned int)from.idx() >= outgoing_hes_per_vertex_.size()) {
1186 1187 1188
            std::cerr << "update_adjacencies(): Vertex handle is out of bounds!" << std::endl;
            return;
        }
1189
        outgoing_hes_per_vertex_[from.idx()].push_back(halfedge_handle(EdgeHandle(i), 0));
1190 1191

        VertexHandle to = edges_[i].to_vertex();
1192
        if((unsigned int)to.idx() >= outgoing_hes_per_vertex_.size()) {
1193 1194 1195 1196
            std::cerr << "update_adjacencies(): Vertex handle is out of bounds!" << std::endl;
            return;
        }
        // Store opposite halfedge handle
1197
        outgoing_hes_per_vertex_[to.idx()].push_back(halfedge_handle(EdgeHandle(i), 1));
1198 1199 1200 1201 1202
    }
}

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

1203
void TopologyKernel::compute_edge_bottom_up_adjacencies() {
1204 1205 1206

    // Clear
    incident_hfs_per_he_.clear();
1207
    incident_hfs_per_he_.resize(n_edges_ * 2u);
1208 1209

    // Store incident halffaces per halfedge
1210
    unsigned int n_faces = n_faces_;
1211 1212 1213 1214 1215 1216 1217 1218
    for(unsigned int i = 0; i < n_faces; ++i) {

        std::vector<HalfEdgeHandle> halfedges = faces_[i].halfedges();

        // Go over all halfedges
        for(std::vector<HalfEdgeHandle>::const_iterator he_it = halfedges.begin();
                he_it != halfedges.end(); ++he_it) {

1219 1220
            incident_hfs_per_he_[he_it->idx()].push_back(halfface_handle(FaceHandle(i), 0));
            incident_hfs_per_he_[opposite_halfedge_handle(*he_it).idx()].push_back(
1221 1222 1223
                    halfface_handle(FaceHandle(i), 1));
        }
    }
1224 1225 1226 1227
}

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

1228
void TopologyKernel::compute_face_bottom_up_adjacencies() {
1229 1230 1231

    // Clear
    incident_cell_per_hf_.clear();
1232
    incident_cell_per_hf_.resize(n_faces_ * 2u, InvalidCellHandle);
1233

1234
    unsigned int n_cells = n_cells_;