test_indices.py 12.5 KB
Newer Older
1 2 3 4 5 6 7 8
import unittest
import openmesh

import numpy as np

class Python(unittest.TestCase):

    def setUp(self):
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
        self.mesh = openmesh.PolyMesh()
        vh0 = self.mesh.add_vertex(np.array([0, 0, 0]))
        vh1 = self.mesh.add_vertex(np.array([1, 0, 0]))
        vh2 = self.mesh.add_vertex(np.array([1, 1, 0]))
        vh3 = self.mesh.add_vertex(np.array([0, 1, 0]))
        vh4 = self.mesh.add_vertex(np.array([2, 0, 0]))
        vh5 = self.mesh.add_vertex(np.array([3, 0, 0]))
        vh6 = self.mesh.add_vertex(np.array([3, 1, 0]))
        vh7 = self.mesh.add_vertex(np.array([2, 1, 0]))
        vh8 = self.mesh.add_vertex(np.array([4, 0, 0]))
        vh9 = self.mesh.add_vertex(np.array([5, 0, 0]))
        vh10 = self.mesh.add_vertex(np.array([5, 1, 0]))
        self.mesh.add_face(vh0, vh1, vh2, vh3)
        self.mesh.add_face(vh4, vh5, vh6)
        self.mesh.add_face(vh6, vh7, vh4)
        self.mesh.add_face(vh8, vh9, vh10)
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
        # Test setup:
        #  3 === 2   7 === 6        10
        #  |     |   |   / |       / |
        #  |     |   |  /  |      /  |
        #  |     |   | /   |     /   |
        #  0 === 1   4 === 5   8 === 9

    def test_vertex_vertex_indices(self):
        indices1 = self.mesh.vertex_vertex_indices()
        indices2 = self.mesh.vv_indices()
        for h1 in self.mesh.vertices():
            correct = [h2.idx() for h2 in self.mesh.vv(h1)]
            while len(correct) < 3:
                correct.append(-1)
            correct = np.array(correct)
            self.assertTrue(np.array_equal(indices1[h1.idx()], correct))
            self.assertTrue(np.array_equal(indices2[h1.idx()], correct))
        self.assertEqual(indices1.shape, (self.mesh.n_vertices(), 3))
        self.assertEqual(indices2.shape, (self.mesh.n_vertices(), 3))
        
    def test_vertex_face_indices(self):
        indices1 = self.mesh.vertex_face_indices()
        indices2 = self.mesh.vf_indices()
        for h1 in self.mesh.vertices():
            correct = [h2.idx() for h2 in self.mesh.vf(h1)]
            while len(correct) < 2:
                correct.append(-1)
            correct = np.array(correct)
            self.assertTrue(np.array_equal(indices1[h1.idx()], correct))
            self.assertTrue(np.array_equal(indices2[h1.idx()], correct))
        self.assertEqual(indices1.shape, (self.mesh.n_vertices(), 2))
        self.assertEqual(indices2.shape, (self.mesh.n_vertices(), 2))
        
    def test_vertex_edge_indices(self):
        indices1 = self.mesh.vertex_edge_indices()
        indices2 = self.mesh.ve_indices()
        for h1 in self.mesh.vertices():
            correct = [h2.idx() for h2 in self.mesh.ve(h1)]
            while len(correct) < 3:
                correct.append(-1)
            correct = np.array(correct)
            self.assertTrue(np.array_equal(indices1[h1.idx()], correct))
            self.assertTrue(np.array_equal(indices2[h1.idx()], correct))
        self.assertEqual(indices1.shape, (self.mesh.n_vertices(), 3))
        self.assertEqual(indices2.shape, (self.mesh.n_vertices(), 3))
        
    def test_vertex_outgoing_halfedge_indices(self):
        indices1 = self.mesh.vertex_outgoing_halfedge_indices()
        indices2 = self.mesh.voh_indices()
        for h1 in self.mesh.vertices():
            correct = [h2.idx() for h2 in self.mesh.voh(h1)]
            while len(correct) < 3:
                correct.append(-1)
            correct = np.array(correct)
            self.assertTrue(np.array_equal(indices1[h1.idx()], correct))
            self.assertTrue(np.array_equal(indices2[h1.idx()], correct))
        self.assertEqual(indices1.shape, (self.mesh.n_vertices(), 3))
        self.assertEqual(indices2.shape, (self.mesh.n_vertices(), 3))
        
    def test_vertex_incoming_halfedge_indices(self):
        indices1 = self.mesh.vertex_incoming_halfedge_indices()
        indices2 = self.mesh.vih_indices()
        for h1 in self.mesh.vertices():
            correct = [h2.idx() for h2 in self.mesh.vih(h1)]
            while len(correct) < 3:
                correct.append(-1)
            correct = np.array(correct)
            self.assertTrue(np.array_equal(indices1[h1.idx()], correct))
            self.assertTrue(np.array_equal(indices2[h1.idx()], correct))
        self.assertEqual(indices1.shape, (self.mesh.n_vertices(), 3))
        self.assertEqual(indices2.shape, (self.mesh.n_vertices(), 3))
        
