How to pick a cell from vtkPolyData by coordinate

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

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

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".

Hello,

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

In VTK context, a cell is any composition of vtkPoints, 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

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. . I submitted a merge request with a possible solution here: 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.

take care,

Paulo

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

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.