pythonnumpymeshopen3d

how to make an open stl file watertight


Newbie here!

I have an STL file which is not watertight and the gap is quite big to repair with the close vertex of the trimesh.

I tried with open3d by following this but I have the following error: "ValueError: vector too long"..

Is there any way to make the mesh watertight? I need to calculate the CoM and Inertia matrix but the values would not be correct if my mesh is not watertight/a closed surface.

For the open3d, firstly I uploaded the stl file, I converted it to numpy and then I used the following code:

        pcd = o3d.geometry.PointCloud()
        pcd.points = o3d.utility.Vector3dVector(DataNP)
        o3d.io.write_point_cloud("testinggggg.ply", pcd)
        poisson_mesh = o3d.geometry.TriangleMesh.create_from_point_cloud_poisson(pcd, depth=8, width=0, scale=1.1, linear_fit=False)[0]
        bbox = pcd.get_axis_aligned_bounding_box()
        p_mesh_crop = poisson_mesh.crop(bbox)
        o3d.io.write_triangle_mesh("output_testinggggg.ply", dec_mesh)

Any help is highly appreciated!


Solution

  • I have managed to make the mesh watertight. I will post here in case anyone is having troubles in the future with it.

    My mesh was actually made of 2 smaller meshes, so I had to first merge them together and then use the VTK library to clean the mesh and fill the holes. This made my mesh watertight and I could calculate everything I needed.

    This is the code:

    input1 = vtk.vtkPolyData()
    input2 = vtk.vtkPolyData()
    
    
    input1.DeepCopy(Data1.GetOutput())
    input2.DeepCopy(Data2.GetOutput())
    
    # Append the two meshes 
    appendFilter = vtk.vtkAppendPolyData()
    
    appendFilter.AddInputData(input1)
    appendFilter.AddInputData(input2)
    
    appendFilter.Update()
    
    #  Remove any duplicate points.
    cleanFilter = vtk.vtkCleanPolyData()
    cleanFilter.SetInputConnection(appendFilter.GetOutputPort())
    cleanFilter.Update()
    
    
    # newData = cleanFilter
    
    fill = vtk.vtkFillHolesFilter()
    fill.SetInputConnection(appendFilter.GetOutputPort())   
    fill.SetHoleSize(100)    
    fill.Update()