Commit be1d02fc authored by Mike Kremer's avatar Mike Kremer

Integrated serialization of vector-valued properties.

git-svn-id: http://www.openvolumemesh.org/svnrepo/OpenVolumeMesh/trunk@250 66977474-1d4b-4f09-8fe9-267525286df2
parent bdd11d20
......@@ -77,6 +77,10 @@ public:
virtual bool anonymous() const = 0;
virtual const std::string entityType() const = 0;
virtual const std::string typeNameWrapper() const = 0;
protected:
virtual void delete_multiple_entries(const std::vector<bool>& _tags) = 0;
......
......@@ -111,9 +111,7 @@ public:
}
// Function to serialize a property
virtual void serialize(std::ostream& _ostr) const {
_ostr << "\"" << name_ << "\"" << std::endl;
}
virtual void serialize(std::ostream& /*_ostr*/) const {}
// Function to deserialize a property
virtual void deserialize(std::istream& /*_istr*/) {}
......
......@@ -145,7 +145,6 @@ public:
// Function to serialize a property
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;
......
......@@ -42,6 +42,8 @@
#include "PropertyDefines.hh"
#include "../Geometry/VectorT.hh"
namespace OpenVolumeMesh {
template <> const std::string typeName<int>() { return "int"; }
......
......@@ -59,79 +59,93 @@ class PropertyPtr;
class ResourceManager;
template <class T>
const std::string typeName();
template <> const std::string typeName<int>();
template <> const std::string typeName<unsigned int>();
template <> const std::string typeName<short>();
template <> const std::string typeName<long>();
template <> const std::string typeName<unsigned long>();
template <> const std::string typeName<char>();
template <> const std::string typeName<unsigned char>();
template <> const std::string typeName<bool>();
template <> const std::string typeName<float>();
template <> const std::string typeName<double>();
template <> const std::string typeName<std::string>();
/// Property classes for the different entity types
template<class T>
class VertexPropertyT : public PropertyPtr<OpenVolumeMeshPropertyT<T>, VertexPropHandle> {
public:
VertexPropertyT(const std::string& _name, ResourceManager& _resMan, VertexPropHandle _handle, const T _def = T());
~VertexPropertyT() {}
virtual ~VertexPropertyT() {}
virtual void serialize(std::ostream& _ostr) const;
virtual void deserialize(std::istream& _istr);
virtual const std::string entityType() const { return "VProp"; }
virtual const std::string typeNameWrapper() const { return typeName<T>(); }
};
template<class T>
class EdgePropertyT : public PropertyPtr<OpenVolumeMeshPropertyT<T>, EdgePropHandle> {
public:
EdgePropertyT(const std::string& _name, ResourceManager& _resMan, EdgePropHandle _handle, const T _def = T());
~EdgePropertyT() {}
virtual ~EdgePropertyT() {}
virtual void serialize(std::ostream& _ostr) const;
virtual void deserialize(std::istream& _istr);
virtual const std::string entityType() const { return "EProp"; }
virtual const std::string typeNameWrapper() const { return typeName<T>(); }
};
template<class T>
class HalfEdgePropertyT : public PropertyPtr<OpenVolumeMeshPropertyT<T>, HalfEdgePropHandle> {
public:
HalfEdgePropertyT(const std::string& _name, ResourceManager& _resMan, HalfEdgePropHandle _handle, const T _def = T());
~HalfEdgePropertyT() {}
virtual ~HalfEdgePropertyT() {}
virtual void serialize(std::ostream& _ostr) const;
virtual void deserialize(std::istream& _istr);
virtual const std::string entityType() const { return "HEProp"; }
virtual const std::string typeNameWrapper() const { return typeName<T>(); }
};
template<class T>
class FacePropertyT : public PropertyPtr<OpenVolumeMeshPropertyT<T>, FacePropHandle> {
public:
FacePropertyT(const std::string& _name, ResourceManager& _resMan, FacePropHandle _handle, const T _def = T());
~FacePropertyT() {}
virtual ~FacePropertyT() {}
virtual void serialize(std::ostream& _ostr) const;
virtual void deserialize(std::istream& _istr);
virtual const std::string entityType() const { return "FProp"; }
virtual const std::string typeNameWrapper() const { return typeName<T>(); }
};
template<class T>
class HalfFacePropertyT : public PropertyPtr<OpenVolumeMeshPropertyT<T>, HalfFacePropHandle> {
public:
HalfFacePropertyT(const std::string& _name, ResourceManager& _resMan, HalfFacePropHandle _handle, const T _def = T());
~HalfFacePropertyT() {}
virtual ~HalfFacePropertyT() {}
virtual void serialize(std::ostream& _ostr) const;
virtual void deserialize(std::istream& _istr);
virtual const std::string entityType() const { return "HFProp"; }
virtual const std::string typeNameWrapper() const { return typeName<T>(); }
};
template<class T>
class CellPropertyT : public PropertyPtr<OpenVolumeMeshPropertyT<T>, CellPropHandle> {
public:
CellPropertyT(const std::string& _name, ResourceManager& _resMan, CellPropHandle _handle, const T _def = T());
~CellPropertyT() {}
virtual ~CellPropertyT() {}
virtual void serialize(std::ostream& _ostr) const;
virtual void deserialize(std::istream& _istr);
virtual const std::string entityType() const { return "CProp"; }
virtual const std::string typeNameWrapper() const { return typeName<T>(); }
};
template<class T>
class MeshPropertyT : public PropertyPtr<OpenVolumeMeshPropertyT<T>, MeshPropHandle> {
public:
MeshPropertyT(const std::string& _name, ResourceManager& _resMan, MeshPropHandle _handle, const T _def = T());
~MeshPropertyT() {}
virtual ~MeshPropertyT() {}
virtual void serialize(std::ostream& _ostr) const;
virtual void deserialize(std::istream& _istr);
virtual const std::string entityType() const { return "MProp"; }
virtual const std::string typeNameWrapper() const { return typeName<T>(); }
};
template <class T>
const std::string typeName();
template <> const std::string typeName<int>();
template <> const std::string typeName<unsigned int>();
template <> const std::string typeName<short>();
template <> const std::string typeName<long>();
template <> const std::string typeName<unsigned long>();
template <> const std::string typeName<char>();
template <> const std::string typeName<unsigned char>();
template <> const std::string typeName<bool>();
template <> const std::string typeName<float>();
template <> const std::string typeName<double>();
template <> const std::string typeName<std::string>();
} // Namespace OpenVolumeMesh
#if defined(INCLUDE_TEMPLATES) && !defined(PROPERTYDEFINEST_CC)
......
......@@ -56,8 +56,6 @@ VertexPropertyT<T>::VertexPropertyT(const std::string& _name, ResourceManager& _
template<class T>
void VertexPropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "VProp ";
_ostr << typeName<T>() << " ";
PropertyPtr<OpenVolumeMeshPropertyT<T>, VertexPropHandle>::get()->serialize(_ostr);
}
......@@ -74,8 +72,6 @@ EdgePropertyT<T>::EdgePropertyT(const std::string& _name, ResourceManager& _resM
template<class T>
void EdgePropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "EProp ";
_ostr << typeName<T>() << " ";
PropertyPtr<OpenVolumeMeshPropertyT<T>, EdgePropHandle>::get()->serialize(_ostr);
}
......@@ -92,8 +88,6 @@ HalfEdgePropertyT<T>::HalfEdgePropertyT(const std::string& _name, ResourceManage
template<class T>
void HalfEdgePropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "HEProp ";
_ostr << typeName<T>() << " ";
PropertyPtr<OpenVolumeMeshPropertyT<T>, HalfEdgePropHandle>::get()->serialize(_ostr);
}
......@@ -110,8 +104,6 @@ FacePropertyT<T>::FacePropertyT(const std::string& _name, ResourceManager& _resM
template<class T>
void FacePropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "FProp ";
_ostr << typeName<T>() << " ";
PropertyPtr<OpenVolumeMeshPropertyT<T>, FacePropHandle>::get()->serialize(_ostr);
}
......@@ -128,8 +120,6 @@ HalfFacePropertyT<T>::HalfFacePropertyT(const std::string& _name, ResourceManage
template<class T>
void HalfFacePropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "HFProp ";
_ostr << typeName<T>() << " ";
PropertyPtr<OpenVolumeMeshPropertyT<T>, HalfFacePropHandle>::get()->serialize(_ostr);
}
......@@ -146,8 +136,6 @@ CellPropertyT<T>::CellPropertyT(const std::string& _name, ResourceManager& _resM
template<class T>
void CellPropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "CProp ";
_ostr << typeName<T>() << " ";
PropertyPtr<OpenVolumeMeshPropertyT<T>, CellPropHandle>::get()->serialize(_ostr);
}
......@@ -164,8 +152,6 @@ MeshPropertyT<T>::MeshPropertyT(const std::string& _name, ResourceManager& _resM
template<class T>
void MeshPropertyT<T>::serialize(std::ostream& _ostr) const {
_ostr << "MProp ";
_ostr << typeName<T>() << " ";
PropertyPtr<OpenVolumeMeshPropertyT<T>, MeshPropHandle>::get()->serialize(_ostr);
}
......
......@@ -58,6 +58,8 @@ namespace OpenVolumeMesh {
namespace IO {
using namespace OpenVolumeMesh::Geometry;
//==================================================
template <class MeshT>
......@@ -329,6 +331,24 @@ void FileManager::readProperty(std::istream& _iff, MeshT& _mesh) const {
else if(prop_t == typeName<float>()) generateGenericProperty<float, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<double>()) generateGenericProperty<double, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<std::string>()) generateGenericProperty<std::string, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<Vec2f>()) generateGenericProperty<Vec2f, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<Vec2d>()) generateGenericProperty<Vec2d, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<Vec2i>()) generateGenericProperty<Vec2i, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<Vec2ui>()) generateGenericProperty<Vec2ui, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<Vec3f>()) generateGenericProperty<Vec3f, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<Vec3d>()) generateGenericProperty<Vec3d, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<Vec3i>()) generateGenericProperty<Vec3i, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<Vec3ui>()) generateGenericProperty<Vec3ui, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<Vec4f>()) generateGenericProperty<Vec4f, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<Vec4d>()) generateGenericProperty<Vec4d, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<Vec4i>()) generateGenericProperty<Vec4i, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<Vec4ui>()) generateGenericProperty<Vec4ui, MeshT>(entity_t, name, _iff, _mesh);
}
//==================================================
......@@ -488,6 +508,11 @@ void FileManager::writeProps(std::ostream& _ostr, const IteratorT& _begin, const
std::cerr << "Serialization of anonymous properties is not supported!" << std::endl;
continue;
}
_ostr << (*p_it)->entityType() << " ";
_ostr << (*p_it)->typeNameWrapper() << " ";
_ostr << "\"" << (*p_it)->name() << "\"" << std::endl;
(*p_it)->serialize(_ostr);
}
}
......
/*
* Vector.cc
*
* Created on: Aug 8, 2013
* Author: kremer
*/
#include <string>
#include "VectorT.hh"
namespace OpenVolumeMesh {
using namespace Geometry;
template <> const std::string typeName<Vec2f>() { return "vec2f"; }
template <> const std::string typeName<Vec2d>() { return "vec2d"; }
template <> const std::string typeName<Vec2i>() { return "vec2i"; }
template <> const std::string typeName<Vec2ui>() { return "vec2ui"; }
template <> const std::string typeName<Vec3f>() { return "vec3f"; }
template <> const std::string typeName<Vec3d>() { return "vec3d"; }
template <> const std::string typeName<Vec3i>() { return "vec3i"; }
template <> const std::string typeName<Vec3ui>() { return "vec3ui"; }
template <> const std::string typeName<Vec4f>() { return "vec4f"; }
template <> const std::string typeName<Vec4d>() { return "vec4d"; }
template <> const std::string typeName<Vec4i>() { return "vec4i"; }
template <> const std::string typeName<Vec4ui>() { return "vec4ui"; }
}
......@@ -99,7 +99,7 @@ template <typename Scalar,int N> struct VectorDataT
/// This specialization enables us to use aligned SSE instructions.
template <> struct VectorDataT<float, 4>
{
union
union
{
__m128 m128;
float values_[4];
......@@ -141,7 +141,7 @@ template <> struct VectorDataT<float, 4>
#define TEMPLATE_HEADER template <typename Scalar>
#define CLASSNAME VectorT<Scalar,DIM>
#define CLASSNAME VectorT<Scalar,DIM>
#define DERIVED VectorDataT<Scalar,DIM>
......@@ -191,21 +191,21 @@ template <> struct VectorDataT<float, 4>
/// cross product for Vec3f
template<>
inline VectorT<float,3>
VectorT<float,3>::operator%(const VectorT<float,3>& _rhs) const
VectorT<float,3>::operator%(const VectorT<float,3>& _rhs) const
{
return
return
VectorT<float,3>(values_[1]*_rhs.values_[2]-values_[2]*_rhs.values_[1],
values_[2]*_rhs.values_[0]-values_[0]*_rhs.values_[2],
values_[0]*_rhs.values_[1]-values_[1]*_rhs.values_[0]);
}
/// cross product for Vec3d
template<>
inline VectorT<double,3>
VectorT<double,3>::operator%(const VectorT<double,3>& _rhs) const
{
return
return
VectorT<double,3>(values_[1]*_rhs.values_[2]-values_[2]*_rhs.values_[1],
values_[2]*_rhs.values_[0]-values_[0]*_rhs.values_[2],
values_[0]*_rhs.values_[1]-values_[1]*_rhs.values_[0]);
......@@ -229,16 +229,16 @@ inline VectorT<Scalar,N> operator*(Scalar _s, const VectorT<Scalar,N>& _v) {
/// \relates OpenVolumeMesh::VectorT
/// symmetric version of the dot product
template<typename Scalar, int N>
inline Scalar
inline Scalar
dot(const VectorT<Scalar,N>& _v1, const VectorT<Scalar,N>& _v2) {
return (_v1 | _v2);
return (_v1 | _v2);
}
/// \relates OpenVolumeMesh::VectorT
/// symmetric version of the cross product
template<typename Scalar, int N>
inline VectorT<Scalar,N>
inline VectorT<Scalar,N>
cross(const VectorT<Scalar,N>& _v1, const VectorT<Scalar,N>& _v2) {
return (_v1 % _v2);
}
......@@ -335,6 +335,27 @@ typedef VectorT<double,6> Vec6d;
//=============================================================================
} // namespace Geometry
using namespace Geometry;
template <class T>
const std::string typeName();
template <> const std::string typeName<Vec2f>();
template <> const std::string typeName<Vec2d>();
template <> const std::string typeName<Vec2i>();
template <> const std::string typeName<Vec2ui>();
template <> const std::string typeName<Vec3f>();
template <> const std::string typeName<Vec3d>();
template <> const std::string typeName<Vec3i>();
template <> const std::string typeName<Vec3ui>();
template <> const std::string typeName<Vec4f>();
template <> const std::string typeName<Vec4d>();
template <> const std::string typeName<Vec4i>();
template <> const std::string typeName<Vec4ui>();
} // namespace OpenVolumeMesh
//=============================================================================
#endif // OPENVOLUMEMESH_VECTOR_HH defined
......
......@@ -118,6 +118,125 @@ TEST_F(PolyhedralMeshBase, SaveFileWithProps) {
}
}
TEST_F(PolyhedralMeshBase, SaveFileWithVectorProps) {
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<Vec3d> hfprop = mesh_.request_halfface_property<Vec3d>("MyHalfFaceProp");
VertexPropertyT<Vec2i> vprop = mesh_.request_vertex_property<Vec2i>("MyVertexProp");
for(unsigned int i = 0; i < mesh_.n_halffaces(); ++i) {
hfprop[i] = Vec3d((double)i/2.0, (double)i/2.0, (double)i/2.0);
}
for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
vprop[i] = Vec2i(i, 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(1u, mesh_.n_halfface_props());
EXPECT_EQ(1u, mesh_.n_vertex_props());
HalfFacePropertyT<Vec3d> hfprop2 = mesh_.request_halfface_property<Vec3d>("MyHalfFaceProp");
VertexPropertyT<Vec2i> vprop2 = mesh_.request_vertex_property<Vec2i>("MyVertexProp");
for(unsigned int i = 0; i < mesh_.n_halffaces(); ++i) {
EXPECT_FLOAT_EQ((double)i/2.0, hfprop2[i][0]);
EXPECT_FLOAT_EQ((double)i/2.0, hfprop2[i][1]);
EXPECT_FLOAT_EQ((double)i/2.0, hfprop2[i][2]);
}
for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
EXPECT_EQ(i, vprop2[i][0]);
EXPECT_EQ(i, vprop2[i][1]);
}
}
TEST_F(PolyhedralMeshBase, SerializeVectorValuedProperties) {
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 persistent properties
HalfFacePropertyT<Vec3d> hfprop = mesh_.request_halfface_property<Vec3d>("MyHalfFaceProp");
VertexPropertyT<Vec2i> vprop = mesh_.request_vertex_property<Vec2i>("MyVertexProp");
for(unsigned int i = 0; i < mesh_.n_halffaces(); ++i) {
hfprop[i] = Vec3d((double)i/2.0, (double)i/2.0, (double)i/2.0);
}
for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
vprop[i] = Vec2i(i, i, i);
}
mesh_.set_persistent(hfprop);
mesh_.set_persistent(vprop);
std::ofstream ofs1("hfVecPropTest");
std::ofstream ofs2("vVecPropTest");
hfprop.serialize(ofs1);
vprop.serialize(ofs2);
ofs1.close();
ofs2.close();
/*
* Change property values
*/
for(unsigned int i = 0; i < mesh_.n_halffaces(); ++i) {
hfprop[i] = Vec3d((double)i/3.0, (double)i/3.0, (double)i/3.0);
}
for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
vprop[i] = Vec2i(2*i, 2*i, 2*i);
}
std::ifstream ifs1("hfVecPropTest");
std::ifstream ifs2("vVecPropTest");
hfprop.deserialize(ifs1);
vprop.deserialize(ifs2);
ifs1.close();
ifs2.close();
for(unsigned int i = 0; i < mesh_.n_halffaces(); ++i) {
EXPECT_FLOAT_EQ((double)i/2.0, hfprop[i][0]);
EXPECT_FLOAT_EQ((double)i/2.0, hfprop[i][1]);
EXPECT_FLOAT_EQ((double)i/2.0, hfprop[i][2]);
}
for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
EXPECT_EQ(i, vprop[i][0]);
EXPECT_EQ(i, vprop[i][1]);
}
}
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