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_OBJ.hh
1 #ifndef INCLUDE_UNITTESTS_READ_WRITE_OBJ_HH
2 #define INCLUDE_UNITTESTS_READ_WRITE_OBJ_HH
3
4 #include <gtest/gtest.h>
5 #include <Unittests/unittests_common.hh>
6
7
8 class OpenMeshReadWriteOBJ : 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 obj file of a cube
36 */
37 TEST_F(OpenMeshReadWriteOBJ, LoadSimpleOBJ) {
38
39 mesh_.clear();
40
41 bool ok = OpenMesh::IO::read_mesh(mesh_, "cube-minimal.obj");
42
43 EXPECT_TRUE(ok) << "Unable to load cube-minimal.obj";
44
45 EXPECT_EQ(8u , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
46 EXPECT_EQ(18u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
47 EXPECT_EQ(12u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
48 }
49
50 /*
51 * Just load a obj file of a cube and checks the halfedge and vertex normals
52 */
53 TEST_F(OpenMeshReadWriteOBJ, LoadSimpleOBJCheckHalfEdgeAndVertexNormals) {
54
55 mesh_.clear();
56
57 mesh_.request_halfedge_normals();
58 mesh_.request_vertex_normals();
59
60 OpenMesh::IO::Options options;
61 options += OpenMesh::IO::Options::VertexNormal;
62
63 std::string file_name = "cube-minimal.obj";
64
65 bool ok = OpenMesh::IO::read_mesh(mesh_, file_name,options);
66
67 EXPECT_TRUE(ok) << file_name;
68
69 EXPECT_EQ(8u , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
70 EXPECT_EQ(18u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
71 EXPECT_EQ(12u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
72 EXPECT_EQ(36u , mesh_.n_halfedges()) << "The number of loaded halfedges is not correct!";
73
75 //check vertex normals
76 EXPECT_EQ(0, mesh_.normal(mesh_.vertex_handle(0))[0] ) << "Wrong vertex normal at vertex 0 component 0";
77 EXPECT_EQ(-1, mesh_.normal(mesh_.vertex_handle(0))[1] ) << "Wrong vertex normal at vertex 0 component 1";
78 EXPECT_EQ(0, mesh_.normal(mesh_.vertex_handle(0))[2] ) << "Wrong vertex normal at vertex 0 component 2";
79
80 EXPECT_EQ(0, mesh_.normal(mesh_.vertex_handle(3))[0] ) << "Wrong vertex normal at vertex 3 component 0";
81 EXPECT_EQ(0, mesh_.normal(mesh_.vertex_handle(3))[1] ) << "Wrong vertex normal at vertex 3 component 1";
82 EXPECT_EQ(1, mesh_.normal(mesh_.vertex_handle(3))[2] ) << "Wrong vertex normal at vertex 3 component 2";
83
84 EXPECT_EQ(0, mesh_.normal(mesh_.vertex_handle(4))[0] ) << "Wrong vertex normal at vertex 4 component 0";
85 EXPECT_EQ(-1, mesh_.normal(mesh_.vertex_handle(4))[1] ) << "Wrong vertex normal at vertex 4 component 1";
86 EXPECT_EQ(0, mesh_.normal(mesh_.vertex_handle(4))[2] ) << "Wrong vertex normal at vertex 4 component 2";
87
88 EXPECT_EQ(0, mesh_.normal(mesh_.vertex_handle(7))[0] ) << "Wrong vertex normal at vertex 7 component 0";
89 EXPECT_EQ(0, mesh_.normal(mesh_.vertex_handle(7))[1] ) << "Wrong vertex normal at vertex 7 component 1";
90 EXPECT_EQ(1, mesh_.normal(mesh_.vertex_handle(7))[2] ) << "Wrong vertex normal at vertex 7 component 2";
91
93 //check halfedge normals
94 EXPECT_EQ(0, mesh_.normal(mesh_.halfedge_handle(0))[0] ) << "Wrong halfedge normal at halfedge 0 component 0";
95 EXPECT_EQ(0, mesh_.normal(mesh_.halfedge_handle(0))[1] ) << "Wrong halfedge normal at halfedge 0 component 1";
96 EXPECT_EQ(-1, mesh_.normal(mesh_.halfedge_handle(0))[2] ) << "Wrong halfedge normal at halfedge 0 component 2";
97
98 EXPECT_EQ(-1, mesh_.normal(mesh_.halfedge_handle(10))[0] ) << "Wrong halfedge normal at halfedge 10 component 0";
99 EXPECT_EQ(0, mesh_.normal(mesh_.halfedge_handle(10))[1] ) << "Wrong halfedge normal at halfedge 10 component 1";
100 EXPECT_EQ(0, mesh_.normal(mesh_.halfedge_handle(10))[2] ) << "Wrong halfedge normal at halfedge 10 component 2";
101
102 EXPECT_EQ(0, mesh_.normal(mesh_.halfedge_handle(19))[0] ) << "Wrong halfedge normal at halfedge 19 component 0";
103 EXPECT_EQ(1, mesh_.normal(mesh_.halfedge_handle(19))[1] ) << "Wrong halfedge normal at halfedge 19 component 1";
104 EXPECT_EQ(0, mesh_.normal(mesh_.halfedge_handle(19))[2] ) << "Wrong halfedge normal at halfedge 19 component 2";
105
106 EXPECT_EQ(1, mesh_.normal(mesh_.halfedge_handle(24))[0] ) << "Wrong halfedge normal at halfedge 24 component 0";
107 EXPECT_EQ(0, mesh_.normal(mesh_.halfedge_handle(24))[1] ) << "Wrong halfedge normal at halfedge 24 component 1";
108 EXPECT_EQ(0, mesh_.normal(mesh_.halfedge_handle(24))[2] ) << "Wrong halfedge normal at halfedge 24 component 2";
109
110 EXPECT_EQ(0, mesh_.normal(mesh_.halfedge_handle(30))[0] ) << "Wrong halfedge normal at halfedge 30 component 0";
111 EXPECT_EQ(-1, mesh_.normal(mesh_.halfedge_handle(30))[1] ) << "Wrong halfedge normal at halfedge 30 component 1";
112 EXPECT_EQ(0, mesh_.normal(mesh_.halfedge_handle(30))[2] ) << "Wrong halfedge normal at halfedge 30 component 2";
113
114 EXPECT_EQ(0, mesh_.normal(mesh_.halfedge_handle(35))[0] ) << "Wrong halfedge normal at halfedge 35 component 0";
115 EXPECT_EQ(0, mesh_.normal(mesh_.halfedge_handle(35))[1] ) << "Wrong halfedge normal at halfedge 35 component 1";
116 EXPECT_EQ(1, mesh_.normal(mesh_.halfedge_handle(35))[2] ) << "Wrong halfedge normal at halfedge 35 component 2";
117
118 mesh_.release_vertex_normals();
119 mesh_.release_halfedge_normals();
120
121 }
122
123 /*
124 * Just load a obj file and set vertex color option before loading
125 */
126 TEST_F(OpenMeshReadWriteOBJ, LoadSimpleOBJForceVertexColorsAlthoughNotAvailable) {
127
128 mesh_.clear();
129
130 mesh_.request_vertex_colors();
131
132 std::string file_name = "cube-minimal.obj";
133
134 OpenMesh::IO::Options options;
135 options += OpenMesh::IO::Options::VertexColor;
136
137 bool ok = OpenMesh::IO::read_mesh(mesh_, file_name,options);
138
139 EXPECT_TRUE(ok) << file_name;
140
141 EXPECT_EQ(8u , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
142 EXPECT_EQ(18u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
143 EXPECT_EQ(12u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
144 EXPECT_EQ(36u , mesh_.n_halfedges()) << "The number of loaded halfedges is not correct!";
145
146 }
147
148
149 /*
150 * Just load a obj file of a cube and checks the halfedge texCoords
151 */
152 TEST_F(OpenMeshReadWriteOBJ, LoadSimpleOBJCheckTexCoords) {
153
154 mesh_.clear();
155
156 mesh_.request_halfedge_texcoords2D();
157
158 OpenMesh::IO::Options options;
159 options += OpenMesh::IO::Options::FaceTexCoord;
160
161 std::string file_name = "cube-minimal-texCoords.obj";
162
163 bool ok = OpenMesh::IO::read_mesh(mesh_, file_name,options);
164
165 EXPECT_TRUE(ok) << file_name;
166
167 EXPECT_EQ(1, mesh_.texcoord2D(mesh_.halfedge_handle(0))[0] ) << "Wrong texCoord at halfedge 0 component 0";
168 EXPECT_EQ(1, mesh_.texcoord2D(mesh_.halfedge_handle(0))[1] ) << "Wrong texCoord at halfedge 0 component 1";
169
170 EXPECT_EQ(3, mesh_.texcoord2D(mesh_.halfedge_handle(10))[0] ) << "Wrong texCoord at halfedge 1 component 0";
171 EXPECT_EQ(3, mesh_.texcoord2D(mesh_.halfedge_handle(10))[1] ) << "Wrong texCoord at halfedge 1 component 1";
172
173 EXPECT_EQ(6, mesh_.texcoord2D(mesh_.halfedge_handle(19))[0] ) << "Wrong texCoord at halfedge 4 component 0";
174 EXPECT_EQ(6, mesh_.texcoord2D(mesh_.halfedge_handle(19))[1] ) << "Wrong texCoord at halfedge 4 component 1";
175
176 EXPECT_EQ(7, mesh_.texcoord2D(mesh_.halfedge_handle(24))[0] ) << "Wrong texCoord at halfedge 7 component 0";
177 EXPECT_EQ(7, mesh_.texcoord2D(mesh_.halfedge_handle(24))[1] ) << "Wrong texCoord at halfedge 7 component 1";
178
179 EXPECT_EQ(9, mesh_.texcoord2D(mesh_.halfedge_handle(30))[0] ) << "Wrong texCoord at halfedge 9 component 0";
180 EXPECT_EQ(9, mesh_.texcoord2D(mesh_.halfedge_handle(30))[1] ) << "Wrong texCoord at halfedge 9 component 1";
181
182 EXPECT_EQ(12, mesh_.texcoord2D(mesh_.halfedge_handle(35))[0] ) << "Wrong texCoord at halfedge 11 component 0";
183 EXPECT_EQ(12, mesh_.texcoord2D(mesh_.halfedge_handle(35))[1] ) << "Wrong texCoord at halfedge 11 component 1";
184
185 mesh_.release_halfedge_texcoords2D();
186 }
187
188 /*
189 * Just load a obj file of a square with a material
190 */
191 TEST_F(OpenMeshReadWriteOBJ, LoadObjWithMaterial) {
192
193 mesh_.clear();
194
195 mesh_.request_face_colors();
196
197 OpenMesh::IO::Options options;
198 options += OpenMesh::IO::Options::FaceColor;
199
200 std::string file_name = "square_material.obj";
201
202 bool ok = OpenMesh::IO::read_mesh(mesh_, file_name,options);
203
204 EXPECT_TRUE(ok) << file_name;
205
206 OpenMesh::FaceHandle fh = mesh_.face_handle(mesh_.halfedge_handle(0));
207
208 EXPECT_TRUE(fh.is_valid()) << "fh should be valid";
209
210 EXPECT_EQ(128, mesh_.color(fh)[0] ) << "Wrong vertex color at vertex 0 component 0";
211 EXPECT_EQ(128, mesh_.color(fh)[1] ) << "Wrong vertex color at vertex 0 component 1";
212 EXPECT_EQ(128, mesh_.color(fh)[2] ) << "Wrong vertex color at vertex 0 component 2";
213
214 mesh_.release_face_colors();
215 }
216
217 /*
218 * Just load a obj file of a cube with vertex colors defined directly after the vertex definitions
219 */
220 TEST_F(OpenMeshReadWriteOBJ, LoadSimpleOBJWithVertexColorsAfterVertices) {
221
222 mesh_.clear();
223
224 mesh_.request_vertex_colors();
225
226 OpenMesh::IO::Options options;
227 options += OpenMesh::IO::Options::VertexColor;
228
229 bool ok = OpenMesh::IO::read_mesh(mesh_, "cube-minimal-vertex-colors-after-vertex-definition.obj",options);
230
231 EXPECT_TRUE(ok) << "Unable to load cube-minimal-vertex-colors-after-vertex-definition.obj";
232
233 EXPECT_EQ(8u , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
234 EXPECT_EQ(18u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
235 EXPECT_EQ(12u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
236
237 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(0))[0] ) << "Wrong vertex color at vertex 0 component 0";
238 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(0))[1] ) << "Wrong vertex color at vertex 0 component 1";
239 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(0))[2] ) << "Wrong vertex color at vertex 0 component 2";
240
241 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(3))[0] ) << "Wrong vertex color at vertex 3 component 0";
242 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(3))[1] ) << "Wrong vertex color at vertex 3 component 1";
243 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(3))[2] ) << "Wrong vertex color at vertex 3 component 2";
244
245 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(4))[0] ) << "Wrong vertex color at vertex 4 component 0";
246 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(4))[1] ) << "Wrong vertex color at vertex 4 component 1";
247 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(4))[2] ) << "Wrong vertex color at vertex 4 component 2";
248
249 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(7))[0] ) << "Wrong vertex color at vertex 7 component 0";
250 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(7))[1] ) << "Wrong vertex color at vertex 7 component 1";
251 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(7))[2] ) << "Wrong vertex color at vertex 7 component 2";
252
253 mesh_.release_vertex_colors();
254 }
255
256 /*
257 * Just load a obj file of a cube with vertex colors defined as separate lines
258 */
259 TEST_F(OpenMeshReadWriteOBJ, LoadSimpleOBJWithVertexColorsAsVCLines) {
260
261 mesh_.clear();
262
263 mesh_.request_vertex_colors();
264
265 OpenMesh::IO::Options options;
266 options += OpenMesh::IO::Options::VertexColor;
267
268 bool ok = OpenMesh::IO::read_mesh(mesh_, "cube-minimal-vertex-colors-as-vc-lines.obj",options);
269
270 EXPECT_TRUE(ok) << "Unable to load cube-minimal-vertex-colors-as-vc-lines.obj";
271
272 EXPECT_EQ(8u , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
273 EXPECT_EQ(18u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
274 EXPECT_EQ(12u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
275
276 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(0))[0] ) << "Wrong vertex color at vertex 0 component 0";
277 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(0))[1] ) << "Wrong vertex color at vertex 0 component 1";
278 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(0))[2] ) << "Wrong vertex color at vertex 0 component 2";
279
280 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(3))[0] ) << "Wrong vertex color at vertex 3 component 0";
281 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(3))[1] ) << "Wrong vertex color at vertex 3 component 1";
282 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(3))[2] ) << "Wrong vertex color at vertex 3 component 2";
283
284 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(4))[0] ) << "Wrong vertex color at vertex 4 component 0";
285 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(4))[1] ) << "Wrong vertex color at vertex 4 component 1";
286 EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(4))[2] ) << "Wrong vertex color at vertex 4 component 2";
287
288 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(7))[0] ) << "Wrong vertex color at vertex 7 component 0";
289 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(7))[1] ) << "Wrong vertex color at vertex 7 component 1";
290 EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(7))[2] ) << "Wrong vertex color at vertex 7 component 2";
291
292 mesh_.release_vertex_colors();
293
294 }
295
296 #endif // INCLUDE GUARD