TopologyKernel.hh 24.2 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
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
/*===========================================================================*\
 *                                                                           *
 *                            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$                                                *
 *                                                                           *
\*===========================================================================*/

#ifndef TOPOLOGYKERNEL_HH_
#define TOPOLOGYKERNEL_HH_

#include <set>
#include <vector>
#include <iostream>
#include <cassert>

#include "BaseEntities.hh"
#include "OpenVolumeMeshHandle.hh"
#include "ResourceManager.hh"
#include "Iterators.hh"

namespace OpenVolumeMesh {

class TopologyKernel : public ResourceManager {
public:

    TopologyKernel();
    virtual ~TopologyKernel();

    /*
     * Defines and constants
     */

    static const VertexHandle   InvalidVertexHandle;
    static const EdgeHandle     InvalidEdgeHandle;
    static const FaceHandle     InvalidFaceHandle;
    static const CellHandle     InvalidCellHandle;
    static const HalfEdgeHandle InvalidHalfEdgeHandle;
    static const HalfFaceHandle InvalidHalfFaceHandle;

    typedef OpenVolumeMeshEdge Edge;
    typedef OpenVolumeMeshFace Face;
    typedef OpenVolumeMeshCell Cell;

    //=====================================================================
    // Iterators
    //=====================================================================

    friend class VertexOHalfEdgeIter;
    friend class HalfEdgeHalfFaceIter;
    friend class VertexCellIter;
    friend class HalfEdgeCellIter;
    friend class CellVertexIter;
    friend class CellCellIter;
    friend class BoundaryFaceIter;
    friend class VertexIter;
    friend class EdgeIter;
    friend class HalfEdgeIter;
    friend class FaceIter;
    friend class HalfFaceIter;
    friend class CellIter;

    VertexOHalfEdgeIter voh_iter(const VertexHandle& _h) const {
        return VertexOHalfEdgeIter(_h, this);
    }

    HalfEdgeHalfFaceIter hehf_iter(const HalfEdgeHandle& _h) const {
        return HalfEdgeHalfFaceIter(_h, this);
    }

    VertexCellIter vc_iter(const VertexHandle& _h) const {
        return VertexCellIter(_h, this);
    }

    HalfEdgeCellIter hec_iter(const HalfEdgeHandle& _h) const {
        return HalfEdgeCellIter(_h, this);
    }

    CellVertexIter cv_iter(const CellHandle& _h) const {
        return CellVertexIter(_h, this);
    }

    CellCellIter cc_iter(const CellHandle& _h) const {
        return CellCellIter(_h, this);
    }

    BoundaryFaceIter bf_iter() const {
        return BoundaryFaceIter(this);
    }

    VertexIter v_iter() const {
        return VertexIter(this);
    }

    VertexIter vertices_begin() const {
        return VertexIter(this, VertexHandle(0));
    }

    VertexIter vertices_end() const {
        return VertexIter(this, VertexHandle(n_vertices()));
    }

    EdgeIter e_iter() const {
        return EdgeIter(this);
    }

    EdgeIter edges_begin() const {
        return EdgeIter(this, EdgeHandle(0));
    }

    EdgeIter edges_end() const {
        return EdgeIter(this, EdgeHandle(edges_.size()));
    }

    HalfEdgeIter he_iter() const {
        return HalfEdgeIter(this);
    }

    HalfEdgeIter halfedges_begin() const {
        return HalfEdgeIter(this, HalfEdgeHandle(0));
    }

    HalfEdgeIter halfedges_end() const {
        return HalfEdgeIter(this, HalfEdgeHandle(edges_.size() * 2));
    }

    FaceIter f_iter() const {
        return FaceIter(this);
    }

    FaceIter faces_begin() const {
        return FaceIter(this, FaceHandle(0));
    }

    FaceIter faces_end() const {
        return FaceIter(this, FaceHandle(faces_.size()));
    }

    HalfFaceIter hf_iter() const {
        return HalfFaceIter(this);
    }

    HalfFaceIter halffaces_begin() const {
        return HalfFaceIter(this, HalfFaceHandle(0));
    }

    HalfFaceIter halffaces_end() const {
        return HalfFaceIter(this, HalfFaceHandle(faces_.size() * 2));
    }

    CellIter c_iter() const {
        return CellIter(this);
    }

