Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
unittests_files.cc
1 #include <gtest/gtest.h>
2 #include <Unittests/unittests_common.hh>
3 
4 #include <OpenVolumeMesh/FileManager/FileManager.hh>
5 
6 using namespace OpenVolumeMesh;
7 
8 using namespace Geometry;
9 
10 TEST_F(PolyhedralMeshBase, LoadFile) {
11 
13 
14  ASSERT_TRUE(fileManager.readFile("Cylinder.ovm", mesh_));
15 
16  EXPECT_EQ(399u, mesh_.n_vertices());
17  EXPECT_EQ(1070u, mesh_.n_edges());
18  EXPECT_EQ(960u, mesh_.n_faces());
19  EXPECT_EQ(288u, mesh_.n_cells());
20 }
21 
22 TEST_F(PolyhedralMeshBase, LoadNonManifoldMesh) {
23 
25 
26  ASSERT_TRUE(fileManager.readFile("NonManifold.ovm", mesh_));
27 
28  EXPECT_EQ(12u, mesh_.n_vertices());
29  EXPECT_EQ(20u, mesh_.n_edges());
30  EXPECT_EQ(11u, mesh_.n_faces());
31  EXPECT_EQ(2u, mesh_.n_cells());
32 }
33 
34 TEST_F(HexahedralMeshBase, LoadFile) {
35 
37 
38  ASSERT_TRUE(fileManager.isHexahedralMesh("Cylinder.ovm"));
39  ASSERT_TRUE(fileManager.readFile("Cylinder.ovm", mesh_));
40 
41  EXPECT_EQ(399u, mesh_.n_vertices());
42  EXPECT_EQ(1070u, mesh_.n_edges());
43  EXPECT_EQ(960u, mesh_.n_faces());
44  EXPECT_EQ(288u, mesh_.n_cells());
45 }
46 
47 TEST_F(PolyhedralMeshBase, SaveFile) {
48 
50 
51  ASSERT_TRUE(fileManager.readFile("Cylinder.ovm", mesh_));
52 
53  EXPECT_EQ(399u, mesh_.n_vertices());
54  EXPECT_EQ(1070u, mesh_.n_edges());
55  EXPECT_EQ(960u, mesh_.n_faces());
56  EXPECT_EQ(288u, mesh_.n_cells());
57 
58  // Write file
59  ASSERT_TRUE(fileManager.writeFile("Cylinder.copy.ovm", mesh_));
60 
61  mesh_.clear();
62 
63  ASSERT_TRUE(fileManager.readFile("Cylinder.copy.ovm", mesh_));
64 
65  EXPECT_EQ(399u, mesh_.n_vertices());
66  EXPECT_EQ(1070u, mesh_.n_edges());
67  EXPECT_EQ(960u, mesh_.n_faces());
68  EXPECT_EQ(288u, mesh_.n_cells());
69 }
70 
71 TEST_F(PolyhedralMeshBase, SaveFileWithProps) {
72 
74 
75  ASSERT_TRUE(fileManager.readFile("Cylinder.ovm", mesh_));
76 
77  EXPECT_EQ(399u, mesh_.n_vertices());
78  EXPECT_EQ(1070u, mesh_.n_edges());
79  EXPECT_EQ(960u, mesh_.n_faces());
80  EXPECT_EQ(288u, mesh_.n_cells());
81 
82  // Attach non-persistent properties
83  HalfFacePropertyT<float> hfprop = mesh_.request_halfface_property<float>("MyHalfFaceProp");
84  VertexPropertyT<unsigned int> vprop = mesh_.request_vertex_property<unsigned int>("MyVertexProp");
85 
86  for(unsigned int i = 0; i < mesh_.n_halffaces(); ++i) {
87  hfprop[i] = (float)i/2.0f;
88  }
89  for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
90  vprop[i] = i;
91  }
92 
93  mesh_.set_persistent(hfprop);
94  mesh_.set_persistent(vprop);
95 
96  // Write file
97  ASSERT_TRUE(fileManager.writeFile("Cylinder.copy.ovm", mesh_));
98 
99  mesh_.clear();
100 
101  ASSERT_TRUE(fileManager.readFile("Cylinder.copy.ovm", mesh_));
102 
103  EXPECT_EQ(399u, mesh_.n_vertices());
104  EXPECT_EQ(1070u, mesh_.n_edges());
105  EXPECT_EQ(960u, mesh_.n_faces());
106  EXPECT_EQ(288u, mesh_.n_cells());
107 
108  EXPECT_EQ(1u, mesh_.n_halfface_props());
109  EXPECT_EQ(1u, mesh_.n_vertex_props());
110 
111  HalfFacePropertyT<float> hfprop2 = mesh_.request_halfface_property<float>("MyHalfFaceProp");
112  VertexPropertyT<unsigned int> vprop2 = mesh_.request_vertex_property<unsigned int>("MyVertexProp");
113 
114  for(unsigned int i = 0; i < mesh_.n_halffaces(); ++i) {
115  EXPECT_FLOAT_EQ((float)i/2.0f, hfprop2[i]);
116  }
117  for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
118  EXPECT_EQ(i, vprop2[i]);
119  }
120 }
121 
122 TEST_F(PolyhedralMeshBase, SaveFileWithVectorProps) {
123 
125 
126  ASSERT_TRUE(fileManager.readFile("Cylinder.ovm", mesh_));
127 
128  EXPECT_EQ(399u, mesh_.n_vertices());
129  EXPECT_EQ(1070u, mesh_.n_edges());
130  EXPECT_EQ(960u, mesh_.n_faces());
131  EXPECT_EQ(288u, mesh_.n_cells());
132 
133  // Attach non-persistent properties
134  HalfFacePropertyT<Vec3d> hfprop = mesh_.request_halfface_property<Vec3d>("MyHalfFaceProp");
135  VertexPropertyT<Vec2i> vprop = mesh_.request_vertex_property<Vec2i>("MyVertexProp");
136 
137  for(unsigned int i = 0; i < mesh_.n_halffaces(); ++i) {
138  hfprop[i] = Vec3d((double)i/2.0, (double)i/2.0, (double)i/2.0);
139  }
140  for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
141  vprop[i] = Vec2i(i, i);
142  }
143 
144  mesh_.set_persistent(hfprop);
145  mesh_.set_persistent(vprop);
146 
147  // Write file
148  ASSERT_TRUE(fileManager.writeFile("Cylinder.copy.ovm", mesh_));
149 
150  mesh_.clear();
151 
152  ASSERT_TRUE(fileManager.readFile("Cylinder.copy.ovm", mesh_));
153 
154  EXPECT_EQ(399u, mesh_.n_vertices());
155  EXPECT_EQ(1070u, mesh_.n_edges());
156  EXPECT_EQ(960u, mesh_.n_faces());
157  EXPECT_EQ(288u, mesh_.n_cells());
158 
159  EXPECT_EQ(1u, mesh_.n_halfface_props());
160  EXPECT_EQ(1u, mesh_.n_vertex_props());
161 
162  HalfFacePropertyT<Vec3d> hfprop2 = mesh_.request_halfface_property<Vec3d>("MyHalfFaceProp");
163  VertexPropertyT<Vec2i> vprop2 = mesh_.request_vertex_property<Vec2i>("MyVertexProp");
164 
165  for(unsigned int i = 0; i < mesh_.n_halffaces(); ++i) {
166  EXPECT_DOUBLE_EQ((double)i/2.0, hfprop2[i][0]);
167  EXPECT_DOUBLE_EQ((double)i/2.0, hfprop2[i][1]);
168  EXPECT_DOUBLE_EQ((double)i/2.0, hfprop2[i][2]);
169  }
170  for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
171  EXPECT_EQ((int)i, vprop2[i][0]);
172  EXPECT_EQ((int)i, vprop2[i][1]);
173  }
174 }
175 
176 TEST_F(PolyhedralMeshBase, SerializeVectorValuedProperties) {
177 
179 
180  ASSERT_TRUE(fileManager.readFile("Cylinder.ovm", mesh_));
181 
182  EXPECT_EQ(399u, mesh_.n_vertices());
183  EXPECT_EQ(1070u, mesh_.n_edges());
184  EXPECT_EQ(960u, mesh_.n_faces());
185  EXPECT_EQ(288u, mesh_.n_cells());
186 
187  // Attach persistent properties
188  HalfFacePropertyT<Vec3d> hfprop = mesh_.request_halfface_property<Vec3d>("MyHalfFaceProp");
189  VertexPropertyT<Vec2i> vprop = mesh_.request_vertex_property<Vec2i>("MyVertexProp");
190 
191  for(unsigned int i = 0; i < mesh_.n_halffaces(); ++i) {
192  hfprop[i] = Vec3d((double)i/2.0, (double)i/2.0, (double)i/2.0);
193  }
194  for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
195  vprop[i] = Vec2i(i, i);
196  }
197 
198  mesh_.set_persistent(hfprop);
199  mesh_.set_persistent(vprop);
200 
201  std::ofstream ofs1("hfVecPropTest");
202  std::ofstream ofs2("vVecPropTest");
203 
204  hfprop.serialize(ofs1);
205  vprop.serialize(ofs2);
206 
207  ofs1.close();
208  ofs2.close();
209 
210  /*
211  * Change property values
212  */
213  for(unsigned int i = 0; i < mesh_.n_halffaces(); ++i) {
214  hfprop[i] = Vec3d((double)i/3.0, (double)i/3.0, (double)i/3.0);
215  }
216 
217  for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
218  vprop[i] = Vec2i(2*i, 2*i);
219  }
220 
221  std::ifstream ifs1("hfVecPropTest");
222  std::ifstream ifs2("vVecPropTest");
223 
224  hfprop.deserialize(ifs1);
225  vprop.deserialize(ifs2);
226 
227  ifs1.close();
228  ifs2.close();
229 
230  for(unsigned int i = 0; i < mesh_.n_halffaces(); ++i) {
231  EXPECT_DOUBLE_EQ((double)i/2.0, hfprop[i][0]);
232  EXPECT_DOUBLE_EQ((double)i/2.0, hfprop[i][1]);
233  EXPECT_DOUBLE_EQ((double)i/2.0, hfprop[i][2]);
234  }
235  for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
236  EXPECT_EQ((int)i, vprop[i][0]);
237  EXPECT_EQ((int)i, vprop[i][1]);
238  }
239 }
240 
241 TEST_F(PolyhedralMeshBase, LoadFileWithProps) {
242 
244 
245  ASSERT_TRUE(fileManager.readFile("Cube_with_props.ovm", mesh_));
246 
247  EXPECT_EQ(8u, mesh_.n_vertices());
248  EXPECT_EQ(12u, mesh_.n_edges());
249  EXPECT_EQ(6u, mesh_.n_faces());
250  EXPECT_EQ(1u, mesh_.n_cells());
251 
252  EXPECT_EQ(1u, mesh_.n_vertex_props());
253  EXPECT_EQ(1u, mesh_.n_edge_props());
254  EXPECT_EQ(0u, mesh_.n_halfedge_props());
255  EXPECT_EQ(1u, mesh_.n_face_props());
256  EXPECT_EQ(1u, mesh_.n_halfface_props());
257  EXPECT_EQ(0u, mesh_.n_cell_props());
258 }
259 
260 TEST_F(PolyhedralMeshBase, SaveFileWithProps2) {
261 
263 
264  ASSERT_TRUE(fileManager.readFile("Cube_with_props.ovm", mesh_));
265 
266  EXPECT_EQ(8u, mesh_.n_vertices());
267  EXPECT_EQ(12u, mesh_.n_edges());
268  EXPECT_EQ(6u, mesh_.n_faces());
269  EXPECT_EQ(1u, mesh_.n_cells());
270 
271  EXPECT_EQ(1u, mesh_.n_vertex_props());
272  EXPECT_EQ(1u, mesh_.n_edge_props());
273  EXPECT_EQ(0u, mesh_.n_halfedge_props());
274  EXPECT_EQ(1u, mesh_.n_face_props());
275  EXPECT_EQ(1u, mesh_.n_halfface_props());
276  EXPECT_EQ(0u, mesh_.n_cell_props());
277 
278  ASSERT_TRUE(fileManager.writeFile("Cube_with_props.copy.ovm", mesh_));
279 
280  mesh_.clear();
281 
282  ASSERT_TRUE(fileManager.readFile("Cube_with_props.copy.ovm", mesh_));
283 
284  EXPECT_EQ(8u, mesh_.n_vertices());
285  EXPECT_EQ(12u, mesh_.n_edges());
286  EXPECT_EQ(6u, mesh_.n_faces());
287  EXPECT_EQ(1u, mesh_.n_cells());
288 
289  EXPECT_EQ(1u, mesh_.n_vertex_props());
290  EXPECT_EQ(1u, mesh_.n_edge_props());
291  EXPECT_EQ(0u, mesh_.n_halfedge_props());
292  EXPECT_EQ(1u, mesh_.n_face_props());
293  EXPECT_EQ(1u, mesh_.n_halfface_props());
294  EXPECT_EQ(0u, mesh_.n_cell_props());
295 }
296 
bool readFile(const std::string &_filename, MeshT &_mesh, bool _topologyCheck=true, bool _computeBottomUpIncidences=true) const
Read a mesh from a file.
Definition: FileManagerT.cc:66
Property classes for the different entity types.
Read/Write mesh data from/to files.
Definition: FileManager.hh:60
bool isHexahedralMesh(const std::string &_filename) const
Test whether given file contains a hexahedral mesh.
Definition: FileManager.cc:139
bool writeFile(const std::string &_filename, const MeshT &_mesh) const
Write a mesh to a file.