Developer Documentation

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
12 OpenVolumeMesh::IO::FileManager fileManager;
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
24 OpenVolumeMesh::IO::FileManager fileManager;
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
36 OpenVolumeMesh::IO::FileManager fileManager;
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
49 OpenVolumeMesh::IO::FileManager fileManager;
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
73 OpenVolumeMesh::IO::FileManager fileManager;
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[HalfFaceHandle(i)] = (float)i/2.0f;
88 }
89 for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
90 vprop[VertexHandle(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[HalfFaceHandle(i)]);
116 }
117 for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
118 EXPECT_EQ(i, vprop2[VertexHandle(i)]);
119 }
120 }
121
122 TEST_F(PolyhedralMeshBase, SaveFileWithVectorProps) {
123
124 OpenVolumeMesh::IO::FileManager fileManager;
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[HalfFaceHandle(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[VertexHandle(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 HalfFaceHandle hfh(i);
167 EXPECT_DOUBLE_EQ((double)i/2.0, hfprop2[hfh][0]);
168 EXPECT_DOUBLE_EQ((double)i/2.0, hfprop2[hfh][1]);
169 EXPECT_DOUBLE_EQ((double)i/2.0, hfprop2[hfh][2]);
170 }
171 for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
172 VertexHandle vh(i);
173 EXPECT_EQ((int)i, vprop2[vh][0]);
174 EXPECT_EQ((int)i, vprop2[vh][1]);
175 }
176 }
177
178 TEST_F(PolyhedralMeshBase, SerializeVectorValuedProperties) {
179
180 OpenVolumeMesh::IO::FileManager fileManager;
181
182 ASSERT_TRUE(fileManager.readFile("Cylinder.ovm", mesh_));
183
184 EXPECT_EQ(399u, mesh_.n_vertices());
185 EXPECT_EQ(1070u, mesh_.n_edges());
186 EXPECT_EQ(960u, mesh_.n_faces());
187 EXPECT_EQ(288u, mesh_.n_cells());
188
189 // Attach persistent properties
190 HalfFacePropertyT<Vec3d> hfprop = mesh_.request_halfface_property<Vec3d>("MyHalfFaceProp");
191 VertexPropertyT<Vec2i> vprop = mesh_.request_vertex_property<Vec2i>("MyVertexProp");
192
193 for(unsigned int i = 0; i < mesh_.n_halffaces(); ++i) {
194 HalfFaceHandle hfh(i);
195 hfprop[hfh] = Vec3d((double)i/2.0, (double)i/2.0, (double)i/2.0);
196 }
197 for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
198 VertexHandle vh(i);
199 vprop[vh] = Vec2i(i, i);
200 }
201
202 mesh_.set_persistent(hfprop);
203 mesh_.set_persistent(vprop);
204
205 std::ofstream ofs1("hfVecPropTest");
206 std::ofstream ofs2("vVecPropTest");
207
208 hfprop.serialize(ofs1);
209 vprop.serialize(ofs2);
210
211 ofs1.close();
212 ofs2.close();
213
214 /*
215 * Change property values
216 */
217 for(unsigned int i = 0; i < mesh_.n_halffaces(); ++i) {
218 hfprop[HalfFaceHandle(i)] = Vec3d((double)i/3.0, (double)i/3.0, (double)i/3.0);
219 }
220
221 for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
222 vprop[VertexHandle(i)] = Vec2i(2*i, 2*i);
223 }
224
225 std::ifstream ifs1("hfVecPropTest");
226 std::ifstream ifs2("vVecPropTest");
227
228 hfprop.deserialize(ifs1);
229 vprop.deserialize(ifs2);
230
231 ifs1.close();
232 ifs2.close();
233
234 for(unsigned int i = 0; i < mesh_.n_halffaces(); ++i) {
235 HalfFaceHandle hfh(i);
236 EXPECT_DOUBLE_EQ((double)i/2.0, hfprop[hfh][0]);
237 EXPECT_DOUBLE_EQ((double)i/2.0, hfprop[hfh][1]);
238 EXPECT_DOUBLE_EQ((double)i/2.0, hfprop[hfh][2]);
239 }
240 for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
241 VertexHandle vh(i);
242 EXPECT_EQ((int)i, vprop[vh][0]);
243 EXPECT_EQ((int)i, vprop[vh][1]);
244 }
245 }
246
247 TEST_F(PolyhedralMeshBase, LoadFileWithProps) {
248
249 OpenVolumeMesh::IO::FileManager fileManager;
250
251 ASSERT_TRUE(fileManager.readFile("Cube_with_props.ovm", mesh_));
252
253 EXPECT_EQ(8u, mesh_.n_vertices());
254 EXPECT_EQ(12u, mesh_.n_edges());
255 EXPECT_EQ(6u, mesh_.n_faces());
256 EXPECT_EQ(1u, mesh_.n_cells());
257
258 EXPECT_EQ(1u, mesh_.n_vertex_props());
259 EXPECT_EQ(1u, mesh_.n_edge_props());
260 EXPECT_EQ(0u, mesh_.n_halfedge_props());
261 EXPECT_EQ(1u, mesh_.n_face_props());
262 EXPECT_EQ(1u, mesh_.n_halfface_props());
263 EXPECT_EQ(0u, mesh_.n_cell_props());
264 }
265
266 TEST_F(PolyhedralMeshBase, SaveFileWithProps2) {
267
268 OpenVolumeMesh::IO::FileManager fileManager;
269
270 ASSERT_TRUE(fileManager.readFile("Cube_with_props.ovm", mesh_));
271
272 EXPECT_EQ(8u, mesh_.n_vertices());
273 EXPECT_EQ(12u, mesh_.n_edges());
274 EXPECT_EQ(6u, mesh_.n_faces());
275 EXPECT_EQ(1u, mesh_.n_cells());
276
277 EXPECT_EQ(1u, mesh_.n_vertex_props());
278 EXPECT_EQ(1u, mesh_.n_edge_props());
279 EXPECT_EQ(0u, mesh_.n_halfedge_props());
280 EXPECT_EQ(1u, mesh_.n_face_props());
281 EXPECT_EQ(1u, mesh_.n_halfface_props());
282 EXPECT_EQ(0u, mesh_.n_cell_props());
283
284 ASSERT_TRUE(fileManager.writeFile("Cube_with_props.copy.ovm", mesh_));
285
286 mesh_.clear();
287
288 ASSERT_TRUE(fileManager.readFile("Cube_with_props.copy.ovm", mesh_));
289
290 EXPECT_EQ(8u, mesh_.n_vertices());
291 EXPECT_EQ(12u, mesh_.n_edges());
292 EXPECT_EQ(6u, mesh_.n_faces());
293 EXPECT_EQ(1u, mesh_.n_cells());
294
295 EXPECT_EQ(1u, mesh_.n_vertex_props());
296 EXPECT_EQ(1u, mesh_.n_edge_props());
297 EXPECT_EQ(0u, mesh_.n_halfedge_props());
298 EXPECT_EQ(1u, mesh_.n_face_props());
299 EXPECT_EQ(1u, mesh_.n_halfface_props());
300 EXPECT_EQ(0u, mesh_.n_cell_props());
301 }
302
OpenVolumeMesh::Geometry::VectorT
Definition: Vector11T.hh:78
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
OpenVolumeMesh::HandleT< Entity::HalfFace >
OpenVolumeMesh
Definition: ColorAttrib.hh:53
HexahedralMeshBase
Definition: unittests_common.hh:59
OpenVolumeMesh::PropertyTT
Definition: ForwardDeclarations.hh:18
PolyhedralMeshBase
Definition: unittests_common.hh:21
OpenVolumeMesh::IO::FileManager
Read/Write mesh data from/to files.
Definition: FileManager.hh:62
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