[vtkusers] FW: Color mapping 3d points cloud

adrian C A adrianca88 at hotmail.com
Tue Apr 10 04:12:45 EDT 2012


Hi,

thanks for your help. After read the code of the links, I have tried to render my cloud of 3d point, but I get an exception when I call the render() method. The code that I have implemented is the next:


    vtkSmartPointer<vtkPolyData> pData = vtkSmartPointer<vtkPolyData>::New();    

    vtkSmartPointer<vtkPolyDataReader> reader = vtkSmartPointer<vtkPolyDataReader>::New();
    reader->SetFileName("prueba.vtk");
    pData =    reader->GetOutput();
    pData->Update();

    vtkSmartPointer<vtkDelaunay3D> delaunay = vtkSmartPointer<vtkDelaunay3D>::New();
    delaunay->SetInput(pData);
    delaunay->SetAlpha(2.8);
    delaunay->Update();

    vtkUnstructuredGrid* outputGrid = delaunay->GetOutput();
 
    double bounds[6];
    outputGrid->GetBounds(bounds);
 
    // Find min and max z
    double minz = bounds[4];
    double maxz = bounds[5];
    cout << "minz: " << minz << std::endl;
    cout << "maxz: " << maxz << std::endl;
 
    vtkSmartPointer<vtkLookupTable> colorLookupTable = vtkSmartPointer<vtkLookupTable>::New(); //Pasa de valores escalares a RGB
    colorLookupTable->SetTableRange(minz, maxz);
    colorLookupTable->Build();
 
    // Generate the colors for each point based on the color map
    vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New(); //Array dinamico de caracteres
    colors->SetNumberOfComponents(3);
    colors->SetName("Colors");
 
    cout << "There are " << outputGrid->GetNumberOfPoints() << " points." << endl;
 
    for(int i = 0; i < outputGrid->GetNumberOfPoints(); i++)
    {
        double p[3];
        outputGrid->GetPoint(i,p);
 
        double dcolor[3];
        colorLookupTable->GetColor(p[2], dcolor); //Devuelve el color RGB para cada escalar
        cout << "dcolor: " << dcolor[0] << " " << dcolor[1] << " " << dcolor[2] << endl;
        unsigned char color[3];
        for(unsigned int j = 0; j < 3; j++)
        {
            color[j] = static_cast<unsigned char>(255.0 * dcolor[j]);
        }
        cout << "color: " << (int)color[0] << " " << (int)color[1] << " " << (int)color[2] << endl;
 
        colors->InsertNextTupleValue(color);
    }
 
    outputGrid->GetPointData()->SetScalars(colors);

    
    vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(outputGrid->GetProducerPort());

    vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
 
    // Create a renderer, render window, and interactor
    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);
 
    // Add the actor to the scene
    renderer->AddActor(actor);
    renderer->SetBackground(.1, .2, .3);
 
    // Render and interact
    renderWindowInteractor->Initialize();
    renderWindow->Render();
    renderWindowInteractor->Start();


Have you got any idea of why I get this exception??

Again, a lot of thanks.

Adrian.


From: adrianca88 at hotmail.com
To: vtkusers at vtk.org
Subject: Color mapping 3d points cloud
Date: Fri, 30 Mar 2012 10:25:26 +0200







Hi,

I have a cloud of 3d points that I read from file (each line stores de x y z coordinates) and save in a vtkPolyData using a vtkPoints. Next, using vtkDelaunay3d I achive to get the 3D rendering.
My problem is that I want to create a colorMapping that represents different heigh with different colour, but I don't know how set this scalar value (which, in this case, depends on the z variable).

Sorry if the question is very easy to solve but I am very newbie with VTK.

A lot of thanks, 

Adrian.
 		 	   		   		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120410/a7f70990/attachment.htm>


More information about the vtkusers mailing list