# How to select point with vtkWorldPointPicker?

**URL:** <https://discourse.vtk.org/t/how-to-select-point-with-vtkworldpointpicker/14516>\
**Category:** Support\
**Created:** [September 2, 2024, 3:24pm UTC](https://discourse.vtk.org/t/how-to-select-point-with-vtkworldpointpicker/14516 "2024-09-02T15:24:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![learn\_forever](https://discourse.vtk.org/user_avatar/discourse.vtk.org/learn_forever/32/754_2.png) [@learn\_forever](https://discourse.vtk.org/u/learn_forever)\
**Post date:** [September 2, 2024, 3:24pm UTC](https://discourse.vtk.org/t/how-to-select-point-with-vtkworldpointpicker/14516/1 "2024-09-02T15:24:22Z")

</div>

Hi,  
In my scene, I added two renderer into the vtkRenderWindow. And set the layer to 0 and 1 respectively. Here I want to pick the point in the layer 0 with vtkWorldPointPicker. But the result was always wrong. So how can I corretly pick the right point in layer 0?

Thank in advance.

---

<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 3, 2024, 1:49pm UTC](https://discourse.vtk.org/t/how-to-select-point-with-vtkworldpointpicker/14516/2 "2024-09-03T13:49:27Z")

</div>

Hello,

What have you tried so far?

best,

PC

---

<div class="post-metadata">

**Author:** ![learn\_forever](https://discourse.vtk.org/user_avatar/discourse.vtk.org/learn_forever/32/754_2.png) [@learn\_forever](https://discourse.vtk.org/u/learn_forever)\
**Post date:** [September 5, 2024, 3:52am UTC](https://discourse.vtk.org/t/how-to-select-point-with-vtkworldpointpicker/14516/3 "2024-09-05T03:52:45Z")

</div>

Hi:

I set the depth buffer on with vtkRenderer.PreserveDepthBufferOn before picking the point. This is just a workaround since I can’t find other solutions.

Best.

---

<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 5, 2024, 11:14am UTC](https://discourse.vtk.org/t/how-to-select-point-with-vtkworldpointpicker/14516/4 "2024-09-05T11:14:18Z")

</div>

Hi,

Here’s how I do picking (with a `vtkPropPicker`) in my render window, which also has two renderers:

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

    // Prevent picking of wrong world coordinates.
    layer1_renderer->SetPreserveDepthBuffer( true ); //foreground renderer
    layer0_renderer->Render(); //main renderer

    // Get the 3D object under the 2D screen coordinates (ray casting).
    vtkSmartPointer<vtkPropPicker> picker = vtkSmartPointer<vtkPropPicker>::New();
    picker->Pick(clickPos[0], clickPos[1], 0, layer0_renderer);

    // if a vtkActor or subclass was picked
    if( picker->GetActor() ) {

        // Get the cell of a 3D object under the 2D screen coordinates (ray casting).
        vtkSmartPointer<vtkCellPicker> cellPicker = vtkSmartPointer<vtkCellPicker>::New();
        cellPicker->Pick(clickPos[0], clickPos[1], 0, layer0_renderer);

        //if an actor's cell was picked
        if( cellPicker->GetCellId() != -1 ) {

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

            // Print which actor has been picked.
            std::stringstream str;
            picker->GetActor()->Print( str );
            printf( "Picked scene object info:\n" + QString( str.str().c_str() ) );
            printf( "----- end of scene object info ------" );

            // Output the picked location.
            printf( "Picked location (world coordinates): X="
                                              + QString::number(pos[0]) + " Y=" + QString::number(pos[1])
                    + " Z=" + QString::number(pos[2]) );

            // Output the probed value at the picked location.
            vtkDataSet* dataSet = cellPicker->GetDataSet();
            vtkCellData* cellData = dataSet->GetCellData();
            vtkDataArray* dataArray = cellData->GetScalars();
            if( dataArray && dataArray->GetNumberOfComponents() <= 200 ){
                double values[200]; //200 fields is a fairly large number.
                dataArray->GetTuple( cellPicker->GetCellId(), values );
                QString valuesText = "NO VALUES";
                for( int i = 0; i < dataArray->GetNumberOfComponents(); ++i )
                    if( i == 0)
                        valuesText = QString::number(values[0]);
                    else
                        valuesText = valuesText + "; " + QString::number(values[i]);
                printf( "Picked cell id: " + QString::number( cellPicker->GetCellId() ) );
                printf( "Picked vtkDataSet pointer: " + QString::number( (long long)dataSet ) );
                printf( "Picked value(s): " + valuesText);
            } else
                printf( "v3dMouseInteractor::OnLeftButtonUp(): probing not possible if VTK object has no fields or more than 200 fields in it." );
        }// if a cell was picked
    } //if a vtkActor was picked
    else{ //something else was picked
        printf( "v3dMouseInteractor::OnLeftButtonUp(): an unsupported scene element was picked." );
    }

    //restore normal behavior
    layer1_renderer->SetPreserveDepthBuffer( false );

    // Forward the event to the base class.
    vtkInteractorStyleTrackballCamera::OnLeftButtonUp();
}

```

I hope this helps casting light on the issue.

regards,

PC
