Get cell data of a mesh at selected point positions

Hi Andras,
I have data points and want to map it on a brain mesh to get the cell values of brain mesh at data points. how can I do it. any suggestions?

Thank you

Regards,
Saima Safdar

You can use a vtkCellLocator to get closest point to each data point and then get the cell value from cell ID as shown in this example: https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get_scalar_values_at_surface_of_a_model

Hi Andras,
I am using the findcell based on fiducial points to select the cells but there are less number of cells selected as compared to fiducial points. I need all cells for each fiducial and nearby cells as well of the mesh. How would i be able to get this. any idea.

Thank you

Regards,
Saima Safdar

as in the image below you can see only few cells are selected.

image

The script that I’ve linked above works very robustly for me. If you can save your scene in a .mrb file, upload it somewhere, and post the link then I can have a look at it.

Hi Andras,
I uploaded it on googledrive. here is the link.

https://drive.google.com/drive/folders/1WFGnfJRLm65MxF99JzKdnnvGyPLRhO-u?usp=sharing

Regards,
Saima Safdar

Thanks, the scene file helped. The main issue was that the points were not on the surface of the model and the locator’s FindCell method required points to be on the surface (it uses 0 tolerance value for finding a cell).

I’ve updated the example script to find the closest cell and also updated the example to not recreate the selection array if it already exists (so that the code runs correctly even if you run it on a mesh where you’ve used it on already).

image

Hi Andras,
Thank you so much. Andras what should I do if I need to select the other neighbouring cells in between the selected cells.

Thankyou

Regards,
Saima Safdar

See this example: https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/CellPointNeighbors/

Hi Andras,
how can i use this within slicer. what will be the steps.

I am so sorry if I am asking a simple question because I do not understand how i am going to use it within slicer.

Thank you

Regards,
Saima Safdar

Look at the example to see what VTK classes and what methods of that classes are used to get cell neighbors.

Hi Andras,
I was trying to do what you said. the program below is used to extract neighbouring cells relevant to a single cell for example; 2978 is the cell id of one of the cells in the mesh and it gets its cell point ids and based on the cell point ids it gets the neighouring cells. but my program is crashing. could you please tell me what is wrong what I am doing.

actually problem starts with line “trianglefilter.GetOutput().GetCellNeighbors(2978, idList, neighbourcells)`”" the second argument requires a vtk object. I am passing the vtklist object but dont understand what to do. Could you please advice what am i doing wrong.
Thanks

modelNode = slicer.util.getNode(inputVolume.GetID())
m = modelNode.GetMesh()

trianglefilter = vtk.vtkTriangleFilter()

trianglefilter.SetInputData(m)
trianglefilter.Update()

cellPointIds = vtk.vtkIdList()

trianglefilter.GetOutput().GetCellPoints(2978, cellPointIds)

for i in range(0,3):
    print(cellPointIds.GetId(i))
    
neighbourcells = vtk.vtkIdList()

idList = vtk.vtkIdList()
idList.SetNumberOfIds(3)
idList.SetId(0,1552)

> trianglefilter.GetOutput().GetCellNeighbors(2978, idList, neighbourcells)

print(neighbourcells.GetNumberOfIds())

I figured out my mistake. Thank you andras.

Regards,
Saima Safdar

1 Like

Hi Andras,
I need to find the vertices for each selected cell in the above mesh. How would I be able to get it.
Please help.

Thank you

Regards,
Saima Safdar

vtkCell::GetPointIds returns the list of point indices for a cell. You can get the cell object using GetCell.

thanks andras.

another question how to get the x y z of pointids.

Thanks

Can I select the cells with a ball(paint brush in 3d view) not the point because of large model, and then saved as a polydata?

Please post your question as a new topic. Add some context and preferably illustrations, because from this sentence alone it is not clear what you are trying to achieve.

You can find all these details in VTK API documentation and examples of the corresponding class.

Hi Andras,
I am getting the following error for the example script above. I do not understand what is the problem. I created fiducials and using fiducials i need to select the surface elements on brain mesh like previously. Could you please help.

Traceback (most recent call last):
File “/home/saima/slicer/Slicer-4.11.0-2019-09-17-linux-amd64/ImageAsaModel/MeshNodesToFiducials/MeshNodesToFiducials.py”, line 73, in onApplyButton
logic.run(self.ui.inputSelector.currentNode(), self.ui.outputSelector.currentNode(), imageThreshold, enableScreenshotsFlag)
File “/home/saima/slicer/Slicer-4.11.0-2019-09-17-linux-amd64/ImageAsaModel/MeshNodesToFiducials/MeshNodesToFiducials.py”, line 207, in run
arr = self.onPointsModified(selectionArray, markupsNode, cell)
File “/home/saima/slicer/Slicer-4.11.0-2019-09-17-linux-amd64/ImageAsaModel/MeshNodesToFiducials/MeshNodesToFiducials.py”, line 141, in onPointsModified
selectionArray.SetValue(closestCell, 100)
ValueError: expects 0 <= id && id < GetNumberOfValues()

closestCell value is out of range (there is no item with such index in selectionArray).

Hi Andras,
Is it possible to get the neighboring cells for a vtkunstructured grid 2d.

Regards,
Saima Safdar