test_trimesh_collapse.py 18.2 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 70 71 72 73

        # 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)

        self.mesh.request_vertex_status()
        self.mesh.request_edge_status()
        self.mesh.request_face_status()

        # 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
74 75 76 77
        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])))
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
        
        # 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)
        
        self.mesh.request_vertex_status()
        self.mesh.request_edge_status()
        self.mesh.request_face_status()
        
        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
        self.assertTrue(self.mesh.status(self.mesh.face_handle(0)).deleted())
        self.assertFalse(self.mesh.status(self.mesh.face_handle(1)).deleted())
        self.assertFalse(self.mesh.status(self.mesh.face_handle(2)).deleted())
        self.assertTrue(self.mesh.status(self.mesh.face_handle(3)).deleted())

        # 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)

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

        fv_it = self.mesh.fv(fh_2)

206 207 208
        self.assertEqual(next(fv_it).idx(), 2)
        self.assertEqual(next(fv_it).idx(), 1)
        self.assertEqual(next(fv_it).idx(), 3)
209 210 211 212 213 214 215

        # 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)

216 217 218
        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)
219

220 221 222
        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)
223 224 225 226 227 228 229

        # 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)

230 231 232
        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)
233

234 235 236
        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)
237 238 239

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

        # Vertex 2 outgoing
        voh_it = self.mesh.voh(self.mesh.vertex_handle(2))
247 248 249 250
        self.assertEqual(next(voh_it).idx(), 3)
        self.assertEqual(next(voh_it).idx(), 6)
        with self.assertRaises(StopIteration):
            next(voh_it)
251 252 253

        # Vertex 2 outgoing
        voh_it = self.mesh.voh(self.mesh.vertex_handle(3))
254 255 256 257
        self.assertEqual(next(voh_it).idx(), 11)
        self.assertEqual(next(voh_it).idx(), 7)
        with self.assertRaises(StopIteration):
            next(voh_it)
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

        # ===================================================================
        # 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)

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

        fv_it = self.mesh.fv(fh_1)

294 295 296
        self.assertEqual(next(fv_it).idx(), 1)
        self.assertEqual(next(fv_it).idx(), 2)
        self.assertEqual(next(fv_it).idx(), 0)
297 298 299 300 301 302 303

        # 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)

304 305 306
        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)
307

308 309 310
        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)
311 312 313 314 315 316 317

        # 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)

318 319 320
        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)
321

322 323 324
        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)
325 326 327

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

        # Vertex 1 outgoing
        voh_it = self.mesh.voh(self.mesh.vertex_handle(1))
335 336 337 338
        self.assertEqual(next(voh_it).idx(), 0)
        self.assertEqual(next(voh_it).idx(), 2)
        with self.assertRaises(StopIteration):
            next(voh_it)
339 340 341

        # Vertex 2 outgoing
        voh_it = self.mesh.voh(self.mesh.vertex_handle(2))
342 343 344 345
        self.assertEqual(next(voh_it).idx(), 3)
        self.assertEqual(next(voh_it).idx(), 4)
        with self.assertRaises(StopIteration):
            next(voh_it)
346 347 348 349 350 351 352 353 354 355

        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
356 357 358 359 360
        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])))
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
        
        # 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)

        self.mesh.request_vertex_status()
        self.mesh.request_edge_status()
        self.mesh.request_face_status()

        # =============================================
        # 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
431 432 433 434 435 436 437
        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])))
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 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
        
        # 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

        # Request the status bits
        self.mesh.request_vertex_status()
        self.mesh.request_edge_status()
        self.mesh.request_face_status()

        # =============================================
        # 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)

        self.assertTrue(self.mesh.status(vh_from).deleted())
        self.assertFalse(self.mesh.status(vh_to).deleted())


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