Forum

Author Topic: Remove polygons from mesh  (Read 3072 times)

apicca

  • Newbie
  • *
  • Posts: 1
    • View Profile
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!

Alexey Pasumansky

  • Agisoft Technical Support
  • Hero Member
  • *****
  • Posts: 15282
    • View Profile
Re: Remove polygons from mesh
« Reply #1 on: April 12, 2024, 01:11:24 PM »
Hello apicca,

To delete the faces from the model you need to make them selected and then call:
Code: [Select]
chunk.model.removeSelection()In your code the assignment model variable seems to be missing, for example, model=chunk.model

To select the polygons you can do the following, according to your code:
Code: [Select]
for face_index in range(len(chunk.model.faces)):
   if face_index in polygons_to_remove:
      chunk.model.faces(face_index.selected) = True
Or just apply face selection directly in loop, where you check, if the face fits threshold or not.
Best regards,
Alexey Pasumansky,
Agisoft LLC