43 #ifndef TOPOLOGYKERNEL_HH_ 44 #define TOPOLOGYKERNEL_HH_ 50 #include "BaseEntities.hh" 51 #include "OpenVolumeMeshHandle.hh" 52 #include "ResourceManager.hh" 53 #include "Iterators.hh" 110 template <
class Circulator>
111 static Circulator make_end_circulator(
const Circulator& _circ)
113 Circulator end = _circ;
115 end.lap(_circ.max_laps());
127 std::pair<VertexOHalfEdgeIter, VertexOHalfEdgeIter> outgoing_halfedges(
const VertexHandle& _h,
int _max_laps = 1)
const {
129 return std::make_pair(begin, make_end_circulator(begin));
136 std::pair<VertexVertexIter, VertexVertexIter> vertex_vertices(
const VertexHandle& _h,
int _max_laps = 1)
const {
138 return std::make_pair(begin, make_end_circulator(begin));
145 std::pair<HalfEdgeHalfFaceIter, HalfEdgeHalfFaceIter> halfedge_halffaces(
const HalfEdgeHandle& _h,
int _max_laps = 1)
const {
147 return std::make_pair(begin, make_end_circulator(begin));
154 std::pair<VertexFaceIter, VertexFaceIter> vertex_faces(
const VertexHandle& _h,
int _max_laps = 1)
const {
156 return std::make_pair(begin, make_end_circulator(begin));
163 std::pair<VertexCellIter, VertexCellIter> vertex_cells(
const VertexHandle& _h,
int _max_laps = 1)
const {
165 return std::make_pair(begin, make_end_circulator(begin));
172 std::pair<HalfEdgeCellIter, HalfEdgeCellIter> halfedge_cells(
const HalfEdgeHandle& _h,
int _max_laps = 1)
const {
174 return std::make_pair(begin, make_end_circulator(begin));
181 std::pair<CellVertexIter, CellVertexIter> cell_vertices(
const CellHandle& _h,
int _max_laps = 1)
const {
183 return std::make_pair(begin, make_end_circulator(begin));
190 std::pair<CellCellIter, CellCellIter> cell_cells(
const CellHandle& _h,
int _max_laps = 1)
const {
192 return std::make_pair(begin, make_end_circulator(begin));
199 std::pair<HalfFaceVertexIter, HalfFaceVertexIter> halfface_vertices(
const HalfFaceHandle& _h,
int _max_laps = 1)
const {
201 return std::make_pair(begin, make_end_circulator(begin));
208 std::pair<BoundaryHalfFaceHalfFaceIter, BoundaryHalfFaceHalfFaceIter> boundary_halfface_halffaces(
const HalfFaceHandle& _h,
int _max_laps = 1)
const {
210 return std::make_pair(begin, make_end_circulator(begin));
233 std::pair<VertexIter, VertexIter> vertices()
const {
234 return std::make_pair(vertices_begin(), vertices_end());
249 std::pair<EdgeIter, EdgeIter> edges()
const {
250 return std::make_pair(edges_begin(), edges_end());
265 std::pair<HalfEdgeIter, HalfEdgeIter> halfedges()
const {
266 return std::make_pair(halfedges_begin(), halfedges_end());
281 std::pair<FaceIter, FaceIter> faces()
const {
282 return std::make_pair(faces_begin(), faces_end());
297 std::pair<HalfFaceIter, HalfFaceIter> halffaces()
const {
298 return std::make_pair(halffaces_begin(), halffaces_end());
313 std::pair<CellIter, CellIter> cells()
const {
314 return std::make_pair(cells_begin(), cells_end());
324 virtual size_t n_edges()
const {
return edges_.size(); }
328 virtual size_t n_faces()
const {
return faces_.size(); }
332 virtual size_t n_cells()
const {
return cells_.size(); }
354 if(g % 2 == 0)
return (g / 2);
383 virtual FaceHandle add_face(
const std::vector<HalfEdgeHandle>& _halfedges,
bool _topologyCheck =
false);
395 virtual CellHandle add_cell(
const std::vector<HalfFaceHandle>& _halffaces,
bool _topologyCheck =
false);
466 assert(has_vertex_bottom_up_incidences());
467 assert(_vh.is_valid() && (size_t)_vh.idx() < outgoing_hes_per_vertex_.size());
469 return outgoing_hes_per_vertex_[_vh.idx()].size();
474 assert(has_edge_bottom_up_incidences());
475 assert(_eh.is_valid() && (size_t)_eh.idx() < edges_.size());
476 assert((
size_t)
halfedge_handle(_eh, 0).idx() < incident_hfs_per_he_.size());
483 assert(_fh.is_valid() && (size_t)_fh.idx() < faces_.size());
485 return face(_fh).halfedges().size();
490 assert(_ch.is_valid() && (size_t)_ch.idx() < cells_.size());
492 return cell(_ch).halffaces().size();
512 virtual bool is_deleted(
const VertexHandle& _h)
const {
return vertex_deleted_[_h.idx()]; }
513 virtual bool is_deleted(
const EdgeHandle& _h)
const {
return edge_deleted_[_h.idx()]; }
514 virtual bool is_deleted(
const HalfEdgeHandle& _h)
const {
return edge_deleted_[_h.idx()/2]; }
515 virtual bool is_deleted(
const FaceHandle& _h)
const {
return face_deleted_[_h.idx()]; }
516 virtual bool is_deleted(
const HalfFaceHandle& _h)
const {
return face_deleted_[_h.idx()/2]; }
517 virtual bool is_deleted(
const CellHandle& _h)
const {
return cell_deleted_[_h.idx()]; }
521 template <
class ContainerT>
522 void get_incident_edges(
const ContainerT& _vs, std::set<EdgeHandle>& _es)
const;
524 template <
class ContainerT>
525 void get_incident_faces(
const ContainerT& _es, std::set<FaceHandle>& _fs)
const;
527 template <
class ContainerT>
528 void get_incident_cells(
const ContainerT& _fs, std::set<CellHandle>& _cs)
const;
554 virtual void delete_multiple_vertices(
const std::vector<bool>& _tag);
556 virtual void delete_multiple_edges(
const std::vector<bool>& _tag);
558 virtual void delete_multiple_faces(
const std::vector<bool>& _tag);
560 virtual void delete_multiple_cells(
const std::vector<bool>& _tag);
564 explicit EdgeCorrector(
const std::vector<int>& _newIndices) :
565 newIndices_(_newIndices) {}
567 void operator()(Edge& _edge) {
568 _edge.set_from_vertex(
VertexHandle(newIndices_[_edge.from_vertex().idx()]));
569 _edge.set_to_vertex(
VertexHandle(newIndices_[_edge.to_vertex().idx()]));
572 const std::vector<int>& newIndices_;
577 explicit FaceCorrector(
const std::vector<int>& _newIndices) :
578 newIndices_(_newIndices) {}
580 void operator()(Face& _face) {
581 std::vector<HalfEdgeHandle> hes = _face.halfedges();
582 for(std::vector<HalfEdgeHandle>::iterator he_it = hes.begin(),
583 he_end = hes.end(); he_it != he_end; ++he_it) {
589 _face.set_halfedges(hes);
592 const std::vector<int>& newIndices_;
597 explicit CellCorrector(
const std::vector<int>& _newIndices) :
598 newIndices_(_newIndices) {}
600 void operator()(Cell& _cell) {
601 std::vector<HalfFaceHandle> hfs = _cell.halffaces();
602 for(std::vector<HalfFaceHandle>::iterator hf_it = hfs.begin(),
603 hf_end = hfs.end(); hf_it != hf_end; ++hf_it) {
609 _cell.set_halffaces(hfs);
612 const std::vector<int>& newIndices_;
630 virtual void clear(
bool _clearProps =
true) {
635 vertex_deleted_.clear();
636 edge_deleted_.clear();
637 face_deleted_.clear();
638 cell_deleted_.clear();
639 n_deleted_vertices_ = 0;
640 n_deleted_edges_ = 0;
641 n_deleted_faces_ = 0;
642 n_deleted_cells_ = 0;
643 outgoing_hes_per_vertex_.clear();
644 incident_hfs_per_he_.clear();
645 incident_cell_per_hf_.clear();
651 clear_vertex_props();
653 clear_halfedge_props();
655 clear_halfface_props();
674 void enable_bottom_up_incidences(
bool _enable =
true) {
676 enable_vertex_bottom_up_incidences(_enable);
677 enable_edge_bottom_up_incidences(_enable);
678 enable_face_bottom_up_incidences(_enable);
681 void enable_vertex_bottom_up_incidences(
bool _enable =
true) {
683 if(_enable && !v_bottom_up_) {
686 compute_vertex_bottom_up_incidences();
690 outgoing_hes_per_vertex_.clear();
693 v_bottom_up_ = _enable;
696 void enable_edge_bottom_up_incidences(
bool _enable =
true) {
698 if(_enable && !e_bottom_up_) {
701 compute_edge_bottom_up_incidences();
704 #if defined(__clang_major__) && (__clang_major__ >= 5) 705 for(
EdgeIter e_it = edges_begin(), e_end = edges_end();
706 e_it != e_end; ++e_it) {
707 reorder_incident_halffaces(*e_it);
710 std::for_each(edges_begin(), edges_end(),
711 fun::bind(&TopologyKernel::reorder_incident_halffaces,
this, fun::placeholders::_1));
717 incident_hfs_per_he_.clear();
720 e_bottom_up_ = _enable;
723 void enable_face_bottom_up_incidences(
bool _enable =
true) {
725 bool updateOrder =
false;
726 if(_enable && !f_bottom_up_) {
729 compute_face_bottom_up_incidences();
735 incident_cell_per_hf_.clear();
738 f_bottom_up_ = _enable;
742 #if defined(__clang_major__) && (__clang_major__ >= 5) 743 for(
EdgeIter e_it = edges_begin(), e_end = edges_end();
744 e_it != e_end; ++e_it) {
745 reorder_incident_halffaces(*e_it);
748 std::for_each(edges_begin(), edges_end(),
749 fun::bind(&TopologyKernel::reorder_incident_halffaces,
this, fun::placeholders::_1));
755 bool has_full_bottom_up_incidences()
const {
756 return (has_vertex_bottom_up_incidences() &&
757 has_edge_bottom_up_incidences() &&
758 has_face_bottom_up_incidences());
761 bool has_vertex_bottom_up_incidences()
const {
return v_bottom_up_; }
763 bool has_edge_bottom_up_incidences()
const {
return e_bottom_up_; }
765 bool has_face_bottom_up_incidences()
const {
return f_bottom_up_; }
768 void enable_deferred_deletion(
bool _enable =
true);
769 bool deferred_deletion_enabled()
const {
return deferred_deletion; }
772 void enable_fast_deletion(
bool _enable =
true) { fast_deletion = _enable; }
773 bool fast_deletion_enabled()
const {
return fast_deletion; }
778 void compute_vertex_bottom_up_incidences();
780 void compute_edge_bottom_up_incidences();
782 void compute_face_bottom_up_incidences();
784 void reorder_incident_halffaces(
const EdgeHandle& _eh);
787 std::vector<std::vector<HalfEdgeHandle> > outgoing_hes_per_vertex_;
790 std::vector<std::vector<HalfFaceHandle> > incident_hfs_per_he_;
793 std::vector<CellHandle> incident_cell_per_hf_;
802 bool deferred_deletion;
825 assert(_halfFaceHandle.is_valid() && (size_t)_halfFaceHandle.idx() < faces_.size() * 2u);
826 assert(has_face_bottom_up_incidences());
827 assert((
size_t)_halfFaceHandle.idx() < incident_cell_per_hf_.size());
828 return incident_cell_per_hf_[_halfFaceHandle.idx()] == InvalidCellHandle;
831 bool is_boundary(
const FaceHandle& _faceHandle)
const {
832 assert(_faceHandle.is_valid() && (size_t)_faceHandle.idx() < faces_.size());
833 assert(has_face_bottom_up_incidences());
838 bool is_boundary(
const EdgeHandle& _edgeHandle)
const {
839 assert(has_edge_bottom_up_incidences());
840 assert(_edgeHandle.is_valid() && (size_t)_edgeHandle.idx() < edges_.size());
843 hehf_it.valid(); ++hehf_it) {
844 if(is_boundary(face_handle(*hehf_it))) {
852 assert(has_edge_bottom_up_incidences());
853 assert(_halfedgeHandle.is_valid() && (size_t)_halfedgeHandle.idx() < edges_.size() * 2u);
856 hehf_it.valid(); ++hehf_it) {
857 if(is_boundary(face_handle(*hehf_it))) {
864 bool is_boundary(
const VertexHandle& _vertexHandle)
const {
865 assert(has_vertex_bottom_up_incidences());
866 assert(_vertexHandle.is_valid() && (size_t)_vertexHandle.idx() <
n_vertices());
869 if(is_boundary(*voh_it))
return true;
874 size_t n_vertices_in_cell(
const CellHandle& _ch)
const {
875 assert(_ch.is_valid() && (size_t)_ch.idx() < cells_.size());
877 std::set<VertexHandle> vertices;
878 std::vector<HalfFaceHandle> hfs =
cell(_ch).halffaces();
879 for(std::vector<HalfFaceHandle>::const_iterator hf_it = hfs.begin();
880 hf_it != hfs.end(); ++hf_it) {
881 std::vector<HalfEdgeHandle> hes =
halfface(*hf_it).halfedges();
882 for(std::vector<HalfEdgeHandle>::const_iterator he_it = hes.begin();
883 he_it != hes.end(); ++he_it) {
884 vertices.insert(
halfedge(*he_it).to_vertex());
887 return vertices.size();
897 return Edge(_edge.to_vertex(), _edge.from_vertex());
901 std::vector<HalfEdgeHandle> opp_halfedges;
902 for(std::vector<HalfEdgeHandle>::const_iterator it = _face.halfedges().begin(); it
903 != _face.halfedges().end(); ++it) {
904 opp_halfedges.insert(opp_halfedges.begin(), opposite_halfedge_handle(*it));
907 return Face(opp_halfedges);
917 assert(_h.is_valid());
926 assert(_h.is_valid());
935 assert(_h.is_valid());
942 assert(_h.is_valid());
949 assert(_h.is_valid());
953 if(_h.idx() % 2 == 0) {
961 assert(_h.is_valid());
965 if(_h.idx() % 2 == 0) {
971 bool inline needs_garbage_collection()
const {
972 return n_deleted_vertices_ > 0 || n_deleted_edges_ > 0 || n_deleted_faces_ > 0 || n_deleted_cells_ > 0;
978 std::vector<Edge> edges_;
981 std::vector<Face> faces_;
984 std::vector<Cell> cells_;
986 std::vector<bool> vertex_deleted_;
987 std::vector<bool> edge_deleted_;
988 std::vector<bool> face_deleted_;
989 std::vector<bool> cell_deleted_;
992 size_t n_deleted_vertices_ = 0;
993 size_t n_deleted_edges_ = 0;
994 size_t n_deleted_faces_ = 0;
995 size_t n_deleted_cells_ = 0;
bool bind(osg::GeometryPtr &_geo, Mesh &_mesh)
virtual void swap_vertex_indices(VertexHandle _h1, VertexHandle _h2)
Exchanges the indices of two vertices while keeping the mesh otherwise unaffected.
const Edge & edge(const EdgeHandle &_edgeHandle) const
Get edge with handle _edgeHandle.
virtual size_t n_halfedges() const
Get number of halfedges in mesh.
CellIter delete_cell_range(const CellIter &_first, const CellIter &_last)
Delete range of cells.
FaceIter delete_face_core(const FaceHandle &_h)
Delete face from mesh.
virtual VertexIter delete_vertex(const VertexHandle &_h)
Delete vertex from mesh.
static HalfEdgeHandle halfedge_handle(const EdgeHandle &_h, const unsigned char _subIdx)
Conversion function.
size_t valence(const VertexHandle &_vh) const
Get valence of vertex (number of incident edges)
void resize_cprops(size_t _nc)
Change size of stored cell properties.
size_t n_logical_cells() const
Get number of undeleted cells in mesh.
size_t n_logical_halffaces() const
Get number of undeleted halffaces in mesh.
const Cell & cell(const CellHandle &_cellHandle) const
Get cell with handle _cellHandle.
Edge opposite_halfedge(const HalfEdgeHandle &_halfEdgeHandle) const
Get opposite halfedge that corresponds to halfedge with handle _halfEdgeHandle.
void resize_eprops(size_t _ne)
Change size of stored edge properties.
void resize_vprops(size_t _nv)
Change size of stored vertex properties.
VertexIter delete_vertex_core(const VertexHandle &_h)
Delete vertex from mesh.
virtual size_t n_halffaces() const
Get number of halffaces in mesh.
virtual FaceHandle add_face(const std::vector< HalfEdgeHandle > &_halfedges, bool _topologyCheck=false)
Add face via incident edges.
virtual EdgeHandle add_edge(const VertexHandle &_fromVertex, const VertexHandle &_toHandle, bool _allowDuplicates=false)
Add edge.
virtual FaceIter delete_face(const FaceHandle &_h)
Delete face from mesh.
virtual void swap_cell_indices(CellHandle _h1, CellHandle _h2)
Exchanges the indices of two cells while keeping the mesh otherwise unaffected.
Face opposite_halfface(const HalfFaceHandle &_halfFaceHandle) const
Get opposite halfface that corresponds to halfface with handle _halfFaceHandle.
size_t valence(const CellHandle &_ch) const
Get valence of cell (number of incident faces)
EdgeIter delete_edge_core(const EdgeHandle &_h)
Delete edge from mesh.
static EdgeHandle edge_handle(const HalfEdgeHandle &_h)
Handle conversion.
size_t valence(const FaceHandle &_fh) const
Get valence of face (number of incident edges)
Face halfface(const HalfFaceHandle &_halfFaceHandle) const
Get face that corresponds to halfface with handle _halfFaceHandle.
HalfFaceHandle adjacent_halfface_in_cell(const HalfFaceHandle &_halfFaceHandle, const HalfEdgeHandle &_halfEdgeHandle) const
Get halfface that is adjacent (w.r.t. a common halfedge) within the same cell.
virtual size_t n_faces() const
Get number of faces in mesh.
virtual CellHandle add_cell(const std::vector< HalfFaceHandle > &_halffaces, bool _topologyCheck=false)
Add cell via incident halffaces.
void set_cell(const CellHandle &_ch, const std::vector< HalfFaceHandle > &_hfs)
Set the half-faces of a cell.
virtual void collect_garbage()
Delete all entities that are marked as deleted.
HalfEdgeHandle prev_halfedge_in_halfface(const HalfEdgeHandle &_heh, const HalfFaceHandle &_hfh) const
Get previous halfedge within a halfface.
const Face & face(const FaceHandle &_faceHandle) const
Get face with handle _faceHandle.
void resize_fprops(size_t _nf)
Change size of stored face properties.
void set_face(const FaceHandle &_fh, const std::vector< HalfEdgeHandle > &_hes)
Set the half-edges of a face.
virtual void clear(bool _clearProps=true)
Clear whole mesh.
size_t n_logical_edges() const
Get number of undeleted edges in mesh.
size_t n_logical_vertices() const
Get number of undeleted vertices in mesh.
virtual size_t n_cells() const
Get number of cells in mesh.
void set_edge(const EdgeHandle &_eh, const VertexHandle &_fromVertex, const VertexHandle &_toVertex)
Set the vertices of an edge.
size_t valence(const EdgeHandle &_eh) const
Get valence of edge (number of incident faces)
virtual size_t n_edges() const
Get number of edges in mesh.
CellHandle incident_cell(const HalfFaceHandle &_halfFaceHandle) const
Get cell that is incident to the given halfface.
size_t n_logical_faces() const
Get number of undeleted faces in mesh.
Edge halfedge(const HalfEdgeHandle &_halfEdgeHandle) const
Get edge that corresponds to halfedge with handle _halfEdgeHandle.
static HalfFaceHandle halfface_handle(const FaceHandle &_h, const unsigned char _subIdx)
Conversion function.
virtual void swap_edge_indices(EdgeHandle _h1, EdgeHandle _h2)
Exchanges the indices of two edges while keeping the mesh otherwise unaffected.
CellIter delete_cell_core(const CellHandle &_h)
Delete cell from mesh.
HalfEdgeHandle next_halfedge_in_halfface(const HalfEdgeHandle &_heh, const HalfFaceHandle &_hfh) const
Get next halfedge within a halfface.
virtual void swap_face_indices(FaceHandle _h1, FaceHandle _h2)
Exchanges the indices of two faces while keeping the mesh otherwise unaffected.
virtual size_t n_vertices() const
Get number of vertices in mesh.
virtual CellIter delete_cell(const CellHandle &_h)
Delete cell from mesh.
size_t n_logical_halfedges() const
Get number of undeleted halfedges in mesh.
HalfFaceHandle halfface_extensive(const std::vector< VertexHandle > &_vs) const
virtual EdgeIter delete_edge(const EdgeHandle &_h)
Delete edge from mesh.
virtual VertexHandle add_vertex()
Add abstract vertex.