# Get cell data of a mesh at selected point positions

**URL:** <https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859>\
**Category:** Development\
**Created:** [October 22, 2019, 1:28am UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859 "2019-10-22T01:28:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [October 22, 2019, 1:28am UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/1 "2019-10-22T01:28:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [October 22, 2019, 1:17pm UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/2 "2019-10-22T13:17:54Z")

</div>

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](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Get_scalar_values_at_surface_of_a_model)

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [November 5, 2019, 5:55am UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/3 "2019-11-05T05:55:44Z")

</div>

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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/f/a/fac33aa560e11ee297f0a797aefea041a5f0ff40.png)

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 5, 2019, 2:16pm UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/4 "2019-11-05T14:16:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [November 6, 2019, 4:16am UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/5 "2019-11-06T04:16:36Z")

</div>

> [@lassoan](#):
>
> ery robustly for m

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

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

Regards,  
Saima Safdar

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 6, 2019, 5:41am UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/6 "2019-11-06T05:41:40Z")

</div>

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](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Select_cells_of_a_using_markups_fiducial_points) 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](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/f/3f8261af1125092ef1047c68237ecc5db9a2f6f2.png)

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [November 6, 2019, 5:52am UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/7 "2019-11-06T05:52:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 6, 2019, 12:42pm UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/8 "2019-11-06T12:42:36Z")

</div>

See this example: [https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/CellPointNeighbors/](https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/CellPointNeighbors/)

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [November 7, 2019, 6:27am UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/9 "2019-11-07T06:27:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [November 7, 2019, 12:34pm UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/10 "2019-11-07T12:34:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [November 14, 2019, 4:04am UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/11 "2019-11-14T04:04:55Z")

</div>

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())

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [November 14, 2019, 5:31am UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/12 "2019-11-14T05:31:39Z")

</div>

I figured out my mistake. Thank you andras.

Regards,  
Saima Safdar

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [January 16, 2020, 7:48am UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/13 "2020-01-16T07:48:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 16, 2020, 9:52pm UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/14 "2020-01-16T21:52:03Z")

</div>

[vtkCell::GetPointIds](https://vtk.org/doc/nightly/html/classvtkCell.html) returns the list of point indices for a cell. You can get the cell object using [GetCell](https://vtk.org/doc/nightly/html/classvtkUnstructuredGrid.html#a0910469a383a022097d324c1f40fb831).

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [January 17, 2020, 4:45am UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/15 "2020-01-17T04:45:45Z")

</div>

thanks andras.

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

Thanks

---

<div class="post-metadata">

**Author:** ![timeanddoctor](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/timeanddoctor/32/5839_2.png) [@timeanddoctor](https://discourse.slicer.org/u/timeanddoctor)\
**Post date:** [January 17, 2020, 1:11pm UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/16 "2020-01-17T13:11:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [January 17, 2020, 1:37pm UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/17 "2020-01-17T13:37:43Z")

</div>

> [@timeanddoctor](#):
>
> 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.

> [@Saima](#):
>
> another question how to get the x y z of pointids.

You can find all these details in [VTK API documentation](https://vtk.org/doc/nightly/html/) and [examples](https://lorensen.github.io/VTKExamples/site/) of the corresponding class.

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [February 3, 2020, 7:51am UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/18 "2020-02-03T07:51:07Z")

</div>

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()

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [February 3, 2020, 2:01pm UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/19 "2020-02-03T14:01:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Saima](https://avatars.discourse-cdn.com/v4/letter/s/9d8465/32.png) [@Saima](https://discourse.slicer.org/u/Saima)\
**Post date:** [November 12, 2021, 4:10am UTC](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859/20 "2021-11-12T04:10:05Z")

</div>

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

Regards,  
Saima Safdar

[Next page](https://discourse.slicer.org/t/get-cell-data-of-a-mesh-at-selected-point-positions/8859.md?page=2)
