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  // using the default color type Vec3uc from DefaultTraits in Traits.hh
64  Mesh::Color testColor(255, 128, 64);
65 
66  // setting colors (different from black)
67  for (Mesh::VertexIter vit = mesh_.vertices_begin(), vitend = mesh_.vertices_end(); vit != vitend; ++vit)
68  mesh_.set_color(*vit, testColor);
69 
70  // check if the colors are correctly setted
71  int count = 0;
72  for (Mesh::VertexIter vit = mesh_.vertices_begin(), vitend = mesh_.vertices_end(); vit != vitend; ++vit) {
73  Mesh::Color color = mesh_.color(*vit);
74  if ( color[0] != testColor[0] || color[1] != testColor[1] || color[2] != testColor[2] )
75  ++ count;
76  }
77 
78  EXPECT_EQ(0, count) << "Vertices have the wrong color!";
79 
80  // write the mesh_
82  OpenMesh::IO::write_mesh(mesh_, "temp.off", opt);
83  OpenMesh::IO::read_mesh(mesh_, "temp.off", opt);
84  remove("temp.off");
85 
86  // check if vertices still have the same color
87  count = 0;
88  for (Mesh::VertexIter vit = mesh_.vertices_begin(), vitend = mesh_.vertices_end(); vit != vitend; ++vit) {
89  Mesh::Color color = mesh_.color(*vit);
90  if ( color[0] != testColor[0] || color[1] != testColor[1] || color[2] != testColor[2] )
91  ++ count;
92  }
93 
94  EXPECT_EQ(0, count) << "Vertices should have the same color after writing and reading the OFF file!";
95 
96  mesh_.release_vertex_colors();
97 }
98 
99 TEST_F(OpenMeshReadWriteOFF, WriteAndReadFloatVertexColorsToAndFromOFFFile) {
100 
101  mesh_.clear();
102 
103  mesh_.request_vertex_colors();
104 
106 
107  bool ok = OpenMesh::IO::read_mesh(mesh_, "meshlab.ply", opt);
108 
109  EXPECT_TRUE(ok) << "meshlab.ply could not be read!";
110 
111  opt.clear();
114 
115  // write the mesh_
116  ok = OpenMesh::IO::write_mesh(mesh_, "cube_floating.off", opt);
117  EXPECT_TRUE(ok) << "cube_floating.off could not be written!";
118  mesh_.clear();
119  ok = OpenMesh::IO::read_mesh(mesh_, "cube_floating.off", opt);
120  EXPECT_TRUE(ok) << "cube_floating.off could not be read!";
121 
122  EXPECT_EQ(8u , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
123  EXPECT_EQ(18u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
124  EXPECT_EQ(12u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
125 
126  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(0))[0] ) << "Wrong vertex color at vertex 0 component 0";
127  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(0))[1] ) << "Wrong vertex color at vertex 0 component 1";
128  EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(0))[2] ) << "Wrong vertex color at vertex 0 component 2";
129 
130  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(3))[0] ) << "Wrong vertex color at vertex 3 component 0";
131  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(3))[1] ) << "Wrong vertex color at vertex 3 component 1";
132  EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(3))[2] ) << "Wrong vertex color at vertex 3 component 2";
133 
134  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(4))[0] ) << "Wrong vertex color at vertex 4 component 0";
135  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(4))[1] ) << "Wrong vertex color at vertex 4 component 1";
136  EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(4))[2] ) << "Wrong vertex color at vertex 4 component 2";
137 
138  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(7))[0] ) << "Wrong vertex color at vertex 7 component 0";
139  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(7))[1] ) << "Wrong vertex color at vertex 7 component 1";
140  EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(7))[2] ) << "Wrong vertex color at vertex 7 component 2";
141 
142  EXPECT_FALSE(opt.vertex_has_normal()) << "Wrong user opt are returned!";
143  EXPECT_FALSE(opt.vertex_has_texcoord()) << "Wrong user opt are returned!";
144  EXPECT_TRUE(opt.vertex_has_color()) << "Wrong user opt are returned!";
145  EXPECT_TRUE(opt.color_is_float()) << "Wrong user opt are returned!";
146 
147  mesh_.release_vertex_colors();
148 }
149 
150 TEST_F(OpenMeshReadWriteOFF, WriteAndReadBinaryFloatVertexColorsToAndFromOFFFile) {
151 
152  mesh_.clear();
153 
154  mesh_.request_vertex_colors();
155 
157 
158  bool ok = OpenMesh::IO::read_mesh(mesh_, "meshlab.ply", opt);
159 
160  EXPECT_TRUE(ok) << "meshlab.ply could not be read!";
161 
162  opt.clear();
166 
167  // write the mesh_
168  ok = OpenMesh::IO::write_mesh(mesh_, "cube_floating_binary.off", opt);
169  EXPECT_TRUE(ok) << "cube_floating_binary.off could not be written!";
170  mesh_.clear();
171  opt.clear();
175  ok = OpenMesh::IO::read_mesh(mesh_, "cube_floating_binary.off", opt);
176  EXPECT_TRUE(ok) << "cube_floating_binary.off could not be read!";
177 
178  EXPECT_EQ(8u , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
179  EXPECT_EQ(18u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
180  EXPECT_EQ(12u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
181 
182  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(0))[0] ) << "Wrong vertex color at vertex 0 component 0";
183  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(0))[1] ) << "Wrong vertex color at vertex 0 component 1";
184  EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(0))[2] ) << "Wrong vertex color at vertex 0 component 2";
185 
186  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(3))[0] ) << "Wrong vertex color at vertex 3 component 0";
187  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(3))[1] ) << "Wrong vertex color at vertex 3 component 1";
188  EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(3))[2] ) << "Wrong vertex color at vertex 3 component 2";
189 
190  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(4))[0] ) << "Wrong vertex color at vertex 4 component 0";
191  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(4))[1] ) << "Wrong vertex color at vertex 4 component 1";
192  EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(4))[2] ) << "Wrong vertex color at vertex 4 component 2";
193 
194  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(7))[0] ) << "Wrong vertex color at vertex 7 component 0";
195  EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(7))[1] ) << "Wrong vertex color at vertex 7 component 1";
196  EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(7))[2] ) << "Wrong vertex color at vertex 7 component 2";
197 
198  EXPECT_FALSE(opt.vertex_has_normal()) << "Wrong user opt are returned!";
199  EXPECT_FALSE(opt.vertex_has_texcoord()) << "Wrong user opt are returned!";
200  EXPECT_FALSE(opt.face_has_color()) << "Wrong user opt are returned!";
201  EXPECT_TRUE(opt.vertex_has_color()) << "Wrong user opt are returned!";
202  EXPECT_TRUE(opt.color_is_float()) << "Wrong user opt are returned!";
203  EXPECT_TRUE(opt.is_binary()) << "Wrong user opt are returned!";
204 
205  mesh_.release_vertex_colors();
206 }
207 }
Set binary mode for r/w.
Definition: Options.hh:105
Kernel::Color Color
Color type.
Definition: PolyMeshT.hh:119
Has (r) / store (w) float values for colors (currently only implemented for PLY and OFF files) ...
Definition: Options.hh:117
bool read_mesh(Mesh &_mesh, const std::string &_filename)
Read a mesh from file _filename.
Definition: MeshIO.hh:104
Set options for reader/writer modules.
Definition: Options.hh:95
VertexHandle add_vertex(const Point &_p)
Alias for new_vertex(const Point&).
Definition: PolyMeshT.hh:236
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:115
Has (r) / store (w) vertex colors.
Definition: Options.hh:110
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:199