def get_weighted_edges( faces: np.ndarray, self_edges: bool = True) -> Tuple[np.ndarray, np.ndarray]: r"""Gets unique edges and degree weights from a triangular mesh. The shorthands used below are: `T`: The number of triangles in the mesh. `E`: The number of unique directed edges in the mesh. Args: faces: A [T, 3] `int32` numpy.ndarray of triangle vertex indices. self_edges: A `bool` flag. If true, then for every vertex 'i' an edge [i, i] is added to edge list. Returns: edges: A [E, 2] `int32` numpy.ndarray of directed edges. weights: A [E] `float32` numpy.ndarray denoting edge weights. The degree of a vertex is the number of edges incident on the vertex, including any self-edges. The weight for an edge $w_{ij}$ connecting vertex $v_i$ and vertex $v_j$ is defined as, $$ w_{ij} = 1.0 / degree(v_i) \sum_{j} w_{ij} = 1 $$ """ edges = mesh_utils.extract_unique_edges_from_triangular_mesh( faces, directed_edges=True).astype(np.int32) if self_edges: vertices = np.expand_dims(np.unique(edges[:, 0]), axis=1) self_edges = np.concatenate((vertices, vertices), axis=1) edges = np.unique(np.concatenate((edges, self_edges), axis=0), axis=0) weights = mesh_utils.get_degree_based_edge_weights(edges, dtype=np.float32) return edges, weights
def _weighted_edges(triangles): """Gets unique edges and degree weights from a triangular mesh.""" edges = mesh_utils.extract_unique_edges_from_triangular_mesh( triangles, directed_edges=True).astype(np.int32) weights = mesh_utils.get_degree_based_edge_weights(edges, dtype=np.float32) return edges, weights
def test_extract_undirected_edges_from_triangular_mesh_preset( self, test_inputs, test_outputs): """Tests that the output contain the expected edges.""" edges = utils.extract_unique_edges_from_triangular_mesh( test_inputs, directed_edges=False) edges.sort(axis=1) # Ensure edge tuple ordered by first vertex. self.assertEqual(sorted(edges.tolist()), test_outputs)
def test_extract_edges_from_triangular_mesh_raised(self, invalid_input, error_msg): """Tests that the shape exceptions are properly raised.""" with self.assertRaisesRegexp(ValueError, error_msg): utils.extract_unique_edges_from_triangular_mesh(invalid_input)
def test_extract_directed_edges_from_triangular_mesh_preset( self, test_inputs, test_outputs): """Tests that the output contain the expected edges.""" edges = utils.extract_unique_edges_from_triangular_mesh( test_inputs, directed_edges=True) self.assertEqual(sorted(edges.tolist()), test_outputs)