Developer Documentation

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