Commit 267a04bd authored by Max Lyon's avatar Max Lyon

implemented fast and deferred deletion of entities

fast: swap deleted entity with last in vector, resize vector
deferred: only mark entity as deleted, keep deleted entity in vector
parent 4ebfee6c
......@@ -67,6 +67,8 @@ public:
virtual void delete_element(size_t _idx) = 0;
virtual void swap_elements(size_t _idx0, size_t _idx1) = 0;
virtual void serialize(std::ostream& _ostr) const = 0;
virtual void deserialize(std::istream& _istr) = 0;
......
......@@ -95,11 +95,41 @@ public:
VertexIter nV = TopologyKernelT::delete_vertex(_h);
vertices_.erase(vertices_.begin() + _h.idx());
if (TopologyKernelT::deferred_deletion_enabled())
{
}
else
vertices_.erase(vertices_.begin() + _h.idx());
return nV;
}
virtual void collect_garbage()
{
TopologyKernelT::collect_garbage();
for (unsigned int i = vertices_.size(); i > 0; --i)
if (TopologyKernelT::is_deleted(VertexHandle(i-1)))
{
vertices_.erase(vertices_.begin() + (i-1));
}
}
virtual void swap_vertices(VertexHandle _h1, VertexHandle _h2)
{
assert(_h1.idx() >= 0 && _h1.idx() < (int)vertices_.size());
assert(_h2.idx() >= 0 && _h2.idx() < (int)vertices_.size());
if (_h1 == _h2)
return;
std::swap(vertices_[_h1.idx()], vertices_[_h2.idx()]);
TopologyKernelT::swap_vertices(_h1, _h2);
}
protected:
virtual void delete_multiple_vertices(const std::vector<bool>& _tag) {
......
......@@ -240,6 +240,10 @@ BaseIter(_mesh, _ref_h, _max_laps) {
std::sort(cells_.begin(), cells_.end());
cells_.resize(std::unique(cells_.begin(), cells_.end()) - cells_.begin());
// Remove invalid handles
if ((cells_.size() > 0) && !cells_.front().is_valid())
cells_.erase(cells_.begin());
cur_index_ = 0;
BaseIter::valid(cells_.size()>0);
if(BaseIter::valid()) {
......@@ -386,8 +390,9 @@ CellVertexIter::CellVertexIter(const CellHandle& _ref_h,
const TopologyKernel* _mesh, int _max_laps) :
BaseIter(_mesh, _ref_h, _max_laps) {
std::vector<HalfFaceHandle>::const_iterator hf_iter = BaseIter::mesh()->cell(_ref_h).halffaces().begin();
for(; hf_iter != BaseIter::mesh()->cell(_ref_h).halffaces().end(); ++hf_iter) {
OpenVolumeMeshCell c = BaseIter::mesh()->cell(_ref_h);
std::vector<HalfFaceHandle>::const_iterator hf_iter = c.halffaces().begin();
for(; hf_iter != c.halffaces().end(); ++hf_iter) {
const OpenVolumeMeshFace& halfface = BaseIter::mesh()->halfface(*hf_iter);
const std::vector<HalfEdgeHandle>& hes = halfface.halfedges();
for(std::vector<HalfEdgeHandle>::const_iterator he_iter = hes.begin(); he_iter != hes.end(); ++he_iter) {
......@@ -661,7 +666,8 @@ bf_it_(_mesh->faces_begin()) {
}
while(bf_it_ != BaseIter::mesh()->faces_end() &&
!BaseIter::mesh()->is_boundary(*bf_it_)) {
!BaseIter::mesh()->is_boundary(*bf_it_) &&
BaseIter::mesh()->is_deleted(bf_it_.cur_handle())){
++bf_it_;
}
BaseIter::valid(bf_it_ != BaseIter::mesh()->faces_end());
......@@ -675,7 +681,8 @@ BoundaryFaceIter& BoundaryFaceIter::operator--() {
--bf_it_;
while(bf_it_ >= BaseIter::mesh()->faces_begin() &&
!BaseIter::mesh()->is_boundary(*bf_it_)) {
!BaseIter::mesh()->is_boundary(*bf_it_) &&
BaseIter::mesh()->is_deleted(bf_it_.cur_handle())){
--bf_it_;
}
if(bf_it_ >= BaseIter::mesh()->faces_begin()) {
......@@ -691,7 +698,8 @@ BoundaryFaceIter& BoundaryFaceIter::operator++() {
++bf_it_;
while(bf_it_ != BaseIter::mesh()->faces_end() &&
!BaseIter::mesh()->is_boundary(*bf_it_)) {
!BaseIter::mesh()->is_boundary(*bf_it_) &&
BaseIter::mesh()->is_deleted(bf_it_.cur_handle())){
++bf_it_;
}
if(bf_it_ != BaseIter::mesh()->faces_end()) {
......@@ -711,18 +719,20 @@ VertexIter::VertexIter(const TopologyKernel* _mesh, const VertexHandle& _vh) :
BaseIter(_mesh, _vh),
cur_index_(_vh.idx()) {
if((unsigned int)cur_index_ >= BaseIter::mesh()->n_vertices()) {
BaseIter::valid(false);
}
if(BaseIter::valid()) {
BaseIter::cur_handle(VertexHandle(cur_index_));
}
while ((unsigned int)cur_index_ < BaseIter::mesh()->n_vertices() && BaseIter::mesh()->is_deleted(VertexHandle(cur_index_)))
++cur_index_;
if((unsigned int)cur_index_ >= BaseIter::mesh()->n_vertices()) {
BaseIter::valid(false);
}
BaseIter::cur_handle(VertexHandle(cur_index_));
}
VertexIter& VertexIter::operator--() {
--cur_index_;
while (cur_index_ >= 0 && BaseIter::mesh()->is_deleted(VertexHandle(cur_index_)))
--cur_index_;
if(cur_index_ < 0) {
BaseIter::valid(false);
}
......@@ -734,6 +744,8 @@ VertexIter& VertexIter::operator--() {
VertexIter& VertexIter::operator++() {
++cur_index_;
while ((unsigned int)cur_index_ < BaseIter::mesh()->n_vertices() && BaseIter::mesh()->is_deleted(VertexHandle(cur_index_)))
++cur_index_;
if((unsigned int)cur_index_ >= BaseIter::mesh()->n_vertices()) {
BaseIter::valid(false);
}
......@@ -750,34 +762,38 @@ EdgeIter::EdgeIter(const TopologyKernel* _mesh, const EdgeHandle& _eh) :
BaseIter(_mesh, _eh),
cur_index_(_eh.idx()) {
if((unsigned int)cur_index_ >= BaseIter::mesh()->edges_.size()) {
BaseIter::valid(false);
}
if(BaseIter::valid()) {
BaseIter::cur_handle(EdgeHandle(cur_index_));
}
while ((unsigned int)cur_index_ < BaseIter::mesh()->edges_.size() && BaseIter::mesh()->is_deleted(EdgeHandle(cur_index_)))
++cur_index_;
if((unsigned int)cur_index_ >= BaseIter::mesh()->edges_.size()) {
BaseIter::valid(false);
}
BaseIter::cur_handle(EdgeHandle(cur_index_));
}
EdgeIter& EdgeIter::operator--() {
--cur_index_;
if(cur_index_ < 0) {
BaseIter::valid(false);
}
BaseIter::cur_handle(EdgeHandle(cur_index_));
return *this;
--cur_index_;
while (cur_index_ >= 0 && BaseIter::mesh()->is_deleted(EdgeHandle(cur_index_)))
--cur_index_;
if(cur_index_ < 0) {
BaseIter::valid(false);
}
BaseIter::cur_handle(EdgeHandle(cur_index_));
return *this;
}
EdgeIter& EdgeIter::operator++() {
++cur_index_;
if((unsigned int)cur_index_ >= BaseIter::mesh()->edges_.size()) {
BaseIter::valid(false);
}
BaseIter::cur_handle(EdgeHandle(cur_index_));
return *this;
++cur_index_;
while ((unsigned int)cur_index_ < BaseIter::mesh()->edges_.size() && BaseIter::mesh()->is_deleted(EdgeHandle(cur_index_)))
++cur_index_;
if((unsigned int)cur_index_ >= BaseIter::mesh()->edges_.size()) {
BaseIter::valid(false);
}
BaseIter::cur_handle(EdgeHandle(cur_index_));
return *this;
}
////================================================================================================
......@@ -789,34 +805,38 @@ HalfEdgeIter::HalfEdgeIter(const TopologyKernel* _mesh, const HalfEdgeHandle& _h
BaseIter(_mesh, _heh),
cur_index_(_heh.idx()) {
if((unsigned int)cur_index_ >= BaseIter::mesh()->edges_.size() * 2) {
BaseIter::valid(false);
}
if(BaseIter::valid()) {
BaseIter::cur_handle(HalfEdgeHandle(cur_index_));
}
while ((unsigned int)cur_index_ < BaseIter::mesh()->edges_.size() * 2 && BaseIter::mesh()->is_deleted(HalfEdgeHandle(cur_index_)))
++cur_index_;
if((unsigned int)cur_index_ >= BaseIter::mesh()->edges_.size() * 2) {
BaseIter::valid(false);
}
BaseIter::cur_handle(HalfEdgeHandle(cur_index_));
}
HalfEdgeIter& HalfEdgeIter::operator--() {
--cur_index_;
if(cur_index_ < 0) {
BaseIter::valid(false);
}
BaseIter::cur_handle(HalfEdgeHandle(cur_index_));
return *this;
--cur_index_;
while (cur_index_ >= 0 && BaseIter::mesh()->is_deleted(HalfEdgeHandle(cur_index_)))
--cur_index_;
if(cur_index_ < 0) {
BaseIter::valid(false);
}
BaseIter::cur_handle(HalfEdgeHandle(cur_index_));
return *this;
}
HalfEdgeIter& HalfEdgeIter::operator++() {
++cur_index_;
if((unsigned int)cur_index_ >= BaseIter::mesh()->edges_.size() * 2) {
BaseIter::valid(false);
}
BaseIter::cur_handle(HalfEdgeHandle(cur_index_));
return *this;
++cur_index_;
while ((unsigned int)cur_index_ < BaseIter::mesh()->edges_.size() * 2 && BaseIter::mesh()->is_deleted(HalfEdgeHandle(cur_index_)))
++cur_index_;
if((unsigned int)cur_index_ >= BaseIter::mesh()->edges_.size() * 2) {
BaseIter::valid(false);
}
BaseIter::cur_handle(HalfEdgeHandle(cur_index_));
return *this;
}
////================================================================================================
......@@ -828,34 +848,38 @@ FaceIter::FaceIter(const TopologyKernel* _mesh, const FaceHandle& _fh) :
BaseIter(_mesh, _fh),
cur_index_(_fh.idx()) {
if((unsigned int)cur_index_ >= BaseIter::mesh()->faces_.size()) {
BaseIter::valid(false);
}
if(BaseIter::valid()) {
BaseIter::cur_handle(FaceHandle(cur_index_));
}
while ((unsigned int)cur_index_ < BaseIter::mesh()->faces_.size() && BaseIter::mesh()->is_deleted(FaceHandle(cur_index_)))
++cur_index_;
if((unsigned int)cur_index_ >= BaseIter::mesh()->faces_.size()) {
BaseIter::valid(false);
}
BaseIter::cur_handle(FaceHandle(cur_index_));
}
FaceIter& FaceIter::operator--() {
--cur_index_;
if(cur_index_ < 0) {
BaseIter::valid(false);
}
BaseIter::cur_handle(FaceHandle(cur_index_));
return *this;
--cur_index_;
while (cur_index_ >= 0 && BaseIter::mesh()->is_deleted(FaceHandle(cur_index_)))
--cur_index_;
if(cur_index_ < 0) {
BaseIter::valid(false);
}
BaseIter::cur_handle(FaceHandle(cur_index_));
return *this;
}
FaceIter& FaceIter::operator++() {
++cur_index_;
if((unsigned int)cur_index_ >= BaseIter::mesh()->faces_.size()) {
BaseIter::valid(false);
}
BaseIter::cur_handle(FaceHandle(cur_index_));
return *this;
++cur_index_;
while ((unsigned int)cur_index_ < BaseIter::mesh()->faces_.size() && BaseIter::mesh()->is_deleted(FaceHandle(cur_index_)))
++cur_index_;
if((unsigned int)cur_index_ >= BaseIter::mesh()->faces_.size()) {
BaseIter::valid(false);
}
BaseIter::cur_handle(FaceHandle(cur_index_));
return *this;
}
////================================================================================================
......@@ -867,34 +891,38 @@ HalfFaceIter::HalfFaceIter(const TopologyKernel* _mesh, const HalfFaceHandle& _h
BaseIter(_mesh, _hfh),
cur_index_(_hfh.idx()) {
if((unsigned int)cur_index_ >= BaseIter::mesh()->faces_.size() * 2) {
BaseIter::valid(false);
}
if(BaseIter::valid()) {
BaseIter::cur_handle(HalfFaceHandle(cur_index_));
}
while ((unsigned int)cur_index_ < BaseIter::mesh()->faces_.size() * 2 && BaseIter::mesh()->is_deleted(HalfFaceHandle(cur_index_)))
++cur_index_;
if((unsigned int)cur_index_ >= BaseIter::mesh()->faces_.size() * 2) {
BaseIter::valid(false);
}
BaseIter::cur_handle(HalfFaceHandle(cur_index_));
}
HalfFaceIter& HalfFaceIter::operator--() {
--cur_index_;
if(cur_index_ < 0) {
BaseIter::valid(false);
}
BaseIter::cur_handle(HalfFaceHandle(cur_index_));
return *this;
--cur_index_;
while (cur_index_ >= 0 && BaseIter::mesh()->is_deleted(HalfFaceHandle(cur_index_)))
--cur_index_;
if(cur_index_ < 0) {
BaseIter::valid(false);
}
BaseIter::cur_handle(HalfFaceHandle(cur_index_));
return *this;
}
HalfFaceIter& HalfFaceIter::operator++() {
++cur_index_;
if((unsigned int)cur_index_ >= BaseIter::mesh()->faces_.size() * 2) {
BaseIter::valid(false);
}
BaseIter::cur_handle(HalfFaceHandle(cur_index_));
return *this;
++cur_index_;
while ((unsigned int)cur_index_ < BaseIter::mesh()->faces_.size() * 2 && BaseIter::mesh()->is_deleted(HalfFaceHandle(cur_index_)))
++cur_index_;
if((unsigned int)cur_index_ >= BaseIter::mesh()->faces_.size() * 2) {
BaseIter::valid(false);
}
BaseIter::cur_handle(HalfFaceHandle(cur_index_));
return *this;
}
////================================================================================================
......@@ -906,34 +934,38 @@ CellIter::CellIter(const TopologyKernel* _mesh, const CellHandle& _ch) :
BaseIter(_mesh, _ch),
cur_index_(_ch.idx()) {
if((unsigned int)cur_index_ >= BaseIter::mesh()->cells_.size()) {
BaseIter::valid(false);
}
if(BaseIter::valid()) {
BaseIter::cur_handle(CellHandle(cur_index_));
}
while ((unsigned int)cur_index_ < BaseIter::mesh()->cells_.size() && BaseIter::mesh()->is_deleted(CellHandle(cur_index_)))
++cur_index_;
if((unsigned int)cur_index_ >= BaseIter::mesh()->cells_.size()) {
BaseIter::valid(false);
}
BaseIter::cur_handle(CellHandle(cur_index_));
}
CellIter& CellIter::operator--() {
--cur_index_;
if(cur_index_ < 0) {
BaseIter::valid(false);
}
BaseIter::cur_handle(CellHandle(cur_index_));
return *this;
--cur_index_;
while (cur_index_ >= 0 && BaseIter::mesh()->is_deleted(CellHandle(cur_index_)))
--cur_index_;
if(cur_index_ < 0) {
BaseIter::valid(false);
}
BaseIter::cur_handle(CellHandle(cur_index_));
return *this;
}
CellIter& CellIter::operator++() {
++cur_index_;
if((unsigned int)cur_index_ >= BaseIter::mesh()->cells_.size()) {
BaseIter::valid(false);
}
BaseIter::cur_handle(CellHandle(cur_index_));
return *this;
++cur_index_;
while ((unsigned int)cur_index_ < BaseIter::mesh()->cells_.size() && BaseIter::mesh()->is_deleted(CellHandle(cur_index_)))
++cur_index_;
if((unsigned int)cur_index_ >= BaseIter::mesh()->cells_.size()) {
BaseIter::valid(false);
}
BaseIter::cur_handle(CellHandle(cur_index_));
return *this;
}
} // Namespace OpenVolumeMesh
......@@ -100,7 +100,7 @@ public:
data_.reserve(_n);
}
virtual void resize(size_t _n) {
data_.resize(_n, def_);
data_.resize(_n, def_);
}
virtual void clear() {
data_.clear();
......@@ -113,9 +113,9 @@ public:
std::swap(data_[_i0], data_[_i1]);
}
void delete_element(size_t _idx) {
data_.erase(data_.begin() + _idx);
}
void delete_element(size_t _idx) {
data_.erase(data_.begin() + _idx);
}
public:
......
......@@ -83,6 +83,8 @@ public:
virtual void delete_element(size_t _idx);
virtual void swap_elements(size_t _idx0, size_t _idx1);
const_iterator begin() const { return ptr::shared_ptr<PropT>::get()->begin(); }
iterator begin() { return ptr::shared_ptr<PropT>::get()->begin(); }
......
......@@ -83,6 +83,11 @@ void PropertyPtr<PropT,HandleT>::delete_element(size_t _idx) {
ptr::shared_ptr<PropT>::get()->delete_element(_idx);
}
template <class PropT, class HandleT>
void PropertyPtr<PropT,HandleT>::swap_elements(size_t _idx0, size_t _idx1) {
ptr::shared_ptr<PropT>::get()->swap(_idx0, _idx1);
}
template <class PropT, class HandleT>
void PropertyPtr<PropT,HandleT>::set_handle(const OpenVolumeMeshHandle& _handle) {
return ptr::shared_ptr<PropT>::get()->set_handle(_handle);
......
......@@ -106,6 +106,36 @@ void ResourceManager::cell_deleted(const CellHandle& _h) {
entity_deleted(cell_props_, _h);
}
void ResourceManager::swap_cell_properties(CellHandle _h1, CellHandle _h2){
swap_property_elements(cell_props_begin(), cell_props_end(), _h1, _h2);
}
void ResourceManager::swap_face_properties(FaceHandle _h1, FaceHandle _h2){
swap_property_elements(face_props_begin(), face_props_end(), _h1, _h2);
}
void ResourceManager::swap_halfface_properties(HalfFaceHandle _h1, HalfFaceHandle _h2){
swap_property_elements(halfface_props_begin(), halfface_props_end(), _h1, _h2);
}
void ResourceManager::swap_edge_properties(EdgeHandle _h1, EdgeHandle _h2){
swap_property_elements(edge_props_begin(), edge_props_end(), _h1, _h2);
}
void ResourceManager::swap_halfedge_properties(HalfEdgeHandle _h1, HalfEdgeHandle _h2){
swap_property_elements(halfedge_props_begin(), halfedge_props_end(), _h1, _h2);
}
void ResourceManager::swap_vertex_properties(VertexHandle _h1, VertexHandle _h2){
swap_property_elements(vertex_props_begin(), vertex_props_end(), _h1, _h2);
}
void ResourceManager::release_property(VertexPropHandle _handle) {
remove_property(vertex_props_, _handle.idx());
......
......@@ -102,6 +102,27 @@ protected:
void cell_deleted(const CellHandle& _h);
void swap_cell_properties(CellHandle _h1, CellHandle _h2);
void swap_face_properties(FaceHandle _h1, FaceHandle _h2);
void swap_halfface_properties(HalfFaceHandle _h1, HalfFaceHandle _h2);
void swap_edge_properties(EdgeHandle _h1, EdgeHandle _h2);
void swap_halfedge_properties(HalfEdgeHandle _h1, HalfEdgeHandle _h2);
void swap_vertex_properties(VertexHandle _h1, VertexHandle _h2);
template <typename PropIterator, typename Handle>
void swap_property_elements(PropIterator _begin, PropIterator _end, Handle _h1, Handle _h2)
{
PropIterator p_iter = _begin;
for (; p_iter != _end; ++p_iter)
(*p_iter)->swap_elements(_h1, _h2);
}
public:
void clear_vertex_props() { clearVec(vertex_props_); }
......
This diff is collapsed.
......@@ -468,6 +468,16 @@ public:
virtual CellIter delete_cell(const CellHandle& _h);
virtual void collect_garbage();
virtual bool is_deleted(const VertexHandle& _h) const { return vertex_deleted_[_h.idx()]; }
virtual bool is_deleted(const EdgeHandle& _h) const { return edge_deleted_[_h.idx()]; }
virtual bool is_deleted(const HalfEdgeHandle& _h) const { return edge_deleted_[_h.idx()/2]; }
virtual bool is_deleted(const FaceHandle& _h) const { return face_deleted_[_h.idx()]; }
virtual bool is_deleted(const HalfFaceHandle& _h) const { return face_deleted_[_h.idx()/2]; }
virtual bool is_deleted(const CellHandle& _h) const { return cell_deleted_[_h.idx()]; }
private:
template <class ContainerT>
......@@ -489,6 +499,14 @@ private:
protected:
virtual void swap_cells(CellHandle _h1, CellHandle _h2);
virtual void swap_faces(FaceHandle _h1, FaceHandle _h2);
virtual void swap_edges(EdgeHandle _h1, EdgeHandle _h2);
virtual void swap_vertices(VertexHandle _h1, VertexHandle _h2);
virtual void delete_multiple_vertices(const std::vector<bool>& _tag);
virtual void delete_multiple_edges(const std::vector<bool>& _tag);
......@@ -570,6 +588,10 @@ public:
edges_.clear();
faces_.clear();
cells_.clear();
vertex_deleted_.clear();
edge_deleted_.clear();
face_deleted_.clear();
cell_deleted_.clear();
outgoing_hes_per_vertex_.clear();
incident_hfs_per_he_.clear();
incident_cell_per_hf_.clear();
......@@ -694,7 +716,16 @@ public:
bool has_face_bottom_up_incidences() const { return f_bottom_up_; }
private:
void enable_deferred_deletion(bool _enable = true) { deferred_deletion = _enable; }
bool deferred_deletion_enabled() const { return deferred_deletion; }
void enable_fast_deletion(bool _enable = true) { fast_deletion = _enable; }
bool fast_deletion_enabled() const { return fast_deletion; }
protected:
void compute_vertex_bottom_up_incidences();
......@@ -713,12 +744,17 @@ private:
// Incident cell (at most one) per halfface
std::vector<CellHandle> incident_cell_per_hf_;
private:
bool v_bottom_up_;
bool e_bottom_up_;
bool f_bottom_up_;