unittests_files.cc 8.62 KB
Newer Older
1
#include "unittests_common.hh"
2
3
4

#include <OpenVolumeMesh/FileManager/FileManager.hh>

5
using namespace OpenVolumeMesh;
6

7
using namespace Geometry;
8

9
10
11
12
13
14
15
16
17
18
19
20
TEST_F(PolyhedralMeshBase, LoadFile) {

  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());
}

21
22
23
24
25
26
27
28
29
30
31
32
TEST_F(PolyhedralMeshBase, LoadNonManifoldMesh) {

  OpenVolumeMesh::IO::FileManager fileManager;

  ASSERT_TRUE(fileManager.readFile("NonManifold.ovm", mesh_));

  EXPECT_EQ(12u, mesh_.n_vertices());
  EXPECT_EQ(20u, mesh_.n_edges());
  EXPECT_EQ(11u, mesh_.n_faces());
  EXPECT_EQ(2u, mesh_.n_cells());
}

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
TEST_F(HexahedralMeshBase, LoadFile) {

  OpenVolumeMesh::IO::FileManager fileManager;

  ASSERT_TRUE(fileManager.isHexahedralMesh("Cylinder.ovm"));
  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());
}

TEST_F(PolyhedralMeshBase, SaveFile) {

  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());

  // 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());
}

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
TEST_F(PolyhedralMeshBase, SaveFileWithProps) {

  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<float> hfprop = mesh_.request_halfface_property<float>("MyHalfFaceProp");
  VertexPropertyT<unsigned int> vprop = mesh_.request_vertex_property<unsigned int>("MyVertexProp");

  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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  }

  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());

107
108
109
110
111
112
113
  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) {
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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) {
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
  }

  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) {
165
166
167
168
      HalfFaceHandle hfh(i);
      EXPECT_DOUBLE_EQ((double)i/2.0, hfprop2[hfh][0]);
      EXPECT_DOUBLE_EQ((double)i/2.0, hfprop2[hfh][1]);
      EXPECT_DOUBLE_EQ((double)i/2.0, hfprop2[hfh][2]);
169
170
  }
  for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
171
172
173
      VertexHandle vh(i);
      EXPECT_EQ((int)i, vprop2[vh][0]);
      EXPECT_EQ((int)i, vprop2[vh][1]);
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  }
}

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) {
193
194
      HalfFaceHandle hfh(i);
      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
198
      VertexHandle vh(i);
      vprop[vh] = Vec2i(i, i);
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
  }

  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) {
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
225
226
227
228
229
230
231
232
233
  }

  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) {
234
235
236
237
      HalfFaceHandle hfh(i);
      EXPECT_DOUBLE_EQ((double)i/2.0, hfprop[hfh][0]);
      EXPECT_DOUBLE_EQ((double)i/2.0, hfprop[hfh][1]);
      EXPECT_DOUBLE_EQ((double)i/2.0, hfprop[hfh][2]);
238
239
  }
  for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
240
241
242
      VertexHandle vh(i);
      EXPECT_EQ((int)i, vprop[vh][0]);
      EXPECT_EQ((int)i, vprop[vh][1]);
243
244
245
  }
}

246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
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());
263
264
}

265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
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());
}
301