Commit 67e63fff authored by Mike Kremer's avatar Mike Kremer

Implemented (de-)serialization of properties for integral types

git-svn-id: http://www.openvolumemesh.org/svnrepo/OpenVolumeMesh/trunk@143 66977474-1d4b-4f09-8fe9-267525286df2
parent 6c0b01ce
......@@ -114,11 +114,7 @@ public:
}
// Function to deserialize a property
virtual void deserialize(std::istream& _istr) {
std::string str;
_istr >> str;
name_ = str;
}
virtual void deserialize(std::istream& /*_istr*/) {}
public:
// I/O support
......
......@@ -149,7 +149,6 @@ public:
// Function to deserialize a property
virtual void deserialize(std::istream& _istr) {
OpenVolumeMeshBaseProperty::deserialize(_istr);
for(unsigned int i = 0; i < n_elements(); ++i) {
value_type val;
_istr >> val;
......
/*===========================================================================*\
* *
* OpenVolumeMesh *
* Copyright (C) 2011 by Computer Graphics Group, RWTH Aachen *
* www.openvolumemesh.org *
* *
*---------------------------------------------------------------------------*
* This file is part of OpenVolumeMesh. *
* *
* OpenVolumeMesh is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version with the *
* following exceptions: *
* *
* If other files instantiate templates or use macros *
* or inline functions from this file, or you compile this file and *
* link it with other files to produce an executable, this file does *
* not by itself cause the resulting executable to be covered by the *
* GNU Lesser General Public License. This exception does not however *
* invalidate any other reasons why the executable file might be *
* covered by the GNU Lesser General Public License. *
* *
* OpenVolumeMesh is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU LesserGeneral Public *
* License along with OpenVolumeMesh. If not, *
* see <http://www.gnu.org/licenses/>. *
* *
\*===========================================================================*/
/*===========================================================================*\
* *
* $Revision: 36 $ *
* $Date: 2012-01-10 18:00:06 +0100 (Di, 10 Jan 2012) $ *
* $LastChangedBy: kremer $ *
* *
\*===========================================================================*/
#include "PropertyDefines.hh"
namespace OpenVolumeMesh {
template <> const std::string typeName<int>() { return "int"; }
template <> const std::string typeName<unsigned int>() { return "uint"; }
template <> const std::string typeName<short>() { return "short"; }
template <> const std::string typeName<long>() { return "long"; }
template <> const std::string typeName<unsigned long>() { return "ulong"; }
template <> const std::string typeName<char>() { return "char"; }
template <> const std::string typeName<unsigned char>() { return "uchar"; }
template <> const std::string typeName<bool>() { return "bool"; }
template <> const std::string typeName<float>() { return "float"; }
template <> const std::string typeName<double>() { return "double"; }
template <> const std::string typeName<std::string>() { return "string"; }
} // Namespace OpenVolumeMesh
......@@ -45,6 +45,7 @@
#include <iostream>
#include <typeinfo>
#include <stdexcept>
#include "BaseProperty.hh"
#include "PropertyHandles.hh"
......@@ -117,7 +118,19 @@ public:
};
template <class T>
const std::string typeName() { return typeid(T).name(); }
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
......
......@@ -174,4 +174,9 @@ void MeshPropertyT<T>::deserialize(std::istream& _istr) {
PropertyPtr<OpenVolumeMeshPropertyT<T>, MeshPropHandle>::get()->deserialize(_istr);
}
template <class T>
const std::string typeName() {
throw std::runtime_error("Serialization is not supported for these data types!");
}
} // Namespace OpenVolumeMesh
......@@ -109,6 +109,14 @@ public:
private:
// Read property
template <class MeshT>
void readProperty(std::istream& _iff, MeshT& _mesh) const;
template <class PropT, class MeshT>
void generateGenericProperty(const std::string& _entity_t, const std::string& _name,
std::istream& _iff, MeshT& _mesh) const;
// Write props
template<class IteratorT>
void writeProps(std::ostream& _ostr, const IteratorT& _begin, const IteratorT& _end) const;
......
......@@ -266,6 +266,14 @@ bool FileManager::readFile(const std::string& _filename, MeshT& _mesh,
}
}
while(!iff.eof()) {
// "End of file reached while searching for input!"
// is thrown here. \TODO Fix it!
// Read property
readProperty(iff, _mesh);
}
iff.close();
// Compute top-down-adjacencies
......@@ -285,6 +293,77 @@ bool FileManager::readFile(const std::string& _filename, MeshT& _mesh,
//==================================================
template <class MeshT>
void FileManager::readProperty(std::istream& _iff, MeshT& _mesh) const {
std::string line, entity_t, prop_t, name;
std::stringstream sstr;
getCleanLine(_iff, line);
if(line.empty()) return;
sstr.clear();
sstr.str(line);
sstr >> entity_t;
std::transform(entity_t.begin(), entity_t.end(), entity_t.begin(), ::tolower);
sstr >> prop_t;
std::transform(prop_t.begin(), prop_t.end(), prop_t.begin(), ::tolower);
name = line;
extractQuotedText(name);
if(prop_t == typeName<int>()) generateGenericProperty<int, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<unsigned int>()) generateGenericProperty<unsigned int, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<short>()) generateGenericProperty<short, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<long>()) generateGenericProperty<long, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<unsigned long>()) generateGenericProperty<unsigned long, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<char>()) generateGenericProperty<char, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<unsigned char>()) generateGenericProperty<unsigned char, MeshT>(entity_t, name, _iff, _mesh);
else if(prop_t == typeName<bool>()) generateGenericProperty<bool, MeshT>(entity_t, name, _iff, _mesh);
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);
}
//==================================================
template <class PropT, class MeshT>
void FileManager::generateGenericProperty(const std::string& _entity_t, const std::string& _name,
std::istream& _iff, MeshT& _mesh) const {
if(_entity_t == "vprop") {
VertexPropertyT<PropT> prop = _mesh.template request_vertex_property<PropT>(_name);
prop.deserialize(_iff);
_mesh.set_persistent(prop);
} else if(_entity_t == "eprop") {
EdgePropertyT<PropT> prop = _mesh.template request_edge_property<PropT>(_name);
prop.deserialize(_iff);
_mesh.set_persistent(prop);
} else if(_entity_t == "heprop") {
HalfEdgePropertyT<PropT> prop = _mesh.template request_halfedge_property<PropT>(_name);
prop.deserialize(_iff);
_mesh.set_persistent(prop);
} else if(_entity_t == "fprop") {
FacePropertyT<PropT> prop = _mesh.template request_face_property<PropT>(_name);
prop.deserialize(_iff);
_mesh.set_persistent(prop);
} else if(_entity_t == "hfprop") {
HalfFacePropertyT<PropT> prop = _mesh.template request_halfface_property<PropT>(_name);
prop.deserialize(_iff);
_mesh.set_persistent(prop);
} else if(_entity_t == "cprop") {
CellPropertyT<PropT> prop = _mesh.template request_cell_property<PropT>(_name);
prop.deserialize(_iff);
_mesh.set_persistent(prop);
} else if(_entity_t == "mprop") {
MeshPropertyT<PropT> prop = _mesh.template request_mesh_property<PropT>(_name);
prop.deserialize(_iff);
_mesh.set_persistent(prop);
}
}
//==================================================
template<class MeshT>
bool FileManager::writeFile(const std::string& _filename, const MeshT& _mesh) const {
......
......@@ -9,16 +9,6 @@ Vertices
1.0 -1.0 1.0
1.0 1.0 1.0
-1.0 1.0 1.0
Vertex_Property "Vertex Weights"
float
1.363
6.334
2.766
8.348
4.214
2.136
7.114
0.651
Edges
12
0 1
......@@ -33,8 +23,27 @@ Edges
1 5
2 6
3 7
Edge_Property "Edge Tag"
bool
Faces
6
4 0 2 4 6
4 8 10 12 14
4 18 10 21 3
4 16 15 23 6
4 20 12 23 5
4 0 18 9 17
Polyhedra
1
6 1 2 5 6 9 10
VProp float "Vertex Weights"
1.363
6.334
2.766
8.348
4.214
2.136
7.114
0.651
EProp bool "Edge Tag"
1
1
0
......@@ -47,24 +56,14 @@ bool
0
1
1
Faces
6
4 0 2 4 6
4 8 10 12 14
4 18 10 21 3
4 16 15 23 6
4 20 12 23 5
4 0 18 9 17
Face_Property "Face Selection"
bool
FProp bool "Face Selection"
1
1
0
1
1
0
HalfFace_Property "HalfFace Constraints"
double
HFProp double "HalfFace Constraints"
1.22354
0.11698
1.83562
......@@ -77,6 +76,3 @@ double
2.22457
0.10957
1.09758
Polyhedra
1
6 1 2 5 6 9 10
......@@ -104,64 +104,74 @@ TEST_F(PolyhedralMeshBase, SaveFileWithProps) {
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());
EXPECT_EQ(1u, mesh_.n_halfface_props());
EXPECT_EQ(1u, mesh_.n_vertex_props());
HalfFacePropertyT<float> hfprop2 = mesh_.request_halfface_property<float>("MyHalfFaceProp");
VertexPropertyT<unsigned int> vprop2 = mesh_.request_vertex_property<unsigned int>("MyVertexProp");
for(unsigned int i = 0; i < mesh_.n_halffaces(); ++i) {
EXPECT_FLOAT_EQ((float)i/2.0f, hfprop2[i]);
}
for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
EXPECT_EQ(i, vprop2[i]);
}
}
TEST_F(PolyhedralMeshBase, LoadFileWithProps) {
OpenVolumeMesh::IO::FileManager fileManager;
ASSERT_TRUE(fileManager.readFile("Cube_with_props.ovm", mesh_));
EXPECT_EQ(8u, mesh_.n_vertices());
EXPECT_EQ(12u, mesh_.n_edges());
EXPECT_EQ(6u, mesh_.n_faces());
EXPECT_EQ(1u, mesh_.n_cells());
EXPECT_EQ(1u, mesh_.n_vertex_props());
EXPECT_EQ(1u, mesh_.n_edge_props());
EXPECT_EQ(0u, mesh_.n_halfedge_props());
EXPECT_EQ(1u, mesh_.n_face_props());
EXPECT_EQ(1u, mesh_.n_halfface_props());
EXPECT_EQ(0u, mesh_.n_cell_props());
}
//TEST_F(PolyhedralMeshBase, LoadFileWithProps) {
//
// OpenVolumeMesh::IO::FileManager fileManager;
//
// ASSERT_TRUE(fileManager.readFile("Cube_with_props.ovm", mesh_));
//
// EXPECT_EQ(8u, mesh_.n_vertices());
// EXPECT_EQ(12u, mesh_.n_edges());
// EXPECT_EQ(6u, mesh_.n_faces());
// EXPECT_EQ(1u, mesh_.n_cells());
//
// EXPECT_EQ(1u, mesh_.n_vprops());
// EXPECT_EQ(1u, mesh_.n_eprops());
// EXPECT_EQ(0u, mesh_.n_heprops());
// EXPECT_EQ(1u, mesh_.n_fprops());
// EXPECT_EQ(1u, mesh_.n_hfprops());
// EXPECT_EQ(0u, mesh_.n_cprops());
//}
//
//TEST_F(PolyhedralMeshBase, SaveFileWithProps) {
//
// OpenVolumeMesh::IO::FileManager fileManager;
//
// ASSERT_TRUE(fileManager.readFile("Cube_with_props.ovm", mesh_));
//
// EXPECT_EQ(8u, mesh_.n_vertices());
// EXPECT_EQ(12u, mesh_.n_edges());
// EXPECT_EQ(6u, mesh_.n_faces());
// EXPECT_EQ(1u, mesh_.n_cells());
//
// EXPECT_EQ(1u, mesh_.n_vprops());
// EXPECT_EQ(1u, mesh_.n_eprops());
// EXPECT_EQ(0u, mesh_.n_heprops());
// EXPECT_EQ(1u, mesh_.n_fprops());
// EXPECT_EQ(1u, mesh_.n_hfprops());
// EXPECT_EQ(0u, mesh_.n_cprops());
//
// ASSERT_TRUE(fileManager.writeFile("Cube_with_props.copy.ovm", mesh_));
//
// mesh_.clear();
//
// ASSERT_TRUE(fileManager.readFile("Cube_with_props.copy.ovm", mesh_));
//
// EXPECT_EQ(8u, mesh_.n_vertices());
// EXPECT_EQ(12u, mesh_.n_edges());
// EXPECT_EQ(6u, mesh_.n_faces());
// EXPECT_EQ(1u, mesh_.n_cells());
//
// EXPECT_EQ(1u, mesh_.n_vprops());
// EXPECT_EQ(1u, mesh_.n_eprops());
// EXPECT_EQ(0u, mesh_.n_heprops());
// EXPECT_EQ(1u, mesh_.n_fprops());
// EXPECT_EQ(1u, mesh_.n_hfprops());
// EXPECT_EQ(0u, mesh_.n_cprops());
//}
TEST_F(PolyhedralMeshBase, SaveFileWithProps2) {
OpenVolumeMesh::IO::FileManager fileManager;
ASSERT_TRUE(fileManager.readFile("Cube_with_props.ovm", mesh_));
EXPECT_EQ(8u, mesh_.n_vertices());
EXPECT_EQ(12u, mesh_.n_edges());
EXPECT_EQ(6u, mesh_.n_faces());
EXPECT_EQ(1u, mesh_.n_cells());
EXPECT_EQ(1u, mesh_.n_vertex_props());
EXPECT_EQ(1u, mesh_.n_edge_props());
EXPECT_EQ(0u, mesh_.n_halfedge_props());
EXPECT_EQ(1u, mesh_.n_face_props());
EXPECT_EQ(1u, mesh_.n_halfface_props());
EXPECT_EQ(0u, mesh_.n_cell_props());
ASSERT_TRUE(fileManager.writeFile("Cube_with_props.copy.ovm", mesh_));
mesh_.clear();
ASSERT_TRUE(fileManager.readFile("Cube_with_props.copy.ovm", mesh_));
EXPECT_EQ(8u, mesh_.n_vertices());
EXPECT_EQ(12u, mesh_.n_edges());
EXPECT_EQ(6u, mesh_.n_faces());
EXPECT_EQ(1u, mesh_.n_cells());
EXPECT_EQ(1u, mesh_.n_vertex_props());
EXPECT_EQ(1u, mesh_.n_edge_props());
EXPECT_EQ(0u, mesh_.n_halfedge_props());
EXPECT_EQ(1u, mesh_.n_face_props());
EXPECT_EQ(1u, mesh_.n_halfface_props());
EXPECT_EQ(0u, mesh_.n_cell_props());
}
#endif // INCLUDE GUARD
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