[vtkusers] How do I display a scalar field over a rectilinear grid using colors
Roland Krause
rokrau at yahoo.com
Mon Feb 15 21:02:14 EST 2010
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
More information about the vtkusers
mailing list