unittests_trimesh_collapse.hh 17 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#ifndef INCLUDE_UNITTESTS_TRIMESH_COLLAPSE_HH
#define INCLUDE_UNITTESTS_TRIMESH_COLLAPSE_HH

#include <gtest/gtest.h>
#include <Unittests/unittests_common.hh>

#include <iostream>

class OpenMeshCollapse : public OpenMeshBase {

    protected:

        // This function is called before each test is run
        virtual void SetUp() {
        }

        // This function is called after all tests are through
        virtual void TearDown() {

            // Do some final stuff with the member data here...
        }

    // Member already defined in OpenMeshBase
    //Mesh mesh_;  
};

/*
 * ====================================================================
 * Define tests below
 * ====================================================================
 */

/*
 * Collapsing a tetrahedron
 */
TEST_F(OpenMeshCollapse, CollapseTetrahedron) {

  mesh_.clear();

  // Add some vertices
  Mesh::VertexHandle vhandle[4];

  vhandle[0] = mesh_.add_vertex(Mesh::Point(0, 0, 0));
  vhandle[1] = mesh_.add_vertex(Mesh::Point(0, 1, 0));
  vhandle[2] = mesh_.add_vertex(Mesh::Point(1, 1, 0));
  vhandle[3] = mesh_.add_vertex(Mesh::Point(1, 0, 0));

  // Add four faces
  std::vector<Mesh::VertexHandle> face_vhandles;

  face_vhandles.push_back(vhandle[0]);
  face_vhandles.push_back(vhandle[1]);
  face_vhandles.push_back(vhandle[2]);
  mesh_.add_face(face_vhandles);

  face_vhandles.clear();

  face_vhandles.push_back(vhandle[0]);
  face_vhandles.push_back(vhandle[2]);
  face_vhandles.push_back(vhandle[3]);
  mesh_.add_face(face_vhandles);

  face_vhandles.clear();

  face_vhandles.push_back(vhandle[2]);
  face_vhandles.push_back(vhandle[1]);
  face_vhandles.push_back(vhandle[3]);
  mesh_.add_face(face_vhandles);

  face_vhandles.clear();

  face_vhandles.push_back(vhandle[3]);
  face_vhandles.push_back(vhandle[1]);
  face_vhandles.push_back(vhandle[0]);
  mesh_.add_face(face_vhandles);

  mesh_.request_vertex_status();
  mesh_.request_edge_status();
  mesh_.request_face_status();

  Mesh::HalfedgeHandle v0v1 = mesh_.halfedge_handle(0);
  Mesh::HalfedgeHandle v1v0 = mesh_.opposite_halfedge_handle(v0v1);

  Mesh::HalfedgeHandle v1vL = mesh_.next_halfedge_handle(v0v1);
  Mesh::HalfedgeHandle vLv1 = mesh_.opposite_halfedge_handle(v1vL);
  Mesh::HalfedgeHandle vLv0 = mesh_.next_halfedge_handle(v1vL);
  Mesh::HalfedgeHandle v0vL = mesh_.opposite_halfedge_handle(vLv0);

  Mesh::HalfedgeHandle vLvR = mesh_.next_halfedge_handle(v0vL);
  Mesh::HalfedgeHandle vRvL = mesh_.opposite_halfedge_handle(vLvR);

  Mesh::HalfedgeHandle v0vR = mesh_.next_halfedge_handle(v1v0);
  Mesh::HalfedgeHandle vRv0 = mesh_.opposite_halfedge_handle(v0vR);
  Mesh::HalfedgeHandle vRv1 = mesh_.next_halfedge_handle(v0vR);
  Mesh::HalfedgeHandle v1vR = mesh_.opposite_halfedge_handle(vRv1);



  Mesh::VertexHandle v0     = mesh_.from_vertex_handle(v0v1);
  Mesh::VertexHandle v1     = mesh_.to_vertex_handle(v0v1);
  Mesh::VertexHandle vL     = mesh_.to_vertex_handle(mesh_.next_halfedge_handle(v0v1));
  Mesh::VertexHandle vR     = mesh_.to_vertex_handle(mesh_.next_halfedge_handle(v1v0));

  // ===================================================================
  // Check preconditions
  // ===================================================================

  EXPECT_TRUE( mesh_.is_collapse_ok(v0v1) ) << "Collapse not ok for halfedge 0";
  EXPECT_TRUE( mesh_.is_collapse_ok(v1v0) ) << "Collapse not ok for opposite of halfedge 0";

  // Test the Vertex indices
  EXPECT_EQ(0, v0.idx() ) << "Index wrong for from vertex of collapse halfedge";
  EXPECT_EQ(1, v1.idx() ) << "Index wrong for to vertex of collapse halfedge";
  EXPECT_EQ(2, vL.idx() ) << "Index wrong for left vertex of collapse halfedge";
  EXPECT_EQ(3, vR.idx() ) << "Index wrong for right vertex of collapse halfedge";

  // Check the halfedges
  EXPECT_EQ(0, v0v1.idx() ) << "Index wrong for collapse halfedge";
  EXPECT_EQ(1, v1v0.idx() ) << "Index wrong for opposite collapse halfedge";

  EXPECT_EQ(2  , v1vL.idx() ) << "Index wrong for v1vL halfedge";
  EXPECT_EQ(3  , vLv1.idx() ) << "Index wrong for vLv1 halfedge";
  EXPECT_EQ(4  , vLv0.idx() ) << "Index wrong for vLv0 halfedge";
  EXPECT_EQ(5  , v0vL.idx() ) << "Index wrong for v0vL halfedge";

  EXPECT_EQ(6  , vLvR.idx() ) << "Index wrong for vLvR halfedge";
  EXPECT_EQ(7  , vRvL.idx() ) << "Index wrong for vRvL halfedge";

  EXPECT_EQ(8  , vRv0.idx() ) << "Index wrong for vRv0 halfedge";
  EXPECT_EQ(9  , v0vR.idx() ) << "Index wrong for v0vR halfedge";

  EXPECT_EQ(10 , v1vR.idx() ) << "Index wrong for v1vR halfedge";
  EXPECT_EQ(11 , vRv1.idx() ) << "Index wrong for vRv1 halfedge";

  // ===================================================================
  // Execute collapse
  // ===================================================================

  mesh_.collapse(v0v1);

  // ===================================================================
  // Check configuration afterwards
  // ===================================================================

  /** Now the configuration should look like this:
   *  The numbers at the side denote the halfedges
   *           1
   *          / \
   *         /   \
   *       //    \\
   *      3/2    11\10
   *      //        \\
   *     /    6-->   \
   *    2 ----------- 3
   *        <--7
   *
   */


  EXPECT_EQ(4 , mesh_.n_faces() ) << "Wrong number of faces (garbage collection not executed!)";

  // Check if the right vertices got deleted
  EXPECT_TRUE( mesh_.status(mesh_.face_handle(0)).deleted() ) << "Face 0 not deleted";
  EXPECT_FALSE( mesh_.status(mesh_.face_handle(1)).deleted() ) << "Face 1 deleted";
  EXPECT_FALSE( mesh_.status(mesh_.face_handle(2)).deleted() ) << "Face 2 deleted";
  EXPECT_TRUE( mesh_.status(mesh_.face_handle(3)).deleted() ) << "Face 3 not deleted";

  // Check the vertices of the two remaining faces
  Mesh::FaceHandle fh_1 = mesh_.face_handle(1);
  Mesh::FaceHandle fh_2 = mesh_.face_handle(2);

  Mesh::FaceVertexIter fv_it =  mesh_.fv_begin(fh_1);

  EXPECT_EQ(1 , fv_it.handle().idx() ) << "Index wrong for Vertex 1 of face 1";
  ++fv_it;
  EXPECT_EQ(2 , fv_it.handle().idx() ) << "Index wrong for Vertex 2 of face 1";
  ++fv_it;
  EXPECT_EQ(3 , fv_it.handle().idx() ) << "Index wrong for Vertex 3 of face 1";

  fv_it =  mesh_.fv_begin(fh_2);
  EXPECT_EQ(2 , fv_it.handle().idx() ) << "Index wrong for Vertex 1 of face 2";
  ++fv_it;
  EXPECT_EQ(1 , fv_it.handle().idx() ) << "Index wrong for Vertex 2 of face 2";
  ++fv_it;
  EXPECT_EQ(3 , fv_it.handle().idx() ) << "Index wrong for Vertex 3 of face 2";

  // Get the first halfedge of face 1
  Mesh::HalfedgeHandle fh_1_he = mesh_.halfedge_handle(fh_1);

  EXPECT_EQ(11 , fh_1_he.idx() ) << "Index wrong for first halfedge of face 1";
  EXPECT_EQ(1  , mesh_.to_vertex_handle(fh_1_he).idx() ) << "First halfedge inside face 1 pointing to wrong vertex";

  Mesh::HalfedgeHandle next = mesh_.next_halfedge_handle(fh_1_he);
  EXPECT_EQ(2 , next.idx() ) << "Index wrong for second halfedge inside face 1 ";
  EXPECT_EQ(2 , mesh_.to_vertex_handle(next).idx() ) << "second halfedge inside face 1 pointing to wrong vertex ";

  next = mesh_.next_halfedge_handle(next);
  EXPECT_EQ(6 , next.idx() ) << "Index wrong for third halfedge inside face 1 ";
  EXPECT_EQ(3 , mesh_.to_vertex_handle(next).idx() ) << "Third halfedge inside face 1 pointing to wrong vertex ";

  // Get the first halfedge of face 2
  Mesh::HalfedgeHandle fh_2_he = mesh_.halfedge_handle(fh_2);

  EXPECT_EQ(7 , fh_2_he.idx() ) << "Index wrong for first halfedge of face 2";
  EXPECT_EQ(2  , mesh_.to_vertex_handle(fh_2_he).idx() ) << "First halfedge inside face 2 pointing to wrong vertex";

  next = mesh_.next_halfedge_handle(fh_2_he);
  EXPECT_EQ(3 , next.idx() ) << "Index wrong for second halfedge inside face 2";
  EXPECT_EQ(1 , mesh_.to_vertex_handle(next).idx() ) << "second halfedge inside face 2 pointing to wrong vertex ";

  next = mesh_.next_halfedge_handle(next);
  EXPECT_EQ(10 , next.idx() ) << "Index wrong for third halfedge inside face 2";
  EXPECT_EQ(3 , mesh_.to_vertex_handle(next).idx() ) << "Third halfedge inside face 2 pointing to wrong vertex ";

  // Vertex 1 outgoing
  Mesh::VertexOHalfedgeIter voh_it = mesh_.voh_begin(mesh_.vertex_handle(1));
  EXPECT_EQ(10 , voh_it.handle().idx() ) << "Index wrong for first outgoing halfedge of vertex 1";
  ++voh_it;
  EXPECT_EQ(2  , voh_it.handle().idx() ) << "Index wrong for second outgoing halfedge of vertex 1";
  ++voh_it;
  EXPECT_EQ(10 , voh_it.handle().idx() ) << "Index wrong for third(one lap) outgoing halfedge of vertex 1";

  // Vertex 2 outgoing
  voh_it = mesh_.voh_begin(mesh_.vertex_handle(2));
  EXPECT_EQ(3 , voh_it.handle().idx() ) << "Index wrong for first outgoing halfedge of vertex 2";
  ++voh_it;
  EXPECT_EQ(6 , voh_it.handle().idx() ) << "Index wrong for second outgoing halfedge of vertex 2";
  ++voh_it;
  EXPECT_EQ(3 , voh_it.handle().idx() ) << "Index wrong for third(one lap) outgoing halfedge of vertex 2";

  // Vertex 3 outgoing
  voh_it = mesh_.voh_begin(mesh_.vertex_handle(3));
  EXPECT_EQ(11 , voh_it.handle().idx() ) << "Index wrong for first outgoing halfedge of vertex 3";
  ++voh_it;
  EXPECT_EQ(7  , voh_it.handle().idx() ) << "Index wrong for second outgoing halfedge of vertex 3";
  ++voh_it;
  EXPECT_EQ(11 , voh_it.handle().idx() ) << "Index wrong for third(one lap) outgoing halfedge of vertex 3";

  // ===================================================================
  // Cleanup
  // ===================================================================
  mesh_.garbage_collection();

  // ===================================================================
  // Check configuration afterwards
  // ===================================================================

  /** Now the configuration should look like this:
     *  The numbers at the side denote the halfedges
     *           0
     *          / \
     *         /   \
     *       //    \\
     *      4/5     0\1
     *      //        \\
     *     /    3-->   \
     *    2 ----------- 1
     *        <--2
     *
     */

  EXPECT_EQ(2 , mesh_.n_faces() ) << "Wrong number of faces (garbage collection executed!)";

  // Check the vertices of the two remaining faces
  Mesh::FaceHandle fh_0 = mesh_.face_handle(0);
  fh_1 = mesh_.face_handle(1);

  fv_it =  mesh_.fv_begin(fh_0);

  EXPECT_EQ(2 , fv_it.handle().idx() ) << "Index wrong for Vertex 1 of face 0 after garbage collection";
  ++fv_it;
  EXPECT_EQ(1 , fv_it.handle().idx() ) << "Index wrong for Vertex 2 of face 0 after garbage collection";
  ++fv_it;
  EXPECT_EQ(0 , fv_it.handle().idx() ) << "Index wrong for Vertex 3 of face 0 after garbage collection";

  fv_it =  mesh_.fv_begin(fh_1);
  EXPECT_EQ(1 , fv_it.handle().idx() ) << "Index wrong for Vertex 1 of face 1 after garbage collection";
  ++fv_it;
  EXPECT_EQ(2 , fv_it.handle().idx() ) << "Index wrong for Vertex 2 of face 1 after garbage collection";
  ++fv_it;
  EXPECT_EQ(0 , fv_it.handle().idx() ) << "Index wrong for Vertex 3 of face 1 after garbage collection";

  // Get the first halfedge of face 1
  Mesh::HalfedgeHandle fh_0_he = mesh_.halfedge_handle(fh_0);

  EXPECT_EQ(5 , fh_0_he.idx() ) << "Index wrong for first halfedge of face 0";
  EXPECT_EQ(2  , mesh_.to_vertex_handle(fh_0_he).idx() ) << "First halfedge inside face 0 pointing to wrong vertex";

  next = mesh_.next_halfedge_handle(fh_0_he);
  EXPECT_EQ(3 , next.idx() ) << "Index wrong for second halfedge inside face 0 ";
  EXPECT_EQ(1 , mesh_.to_vertex_handle(next).idx() ) << "second halfedge inside face 0 pointing to wrong vertex ";

  next = mesh_.next_halfedge_handle(next);
  EXPECT_EQ(0 , next.idx() ) << "Index wrong for third halfedge inside face 0 ";
  EXPECT_EQ(0 , mesh_.to_vertex_handle(next).idx() ) << "Third halfedge inside face 0 pointing to wrong vertex ";

  // Get the first halfedge of face 1
  fh_1_he = mesh_.halfedge_handle(fh_1);

  EXPECT_EQ(1 , fh_1_he.idx() ) << "Index wrong for first halfedge of face 1";
  EXPECT_EQ(1  , mesh_.to_vertex_handle(fh_1_he).idx() ) << "First halfedge inside face 1 pointing to wrong vertex";

  next = mesh_.next_halfedge_handle(fh_1_he);
  EXPECT_EQ(2 , next.idx() ) << "Index wrong for second halfedge inside face 1 ";
  EXPECT_EQ(2 , mesh_.to_vertex_handle(next).idx() ) << "second halfedge inside face 1 pointing to wrong vertex ";

  next = mesh_.next_halfedge_handle(next);
  EXPECT_EQ(4 , next.idx() ) << "Index wrong for third halfedge inside face 1 ";
  EXPECT_EQ(0 , mesh_.to_vertex_handle(next).idx() ) << "Third halfedge inside face 1 pointing to wrong vertex ";


  // Vertex 0 outgoing
  voh_it = mesh_.voh_begin(mesh_.vertex_handle(0));
  EXPECT_EQ(1 , voh_it.handle().idx() ) << "Index wrong for first outgoing halfedge of vertex 0";
  ++voh_it;
  EXPECT_EQ(5 , voh_it.handle().idx() ) << "Index wrong for second outgoing halfedge of vertex 0";
  ++voh_it;
  EXPECT_EQ(1 , voh_it.handle().idx() ) << "Index wrong for third(one lap) outgoing halfedge of vertex 0";

  // Vertex 1 outgoing
  voh_it = mesh_.voh_begin(mesh_.vertex_handle(1));
  EXPECT_EQ(0 , voh_it.handle().idx() ) << "Index wrong for first outgoing halfedge of vertex 1";
  ++voh_it;
  EXPECT_EQ(2 , voh_it.handle().idx() ) << "Index wrong for second outgoing halfedge of vertex 1";
  ++voh_it;
  EXPECT_EQ(0 , voh_it.handle().idx() ) << "Index wrong for third(one lap) outgoing halfedge of vertex 1";

  // Vertex 2 outgoing
  voh_it = mesh_.voh_begin(mesh_.vertex_handle(2));
  EXPECT_EQ(3 , voh_it.handle().idx() ) << "Index wrong for first outgoing halfedge of vertex 2";
  ++voh_it;
  EXPECT_EQ(4 , voh_it.handle().idx() ) << "Index wrong for second outgoing halfedge of vertex 2";
  ++voh_it;
  EXPECT_EQ(3 , voh_it.handle().idx() ) << "Index wrong for third(one lap) outgoing halfedge of vertex 2";

  EXPECT_FALSE( mesh_.is_collapse_ok(mesh_.halfedge_handle(0)) ) << "Collapse should be not ok for halfedge 0";
  EXPECT_FALSE( mesh_.is_collapse_ok(mesh_.halfedge_handle(1)) ) << "Collapse should be not ok for halfedge 1";
  EXPECT_FALSE( mesh_.is_collapse_ok(mesh_.halfedge_handle(2)) ) << "Collapse should be not ok for halfedge 2";
  EXPECT_FALSE( mesh_.is_collapse_ok(mesh_.halfedge_handle(3)) ) << "Collapse should be not ok for halfedge 3";
  EXPECT_FALSE( mesh_.is_collapse_ok(mesh_.halfedge_handle(4)) ) << "Collapse should be not ok for halfedge 4";
  EXPECT_FALSE( mesh_.is_collapse_ok(mesh_.halfedge_handle(5)) ) << "Collapse should be not ok for halfedge 5";
}

