# using CellPicker with Trame "Surface Picking" example

**URL:** https://discourse.vtk.org/t/using-cellpicker-with-trame-surface-picking-example/10404
**Category:** Support
**Created:** [January 9, 2023, 1:57am UTC](https://discourse.vtk.org/t/using-cellpicker-with-trame-surface-picking-example/10404 "2023-01-09T01:57:08Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Robert\_Collar](https://discourse.vtk.org/user_avatar/discourse.vtk.org/robert_collar/32/6256_2.png) [@Robert\_Collar](https://discourse.vtk.org/u/Robert_Collar)
#### Post date: [January 9, 2023, 1:57am UTC](https://discourse.vtk.org/t/using-cellpicker-with-trame-surface-picking-example/10404/1 "2023-01-09T01:57:08Z")

</div>

Hi VTK Community!

In the “Surface Picking” Trame example, found [here](https://github.com/Kitware/trame/tree/master/examples/06_vtk/Applications/SurfacePicking), a user can “pick” locations on the screen and the closest point in the racecar mesh is selected and indicated with a cone pointer + tooltip. As far as I can tell, the mechanism for this feature is as follows: upon a “click” event, the event data are passed to `clickData`, from which the `"world position"` is extracted. The point closest to the “world position” coordinates is identified using the `FindPoint()` method, and the dataset associated with that point is displayed in the tooltip.

I’d like to alter this example such that a cell is selected rather than a point. Intuitively, I reached for the `FindCell()` method, but its use is much more complicated than I thought, as its performance seems to depend on many parameters (7 inputs compared to just 1 for `FindPoint()`!). Given this complexity (for example, I’m not even sure how to pass an initialization cell to `FindCell()`) I searched for an alternative. I’ve come across the `CellPicker()` class which seems to be an efficient way to select a cell upon a click event. However, I can’t find a way to reset the picker used by the `vtkView()` object (or would it be the `vtkGeometryRepresentation()` object?).

In summary, how might I adapt this example to make use of the `CellPicker` class to efficiently select cells in a mesh?

As an aside, the cherry on top would be if someone could point to a reference that explains how the `vtkView()` + `vtkGeometryRepresentation()` paradigm differs from, for example, the pipeline employed in the “Remote Selection” Trame example [here](https://github.com/Kitware/trame/tree/master/examples/06_vtk/Applications/RemoteSelection).

---

<div class="post-metadata">

### Author: ![Sebastien\_Jourdain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastien_jourdain/32/100_2.png) [@Sebastien\_Jourdain](https://discourse.vtk.org/u/Sebastien_Jourdain)
#### Post date: [January 9, 2023, 8:22pm UTC](https://discourse.vtk.org/t/using-cellpicker-with-trame-surface-picking-example/10404/2 "2023-01-09T20:22:52Z")

</div>

So when doing selection with VtkView/VtkRepresentation, you are also getting a ray, which can be used for your [FindCell](https://vtk.org/doc/nightly/html/classvtkDataSet.html#a2221c10d3c4cca44e82c5ef70e4e1cbd).

> virtual vtkIdType vtkDataSet::FindCell ( double x[3],  
> vtkCell \* cell,  
> vtkIdType cellId,  
> double tol2,  
> int & subId,  
> double pcoords[3],  
> double \* weights  
> )   
> pure virtual  
> Locate cell based on global coordinate x and tolerance squared.
> 
> If cell and cellId is non-nullptr, then search starts from this cell and looks at immediate neighbors. Returns cellId \>= 0 if inside, \< 0 otherwise. The parametric coordinates are provided in pcoords[3]. The interpolation weights are returned in weights. (The number of weights is equal to the number of points in the found cell). Tolerance is used to control how close the point is to be considered “in” the cell. THIS METHOD IS NOT THREAD SAFE.

So the difference with VtkView/VtkRepresentation is that since the rendering is happening locally, you can get some xyz coord along with some ray in 3d and 2d (x,y) display coord. While for the RemoteView, you can only get the display (x,y) coordinate. You can still make your implementation only rely in 2d coords and let VTK/C++ do the ray/intersection.

HTH,

Seb

---

<div class="post-metadata">

### Author: ![Robert\_Collar](https://discourse.vtk.org/user_avatar/discourse.vtk.org/robert_collar/32/6256_2.png) [@Robert\_Collar](https://discourse.vtk.org/u/Robert_Collar)
#### Post date: [January 12, 2023, 4:33am UTC](https://discourse.vtk.org/t/using-cellpicker-with-trame-surface-picking-example/10404/3 "2023-01-12T04:33:00Z")

</div>

Hi Sebastien,

Thank you for the help! While it’s still not quite clear how I would use a ray with `FindCell()`, I was able to successfully use just the 3D coordinates (`"worldPosition"`) with `FindCell()`, as follows:

```auto
xyx = data["worldPosition"]
gen_cell = vtkGenericCell()
sub_id = reference(0)
pc = [0, 0, 0]
weights = [0,0,0,0,0,0,0,0,0,0,0,0]
face_idx = lines_polydata.FindCell(xyx, gen_cell, -1, 0.01, sub_id, pc, weights)

```

---

<div class="post-metadata">

### Author: ![Sebastien\_Jourdain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sebastien_jourdain/32/100_2.png) [@Sebastien\_Jourdain](https://discourse.vtk.org/u/Sebastien_Jourdain)
#### Post date: [January 12, 2023, 3:21pm UTC](https://discourse.vtk.org/t/using-cellpicker-with-trame-surface-picking-example/10404/4 "2023-01-12T15:21:30Z")

</div>

You can use a cell locator too like described [here](https://stackoverflow.com/questions/50468069/vtkcelllocator-findclosestpoint-usage-in-python)

---

<div class="post-metadata">

### Author: ![Robert\_Collar](https://discourse.vtk.org/user_avatar/discourse.vtk.org/robert_collar/32/6256_2.png) [@Robert\_Collar](https://discourse.vtk.org/u/Robert_Collar)
#### Post date: [January 16, 2023, 1:29am UTC](https://discourse.vtk.org/t/using-cellpicker-with-trame-surface-picking-example/10404/5 "2023-01-16T01:29:04Z")

</div>

Thank you @Sebastien_Jourdain! I’ve opted to use a `vtkCellPicker`, as shown in [this example](https://discourse.vtk.org/t/unexpected-behavior-when-selecting-and-highlighting-cells-in-trame/10333/8) from one of my other posts. I appreciate the suggestion!
