[vtkusers] Problem coloring an unstructured grid by point array data

Ellen M. Price ellen.price at cfa.harvard.edu
Wed Aug 15 14:39:02 EDT 2018


Hi vtkUsers,

This seems like a simple enough task, yet I can't find any examples that
fit what I am trying to do. I have the following C++ code for a VTK
pipeline. The problem is that the output I get from rendering this
pipeline is different from that I would see in VisIt or ParaView, in
that the mesh face facing the camera is shaded solid, when I know the
data there should show a gradient; the face that initially faces away
from the camera is correctly shaded (after rotating so that it can be
seen). I can't think of any reason why this would happen. Please, if
anyone can see what I'm doing wrong, help is greatly appreciated.

Thanks,
Ellen Price


#include <vtkSmartPointer.h>
#include <vtkLookupTable.h>
#include <vtkColorSeries.h>
#include <vtkPointData.h>
#include <vtkUnstructuredGrid.h>
#include <vtkXMLUnstructuredGridReader.h>
#include <vtkDataSetMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <iostream>

#define ARRAY   "Vec_0xb5ea60_1rho.0"

int main(int argc, char *argv[])
{
    if (argc < 2)
    {
        std::cout << "Usage: " << argv[0] << " grid.vtu" << std::endl;
        return EXIT_FAILURE;
    }

    vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
        vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
    reader->SetFileName(argv[1]);
    reader->Update();

    vtkSmartPointer<vtkLookupTable> lookup =
        vtkSmartPointer<vtkLookupTable>::New();
    vtkSmartPointer<vtkColorSeries> colors =
        vtkSmartPointer<vtkColorSeries>::New();
    colors->SetColorScheme(vtkColorSeries::WILD_FLOWER);
    colors->BuildLookupTable(lookup, vtkColorSeries::ORDINAL);

    vtkSmartPointer<vtkDataSetMapper> mapper =
        vtkSmartPointer<vtkDataSetMapper>::New();
    mapper->SetInputConnection(reader->GetOutputPort());
    mapper->ScalarVisibilityOn();
    mapper->SetColorModeToMapScalars();
    mapper->SetLookupTable(lookup);
    mapper->SetScalarModeToUsePointFieldData();
    mapper->SelectColorArray(ARRAY);

    vtkPointData *pd = reader->GetOutput()->GetPointData();
    mapper->SetScalarRange(pd->GetArray(ARRAY)->GetRange());

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

    vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();

    vtkSmartPointer<vtkRenderWindow> window =
        vtkSmartPointer<vtkRenderWindow>::New();
    window->AddRenderer(renderer);

    vtkSmartPointer<vtkRenderWindowInteractor> interactor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
    interactor->SetRenderWindow(window);

    renderer->AddActor(actor);
    window->SetSize(1280, 720);

    window->Render();
    interactor->Start();

    return EXIT_SUCCESS;
}


More information about the vtkusers mailing list