[vtkusers] vtkButterflySubdivisionFilter colour problem

madz madaramh at gmail.com
Sun Nov 24 23:14:06 EST 2013


I need to make a surface plot with vtk and experimented with several methods.
When I use vtkLoopSubdivisionFilter I can see that the appearance of the
graph becomes rather messed up, so I tried vtkButterflySubdivisionFilter but
then the colour were all messed up. Here are a couple of screenshots, with
first being the vtkLoopSubdivisionFilter and second being the
vtkButterflySubdivisionFilter.

     <http://vtk.1045678.n5.nabble.com/file/n5724602/loopdiv.png>      
<http://vtk.1045678.n5.nabble.com/file/n5724602/butterflydiv.png>    

I searched for a workaround and came across this,
http://vtk.1045678.n5.nabble.com/Uneven-colors-after-the-vtkButterflySubdivisionFilter-td5720205.html.
I applied the workaround but it didn't seem to work, can you please tell
where I went wrong, I've displayed the code below.

int main(int, char *[])
{
        // Create a grid of points (height/terrian map)
        vtkSmartPointer<vtkPoints> points =
                vtkSmartPointer<vtkPoints>::New();

        unsigned int GridSize = 5;
        double xx, yy, zz;

        points->InsertNextPoint(0, 0, 0);
        points->InsertNextPoint(0, 1, 3);
        points->InsertNextPoint(0, 2, 1);
        points->InsertNextPoint(1, 0, 1);
        points->InsertNextPoint(1, 1, 1);
        points->InsertNextPoint(1, 2, 3);
        points->InsertNextPoint(2, 0, 1);
        points->InsertNextPoint(2, 1, 2);
        points->InsertNextPoint(2, 2, 2);
        points->InsertNextPoint(3, 0, 2);
        points->InsertNextPoint(3, 1, 0);
        points->InsertNextPoint(3, 2, 3);


        // Add the grid points to a polydata object
        vtkSmartPointer<vtkPolyData> inputPolyData =
                vtkSmartPointer<vtkPolyData>::New();
        inputPolyData->SetPoints(points);

        // Triangulate the grid points
        vtkSmartPointer<vtkDelaunay2D> delaunay =
                vtkSmartPointer<vtkDelaunay2D>::New();
#if VTK_MAJOR_VERSION <= 5
        delaunay->SetInput(inputPolyData);
#else
        delaunay->SetInputData(inputPolyData);
#endif
        delaunay->Update();
        vtkPolyData* outputPolyData = delaunay->GetOutput();

        double bounds[6];
        outputPolyData->GetBounds(bounds);

        // Find min and max z
        double minz = bounds[4];
        double maxz = bounds[5];

        std::cout << "minz: " << minz << std::endl;
        std::cout << "maxz: " << maxz << std::endl;

        // Create the color map
        vtkSmartPointer<vtkLookupTable> colorLookupTable =
                vtkSmartPointer<vtkLookupTable>::New();
        colorLookupTable->SetTableRange(minz, maxz);
        colorLookupTable->Build();

        // Generate the colors for each point based on the color map
        vtkSmartPointer<vtkUnsignedCharArray> colors =
                vtkSmartPointer<vtkUnsignedCharArray>::New();
        colors->SetNumberOfComponents(3);
        colors->SetName("Colors");

        std::cout << "There are " << outputPolyData->GetNumberOfPoints()
                << " points." << std::endl;

        for(int i = 0; i < outputPolyData->GetNumberOfPoints(); i++)
        {
                double p[3];
                outputPolyData->GetPoint(i,p);

                double dcolor[3];
                colorLookupTable->GetColor(p[2], dcolor);

                unsigned char color[3];
                for(unsigned int j = 0; j < 3; j++)
                {
                        color[j] = static_cast<unsigned char>(255.0 *
dcolor[j]);
                }
                colors->InsertNextTupleValue(color);
        }

        outputPolyData->GetPointData()->SetScalars(colors);

        // Create a mapper and actor
        vtkSmartPointer<vtkPolyDataMapper> mapper =
                vtkSmartPointer<vtkPolyDataMapper>::New();
#if VTK_MAJOR_VERSION <= 5
        mapper->SetInputConnection(outputPolyData->GetProducerPort());
#else
        mapper->SetInputData(outputPolyData);
#endif

        vtkSmartPointer<vtkCubeAxesActor> axes =
vtkSmartPointer<vtkCubeAxesActor>::New();
        axes->SetBounds(delaunay->GetOutput()->GetBounds());
        axes->SetFlyModeToOuterEdges();
        axes->XAxisMinorTickVisibilityOff();
        axes->YAxisMinorTickVisibilityOff();
        axes->ZAxisMinorTickVisibilityOff();
        axes->SetXLabelFormat("%6.1f");

        vtkSmartPointer<vtkButterflySubdivisionFilter> subdivide =
                vtkSmartPointer<vtkButterflySubdivisionFilter>::New();
        subdivide->SetNumberOfSubdivisions(3);
#if VTK_MAJOR_VERSION <= 5
        subdivide->SetInputConnection(outputPolyData->GetProducerPort());
#else
        subdivide->SetInputData(outputPolyData);
#endif
        subdivide->Update();

        // Now create rgb triples
        vtkSmartPointer<vtkUnsignedCharArray> rgbcolors =
                vtkSmartPointer<vtkUnsignedCharArray>::New();
        rgbcolors->SetNumberOfComponents(3);
        rgbcolors->SetNumberOfTuples(outputPolyData->GetNumberOfPoints());
        rgbcolors->SetName("rgbColors");
        vtkPointData *pd = subdivide->GetOutput()->GetPointData();
        vtkIdType num = subdivide->GetOutput()->GetNumberOfPoints();

        for(int i = 0; i < num; i++)
        {
                double tuple[3];
                pd->GetScalars()->GetTuple(i, tuple);

                unsigned char rgb[3];
                for(int j = 0; j < 3; j++)
                {
                        if (tuple[j] < 0)
                        {
                                rgb[j] = 0;
                        }
                        else if (tuple[j] > 255.0)
                        {
                                rgb[j] = 255.0;
                        }
                        else
                        {
                                rgb[j] = tuple[j];
                        }
                }
                rgbcolors->InsertTuple3(i, rgb[0], rgb[1], rgb[2]);
        }
        pd->SetScalars(rgbcolors);

        // Create a mapper and actor
        vtkSmartPointer<vtkPolyDataMapper> mapper2 =
                vtkSmartPointer<vtkPolyDataMapper>::New();
        mapper2->SetInputConnection(subdivide->GetOutputPort());

        vtkSmartPointer<vtkActor> actor =
                vtkSmartPointer<vtkActor>::New();
        actor->SetMapper(mapper2);

        // 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);

        //prevents the pointer option from going haywire
        vtkSmartPointer<vtkInteractorStyleTrackballCamera> style =
vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New();
        renderWindowInteractor->SetInteractorStyle(style);

        // Add the actor to the scene
        renderer->AddActor(actor);
        renderer->SetBackground(.1, .2, .3);

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

        renderer->GetActiveCamera()->Elevation(290);
        renderer->AddViewProp(axes);
        axes->SetCamera(renderer->GetActiveCamera());

        renderWindowInteractor->Start();

        return EXIT_SUCCESS;
}

I'm using vtk 5.8, I cannot change to 6.0 since the whole project is built
on 5.8. Thank you. 



--
View this message in context: http://vtk.1045678.n5.nabble.com/vtkButterflySubdivisionFilter-colour-problem-tp5724602.html
Sent from the VTK - Users mailing list archive at Nabble.com.


More information about the vtkusers mailing list