[vtkusers] Trouble with vtkLinearExtrusionFilter and Coloring and vtkSplineFilter

Christopher Bruns cmbruns at stanford.edu
Wed Jun 14 19:12:42 EDT 2006


Please help.  I am having some trouble creating a graphics object in vtk 
the way I want to. 

For molecular visualization I create a thick ribbon that follows a path 
through space.  I am trying to compose a pipeline as follows:

(points/normals/colors)->vtkSplineFilter->vtkRibbonFilter->vtkLinearExtrusionFilter

Unfortunately, this only works if I exclude the vtkSplineFilter and also 
exclude the color scalars.

Without color and without spline smoothing I can create a nice 
monochromatic thick ribbon.

If I stop at the vtkRibbonFilter, I can get a nice colored 
zero-thickness smooth splined ribbon.  But I want it thick.

When vtkLinearExtrusionFilter is combined with either vtkSplineFilter or 
color scalars, my (Java) program crashes with a reference to one of the 
vtk native libraries.

I can work around the first problem (vtkSplineFilter vs. 
vtkLinearExtrusionFilter) by using vtkCardinalSpline to compute my own 
interpolated positions and normals.  This way I can get a nice thick and 
smooth ribbon, but which still has no internal coloring.

I experience this same situation with vtk versions 4.4 and 5.0.  I have 
not tried other versions.

What can I do to get internally colored extruded ribbons?

--Chris Bruns


What follows is a test program to help clarify my summary:

/*
 * Created on Jun 14, 2006
 * Original author: Christopher Bruns
 */
package org.simtk.moleculargraphics.cartoon;

import vtk.*;

import org.simtk.moleculargraphics.VTKLibraries;
import javax.swing.JFrame;

/**
 * 
  * @author Christopher Bruns
  *
  * Test program to demonstrate problem coloring extruded ribbon with 
scalars
 */
public class TestColoredRibbon {
    public static void main(String[] args) {
        VTKLibraries.load(); // (my custom native library loader)

        // Program crashes or hangs with the extruded object when either 
colors are enabled,
        // or the spline smoothing filter is used.

//        boolean extrudeShape = true;  // if true, color and smooth 
must be false
//        boolean smoothPath = false; // if true, extrudeShape must be 
false, or else hang
//        boolean colorShape = false; // if true, extrudeShape must be 
false, or else crash
       
        boolean extrudeShape = false;  // if true, color and smooth must 
be false
        boolean smoothPath = true; // if true, extrudeShape must be 
false, or else hang
        boolean colorShape = true; // if true, extrudeShape must be 
false, or else crash

        // Color lookup table
        vtkLookupTable lut = new vtkLookupTable();
        int numberOfColors = 3;
        lut.SetNumberOfTableValues(numberOfColors);
        lut.Build();
        int redColor = 0;
        int whiteColor = 1;
        int blueColor = 2;
        lut.SetTableValue(redColor, 1.0, 0.0, 0.0, 1.0); // red
        lut.SetTableValue(whiteColor, 1.0, 1.0, 1.0, 1.0); // white
        lut.SetTableValue(blueColor, 0.0, 0.0, 1.0, 1.0); // blue

        // Data structures for points, normals, and colors
        vtkPoints linePoints = new vtkPoints();
        vtkFloatArray lineNormals = new vtkFloatArray();
        lineNormals.SetNumberOfComponents(3);
        vtkFloatArray lineColors = new vtkFloatArray();
        lineColors.SetNumberOfComponents(1);
       
        // Create a line with three points and a slight kink
        // Point 1
        linePoints.InsertNextPoint(0, 0, 0);
        lineNormals.InsertNextTuple3(-0.44, 0.90, 0);
        lineColors.InsertNextValue(redColor);
       
        // Point 2
        linePoints.InsertNextPoint(1, 0.1, 0);
        lineNormals.InsertNextTuple3(0, 1, 0);
        lineColors.InsertNextValue(whiteColor);
       
        // Point 3
        linePoints.InsertNextPoint(2, 0, 0);
        lineNormals.InsertNextTuple3(0.44, 0.90, 0.0);
        lineColors.InsertNextValue(blueColor);
       
        // Connect points into a line
        int numberOfPoints = linePoints.GetNumberOfPoints();
        vtkCellArray lineCells = new vtkCellArray();
        lineCells.InsertNextCell(numberOfPoints);
        for (int i = 0; i < numberOfPoints; i ++)
            lineCells.InsertCellPoint(i);
       
        // Create a proper vtkPolyData object from the line
        vtkPolyData lineData = new vtkPolyData();
        lineData.SetPoints(linePoints);
        lineData.GetPointData().SetNormals(lineNormals);
        // Coloring by adding scalars causes crash with extruded shape
        if (colorShape) lineData.GetPointData().SetScalars(lineColors);
        lineData.SetLines(lineCells);       

        // Smooth the line with a spline function
        vtkSplineFilter splineFilter = new vtkSplineFilter();
        splineFilter.SetInput(lineData);
        splineFilter.SetMaximumNumberOfSubdivisions(7);
       
        // Widen the line into a ribbon
        vtkRibbonFilter ribbonFilter = new vtkRibbonFilter();
        ribbonFilter.SetWidth(0.4);
        ribbonFilter.SetAngle(0); // Perpendicular to normals
        if (smoothPath)
            ribbonFilter.SetInput(splineFilter.GetOutput()); // use spline
        else
            ribbonFilter.SetInput(lineData); // skip spline

        vtkLinearExtrusionFilter ribbonThicknessFilter = new 
vtkLinearExtrusionFilter();
        ribbonThicknessFilter.SetCapping(1);
        ribbonThicknessFilter.SetExtrusionTypeToNormalExtrusion();
        ribbonThicknessFilter.SetScaleFactor(0.1);
        ribbonThicknessFilter.SetInput(ribbonFilter.GetOutput());

        // The stupid mapper needs to be explicitly told the range of 
the color lookup table
        vtkPolyDataMapper mapper = new vtkPolyDataMapper();       
        mapper.SetLookupTable(lut);
        mapper.SetScalarRange(0.0, lut.GetNumberOfTableValues() - 
1.0);       
       
        // Rendering before extrusion works, after extrusion does not
        // (unless coloring and smoothing are not attempted)
        // mapper.SetInput(lineData); // simple line -- works
        // mapper.SetInput(splineFilter.GetOutput()); // smoothed line 
-- works
        // mapper.SetInput(cleanFilter.GetOutput()); // cleaned line -- 
works
        if (extrudeShape) {
            // thick ribbon
            //  -- crashes if scalars are present
            //  -- hangs if spline filter is included
            mapper.SetInput(ribbonThicknessFilter.GetOutput());           
        }
        else {
            mapper.SetInput(ribbonFilter.GetOutput()); // ribbon -- works
        }

        vtkActor actor = new vtkActor();
        actor.SetMapper(mapper);

        // VTK rendering window
        vtkPanel panel = new vtkPanel();
        panel.GetRenderer().SetBackground(1,1,1); // white backgrounds rule
        panel.GetRenderer().AddProp(actor);
       
        // Java swing window to hold the rendering window
        JFrame frame = new JFrame("Test extruded strip color");
        frame.getContentPane().add(panel);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }
}




More information about the vtkusers mailing list