    CellIter cells_begin() const {
        return CellIter(this, CellHandle(0));
    }

    CellIter cells_end() const {
        return CellIter(this, CellHandle(cells_.size()));
    }

    /*
     * Virtual functions with implementation
     */

    /// Get number of vertices in mesh
    virtual unsigned int n_vertices()   const { return n_vertices_; }
    /// Get number of edges in mesh
    virtual unsigned int n_edges()      const { return edges_.size(); }
    /// Get number of halfedges in mesh
    virtual unsigned int n_halfedges()  const { return (2u * edges_.size()); }
    /// Get number of faces in mesh
    virtual unsigned int n_faces()      const { return faces_.size(); }
    /// Get number of halffaces in mesh
    virtual unsigned int n_halffaces()  const { return (2u * faces_.size()); }
    /// Get number of cells in mesh
    virtual unsigned int n_cells()      const { return cells_.size(); }

    /// Add abstract vertex
    virtual VertexHandle add_vertex() {

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

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

    /// Add edge
    virtual EdgeHandle add_edge(const VertexHandle& _fromVertex, const VertexHandle& _toHandle);

    /// Add face via incident edges
    virtual FaceHandle add_face(const std::vector<HalfEdgeHandle>& _halfedges, bool _topologyCheck = true);

    /// Add face via incident vertices
    virtual FaceHandle add_face(const std::vector<VertexHandle>& _vertices);

    /// Add cell via incident halffaces
    virtual CellHandle add_cell(const std::vector<HalfFaceHandle>& _halffaces, bool _topologyCheck = true);

    /*
     * Non-virtual functions
     */

    /// Get edge with handle _edgeHandle
    const Edge& edge(const EdgeHandle& _edgeHandle) const;

    /// Get face with handle _faceHandle
    const Face& face(const FaceHandle& _faceHandle) const;

    /// Get cell with handle _cellHandle
    const Cell& cell(const CellHandle& _cellHandle) const;

    /// Get edge with handle _edgeHandle
    Edge& edge(const EdgeHandle& _edgeHandle);

    /// Get face with handle _faceHandle
    Face& face(const FaceHandle& _faceHandle);

    /// Get cell with handle _cellHandle
    Cell& cell(const CellHandle& _cellHandle);

    /// Get edge that corresponds to halfedge with handle _halfEdgeHandle
    const Edge halfedge(const HalfEdgeHandle& _halfEdgeHandle) const;

    /// Get face that corresponds to halfface with handle _halfFaceHandle
    const Face halfface(const HalfFaceHandle& _halfFaceHandle) const;

    /// Get opposite halfedge that corresponds to halfedge with handle _halfEdgeHandle
    const Edge opposite_halfedge(const HalfEdgeHandle& _halfEdgeHandle) const;

    /// Get opposite halfface that corresponds to halfface with handle _halfFaceHandle
    const Face opposite_halfface(const HalfFaceHandle& _halfFaceHandle) const;

    // Get halfedge from vertex _vh1 to _vh2
    const HalfEdgeHandle halfedge(const VertexHandle& _vh1, const VertexHandle& _vh2) const;

    /// Get next halfedge within a halfface
    const HalfEdgeHandle next_halfedge_in_halfface(const HalfEdgeHandle& _heh, const HalfFaceHandle& _hfh) const;

    /// Get previous halfedge within a halfface
    const HalfEdgeHandle prev_halfedge_in_halfface(const HalfEdgeHandle& _heh, const HalfFaceHandle& _hfh) const;

    /// Get valence of vertex (number of incident edges)
    inline unsigned int valence(const VertexHandle& _vh) const {
        if(!has_vertex_adjacencies_) {
            std::cerr << "Could not get vertex valence: No bottom-up adjacencies for vertices available!" << std::endl;
            return 0u;
        }
        assert((unsigned int)_vh < outgoing_hes_per_vertex_.size());
        return outgoing_hes_per_vertex_[_vh.idx()].size();
    }

    /// Get valence of edge (number of incident faces)
    inline unsigned int valence(const EdgeHandle& _eh) const {
        if(!has_edge_adjacencies_) {
            std::cerr << "Could not get edge valence: No bottom-up adjacencies for edges available!" << std::endl;
            return 0u;
        }
        assert((unsigned int)halfedge_handle(_eh, 0) < incident_hfs_per_he_.size());
        return incident_hfs_per_he_[halfedge_handle(_eh, 0)].size();
    }

    /// Get valence of face (number of incident edges)
    inline unsigned int valence(const FaceHandle& _fh) const {

        assert((unsigned int)_fh < faces_.size());
        return face(_fh).halfedges().size();
    }

    /// Get valence of cell (number of incident faces)
    inline unsigned int valence(const CellHandle& _ch) const {

        assert((unsigned int)_ch < cells_.size());
        return cell(_ch).halffaces().size();
    }

    /**
     * \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. This invalidates all bottom-up
     * adjacencies. See class StatusAttrib that
     * provides a proper garbage collection.
     *
     * @param _h A vertex handle
     */
    virtual VertexIter delete_vertex(const VertexHandle& _h) {
        assert(_h.idx() < (int)n_vertices());
        --n_vertices_;

        for(EdgeIter e_it = edges_begin(); e_it != edges_end();) {
            if(edge(*e_it).to_vertex() == _h || edge(*e_it).from_vertex() == _h) {
                e_it = delete_edge(*e_it);
            } else {
                if(edge(*e_it).to_vertex().idx() > _h.idx()) {
                    edge(*e_it).set_to_vertex(VertexHandle(edge(*e_it).to_vertex() - 1));
                }
                if(edge(*e_it).from_vertex().idx() > _h.idx()) {
                    edge(*e_it).set_from_vertex(VertexHandle(edge(*e_it).from_vertex() - 1));
                }
                 ++e_it;
            }
        }

        // Remove property element
        vertex_deleted(_h);

        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. This invalidates all bottom-up
     * adjacencies. See class StatusAttrib that
     * provides a proper garbage collection.
     *
     * @param _h An edge handle
     */
    virtual EdgeIter delete_edge(const EdgeHandle& _h) {
        assert(_h.idx() < (int)edges_.size());

        edges_.erase(edges_.begin() + _h.idx());

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

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

            bool deleted = false;
            for(std::vector<HalfEdgeHandle>::iterator he_it = hes.begin();
                    he_it != hes.end(); ++he_it) {
                if(edge_handle(*he_it) == _h) {
                    f_it = delete_face(*f_it);
                    deleted = true;
                    break;
                } else if(edge_handle(*he_it).idx() > _h.idx()) {
                    *he_it = HalfEdgeHandle(he_it->idx() - 2);
                }
            }
            if(!deleted) {
                face(*f_it).set_halfedges(hes);
                ++f_it;
            }
        }

        // Remove property element
        edge_deleted(_h);

        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. This invalidates all bottom-up
     * adjacencies. See class StatusAttrib that
     * provides a proper garbage collection.
     *
     * @param _h A face handle
     */
    virtual FaceIter delete_face(const FaceHandle& _h) {
        assert(_h.idx() < (int)faces_.size());

        faces_.erase(faces_.begin() + _h.idx());

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

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

            bool deleted = false;
            for(std::vector<HalfFaceHandle>::iterator hf_it = hfs.begin();
                    hf_it != hfs.end(); ++hf_it) {
                if(face_handle(*hf_it) == _h) {
                    c_it = delete_cell(*c_it);
                    deleted = true;
                    break;
                } else if(face_handle(*hf_it).idx() > _h.idx()) {
                    *hf_it = HalfFaceHandle(hf_it->idx() - 2);
                }
            }
            if(!deleted) {
                cell(*c_it).set_halffaces(hfs);
                ++c_it;
            }
        }

        // Remove property element
        face_deleted(_h);

        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.
     * This invalidates all bottom-up
     * adjacencies. See class StatusAttrib that
     * provides a proper garbage collection.
     *
     * @param _h A cell handle
     */
    virtual CellIter delete_cell(const CellHandle& _h) {
        assert(_h.idx() < (int)cells_.size());

        cells_.erase(cells_.begin() + _h.idx());

        // Remove property element
        cell_deleted(_h);

        return (cells_begin() + _h.idx());
    }

    /// Clear whole mesh
464
    virtual void clear(bool _clearProps = true) {
465
466
467
468
469
470
471
472
473

        edges_.clear();
        faces_.clear();
        cells_.clear();
        outgoing_hes_per_vertex_.clear();
        incident_hfs_per_he_.clear();
        incident_cell_per_hf_.clear();
        boundary_faces_.clear();

474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
        if(_clearProps) {

            // Delete all property data
            clear_vertex_props();
            clear_edge_props();
            clear_halfedge_props();
            clear_face_props();
            clear_halfface_props();
            clear_cell_props();
            clear_mesh_props();

        } else {
            // Resize props
            resize_vprops(0u);
            resize_eprops(0u);
            resize_fprops(0u);
            resize_cprops(0u);
        }
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
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
709
710
711
712
713
714
715
716
717
718
719
    }

    //=====================================================================
    // Bottom-up Adjacencies
    //=====================================================================

    void update_adjacencies();

    void update_vertex_adjacencies();

    void update_edge_adjacencies();

    void update_face_adjacencies();

    //=====================================================================
    // Connectivity
    //=====================================================================

    /// Get halfface that is adjacent (w.r.t. a common halfedge) within the same cell
    HalfFaceHandle adjacent_halfface_in_cell(const HalfFaceHandle& _halfFaceHandle, const HalfEdgeHandle& _halfEdgeHandle) const;

    /// Get cell that is incident to the given halfface
    CellHandle incident_cell(const HalfFaceHandle& _halfFaceHandle) const;

    unsigned int n_boundary_faces() const {
        if(!has_face_adjacencies_) {
            std::cerr << "Warning: This function needs bottom-up adjacencies for faces!" << std::endl;
            return 0;
        }
        return boundary_faces_.size();
    }

    bool is_boundary(const HalfFaceHandle& _halfFaceHandle) const {
        return _halfFaceHandle.idx() >= 0 && (unsigned int)_halfFaceHandle.idx() < incident_cell_per_hf_.size() &&
                incident_cell_per_hf_[_halfFaceHandle] == InvalidCellHandle;
    }

    bool is_boundary(const FaceHandle& _faceHandle) const {
        return  is_boundary(halfface_handle(_faceHandle, 0)) ||
                is_boundary(halfface_handle(_faceHandle, 1));
    }

    bool is_boundary(const EdgeHandle& _edgeHandle) const {
        if(!has_edge_adjacencies_) {
            std::cerr << "Error: Function is_boundary() needs bottom-up adjacencies for edges!" << std::endl;
            return false;
        }
        for(HalfEdgeHalfFaceIter hehf_it = hehf_iter(halfedge_handle(_edgeHandle, 0));
                hehf_it.valid(); ++hehf_it) {
            if(is_boundary(face_handle(*hehf_it))) {
                return true;
            }
        }
        return false;
    }

    bool is_boundary(const HalfEdgeHandle& _halfedgeHandle) const {
        if(!has_edge_adjacencies_) {
            std::cerr << "Error: Function is_boundary() needs bottom-up adjacencies for edges!" << std::endl;
            return false;
        }
        for(HalfEdgeHalfFaceIter hehf_it = hehf_iter(_halfedgeHandle);
                hehf_it.valid(); ++hehf_it) {
            if(is_boundary(face_handle(*hehf_it))) {
                return true;
            }
        }
        return false;
    }

    bool is_boundary(const VertexHandle& _vertexHandle) const {
        if(!has_vertex_adjacencies_) {
            std::cerr << "Error: Function is_boundary() needs bottom-up adjacencies for vertices!" << std::endl;
            return false;
        }
        for(VertexOHalfEdgeIter voh_it = voh_iter(_vertexHandle); voh_it.valid(); ++voh_it) {
            if(is_boundary(*voh_it)) return true;
        }
        return false;
    }

    unsigned int n_vertices_in_cell(const CellHandle& _ch) const {

        std::set<VertexHandle> vertices;
        std::vector<HalfFaceHandle> hfs = cell(_ch).halffaces();
        for(std::vector<HalfFaceHandle>::const_iterator hf_it = hfs.begin();
                hf_it != hfs.end(); ++hf_it) {
            std::vector<HalfEdgeHandle> hes = halfface(*hf_it).halfedges();
            for(std::vector<HalfEdgeHandle>::const_iterator he_it = hes.begin();
                he_it != hes.end(); ++he_it) {
                vertices.insert(halfedge(*he_it).to_vertex());
            }
        }
        return vertices.size();
    }

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

    /*
     * Non-virtual functions
     */

    const Edge opposite_halfedge(const Edge& _edge) const {
        return Edge(_edge.to_vertex(), _edge.from_vertex());
    }

    const Face opposite_halfface(const Face& _face) const {

        std::vector<HalfEdgeHandle> opp_halfedges;
        for(std::vector<HalfEdgeHandle>::const_iterator it = _face.halfedges().begin(); it
                != _face.halfedges().end(); ++it) {
            opp_halfedges.insert(opp_halfedges.begin(), opposite_halfedge_handle(*it));
        }

        return Face(opp_halfedges);
    }

    /*
     * Static functions
     */

    /// Conversion function
    static inline HalfEdgeHandle halfedge_handle(const EdgeHandle& _h, const unsigned char _subIdx) {
        // Is handle in range?
        if(_h.idx() < 0 || _subIdx > 1) return InvalidHalfEdgeHandle;
        return HalfEdgeHandle((2 * _h.idx()) + (_subIdx ? 1 : 0));
    }

    /// Conversion function
    static inline HalfFaceHandle halfface_handle(const FaceHandle& _h, const unsigned char _subIdx) {
        // Is handle in range?
        if(_h.idx() < 0 || _subIdx > 1) return InvalidHalfFaceHandle;
        return HalfFaceHandle((2 * _h) + (_subIdx ? 1 : 0));
    }

    /// Handle conversion
    static inline EdgeHandle edge_handle(const HalfEdgeHandle& _h) {
        // Is handle in range?
        if(_h.idx() < 0) return InvalidEdgeHandle;
        return EdgeHandle((int)(_h.idx() / 2));
    }

    static inline FaceHandle face_handle(const HalfFaceHandle& _h) {
        // Is handle in range?
        if(_h.idx() < 0) return InvalidFaceHandle;
        return FaceHandle((int)(_h.idx() / 2));
    }

    static inline HalfEdgeHandle opposite_halfedge_handle(const HalfEdgeHandle& _h) {
        // Is handle in range?
        if(_h.idx() < 0) return InvalidHalfEdgeHandle;

        // Is handle even?
        if(_h.idx() % 2 == 0) {
            return HalfEdgeHandle(_h.idx() + 1);
        }
        return HalfEdgeHandle(_h.idx() - 1);
    }

    static inline HalfFaceHandle opposite_halfface_handle(const HalfFaceHandle& _h) {
        // Is handle in range?
        if(_h.idx() < 0) return InvalidHalfFaceHandle;

        // Is handle even?
        if(_h.idx() % 2 == 0) {
            return HalfFaceHandle(_h.idx() + 1);
        }
        return HalfFaceHandle(_h.idx() - 1);
    }

    inline bool has_vertex_adjacencies() const { return has_vertex_adjacencies_; }

    inline bool has_edge_adjacencies() const { return has_edge_adjacencies_; }

    inline bool has_face_adjacencies() const { return has_face_adjacencies_; }

    inline bool has_bottom_up_adjacencies() const {
        return (has_vertex_adjacencies_ && has_edge_adjacencies_ && has_face_adjacencies_);
    }

protected:

    // Number of (abstract) vertices
    unsigned int n_vertices_;

    // List of edges
    std::vector<Edge> edges_;

    // List of faces
    std::vector<Face> faces_;

    // List of cells
    std::vector<Cell> cells_;

private:

    //=======================
    // Bottom-up-adjacencies
    //=======================

    // Outgoing halfedges per vertex
    std::vector<std::vector<HalfEdgeHandle> > outgoing_hes_per_vertex_;

    // Incident halffaces per (directed) halfedge
    std::vector<std::vector<HalfFaceHandle> > incident_hfs_per_he_;

    // Incident cell (at most one) per halfface
    std::vector<CellHandle> incident_cell_per_hf_;

    // Store boundary faces
    std::vector<FaceHandle> boundary_faces_;

    // Indicate whether bottom-up adjacencies
    // have been computed for vertices
    bool has_vertex_adjacencies_;

    // Indicate whether bottom-up adjacencies
    // have been computed for edges
    bool has_edge_adjacencies_;

    // Indicate whether bottom-up adjacencies
    // have been computed for faces
    bool has_face_adjacencies_;
};

}

#endif /* TOPOLOGYKERNEL_HH_ */