[vtkusers] coloring a line with a scalar value

Stephen mailinglists at icaril.eclipse.co.uk
Wed Aug 8 10:46:42 EDT 2007


dear all,

I have been attempting to color lines according to scalar values to no 
avail, an example of my code in Java is appended below. At the moment I 
can only get the actor to appear by setting the color on the actor, 
ideally I want the color determined by the scalar value to conserve the 
number of actors I need to create.

Does anyone know of an example that would illustrate how to do this 
properly?

regards

Stephen




import vtk.*;

public class Lines {

   public static void main( String [] args ){
       System.loadLibrary("vtkCommonJava");
       System.loadLibrary("vtkFilteringJava");
       System.loadLibrary("vtkIOJava");
       System.loadLibrary("vtkImagingJava");
       System.loadLibrary("vtkGraphicsJava");
       System.loadLibrary("vtkRenderingJava");             
       System.loadLibrary("vtkHybridJava");        
       System.loadLibrary("vtkWidgetsJava");         
       vtkRenderer ren = new vtkRenderer();
       vtkRenderWindow renWin = new vtkRenderWindow();
       renWin.AddRenderer(ren);
       vtkRenderWindowInteractor iren = new vtkRenderWindowInteractor();
       iren.SetRenderWindow(renWin);   

       //create two lines                   
       vtkLineSource ls1 = new vtkLineSource();
       vtkLineSource ls2 = new vtkLineSource();
       ls1.SetPoint1(  0, 0, 0 );
       ls1.SetPoint2(  0, 0, 10 );
       ls1.SetResolution(4);
       ls2.SetPoint1( new double[] { 0, 0, 10} );
       ls2.SetPoint2( new double[] { 10, 10, 10} );
       ls1.SetResolution(4);
       ls2.SetResolution(4);

       vtkAppendPolyData polyDataCln = new vtkAppendPolyData();
       polyDataCln.AddInput( ls1.GetOutput());
       polyDataCln.AddInput( ls2.GetOutput());


       vtkLookupTable scalarLUT = new vtkLookupTable();
       scalarLUT.Build();     
       scalarLUT.SetTableValue(1 , 1, 0, 1, 0);
       scalarLUT.SetTableValue(2 , 0, 0, 0, 1);


        vtkIntArray colors = new vtkIntArray();
       colors.SetNumberOfTuples(2); // two lines to color
       colors.SetNumberOfComponents(1);// one scalar value for each line
       colors.SetLookupTable(scalarLUT);
       colors.InsertComponent(0 , 0 , 1);//setting scalar value 1 for 
line 1
       colors.InsertComponent(1 , 0 , 2);//setting scalar value 1 for 
line 2
                     polyDataCln.GetOutput().GetPointData().SetScalars( 
colors );

       vtkPolyDataMapper glyphMapper = new vtkPolyDataMapper();
                                   glyphMapper.SetInput( 
polyDataCln.GetOutput() );
       glyphMapper.SetLookupTable( scalarLUT );
       glyphMapper.SetScalarRange( 0 , 2 );
       glyphMapper.ScalarVisibilityOn();
                    vtkActor actor = new vtkActor();
       actor.SetMapper( glyphMapper );
       actor.GetProperty().SetLineWidth( 6 );
       actor.GetProperty().SetColor( 0,0.8,0);//if this line is 
commented out nothing is visible
              ren.AddActor( actor );
              ren.ResetCamera();
       ren.GetActiveCamera().SetFocalPoint( 0 , 0, 5);
       ren.GetActiveCamera().SetPosition( 0 , 50, 5);
       ren.GetActiveCamera().ComputeViewPlaneNormal();
       ren.GetActiveCamera().SetViewUp(1,0, 0);
       ren.GetActiveCamera().OrthogonalizeViewUp();
       ren.GetActiveCamera().Azimuth(60);
       ren.GetActiveCamera().Roll(0);
       ren.GetActiveCamera().Dolly(2);
       ren.ResetCameraClippingRange();               
ren.SetBackground(1, 1, 1);
       renWin.SetSize(750, 400);

       iren.Initialize();
       renWin.Render();
       iren.Start();

   }
}




More information about the vtkusers mailing list