Extract Cell and Point Id's from PolyData (not vtksource)

Hello,

I’ve searched for several days for the answer to this but if I missed an obvious answer then I apologize.

Here is the setup for my issue.

I am reading mesh data from and HDF5 file and assigning it to a vtkPolyData structure through vtkPoints().SetPoint(global id from HDF5, node_arrray[]) and vtkCellArray().InsertNextCell(cell type). I can visualize any mesh that I have loaded so far with no trouble.

Ultimately I want to draw a box around an arbitrary section of the mesh and extract the cell and point id’s from it. I have set up the box and clipped the data with vtkClipPolyData() which works fine and returns the number of cells and points just fine. My issue then comes from trying to determine which cells and points it has selected. I have been using https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/ImplicitDataSetClipping/ as an example which seems like it should work but for every example I find that pulls the id’s it always extracts that info from a vtksource (such as vtkSphereSource) which is then linked to a vtkIdFilter through the GetOutputPort() of the given source.

Is there a way to extract the point ids (that were set in vtkPoints().SetPoint) through a clipping function?

(in the uploaded image the opaque black box is the implicit function I’m using to clip cells from the green mesh)

Thank you

vtkIdFilter should do the trick: it does not care about where its input comes from, it passes the input data set (not even required to be vtkPolyData for that matter) and adds arrays with point and/or cell ids. Note that vtkClipPolyData filter generates scalars so if you pass the ids as field data arrays with specified names then you should be able to pick them up after clipping.

1 Like

Thank you very much for your reply.

My issue was that I was setting my input to the vtkIdFilter as my mesh but I was then trying to feed that mesh to the clipper rather than creating a new output from the filter. I’ll add my code in case anyone else has this issue.

########## Filter for pulling clipped data #################
cellid = vtk.vtkIdFilter()
cellid.SetCellIds(True)
cellid.SetInputData(mesh) # vtkPolyData()
cellid.SetPointIds(False)
cellid.SetIdsArrayName(“Cell_IDs”)
cellid.Update()

pointid = vtk.vtkIdFilter()
pointid.SetInputConnection(cellid.GetOutputPort())
pointid.SetCellIds(False)
pointid.SetPointIds(True)
pointid.SetIdsArrayName(“Point_IDs”)
pointid.Update()
############################################################

update_mesh = pointid.GetOutput() # Feed this to the clipping algorithm not mesh

1 Like

Sorry to have to follow up with a second questions but something doesn’t seem to be right. No matter what I do the point id array is always ‘none’ post clipping. The code is as follows:

########## Filter for pulling clipped data #################
pointid = vtk.vtkIdFilter()
pointid.SetInputData(mesh) # A vtkPolyData() mesh with 20525 points and 10000 cells
pointid.CellIdsOff()
pointid.PointIdsOn()
pointid.SetIdsArrayName(“Point_IDs”)
pointid.Update()

cellid = vtk.vtkIdFilter()
cellid.SetInputConnection(pointid.GetOutputPort())
cellid.CellIdsOn()
cellid.PointIdsOff()
cellid.SetIdsArrayName(“Cell_IDs”)
cellid.Update()
############################################################
update_mesh = cellid.GetOutput()

########## Clipper to Pull Data from Within Cube ###########
clipper = vtk.vtkClipPolyData()
clipper.SetClipFunction(impl_cube) # vtkBox()
clipper.SetInputData(update_mesh)
clipper.InsideOutOn()
clipper.Update()
############################################################
print(“There are %s clipped cells”%(clipper.GetOutput().GetNumberOfCells())) # Returns 1246
print(“There are %s clipped points”%(clipper.GetOutput().GetNumberOfPoints())) # Returns 1253

clipped = clipper.GetOutput()
clipped_cell_ids = clipped.GetCellData().GetArray(‘Cell_IDs’)
clipped_point_ids = clipped.GetPointData().GetArray(‘Point_IDs’)

###################################################################

Why isn’t the point data array getting set or populated?

Thank you.

Ah now I understand, only the point ids are missing. This is for a good reason: clipping produces new points and calculates the associated point data by interpolation. However, ids are integer data and hence cannot be interpolated.

Note BTW that you should be able to generate point ids and cell ids by one instance of vtkIdFilter, you can specify the array names of point ids and cell ids separately.

2 Likes