Developer Documentation
unittests_read_write_OFF.cc
1 #include <gtest/gtest.h>
2 #include <Unittests/unittests_common.hh>
3 #include <cstdio>
4 
5 
6 namespace {
7 
8 class OpenMeshReadWriteOFF : public OpenMeshBase {
9 
10  protected:
11 
12  // This function is called before each test is run
13  virtual void SetUp() {
14 
15  // Do some initial stuff with the member data here...
16  }
17 
18  // This function is called after all tests are through
19  virtual void TearDown() {
20 
21  // Do some final stuff with the member data here...
22  }
23 
24  // Member already defined in OpenMeshBase
25  //Mesh mesh_;
26 };
27 
28 /*
29  * ====================================================================
30  * Define tests below
31  * ====================================================================
32  */
33 
34 /*
35  * Just load a simple mesh file in obj format and count whether
36  * the right number of entities has been loaded.
37  */
38 TEST_F(OpenMeshReadWriteOFF, LoadSimpleOFFFile) {
39 
40  mesh_.clear();
41 
42  bool ok = OpenMesh::IO::read_mesh(mesh_, "cube1.off");
43 
44  EXPECT_TRUE(ok);
45 
46  EXPECT_EQ(7526u , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
47  EXPECT_EQ(22572u, mesh_.n_edges()) << "The number of loaded edges is not correct!";
48  EXPECT_EQ(15048u, mesh_.n_faces()) << "The number of loaded faces is not correct!";
49 }
50 
51 
52 TEST_F(OpenMeshReadWriteOFF, WriteAndReadVertexColorsToAndFromOFFFile) {
53 
54  mesh_.clear();
55 
56  mesh_.request_vertex_colors();
57 
58  mesh_.add_vertex( Mesh::Point(0,0,1) );
59  mesh_.add_vertex( Mesh::Point(0,1,0) );
60  mesh_.add_vertex( Mesh::Point(0,1,1) );
61  mesh_.add_vertex( Mesh::Point(1,0,1) );
62 
63 #ifdef TEST_DOUBLE_TRAITS
64  // using the default color type Vec4f from DefaultTraitsDouble in Traits.hh
65  Mesh::Color testColor(255/255.0, 128/255.0, 64/255.0, 1.0);
66 #else
67  // using the default color type Vec3uc from DefaultTraits in Traits.hh
68  Mesh::Color testColor(255, 128, 64);
69 #endif
70 
71  // setting colors (different from black)
72  for (Mesh::VertexIter vit = mesh_.vertices_begin(), vitend = mesh_.vertices_end(); vit != vitend; ++vit)
73  mesh_.set_color(*vit, testColor);
74 
75  // check if the colors are correctly setted
76  int count = 0;
77  for (Mesh::VertexIter vit = mesh_.vertices_begin(), vitend = mesh_.vertices_end(); vit != vitend; ++vit) {
78  Mesh::Color color = mesh_.color(*vit);
79  bool wrong_color = false;
80  for (size_t i = 0; i < color.size(); ++i)
81  wrong_color = wrong_color || (color[i] != testColor[i]);
82  if (wrong_color)
83  ++ count;
84  }
85 
86  EXPECT_EQ(0, count) << "Vertices have the wrong color!";
87 
88  // write the mesh_
90  OpenMesh::IO::write_mesh(mesh_, "temp.off", opt);
91  OpenMesh::IO::read_mesh(mesh_, "temp.off", opt);
92  remove("temp.off");
93 
94  // check if vertices still have the same color
95  count = 0;
96  for (Mesh::VertexIter vit = mesh_.vertices_begin(), vitend = mesh_.vertices_end(); vit != vitend; ++vit) {
97  Mesh::Color color = mesh_.color(*vit);
98  bool wrong_color = false;
99  for (size_t i = 0; i < color.size(); ++i)
100  wrong_color = wrong_color || (color[i] != testColor[i]);
101  if (wrong_color)
102  ++ count;
103  }
104 
105  EXPECT_EQ(0, count) << "Vertices should have the same color after writing and reading the OFF file!";
106 
107  mesh_.release_vertex_colors();
108 }
109 
110 TEST_F(OpenMeshReadWriteOFF, WriteAndReadFloatVertexColorsToAndFromOFFFile) {
111 
112  mesh_.clear();
113 
114  mesh_.request_vertex_colors();
115 
117 
118  bool ok = OpenMesh::IO::read_mesh(mesh_, "meshlab.ply", opt);
119 
120  EXPECT_TRUE(ok) << "meshlab.ply could not be read!";
121 
122  opt.clear();
125 
126  // write the mesh_
127  ok = OpenMesh::IO::write_mesh(mesh_, "cube_floating.off", opt);
128  EXPECT_TRUE(ok) << "cube_floating.off could not be written!";
129  mesh_.clear();
130  ok = OpenMesh::IO::read_mesh(mesh_, "cube_floating.off", opt);
131  EXPECT_TRUE(ok) << "cube_floating.off could not be read!";
132 
133  EXPECT_EQ(8u , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
134  EXPECT_EQ(18u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
135  EXPECT_EQ(12u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
136 
137 #ifdef TEST_DOUBLE_TRAITS
138  EXPECT_FLOAT_EQ(0.0, mesh_.color(mesh_.vertex_handle(0))[0] ) << "Wrong vertex color at vertex 0 component 0";
139  EXPECT_FLOAT_EQ(0.0, mesh_.color(mesh_.vertex_handle(0))[1] ) << "Wrong vertex color at vertex 0 component 1";
140  EXPECT_FLOAT_EQ(1.0, mesh_.color(mesh_.vertex_handle(0))[2] ) << "Wrong vertex color at vertex 0 component 2";
141 
142  EXPECT_FLOAT_EQ(0.0, mesh_.color(mesh_.vertex_handle(3))[0] ) << "Wrong vertex color at vertex 3 component 0";
143  EXPECT_FLOAT_EQ(0.0, mesh_.color(mesh_.vertex_handle(3))[1] ) << "Wrong vertex color at vertex 3 component 1";
144  EXPECT_FLOAT_EQ(1.0, mesh_.color(mesh_.vertex_handle(3))[2] ) << "Wrong vertex color at vertex 3 component 2";
145 
146  EXPECT_FLOAT_EQ(0.0, mesh_.color(mesh_.vertex_handle(4))[0] ) << "Wrong vertex color at vertex 4 component 0";
147  EXPECT_FLOAT_EQ(0.0, mesh_.color(mesh_.vertex_handle(4))[1] ) << "Wrong vertex color at vertex 4 component 1";
148  EXPECT_FLOAT_EQ(1.0, mesh_.color(mesh_.vertex_handle(4))[2] ) << "Wrong vertex color at vertex 4 component 2";
149 
150  EXPECT_FLOAT_EQ(0.0, mesh_.color(mesh_.vertex_handle(7))[0] ) << "Wrong vertex color at vertex 7 component 0";
151  EXPECT_FLOAT_EQ(0.0, mesh_.color(mesh_.vertex_handle(7))[1] ) << "Wrong vertex color at vertex 7 component 1";
152  EXPECT_FLOAT_EQ(1.0, mesh_.color(mesh_.vertex_handle(7))[2] ) << "Wrong vertex color at vertex 7 component 2";
153 #else
154  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(0))[0] ) << "Wrong vertex color at vertex 0 component 0";
155  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(0))[1] ) << "Wrong vertex color at vertex 0 component 1";
156  EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(0))[2] ) << "Wrong vertex color at vertex 0 component 2";
157 
158  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(3))[0] ) << "Wrong vertex color at vertex 3 component 0";
159  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(3))[1] ) << "Wrong vertex color at vertex 3 component 1";
160  EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(3))[2] ) << "Wrong vertex color at vertex 3 component 2";
161 
162  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(4))[0] ) << "Wrong vertex color at vertex 4 component 0";
163  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(4))[1] ) << "Wrong vertex color at vertex 4 component 1";
164  EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(4))[2] ) << "Wrong vertex color at vertex 4 component 2";
165 
166  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(7))[0] ) << "Wrong vertex color at vertex 7 component 0";
167  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(7))[1] ) << "Wrong vertex color at vertex 7 component 1";
168  EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(7))[2] ) << "Wrong vertex color at vertex 7 component 2";
169 #endif
170 
171  EXPECT_FALSE(opt.vertex_has_normal()) << "Wrong user opt are returned!";
172  EXPECT_FALSE(opt.vertex_has_texcoord()) << "Wrong user opt are returned!";
173  EXPECT_TRUE(opt.vertex_has_color()) << "Wrong user opt are returned!";
174  EXPECT_TRUE(opt.color_is_float()) << "Wrong user opt are returned!";
175 
176  mesh_.release_vertex_colors();
177 }
178 
179 TEST_F(OpenMeshReadWriteOFF, WriteAndReadBinaryFloatVertexColorsToAndFromOFFFile) {
180 
181  mesh_.clear();
182 
183  mesh_.request_vertex_colors();
184 
186 
187  bool ok = OpenMesh::IO::read_mesh(mesh_, "meshlab.ply", opt);
188 
189  EXPECT_TRUE(ok) << "meshlab.ply could not be read!";
190 
191  opt.clear();
195 
196  // write the mesh_
197  ok = OpenMesh::IO::write_mesh(mesh_, "cube_floating_binary.off", opt);
198  EXPECT_TRUE(ok) << "cube_floating_binary.off could not be written!";
199  mesh_.clear();
200  opt.clear();
204  ok = OpenMesh::IO::read_mesh(mesh_, "cube_floating_binary.off", opt);
205  EXPECT_TRUE(ok) << "cube_floating_binary.off could not be read!";
206 
207  EXPECT_EQ(8u , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
208  EXPECT_EQ(18u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
209  EXPECT_EQ(12u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
210 
211 #ifdef TEST_DOUBLE_TRAITS
212  EXPECT_FLOAT_EQ(0.0, mesh_.color(mesh_.vertex_handle(0))[0] ) << "Wrong vertex color at vertex 0 component 0";
213  EXPECT_FLOAT_EQ(0.0, mesh_.color(mesh_.vertex_handle(0))[1] ) << "Wrong vertex color at vertex 0 component 1";
214  EXPECT_FLOAT_EQ(1.0, mesh_.color(mesh_.vertex_handle(0))[2] ) << "Wrong vertex color at vertex 0 component 2";
215 
216  EXPECT_FLOAT_EQ(0.0, mesh_.color(mesh_.vertex_handle(3))[0] ) << "Wrong vertex color at vertex 3 component 0";
217  EXPECT_FLOAT_EQ(0.0, mesh_.color(mesh_.vertex_handle(3))[1] ) << "Wrong vertex color at vertex 3 component 1";
218  EXPECT_FLOAT_EQ(1.0, mesh_.color(mesh_.vertex_handle(3))[2] ) << "Wrong vertex color at vertex 3 component 2";
219 
220  EXPECT_FLOAT_EQ(0.0, mesh_.color(mesh_.vertex_handle(4))[0] ) << "Wrong vertex color at vertex 4 component 0";
221  EXPECT_FLOAT_EQ(0.0, mesh_.color(mesh_.vertex_handle(4))[1] ) << "Wrong vertex color at vertex 4 component 1";
222  EXPECT_FLOAT_EQ(1.0, mesh_.color(mesh_.vertex_handle(4))[2] ) << "Wrong vertex color at vertex 4 component 2";
223 
224  EXPECT_FLOAT_EQ(0.0, mesh_.color(mesh_.vertex_handle(7))[0] ) << "Wrong vertex color at vertex 7 component 0";
225  EXPECT_FLOAT_EQ(0.0, mesh_.color(mesh_.vertex_handle(7))[1] ) << "Wrong vertex color at vertex 7 component 1";
226  EXPECT_FLOAT_EQ(1.0, mesh_.color(mesh_.vertex_handle(7))[2] ) << "Wrong vertex color at vertex 7 component 2";
227 #else
228  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(0))[0] ) << "Wrong vertex color at vertex 0 component 0";
229  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(0))[1] ) << "Wrong vertex color at vertex 0 component 1";
230  EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(0))[2] ) << "Wrong vertex color at vertex 0 component 2";
231 
232  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(3))[0] ) << "Wrong vertex color at vertex 3 component 0";
233  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(3))[1] ) << "Wrong vertex color at vertex 3 component 1";
234  EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(3))[2] ) << "Wrong vertex color at vertex 3 component 2";
235 
236  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(4))[0] ) << "Wrong vertex color at vertex 4 component 0";
237  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(4))[1] ) << "Wrong vertex color at vertex 4 component 1";
238  EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(4))[2] ) << "Wrong vertex color at vertex 4 component 2";
239 
240  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(7))[0] ) << "Wrong vertex color at vertex 7 component 0";
241  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(7))[1] ) << "Wrong vertex color at vertex 7 component 1";
242  EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(7))[2] ) << "Wrong vertex color at vertex 7 component 2";
243 #endif
244 
245  EXPECT_FALSE(opt.vertex_has_normal()) << "Wrong user opt are returned!";
246  EXPECT_FALSE(opt.vertex_has_texcoord()) << "Wrong user opt are returned!";
247  EXPECT_FALSE(opt.face_has_color()) << "Wrong user opt are returned!";
248  EXPECT_TRUE(opt.vertex_has_color()) << "Wrong user opt are returned!";
249  EXPECT_TRUE(opt.color_is_float()) << "Wrong user opt are returned!";
250  EXPECT_TRUE(opt.is_binary()) << "Wrong user opt are returned!";
251 
252  mesh_.release_vertex_colors();
253 }
254 }
Kernel::Color Color
Color type.
Definition: PolyMeshT.hh:116
Has (r) / store (w) vertex colors.
Definition: Options.hh:105
bool write_mesh(const Mesh &_mesh, const std::string &_filename, Options _opt=Options::Default, std::streamsize _precision=6)
Write a mesh to the file _filename.
Definition: MeshIO.hh:207
Has (r) / store (w) float values for colors (currently only implemented for PLY and OFF files) ...
Definition: Options.hh:112
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:112
bool read_mesh(Mesh &_mesh, const std::string &_filename)
Read a mesh from file _filename.
Definition: MeshIO.hh:112
SmartVertexHandle add_vertex(const Point &_p)
Alias for new_vertex(const Point&).
Definition: PolyMeshT.hh:235
Set options for reader/writer modules.
Definition: Options.hh:90
Set binary mode for r/w.
Definition: Options.hh:100