98
    def test_face_vertex_indices_trimesh(self):
99 100 101
        self.mesh = openmesh.TriMesh()
        openmesh.read_mesh(self.mesh, 'TestFiles/cube-minimal.obj')
        
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        indices1 = self.mesh.face_vertex_indices()
        indices2 = self.mesh.fv_indices()
        for fh in self.mesh.faces():
            fv_it = self.mesh.fv(fh)
            vh1 = next(fv_it)
            vh2 = next(fv_it)
            vh3 = next(fv_it)
            self.assertEqual(indices1[fh.idx(), 0], vh1.idx())
            self.assertEqual(indices1[fh.idx(), 1], vh2.idx())
            self.assertEqual(indices1[fh.idx(), 2], vh3.idx())
            self.assertEqual(indices2[fh.idx(), 0], vh1.idx())
            self.assertEqual(indices2[fh.idx(), 1], vh2.idx())
            self.assertEqual(indices2[fh.idx(), 2], vh3.idx())
        self.assertEqual(indices1.shape, (self.mesh.n_faces(), 3))
        self.assertEqual(indices2.shape, (self.mesh.n_faces(), 3))

    def test_face_vertex_indices_polymesh(self):
        indices1 = self.mesh.face_vertex_indices()
        indices2 = self.mesh.fv_indices()
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
        for h1 in self.mesh.faces():
            correct = [h2.idx() for h2 in self.mesh.fv(h1)]
            while len(correct) < 4:
                correct.append(-1)
            correct = np.array(correct)
            self.assertTrue(np.array_equal(indices1[h1.idx()], correct))
            self.assertTrue(np.array_equal(indices2[h1.idx()], correct))
        self.assertEqual(indices1.shape, (self.mesh.n_faces(), 4))
        self.assertEqual(indices2.shape, (self.mesh.n_faces(), 4))
        
    def test_face_face_indices(self):
        indices1 = self.mesh.face_face_indices()
        indices2 = self.mesh.ff_indices()
        for h1 in self.mesh.faces():
            correct = [h2.idx() for h2 in self.mesh.ff(h1)]
            while len(correct) < 1:
                correct.append(-1)
            correct = np.array(correct)
            self.assertTrue(np.array_equal(indices1[h1.idx()], correct))
            self.assertTrue(np.array_equal(indices2[h1.idx()], correct))
        self.assertEqual(indices1.shape, (self.mesh.n_faces(), 1))
        self.assertEqual(indices2.shape, (self.mesh.n_faces(), 1))
        
    def test_face_edge_indices(self):
        indices1 = self.mesh.face_edge_indices()
        indices2 = self.mesh.fe_indices()
        for h1 in self.mesh.faces():
            correct = [h2.idx() for h2 in self.mesh.fe(h1)]
            while len(correct) < 4:
                correct.append(-1)
            correct = np.array(correct)
            self.assertTrue(np.array_equal(indices1[h1.idx()], correct))
            self.assertTrue(np.array_equal(indices2[h1.idx()], correct))
        self.assertEqual(indices1.shape, (self.mesh.n_faces(), 4))
        self.assertEqual(indices2.shape, (self.mesh.n_faces(), 4))
        
    def test_face_halfedge_indices(self):
        indices1 = self.mesh.face_halfedge_indices()
        indices2 = self.mesh.fh_indices()
        for h1 in self.mesh.faces():
            correct = [h2.idx() for h2 in self.mesh.fh(h1)]
            while len(correct) < 4:
                correct.append(-1)
            correct = np.array(correct)
            self.assertTrue(np.array_equal(indices1[h1.idx()], correct))
            self.assertTrue(np.array_equal(indices2[h1.idx()], correct))
        self.assertEqual(indices1.shape, (self.mesh.n_faces(), 4))
        self.assertEqual(indices2.shape, (self.mesh.n_faces(), 4))
