[vtkusers] Please help me use vtkLinearExtrusionFilter [was Re:Trouble with vtkLinearExtrusionFilter and Coloring and vtkSplineFilter]

Christopher Bruns cmbruns at stanford.edu
Wed Jun 21 19:02:16 EDT 2006


  I am a big fan of VTK and sincerely appreciate the fantastic work that 
everyone is doing.  I am having one little problem however.

  I get a "Access violation reading location 0x00000000" when attempting 
to use vtkLinearExtrusionFilter with certain input pipelines, while I 
can get it to work with others.

  Presumably I am neglecting to populate part of the input data pipeline 
correctly.  Can someone please suggest how I might fix my error here?  
Is there some other filter I need to add?

  In my debugger the crash appears to occur at line 214 of 
vtklinearextrusionfilter.cxx in the method RequestData, which contains 
the statement "outputPD->CopyData(pd,ptId,ptId);"  The variable pd 
contains input vtkPointData.  Deeper in the stack, 
pd->Data[0]->GetVoidPointer(0) returns 0x0000000 instead of returning a 
valid pointer, because  pd->Data[0]->Array is zero as well.

  I am appending an example program in C++ (the one I posted last week 
written in Java inspired no replies).  This is the smallest program that 
I could create that shows each of the issues I am having.

  Thanks in advance for any help.  I really would like to be able to 
color extruded ribbons.

-Chris Bruns

// Example program to demonstrate the nature of vtkLinearExtrusionFilter
// when combined with vtkSplineFilter and/or color data

#include "vtkPolyDataMapper.h"
#include "vtkActor.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkLookupTable.h"
#include "vtkFloatArray.h"
#include "vtkCellArray.h"
#include "vtkInteractorStyleSwitch.h"
#include "vtkPolyData.h"
#include "vtkPointData.h"
#include "vtkSplineFilter.h"
#include "vtkRibbonFilter.h"
#include "vtkLinearExtrusionFilter.h"

int main() {
    // To prevent core dump, either
    // A: turn off both coloring and smoothing, OR
    // B: turn off extrusion
    bool colorShape = true;
    bool smoothPath = true;
    bool extrudeShape = true;

    // Create a three-point line, with normals and color scalars

    // Color table
    vtkLookupTable *lut = vtkLookupTable::New();
    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);
    lut->SetTableValue(whiteColor, 1.0, 1.0, 1.0, 1.0);
    lut->SetTableValue(blueColor, 0.0, 0.0, 1.0, 1.0);

    // Points, normals, and colors
    vtkPoints *linePoints = vtkPoints::New();
    vtkFloatArray *lineNormals = vtkFloatArray::New();
    lineNormals->SetNumberOfComponents(3);
    vtkFloatArray *lineColors = vtkFloatArray::New();
    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 = vtkCellArray::New();
    lineCells->InsertNextCell(numberOfPoints);
    for (int i = 0; i < numberOfPoints; i ++)
        lineCells->InsertCellPoint(i);       

    // Create a proper vtkPolyData object from the line
    vtkPolyData *lineData = vtkPolyData::New();
    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 = vtkSplineFilter::New();
    splineFilter->SetInput(lineData);
    splineFilter->SetMaximumNumberOfSubdivisions(20);
   
    // Widen the line into a ribbon
    vtkRibbonFilter *ribbonFilter = vtkRibbonFilter::New();
    ribbonFilter->SetWidth(0.4);
    ribbonFilter->SetAngle(0); // Perpendicular to normals
    if (smoothPath)
        ribbonFilter->SetInput(splineFilter->GetOutput()); // use spline
    else
        ribbonFilter->SetInput(lineData); // skip spline

    // Extrude ribbon to create thickness
    vtkLinearExtrusionFilter *ribbonThicknessFilter = 
vtkLinearExtrusionFilter::New();
    ribbonThicknessFilter->SetCapping(1);
    ribbonThicknessFilter->SetExtrusionTypeToNormalExtrusion();
    ribbonThicknessFilter->SetScaleFactor(0.1);
    ribbonThicknessFilter->SetInput(ribbonFilter->GetOutput());
    // ribbonThicknessFilter->Update(); // Uncomment to cause crash early

    vtkPolyDataMapper *lineMapper = vtkPolyDataMapper::New();
    lineMapper->SetLookupTable(lut);
    lineMapper->SetScalarRange(0.0, lut->GetNumberOfTableValues() - 
1.0);      
    if (extrudeShape) {
        
lineMapper->SetInput(ribbonThicknessFilter->GetOutput());           
    }
    else {
        lineMapper->SetInput(ribbonFilter->GetOutput()); // ribbon -- works
    }

    // Actor
    vtkActor *lineActor = vtkActor::New();
    lineActor->SetMapper(lineMapper);

    // Renderer
    vtkRenderer *ren1 = vtkRenderer::New();
    vtkRenderWindow *renWin = vtkRenderWindow::New();
    renWin->AddRenderer(ren1);

    // Trackball interaction by default
    vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
    vtkInteractorStyleSwitch *trackBall = vtkInteractorStyleSwitch::New();
    trackBall->SetCurrentStyleToTrackballCamera();
    iren->SetInteractorStyle(trackBall);

    // Window
    iren->SetRenderWindow(renWin);
    ren1->SetBackground(1.0,1.0,1.0); // white background
    ren1->AddActor(lineActor);
    renWin->SetSize(800, 600);
    renWin->Render();
    iren->Start();

    // Clean up data structures
    lineMapper->Delete();
    ren1->Delete();
    renWin->Delete();
    iren->Delete();
    lineCells->Delete();
    lineNormals->Delete();
    lineColors->Delete();
    linePoints->Delete();
    lut->Delete();
    trackBall->Delete();
    lineData->Delete();
    splineFilter->Delete();
    ribbonFilter->Delete();
    ribbonThicknessFilter->Delete();

    return 0;
}





More information about the vtkusers mailing list