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

Jean-Do Barnichon jeando.barnichon at free.fr
Mon Mar 24 08:44:22 EDT 2008


ok,
got it working now!
The mistake was just that I forgot to assign the ScalarPointArray to the 
point data of the polydata.
Below is the full test code, just for the record.
J-D


////////////////////////////////////////////////////////////////////
// Simple C++ test for vtkRibbon
//
#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"
#include "vtkPointData.h"

int main()
{
    double minScalar = 1;
    double maxScalar = 10;

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

    
/////////////////////////////////////////////////////////////////////////////
    // 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
    
/////////////////////////////////////////////////////////////////////////////
    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 *ScalarPointArray = vtkFloatArray::New();
    ScalarPointArray->Initialize();
    ScalarPointArray->SetName("ScalarPointArray");
    ScalarPointArray->SetNumberOfComponents(1);
    ScalarPointArray->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);
        ScalarPointArray->SetValue(i,i+1);
    }

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

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

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

    // Then an actor
    vtkActor *TrajActor = vtkActor::New();
    TrajActor->SetMapper(TrajMapper);
    (TrajActor->GetProperty())->SetLineWidth(4);

    
/////////////////////////////////////////////////////////////////////////////
    // 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(TrajActor);
    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();
    TrajMapper->Delete();
    TrajActor->Delete();
    rf->Delete();
    ren->Delete();
    renWin->Delete();
    iren->Delete();

    return 0;
}

Jean-Do Barnichon a écrit :
> Hi Vidyadhar,
> I just added
>    SetValueRange(1,1)
> to the code, but it does not change anything.
> Actually, there is nothing wrong with the lookuptable, it 's somewhere 
> else, obviously.
> Thanks anyway
> J-D
>
> Vidyadhar a écrit :
>> Hi,
>> Apart from Hue and Saturation range, shouldn't you also be setting 
>> the Value range (SetValueRange(double,double).
>> HTH
>> Vidyadhar
>> ----- Original Message ----- From: "Jean-Do Barnichon" 
>> <jeando.barnichon at free.fr>
>> To: <vtkusers at vtk.org>
>> Sent: Saturday, March 22, 2008 5:44 PM
>> Subject: [vtkusers] How to use vtkRibbonFilter to show a scalar field
>>
>>
>>> 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
>>> ...
>>>
>>> _______________________________________________
>>> This is the private VTK discussion list.
>>> Please keep messages on-topic. Check the FAQ at: 
>>> http://www.vtk.org/Wiki/VTK_FAQ
>>> Follow this link to subscribe/unsubscribe:
>>> http://www.vtk.org/mailman/listinfo/vtkusers 
>>
>>
>>
>
>




More information about the vtkusers mailing list