scenepicker problem

I am trying to pick using scene picker but it doesn’t seem to find a cell or
vertex, while proppicker does.
How do I extract a pick position from scene picker? (Or even just a picked
cell)
Minimal example below, which gives -1 (not found) for scenepicker but proppicker works fine:

#include <vtkVersion.h>
#include <vtkSmartPointer.h>
#include <vtkActor.h>
#include <vtkSphereSource.h>
#include <vtkRendererCollection.h>
#include <vtkCellArray.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkObjectFactory.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkPropPicker.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkPLYReader.h>
#include <vtkScenePicker.h>
#include <vtkSphereSource.h>

// Execute application.
int main(int, char *[])
{

    vtkSmartPointer<vtkSphereSource> sphereSource = 
            vtkSmartPointer<vtkSphereSource>::New(); 
    sphereSource->Update(); 

    // Create a polydata object 
    vtkPolyData* polydata = sphereSource->GetOutput(); 

    // Create a mapper 
    vtkSmartPointer<vtkPolyDataMapper> mapper = 
            vtkSmartPointer<vtkPolyDataMapper>::New(); 

#if VTK_MAJOR_VERSION <= 5
mapper->SetInput(polydata);
#else
mapper->SetInputData(polydata);
#endif

    // Create an actor 
    vtkSmartPointer<vtkActor> actor = 
            vtkSmartPointer<vtkActor>::New(); 
    actor->SetMapper(mapper); 

    std::cout << "Actor address: " << actor << std::endl; 

    // A renderer and render window 
    vtkSmartPointer<vtkRenderer> renderer = 
            vtkSmartPointer<vtkRenderer>::New(); 
    vtkSmartPointer<vtkRenderWindow> renderWindow = 
            vtkSmartPointer<vtkRenderWindow>::New(); 
    renderWindow->SetStencilCapable(1); // Needed for point picking on scene 

picker
renderWindow->AddRenderer(renderer);

    // An interactor 
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 
            vtkSmartPointer<vtkRenderWindowInteractor>::New(); 
    renderWindowInteractor->SetRenderWindow(renderWindow); 

    // Here comes the scene picker stuff. [ Just 2 lines ] 
    vtkScenePicker * scenepicker = vtkScenePicker::New(); 
    scenepicker->SetRenderer(renderer); 
    scenepicker->EnableVertexPickingOn(); 
    

    // Add the actors to the scene 
    renderer->AddActor(actor); 
    renderer->SetBackground(0, 0, 1); 

    // Render and interact 
    renderWindow->Render(); 
    renderWindowInteractor->Initialize(); 


    // Now the picking 

stuff…
int e[2] = { 175, 215 };

    // Pick from this location using standard prop picker (this works fine) 
    vtkSmartPointer<vtkPropPicker>  picker = 
            vtkSmartPointer<vtkPropPicker>::New(); 
    picker->Pick(e[0], e[1], 0, renderer); 

    double* pos = picker->GetPickPosition(); 
    std::cout << "Pick position (world coordinates) is: " 
            << pos[0] << " " << pos[1] 
            << " " << pos[2] << std::endl; 
            
    // Pick using scene picker (this fails) 
    vtkIdType vertexId = scenepicker->GetVertexId(e); 
    vtkIdType cellId = scenepicker->GetCellId(e); 
    std::cout << "Scene pick vertex Id " << vertexId << std::endl; 
    std::cout << "Scene pick cell Id " << cellId << std::endl; 

    double world_point[3]; 
    if (vertexId != -1) 
    { 
            polydata->GetPoint(vertexId, world_point); 
            std::cout << "Scene pick position is : " << world_point[0] << " " << 

world_point[1] << " " << world_point[2] << std::endl;;
}

    renderWindowInteractor->Start(); 

    return EXIT_SUCCESS; 

}

There does not seem to be any tests for vtkScenePicker and its last meaningful modification was in 2008, so it is safe to assume that the class is broken.

If you create a test and submit a pull request then VTK developers can more easily run it and see if they find any trivial bug. However, most likely your help will be needed with the figuring out what’s wrong and how to fix it. For example, you can add a breakpoint at picker->Pick, execute the method step by step, and see where things go wrong.