[prev in list] [next in list] [prev in thread] [next in thread] 

List:       vtkusers
Subject:    [vtkusers] How do I display a scalar field over a rectilinear grid
From:       Roland Krause <rokrau () yahoo ! com>
Date:       2010-02-16 2:02:14
Message-ID: 408433.47583.qm () web81102 ! mail ! mud ! yahoo ! com
[Download RAW message or body]

Hi all, I am relatively new to Vtk and have a rather basic question. 

I am trying to display a colored iso-surface plot of an array of scalar values given \
at the points of a rectangular grid.

Below is my code, the result of which is a white plane with the geometry of the \
object (it is a 2D grid) I am trying to visualize. My basic question is of course : \
What's wrong? What am I missing? Is there a basic example of how this should be done? \


My understanding is that I make vtkPolyData out of the vtkRectilinearGrid using the \
filter, then add the scalar array I am trying to plot over the grid as a vtkDataArray \
to the vtkPointData of this vtkPolyData. Finally assign it to be recognized as \
POINT_DATA and then plot the thing using the default lookup table (which also looks \
really wrong btw.) 

Vector<T> is a simple Vector class that has a T * operator() - and a size() function, \
nothing too special...

void SnapshotView2D::plot(const Vector<float> & xc, const Vector<float> & yc, const \
Vector<float> & zc, const Vector<float> & data) {
    const int n1=xc.size(), n2=yc.size(), n3=zc.size();

    vtkSmartPointer<vtkFloatArray> xCoords = vtkFloatArray::New();
    xCoords->SetArray(const_cast<Vector<float>&>(xc),xc.size(),1);

    vtkSmartPointer<vtkFloatArray> yCoords = vtkFloatArray::New();
    yCoords->SetArray(const_cast<Vector<float>&>(yc),yc.size(),1);

    vtkSmartPointer<vtkFloatArray> zCoords = vtkFloatArray::New();
    zCoords->SetArray(const_cast<Vector<float>&>(zc),zc.size(),1);
    
    vtkSmartPointer<vtkFloatArray> scalars = vtkFloatArray::New();
    scalars->SetArray(const_cast<Vector<float>&>(data),data.size(),1);
    scalars->SetName("scalars");

    vtkSmartPointer<vtkRectilinearGrid> rgrid = vtkRectilinearGrid::New();
    rgrid->SetDimensions(n1,n2,n3);
    rgrid->SetXCoordinates(xCoords);
    rgrid->SetYCoordinates(yCoords);
    rgrid->SetZCoordinates(zCoords);

    plane = vtkRectilinearGridGeometryFilter::New();
    plane->SetInput(rgrid);
    plane->SetExtent(0,n1-1,0,n2-1, 0,n3-1);

    vtkPolyData * planeData = plane->GetOutput();
    vtkPointData * pointData = planeData->GetPointData();
    pointData ->AddArray(scalars);
    
    vtkAssignAttribute * assign = vtkAssignAttribute::New();
    assign->SetInput(planeData);
    assign->Assign("scalars", vtkDataSetAttributes::SCALARS, \
vtkAssignAttribute::POINT_DATA);  assign->Update();

    vtkSmartPointer<vtkLookupTable> pLookupTable = \
vtkSmartPointer<vtkLookupTable>::New(); //    \
pLookupTable->SetTableRange(planeData->GetScalarRange());  \
pLookupTable->SetNumberOfTableValues(10); //    pLookupTable->SetHueRange(0.667,0.0);
//    pLookupTable->SetValueRange(0.0,1.0);

    pMapper = vtkPolyDataMapper::New();
    pMapper->SetInput(planeData);
    pMapper->ImmediateModeRenderingOn();
    pMapper->ScalarVisibilityOn();
    pMapper->SetScalarModeToUsePointData();
    pMapper->SetScalarRange(planeData->GetScalarRange());
    
    pMapper->SetLookupTable(pLookupTable); 

    // Create a scalar bar
    vtkScalarBarActor * scalarBar = vtkScalarBarActor::New();
    scalarBar->SetLookupTable(pLookupTable);
//     scalarBar SetTitle "Temperature"
//     [scalarBar GetPositionCoordinate] SetCoordinateSystemToNormalizedViewport
//     [scalarBar GetPositionCoordinate] SetValue 0.1 0.01
//     scalarBar SetOrientationToHorizontal
//     scalarBar SetWidth 0.8
//     scalarBar SetHeight 0.17

    pActor = vtkActor::New();
    pActor->SetMapper(pMapper);
//    pActor->GetProperty()->SetRepresentationToWireframe();
//    pActor->GetProperty()->SetColor(0,0,0);

    pRenderer->AddActor(scalarBar);
    pRenderer->AddActor(pActor);
    pRenderer->SetBackground(1,1,1);
    pRenderer->ResetCamera();

    pRenderWindow->Render();
    pRenderer->SetBackground(0.9,0.9,0.9); // Background color white

    vtkAxesActor * pAxes = vtkAxesActor::New();
    vtkOrientationMarkerWidget * pWidget = vtkOrientationMarkerWidget::New();
    pWidget->SetOutlineColor( 0.9300, 0.5700, 0.1300 );
    pWidget->SetOrientationMarker( pAxes );
    pWidget->SetInteractor(pRenderWindowInteractor);
    pWidget->InteractiveOff();

    // Reset camera
    pRenderer->ResetCamera();
    pRenderWindow->Render();

    pWidget->SetEnabled( 1 );
    pRenderWindowInteractor->Start();
}

I would be greatful if you could point me to an example, I had assumed that this is \
one of the most basic things one could do in Vtk. 

Best regards and many thanks
Roland
_______________________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at \
http://www.kitware.com/opensource/opensource.html

Please keep messages on-topic and check the VTK FAQ at: \
http://www.vtk.org/Wiki/VTK_FAQ

Follow this link to subscribe/unsubscribe:
http://www.vtk.org/mailman/listinfo/vtkusers


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic