Commit 6c0b01ce authored by Mike Kremer's avatar Mike Kremer

Fixed property serialization system. Added serialization to file writer.

git-svn-id: http://www.openvolumemesh.org/svnrepo/OpenVolumeMesh/trunk@142 66977474-1d4b-4f09-8fe9-267525286df2
parent df2bfe3c
......@@ -67,15 +67,18 @@ public:
virtual void delete_element(size_t _idx) = 0;
virtual std::ostream& serialize(std::ostream& _ostr) const = 0;
virtual void serialize(std::ostream& _ostr) const = 0;
virtual std::istream& deserialize(std::istream& _istr) = 0;
virtual void deserialize(std::istream& _istr) = 0;
virtual OpenVolumeMeshHandle handle() const = 0;
virtual bool persistent() const = 0;
virtual bool anonymous() const = 0;
protected:
virtual void resize(unsigned int /*_size*/) = 0;
virtual void set_handle(const OpenVolumeMeshHandle& /*_handle*/) = 0;
......
......@@ -107,10 +107,10 @@ public:
return nV;
}
virtual void clear() {
virtual void clear(bool _clearProps = true) {
vertices_.clear();
TopologyKernelT::clear();
TopologyKernelT::clear(_clearProps);
}
private:
......
......@@ -109,17 +109,15 @@ public:
}
// Function to serialize a property
virtual std::ostream& serialize(std::ostream& _ostr) const {
_ostr << name_ << std::endl;
return _ostr;
virtual void serialize(std::ostream& _ostr) const {
_ostr << "\"" << name_ << "\"" << std::endl;
}
// Function to deserialize a property
virtual std::istream& deserialize(std::istream& _istr) {
virtual void deserialize(std::istream& _istr) {
std::string str;
_istr >> str;
name_ = str;
return _istr;
}
public:
......
......@@ -139,24 +139,22 @@ public:
}
// Function to serialize a property
virtual std::ostream& serialize(std::ostream& _ostr) const {
_ostr << OpenVolumeMeshBaseProperty::serialize(_ostr);
virtual void serialize(std::ostream& _ostr) const {
OpenVolumeMeshBaseProperty::serialize(_ostr);
for(typename vector_type::const_iterator it = data_.begin();
it != data_.end(); ++it) {
_ostr << *it << std::endl;
}
return _ostr;
}
// Function to deserialize a property
virtual std::istream& deserialize(std::istream& _istr) {
virtual void deserialize(std::istream& _istr) {
OpenVolumeMeshBaseProperty::deserialize(_istr);
for(unsigned int i = 0; i < n_elements(); ++i) {
value_type val;
_istr >> val;
data_[i] = val;
}
return _istr;
}
public:
......
......@@ -44,6 +44,7 @@
#define PROPERTYDEFINES_HH_
#include <iostream>
#include <typeinfo>
#include "BaseProperty.hh"
#include "PropertyHandles.hh"
......@@ -63,58 +64,61 @@ class VertexPropertyT : public PropertyPtr<OpenVolumeMeshPropertyT<T>, VertexPro
public:
VertexPropertyT(const std::string& _name, ResourceManager& _resMan, VertexPropHandle _handle);
~VertexPropertyT() {}
virtual std::ostream& serialize(std::ostream& _ostr) const;
virtual std::istream& deserialize(std::istream& _istr);
virtual void serialize(std::ostream& _ostr) const;
virtual void deserialize(std::istream& _istr);
};
template<class T>
class EdgePropertyT : public PropertyPtr<OpenVolumeMeshPropertyT<T>, EdgePropHandle> {
public:
EdgePropertyT(const std::string& _name, ResourceManager& _resMan, EdgePropHandle _handle);
~EdgePropertyT() {}
virtual std::ostream& serialize(std::ostream& _ostr) const;
virtual std::istream& deserialize(std::istream& _istr);
virtual void serialize(std::ostream& _ostr) const;
virtual void deserialize(std::istream& _istr);
};
template<class T>
class HalfEdgePropertyT : public PropertyPtr<OpenVolumeMeshPropertyT<T>, HalfEdgePropHandle> {
public:
HalfEdgePropertyT(const std::string& _name, ResourceManager& _resMan, HalfEdgePropHandle _handle);
~HalfEdgePropertyT() {}
virtual std::ostream& serialize(std::ostream& _ostr) const;
virtual std::istream& deserialize(std::istream& _istr);
virtual void serialize(std::ostream& _ostr) const;
virtual void deserialize(std::istream& _istr);
};
template<class T>
class FacePropertyT : public PropertyPtr<OpenVolumeMeshPropertyT<T>, FacePropHandle> {
public:
FacePropertyT(const std::string& _name, ResourceManager& _resMan, FacePropHandle _handle);
~FacePropertyT() {}
virtual std::ostream& serialize(std::ostream& _ostr) const;
virtual std::istream& deserialize(std::istream& _istr);
virtual void serialize(std::ostream& _ostr) const;
virtual void deserialize(std::istream& _istr);
};
template<class T>
class HalfFacePropertyT : public PropertyPtr<OpenVolumeMeshPropertyT<T>, HalfFacePropHandle> {
public:
HalfFacePropertyT(const std::string& _name, ResourceManager& _resMan, HalfFacePropHandle _handle);
~HalfFacePropertyT() {}
virtual std::ostream& serialize(std::ostream& _ostr) const;
virtual std::istream& deserialize(std::istream& _istr);
virtual void serialize(std::ostream& _ostr) const;
virtual void deserialize(std::istream& _istr);
};
template<class T>
class CellPropertyT : public PropertyPtr<OpenVolumeMeshPropertyT<T>, CellPropHandle> {
public:
CellPropertyT(const std::string& _name, ResourceManager& _resMan, CellPropHandle _handle);
~CellPropertyT() {}
virtual std::ostream& serialize(std::ostream& _ostr) const;
virtual std::istream& deserialize(std::istream& _istr);
virtual void serialize(std::ostream& _ostr) const;
virtual void deserialize(std::istream& _istr);
};
template<class T>
class MeshPropertyT : public PropertyPtr<OpenVolumeMeshPropertyT<T>, MeshPropHandle> {
public:
MeshPropertyT(const std::string& _name, ResourceManager& _resMan, MeshPropHandle _handle);
~MeshPropertyT() {}
virtual std::ostream& serialize(std::ostream& _ostr) const;
virtual std::istream& deserialize(std::istream& _istr);
virtual void serialize(std::ostream& _ostr) const;
virtual void deserialize(std::istream& _istr);
};
template <class T>
const std::string typeName() { return typeid(T).name(); }
} // Namespace OpenVolumeMesh
#if defined(INCLUDE_TEMPLATES) && !defined(PROPERTYDEFINEST_CC)
......
......@@ -55,16 +55,15 @@ VertexPropertyT<T>::VertexPropertyT(const std::string& _name, ResourceManager& _
}
template<class T>
std::ostream& VertexPropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "VProp" << std::endl;
_ostr << PropertyPtr<OpenVolumeMeshPropertyT<T>, VertexPropHandle>::get()->serialize(_ostr);
return _ostr;
void VertexPropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "VProp ";
_ostr << typeName<T>() << " ";
PropertyPtr<OpenVolumeMeshPropertyT<T>, VertexPropHandle>::get()->serialize(_ostr);
}
template<class T>
std::istream& VertexPropertyT<T>::deserialize(std::istream& _istr) {
void VertexPropertyT<T>::deserialize(std::istream& _istr) {
PropertyPtr<OpenVolumeMeshPropertyT<T>, VertexPropHandle>::get()->deserialize(_istr);
return _istr;
}
template<class T>
......@@ -74,16 +73,15 @@ EdgePropertyT<T>::EdgePropertyT(const std::string& _name, ResourceManager& _resM
}
template<class T>
std::ostream& EdgePropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "EProp" << std::endl;
_ostr << PropertyPtr<OpenVolumeMeshPropertyT<T>, EdgePropHandle>::get()->serialize(_ostr);
return _ostr;
void EdgePropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "EProp ";
_ostr << typeName<T>() << " ";
PropertyPtr<OpenVolumeMeshPropertyT<T>, EdgePropHandle>::get()->serialize(_ostr);
}
template<class T>
std::istream& EdgePropertyT<T>::deserialize(std::istream& _istr) {
void EdgePropertyT<T>::deserialize(std::istream& _istr) {
PropertyPtr<OpenVolumeMeshPropertyT<T>, EdgePropHandle>::get()->deserialize(_istr);
return _istr;
}
template<class T>
......@@ -93,16 +91,15 @@ HalfEdgePropertyT<T>::HalfEdgePropertyT(const std::string& _name, ResourceManage
}
template<class T>
std::ostream& HalfEdgePropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "HEProp" << std::endl;
_ostr << PropertyPtr<OpenVolumeMeshPropertyT<T>, HalfEdgePropHandle>::get()->serialize(_ostr);
return _ostr;
void HalfEdgePropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "HEProp ";
_ostr << typeName<T>() << " ";
PropertyPtr<OpenVolumeMeshPropertyT<T>, HalfEdgePropHandle>::get()->serialize(_ostr);
}
template<class T>
std::istream& HalfEdgePropertyT<T>::deserialize(std::istream& _istr) {
void HalfEdgePropertyT<T>::deserialize(std::istream& _istr) {
PropertyPtr<OpenVolumeMeshPropertyT<T>, HalfEdgePropHandle>::get()->deserialize(_istr);
return _istr;
}
template<class T>
......@@ -112,16 +109,15 @@ FacePropertyT<T>::FacePropertyT(const std::string& _name, ResourceManager& _resM
}
template<class T>
std::ostream& FacePropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "FProp" << std::endl;
_ostr << PropertyPtr<OpenVolumeMeshPropertyT<T>, FacePropHandle>::get()->serialize(_ostr);
return _ostr;
void FacePropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "FProp ";
_ostr << typeName<T>() << " ";
PropertyPtr<OpenVolumeMeshPropertyT<T>, FacePropHandle>::get()->serialize(_ostr);
}
template<class T>
std::istream& FacePropertyT<T>::deserialize(std::istream& _istr) {
void FacePropertyT<T>::deserialize(std::istream& _istr) {
PropertyPtr<OpenVolumeMeshPropertyT<T>, FacePropHandle>::get()->deserialize(_istr);
return _istr;
}
template<class T>
......@@ -131,16 +127,15 @@ HalfFacePropertyT<T>::HalfFacePropertyT(const std::string& _name, ResourceManage
}
template<class T>
std::ostream& HalfFacePropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "HFProp" << std::endl;
_ostr << PropertyPtr<OpenVolumeMeshPropertyT<T>, HalfFacePropHandle>::get()->serialize(_ostr);
return _ostr;
void HalfFacePropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "HFProp ";
_ostr << typeName<T>() << " ";
PropertyPtr<OpenVolumeMeshPropertyT<T>, HalfFacePropHandle>::get()->serialize(_ostr);
}
template<class T>
std::istream& HalfFacePropertyT<T>::deserialize(std::istream& _istr) {
void HalfFacePropertyT<T>::deserialize(std::istream& _istr) {
PropertyPtr<OpenVolumeMeshPropertyT<T>, HalfFacePropHandle>::get()->deserialize(_istr);
return _istr;
}
template<class T>
......@@ -150,16 +145,15 @@ CellPropertyT<T>::CellPropertyT(const std::string& _name, ResourceManager& _resM
}
template<class T>
std::ostream& CellPropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "CProp" << std::endl;
_ostr << PropertyPtr<OpenVolumeMeshPropertyT<T>, CellPropHandle>::get()->serialize(_ostr);
return _ostr;
void CellPropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "CProp ";
_ostr << typeName<T>() << " ";
PropertyPtr<OpenVolumeMeshPropertyT<T>, CellPropHandle>::get()->serialize(_ostr);
}
template<class T>
std::istream& CellPropertyT<T>::deserialize(std::istream& _istr) {
void CellPropertyT<T>::deserialize(std::istream& _istr) {
PropertyPtr<OpenVolumeMeshPropertyT<T>, CellPropHandle>::get()->deserialize(_istr);
return _istr;
}
template<class T>
......@@ -169,16 +163,15 @@ MeshPropertyT<T>::MeshPropertyT(const std::string& _name, ResourceManager& _resM
}
template<class T>
std::ostream& MeshPropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "MProp" << std::endl;
_ostr << PropertyPtr<OpenVolumeMeshPropertyT<T>, MeshPropHandle>::get()->serialize(_ostr);
return _ostr;
void MeshPropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "MProp ";
_ostr << typeName<T>() << " ";
PropertyPtr<OpenVolumeMeshPropertyT<T>, MeshPropHandle>::get()->serialize(_ostr);
}
template<class T>
std::istream& MeshPropertyT<T>::deserialize(std::istream& _istr) {
void MeshPropertyT<T>::deserialize(std::istream& _istr) {
PropertyPtr<OpenVolumeMeshPropertyT<T>, MeshPropHandle>::get()->deserialize(_istr);
return _istr;
}
} // Namespace OpenVolumeMesh
......@@ -99,6 +99,8 @@ public:
virtual bool persistent() const { return ptr::shared_ptr<PropT>::get()->persistent(); }
virtual bool anonymous() const { return ptr::shared_ptr<PropT>::get()->name().empty(); }
protected:
virtual void resize(unsigned int _size);
......
......@@ -89,6 +89,8 @@ public:
/// Change size of stored cell properties
void resize_cprops(unsigned int _nc);
protected:
void vertex_deleted(const VertexHandle& _h);
void edge_deleted(const EdgeHandle& _h);
......@@ -97,6 +99,22 @@ public:
void cell_deleted(const CellHandle& _h);
public:
void clear_vertex_props() { clearVec(vertex_props_); }
void clear_edge_props() { clearVec(edge_props_); }
void clear_halfedge_props() { clearVec(halfedge_props_); }
void clear_face_props() { clearVec(face_props_); }
void clear_halfface_props() { clearVec(halfface_props_); }
void clear_cell_props() { clearVec(cell_props_); }
void clear_mesh_props() { clearVec(mesh_props_); }
/// Get number of vertices in mesh
virtual unsigned int n_vertices() const = 0;
/// Get number of edges in mesh
......@@ -170,6 +188,36 @@ public:
template<class T> void set_persistent(MeshPropertyT<T>& _prop, bool _flag = true);
typedef std::vector<BaseProperty*> Properties;
Properties::const_iterator vertex_props_begin() const { return vertex_props_.begin(); }
Properties::const_iterator vertex_props_end() const { return vertex_props_.end(); }
Properties::const_iterator edge_props_begin() const { return edge_props_.begin(); }
Properties::const_iterator edge_props_end() const { return edge_props_.end(); }
Properties::const_iterator halfedge_props_begin() const { return halfedge_props_.begin(); }
Properties::const_iterator halfedge_props_end() const { return halfedge_props_.end(); }
Properties::const_iterator face_props_begin() const { return face_props_.begin(); }
Properties::const_iterator face_props_end() const { return face_props_.end(); }
Properties::const_iterator halfface_props_begin() const { return halfface_props_.begin(); }
Properties::const_iterator halfface_props_end() const { return halfface_props_.end(); }
Properties::const_iterator cell_props_begin() const { return cell_props_.begin(); }
Properties::const_iterator cell_props_end() const { return cell_props_.end(); }
Properties::const_iterator mesh_props_begin() const { return mesh_props_.begin(); }
Properties::const_iterator mesh_props_end() const { return mesh_props_.end(); }
private:
template<class StdVecT>
......@@ -190,19 +238,19 @@ private:
template<class StdVecT>
void clearVec(StdVecT& _vec);
std::vector<BaseProperty*> vertex_props_;
Properties vertex_props_;
std::vector<BaseProperty*> edge_props_;
Properties edge_props_;
std::vector<BaseProperty*> halfedge_props_;
Properties halfedge_props_;
std::vector<BaseProperty*> face_props_;
Properties face_props_;
std::vector<BaseProperty*> halfface_props_;
Properties halfface_props_;
std::vector<BaseProperty*> cell_props_;
Properties cell_props_;
std::vector<BaseProperty*> mesh_props_;
Properties mesh_props_;
};
}
......
......@@ -200,6 +200,15 @@ void ResourceManager::entity_deleted(StdVecT& _vec, const OpenVolumeMeshHandle&
template<class StdVecT>
void ResourceManager::clearVec(StdVecT& _vec) {
for(typename StdVecT::iterator it = _vec.begin();
it != _vec.end(); ++it) {
if(!(*it)->persistent()) {
std::cerr << "Could not clear properties since at " <<
"least one property is still in use!" << std::endl;
return;
}
}
for(typename StdVecT::iterator it = _vec.begin();
it != _vec.end(); ++it) {
delete *it;
......
......@@ -461,7 +461,7 @@ public:
}
/// Clear whole mesh
virtual void clear() {
virtual void clear(bool _clearProps = true) {
edges_.clear();
faces_.clear();
......@@ -471,11 +471,24 @@ public:
incident_cell_per_hf_.clear();
boundary_faces_.clear();
// Resize props
resize_vprops(0u);
resize_eprops(0u);
resize_fprops(0u);
resize_cprops(0u);
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);
}
}
//=====================================================================
......
......@@ -109,6 +109,10 @@ public:
private:
// Write props
template<class IteratorT>
void writeProps(std::ostream& _ostr, const IteratorT& _begin, const IteratorT& _end) const;
// Remove leading and trailing whitespaces
void trimString(std::string& _string) const;
......
......@@ -370,6 +370,21 @@ bool FileManager::writeFile(const std::string& _filename, const MeshT& _mesh) co
off << std::endl;
}
// write vertex props
writeProps(off, _mesh.vertex_props_begin(), _mesh.vertex_props_end());
// write edge props
writeProps(off, _mesh.edge_props_begin(), _mesh.edge_props_end());
// write halfedge props
writeProps(off, _mesh.halfedge_props_begin(), _mesh.halfedge_props_end());
// write face props
writeProps(off, _mesh.face_props_begin(), _mesh.face_props_end());
// write halfface props
writeProps(off, _mesh.halfface_props_begin(), _mesh.halfface_props_end());
// write cell props
writeProps(off, _mesh.cell_props_begin(), _mesh.cell_props_end());
// write mesh props
writeProps(off, _mesh.mesh_props_begin(), _mesh.mesh_props_end());
off.close();
return true;
......@@ -377,6 +392,23 @@ bool FileManager::writeFile(const std::string& _filename, const MeshT& _mesh) co
//==================================================
template<class IteratorT>
void FileManager::writeProps(std::ostream& _ostr, const IteratorT& _begin, const IteratorT& _end) const {
// write props
for(IteratorT p_it = _begin;
p_it != _end; ++p_it) {
if(!(*p_it)->persistent()) continue;
if((*p_it)->anonymous()) {
std::cerr << "Serialization of anonymous properties is not supported!" << std::endl;
continue;
}
(*p_it)->serialize(_ostr);
}
}
//==================================================
} // Namespace IO
} // Namespace FileManager
......@@ -67,6 +67,47 @@ TEST_F(PolyhedralMeshBase, SaveFile) {
EXPECT_EQ(288u, mesh_.n_cells());
}
TEST_F(PolyhedralMeshBase, SaveFileWithProps) {
OpenVolumeMesh::IO::FileManager fileManager;
ASSERT_TRUE(fileManager.readFile("Cylinder.ovm", mesh_));
EXPECT_EQ(399u, mesh_.n_vertices());
EXPECT_EQ(1070u, mesh_.n_edges());
EXPECT_EQ(960u, mesh_.n_faces());
EXPECT_EQ(288u, mesh_.n_cells());
// Attach non-persistent properties
HalfFacePropertyT<float> hfprop = mesh_.request_halfface_property<float>("MyHalfFaceProp");
VertexPropertyT<unsigned int> vprop = mesh_.request_vertex_property<unsigned int>("MyVertexProp");
for(unsigned int i = 0; i < mesh_.n_halffaces(); ++i) {
hfprop[i] = (float)i/2.0f;
}
for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
vprop[i] = i;
}
mesh_.set_persistent(hfprop);
mesh_.set_persistent(vprop);
// Write file
ASSERT_TRUE(fileManager.writeFile("Cylinder.copy.ovm", mesh_));
mesh_.clear();
ASSERT_TRUE(fileManager.readFile("Cylinder.copy.ovm", mesh_));
EXPECT_EQ(399u, mesh_.n_vertices());
EXPECT_EQ(1070u, mesh_.n_edges());
EXPECT_EQ(960u, mesh_.n_faces());
EXPECT_EQ(288u, mesh_.n_cells());
EXPECT_EQ(0u, mesh_.n_halfface_props());
EXPECT_EQ(0u, mesh_.n_vertex_props());
}
//TEST_F(PolyhedralMeshBase, LoadFileWithProps) {
//
// OpenVolumeMesh::IO::FileManager fileManager;
......
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