Commit b8e6f3d7 authored by Jan Möbius's avatar Jan Möbius

Merge branch 'dev-mh-assign' into 'master'

Implement/improve mesh assignment/copying

See merge request !56
parents d13e0db0 f2f59897
Pipeline #10428 passed with stage
in 3 minutes and 42 seconds
......@@ -32,18 +32,11 @@
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision: 36 $ *
* $Date: 2012-01-10 18:00:06 +0100 (Di, 10 Jan 2012) $ *
* $LastChangedBy: kremer $ *
* *
\*===========================================================================*/
#include "BaseProperty.hh"
#include "ResourceManager.hh"
namespace OpenVolumeMesh {
// define this here to emit a vtable only in this translation unit
BaseProperty::~BaseProperty() = default;
} // Namespace OpenVolumeMesh
......@@ -62,7 +62,8 @@ public:
BaseProperty& operator=(const BaseProperty& _cpy) = delete;
virtual ~BaseProperty() {}
virtual ~BaseProperty();
virtual const std::string& name() const = 0;
......@@ -88,8 +89,18 @@ public:
virtual const std::string typeNameWrapper() const = 0;
virtual size_t size() const = 0;
protected:
/// Copy data from other property. `other` MUST point to an object with the same type as `this`!
/// Currently no type check is performed.
virtual void assign_values_from(const BaseProperty *other) = 0;
/// Move data from other property. `other` MUST point to an object with the same type as `this`!
/// Currently no type check is performed.
virtual void move_values_from(BaseProperty *other) = 0;
virtual void delete_multiple_entries(const std::vector<bool>& _tags) = 0;
virtual void resize(size_t /*_size*/) = 0;
......
......@@ -59,10 +59,16 @@ public:
typedef TopologyKernelT KernelT;
/// Constructor
GeometryKernel() {}
GeometryKernel() = default;
/// Destructor
~GeometryKernel() {}
~GeometryKernel() = default;
template<class OtherTopoKernel>
void assign(const GeometryKernel<VecT, OtherTopoKernel> *other) {
TopologyKernelT::assign(other);
other->clone_vertices(vertices_);
}
/// Override of empty add_vertex function
virtual VertexHandle add_vertex() { return add_vertex(VecT()); }
......
......@@ -88,6 +88,9 @@ public:
virtual void resize(size_t _n) {
data_.resize(_n, def_);
}
virtual size_t size() const {
return data_.size();
}
virtual void clear() {
data_.clear();
vector_type().swap(data_);
......
......@@ -91,6 +91,7 @@ public:
const_iterator begin() const { return ptr::shared_ptr<PropT>::get()->begin(); }
iterator begin() { return ptr::shared_ptr<PropT>::get()->begin(); }
size_t size() const { return ptr::shared_ptr<PropT>::get()->size(); }
const_iterator end() const { return ptr::shared_ptr<PropT>::get()->end(); }
iterator end() { return ptr::shared_ptr<PropT>::get()->end(); }
......@@ -114,6 +115,9 @@ public:
protected:
void assign_values_from(const BaseProperty *other) override;
void move_values_from(BaseProperty *other) override;
virtual void delete_multiple_entries(const std::vector<bool>& _tags);
virtual void resize(size_t _size);
......
......@@ -59,6 +59,20 @@ PropertyPtr<PropT,Entity>::~PropertyPtr() {
}
}
template <class PropT, typename Entity>
void PropertyPtr<PropT,Entity>::assign_values_from(const BaseProperty *other) {
auto _other = static_cast<const PropertyPtr<PropT,Entity>*>(other);
// FIXME: would be nice to perform a type check here
ptr::shared_ptr<PropT>::get()->data_vector() = _other->get()->data_vector();
}
template <class PropT, typename Entity>
void PropertyPtr<PropT,Entity>::move_values_from(BaseProperty *other) {
auto _other = static_cast<PropertyPtr<PropT,Entity>*>(other);
// FIXME: would be nice to perform a type check here
ptr::shared_ptr<PropT>::get()->data_vector() = std::move(_other->get()->data_vector());
}
template <class PropT, typename Entity>
void PropertyPtr<PropT,Entity>::resize(size_t _size) {
ptr::shared_ptr<PropT>::get()->resize(_size);
......
......@@ -51,19 +51,8 @@ ResourceManager& ResourceManager::operator=(const ResourceManager &other)
if (this == &other)
return *this;
auto cloneProps = [this](const Properties &src, Properties &dest) {
dest.reserve(src.size());
for (BaseProperty *bp: src) {
dest.push_back(bp->clone(*this, bp->handle()));
}
};
cloneProps(other.vertex_props_, vertex_props_);
cloneProps(other.edge_props_, edge_props_);
cloneProps(other.halfedge_props_, halfedge_props_);
cloneProps(other.face_props_, face_props_);
cloneProps(other.halfface_props_, halfface_props_);
cloneProps(other.cell_props_, cell_props_);
cloneProps(other.mesh_props_, mesh_props_);
assignAllPropertiesFrom<false>(&other);
return *this;
}
......@@ -72,19 +61,8 @@ ResourceManager& ResourceManager::operator=(ResourceManager &&other)
if (this == &other)
return *this;
auto moveProps = [this](Properties &&src, Properties &dest) {
dest = std::move(src);
for (auto prop: dest) {
prop->setResMan(this);
}
};
moveProps(std::move(other.vertex_props_), vertex_props_);
moveProps(std::move(other.edge_props_), edge_props_);
moveProps(std::move(other.halfedge_props_), halfedge_props_);
moveProps(std::move(other.face_props_), face_props_);
moveProps(std::move(other.halfface_props_), halfface_props_);
moveProps(std::move(other.cell_props_), cell_props_);
moveProps(std::move(other.mesh_props_), mesh_props_);
assignAllPropertiesFrom<true>(&other);
return *this;
}
......@@ -271,4 +249,54 @@ void ResourceManager::delete_multiple_cell_props(const std::vector<bool>& _tags)
}
}
template<bool Move>
void ResourceManager::assignProperties(typename std::conditional<Move, Properties&, const Properties&>::type src,
Properties &dest)
{
// If possible, re-use existing properties instead of copying
// everything blindly.
Properties out;
out.reserve(src.size());
for (BaseProperty *srcprop: src) {
bool found = false;
for (auto it = dest.begin(); it != dest.end(); ++it) {
auto dstprop = *it;
if (dstprop->name() == srcprop->name())
{
// TODO: type check
out.push_back(dstprop);
dest.erase(it);
if (Move) {
dstprop->move_values_from(srcprop);
} else {
dstprop->assign_values_from(srcprop);
}
found = true;
break;
}
}
if (!found) {
if (Move) {
out.push_back(srcprop);
} else {
out.push_back(srcprop->clone(*this, OpenVolumeMeshHandle(-1)));
}
}
}
updatePropHandles(out);
dest = std::move(out);
}
template<bool Move>
void ResourceManager::assignAllPropertiesFrom(typename std::conditional<Move, ResourceManager*, const ResourceManager*>::type other)
{
assignProperties<Move>(other->vertex_props_, vertex_props_);
assignProperties<Move>(other->edge_props_, edge_props_);
assignProperties<Move>(other->halfedge_props_, halfedge_props_);
assignProperties<Move>(other->face_props_, face_props_);
assignProperties<Move>(other->halfface_props_, halfface_props_);
assignProperties<Move>(other->cell_props_, cell_props_);
assignProperties<Move>(other->mesh_props_, mesh_props_);
}
} // Namespace OpenVolumeMesh
......@@ -39,6 +39,7 @@
#endif
#include <string>
#include <vector>
#include <type_traits>
#include "OpenVolumeMeshProperty.hh"
#include "PropertyHandles.hh"
......@@ -316,6 +317,12 @@ private:
template<class StdVecT>
void updatePropHandles(StdVecT& _vec);
template<bool Move>
void assignProperties(typename std::conditional<Move, Properties&, const Properties&>::type src,
Properties &dest);
template<bool Move>
void assignAllPropertiesFrom(typename std::conditional<Move, ResourceManager*, const ResourceManager*>::type src);
Properties vertex_props_;
Properties edge_props_;
......
......@@ -58,19 +58,6 @@ const FaceHandle TopologyKernel::InvalidFaceHandle = FaceHandle(-1);
const HalfFaceHandle TopologyKernel::InvalidHalfFaceHandle = HalfFaceHandle(-1);
const CellHandle TopologyKernel::InvalidCellHandle = CellHandle(-1);
TopologyKernel::TopologyKernel() :
n_vertices_(0u),
v_bottom_up_(true),
e_bottom_up_(true),
f_bottom_up_(true),
deferred_deletion(true),
fast_deletion(true)
{
}
TopologyKernel::~TopologyKernel() {
}
//========================================================================================
VertexHandle TopologyKernel::add_vertex() {
......
......@@ -57,8 +57,14 @@ namespace OpenVolumeMesh {
class TopologyKernel : public ResourceManager {
public:
TopologyKernel();
virtual ~TopologyKernel();
TopologyKernel() = default;
virtual ~TopologyKernel() = default;
TopologyKernel& operator=(const TopologyKernel&) = default;
void assign(const TopologyKernel *other) {
*this = *other;
}
/*
* Defines and constants
......@@ -361,7 +367,7 @@ public:
private:
// Cache total vertex number
size_t n_vertices_;
size_t n_vertices_ = 0u;
public:
......@@ -793,15 +799,15 @@ protected:
std::vector<CellHandle> incident_cell_per_hf_;
private:
bool v_bottom_up_;
bool v_bottom_up_ = true;
bool e_bottom_up_;
bool e_bottom_up_ = true;
bool f_bottom_up_;
bool f_bottom_up_ = true;
bool deferred_deletion;
bool deferred_deletion = true;
bool fast_deletion;
bool fast_deletion = true;
//=====================================================================
// Connectivity
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment