getting the whole border edges from the mesh in python

483 views Asked by At

I am trying to create a polygon from a mesh. My idea is to get all the border edges and make a polygon.i found somewhat similar answer to mine in this question Get border edges of mesh - in winding order

I tried to implement the algorithm mention there but i am only getting a small triangle as my border edgesMesh

import plyfile

def edges_to_polygon_loop(edges):
    # Initialize an empty polygon loop
    polygon_loop = []
    
    # Pick an unvisited edge segment
    start_edge = edges[0]
    v_start, v_next = start_edge
    polygon_loop.append(v_start)
    polygon_loop.append(v_next)
    
    # Initialize a set to keep track of visited edges
    visited_edges = set()
    visited_edges.add(start_edge)
    
    # Iterate until we get back to the start vertex
    while v_next != v_start:
        # Find the next unvisited edge segment
        for edge in edges:
            if edge not in visited_edges:
                v_i, v_j = edge
                if v_i == v_next:
                    polygon_loop.append(v_j)
                    v_next = v_j
                    visited_edges.add(edge)
                    break
                elif v_j == v_next:
                    polygon_loop.append(v_i)
                    v_next = v_i
                    visited_edges.add(edge)
                    break
    
    # Return the polygon loop
    return polygon_loop





with open('M:\\volume.ply', 'rb') as f:
    # Read the .ply file
    ply = plyfile.PlyData.read(f)
    
    # Extract the vertex and face data
    vertices = ply['vertex']
    faces = ply['face']
    
    # Initialize an empty list of edges
    edges = []
    
    # Iterate over the faces
    for face in faces:
        # Get the indices of the vertices in the face
        vertex_indices = face['vertex_indices']
        
        # Add the edges to the list
        for i in range(len(vertex_indices)):
            # Get the indices of the endpoints of the edge
            v1_index = vertex_indices[i]
            v2_index = vertex_indices[(i + 1) % len(vertex_indices)]
            
            # Get the coordinates of the endpoints
            v1 = (vertices[v1_index]['x'], vertices[v1_index]['y'], vertices[v1_index]['z'])
            v2 = (vertices[v2_index]['x'], vertices[v2_index]['y'], vertices[v2_index]['z'])
            
            # Add the edge to the list
            edges.append((v1, v2))
    
    # Print the edges

polygon = edges_to_polygon_loop(edges)
1

There are 1 answers

0
ardget On

You would need to eliminate inner edges first, then you can compose the outer loop.

    :

    # Extract the vertex and face data
    vertices = ply['vertex']
    faces = ply['face']
    
    # Initialize an empty list of indices-pairs
    pairs = []

    # Iterate over the faces
    for face in faces:
        # Get the indices of the vertices in the face
        vertex_indices = face['vertex_indices']
        
        # Add the edge-indices to the list
        for i in range(len(vertex_indices)):
            # Get the indices of the endpoints of the edge
            v1_index = vertex_indices[i]
            v2_index = vertex_indices[(i + 1) % len(vertex_indices)]

            if ((v2_index, v1_index) not in pairs):
                pairs.append((v1_index, v2_index))
            else: 
                # Remove inner edge
                pairs.remove((v2_index, v1_index))

    # Initialize an empty list of edges
    edges = []

    # Add the edges to the list
    for (v1_index, v2_index) in pairs:
            
        # Get the coordinates of the endpoints
        v1 = (vertices[v1_index]['x'], vertices[v1_index]['y'], vertices[v1_index]['z'])
        v2 = (vertices[v2_index]['x'], vertices[v2_index]['y'], vertices[v2_index]['z'])
            
        # Add the edge to the list
        edges.append((v1, v2))
    
    # Print the edges

polygon = edges_to_polygon_loop(edges)