test_trimesh_collapse.py 17.6 KB
Newer Older
1 2 3
import unittest
import openmesh

4 5
import numpy as np

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
class Collapse(unittest.TestCase):

    def setUp(self):
        self.mesh = openmesh.TriMesh()
        self.vhandle = []
    
    def test_collapse_quad_with_center(self):
        
        # 0--------1
        # |\      /|
        # | \    / |
        # |  \  /  |
        # |    2   |
        # |  /  \  |
        # | /    \ |
        # 3--------4

        # Add some vertices
24 25 26 27 28
        self.vhandle.append(self.mesh.add_vertex(np.array([0, 0, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([2, 0, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([1, 1, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([0, 2, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([2, 2, 0])))
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

        # Add four faces
        face_vhandles = []

        face_vhandles.append(self.vhandle[0])
        face_vhandles.append(self.vhandle[2])
        face_vhandles.append(self.vhandle[1])
        self.mesh.add_face(face_vhandles)

        face_vhandles = []

        face_vhandles.append(self.vhandle[0])
        face_vhandles.append(self.vhandle[3])
        face_vhandles.append(self.vhandle[2])
        self.mesh.add_face(face_vhandles)

        face_vhandles = []

        face_vhandles.append(self.vhandle[4])
        face_vhandles.append(self.vhandle[2])
        face_vhandles.append(self.vhandle[3])
        self.mesh.add_face(face_vhandles)

        face_vhandles = []

        face_vhandles.append(self.vhandle[4])
        face_vhandles.append(self.vhandle[1])
        face_vhandles.append(self.vhandle[2])
        self.mesh.add_face(face_vhandles)

        # Get the halfedge
        v2v1 = self.mesh.find_halfedge(self.vhandle[2], self.vhandle[1])

        self.assertTrue(v2v1.is_valid())
        self.assertTrue(self.mesh.is_collapse_ok(v2v1))

        # Execute it as a crash test
        self.mesh.collapse(v2v1)

    def test_collapse_tetrahedron_complex(self):
        # Add some vertices
70 71 72 73
        self.vhandle.append(self.mesh.add_vertex(np.array([0, 0, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([0, 1, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([1, 1, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([1, 0, 0])))
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
        
        # Add four faces
        face_vhandles = []
        
        face_vhandles.append(self.vhandle[0])
        face_vhandles.append(self.vhandle[1])
        face_vhandles.append(self.vhandle[2])
        self.mesh.add_face(face_vhandles)
        
        face_vhandles = []
        
        face_vhandles.append(self.vhandle[0])
        face_vhandles.append(self.vhandle[2])
        face_vhandles.append(self.vhandle[3])
        self.mesh.add_face(face_vhandles)
        
        face_vhandles = []
        
        face_vhandles.append(self.vhandle[2])
        face_vhandles.append(self.vhandle[1])
        face_vhandles.append(self.vhandle[3])
        self.mesh.add_face(face_vhandles)
        
        face_vhandles = []
        
        face_vhandles.append(self.vhandle[3])
        face_vhandles.append(self.vhandle[1])
        face_vhandles.append(self.vhandle[0])
        self.mesh.add_face(face_vhandles)
        
        v0v1 = self.mesh.halfedge_handle(0)
        v1v0 = self.mesh.opposite_halfedge_handle(v0v1)

        v1vL = self.mesh.next_halfedge_handle(v0v1)
        vLv1 = self.mesh.opposite_halfedge_handle(v1vL)
        vLv0 = self.mesh.next_halfedge_handle(v1vL)
        v0vL = self.mesh.opposite_halfedge_handle(vLv0)

        vLvR = self.mesh.next_halfedge_handle(v0vL)
        vRvL = self.mesh.opposite_halfedge_handle(vLvR)

        v0vR = self.mesh.next_halfedge_handle(v1v0)
        vRv0 = self.mesh.opposite_halfedge_handle(v0vR)
        vRv1 = self.mesh.next_halfedge_handle(v0vR)
        v1vR = self.mesh.opposite_halfedge_handle(vRv1)

        v0 = self.mesh.from_vertex_handle(v0v1)
        v1 = self.mesh.to_vertex_handle(v0v1)
        vL = self.mesh.to_vertex_handle(self.mesh.next_halfedge_handle(v0v1))
        vR = self.mesh.to_vertex_handle(self.mesh.next_halfedge_handle(v1v0))

        # ===================================================================
        # Check preconditions
        # ===================================================================

        self.assertTrue(self.mesh.is_collapse_ok(v0v1))
        self.assertTrue(self.mesh.is_collapse_ok(v1v0))

        # Test the Vertex indices
        self.assertEqual(v0.idx(), 0)
        self.assertEqual(v1.idx(), 1)
        self.assertEqual(vL.idx(), 2)
        self.assertEqual(vR.idx(), 3)

        # Check the halfedges
        self.assertEqual(v0v1.idx(), 0)
        self.assertEqual(v1v0.idx(), 1)
        
        self.assertEqual(v1vL.idx(), 2)
        self.assertEqual(vLv1.idx(), 3)
        self.assertEqual(vLv0.idx(), 4)
        self.assertEqual(v0vL.idx(), 5)
        
        self.assertEqual(vLvR.idx(), 6)
        self.assertEqual(vRvL.idx(), 7)
        
        self.assertEqual(vRv0.idx(), 8)
        self.assertEqual(v0vR.idx(), 9)
        
        self.assertEqual(v1vR.idx(), 10)
        self.assertEqual(vRv1.idx(), 11)

        # ===================================================================
        # Execute collapse
        # ===================================================================

        self.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

        self.assertEqual(self.mesh.n_faces(), 4)

        # Check if the right vertices got deleted
181 182 183 184
        self.assertTrue(self.mesh.is_deleted(self.mesh.face_handle(0)))
        self.assertFalse(self.mesh.is_deleted(self.mesh.face_handle(1)))
        self.assertFalse(self.mesh.is_deleted(self.mesh.face_handle(2)))
        self.assertTrue(self.mesh.is_deleted(self.mesh.face_handle(3)))
185 186 187 188 189 190 191

        # Check the vertices of the two remaining faces
        fh_1 = self.mesh.face_handle(1)
        fh_2 = self.mesh.face_handle(2)

        fv_it = self.mesh.fv(fh_1)

192 193 194
        self.assertEqual(next(fv_it).idx(), 1)
        self.assertEqual(next(fv_it).idx(), 2)
        self.assertEqual(next(fv_it).idx(), 3)
195 196 197

        fv_it = self.mesh.fv(fh_2)

198 199 200
        self.assertEqual(next(fv_it).idx(), 2)
        self.assertEqual(next(fv_it).idx(), 1)
        self.assertEqual(next(fv_it).idx(), 3)
201 202 203 204 205 206 207

        # Get the first halfedge of face 1
        fh_1_he = self.mesh.halfedge_handle(fh_1)

        self.assertEqual(fh_1_he.idx(), 11)
        self.assertEqual(self.mesh.to_vertex_handle(fh_1_he).idx(), 1)

208 209 210
        next_he = self.mesh.next_halfedge_handle(fh_1_he)
        self.assertEqual(next_he.idx(), 2)
        self.assertEqual(self.mesh.to_vertex_handle(next_he).idx(), 2)
211

212 213 214
        next_he = self.mesh.next_halfedge_handle(next_he)
        self.assertEqual(next_he.idx(), 6)
        self.assertEqual(self.mesh.to_vertex_handle(next_he).idx(), 3)
215 216 217 218 219 220 221

        # Get the first halfedge of face 2
        fh_2_he = self.mesh.halfedge_handle(fh_2)

        self.assertEqual(fh_2_he.idx(), 7)
        self.assertEqual(self.mesh.to_vertex_handle(fh_2_he).idx(), 2)

222 223 224
        next_he = self.mesh.next_halfedge_handle(fh_2_he)
        self.assertEqual(next_he.idx(), 3)
        self.assertEqual(self.mesh.to_vertex_handle(next_he).idx(), 1)
225

226 227 228
        next_he = self.mesh.next_halfedge_handle(next_he)
        self.assertEqual(next_he.idx(), 10)
        self.assertEqual(self.mesh.to_vertex_handle(next_he).idx(), 3)
229 230 231

        # Vertex 1 outgoing
        voh_it = self.mesh.voh(self.mesh.vertex_handle(1))
232 233 234 235
        self.assertEqual(next(voh_it).idx(), 10)
        self.assertEqual(next(voh_it).idx(), 2)
        with self.assertRaises(StopIteration):
            next(voh_it)
236 237 238

        # Vertex 2 outgoing
        voh_it = self.mesh.voh(self.mesh.vertex_handle(2))
239 240 241 242
        self.assertEqual(next(voh_it).idx(), 3)
        self.assertEqual(next(voh_it).idx(), 6)
        with self.assertRaises(StopIteration):
            next(voh_it)
243 244 245

        # Vertex 2 outgoing
        voh_it = self.mesh.voh(self.mesh.vertex_handle(3))
246 247 248 249
        self.assertEqual(next(voh_it).idx(), 11)
        self.assertEqual(next(voh_it).idx(), 7)
        with self.assertRaises(StopIteration):
            next(voh_it)
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

        # ===================================================================
        # Cleanup
        # ===================================================================
        self.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

        self.assertEqual(self.mesh.n_faces(), 2)

        # Check the vertices of the two remaining faces
        fh_0 = self.mesh.face_handle(0)
        fh_1 = self.mesh.face_handle(1)

        fv_it = self.mesh.fv(fh_0)

280 281 282
        self.assertEqual(next(fv_it).idx(), 2)
        self.assertEqual(next(fv_it).idx(), 1)
        self.assertEqual(next(fv_it).idx(), 0)
283 284 285

        fv_it = self.mesh.fv(fh_1)

286 287 288
        self.assertEqual(next(fv_it).idx(), 1)
        self.assertEqual(next(fv_it).idx(), 2)
        self.assertEqual(next(fv_it).idx(), 0)
289 290 291 292 293 294 295

        # Get the first halfedge of face 1
        fh_0_he = self.mesh.halfedge_handle(fh_0)

        self.assertEqual(fh_0_he.idx(), 5)
        self.assertEqual(self.mesh.to_vertex_handle(fh_0_he).idx(), 2)

296 297 298
        next_he = self.mesh.next_halfedge_handle(fh_0_he)
        self.assertEqual(next_he.idx(), 3)
        self.assertEqual(self.mesh.to_vertex_handle(next_he).idx(), 1)
299

300 301 302
        next_he = self.mesh.next_halfedge_handle(next_he)
        self.assertEqual(next_he.idx(), 0)
        self.assertEqual(self.mesh.to_vertex_handle(next_he).idx(), 0)
303 304 305 306 307 308 309

        # Get the first halfedge of face 1
        fh_1_he = self.mesh.halfedge_handle(fh_1)

        self.assertEqual(fh_1_he.idx(), 1)
        self.assertEqual(self.mesh.to_vertex_handle(fh_1_he).idx(), 1)

310 311 312
        next_he = self.mesh.next_halfedge_handle(fh_1_he)
        self.assertEqual(next_he.idx(), 2)
        self.assertEqual(self.mesh.to_vertex_handle(next_he).idx(), 2)
313

314 315 316
        next_he = self.mesh.next_halfedge_handle(next_he)
        self.assertEqual(next_he.idx(), 4)
        self.assertEqual(self.mesh.to_vertex_handle(next_he).idx(), 0)
317 318 319

        # Vertex 0 outgoing
        voh_it = self.mesh.voh(self.mesh.vertex_handle(0))
320 321 322 323
        self.assertEqual(next(voh_it).idx(), 1)
        self.assertEqual(next(voh_it).idx(), 5)
        with self.assertRaises(StopIteration):
            next(voh_it)
324 325 326

        # Vertex 1 outgoing
        voh_it = self.mesh.voh(self.mesh.vertex_handle(1))
327 328 329 330
        self.assertEqual(next(voh_it).idx(), 0)
        self.assertEqual(next(voh_it).idx(), 2)
        with self.assertRaises(StopIteration):
            next(voh_it)
331 332 333

        # Vertex 2 outgoing
        voh_it = self.mesh.voh(self.mesh.vertex_handle(2))
334 335 336 337
        self.assertEqual(next(voh_it).idx(), 3)
        self.assertEqual(next(voh_it).idx(), 4)
        with self.assertRaises(StopIteration):
            next(voh_it)
338 339 340 341 342 343 344 345 346 347

        self.assertFalse(self.mesh.is_collapse_ok(self.mesh.halfedge_handle(0)))
        self.assertFalse(self.mesh.is_collapse_ok(self.mesh.halfedge_handle(1)))
        self.assertFalse(self.mesh.is_collapse_ok(self.mesh.halfedge_handle(2)))
        self.assertFalse(self.mesh.is_collapse_ok(self.mesh.halfedge_handle(3)))
        self.assertFalse(self.mesh.is_collapse_ok(self.mesh.halfedge_handle(4)))
        self.assertFalse(self.mesh.is_collapse_ok(self.mesh.halfedge_handle(5)))

    def test_collapse_tetrahedron(self):
        # Add some vertices
348 349 350 351 352
        self.vhandle.append(self.mesh.add_vertex(np.array([ 0,  0, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([ 1,  0, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([ 0, -1, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([ 0,  1, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([-1,  0, 0])))
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
        
        # Add six faces
        face_vhandles = []
        
        face_vhandles.append(self.vhandle[0])
        face_vhandles.append(self.vhandle[4])
        face_vhandles.append(self.vhandle[2])
        self.mesh.add_face(face_vhandles)

        face_vhandles = []

        face_vhandles.append(self.vhandle[3])
        face_vhandles.append(self.vhandle[4])
        face_vhandles.append(self.vhandle[0])
        self.mesh.add_face(face_vhandles)

        face_vhandles = []

        face_vhandles.append(self.vhandle[2])
        face_vhandles.append(self.vhandle[4])
        face_vhandles.append(self.vhandle[3])
        self.mesh.add_face(face_vhandles)

        face_vhandles = []

        face_vhandles.append(self.vhandle[2])
        face_vhandles.append(self.vhandle[1])
        face_vhandles.append(self.vhandle[0])
        self.mesh.add_face(face_vhandles)

        face_vhandles = []

        face_vhandles.append(self.vhandle[0])
        face_vhandles.append(self.vhandle[1])
        face_vhandles.append(self.vhandle[3])
        self.mesh.add_face(face_vhandles)

        # =============================================
        # Collapse halfedge from 0 to 4
        # =============================================

        heh_collapse1 = self.mesh.halfedge_handle(0)

        self.assertEqual(self.mesh.to_vertex_handle(heh_collapse1).idx(), 4)
        self.assertEqual(self.mesh.from_vertex_handle(heh_collapse1).idx(), 0)

        self.assertTrue(self.mesh.is_collapse_ok(heh_collapse1))
        self.mesh.collapse(heh_collapse1)

        heh_collapse2 = self.mesh.halfedge_handle(2)

        self.assertEqual(self.mesh.to_vertex_handle(heh_collapse2).idx(), 2)
        self.assertEqual(self.mesh.from_vertex_handle(heh_collapse2).idx(), 4)

        self.assertTrue(self.mesh.is_collapse_ok(heh_collapse2))
        self.mesh.collapse(heh_collapse2)

        heh_collapse3 = self.mesh.halfedge_handle(6)

        self.assertEqual(self.mesh.to_vertex_handle(heh_collapse3).idx(), 2)
        self.assertEqual(self.mesh.from_vertex_handle(heh_collapse3).idx(), 3)

        self.assertFalse(self.mesh.is_collapse_ok(heh_collapse3))

    def test_large_collapse_halfedge(self):
        # Add some vertices
419 420 421 422 423 424 425
        self.vhandle.append(self.mesh.add_vertex(np.array([ 0,  1, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([ 1,  0, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([ 2,  1, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([ 0, -1, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([ 2, -1, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([-1,  0, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([ 3,  0, 0])))
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
        
        # Add six faces
        face_vhandles = []
        
        face_vhandles.append(self.vhandle[0])
        face_vhandles.append(self.vhandle[5])
        face_vhandles.append(self.vhandle[1])
        self.mesh.add_face(face_vhandles)

        face_vhandles = []

        face_vhandles.append(self.vhandle[1])
        face_vhandles.append(self.vhandle[5])
        face_vhandles.append(self.vhandle[3])
        self.mesh.add_face(face_vhandles)

        face_vhandles = []

        face_vhandles.append(self.vhandle[0])
        face_vhandles.append(self.vhandle[1])
        face_vhandles.append(self.vhandle[2])
        self.mesh.add_face(face_vhandles)

        face_vhandles = []

        face_vhandles.append(self.vhandle[1])
        face_vhandles.append(self.vhandle[3])
        face_vhandles.append(self.vhandle[4])
        self.mesh.add_face(face_vhandles)

        face_vhandles = []

        face_vhandles.append(self.vhandle[2])
        face_vhandles.append(self.vhandle[1])
        face_vhandles.append(self.vhandle[4])
        self.mesh.add_face(face_vhandles)

        face_vhandles = []

        face_vhandles.append(self.vhandle[2])
        face_vhandles.append(self.vhandle[4])
        face_vhandles.append(self.vhandle[6])
        self.mesh.add_face(face_vhandles)

        # Test setup:
        #    0 ==== 2
        #   / \    /|\
        #  /   \  / | \
        # 5 --- 1   |  6
        #  \   /  \ | /
        #   \ /    \|/
        #    3 ==== 4

        # =============================================
        # Collapse halfedge from 1 to 4
        # =============================================

        heh_collapse = openmesh.HalfedgeHandle()

        for he in self.mesh.halfedges():
            if self.mesh.from_vertex_handle(he).idx() == 1 and self.mesh.to_vertex_handle(he).idx() == 4:
                heh_collapse = he

        # Check our halfedge
        self.assertEqual(self.mesh.to_vertex_handle(heh_collapse).idx(), 4)
        self.assertEqual(self.mesh.from_vertex_handle(heh_collapse).idx(), 1)
        self.assertTrue(self.mesh.is_collapse_ok(heh_collapse))

        # Remember the end vertices
        vh_from = self.mesh.from_vertex_handle(heh_collapse)
        vh_to = self.mesh.to_vertex_handle(heh_collapse)

        # Collapse it
        self.mesh.collapse(heh_collapse)

501 502
        self.assertTrue(self.mesh.is_deleted(vh_from))
        self.assertFalse(self.mesh.is_deleted(vh_to))
503 504 505 506 507


if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(Collapse)
    unittest.TextTestRunner(verbosity=2).run(suite)