Given a set of triangle vertices and faces, separate objects and form separate meshes

前端 未结 1 1846
无人共我
无人共我 2021-01-21 16:54

Edit: I have written a more succinct version of this question here but I am keeping this post because it is a full explanation.

Given a 3D numpy array, marching cubes ca

相关标签:
1条回答
  • 2021-01-21 17:59

    @Paul Broderson answered this question https://stackoverflow.com/a/61590348/12919727

    I will put it here just for aesthetics:

    #!/usr/bin/env python
    """
    Given a list of triangles, find the connected components.
    
    https://stackoverflow.com/q/61584283/2912349
    """
    import itertools
    import networkx as nx
    
    faces = [[ 2,  1,  0],  [ 0,  3,  2],  [ 1,  4,  0],  [ 0,  4,  3],  [ 5,  1,  2],  [ 3,  5,  2],
             [ 5,  4,  1],  [ 4,  5,  3],  [ 8,  7,  6],  [ 6,  9,  8],  [ 7, 10,  6],  [ 6, 10,  9],
             [11,  7,  8],  [ 9, 11,  8],  [11, 10,  7],  [10, 11,  9]]
    
    #create graph
    edges = []
    for face in faces:
        edges.extend(list(itertools.combinations(face, 2)))
    g = nx.from_edgelist(edges)
    
    # compute connected components and print results
    components = list(nx.algorithms.components.connected_components(g))
    
    for component in components:
        print(component)
    
    # {0, 1, 2, 3, 4, 5}
    # {6, 7, 8, 9, 10, 11}
    
    # separate faces by component
    component_to_faces = dict()
    for component in components:
        component_to_faces[tuple(component)] = [face for face in faces if set(face) <= component] # <= operator tests for subset relation
    
    for component, component_faces in component_to_faces.items():
        print(component, component_faces)
    # (0, 1, 2, 3, 4, 5) [[2, 1, 0], [0, 3, 2], [1, 4, 0], [0, 4, 3], [5, 1, 2], [3, 5, 2], [5, 4, 1], [4, 5, 3]]
    # (6, 7, 8, 9, 10, 11) [[8, 7, 6], [6, 9, 8], [7, 10, 6], [6, 10, 9], [11, 7, 8], [9, 11, 8], [11, 10, 7], [10, 11, 9]] 
    
    0 讨论(0)
提交回复
热议问题