def test_faces_per_edge(self): import timeit from lace.shapes import create_cube cube = create_cube(np.zeros(3), 1.) self.assertEqual(len(cube.faces_per_edge), len(self.edges_the_hard_way(cube.f))) for e in cube.faces_per_edge: # Check that each of these edges points to a pair of faces that # share two vertices -- that is, faces that share an edge. self.assertEqual( len(set(cube.f[e[0]]).intersection(set(cube.f[e[1]]))), 2) # Now check that changing the faces clears the cache cube.f = cube.f[[1, 2, 3, 4, 6, 7, 8, 9, 10, 11 ]] # remove [0 1 2] & [4 1 0] so edge [0, 1] is gone self.assertEqual(len(cube.faces_per_edge), len(self.edges_the_hard_way(cube.f))) for e in cube.faces_per_edge: self.assertEqual( len(set(cube.f[e[0]]).intersection(set(cube.f[e[1]]))), 2) # And test that caching happens -- without caching, this takes about 5 seconds: self.assertLess( timeit.timeit( 'cube.faces_per_edge', setup= 'from lace.shapes import create_cube; cube = create_cube([0., 0., 0.], 1.)', number=10000), 0.01)
def test_vertex_indices_in_segments(self): from lace.shapes import create_cube cube = create_cube(np.zeros(3), 1.) cube.segm = { # All quads. 'all': np.arange(12), # Quads 2 and 3. 'two_adjacent_sides': [4, 5, 6, 7], # Quad 0. 'lower_base': [0, 1], } np.testing.assert_array_equal(cube.vertex_indices_in_segments(['all']), np.arange(8)) np.testing.assert_array_equal( len(cube.vertex_indices_in_segments(['lower_base'])), 4) np.testing.assert_array_equal( len(cube.vertex_indices_in_segments(['two_adjacent_sides'])), 6) np.testing.assert_array_equal( len( cube.vertex_indices_in_segments( ['lower_base', 'two_adjacent_sides'])), 7) with self.assertRaises(ValueError): cube.vertex_indices_in_segments(['random_segm'])
def vertices_to_edges_matrix_single_axis(self): from lace.shapes import create_cube cube = create_cube(np.zeros(3), 1.) # Assert that it produces the same results as vertices_to_edges_matrix: self.assertEqual( np.vstack( (cube.vertices_to_edges_matrix_single_axis.dot(cube.v[:, ii]) for ii in range(3))).T, cube.vertices_to_edges_matrix.dot(cube.v.ravel()).reshape((-1, 3)))
def test_remove_redundant_verts(self): eps = 1e-15 from lace.shapes import create_cube cube = create_cube(np.zeros(3), 1.) orig_v = cube.v.copy() orig_f = cube.f.copy() cube.f[1:4] = cube.f[1:4] + len(cube.v) cube.v = np.vstack((cube.v, cube.v + eps)) cube.remove_redundant_verts() np.testing.assert_array_equal(cube.v, orig_v) np.testing.assert_array_equal(cube.f, orig_f)
def test_vert_opposites_per_edge(self): from lace.shapes import create_cube cube = create_cube(np.zeros(3), 1.) opposites = cube.vert_opposites_per_edge self.assertIsInstance(opposites, dict) for e, op in opposites.items(): self.assertIsInstance(e, tuple) self.assertEqual(len(e), 2) faces_with_e0 = set(cube.f[np.any(cube.f == e[0], axis=1)].flatten()) faces_with_e1 = set(cube.f[np.any(cube.f == e[1], axis=1)].flatten()) self.assertEqual( faces_with_e0.intersection(faces_with_e1) - set(e), set(op))
def test_vertices_to_edges_matrix(self): import timeit from lace.shapes import create_cube cube = create_cube(np.zeros(3), 1.) calculated_edges = cube.vertices_to_edges_matrix.dot( cube.v.ravel()).reshape((-1, 3)) self.assertEqual(len(calculated_edges), len(cube.vertices_per_edge)) for e, e_ind in zip(calculated_edges, cube.vertices_per_edge): np.testing.assert_array_equal(e, cube.v[e_ind[0]] - cube.v[e_ind[1]]) # And test that caching happens -- without caching, this takes about 5 seconds: self.assertLess( timeit.timeit( 'cube.vertices_to_edges_matrix', setup= 'from lace.shapes import create_cube; cube = create_cube([0., 0., 0.], 1.)', number=10000), 0.01)
def test_vert_connectivity(self): from lace.shapes import create_cube cube = create_cube(np.zeros(3), 1.) connectivity = cube.vert_connectivity self.assertTrue(sp.issparse(connectivity)) self.assertEqual(connectivity.shape, (cube.v.shape[0], cube.v.shape[0])) # Assert that neighbors are marked: for face in cube.f: face = np.asarray(face, dtype=np.uint32) self.assertNotEqual(connectivity[face[0], face[1]], 0) self.assertNotEqual(connectivity[face[1], face[2]], 0) self.assertNotEqual(connectivity[face[2], face[0]], 0) # Assert that non-neighbors are not marked: for v_index in set(cube.f.flatten()): faces_with_this_v = set(cube.f[np.any(cube.f == v_index, axis=1)].flatten()) not_neighbors_of_this_v = set(cube.f.flatten()) - faces_with_this_v for vert in not_neighbors_of_this_v: self.assertEqual(connectivity[int(vert), int(v_index)], 0) self.assertEqual(connectivity[int(v_index), int(vert)], 0)
def test_vertices_per_edge(self): import timeit from lace.shapes import create_cube cube = create_cube(np.zeros(3), 1.) self.assertEqual(len(cube.vertices_per_edge), len(self.edges_the_hard_way(cube.f))) self.assertEqual( set([(min(a, b), max(a, b)) for a, b in cube.vertices_per_edge]), set(self.edges_the_hard_way(cube.f))) # Now check that changing the faces clears the cache cube.f = cube.f[[1, 2, 3, 4, 6, 7, 8, 9, 10, 11 ]] # remove [0 1 2] & [4 1 0] so edge [0, 1] is gone self.assertEqual(len(cube.vertices_per_edge), len(self.edges_the_hard_way(cube.f))) self.assertEqual( set([(min(a, b), max(a, b)) for a, b in cube.vertices_per_edge]), set(self.edges_the_hard_way(cube.f))) # And test that caching happens -- without caching, this takes about 5 seconds: self.assertLess( timeit.timeit( 'cube.vertices_per_edge', setup= 'from lace.shapes import create_cube; cube = create_cube([0., 0., 0.], 1.)', number=10000), 0.01)
def test_clean_segments(self): from lace.shapes import create_cube cube = create_cube(np.zeros(3), 1.) cube.segm = {'all': np.arange(12)} self.assertEqual(cube.clean_segments(['random_segm', 'all']), ['all'])