[vtkusers] The proper way to return vtkPolyData from a function?

Alex Southern mrapsouthern at gmail.com
Mon Jul 2 06:28:40 EDT 2012


Hi,

Ive tried numerous things to achieve this but however i do it the result 
is the same as that produced by the code below. The vtkPolydata 
complete_model is always empty, despite the line in the class member 
function, output = triFilter->GetOutput(); containing the correct output 
vtkPolyData.

The issue I suspect is related to going out of scope but, no run time 
errors are being thrown. The render window is just empty.

Thanks
Alex

include <vtkSmartPointer.h>
#include <vtkActor.h>
#include <vtkCubeSource.h>
#include <vtkPolyData.h>
#include <vtkCellArray.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkPointData.h>
#include <vtkDelaunay2D.h>

using namespace std;

class getPolyData {
     public:

         void make_vtkPolyData(vtkPolyData *output)
         {
             vtkSmartPointer<vtkPoints> pts = 
vtkSmartPointer<vtkPoints>::New();
             vtkSmartPointer<vtkCellArray> cellArray = 
vtkSmartPointer<vtkCellArray>::New();
             vtkSmartPointer<vtkDelaunay2D> triFilter = 
vtkSmartPointer<vtkDelaunay2D>::New();
             vtkSmartPointer<vtkPolyData> pd1 = 
vtkSmartPointer<vtkPolyData>::New();

             // Generate a concave polygon that is NOT flat (in one plane)
             pts->InsertNextPoint( 1.00, 2.48, 0.00 );
             pts->InsertNextPoint( 1.00, 0.00, 0.00 );
             pts->InsertNextPoint( 1.00, 1.24, 0.29 );
             pts->InsertNextPoint( 0.00, 0.00, 0.00 );
             pts->InsertNextPoint( 0.00, 2.48, 0.00 );
             int p[5] = {0, 1, 2, 3, 4};
             cellArray->InsertNextCell(5,p);

             pd1->SetPoints( pts );
             pd1->SetPolys( cellArray );

             triFilter->SetInput( pd1 );
             triFilter->Update();
             output = triFilter->GetOutput();


             /*writer->SetFileName("g:\\testwrite.vtk");
             writer->SetFileTypeToASCII();
             writer->SetInputConnection( tmp );*/
         }
};

int main()
{

     vtkSmartPointer<vtkPolyData> complete_model = 
vtkSmartPointer<vtkPolyData>::New();

     getPolyData getvtk;
     getvtk.make_vtkPolyData( complete_model );

     //  Create a renderer, render window, and interactor
     vtkSmartPointer<vtkRenderer> renderer = 
vtkSmartPointer<vtkRenderer>::New();
     vtkSmartPointer<vtkRenderWindow> renderWindow = 
vtkSmartPointer<vtkRenderWindow>::New();
     vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = 
vtkSmartPointer<vtkRenderWindowInteractor>::New();

     renderWindow->AddRenderer(renderer);
     renderWindowInteractor->SetRenderWindow(renderWindow);

     // map filtered data
     vtkSmartPointer<vtkPolyDataMapper> mapper = 
vtkSmartPointer<vtkPolyDataMapper>::New();
     vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();

     mapper->SetInput( complete_model );  // change to pd1 to see bad 
polygon
     actor->SetMapper( mapper );
     renderer->AddActor( actor );

     renderer->SetBackground(.5, .1, .1);

     //// Render and interact
     renderWindow->Render();
     renderWindowInteractor->Start();

     return 0;
}



More information about the vtkusers mailing list