Forum

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - apicca

Pages: [1]
1
Python and Java API / Remove polygons from mesh
« on: April 12, 2024, 07:58:13 AM »
Hello,

I am trying to remove all polygons that exceed the size threshold selected.  What is the best way to delete the selected faces? Model.cropSelection(), model.removeSelection()??

I keep on getting Error: 'Metashape.Model' object has no attribute 'selectFaces'

Using Agisoft Metashape Professional 1.7.6


def calculate_triangle_area(vertex1, vertex2, vertex3):
    # Calculate the lengths of the sides of the triangle
    a = (vertex1 - vertex2).norm()
    b = (vertex2 - vertex3).norm()
    c = (vertex3 - vertex1).norm()
   
    s = (a + b + c) / 2.0
   
    # Calculate the area using Heron's formula
    area = (s * (s - a) * (s - b) * (s - c)) ** 0.5
   
    return area

# Function to filter polygons based on their size (area)

def filter_polygons(chunk, max_area_threshold):
 
    polygons_to_remove = []
   
    # Iterate over each face (polygon) in the model
    for face_index in range(len(model.faces)):
        # Get the vertices of the current polygon
        vertices = model.faces[face_index].vertices
       
        # Ensure the polygon is a triangle
        if len(vertices) == 3:
            # Extract the vertices' coordinates
            vertex1 = model.vertices[vertices[0]].coord
            vertex2 = model.vertices[vertices[1]].coord
            vertex3 = model.vertices[vertices[2]].coord
           
            # Calculate the area of the triangle
            area = calculate_triangle_area(vertex1, vertex2, vertex3)
           
            # Check if the area exceeds the specified maximum threshold
            if area > max_area_threshold:
                # Add the face index to the list of polygons to remove
                polygons_to_remove.append(face_index)

    # Remove polygons based on the indices collected
    model.eraseFaces(polygons_to_remove)


max_area_threshold = 5  # Specify your maximum area threshold here

for chunk in chunks:
    # Filter polygons for each chunk
    filter_polygons(chunk, max_area_threshold)

doc.removeSelected()


Thank you!

Pages: [1]