# How to pick a point closest from the mouse cursor?

**URL:** https://discourse.vtk.org/t/how-to-pick-a-point-closest-from-the-mouse-cursor/4318
**Category:** Support
**Created:** [September 29, 2020, 2:59am UTC](https://discourse.vtk.org/t/how-to-pick-a-point-closest-from-the-mouse-cursor/4318 "2020-09-29T02:59:26Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Kim\_William](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kim_william/32/373_2.png) [@Kim\_William](https://discourse.vtk.org/u/Kim_William)
#### Post date: [September 29, 2020, 2:59am UTC](https://discourse.vtk.org/t/how-to-pick-a-point-closest-from-the-mouse-cursor/4318/1 "2020-09-29T02:59:26Z")

</div>

Hi all.  
I have a question about pick a point.

I have a commercial program that provides a function what if the mouse cursor moving, the closest point is selected.  
It is useful when I zoom in the point cloud data and check the coordinate.  
 ![point pick](https://discourse.vtk.org/uploads/default/original/2X/6/6730dc249c61d6834603fc42ea0d695d83dd6a31.gif)

I made a sample program that checking the coordinate along with the mouse cursor.  
I referred to the below link.  
[https://lorensen.github.io/VTKExamples/site/Cxx/Interaction/PointPicker/](https://lorensen.github.io/VTKExamples/site/Cxx/Interaction/PointPicker/)

 ![point pick2](https://discourse.vtk.org/uploads/default/original/2X/a/ac8cb47e087cc4f60432f0432c3ec05da6a2b0e5.gif)

I only can get the point’s coordinate when the cursor is above the point.

Does the VTK library provide the selecting the closest point from the mouse cursor?

I need your advise.

Thank you 🙂

---

<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: [September 29, 2020, 5:13pm UTC](https://discourse.vtk.org/t/how-to-pick-a-point-closest-from-the-mouse-cursor/4318/2 "2020-09-29T17:13:55Z")

</div>

Hi, Kim,

I suggest you to take a look at [https://vtk.org/doc/nightly/html/classvtkPointLocator.html](https://vtk.org/doc/nightly/html/classvtkPointLocator.html) and [https://vtk.org/doc/nightly/html/classvtkCellLocator.html](https://vtk.org/doc/nightly/html/classvtkCellLocator.html) . Both have methods called `FindClosestPoint*()` and `FindClosestCell*()`. One should suit you.

regards,

Paulo

---

<div class="post-metadata">

### Author: ![Kim\_William](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kim_william/32/373_2.png) [@Kim\_William](https://discourse.vtk.org/u/Kim_William)
#### Post date: [October 5, 2020, 12:36am UTC](https://discourse.vtk.org/t/how-to-pick-a-point-closest-from-the-mouse-cursor/4318/3 "2020-10-05T00:36:30Z")

</div>

Hi, Paulo.  
Thank you for your help.  
But I have an additional question…

Do I need a preparation step to use the vtkPointLocator?

I have written the code that picking a point from the GetEventPosition() function on OnMouseMove() event.

I referred to the following examples:  
[https://lorensen.github.io/VTKExamples/site/Cxx/Interaction/Picking/](https://lorensen.github.io/VTKExamples/site/Cxx/Interaction/Picking/)  
[https://lorensen.github.io/VTKExamples/site/Cxx/Interaction/PointPicker/](https://lorensen.github.io/VTKExamples/site/Cxx/Interaction/PointPicker/)

I added the vtkPointLocator and call the FindClosestPoint() function, but it always returns -1.  
I referred to the following examples:  
[https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/PointLocator/](https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/PointLocator/)  
[https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/PointLocatorVisualization/](https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/PointLocatorVisualization/)

Could you please advise for me?

Thank you.

Best regards,  
William Kim

---

<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: [October 8, 2020, 6:12pm UTC](https://discourse.vtk.org/t/how-to-pick-a-point-closest-from-the-mouse-cursor/4318/4 "2020-10-08T18:12:49Z")

</div>

Hello, can you post the part of your code involved in point search?

regards,

Paulo

---

<div class="post-metadata">

### Author: ![Kim\_William](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kim_william/32/373_2.png) [@Kim\_William](https://discourse.vtk.org/u/Kim_William)
#### Post date: [October 12, 2020, 12:09am UTC](https://discourse.vtk.org/t/how-to-pick-a-point-closest-from-the-mouse-cursor/4318/5 "2020-10-12T00:09:39Z")

</div>

Hi Paulo.

I have modified my code and it doesn’t return -1 but it doesn’t work well.

I attached the code part of the mouse event, if you can review my code I can send you my full code.

```
virtual void OnMouseMove() override
{
	if (m_bEnablePicking == true)
	{
		int* pos = this->GetInteractor()->GetEventPosition();
			
		vtkNew<vtkPropPicker> picker;
		picker->Pick(pos[0], pos[1], 0, this->GetDefaultRenderer());
		double* worldPosition = picker->GetPickPosition();
			
		int id = locator_point->FindClosestPoint(pos[0], pos[1], pos[2]);
		double* position = polydata->GetPoint(id);
	}

	vtkInteractorStyleTrackballCamera::OnMouseMove();
}

vtkSmartPointer<vtkPointLocator> locator_point;

```

Thank you.

Best regards,  
William Kim

---

<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: [October 12, 2020, 2:29pm UTC](https://discourse.vtk.org/t/how-to-pick-a-point-closest-from-the-mouse-cursor/4318/6 "2020-10-12T14:29:09Z")

</div>

Hello, Kim,

Try changing your pick handling code to something like this (notice the `vtkPointPicker` in place of `vtkPropPicker`):

```cpp
// Get pick position in 2D screen coordinates.
int* clickPos = this->GetInteractor()->GetEventPosition();

// Get the point of a 3D object under the 2D screen coordinates (ray casting).
vtkSmartPointer<vtkPointPicker> pointPicker = vtkSmartPointer<vtkPointPicker>::New();
pointPicker->Pick(clickPos[0], clickPos[1], 0, this->GetDefaultRenderer());

//if a point was picked...
if( pointPicker->GetPointId() != -1 ) {

      // ...get the picked location in world coordinates.
      double* pos = picker->GetPickPosition();

      // Get the picked vertex.
      int id = locator_point->FindClosestPoint(pos[0], pos[1], pos[2]);
      double* position = polydata->GetPoint(id);
     
      (...)

} else {
      std::cout << "Pick failed." << std::endl;
}

```

I hope this helps,

Paulo

---

<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: [October 12, 2020, 2:35pm UTC](https://discourse.vtk.org/t/how-to-pick-a-point-closest-from-the-mouse-cursor/4318/7 "2020-10-12T14:35:02Z")

</div>

Taking a second look, I think you can stick with the `vtkPropPicker`. So, can you paste the part of the code where you instantiate and configure `locator_point`?

---

<div class="post-metadata">

### Author: ![Kim\_William](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kim_william/32/373_2.png) [@Kim\_William](https://discourse.vtk.org/u/Kim_William)
#### Post date: [October 13, 2020, 12:00am UTC](https://discourse.vtk.org/t/how-to-pick-a-point-closest-from-the-mouse-cursor/4318/8 "2020-10-13T00:00:14Z")

</div>

Hi Paulo.

I attached my code as below:

```
vtkNew<vtkPoints> m_vtkPCDPoints;
vtkNew<vtkPolyData> m_vtkPolydata;
vtkNew<vtkUnsignedCharArray> colors;

//...
m_vtkPolydata->SetPoints(m_vtkPCDPoints);
m_vtkPolydata->GetPointData()->SetScalars(colors);
m_vtkPointLocator->SetDataSet(m_vtkPolydata);
//...

```

Thank you.

---

<div class="post-metadata">

### Author: ![Kim\_William](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kim_william/32/373_2.png) [@Kim\_William](https://discourse.vtk.org/u/Kim_William)
#### Post date: [October 13, 2020, 12:10am UTC](https://discourse.vtk.org/t/how-to-pick-a-point-closest-from-the-mouse-cursor/4318/9 "2020-10-13T00:10:18Z")

</div>

Hi Paulo.

oh… The vtkPointPicker works better than vtkPropPicker.

I think it is fine. Thank you.

May I ask you to review my code that written above comment?

---

<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: [October 13, 2020, 1:17pm UTC](https://discourse.vtk.org/t/how-to-pick-a-point-closest-from-the-mouse-cursor/4318/10 "2020-10-13T13:17:22Z")

</div>

Hi, Kim,

Do `polydata` and `m_vtkPolydata` in your examples point to the same object? If not, you can’t use point IDs of one object to fetch points of another data set. The IDs are like indexes in a container.

all the best,

Paulo

---

<div class="post-metadata">

### Author: ![Kim\_William](https://discourse.vtk.org/user_avatar/discourse.vtk.org/kim_william/32/373_2.png) [@Kim\_William](https://discourse.vtk.org/u/Kim_William)
#### Post date: [October 14, 2020, 5:15am UTC](https://discourse.vtk.org/t/how-to-pick-a-point-closest-from-the-mouse-cursor/4318/11 "2020-10-14T05:15:25Z")

</div>

Hi Paulo.

Thank you for your kindly help.

I solved my issue by your advise.

Thank you 🙂

Best regards,  
William Kim
