# Move sphere along polyline

**URL:** https://discourse.vtk.org/t/move-sphere-along-polyline/2225
**Category:** Support
**Created:** [December 6, 2019, 3:21pm UTC](https://discourse.vtk.org/t/move-sphere-along-polyline/2225 "2019-12-06T15:21:39Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Romain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/romain/32/32_2.png) [@Romain](https://discourse.vtk.org/u/Romain)
#### Post date: [December 6, 2019, 3:21pm UTC](https://discourse.vtk.org/t/move-sphere-along-polyline/2225/1 "2019-12-06T15:21:39Z")

</div>

Hi everyone,

I try to move a sphere along a Polyline.

I use vtkPropPicker to get the current mouse position then I use vtkCellLocator to found the closest point from the picked position to the Polyline using FindClosestPoint method. This method works great only if the pick point is very close to the polyline. Once the mouse is not near the polyline, the closestPointMethod return the first element of my polyline.

I tried to use the method FindClosestPointWithinRadius to increase the distance between the mouse and the polyline but nothing changed.

The following code show the moving part:

```auto
        picker->GetPickPosition(pickCoords);

        vtkSmartPointer<vtkStaticCellLocator> cellLocator =
            vtkSmartPointer<vtkStaticCellLocator>::New();
        cellLocator->SetDataSet(m_polyLine);
        cellLocator->BuildLocator();
        vtkSmartPointer<vtkGenericCell> cell = vtkSmartPointer<vtkGenericCell>::New();
        vtkIdType cellId;
        int subId;
        double dist;
        cellLocator->FindClosestPoint(pickCoords, pointCoords, cell, cellId,
                                      subId, dist);
...
        m_sphere->SetCenter(pointCoords);

```

Is it a bug or do I misuse vtkCellLocator?

Thank you!

---

<div class="post-metadata">

### Author: ![Romain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/romain/32/32_2.png) [@Romain](https://discourse.vtk.org/u/Romain)
#### Post date: [December 10, 2019, 10:03am UTC](https://discourse.vtk.org/t/move-sphere-along-polyline/2225/2 "2019-12-10T10:03:00Z")

</div>

I found the source of the error: the picking coordinates returned when I don’t pick any actor is not as what I expected.  
I tried to use vtkWorldPointPicker like any other picker but I have those warnings:

```auto
2019-12-10 10:52:28.313 ( 22.811s) [] vtkOpenGLState.cxx:203 WARN| Error in cache state for GL_READ_FRAMEBUFFER_BINDING
2019-12-10 10:52:28.334 ( 22.832s) [] vtkOpenGLState.cxx:265 WARN| at stack loc
 at vtksys::SystemInformationImplementation::GetProgramStack in ....

```

For information I use the master branch of VTK with Qt 5.12.5 on Windows 10 Pro and with a Nvidia 2080 Max-Q.

Does anyone have the same behavior?

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [December 10, 2019, 9:21pm UTC](https://discourse.vtk.org/t/move-sphere-along-polyline/2225/3 "2019-12-10T21:21:08Z")

</div>

Since it is very easy to find closest point on a line and we want to allow some picking tolerance, we found that a fast and robust solution is to transform all the lines to display coordinates and find the closest line segment there:

> <https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/Markups/VTKWidgets/vtkSlicerMarkupsWidgetRepresentation2D.cxx#L572-L624>

---

<div class="post-metadata">

### Author: ![Romain](https://discourse.vtk.org/user_avatar/discourse.vtk.org/romain/32/32_2.png) [@Romain](https://discourse.vtk.org/u/Romain)
#### Post date: [December 13, 2019, 2:36pm UTC](https://discourse.vtk.org/t/move-sphere-along-polyline/2225/4 "2019-12-13T14:36:49Z")

</div>

Thank you for your answer! This solution works great and fast. I used vtkSelectVisiblePoints filter to select all visible points on the display and vtkInteractorObserver::ComputeDisplayToWorld and ComputeWorldToDisplay methods.  
There is a limit of using only visible points: if no connexity verification between two consecutive points are done then the closest point on the segment can be in the “void”. For me it’s a very small limitation so I will not improve for now this algorithm.