/*
 * Collapsing a tetrahedron
 */
TEST_F(OpenMeshCollapse, CollapseComplex) {

  mesh_.clear();

  // Add some vertices
Jan Möbius's avatar
Jan Möbius committed
352
  Mesh::VertexHandle vhandle[5];
Jan Möbius's avatar
Jan Möbius committed
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433

  // Setup a pyramid
  vhandle[0] = mesh_.add_vertex(Mesh::Point(0 , 0, 0));
  vhandle[1] = mesh_.add_vertex(Mesh::Point(1 , 0, 0));
  vhandle[2] = mesh_.add_vertex(Mesh::Point(0 ,-1, 0));
  vhandle[3] = mesh_.add_vertex(Mesh::Point(0 , 1, 0));
  vhandle[4] = mesh_.add_vertex(Mesh::Point(-1, 0, 0));

  // Add six faces
  std::vector<Mesh::VertexHandle> face_vhandles;

  face_vhandles.push_back(vhandle[0]);
  face_vhandles.push_back(vhandle[4]);
  face_vhandles.push_back(vhandle[2]);
  mesh_.add_face(face_vhandles);

  face_vhandles.clear();

  face_vhandles.push_back(vhandle[3]);
  face_vhandles.push_back(vhandle[4]);
  face_vhandles.push_back(vhandle[0]);
  mesh_.add_face(face_vhandles);

  face_vhandles.clear();

  face_vhandles.push_back(vhandle[2]);
  face_vhandles.push_back(vhandle[4]);
  face_vhandles.push_back(vhandle[3]);
  mesh_.add_face(face_vhandles);

  face_vhandles.clear();

  face_vhandles.push_back(vhandle[2]);
  face_vhandles.push_back(vhandle[1]);
  face_vhandles.push_back(vhandle[0]);
  mesh_.add_face(face_vhandles);

  face_vhandles.clear();

  face_vhandles.push_back(vhandle[0]);
  face_vhandles.push_back(vhandle[1]);
  face_vhandles.push_back(vhandle[3]);
  mesh_.add_face(face_vhandles);

  mesh_.request_vertex_status();
  mesh_.request_edge_status();
  mesh_.request_face_status();


  // =============================================
  // Collapse halfedge from 0 to 4
  // =============================================

  Mesh::HalfedgeHandle heh_collapse1 = mesh_.halfedge_handle(0);

  EXPECT_EQ(4, mesh_.to_vertex_handle(heh_collapse1).idx()   ) << "To   vertex of collapse halfedge 1 is wrong";
  EXPECT_EQ(0, mesh_.from_vertex_handle(heh_collapse1).idx() ) << "from vertex of collapse halfedge 1 is wrong";

  EXPECT_TRUE( mesh_.is_collapse_ok(heh_collapse1) ) << "Collapse not ok for collapse first halfedge (0)";
  mesh_.collapse(heh_collapse1);

  Mesh::HalfedgeHandle heh_collapse2 = mesh_.halfedge_handle(2);

  EXPECT_EQ(2, mesh_.to_vertex_handle(heh_collapse2).idx()   ) << "To   vertex of collapse halfedge 2 is wrong";
  EXPECT_EQ(4, mesh_.from_vertex_handle(heh_collapse2).idx() ) << "from vertex of collapse halfedge 2 is wrong";

  EXPECT_TRUE( mesh_.is_collapse_ok(heh_collapse2) ) << "Collapse not ok for collapse second halfedge (2)";
  mesh_.collapse(heh_collapse2);

  Mesh::HalfedgeHandle heh_collapse3 = mesh_.halfedge_handle(6);

  EXPECT_EQ(2, mesh_.to_vertex_handle(heh_collapse3).idx()   ) << "To   vertex of collapse halfedge 3 is wrong";
  EXPECT_EQ(3, mesh_.from_vertex_handle(heh_collapse3).idx() ) << "from vertex of collapse halfedge 3 is wrong";

  EXPECT_FALSE( mesh_.is_collapse_ok(heh_collapse3) ) << "Collapse not ok for collapse third halfedge (6)";


}


#endif // INCLUDE GUARD