OpenMesh

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

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
unittests_trimesh_garbage_collection.hh
1 #ifndef INCLUDE_UNITTESTS_TRIMESH_GARBAGE_COLLECTION_HH
2 #define INCLUDE_UNITTESTS_TRIMESH_GARBAGE_COLLECTION_HH
3
4 #include <gtest/gtest.h>
5 #include <Unittests/unittests_common.hh>
6
7 #include <iostream>
8
9 class OpenMeshTriMeshGarbageCollection : public OpenMeshBase {
10
11 protected:
12
13 // This function is called before each test is run
14 virtual void SetUp() {
15 }
16
17 // This function is called after all tests are through
18 virtual void TearDown() {
19
20 // Do some final stuff with the member data here...
21 }
22
23 // Member already defined in OpenMeshBase
24 //Mesh mesh_;
25 };
26
27 /*
28 * ====================================================================
29 * Define tests below
30 * ====================================================================
31 */
32
33 /* Adds a cube to a trimesh and deletes vertex 0 and calls garbage collection afterwards
34 */
35 TEST_F(OpenMeshTriMeshGarbageCollection, StandardGarbageCollection) {
36
37 mesh_.clear();
38
39 mesh_.request_vertex_status();
40 mesh_.request_edge_status();
41 mesh_.request_halfedge_status();
42 mesh_.request_face_status();
43
44 // Add some vertices
45 Mesh::VertexHandle vhandle[8];
46 vhandle[0] = mesh_.add_vertex(Mesh::Point(-1, -1, 1));
47 vhandle[1] = mesh_.add_vertex(Mesh::Point( 1, -1, 1));
48 vhandle[2] = mesh_.add_vertex(Mesh::Point( 1, 1, 1));
49 vhandle[3] = mesh_.add_vertex(Mesh::Point(-1, 1, 1));
50 vhandle[4] = mesh_.add_vertex(Mesh::Point(-1, -1, -1));
51 vhandle[5] = mesh_.add_vertex(Mesh::Point( 1, -1, -1));
52 vhandle[6] = mesh_.add_vertex(Mesh::Point( 1, 1, -1));
53 vhandle[7] = mesh_.add_vertex(Mesh::Point(-1, 1, -1));
54
55 // Add six faces to form a cube
56 std::vector<Mesh::VertexHandle> face_vhandles;
57
58 face_vhandles.clear();
59 face_vhandles.push_back(vhandle[0]);
60 face_vhandles.push_back(vhandle[1]);
61 face_vhandles.push_back(vhandle[3]);
62 mesh_.add_face(face_vhandles);
63
64 face_vhandles.clear();
65 face_vhandles.push_back(vhandle[1]);
66 face_vhandles.push_back(vhandle[2]);
67 face_vhandles.push_back(vhandle[3]);
68 mesh_.add_face(face_vhandles);
69
70 //=======================
71
72 face_vhandles.clear();
73 face_vhandles.push_back(vhandle[7]);
74 face_vhandles.push_back(vhandle[6]);
75 face_vhandles.push_back(vhandle[5]);
76 mesh_.add_face(face_vhandles);
77
78 face_vhandles.clear();
79 face_vhandles.push_back(vhandle[7]);
80 face_vhandles.push_back(vhandle[5]);
81 face_vhandles.push_back(vhandle[4]);
82 mesh_.add_face(face_vhandles);
83
84 //=======================
85
86 face_vhandles.clear();
87 face_vhandles.push_back(vhandle[1]);
88 face_vhandles.push_back(vhandle[0]);
89 face_vhandles.push_back(vhandle[4]);
90 mesh_.add_face(face_vhandles);
91
92 face_vhandles.clear();
93 face_vhandles.push_back(vhandle[1]);
94 face_vhandles.push_back(vhandle[4]);
95 face_vhandles.push_back(vhandle[5]);
96 mesh_.add_face(face_vhandles);
97
98 //=======================
99
100 face_vhandles.clear();
101 face_vhandles.push_back(vhandle[2]);
102 face_vhandles.push_back(vhandle[1]);
103 face_vhandles.push_back(vhandle[5]);
104 mesh_.add_face(face_vhandles);
105
106 face_vhandles.clear();
107 face_vhandles.push_back(vhandle[2]);
108 face_vhandles.push_back(vhandle[5]);
109 face_vhandles.push_back(vhandle[6]);
110 mesh_.add_face(face_vhandles);
111
112
113 //=======================
114
115 face_vhandles.clear();
116 face_vhandles.push_back(vhandle[3]);
117 face_vhandles.push_back(vhandle[2]);
118 face_vhandles.push_back(vhandle[6]);
119 mesh_.add_face(face_vhandles);
120
121 face_vhandles.clear();
122 face_vhandles.push_back(vhandle[3]);
123 face_vhandles.push_back(vhandle[6]);
124 face_vhandles.push_back(vhandle[7]);
125 mesh_.add_face(face_vhandles);
126
127 //=======================
128
129 face_vhandles.clear();
130 face_vhandles.push_back(vhandle[0]);
131 face_vhandles.push_back(vhandle[3]);
132 face_vhandles.push_back(vhandle[7]);
133 mesh_.add_face(face_vhandles);
134
135 face_vhandles.clear();
136 face_vhandles.push_back(vhandle[0]);
137 face_vhandles.push_back(vhandle[7]);
138 face_vhandles.push_back(vhandle[4]);
139 mesh_.add_face(face_vhandles);
140
141
142 // Test setup:
143 //
144 //
145 // 3 ======== 2
146 // / /|
147 // / / | z
148 // 0 ======== 1 | |
149 // | | | | y
150 // | 7 | 6 | /
151 // | | / | /
152 // | |/ |/
153 // 4 ======== 5 -------> x
154 //
155
156 // Check setup
157 EXPECT_EQ(8u, mesh_.n_vertices() ) << "Wrong initial number of vertices";
158 EXPECT_EQ(12u, mesh_.n_faces() ) << "Wrong initial number of faces";
159
160 mesh_.delete_vertex(vhandle[0]);
161
162 // Check setup
163 EXPECT_EQ(8u, mesh_.n_vertices() ) << "Wrong number of vertices after deletion";
164 EXPECT_EQ(12u, mesh_.n_faces() ) << "Wrong number of faces after deletion";
165
166 mesh_.garbage_collection();
167
168 // Check setup
169 EXPECT_EQ(7u, mesh_.n_vertices() ) << "Wrong number of vertices after garbage collection";
170 EXPECT_EQ(8u, mesh_.n_faces() ) << "Wrong number of faces after garbage collection";
171
172 }
173
174 /* Adds a cube to a trimesh and deletes vertex 0 and calls garbage collection afterwards.
175 * But this time, we track the vertex handles , halfedge handles, and face handles
176 */
177 TEST_F(OpenMeshTriMeshGarbageCollection, TrackedGarbageCollection) {
178
179 mesh_.clear();
180
181 mesh_.request_vertex_status();
182 mesh_.request_edge_status();
183 mesh_.request_halfedge_status();
184 mesh_.request_face_status();
185
186 // Add some vertices
187 Mesh::VertexHandle vhandle[8];
188 vhandle[0] = mesh_.add_vertex(Mesh::Point(-1, -1, 1));
189 vhandle[1] = mesh_.add_vertex(Mesh::Point( 1, -1, 1));
190 vhandle[2] = mesh_.add_vertex(Mesh::Point( 1, 1, 1));
191 vhandle[3] = mesh_.add_vertex(Mesh::Point(-1, 1, 1));
192 vhandle[4] = mesh_.add_vertex(Mesh::Point(-1, -1, -1));
193 vhandle[5] = mesh_.add_vertex(Mesh::Point( 1, -1, -1));
194 vhandle[6] = mesh_.add_vertex(Mesh::Point( 1, 1, -1));
195 vhandle[7] = mesh_.add_vertex(Mesh::Point(-1, 1, -1));
196
197 // Add six faces to form a cube
198 std::vector<Mesh::VertexHandle> face_vhandles;
199
200 // 0
201 face_vhandles.clear();
202 face_vhandles.push_back(vhandle[0]);
203 face_vhandles.push_back(vhandle[1]);
204 face_vhandles.push_back(vhandle[3]);
205 mesh_.add_face(face_vhandles);
206
207 // 1
208 face_vhandles.clear();
209 face_vhandles.push_back(vhandle[1]);
210 face_vhandles.push_back(vhandle[2]);
211 face_vhandles.push_back(vhandle[3]);
212 mesh_.add_face(face_vhandles);
213
214 //=======================
215
216 // 2
217 face_vhandles.clear();
218 face_vhandles.push_back(vhandle[7]);
219 face_vhandles.push_back(vhandle[6]);
220 face_vhandles.push_back(vhandle[5]);
221 mesh_.add_face(face_vhandles);
222
223 // 3
224 face_vhandles.clear();
225 face_vhandles.push_back(vhandle[7]);
226 face_vhandles.push_back(vhandle[5]);
227 face_vhandles.push_back(vhandle[4]);
228 mesh_.add_face(face_vhandles);
229
230 //=======================
231
232 // 4
233 face_vhandles.clear();
234 face_vhandles.push_back(vhandle[1]);
235 face_vhandles.push_back(vhandle[0]);
236 face_vhandles.push_back(vhandle[4]);
237 mesh_.add_face(face_vhandles);
238
239 // 5
240 face_vhandles.clear();
241 face_vhandles.push_back(vhandle[1]);
242 face_vhandles.push_back(vhandle[4]);
243 face_vhandles.push_back(vhandle[5]);
244 mesh_.add_face(face_vhandles);
245
246 //=======================
247
248 // 6
249 face_vhandles.clear();
250 face_vhandles.push_back(vhandle[2]);
251 face_vhandles.push_back(vhandle[1]);
252 face_vhandles.push_back(vhandle[5]);
253 mesh_.add_face(face_vhandles);
254
255 // 7
256 face_vhandles.clear();
257 face_vhandles.push_back(vhandle[2]);
258 face_vhandles.push_back(vhandle[5]);
259 face_vhandles.push_back(vhandle[6]);
260 mesh_.add_face(face_vhandles);
261
262
263 //=======================
264
265 // 8
266 face_vhandles.clear();
267 face_vhandles.push_back(vhandle[3]);
268 face_vhandles.push_back(vhandle[2]);
269 face_vhandles.push_back(vhandle[6]);
270 mesh_.add_face(face_vhandles);
271
272 // 9
273 face_vhandles.clear();
274 face_vhandles.push_back(vhandle[3]);
275 face_vhandles.push_back(vhandle[6]);
276 face_vhandles.push_back(vhandle[7]);
277 mesh_.add_face(face_vhandles);
278
279 //=======================
280
281 // 10
282 face_vhandles.clear();
283 face_vhandles.push_back(vhandle[0]);
284 face_vhandles.push_back(vhandle[3]);
285 face_vhandles.push_back(vhandle[7]);
286 mesh_.add_face(face_vhandles);
287
288 // 11
289 face_vhandles.clear();
290 face_vhandles.push_back(vhandle[0]);
291 face_vhandles.push_back(vhandle[7]);
292 face_vhandles.push_back(vhandle[4]);
293 mesh_.add_face(face_vhandles);
294
295
296 // Test setup:
297 //
298 //
299 // 3 ======== 2
300 // / /|
301 // / / | z
302 // 0 ======== 1 | |
303 // | | | | y
304 // | 7 | 6 | /
305 // | | / | /
306 // | |/ |/
307 // 4 ======== 5 -------> x
308 //
309
310 // Check setup
311 EXPECT_EQ(8u, mesh_.n_vertices() ) << "Wrong initial number of vertices";
312 EXPECT_EQ(12u, mesh_.n_faces() ) << "Wrong initial number of faces";
313
314 //==================================================
315 // Create vectors containing the current handles
316 //==================================================
317 std::vector<Mesh::VertexHandle> vertexHandles;
318 for ( Mesh::VertexIter v_it = mesh_.vertices_begin(); v_it != mesh_.vertices_end(); ++v_it)
319 vertexHandles.push_back(v_it.handle());
320
321 std::vector<Mesh::HalfedgeHandle> halfedgeHandles;
322 for ( Mesh::HalfedgeIter he_it = mesh_.halfedges_begin(); he_it != mesh_.halfedges_end(); ++he_it)
323 halfedgeHandles.push_back(he_it.handle());
324
325 std::vector<Mesh::FaceHandle> faceHandles;
326 for ( Mesh::FaceIter f_it = mesh_.faces_begin(); f_it != mesh_.faces_end(); ++f_it)
327 faceHandles.push_back(f_it.handle());
328
329 //==================================================
330 // Create vectors containing pointers current handles
331 //==================================================
332 std::vector<Mesh::VertexHandle*> vertexHandlesP;
333 for ( unsigned int i = 0 ; i < vertexHandles.size() ; ++i)
334 vertexHandlesP.push_back(&(vertexHandles[i]));
335
336 std::vector<Mesh::HalfedgeHandle*> halfedgeHandlesP;
337 for ( unsigned int i = 0 ; i < halfedgeHandles.size() ; ++i) {
338 halfedgeHandlesP.push_back(&(halfedgeHandles[i]));
339 }
340
341 std::vector<Mesh::FaceHandle*> faceHandlesP;
342 for ( unsigned int i = 0 ; i < faceHandles.size() ; ++i)
343 faceHandlesP.push_back(&(faceHandles[i]));
344
345 // // REMOVE
346 //
347 // OpenMesh::HPropHandleT<int> handle;
348 // mesh_.add_property(handle,"tmp");
349 //
350 //
351 // int count = 0;
352 //
353 // for ( Mesh::HalfedgeIter he_it = mesh_.halfedges_begin(); he_it != mesh_.halfedges_end() ; ++he_it) {
354 // mesh_.property(handle,he_it) =count;
355 // ++count;
356 // }
357
358 // std::cerr << "Vertex : ";
359 // for ( unsigned int i = 0 ; i < vertexHandles.size() ; ++i)
360 // std::cerr << vertexHandles[i].idx() << " ";
361 // std::cerr << std::endl;
362 //
363 // std::cerr << "Halfedge : ";
364 // for ( unsigned int i = 0 ; i < halfedgeHandles.size() ; ++i)
365 // std::cerr << halfedgeHandles[i].idx() << " ";
366 // std::cerr << std::endl;
367 //
368 // std::cerr << "Halfedge property : ";
369 // for ( Mesh::HalfedgeIter he_it = mesh_.halfedges_begin(); he_it != mesh_.halfedges_end() ; ++he_it) {
370 // std::cerr << mesh_.property(handle,he_it) << " ";
371 // }
372 // std::cerr << std::endl;
373 //
374 // std::cerr << "Face : ";
375 // for ( unsigned int i = 0 ; i < faceHandles.size() ; ++i)
376 // std::cerr << faceHandles[i].idx() << " ";
377 // std::cerr << std::endl;
378 // REMOVE END
379
380
381 // Deleting vertex 0
382 // -> deletes vertex 0
383 // -> deletes faces 0,4,10,11
384 mesh_.delete_vertex(vhandle[0]);
385 //mesh_.delete_vertex(vhandle[7]);
386
387 // Check setup
388 EXPECT_EQ(8u, mesh_.n_vertices() ) << "Wrong number of vertices after deletion";
389 EXPECT_EQ(12u, mesh_.n_faces() ) << "Wrong number of faces after deletion";
390
391 mesh_.garbage_collection(vertexHandlesP,halfedgeHandlesP,faceHandlesP,true,true,true);
392
393 // Check setup
394 EXPECT_EQ(7u, mesh_.n_vertices() ) << "Wrong number of vertices after garbage collection";
395 EXPECT_EQ(8u, mesh_.n_faces() ) << "Wrong number of faces after garbage collection";
396
397 //================================
398 // Check the updated handles
399 //================================
400
401 // // // REMOVE
402 // std::cerr << "Vertex : ";
403 // for ( unsigned int i = 0 ; i < vertexHandles.size() ; ++i)
404 // std::cerr << vertexHandles[i].idx() << " ";
405 // std::cerr << std::endl;
406 //
407 // std::cerr << "Halfedge : ";
408 // for ( unsigned int i = 0 ; i < halfedgeHandles.size() ; ++i)
409 // std::cerr << halfedgeHandles[i].idx() << " ";
410 // std::cerr << std::endl;
411 //
412 // std::cerr << "Halfedge property : ";
413 // for ( Mesh::HalfedgeIter he_it = mesh_.halfedges_begin(); he_it != mesh_.halfedges_end() ; ++he_it) {
414 // std::cerr << mesh_.property(handle,he_it) << " ";
415 // }
416 // std::cerr << std::endl;
417 //
418 // std::cerr << "Face : ";
419 // for ( unsigned int i = 0 ; i < faceHandles.size() ; ++i)
420 // std::cerr << faceHandles[i].idx() << " ";
421 // std::cerr << std::endl;
422 // REMOVE END
423
424 // Check setup of vertices
425 EXPECT_EQ(-1, vertexHandles[0].idx() ) << "Wrong vertex handle after update";
426 EXPECT_EQ(1 , vertexHandles[1].idx() ) << "Wrong vertex handle after update";
427 EXPECT_EQ(2 , vertexHandles[2].idx() ) << "Wrong vertex handle after update";
428 EXPECT_EQ(3 , vertexHandles[3].idx() ) << "Wrong vertex handle after update";
429 EXPECT_EQ(4 , vertexHandles[4].idx() ) << "Wrong vertex handle after update";
430 EXPECT_EQ(5 , vertexHandles[5].idx() ) << "Wrong vertex handle after update";
431 EXPECT_EQ(6 , vertexHandles[6].idx() ) << "Wrong vertex handle after update";
432 EXPECT_EQ(0 , vertexHandles[7].idx() ) << "Wrong vertex handle after update";
433
434 // Check setup of halfedge handles
435 EXPECT_EQ(-1, halfedgeHandles[0 ].idx() ) << "Wrong halfedge handle after update";
436 EXPECT_EQ(-1, halfedgeHandles[1 ].idx() ) << "Wrong halfedge handle after update";
437 EXPECT_EQ( 2, halfedgeHandles[2 ].idx() ) << "Wrong halfedge handle after update";
438 EXPECT_EQ( 3, halfedgeHandles[3 ].idx() ) << "Wrong halfedge handle after update";
439 EXPECT_EQ(-1, halfedgeHandles[4 ].idx() ) << "Wrong halfedge handle after update";
440 EXPECT_EQ(-1, halfedgeHandles[5 ].idx() ) << "Wrong halfedge handle after update";
441 EXPECT_EQ( 6, halfedgeHandles[6 ].idx() ) << "Wrong halfedge handle after update";
442 EXPECT_EQ( 7, halfedgeHandles[7 ].idx() ) << "Wrong halfedge handle after update";
443 EXPECT_EQ( 8, halfedgeHandles[8 ].idx() ) << "Wrong halfedge handle after update";
444 EXPECT_EQ( 9, halfedgeHandles[9 ].idx() ) << "Wrong halfedge handle after update";
445 EXPECT_EQ(10, halfedgeHandles[10].idx() ) << "Wrong halfedge handle after update";
446 EXPECT_EQ(11, halfedgeHandles[11].idx() ) << "Wrong halfedge handle after update";
447 EXPECT_EQ(12, halfedgeHandles[12].idx() ) << "Wrong halfedge handle after update";
448 EXPECT_EQ(13, halfedgeHandles[13].idx() ) << "Wrong halfedge handle after update";
449 EXPECT_EQ(14, halfedgeHandles[14].idx() ) << "Wrong halfedge handle after update";
450 EXPECT_EQ(15, halfedgeHandles[15].idx() ) << "Wrong halfedge handle after update";
451 EXPECT_EQ(16, halfedgeHandles[16].idx() ) << "Wrong halfedge handle after update";
452 EXPECT_EQ(17, halfedgeHandles[17].idx() ) << "Wrong halfedge handle after update";
453 EXPECT_EQ(18, halfedgeHandles[18].idx() ) << "Wrong halfedge handle after update";
454 EXPECT_EQ(19, halfedgeHandles[19].idx() ) << "Wrong halfedge handle after update";
455 EXPECT_EQ(-1, halfedgeHandles[20].idx() ) << "Wrong halfedge handle after update";
456 EXPECT_EQ(-1, halfedgeHandles[21].idx() ) << "Wrong halfedge handle after update";
457 EXPECT_EQ(22, halfedgeHandles[22].idx() ) << "Wrong halfedge handle after update";
458 EXPECT_EQ(23, halfedgeHandles[23].idx() ) << "Wrong halfedge handle after update";
459 EXPECT_EQ(24, halfedgeHandles[24].idx() ) << "Wrong halfedge handle after update";
460 EXPECT_EQ(25, halfedgeHandles[25].idx() ) << "Wrong halfedge handle after update";
461 EXPECT_EQ(26, halfedgeHandles[26].idx() ) << "Wrong halfedge handle after update";
462 EXPECT_EQ(27, halfedgeHandles[27].idx() ) << "Wrong halfedge handle after update";
463 EXPECT_EQ(20, halfedgeHandles[28].idx() ) << "Wrong halfedge handle after update";
464 EXPECT_EQ(21, halfedgeHandles[29].idx() ) << "Wrong halfedge handle after update";
465 EXPECT_EQ( 4, halfedgeHandles[30].idx() ) << "Wrong halfedge handle after update";
466 EXPECT_EQ( 5, halfedgeHandles[31].idx() ) << "Wrong halfedge handle after update";
467 EXPECT_EQ( 0, halfedgeHandles[32].idx() ) << "Wrong halfedge handle after update";
468 EXPECT_EQ( 1, halfedgeHandles[33].idx() ) << "Wrong halfedge handle after update";
469 EXPECT_EQ(-1, halfedgeHandles[34].idx() ) << "Wrong halfedge handle after update";
470 EXPECT_EQ(-1, halfedgeHandles[35].idx() ) << "Wrong halfedge handle after update";
471
472 // Check setup of faces
473 EXPECT_EQ(-1 , faceHandles[0 ].idx() ) << "Wrong face handle after update";
474 EXPECT_EQ(1 , faceHandles[1 ].idx() ) << "Wrong face handle after update";
475 EXPECT_EQ(2 , faceHandles[2 ].idx() ) << "Wrong face handle after update";
476 EXPECT_EQ(3 , faceHandles[3 ].idx() ) << "Wrong face handle after update";
477 EXPECT_EQ(-1 , faceHandles[4 ].idx() ) << "Wrong face handle after update";
478 EXPECT_EQ(5 , faceHandles[5 ].idx() ) << "Wrong face handle after update";
479 EXPECT_EQ(6 , faceHandles[6 ].idx() ) << "Wrong face handle after update";
480 EXPECT_EQ(7 , faceHandles[7 ].idx() ) << "Wrong face handle after update";
481 EXPECT_EQ(4 , faceHandles[8 ].idx() ) << "Wrong face handle after update";
482 EXPECT_EQ(0 , faceHandles[9 ].idx() ) << "Wrong face handle after update";
483 EXPECT_EQ(-1 , faceHandles[10].idx() ) << "Wrong face handle after update";
484 EXPECT_EQ(-1 , faceHandles[11].idx() ) << "Wrong face handle after update";
485
486 }
487
488
489
490 #endif // INCLUDE GUARD