OpenMesh

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
unittests_read_write_OFF.hh
1 #ifndef INCLUDE_UNITTESTS_READ_WRITE_OFF_HH
2 #define INCLUDE_UNITTESTS_READ_WRITE_OFF_HH
3
4 #include <gtest/gtest.h>
5 #include <Unittests/unittests_common.hh>
6
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_
81 OpenMesh::IO::Options opt(OpenMesh::IO::Options::VertexColor);
82 OpenMesh::IO::write_mesh(mesh_, "temp.off", opt);
83 OpenMesh::IO::read_mesh(mesh_, "temp.off", opt);
84
85 // check if vertices still have the same color
86 count = 0;
87 for (Mesh::VertexIter vit = mesh_.vertices_begin(), vitend = mesh_.vertices_end(); vit != vitend; ++vit) {
88 Mesh::Color color = mesh_.color(vit);
89 if ( color[0] != testColor[0] || color[1] != testColor[1] || color[2] != testColor[2] )
90 ++ count;
91 }
92
93 EXPECT_EQ(0, count) << "Vertices should have the same color after writing and reading the OFF file!";
94
95 mesh_.release_vertex_colors();
96 }
97
98 TEST_F(OpenMeshReadWriteOFF, WriteAndReadFloatVertexColorsToAndFromOFFFile) {
99
100 mesh_.clear();
101
102 mesh_.request_vertex_colors();
103
104 OpenMesh::IO::Options opt(OpenMesh::IO::Options::VertexColor);
105
106 bool ok = OpenMesh::IO::read_mesh(mesh_, "meshlab.ply", opt);
107
108 EXPECT_TRUE(ok) << "meshlab.ply could not be read!";
109
110 opt.clear();
111 opt += OpenMesh::IO::Options::VertexColor;
112 opt += OpenMesh::IO::Options::ColorFloat;
113
114 // write the mesh_
115 ok = OpenMesh::IO::write_mesh(mesh_, "cube_floating.off", opt);
116 EXPECT_TRUE(ok) << "cube_floating.off could not be written!";
117 mesh_.clear();
118 ok = OpenMesh::IO::read_mesh(mesh_, "cube_floating.off", opt);
119 EXPECT_TRUE(ok) << "cube_floating.off could not be read!";
120
121 EXPECT_EQ(8u , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
122 EXPECT_EQ(18u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
123 EXPECT_EQ(12u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
124
125 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(0))[0] ) << "Wrong vertex color at vertex 0 component 0";
126 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(0))[1] ) << "Wrong vertex color at vertex 0 component 1";
127 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(0))[2] ) << "Wrong vertex color at vertex 0 component 2";
128
129 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(3))[0] ) << "Wrong vertex color at vertex 3 component 0";
130 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(3))[1] ) << "Wrong vertex color at vertex 3 component 1";
131 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(3))[2] ) << "Wrong vertex color at vertex 3 component 2";
132
133 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(4))[0] ) << "Wrong vertex color at vertex 4 component 0";
134 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(4))[1] ) << "Wrong vertex color at vertex 4 component 1";
135 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(4))[2] ) << "Wrong vertex color at vertex 4 component 2";
136
137 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(7))[0] ) << "Wrong vertex color at vertex 7 component 0";
138 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(7))[1] ) << "Wrong vertex color at vertex 7 component 1";
139 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(7))[2] ) << "Wrong vertex color at vertex 7 component 2";
140
141 EXPECT_FALSE(opt.vertex_has_normal()) << "Wrong user opt are returned!";
142 EXPECT_FALSE(opt.vertex_has_texcoord()) << "Wrong user opt are returned!";
143 EXPECT_TRUE(opt.vertex_has_color()) << "Wrong user opt are returned!";
144 EXPECT_TRUE(opt.color_is_float()) << "Wrong user opt are returned!";
145
146 mesh_.release_vertex_colors();
147 }
148
149 TEST_F(OpenMeshReadWriteOFF, WriteAndReadBinaryFloatVertexColorsToAndFromOFFFile) {
150
151 mesh_.clear();
152
153 mesh_.request_vertex_colors();
154
155 OpenMesh::IO::Options opt(OpenMesh::IO::Options::VertexColor);
156
157 bool ok = OpenMesh::IO::read_mesh(mesh_, "meshlab.ply", opt);
158
159 EXPECT_TRUE(ok) << "meshlab.ply could not be read!";
160
161 opt.clear();
162 opt += OpenMesh::IO::Options::VertexColor;
163 opt += OpenMesh::IO::Options::Binary;
164 opt += OpenMesh::IO::Options::ColorFloat;
165
166 // write the mesh_
167 ok = OpenMesh::IO::write_mesh(mesh_, "cube_floating_binary.off", opt);
168 EXPECT_TRUE(ok) << "cube_floating_binary.off could not be written!";
169 mesh_.clear();
170 opt.clear();
171 opt += OpenMesh::IO::Options::VertexColor;
172 opt += OpenMesh::IO::Options::Binary;
173 opt += OpenMesh::IO::Options::ColorFloat;
174 ok = OpenMesh::IO::read_mesh(mesh_, "cube_floating_binary.off", opt);
175 EXPECT_TRUE(ok) << "cube_floating_binary.off could not be read!";
176
177 EXPECT_EQ(8u , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
178 EXPECT_EQ(18u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
179 EXPECT_EQ(12u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
180
181 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(0))[0] ) << "Wrong vertex color at vertex 0 component 0";
182 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(0))[1] ) << "Wrong vertex color at vertex 0 component 1";
183 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(0))[2] ) << "Wrong vertex color at vertex 0 component 2";
184
185 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(3))[0] ) << "Wrong vertex color at vertex 3 component 0";
186 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(3))[1] ) << "Wrong vertex color at vertex 3 component 1";
187 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(3))[2] ) << "Wrong vertex color at vertex 3 component 2";
188
189 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(4))[0] ) << "Wrong vertex color at vertex 4 component 0";
190 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(4))[1] ) << "Wrong vertex color at vertex 4 component 1";
191 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(4))[2] ) << "Wrong vertex color at vertex 4 component 2";
192
193 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(7))[0] ) << "Wrong vertex color at vertex 7 component 0";
194 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(7))[1] ) << "Wrong vertex color at vertex 7 component 1";
195 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(7))[2] ) << "Wrong vertex color at vertex 7 component 2";
196
197 EXPECT_FALSE(opt.vertex_has_normal()) << "Wrong user opt are returned!";
198 EXPECT_FALSE(opt.vertex_has_texcoord()) << "Wrong user opt are returned!";
199 EXPECT_FALSE(opt.face_has_color()) << "Wrong user opt are returned!";
200 EXPECT_TRUE(opt.vertex_has_color()) << "Wrong user opt are returned!";
201 EXPECT_TRUE(opt.color_is_float()) << "Wrong user opt are returned!";
202 EXPECT_TRUE(opt.is_binary()) << "Wrong user opt are returned!";
203
204 mesh_.release_vertex_colors();
205 }
206
207 #endif // INCLUDE GUARD