How to select point with vtkWorldPointPicker?

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.

Hello,

What have you tried so far?

best,

PC

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.

Hi,

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

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