169 170 171 172 173 174 175 176 177 178 179 180 181 182

    def test_edge_vertex_indices(self):
        indices1 = self.mesh.edge_vertex_indices()
        indices2 = self.mesh.ev_indices()
        for eh in self.mesh.edges():
            heh = self.mesh.halfedge_handle(eh, 0)
            vh1 = self.mesh.from_vertex_handle(heh)
            vh2 = self.mesh.to_vertex_handle(heh)
            self.assertEqual(indices1[eh.idx(), 0], vh1.idx())
            self.assertEqual(indices1[eh.idx(), 1], vh2.idx())
            self.assertEqual(indices2[eh.idx(), 0], vh1.idx())
            self.assertEqual(indices2[eh.idx(), 1], vh2.idx())
        self.assertEqual(indices1.shape, (self.mesh.n_edges(), 2))
        self.assertEqual(indices2.shape, (self.mesh.n_edges(), 2))
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
        
    def test_edge_face_indices(self):
        indices1 = self.mesh.edge_face_indices()
        indices2 = self.mesh.ef_indices()
        for eh in self.mesh.edges():
            heh1 = self.mesh.halfedge_handle(eh, 0)
            heh2 = self.mesh.halfedge_handle(eh, 1)
            fh1 = self.mesh.face_handle(heh1)
            fh2 = self.mesh.face_handle(heh2)
            self.assertEqual(indices1[eh.idx(), 0], fh1.idx())
            self.assertEqual(indices1[eh.idx(), 1], fh2.idx())
            self.assertEqual(indices2[eh.idx(), 0], fh1.idx())
            self.assertEqual(indices2[eh.idx(), 1], fh2.idx())
        self.assertEqual(indices1.shape, (self.mesh.n_edges(), 2))
        self.assertEqual(indices2.shape, (self.mesh.n_edges(), 2))
        
    def test_edge_halfedge_indices(self):
        indices1 = self.mesh.edge_halfedge_indices()
        indices2 = self.mesh.eh_indices()
        for eh in self.mesh.edges():
            heh1 = self.mesh.halfedge_handle(eh, 0)
            heh2 = self.mesh.halfedge_handle(eh, 1)
            self.assertEqual(indices1[eh.idx(), 0], heh1.idx())
            self.assertEqual(indices1[eh.idx(), 1], heh2.idx())
            self.assertEqual(indices2[eh.idx(), 0], heh1.idx())
            self.assertEqual(indices2[eh.idx(), 1], heh2.idx())
        self.assertEqual(indices1.shape, (self.mesh.n_edges(), 2))
        self.assertEqual(indices2.shape, (self.mesh.n_edges(), 2))
211 212 213 214 215 216 217 218 219 220 221 222 223

    def test_halfedge_vertex_indices(self):
        indices1 = self.mesh.halfedge_vertex_indices()
        indices2 = self.mesh.hv_indices()
        for heh in self.mesh.halfedges():
            vh1 = self.mesh.from_vertex_handle(heh)
            vh2 = self.mesh.to_vertex_handle(heh)
            self.assertEqual(indices1[heh.idx(), 0], vh1.idx())
            self.assertEqual(indices1[heh.idx(), 1], vh2.idx())
            self.assertEqual(indices2[heh.idx(), 0], vh1.idx())
            self.assertEqual(indices2[heh.idx(), 1], vh2.idx())
        self.assertEqual(indices1.shape, (self.mesh.n_halfedges(), 2))
        self.assertEqual(indices2.shape, (self.mesh.n_halfedges(), 2))
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
        
    def test_halfedge_to_vertex_indices(self):
        indices1 = self.mesh.halfedge_to_vertex_indices()
        indices2 = self.mesh.htv_indices()
        for heh in self.mesh.halfedges():
            vh = self.mesh.to_vertex_handle(heh)
            self.assertEqual(indices1[heh.idx()], vh.idx())
            self.assertEqual(indices2[heh.idx()], vh.idx())
        self.assertEqual(indices1.shape, (self.mesh.n_halfedges(),))
        self.assertEqual(indices2.shape, (self.mesh.n_halfedges(),))
        
    def test_halfedge_from_vertex_indices(self):
        indices1 = self.mesh.halfedge_from_vertex_indices()
        indices2 = self.mesh.hfv_indices()
        for heh in self.mesh.halfedges():
            vh = self.mesh.from_vertex_handle(heh)
            self.assertEqual(indices1[heh.idx()], vh.idx())
            self.assertEqual(indices2[heh.idx()], vh.idx())
        self.assertEqual(indices1.shape, (self.mesh.n_halfedges(),))
        self.assertEqual(indices2.shape, (self.mesh.n_halfedges(),))
        
    def test_halfedge_face_indices(self):
        indices1 = self.mesh.halfedge_face_indices()
        indices2 = self.mesh.hf_indices()
        for heh in self.mesh.halfedges():
            fh = self.mesh.face_handle(heh)
            self.assertEqual(indices1[heh.idx()], fh.idx())
            self.assertEqual(indices2[heh.idx()], fh.idx())
        self.assertEqual(indices1.shape, (self.mesh.n_halfedges(),))
        self.assertEqual(indices2.shape, (self.mesh.n_halfedges(),))
        
    def test_halfedge_edge_indices(self):
        indices1 = self.mesh.halfedge_edge_indices()
        indices2 = self.mesh.he_indices()
        for heh in self.mesh.halfedges():
            eh = self.mesh.edge_handle(heh)
            self.assertEqual(indices1[heh.idx()], eh.idx())
            self.assertEqual(indices2[heh.idx()], eh.idx())
        self.assertEqual(indices1.shape, (self.mesh.n_halfedges(),))
        self.assertEqual(indices2.shape, (self.mesh.n_halfedges(),))
264 265 266 267 268


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