[vtkusers] How to use vtkRibbonFilter to show a scalar field

Jean-Do Barnichon jeando.barnichon at free.fr
Sun Mar 23 08:00:50 EDT 2008


well,

I worked a bit more on the problem, so I wrote a simplified code sample 
to check more easily what was going on. I initially thought the ribbon 
was not displayed at all, but that was wrong... actually it is displayed 
in white, that is without taking into account the scalar field values.
So my question is :
how to make the ribbon color as a function of the scalar field known at 
the points?
Any idea where the problem might come from in the code sample given below?

Thanks in advance for helping
J-D


////////////////////////////////////////////////////////////////////
// Simple C++ test for vtkRibbon
//
#include "vtkPolyDataWriter.h"
#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkProperty.h"
#include "vtkCamera.h"
#include "vtkPolydata.h"
#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkLookupTable.h"
#include "vtkTextProperty.h"
#include "vtkScalarBarActor.h"
#include "vtkRibbonFilter.h"
#include "vtkFloatArray.h"

int main()
{
    
/////////////////////////////////////////////////////////////////////////////
    // Create a lookup table that consists of the full hue circle (from 
HSV).
    
/////////////////////////////////////////////////////////////////////////////
    vtkLookupTable *hueLut = vtkLookupTable::New();
    hueLut->SetHueRange (160./240., 0.);
    hueLut->SetSaturationRange (1, 1);
    double minScalar = 1;
    double maxScalar = 10;
    hueLut->SetTableRange (minScalar, maxScalar);
    hueLut->Build();


    
/////////////////////////////////////////////////////////////////////////////
    // Create a text property scalar bar
    
/////////////////////////////////////////////////////////////////////////////
    vtkTextProperty *tprop = vtkTextProperty::New();
    tprop->SetColor(0, 0, 0);
    tprop->SetFontSize(12);
    tprop->SetFontFamilyToArial();
    tprop->ItalicOn();
    tprop->ShadowOn();


    
/////////////////////////////////////////////////////////////////////////////
    // Create a ScalarBarActor as interaction does not work for 
ScalarBarWidget
    
/////////////////////////////////////////////////////////////////////////////
    vtkScalarBarActor *scalarBar = vtkScalarBarActor::New();
    scalarBar->SetTitle("Scalar value");
    scalarBar->SetLookupTable(hueLut);
    scalarBar->SetLabelTextProperty(tprop);
    scalarBar->SetTitleTextProperty(tprop);
    scalarBar->SetHeight(0.5);
    scalarBar->SetWidth(0.1);
    scalarBar->SetPosition(0.01,0.20);
    scalarBar->SetPosition2(0.1,0.80);


    
/////////////////////////////////////////////////////////////////////////////
    // Add a trajectory in the vertical (x-z) polyplane
    
/////////////////////////////////////////////////////////////////////////////
    int npt = 10; // number of points in the trajectory
    vtkFloatArray *ScalarArray = vtkFloatArray::New();
    ScalarArray->Initialize();
    ScalarArray->SetName("ScalarArray");
    ScalarArray->SetNumberOfComponents(1);
    ScalarArray->SetNumberOfValues(npt);

    // Create data for the trajectory
    vtkPoints* trajpoints  = vtkPoints::New(VTK_DOUBLE);
    vtkCellArray* trajpolyline  = vtkCellArray::New();
    trajpolyline->InsertNextCell(npt);

    // Insert points in the trajectory
    for (int i=0; i<npt; i++) {
        trajpoints->InsertPoint(i,i,1.0,sqrt((float)i));
        trajpolyline->InsertCellPoint(i);
        ScalarArray->SetValue(i,i+1);
    }

    // Create the 2D Profile for the trajectory
    vtkPolyData *trajPD = vtkPolyData::New();
    trajPD->SetPoints(trajpoints);
    trajPD->SetLines(trajpolyline);

    // Create the filter
    vtkRibbonFilter *rf = vtkRibbonFilter::New();
    rf->SetInput(trajPD);
    rf->SetWidth(0.1);
    rf->SetWidthFactor(5);

    // Add a mapper
    vtkPolyDataMapper *RibonMapper = vtkPolyDataMapper::New();
    RibonMapper->SetInputConnection(rf->GetOutputPort());
    RibonMapper->SetScalarRange(minScalar, maxScalar);
    RibonMapper->SetLookupTable(hueLut);
    RibonMapper->ScalarVisibilityOn();
    RibonMapper->SelectColorArray("ScalarArray");
    RibonMapper->SetScalarModeToUsePointFieldData();
    RibonMapper->SetColorModeToMapScalars();

    // Then an actor
    vtkActor *RibonActor = vtkActor::New();
    RibonActor->SetMapper(RibonMapper);
    (RibonActor->GetProperty())->SetInterpolationToFlat();

  // Create the graphics structure.
  vtkRenderer *ren = vtkRenderer::New();
  vtkRenderWindow *renWin = vtkRenderWindow::New();

  renWin->AddRenderer(ren);
  renWin->SetSize(600, 600);

  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
  iren->SetRenderWindow(renWin);

  // Add the actors to the renderer, set the background and size
  ren->AddActor(scalarBar);
  ren->AddActor(RibonActor);
  ren->SetBackground(0.1, 0.2, 0.4);

  // We'll zoom in a little by accessing the camera
  ren->GetActiveCamera()->ParallelProjectionOn();
  ren->GetActiveCamera()->SetFocalPoint(0,0,0);
  ren->GetActiveCamera()->SetPosition(0,-1,0);
  ren->GetActiveCamera()->SetViewUp(0,0,1);
  ren->ResetCamera();
  renWin->Render();
 
  // This starts the event loop and as a side effect causes an initial 
render.
  iren->Start();

  // Exiting from here, we have to delete all the instances that have 
been created.
  hueLut->Delete();
  tprop->Delete();
  scalarBar->Delete();
  trajpoints->Delete();
  trajpolyline->Delete();
  RibonMapper->Delete();
  RibonActor->Delete();
  rf->Delete();
  ren->Delete();
  renWin->Delete();
  iren->Delete();

  return 0;
}



Jean-Do Barnichon a écrit :
> Hello all,
>
> I want to display a scalar field along a polyline, by making the color 
> of each line segment depending on some color scale defined through a 
> vtkLookupTable.
> Actually, the scalars are known at the polyline nodes, and theses 
> scalars are stored in a vtkFloatArray with 1 component.
> Well the thing is that I do not get the ribbon in the display, so I 
> guess I'm probably doing something wrong.
> Note that if I set a given color for the whole actor (obviously it's 
> not really what I want, but just for testing) by uncommenting the last 
> line of the code snippet given below :
>    RibonActor->GetProperty()->SetColor(1.,0.,0.);
> it works in the sense that the ribbon is displayed in full red.
>
> Any idea where the problem might come from in my code ?
> Thanks,
> J-D
>
> Code Snippet removed




More information about the vtkusers mailing list