How to get external faces from a vtk file?

Viewed 42

I have a cube made of hexaedrons and I want to get all the external faces (of the hexaedrons, not the "big" cube) but I'm not able to do it. I think the best way is to iterate over all faces and get the ones that don't have adjacent faces, don't know if this is the correct way. My code is in python but answers in both C/C++ and python are welcome

    # Read the source file.
    reader = vtk.vtkXMLUnstructuredGridReader()
    reader.SetFileName(file_name)
    reader.Update()
    
    output = reader.GetOutput()
    points = output.GetPoints()
    
    # get faces
    faces = []
    for i in range(output.GetNumberOfCells()):
        cell = output.GetCell(i)
        #print(type(cell)) hexaedron
        cell_points_ids = []
        for face_index in range(cell.GetNumberOfFaces()):
            a=vtk.reference([0,0,0,0])
            cell.GetFaceToAdjacentFaces(face_index,a)

Update

gf = vtk.vtkGeometryFilter()
gf.SetInputConnection(reader.GetOutputPort())
gf.Update()
polydata_output = gf.GetOutput()

print(polydata_output.GetNumberOfCells()) # 291024
print(output.GetNumberOfCells()) #48504 

Update 2

sc = vtk.vtkStaticCleanUnstructuredGrid()
sc.Update()
sc.SetInputData(output)

sc.Update()
gf = vtk.vtkGeometryFilter()
sc_output = sc.GetOutput()
gf.SetInputData(sc_output)
#gf.SetInputConnection(reader.GetOutputPort())
gf.Update()
polydata_output = gf.GetOutput()

print(polydata_output.GetNumberOfCells()) # 19120
print(output.GetNumberOfCells()) #48504 

cube inside

1 Answers

The vtkGeometryFilter will extract All 2D faces that are used by only one 3D cell. (I pointed the native Cxx API doc but python API is quite the same)

Related