test_trimesh_circulator_halfedge_loop.py 4.23 KB
Newer Older
1 2 3
import unittest
import openmesh

4 5
import numpy as np

6 7 8 9 10 11 12 13
class TrimeshCirculatorHalfedgeLoop(unittest.TestCase):

    def setUp(self):
        self.mesh = openmesh.TriMesh()
        self.vhandle = []
    
    def test_halfedge_loop_with_face(self):
        # Add some vertices
14 15 16 17 18 19
        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([3,  0, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([4,  1, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([2, -1, 0])))
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

        # 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[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[2])
        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[1])
        face_vhandles.append(self.vhandle[5])
        face_vhandles.append(self.vhandle[3])
        self.mesh.add_face(face_vhandles)

        # Test setup:
        #
        # edge x => halfedge x/x+1
        # i.e. edge 0 => halfedge 0/1
        #
        # 0 --4--- 2 ------ 4
        #  \      / \      /
        #   0  0 2   6  2 /
        #    \  /  1  \  /
        #     1 ---8--- 3
        #      \       /
        #       \  3  /
        #        \   /
        #         \ /
        #          5
        
        # Circle around face 1
        hl_it = self.mesh.hl(self.mesh.halfedge_handle(3))

69 70 71 72 73
        self.assertEqual(next(hl_it).idx(), 3)
        self.assertEqual(next(hl_it).idx(), 6)
        self.assertEqual(next(hl_it).idx(), 8)
        with self.assertRaises(StopIteration):
            next(hl_it)
74 75 76

    def test_halfedge_loop_without_face(self):
        # Add some vertices
77 78 79 80 81 82
        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([3,  0, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([4,  1, 0])))
        self.vhandle.append(self.mesh.add_vertex(np.array([2, -1, 0])))
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
        
        # Add three 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[2])
        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[1])
        face_vhandles.append(self.vhandle[5])
        face_vhandles.append(self.vhandle[3])
        self.mesh.add_face(face_vhandles)

        # Test setup:
        #
        # H => hole (no face)
        # fx => face #x
        # edge 0 => halfedge 0/1
        #
        # 0 --4--- 2 -10--- 4
        #  \      / \      /
        #   0 f0 2   6 f2 8
        #    \  /  H  \  /
        #     1 ---16---3
        #      \       /
        #      12 f3 14
        #        \   /
        #         \ /
        #          5

        # Circle around the hole
        hl_it = self.mesh.hl(self.mesh.halfedge_handle(3))

126 127 128 129 130
        self.assertEqual(next(hl_it).idx(), 3)
        self.assertEqual(next(hl_it).idx(), 17)
        self.assertEqual(next(hl_it).idx(), 7)
        with self.assertRaises(StopIteration):
            next(hl_it)
131 132 133 134 135


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