# How to pick a cell from vtkPolyData by coordinate

**URL:** https://discourse.vtk.org/t/how-to-pick-a-cell-from-vtkpolydata-by-coordinate/9096
**Category:** Support
**Created:** [August 6, 2022, 11:17am UTC](https://discourse.vtk.org/t/how-to-pick-a-cell-from-vtkpolydata-by-coordinate/9096 "2022-08-06T11:17:28Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![rockydut](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rockydut/32/4261_2.png) [@rockydut](https://discourse.vtk.org/u/rockydut)
#### Post date: [August 6, 2022, 11:17am UTC](https://discourse.vtk.org/t/how-to-pick-a-cell-from-vtkpolydata-by-coordinate/9096/1 "2022-08-06T11:17:29Z")

</div>

Dear all, I extract edges from a polydata firstly, as showing below:

vtkNew edges;  
edges-\>SetInputData(data);  
edges-\>Update();  
vtkPolyData \*poly = edges-\>GetOutput();

Then I want to pick the corresponding line close to the mouse picked point.

The mouse picked point can be get from:  
int\* clickpos = this-\>GetInteractor()-\>GetEventPosition();  
vtkNew picker;  
picker-\>Pick(clickpos[0], clickpos[1], 0, GetRenderer());  
double \*clickedcoord = picker-\>GetPickPosition();

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [August 6, 2022, 10:10pm UTC](https://discourse.vtk.org/t/how-to-pick-a-cell-from-vtkpolydata-by-coordinate/9096/2 "2022-08-06T22:10:47Z")

</div>

Hi,

Can you, please, post your code between two ```? Doing so prevents parts of your code being parsed as markdown syntax as well as making your code more readable.

regards,

Paulo

---

<div class="post-metadata">

### Author: ![rockydut](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rockydut/32/4261_2.png) [@rockydut](https://discourse.vtk.org/u/rockydut)
#### Post date: [August 7, 2022, 1:05am UTC](https://discourse.vtk.org/t/how-to-pick-a-cell-from-vtkpolydata-by-coordinate/9096/3 "2022-08-07T01:05:01Z")

</div>

```
vtkDataSetMapper* mapper = dynamic_cast<vtkDataSetMapper*>(actor->GetMapper());
vtkDataSet* data = mapper->GetInput();

vtkNew<vtkExtractEdges> edges;
edges->SetInputData(data);
edges->Update();
vtkPolyData *poly = edges->GetOutput();

int* clickpos = this->GetInteractor()->GetEventPosition();
vtkNew<vtkPropPicker> picker;
picker->Pick(clickpos[0], clickpos[1], 0, GetRenderer());
double *clickedcoord = picker->GetPickPosition();

///////////
//Then, I want to selectithe line from "poly" which near the "clickedcoord".

```

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [August 7, 2022, 1:06pm UTC](https://discourse.vtk.org/t/how-to-pick-a-cell-from-vtkpolydata-by-coordinate/9096/4 "2022-08-07T13:06:40Z")

</div>

Hello,

I suggest you to study this example: [https://kitware.github.io/vtk-examples/site/Cxx/PolyData/CellLocator/](https://kitware.github.io/vtk-examples/site/Cxx/PolyData/CellLocator/)

In VTK context, a cell is any composition of `vtkPoint`s, which is a sort of atomic object. The simplest cell type is a vertex which is composed by a single “atom”. Other cell types include:  
 ![image](https://discourse.vtk.org/uploads/default/original/2X/5/542a06827c881ecd5c8a80347ba356eba762a896.png)

I belive you want to query the cell of type `VTK_LINE`(3) or `VTK_POLY_LINE`(4) that is near `clickedcoord[0]`, `clickedcoord[1]`, `clickedcoord[2]`. So you can set either `3` or `4` as the `subId` parameter of the `FindClosestPoint()` method in the example above.

**NOTICE VTK 9** : If you’re using VTK 9, be aware that the conversion to world coordinates in picking operations has a standing bug: [vtkPropPicker in VTK 9.1 is now returning wrong world coordinates.](https://discourse.vtk.org/t/vtkproppicker-in-vtk-9-1-is-now-returning-wrong-world-coordinates/8061) . I submitted a merge request with a possible solution here: [https://gitlab.kitware.com/vtk/vtk/-/merge\_requests/9125](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/9125) . If you experience this bug and can’t wait for a solution to make it downstream, you can patch VTK yourself (like I did) as described here: [VTK 9: the pick marker is not positioned on the picked location, instead it is now placed in a plane orthogonal to the camera. · Issue #273 · PauloCarvalhoRJ/gammaray · GitHub](https://github.com/PauloCarvalhoRJ/gammaray/issues/273#issuecomment-1107924892).

take care,

Paulo

---

<div class="post-metadata">

### Author: ![rockydut](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rockydut/32/4261_2.png) [@rockydut](https://discourse.vtk.org/u/rockydut)
#### Post date: [August 9, 2022, 8:03am UTC](https://discourse.vtk.org/t/how-to-pick-a-cell-from-vtkpolydata-by-coordinate/9096/5 "2022-08-09T08:03:11Z")

</div>

Thank you for your reply.  
My question is how to find a cell in the polydata which is nearest close to a point.

---

<div class="post-metadata">

### Author: ![will.schroeder](https://discourse.vtk.org/user_avatar/discourse.vtk.org/will.schroeder/32/233_2.png) [@will.schroeder](https://discourse.vtk.org/u/will.schroeder)
#### Post date: [August 9, 2022, 10:31am UTC](https://discourse.vtk.org/t/how-to-pick-a-cell-from-vtkpolydata-by-coordinate/9096/6 "2022-08-09T10:31:05Z")

</div>

vtkPolyData inherits FindCell() methods from vtkPointSet (which is an implementation of the pure virtual FindCell() defined in vtkDataSet - look in vtkDataSet for documentation about FindCell()). Using vtkPolyData::FindCell() will work for most applications and is reasonably fast.

However note that this is potentially a complex operation as there are different ways to “find cells” depending on your application. If you really want to hurt your brain, look into the vtkFindCellStrategy class and derived classes.

---

<div class="post-metadata">

### Author: ![KEN](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ken/32/5637_2.png) [@KEN](https://discourse.vtk.org/u/KEN)
#### Post date: [August 9, 2022, 2:21pm UTC](https://discourse.vtk.org/t/how-to-pick-a-cell-from-vtkpolydata-by-coordinate/9096/7 "2022-08-09T14:21:55Z")

</div>

1- use vtkCellPicker  
2- get picked cell then loop the edges  
3- calculate distance between “clickedcoord” and each edge and find minimum  
4- use minimum edge points to get ids from edge data using FindPoint function  
5- use GetPointCells function on edge data to get cells of point 1 and 2 of the selected edge  
6- the common cell id is the line id in your edge data
