[vtkusers] How do I display a scalar field over a rectilinear grid using colors
David Doria
daviddoria+vtk at gmail.com
Tue Feb 16 08:08:26 EST 2010
On Mon, Feb 15, 2010 at 9:02 PM, Roland Krause <rokrau at yahoo.com> wrote:
> 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
This :
vtkSmartPointer<vtkFloatArray> xCoords = vtkFloatArray::New();
is very bad - the reference count (the whole thing that makes smart
pointers work) will be off unless you use:
vtkSmartPointer<vtkFloatArray> xCoords = vtkSmartPointer<vtkFloatArray>::New();
on all of your smart pointer initializations.
Take a look at this:
http://www.vtk.org/Wiki/VTK/Tutorials/SmartPointers
To get to your real question:
This example:
http://www.vtk.org/Wiki/VTK/Examples/Color_a_mesh_by_height
demonstrates how to color a PolyData. I've never used
vtkRectilinearGrid, but I believe the process should be identical.
Take a look at that example and see if it helps at all - if not, let
us know and we'll take a closer look.
Thanks,
David
More information about the vtkusers
mailing list