How to get pixel color from 3D coordinates ?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to get pixel color from 3D coordinates ?

corentin.desfarges
Hi

I'm trying to get the color of a pixel picked from a vtkPolyData.
I tried to combine this two tutorials : PickPixel and (Point)Picking

I can get the coordinates of the picked point, but unfortunatly, I'm can't get the vtkCell...

Here is the code I use :

    std::string inputFilename = "/home/cde/data/mesh.vtk";
    vtkSmartPointer<vtkGenericDataObjectReader> reader = vtkSmartPointer<vtkGenericDataObjectReader>::New();
    reader->SetFileName(inputFilename.c_str());
    reader->Update();
    polydata = reader->GetPolyDataOutput();

    [...]

    double* pos = picker->GetPickPosition();

    vtkPointData* pd = polydata->GetPointData();
    if( !pd )
    {
        return;
    }

    // Use tolerance as a function of size of source data
    double tol2 = polydata->GetLength();
    tol2 = tol2 ? tol2*tol2 / 1000.0 : 0.001;

    // Find the cell that contains pos
    int subId;
    double pcoords[3], weights[8];
    vtkCell* cell = polydata->FindAndGetCell( pos, NULL, -1, tol2, subId, pcoords, weights );

    if( cell )
    {
        // Interpolate the point data
        this->PointData->InterpolatePoint( pd, 0, cell->PointIds, weights );
        double* tuple = this->PointData->GetScalars()->GetTuple( 0 );

        double r = std::stod(vtkVariant(tuple[0]).ToString())/255;
        double g = std::stod(vtkVariant(tuple[1]).ToString())/255;
        double b = std::stod(vtkVariant(tuple[2]).ToString())/255;

        std::cout << r*255 << " " << g*255 << " " << b*255 << std::endl;

        renderer->SetBackground(r,g,b);
    }
    else
    {
        std::cout << "no cell found " << std::endl;
    }

Thanks for your help,

Corentin
Reply | Threaded
Open this post in threaded view
|

Re: How to get pixel color from 3D coordinates ?

Tharun
May I know if you solved this problem?