unittests_files.cc 8.32 KB
Newer Older
1
2
3
4
5
#include <gtest/gtest.h>
#include <Unittests/unittests_common.hh>

#include <OpenVolumeMesh/FileManager/FileManager.hh>

6
7
using namespace OpenVolumeMesh;

8
9
10
11
12
13
14
15
16
17
18
19
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());
}

20
21
22
23
24
25
26
27
28
29
30
31
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());
}

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

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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) {
      hfprop[i] = (float)i/2.0f;
  }
  for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
      vprop[i] = i;
  }

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

106
107
108
109
110
111
112
113
114
115
116
117
118
119
  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]);
  }
}

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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) {
      hfprop[i] = Vec3d((double)i/2.0, (double)i/2.0, (double)i/2.0);
  }
  for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
139
      vprop[i] = Vec2i(i, i);
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
  }

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

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) {
      hfprop[i] = Vec3d((double)i/2.0, (double)i/2.0, (double)i/2.0);
  }
  for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
193
      vprop[i] = Vec2i(i, i);
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
  }

  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) {
	  hfprop[i] = Vec3d((double)i/3.0, (double)i/3.0, (double)i/3.0);
  }

  for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
216
	  vprop[i] = Vec2i(2*i, 2*i);
217
218
219
220
221
222
223
224
225
226
227
228
  }

  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) {
229
230
231
      EXPECT_DOUBLE_EQ((double)i/2.0, hfprop[i][0]);
      EXPECT_DOUBLE_EQ((double)i/2.0, hfprop[i][1]);
      EXPECT_DOUBLE_EQ((double)i/2.0, hfprop[i][2]);
232
233
234
235
236
237
238
  }
  for(unsigned int i = 0; i < mesh_.n_vertices(); ++i) {
      EXPECT_EQ(i, vprop[i][0]);
      EXPECT_EQ(i, vprop[i][1]);
  }
}

239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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());
256
257
}

258
259
260
261
262
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
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());
}
294